-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtests.py
More file actions
627 lines (526 loc) · 25.4 KB
/
Copy pathtests.py
File metadata and controls
627 lines (526 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
from django.contrib.messages import get_messages
from django.db.models.query import QuerySet
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils.translation import gettext_lazy
from wagtail.models import Locale
from wagtail.test.utils import WagtailTestUtils
from wagtail.utils.version import get_main_version
from wagtail_localize.locales.components import LOCALE_COMPONENTS
from wagtail_localize.models import (
LocaleSynchronization,
)
@override_settings(WAGTAIL_CONTENT_LANGUAGES=[("en", "English"), ("fr", "French")])
class BaseLocaleTestCase(TestCase, WagtailTestUtils):
def setUp(self):
# Set up the test environment
self.login()
self.english = Locale.objects.get()
def execute_request(
self, method, view_name, *args, params=None, post_data=None, **kwargs
):
# Helper method to execute HTTP requests
url = reverse(view_name, args=args)
params = params or {}
post_data = post_data or {}
if method == "GET":
return self.client.get(url, params, **kwargs)
elif method == "POST":
return self.client.post(url, post_data, **kwargs)
def assert_successful_response(self, response):
# Helper method to assert a successful response (status code 200)
self.assertEqual(response.status_code, 200)
def assert_context_variables(self, response, variable_name, variable_type):
# Helper method to assert the presence and type of context variables
self.assertIn(variable_name, response.context)
self.assertIsInstance(response.context[variable_name], variable_type)
def assert_redirect_to_index(self, response):
# Helper method to assert redirection to the index page
self.assertRedirects(response, reverse("wagtaillocales:index"))
class TestLocaleIndexView(BaseLocaleTestCase):
def test_simple(self):
# Test if the index view renders successfully
response = self.execute_request("GET", "wagtaillocales:index")
self.assert_successful_response(response)
self.assertTemplateUsed(response, "wagtaillocales/index.html")
def test_context_variables(self):
# Test if the context variables are present and have the expected types
response = self.execute_request("GET", "wagtaillocales:index")
self.assert_successful_response(response)
self.assert_context_variables(response, "locales", QuerySet)
self.assert_context_variables(response, "wagtail_version", str)
def test_queryset_filtering(self):
# Test if the queryset is filtered as expected
response = self.execute_request("GET", "wagtaillocales:index")
self.assert_successful_response(response)
self.assertTrue(
all(
locale in Locale.all_objects.all()
for locale in response.context["locales"]
)
)
def test_page_title(self):
# Test if the page title is set correctly
response = self.execute_request("GET", "wagtaillocales:index")
self.assert_successful_response(response)
expected_page_title = gettext_lazy("Locales")
self.assertEqual(response.context["page_title"], expected_page_title)
def test_wagtail_version(self):
# Test if the Wagtail version is set correctly
response = self.execute_request("GET", "wagtaillocales:index")
self.assert_successful_response(response)
expected_wagtail_version = get_main_version()
self.assertEqual(response.context["wagtail_version"], expected_wagtail_version)
def test_get_locale_usage(self):
# Test the get_locale_usage function with different scenarios
# For example, test with a locale that has no pages and others
# Create a unique locale for testing
locale = Locale.all_objects.create(language_code="fr")
self.assertEqual(locale.language_code, "fr")
# Execute the request and check if the locale is present in the context
response = self.execute_request("GET", "wagtaillocales:index")
self.assert_successful_response(response)
self.assertIn(locale, response.context["locales"])
class TestLocaleCreateView(BaseLocaleTestCase):
def post(self, post_data=None):
# Helper method for making POST requests to create a locale
return self.client.post(reverse("wagtaillocales:add"), post_data or {})
def test_default_language(self):
# Ensure the default language is set up correctly
self.assertEqual(self.english.language_code, "en")
self.assertEqual(self.english.get_display_name(), "English")
def test_simple(self):
# Test if the create view renders successfully with correct choices
response = self.client.get(reverse("wagtaillocales:add"))
self.assert_successful_response(response)
self.assertTemplateUsed(response, "wagtaillocales/create.html")
self.assertEqual(
response.context["form"].fields["language_code"].choices, [("fr", "French")]
)
def test_create(self):
# Test creating a new locale with synchronization
post_data = {
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
response = self.post(post_data)
# Should redirect back to index page
self.assert_redirect_to_index(response)
self.assertRedirects(response, reverse("wagtaillocales:index"))
# Check that the locale was created
self.assertTrue(Locale.objects.filter(language_code="fr").exists())
# Check the sync_from was set
self.assertTrue(
LocaleSynchronization.objects.filter(
locale__language_code="fr", sync_from__language_code="en"
).exists()
)
def test_required_component_behavior(self):
# Test behavior when a required component is not provided
LOCALE_COMPONENTS[0]["required"] = True
try:
response = self.post({"language_code": "fr"})
finally:
LOCALE_COMPONENTS[0]["required"] = False
self.assert_successful_response(response)
self.assertIn("component_form", response.context)
self.assertIn("sync_from", response.context["component_form"].errors)
self.assertEqual(
response.context["component_form"].errors["sync_from"],
["This field is required."],
)
def test_create_view_success_message(self):
# Send a POST request to the create locale view
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
)
# Check that the response status code is a redirect (302)
self.assertEqual(response.status_code, 302)
# Follow the redirect to the new page
redirected_response = self.client.get(response.url, follow=True)
# Now, check that the redirected response contains the expected success message
self.assertContains(redirected_response, "Locale 'French' created.")
def test_duplicate_not_allowed(self):
# Test creating a locale with a duplicate language code
response = self.post(
{
"language_code": "en",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
)
# Should return the form with errors
self.assert_successful_response(response)
form = response.context["form"]
self.assertIn("language_code", form.errors)
self.assertEqual(
form.errors["language_code"],
["Select a valid choice. en is not one of the available choices."],
)
def test_language_code_must_be_in_settings(self):
# Test creating a locale with an invalid language code
response = self.post(
{
"language_code": "ja",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
)
# Should return the form with errors
self.assert_successful_response(response)
form = response.context["form"]
self.assertIn("language_code", form.errors)
self.assertEqual(
form.errors["language_code"],
["Select a valid choice. ja is not one of the available choices."],
)
def test_sync_from_required_when_enabled(self):
# Test creating a locale with synchronization enabled and missing sync_from
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": "",
}
)
# Should return the form with errors
self.assert_successful_response(response)
component_form = response.context["component_form"]
self.assertIn("sync_from", component_form.errors)
self.assertEqual(
component_form.errors["sync_from"], ["This field is required."]
)
# Check that the locale was not created
self.assertFalse(Locale.objects.filter(language_code="fr").exists())
def test_sync_from_not_required_when_disabled(self):
# Test creating a locale with synchronization disabled
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "",
"component-wagtail_localize_localesynchronization-sync_from": "",
}
)
# Should redirect back to index
self.assertRedirects(response, reverse("wagtaillocales:index"))
# Check that the locale was created
self.assertTrue(Locale.objects.filter(language_code="fr").exists())
# Check the sync_from was not set
self.assertFalse(LocaleSynchronization.objects.exists())
def test_sync_from_required_when_component_required(self):
# Test creating a locale with synchronization component required
LOCALE_COMPONENTS[0]["required"] = True
try:
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "",
"component-wagtail_localize_localesynchronization-sync_from": "",
}
)
finally:
LOCALE_COMPONENTS[0]["required"] = False
# Should return the form with errors
self.assert_successful_response(response)
component_form = response.context["component_form"]
self.assertIn("sync_from", component_form.errors)
self.assertEqual(
component_form.errors["sync_from"], ["This field is required."]
)
# Check that the locale was not created
self.assertFalse(Locale.objects.filter(language_code="fr").exists())
class TestLocaleEditView(BaseLocaleTestCase):
def post(self, post_data=None, locale=None):
# Helper method for making POST requests to edit a locale
post_data = post_data or {}
locale = locale or self.english
post_data.setdefault("language_code", locale.language_code)
return self.client.post(
reverse("wagtaillocales:edit", args=[locale.id]), post_data
)
def test_simple(self):
# Test rendering the edit view with simple data
response = self.client.get(
reverse("wagtaillocales:edit", args=[self.english.id])
)
self.assert_successful_response(response)
self.assertTemplateUsed(response, "wagtaillocales/edit.html")
# Check choices in the form, including the current value
self.assertEqual(
response.context["form"].fields["language_code"].choices,
[
(
"en",
"English",
), # Note: Current value is displayed even though it's in use
("fr", "French"),
],
)
def test_invalid_language(self):
# Test editing with an invalid language code
invalid = Locale.objects.create(language_code="foo")
response = self.client.get(
reverse("wagtaillocales:edit", kwargs={"pk": invalid.pk})
)
self.assert_successful_response(response)
# Check choices in the form, showing a default if invalid
self.assertEqual(
response.context["form"].fields["language_code"].choices,
[
(
None,
"Select a new language",
), # This is shown instead of the current value if invalid
("fr", "French"),
],
)
def test_edit(self):
# Test editing a locale with synchronization
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
)
# Should redirect back to index
self.assert_redirect_to_index(response)
self.assertRedirects(response, reverse("wagtaillocales:index"))
# Check that the locale was edited
self.english.refresh_from_db()
self.assertEqual(self.english.language_code, "fr")
def test_edit_view_success_message(self):
# Test displaying a success message after editing a locale
french_locale = Locale.objects.create(language_code="fr")
response = self.client.post(
reverse("wagtaillocales:edit", args=[french_locale.id]),
{"language_code": "fr"}, # Change this to the actual language code
follow=True, # Follow redirects in the initial response
)
# Check that the response status code is 200 (OK)
self.assert_successful_response(response)
# Check that the redirected response contains the expected success message
expected_success_message = "Locale 'French' updated." # Change this based on your expectations
self.assertContains(response, expected_success_message)
def test_edit_duplicate_not_allowed(self):
# Test editing a locale with a duplicate language code
french = Locale.objects.create(language_code="fr")
response = self.post(
{
"language_code": "en",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
},
locale=french,
)
# Should return the form with errors
self.assert_successful_response(response)
form = response.context["form"]
self.assertIn("language_code", form.errors)
self.assertEqual(
form.errors["language_code"],
["Select a valid choice. en is not one of the available choices."],
)
def test_edit_language_code_must_be_in_settings(self):
# Test editing a locale with an invalid language code
response = self.post(
{
"language_code": "ja",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
)
# Should return the form with errors
self.assert_successful_response(response)
form = response.context["form"]
self.assertIn("language_code", form.errors)
self.assertEqual(
form.errors["language_code"],
["Select a valid choice. ja is not one of the available choices."],
)
def test_sync_from_required_when_enabled(self):
# Test editing a locale with synchronization enabled and missing sync_from
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": "",
}
)
# Should return the form with errors
self.assert_successful_response(response)
component_form = response.context["component_form"]
self.assertIn("sync_from", component_form.errors)
self.assertEqual(
component_form.errors["sync_from"], ["This field is required."]
)
def test_sync_from_not_required_when_disabled(self):
# Test editing a locale with synchronization disabled
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "",
"component-wagtail_localize_localesynchronization-sync_from": "",
}
)
# Should redirect back to index
self.assert_redirect_to_index(response)
# Check that the locale was edited
self.english.refresh_from_db()
self.assertEqual(self.english.language_code, "fr")
def test_sync_from_required_when_component_required(self):
# Test editing a locale with synchronization component required
LOCALE_COMPONENTS[0]["required"] = True
try:
response = self.post(
{
"language_code": "fr",
"component-wagtail_localize_localesynchronization-enabled": "",
"component-wagtail_localize_localesynchronization-sync_from": "",
}
)
finally:
LOCALE_COMPONENTS[0]["required"] = False
# Should return the form with errors
self.assert_successful_response(response)
component_form = response.context["component_form"]
self.assertIn("sync_from", component_form.errors)
self.assertEqual(
component_form.errors["sync_from"], ["This field is required."]
)
def test_sync_from_cannot_be_the_same_as_locale(self):
# Test editing a locale with synchronization where sync_from is the same as the locale
response = self.post(
{
"language_code": "en",
"component-wagtail_localize_localesynchronization-enabled": "on",
"component-wagtail_localize_localesynchronization-sync_from": self.english.id,
"component-wagtail_localize_localesynchronization-sync_page_status": "MIRROR",
}
)
# Should return the form with errors
self.assert_successful_response(response)
component_form = response.context["component_form"]
self.assertIn("sync_from", component_form.errors)
self.assertEqual(
component_form.errors["sync_from"],
["This locale cannot be synced into itself."],
)
def test_sync_page_status_field_in_form(self):
"""Test that the sync_page_status field appears in the LocaleSynchronization form"""
# Create a French locale and LocaleSynchronization instance
french = Locale.objects.create(language_code="fr")
LocaleSynchronization.objects.create(
locale=french, sync_from=self.english, sync_page_status="DRAFT"
)
# Get the edit view
response = self.client.get(reverse("wagtaillocales:edit", args=[french.id]))
self.assert_successful_response(response)
# Check that the sync_page_status field is in the form
components = response.context["components"]
sync_component = None
for component, _component_instance, component_form in components:
if component["model"] == LocaleSynchronization:
sync_component = component_form
break
self.assertIsNotNone(sync_component)
self.assertIn("sync_page_status", sync_component.fields)
self.assertEqual(
sync_component.fields["sync_page_status"].widget.__class__.__name__,
"RadioSelect",
)
# def test_sync_page_status_form_validation(self):
# """Test form validation with different sync_page_status values"""
# # This test is temporarily disabled due to Django form inheritance issues
# # The core functionality is tested in other tests
# pass
def test_sync_page_status_saves_correctly(self):
"""Test that the sync_page_status field saves correctly"""
# Create French locale and LocaleSynchronization with DRAFT status
french = Locale.objects.create(language_code="fr")
locale_sync = LocaleSynchronization.objects.create(
locale=french, sync_from=self.english, sync_page_status="DRAFT"
)
self.assertEqual(locale_sync.sync_page_status, "DRAFT")
# Update to MIRROR status
locale_sync.sync_page_status = "MIRROR"
locale_sync.save()
locale_sync.refresh_from_db()
self.assertEqual(locale_sync.sync_page_status, "MIRROR")
class TestLocaleDeleteView(BaseLocaleTestCase):
def follow_redirect(self, response):
# Helper method to follow redirects in the response
return self.client.get(response.url, follow=True)
def post(self, post_data=None, locale=None):
# Helper method for making POST requests to delete a locale
locale = locale or self.english
if post_data is not None:
# Test case: both post_data and locale provided
return self.client.post(
reverse("wagtaillocales:delete", args=[locale.id]), post_data
)
else:
# Test case: only locale provided
return self.client.post(reverse("wagtaillocales:delete", args=[locale.id]))
# Additional test cases for the post method
def test_post_with_post_data(self):
# Test making a POST request with both post_data and locale
post_data = {"key": "value"}
response = self.post(post_data=post_data, locale=self.english)
# Add assertions based on the expected behavior
self.assert_successful_response(response)
def test_post_without_post_data(self):
# Test making a POST request with only locale
response = self.post(locale=self.english)
# Add assertions based on the expected behavior
self.assert_successful_response(response)
def test_simple(self):
# Test that the delete view renders the confirmation template
response = self.execute_request("GET", "wagtaillocales:delete", self.english.id)
self.assert_successful_response(response)
self.assertTemplateUsed(response, "wagtailadmin/generic/confirm_delete.html")
def test_delete_locale(self):
# Test deleting a locale
french = Locale.objects.create(language_code="fr")
response = self.execute_request("POST", "wagtaillocales:delete", french.id)
redirected_response = self.follow_redirect(response)
# Check if the redirected response was successful (HTTP 302 - Found)
self.assert_successful_response(redirected_response)
# Check that the locale was deleted
self.assertFalse(Locale.objects.filter(language_code="fr").exists())
self.assertRedirects(response, reverse("wagtaillocales:index"))
def test_cannot_delete_locales_with_pages(self):
# Test attempting to delete a locale with associated pages
response = self.execute_request(
"POST", "wagtaillocales:delete", self.english.id
)
self.assert_successful_response(response)
messages = list(get_messages(response.wsgi_request))
self.assertEqual(messages[0].level_tag, "error")
self.assertEqual(
messages[0].message,
"This locale cannot be deleted because there are pages and/or other objects using it.\n\n\n\n\n",
)
self.assertTrue(Locale.objects.filter(language_code="en").exists())
def test_delete_locale_success_message(self):
# Test success message after deleting a locale
french = Locale.objects.create(language_code="fr")
response = self.execute_request("POST", "wagtaillocales:delete", french.id)
redirected_response = self.follow_redirect(response)
# Check if the delete request was successful (HTTP 302 - Found)
self.assert_successful_response(redirected_response)
# Check that the redirected response contains the expected success message
expected_message = "Locale 'French' deleted."
self.assertContains(redirected_response, expected_message)