Skip to content

Commit 0f47e29

Browse files
committed
Add tests
1 parent 8f88742 commit 0f47e29

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

wagtail_localize/tests/test_synctree.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import unittest
2+
13
from django.contrib.contenttypes.models import ContentType
24
from django.test import TestCase
35
from django.urls import reverse
6+
from wagtail import VERSION as WAGTAIL_VERSION
47
from wagtail.core.models import Locale, Page
58
from wagtail.tests.utils import WagtailTestUtils
69

@@ -9,6 +12,12 @@
912
from wagtail_localize.test.models import TestHomePage, TestPage
1013

1114

15+
try:
16+
from wagtail import hooks
17+
except ImportError:
18+
from wagtail.core import hooks
19+
20+
1221
class TestPageIndex(TestCase):
1322
def setUp(self):
1423
self.en_locale = Locale.objects.get(language_code="en")
@@ -93,7 +102,7 @@ def test_from_database(self):
93102
self.assertEqual(canadaonlypage_entry.aliased_locales, [])
94103

95104

96-
class TestSignalsAndHooks(TestCase, WagtailTestUtils):
105+
class SyncTreeTestsSetupBase(TestCase):
97106
def setUp(self):
98107
self.en_locale = Locale.objects.get(language_code="en")
99108
self.fr_locale = Locale.objects.create(language_code="fr")
@@ -126,6 +135,11 @@ def setUp(self):
126135
)
127136
)
128137

138+
139+
class TestSignalsAndHooks(SyncTreeTestsSetupBase, WagtailTestUtils):
140+
def setUp(self):
141+
super().setUp()
142+
129143
LocaleSynchronization.objects.create(
130144
locale=self.fr_locale,
131145
sync_from=self.en_locale,
@@ -263,3 +277,64 @@ def test_create_homepage_in_sync_source_locale(self):
263277
self.assertTrue(new_en_homepage.has_translation(self.fr_locale))
264278
self.assertTrue(new_en_homepage.has_translation(self.fr_ca_locale))
265279
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+
)

wagtail_localize/wagtail_hooks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,11 @@ def register_icons(icons):
469469
return icons + ["wagtail_localize/icons/wagtail-localize-convert.svg"]
470470

471471

472-
if WAGTAIL_VERSION >= (2, 17):
472+
if WAGTAIL_VERSION >= (3, 0):
473473
from .models import LocaleSynchronization
474474

475475
@hooks.register("construct_synced_page_tree_list")
476476
def construct_synced_page_tree_list(pages: List[Page], action: str):
477-
478477
locale_sync_map = {}
479478
for page in pages:
480479
# TODO: what about locale C follows B which follows A, when we come in from A?

0 commit comments

Comments
 (0)