Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Apr 5, 2022
1 parent 8f88742 commit 0f47e29
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
77 changes: 76 additions & 1 deletion wagtail_localize/tests/test_synctree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest

from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.urls import reverse
from wagtail import VERSION as WAGTAIL_VERSION
from wagtail.core.models import Locale, Page
from wagtail.tests.utils import WagtailTestUtils

Expand All @@ -9,6 +12,12 @@
from wagtail_localize.test.models import TestHomePage, TestPage


try:
from wagtail import hooks
except ImportError:
from wagtail.core import hooks


class TestPageIndex(TestCase):
def setUp(self):
self.en_locale = Locale.objects.get(language_code="en")
Expand Down Expand Up @@ -93,7 +102,7 @@ def test_from_database(self):
self.assertEqual(canadaonlypage_entry.aliased_locales, [])


class TestSignalsAndHooks(TestCase, WagtailTestUtils):
class SyncTreeTestsSetupBase(TestCase):
def setUp(self):
self.en_locale = Locale.objects.get(language_code="en")
self.fr_locale = Locale.objects.create(language_code="fr")
Expand Down Expand Up @@ -126,6 +135,11 @@ def setUp(self):
)
)


class TestSignalsAndHooks(SyncTreeTestsSetupBase, WagtailTestUtils):
def setUp(self):
super().setUp()

LocaleSynchronization.objects.create(
locale=self.fr_locale,
sync_from=self.en_locale,
Expand Down Expand Up @@ -263,3 +277,64 @@ def test_create_homepage_in_sync_source_locale(self):
self.assertTrue(new_en_homepage.has_translation(self.fr_locale))
self.assertTrue(new_en_homepage.has_translation(self.fr_ca_locale))
self.assertTrue(new_en_homepage.has_translation(self.es_locale))


@unittest.skipUnless(
WAGTAIL_VERSION >= (3, 0),
"construct_synced_page_tree_list was added starting with Wagtail 3.0",
)
class TestConstructSyncedPageTreeListHook(SyncTreeTestsSetupBase):
def _get_hook_function(self):
the_hooks = hooks.get_hooks("construct_synced_page_tree_list")
return the_hooks[0]

def setup_locale_synchronisation(self, locale, sync_from_locale):
LocaleSynchronization.objects.create(
locale=locale,
sync_from=sync_from_locale,
)

def test_hook(self):
the_hooks = hooks.get_hooks("construct_synced_page_tree_list")
self.assertEqual(len(the_hooks), 1)

def test_hook_returns_nothing_without_locale_synchronisation(self):
hook = self._get_hook_function()
for action in ["unpublish", "delete", "move"]:
with self.subTest(f"Calling construct_synced_page_tree_list with {action}"):
results = hook([self.en_aboutpage], action)
self.assertDictEqual(results, {})

def test_hook_returns_relevant_pages_from_synced_locale_on_unpublish_action(self):
self.setup_locale_synchronisation(self.fr_locale, self.en_locale)
hook = self._get_hook_function()
results = hook([self.en_aboutpage], "unpublish")
self.assertIsNotNone(results.get(self.en_aboutpage))
self.assertQuerysetEqual(
results[self.en_aboutpage], Page.objects.filter(pk=self.fr_aboutpage.pk)
)

# unpublish should not include alias pages as they follow the parent
self.fr_aboutpage.alias_of = self.en_aboutpage
self.fr_aboutpage.save()
results = hook([self.en_aboutpage], "unpublish")
self.assertIsNotNone(results.get(self.en_aboutpage))
self.assertQuerysetEqual(results[self.en_aboutpage], Page.objects.none())

def test_hook_returns_relevant_pages_from_synced_locale_on_move_action(self):
self.setup_locale_synchronisation(self.fr_locale, self.en_locale)
hook = self._get_hook_function()
results = hook([self.en_aboutpage], "move")
self.assertIsNotNone(results.get(self.en_aboutpage))
self.assertQuerysetEqual(
results[self.en_aboutpage], Page.objects.filter(pk=self.fr_aboutpage.pk)
)

def test_hook_returns_relevant_pages_from_synced_locale_on_delete_action(self):
self.setup_locale_synchronisation(self.fr_locale, self.en_locale)
hook = self._get_hook_function()
results = hook([self.en_aboutpage], "move")
self.assertIsNotNone(results.get(self.en_aboutpage))
self.assertQuerysetEqual(
results[self.en_aboutpage], Page.objects.filter(pk=self.fr_aboutpage.pk)
)
3 changes: 1 addition & 2 deletions wagtail_localize/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,11 @@ def register_icons(icons):
return icons + ["wagtail_localize/icons/wagtail-localize-convert.svg"]


if WAGTAIL_VERSION >= (2, 17):
if WAGTAIL_VERSION >= (3, 0):
from .models import LocaleSynchronization

@hooks.register("construct_synced_page_tree_list")
def construct_synced_page_tree_list(pages: List[Page], action: str):

locale_sync_map = {}
for page in pages:
# TODO: what about locale C follows B which follows A, when we come in from A?
Expand Down

0 comments on commit 0f47e29

Please sign in to comment.