Exemplo n.º 1
0
 def test_03_multilingual_middleware_get_lang_from_request(self):
     class Mock:
         pass
     
     middle = MultilingualURLMiddleware()
     
     
     with SettingsOverride(CMS_LANGUAGES = {'klingon':'Klingon'}):
         request = Mock()
         setattr(request, 'session', {})
         setattr(request,'path_info', '/en/whatever')
         setattr(request,'path', '/en/whatever')
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'en')
     
         setattr(request,'path_info', 'whatever')
         setattr(request,'path', 'whatever')
         setattr(request,'session', {'django_language':'klingon'})
         setattr(request,'COOKIES', {})
         setattr(request,'META', {})
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'klingon') # the session's language. Nerd.
         
         request = Mock()
         setattr(request,'path_info', 'whatever')
         setattr(request,'path', 'whatever')
         setattr(request,'COOKIES', {'django_language':'klingon'})
         setattr(request,'META', {})
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'klingon') # the cookies language.
         
         # Now the following should revert to the default language (en)
         setattr(request,'COOKIES', {'django_language':'elvish'})
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'en') # The default
Exemplo n.º 2
0
    def test_14_multilingual_urls_point_to_original_language_when_not_redirecting(
            self):
        """
        When using the fallback language for a request, the replaced urls point to
        the requsted, instead of the fallback language.
        """
        from cms.views import details
        middleware = MultilingualURLMiddleware()
        requested_language = settings.LANGUAGES[1][0]
        fallback_language = settings.LANGUAGES[0][0]

        test_settings = dict(CMS_TEMPLATES=((TEMPLATE_NAME, ''), ),
                             CMS_LANGUAGE_FALLBACK='no_redirect')

        with SettingsOverride(**test_settings):
            instance, plugin_class = CMSPlugin.objects.all(
            )[0].get_plugin_instance()
            instance.body = '<a href="/slug/">fnord</a>'
            instance.save()
            request = self.get_request(path="/fr/renderingtestcase-slug ",
                                       language=requested_language)
            middleware.process_request(request)
            response = details(request, slug=self.test_page.get_slug())
            response = middleware.process_response(request, response)
            self.assertContains(response, '/%s/slug' % requested_language)
            self.assertNotContains(response, '/%s/slug' % fallback_language)
Exemplo n.º 3
0
    def test_multilingual_middleware_handles_redirections(self):

        middle = MultilingualURLMiddleware()

        request = AttributeObject(
            session={},
            path_info='whatever',
            path='whatever',
            COOKIES={
                'django_language': 'en',
            },
            META = {},
            LANGUAGE_CODE = 'en'
        )
        
        # Don't re-prefix
        response = middle.process_response(request,HttpResponseRedirect('/en/some-path/'))
        self.assertTrue(response['Location'] == '/en/some-path/')

        response = middle.process_response(request,HttpResponseRedirect('%ssome-path/'%settings.MEDIA_URL))
        self.assertTrue(response['Location'] == '%ssome-path/' %settings.MEDIA_URL)

        response = middle.process_response(request,HttpResponseRedirect('%ssome-path/'%settings.STATIC_URL))
        self.assertTrue(response['Location'] == '%ssome-path/' %settings.STATIC_URL)
        

        # Prefix
        response = middle.process_response(request,HttpResponseRedirect('/xx/some-path/'))
        self.assertTrue(response['Location'] == '/en/xx/some-path/')
Exemplo n.º 4
0
    def test_form_multilingual(self):
        """
        Tests for correct form URL mangling
        """
        language = 'en'
        pages_root = self.get_pages_root()
        middle = MultilingualURLMiddleware()
        request = self.get_request(pages_root,language=language)

        html = '<form action="%sfoo/bar/"> </form>' % pages_root
        response = middle.process_response(request,HttpResponse(html))
        self.assertContains(response,'/%s%sfoo/bar/' % (language,pages_root))
        self.assertContains(response,'/en/content/foo/bar/')
Exemplo n.º 5
0
    def test_form_multilingual(self):
        """
        Tests for correct form URL mangling
        """
        language = 'en'
        pages_root = self.get_pages_root()
        middle = MultilingualURLMiddleware()
        request = self.get_request(pages_root, language=language)

        html = '<form action="%sfoo/bar/"> </form>' % pages_root
        response = middle.process_response(request, HttpResponse(html))
        self.assertContains(response, '/%s%sfoo/bar/' % (language, pages_root))
        self.assertContains(response, '/en/content/foo/bar/')
Exemplo n.º 6
0
 def test_multilingual_middleware_get_lang_from_request(self):
     
     middle = MultilingualURLMiddleware()
     
     FRENCH = 'fr'
     ELVISH = 'x-elvish'
     
     with SettingsOverride(CMS_LANGUAGES=((FRENCH, 'French'),("it", "Italian")), CMS_FRONTEND_LANGUAGES=(FRENCH,)):
         request = AttributeObject(
             session={},
             path_info='/it/whatever',
             path='/it/whatever',
             COOKIES={},
             META={},
         )
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'en')#falls back to default
         
         
         request = AttributeObject(
             session={
                 'django_language': FRENCH,
             },
             path_info='whatever',
             path='whatever',
             COOKIES={},
             META={},
         )
         result = middle.get_language_from_request(request)
         self.assertEqual(result, FRENCH) # the session's language.
         
         
         request = AttributeObject(
             path_info='whatever',
             path='whatever',
             COOKIES={
                 'django_language': FRENCH,
             },
             META={},
         )
         result = middle.get_language_from_request(request)
         self.assertEqual(result, FRENCH) # the cookies language.
         
         # Now the following should revert to the default language (en)
         request.COOKIES['django_language'] = ELVISH
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'en') # The default
Exemplo n.º 7
0
 def test_multilingual_middleware_get_lang_from_request(self):
     
     middle = MultilingualURLMiddleware()
     
     KLINGON = 'x-klingon'
     ELVISH = 'x-elvish'
     
     with SettingsOverride(CMS_LANGUAGES=((KLINGON, 'Klingon'),)):
         request = AttributeObject(
             session={},
             path_info='/en/whatever',
             path='/en/whatever'
         )
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'en')
         
         
         request = AttributeObject(
             session={
                 'django_language': KLINGON,
             },
             path_info='whatever',
             path='whatever',
             COOKIES={},
             META={},
         )
         result = middle.get_language_from_request(request)
         self.assertEqual(result, KLINGON) # the session's language. Nerd.
         
         
         request = AttributeObject(
             path_info='whatever',
             path='whatever',
             COOKIES={
                 'django_language': KLINGON,
             },
             META={},
         )
         result = middle.get_language_from_request(request)
         self.assertEqual(result, KLINGON) # the cookies language.
         
         # Now the following should revert to the default language (en)
         request.COOKIES['django_language'] = ELVISH
         result = middle.get_language_from_request(request)
         self.assertEqual(result, 'en') # The default
Exemplo n.º 8
0
    def test_multilingual_middleware_ignores_static_url(self):

        middle = MultilingualURLMiddleware()
        KLINGON = 'x-klingon'

        with SettingsOverride(CMS_LANGUAGES=((KLINGON, 'Klingon'), )):
            request = AttributeObject(session={},
                                      path_info='whatever',
                                      path='whatever',
                                      COOKIES={
                                          'django_language': KLINGON,
                                      },
                                      META={},
                                      LANGUAGE_CODE=KLINGON)
            html = """<ul>
                <li><a href="/some-page/">some page</a></li>
                <li><a href="%simages/some-media-file.jpg">some media file</a></li>
                <li><a href="%simages/some-static-file.jpg">some static file</a></li>
                <li><a href="%simages/some-admin-file.jpg">some admin media file</a></li>
                <li><a href="%simages/some-other-file.jpg">some static file</a></li>
                </ul>""" % (
                settings.MEDIA_URL,
                settings.STATIC_URL,
                settings.ADMIN_MEDIA_PREFIX,
                '/some-path/',
            )

            response = middle.process_response(request, HttpResponse(html))

            # These paths shall be prefixed
            self.assertTrue('href="/%s/some-page/' %
                            KLINGON in response.content)
            self.assertTrue('href="/%s%simages/some-other-file.jpg' %
                            (KLINGON, '/some-path/') in response.content)

            # These shall not
            self.assertTrue('href="%simages/some-media-file.jpg' %
                            settings.MEDIA_URL in response.content)
            self.assertTrue('href="%simages/some-static-file.jpg' %
                            settings.STATIC_URL in response.content)
            self.assertTrue('href="%simages/some-admin-file.jpg' %
                            settings.ADMIN_MEDIA_PREFIX in response.content)
Exemplo n.º 9
0
    def test_03_multilingual_middleware_get_lang_from_request(self):
        class Mock:
            pass

        middle = MultilingualURLMiddleware()

        with SettingsOverride(CMS_LANGUAGES={'klingon': 'Klingon'}):
            request = Mock()
            setattr(request, 'session', {})
            setattr(request, 'path_info', '/en/whatever')
            setattr(request, 'path', '/en/whatever')
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'en')

            setattr(request, 'path_info', 'whatever')
            setattr(request, 'path', 'whatever')
            setattr(request, 'session', {'django_language': 'klingon'})
            setattr(request, 'COOKIES', {})
            setattr(request, 'META', {})
            result = middle.get_language_from_request(request)
            self.assertEqual(result,
                             'klingon')  # the session's language. Nerd.

            request = Mock()
            setattr(request, 'path_info', 'whatever')
            setattr(request, 'path', 'whatever')
            setattr(request, 'COOKIES', {'django_language': 'klingon'})
            setattr(request, 'META', {})
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'klingon')  # the cookies language.

            # Now the following should revert to the default language (en)
            setattr(request, 'COOKIES', {'django_language': 'elvish'})
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'en')  # The default
Exemplo n.º 10
0
    def test_multilingual_middleware_handles_redirections(self):

        middle = MultilingualURLMiddleware()

        request = AttributeObject(session={},
                                  path_info='whatever',
                                  path='whatever',
                                  COOKIES={
                                      'django_language': 'en',
                                  },
                                  META={},
                                  LANGUAGE_CODE='en')

        # Don't re-prefix
        response = middle.process_response(
            request, HttpResponseRedirect('/en/some-path/'))
        self.assertTrue(response['Location'] == '/en/some-path/')

        response = middle.process_response(
            request, HttpResponseRedirect('%ssome-path/' % settings.MEDIA_URL))
        self.assertTrue(response['Location'] == '%ssome-path/' %
                        settings.MEDIA_URL)

        response = middle.process_response(
            request,
            HttpResponseRedirect('%ssome-path/' % settings.STATIC_URL))
        self.assertTrue(response['Location'] == '%ssome-path/' %
                        settings.STATIC_URL)

        # Prefix
        response = middle.process_response(
            request, HttpResponseRedirect('/xx/some-path/'))
        self.assertTrue(response['Location'] == '/en/xx/some-path/')
Exemplo n.º 11
0
    def test_multilingual_middleware_ignores_static_url(self):

        middle = MultilingualURLMiddleware()
        FRENCH = 'x-FRENCH'
        
        with SettingsOverride(CMS_LANGUAGES=((FRENCH, 'FRENCH'),)):
            request = AttributeObject(
                session={},
                path_info='whatever',
                path='whatever',
                COOKIES={
                    'django_language': FRENCH,
                },
                META = {},
                LANGUAGE_CODE = FRENCH
            )
            html = """<ul>
                <li><a href="/some-page/">some page</a></li>
                <li><a href="%simages/some-media-file.jpg">some media file</a></li>
                <li><a href="%simages/some-static-file.jpg">some static file</a></li>
                <li><a href="%simages/some-admin-file.jpg">some admin media file</a></li>
                <li><a href="%simages/some-other-file.jpg">some static file</a></li>
                </ul>""" %(
                    settings.MEDIA_URL,
                    settings.STATIC_URL,
                    admin_static_url(),
                    '/some-path/',
                )
            
            response = middle.process_response(request,HttpResponse(html))
            
            # These paths shall be prefixed
            self.assertTrue('href="/%s/some-page/' %FRENCH in response.content)
            self.assertTrue('href="/%s%simages/some-other-file.jpg' %(FRENCH, '/some-path/') in response.content)

            # These shall not
            self.assertTrue('href="%simages/some-media-file.jpg' %settings.MEDIA_URL in response.content)
            self.assertTrue('href="%simages/some-static-file.jpg' %settings.STATIC_URL in response.content)            
            self.assertTrue('href="%simages/some-admin-file.jpg' %admin_static_url() in response.content)
Exemplo n.º 12
0
    def test_14_multilingual_urls_point_to_original_language_when_not_redirecting(self):
        """
        When using the fallback language for a request, the replaced urls point to
        the requsted, instead of the fallback language.
        """
        from cms.views import details

        middleware = MultilingualURLMiddleware()
        requested_language = settings.LANGUAGES[1][0]
        fallback_language = settings.LANGUAGES[0][0]

        test_settings = dict(CMS_TEMPLATES=((TEMPLATE_NAME, ""),), CMS_LANGUAGE_FALLBACK="no_redirect")

        with SettingsOverride(**test_settings):
            instance, plugin_class = CMSPlugin.objects.all()[0].get_plugin_instance()
            instance.body = '<a href="/slug/">fnord</a>'
            instance.save()
            request = self.get_request(path="/fr/renderingtestcase-slug ", language=requested_language)
            middleware.process_request(request)
            response = details(request, slug=self.test_page.get_slug())
            response = middleware.process_response(request, response)
            self.assertContains(response, "/%s/slug" % requested_language)
            self.assertNotContains(response, "/%s/slug" % fallback_language)
Exemplo n.º 13
0
    def test_multilingual_middleware_get_lang_from_request(self):

        middle = MultilingualURLMiddleware()

        FRENCH = 'fr'
        ELVISH = 'x-elvish'

        with SettingsOverride(CMS_LANGUAGES=((FRENCH, 'French'), ("it",
                                                                  "Italian")),
                              CMS_FRONTEND_LANGUAGES=(FRENCH, )):
            request = AttributeObject(
                session={},
                path_info='/it/whatever',
                path='/it/whatever',
                COOKIES={},
                META={},
            )
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'en')  #falls back to default

            request = AttributeObject(
                session={
                    'django_language': FRENCH,
                },
                path_info='whatever',
                path='whatever',
                COOKIES={},
                META={},
            )
            result = middle.get_language_from_request(request)
            self.assertEqual(result, FRENCH)  # the session's language.

            request = AttributeObject(
                path_info='whatever',
                path='whatever',
                COOKIES={
                    'django_language': FRENCH,
                },
                META={},
            )
            result = middle.get_language_from_request(request)
            self.assertEqual(result, FRENCH)  # the cookies language.

            # Now the following should revert to the default language (en)
            request.COOKIES['django_language'] = ELVISH
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'en')  # The default
Exemplo n.º 14
0
    def test_multilingual_middleware_get_lang_from_request(self):

        middle = MultilingualURLMiddleware()

        KLINGON = 'x-klingon'
        ELVISH = 'x-elvish'

        with SettingsOverride(CMS_LANGUAGES=((KLINGON, 'Klingon'), )):
            request = AttributeObject(session={},
                                      path_info='/en/whatever',
                                      path='/en/whatever')
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'en')

            request = AttributeObject(
                session={
                    'django_language': KLINGON,
                },
                path_info='whatever',
                path='whatever',
                COOKIES={},
                META={},
            )
            result = middle.get_language_from_request(request)
            self.assertEqual(result, KLINGON)  # the session's language. Nerd.

            request = AttributeObject(
                path_info='whatever',
                path='whatever',
                COOKIES={
                    'django_language': KLINGON,
                },
                META={},
            )
            result = middle.get_language_from_request(request)
            self.assertEqual(result, KLINGON)  # the cookies language.

            # Now the following should revert to the default language (en)
            request.COOKIES['django_language'] = ELVISH
            result = middle.get_language_from_request(request)
            self.assertEqual(result, 'en')  # The default