|
| 1 | +import unittest |
| 2 | + |
1 | 3 | from django.contrib.contenttypes.models import ContentType
|
2 | 4 | from django.test import TestCase
|
3 | 5 | from django.urls import reverse
|
| 6 | +from wagtail import VERSION as WAGTAIL_VERSION |
4 | 7 | from wagtail.core.models import Locale, Page
|
5 | 8 | from wagtail.tests.utils import WagtailTestUtils
|
6 | 9 |
|
|
9 | 12 | from wagtail_localize.test.models import TestHomePage, TestPage
|
10 | 13 |
|
11 | 14 |
|
| 15 | +try: |
| 16 | + from wagtail import hooks |
| 17 | +except ImportError: |
| 18 | + from wagtail.core import hooks |
| 19 | + |
| 20 | + |
12 | 21 | class TestPageIndex(TestCase):
|
13 | 22 | def setUp(self):
|
14 | 23 | self.en_locale = Locale.objects.get(language_code="en")
|
@@ -93,7 +102,7 @@ def test_from_database(self):
|
93 | 102 | self.assertEqual(canadaonlypage_entry.aliased_locales, [])
|
94 | 103 |
|
95 | 104 |
|
96 |
| -class TestSignalsAndHooks(TestCase, WagtailTestUtils): |
| 105 | +class SyncTreeTestsSetupBase(TestCase): |
97 | 106 | def setUp(self):
|
98 | 107 | self.en_locale = Locale.objects.get(language_code="en")
|
99 | 108 | self.fr_locale = Locale.objects.create(language_code="fr")
|
@@ -126,6 +135,11 @@ def setUp(self):
|
126 | 135 | )
|
127 | 136 | )
|
128 | 137 |
|
| 138 | + |
| 139 | +class TestSignalsAndHooks(SyncTreeTestsSetupBase, WagtailTestUtils): |
| 140 | + def setUp(self): |
| 141 | + super().setUp() |
| 142 | + |
129 | 143 | LocaleSynchronization.objects.create(
|
130 | 144 | locale=self.fr_locale,
|
131 | 145 | sync_from=self.en_locale,
|
@@ -263,3 +277,64 @@ def test_create_homepage_in_sync_source_locale(self):
|
263 | 277 | self.assertTrue(new_en_homepage.has_translation(self.fr_locale))
|
264 | 278 | self.assertTrue(new_en_homepage.has_translation(self.fr_ca_locale))
|
265 | 279 | self.assertTrue(new_en_homepage.has_translation(self.es_locale))
|
| 280 | + |
| 281 | + |
| 282 | +@unittest.skipUnless( |
| 283 | + WAGTAIL_VERSION >= (3, 0), |
| 284 | + "construct_synced_page_tree_list was added starting with Wagtail 3.0", |
| 285 | +) |
| 286 | +class TestConstructSyncedPageTreeListHook(SyncTreeTestsSetupBase): |
| 287 | + def _get_hook_function(self): |
| 288 | + the_hooks = hooks.get_hooks("construct_synced_page_tree_list") |
| 289 | + return the_hooks[0] |
| 290 | + |
| 291 | + def setup_locale_synchronisation(self, locale, sync_from_locale): |
| 292 | + LocaleSynchronization.objects.create( |
| 293 | + locale=locale, |
| 294 | + sync_from=sync_from_locale, |
| 295 | + ) |
| 296 | + |
| 297 | + def test_hook(self): |
| 298 | + the_hooks = hooks.get_hooks("construct_synced_page_tree_list") |
| 299 | + self.assertEqual(len(the_hooks), 1) |
| 300 | + |
| 301 | + def test_hook_returns_nothing_without_locale_synchronisation(self): |
| 302 | + hook = self._get_hook_function() |
| 303 | + for action in ["unpublish", "delete", "move"]: |
| 304 | + with self.subTest(f"Calling construct_synced_page_tree_list with {action}"): |
| 305 | + results = hook([self.en_aboutpage], action) |
| 306 | + self.assertDictEqual(results, {}) |
| 307 | + |
| 308 | + def test_hook_returns_relevant_pages_from_synced_locale_on_unpublish_action(self): |
| 309 | + self.setup_locale_synchronisation(self.fr_locale, self.en_locale) |
| 310 | + hook = self._get_hook_function() |
| 311 | + results = hook([self.en_aboutpage], "unpublish") |
| 312 | + self.assertIsNotNone(results.get(self.en_aboutpage)) |
| 313 | + self.assertQuerysetEqual( |
| 314 | + results[self.en_aboutpage], Page.objects.filter(pk=self.fr_aboutpage.pk) |
| 315 | + ) |
| 316 | + |
| 317 | + # unpublish should not include alias pages as they follow the parent |
| 318 | + self.fr_aboutpage.alias_of = self.en_aboutpage |
| 319 | + self.fr_aboutpage.save() |
| 320 | + results = hook([self.en_aboutpage], "unpublish") |
| 321 | + self.assertIsNotNone(results.get(self.en_aboutpage)) |
| 322 | + self.assertQuerysetEqual(results[self.en_aboutpage], Page.objects.none()) |
| 323 | + |
| 324 | + def test_hook_returns_relevant_pages_from_synced_locale_on_move_action(self): |
| 325 | + self.setup_locale_synchronisation(self.fr_locale, self.en_locale) |
| 326 | + hook = self._get_hook_function() |
| 327 | + results = hook([self.en_aboutpage], "move") |
| 328 | + self.assertIsNotNone(results.get(self.en_aboutpage)) |
| 329 | + self.assertQuerysetEqual( |
| 330 | + results[self.en_aboutpage], Page.objects.filter(pk=self.fr_aboutpage.pk) |
| 331 | + ) |
| 332 | + |
| 333 | + def test_hook_returns_relevant_pages_from_synced_locale_on_delete_action(self): |
| 334 | + self.setup_locale_synchronisation(self.fr_locale, self.en_locale) |
| 335 | + hook = self._get_hook_function() |
| 336 | + results = hook([self.en_aboutpage], "move") |
| 337 | + self.assertIsNotNone(results.get(self.en_aboutpage)) |
| 338 | + self.assertQuerysetEqual( |
| 339 | + results[self.en_aboutpage], Page.objects.filter(pk=self.fr_aboutpage.pk) |
| 340 | + ) |
0 commit comments