|
1
|
|
|
from __future__ import unicode_literals |
|
2
|
|
|
import django |
|
3
|
|
|
from django.core.urlresolvers import reverse, resolve |
|
4
|
|
|
from django.test import RequestFactory |
|
5
|
|
|
from django.utils import translation |
|
6
|
|
|
from parler.templatetags.parler_tags import get_translated_url |
|
7
|
|
|
from .utils import AppTestCase |
|
8
|
|
|
from .testapp.models import ArticleSlugModel |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class UrlTests(AppTestCase): |
|
12
|
|
|
""" |
|
13
|
|
|
Test model construction |
|
14
|
|
|
""" |
|
15
|
|
|
urls = 'parler.tests.testapp.urls' |
|
16
|
|
|
|
|
17
|
|
|
@classmethod |
|
18
|
|
|
def setUpClass(cls): |
|
19
|
|
|
super(UrlTests, cls).setUpClass() |
|
20
|
|
|
article = ArticleSlugModel(_current_language=cls.conf_fallback, slug='default') |
|
21
|
|
|
article.set_current_language(cls.other_lang1) |
|
22
|
|
|
article.slug = 'lang1' |
|
23
|
|
|
article.set_current_language(cls.other_lang2) |
|
24
|
|
|
article.slug = 'lang2' |
|
25
|
|
|
article.save() |
|
26
|
|
|
cls.article = article |
|
27
|
|
|
|
|
28
|
|
|
def test_init_data(self): |
|
29
|
|
|
""" |
|
30
|
|
|
Test whether the model is properly stored. |
|
31
|
|
|
""" |
|
32
|
|
|
self.assertEqual(self.article.safe_translation_getter('slug', language_code=self.conf_fallback), 'default') |
|
33
|
|
|
self.assertEqual(self.article.safe_translation_getter('slug', language_code=self.other_lang1), 'lang1') |
|
34
|
|
|
self.assertEqual(self.article.safe_translation_getter('slug', language_code=self.other_lang2), 'lang2') |
|
35
|
|
|
|
|
36
|
|
|
def test_get_absolute_url(self): |
|
37
|
|
|
""" |
|
38
|
|
|
Test whether the absolute URL values are correct. |
|
39
|
|
|
""" |
|
40
|
|
|
self.article.set_current_language(self.conf_fallback) |
|
41
|
|
|
self.assertEqual(self.article.get_absolute_url(), '/{0}/article/default/'.format(self.conf_fallback)) |
|
42
|
|
|
|
|
43
|
|
|
# Switching gives the proper URL prefix too because switch_translation(self) is applied. |
|
44
|
|
|
self.article.set_current_language(self.other_lang1) |
|
45
|
|
|
self.assertEqual(self.article.get_absolute_url(), '/{0}/article/lang1/'.format(self.other_lang1)) |
|
46
|
|
|
|
|
47
|
|
|
self.article.set_current_language(self.other_lang2) |
|
48
|
|
|
self.assertEqual(self.article.get_absolute_url(), '/{0}/article/lang2/'.format(self.other_lang2)) |
|
49
|
|
|
|
|
50
|
|
|
def test_get_translated_url(self): |
|
51
|
|
|
""" |
|
52
|
|
|
Test whether get_translated_url works properly in templates. |
|
53
|
|
|
""" |
|
54
|
|
|
# Pretend there is a request on /af/article-lang1/ |
|
55
|
|
|
with translation.override(self.other_lang1): |
|
56
|
|
|
self.article.set_current_language(self.other_lang1) |
|
57
|
|
|
context = { |
|
58
|
|
|
'request': RequestFactory().get('/{0}/article/lang1/'.format(self.other_lang1)), |
|
59
|
|
|
'object': self.article |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
# Simulate {% get_translated_url CODE object %} syntax. |
|
63
|
|
|
# The object.get_absolute_url() will be used to get a translated URL. |
|
64
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.other_lang2), '/{0}/article/lang2/'.format(self.other_lang2)) |
|
65
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.conf_fallback), '/{0}/article/default/'.format(self.conf_fallback)) |
|
66
|
|
|
|
|
67
|
|
|
def test_get_translated_url_view_kwargs(self): |
|
68
|
|
|
""" |
|
69
|
|
|
Test that get_translated_url can handle view kwargs. |
|
70
|
|
|
""" |
|
71
|
|
|
with translation.override(self.other_lang1): |
|
72
|
|
|
url = reverse('view-kwargs-test-view') |
|
73
|
|
|
self.assertEqual(url, '/{0}/tests/kwargs-view/'.format(self.other_lang1)) |
|
74
|
|
|
|
|
75
|
|
|
context = { |
|
76
|
|
|
'request': RequestFactory().get(url), |
|
77
|
|
|
} |
|
78
|
|
|
context['request'].resolver_match = resolve(url) # Simulate WSGIHandler.get_response() |
|
79
|
|
|
|
|
80
|
|
|
# Simulate {% get_translated_url CODE %} syntax |
|
81
|
|
|
# The request.resolver_match will be used to get a translated URL. |
|
82
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.other_lang2), '/{0}/tests/kwargs-view/'.format(self.other_lang2)) |
|
83
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.conf_fallback), '/{0}/tests/kwargs-view/'.format(self.conf_fallback)) |
|
84
|
|
|
|
|
85
|
|
|
def test_get_translated_url_query_string(self): |
|
86
|
|
|
""" |
|
87
|
|
|
Test that the querystring is copied to the translated URL. |
|
88
|
|
|
""" |
|
89
|
|
|
# Pretend there is a request on /af/article-lang1/ |
|
90
|
|
|
with translation.override(self.other_lang1): |
|
91
|
|
|
self.article.set_current_language(self.other_lang1) |
|
92
|
|
|
context = { |
|
93
|
|
|
'request': RequestFactory().get('/{0}/article/lang1/'.format(self.other_lang1), { |
|
94
|
|
|
'next': '/fr/propri\xe9t\xe9/add/' |
|
95
|
|
|
}), |
|
96
|
|
|
'object': self.article |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
# Simulate {% get_translated_url CODE object %} syntax. |
|
100
|
|
|
# The object.get_absolute_url() will be used to get a translated URL. |
|
101
|
|
|
added_qs = "?next=%2Ffr%2Fpropri%C3%A9t%C3%A9%2Fadd%2F" |
|
102
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.other_lang2), '/{0}/article/lang2/{1}'.format(self.other_lang2, added_qs)) |
|
103
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.conf_fallback), '/{0}/article/default/{1}'.format(self.conf_fallback, added_qs)) |
|
104
|
|
|
|
|
105
|
|
|
# If the object is passed explicitly, it's likely not the current page. |
|
106
|
|
|
# Hence the querystring will not be copied in this case. |
|
107
|
|
|
self.assertEqual(get_translated_url(context, lang_code=self.other_lang2, object=self.article), '/{0}/article/lang2/'.format(self.other_lang2)) |
|
108
|
|
|
|
|
109
|
|
|
def test_translatable_slug_mixin(self): |
|
110
|
|
|
""" |
|
111
|
|
|
Test whether translated slugs are properly resolved. |
|
112
|
|
|
""" |
|
113
|
|
|
# Try calls on regular translated views first |
|
114
|
|
|
with translation.override(self.other_lang1): # This simulates LocaleMiddleware |
|
115
|
|
|
response = self.client.get('/{0}/article/lang1/'.format(self.other_lang1)) |
|
116
|
|
|
self.assertContains(response, 'view: lang1') |
|
117
|
|
|
|
|
118
|
|
|
with translation.override(self.other_lang2): |
|
119
|
|
|
response = self.client.get('/{0}/article/lang2/'.format(self.other_lang2)) |
|
120
|
|
|
self.assertContains(response, 'view: lang2') |
|
121
|
|
|
|
|
122
|
|
|
def test_translatable_slug_mixin_redirect(self): |
|
123
|
|
|
""" |
|
124
|
|
|
Test whether calling a translated URL by their fallback causes a redirect. |
|
125
|
|
|
""" |
|
126
|
|
|
# Try call on the default slug (which is resolvable), although there is a translated version. |
|
127
|
|
|
with translation.override(self.other_lang2): |
|
128
|
|
|
response = self.client.get('/{0}/article/default/'.format(self.other_lang2)) |
|
129
|
|
|
if django.VERSION >= (1, 9): |
|
130
|
|
|
self.assertRedirects(response, '/{0}/article/lang2/'.format(self.other_lang2), status_code=301) |
|
131
|
|
|
elif django.VERSION >= (1, 7): |
|
132
|
|
|
self.assertRedirects(response, 'http://testserver/{0}/article/lang2/'.format(self.other_lang2), status_code=301) |
|
133
|
|
|
else: |
|
134
|
|
|
self.assertEqual(response.status_code, 301, "Unexpected response, got: {0}, expected 301".format(response.content)) |
|
135
|
|
|
self.assertEqual(response['Location'], 'http://testserver/{0}/article/lang2/'.format(self.other_lang2)) |
|
136
|
|
|
|