From 988e33bbcc18a9c99a141bb4cd06acce0498e851 Mon Sep 17 00:00:00 2001 From: zerolab Date: Sat, 10 Feb 2024 11:38:18 +0000 Subject: [PATCH] Ensure alias page uses parent id in "translate this page" --- .../tests/test_convert_to_alias.py | 26 ++-- .../tests/test_submit_translations.py | 128 +++++++++++------- wagtail_localize/wagtail_hooks.py | 6 +- 3 files changed, 98 insertions(+), 62 deletions(-) diff --git a/wagtail_localize/tests/test_convert_to_alias.py b/wagtail_localize/tests/test_convert_to_alias.py index 275d0ea25..9c90a1250 100644 --- a/wagtail_localize/tests/test_convert_to_alias.py +++ b/wagtail_localize/tests/test_convert_to_alias.py @@ -8,13 +8,14 @@ from wagtail_localize.wagtail_hooks import ConvertToAliasPageActionMenuItem -class ConvertToAliasTestData(WagtailTestUtils): - def setUp(self): - self.en_locale = Locale.objects.get(language_code="en") - self.fr_locale = Locale.objects.create(language_code="fr") - - self.home_page = Page.objects.get(depth=2) - self.page = self.home_page.add_child( +class ConvertToAliasTestBase(WagtailTestUtils, TestCase): + @classmethod + def setUpTestData(cls): + cls.en_locale = Locale.objects.get(language_code="en") + cls.fr_locale = Locale.objects.create(language_code="fr") + + cls.home_page = Page.objects.get(depth=2) + cls.page = cls.home_page.add_child( instance=TestPage( title="The title", slug="the-page", @@ -22,15 +23,16 @@ def setUp(self): ) LocaleSynchronization.objects.create( - locale=self.fr_locale, - sync_from=self.en_locale, + locale=cls.fr_locale, + sync_from=cls.en_locale, ) - self.fr_page = self.page.get_translation(self.fr_locale) + cls.fr_page = cls.page.get_translation(cls.fr_locale) + def setUp(self): self.login() -class ConvertToAliasTest(ConvertToAliasTestData, TestCase): +class ConvertToAliasTest(ConvertToAliasTestBase): def _page_action_is_shown(self, page, view="edit"): menu_item = ConvertToAliasPageActionMenuItem() context = {"view": view, "page": page} @@ -114,7 +116,7 @@ def test_page_action_available_for_translated_page_from_translated_page(self): self.assertTrue(self._page_action_is_shown(de_page)) -class ConvertToAliasViewTest(ConvertToAliasTestData, TestCase): +class ConvertToAliasViewTest(ConvertToAliasTestBase): def setUp(self): super().setUp() # submit for translation, thus no longer an alias diff --git a/wagtail_localize/tests/test_submit_translations.py b/wagtail_localize/tests/test_submit_translations.py index b3d0bc34a..a458b81fd 100644 --- a/wagtail_localize/tests/test_submit_translations.py +++ b/wagtail_localize/tests/test_submit_translations.py @@ -60,30 +60,34 @@ def strip_user_perms(): ("es", "Spanish"), ], ) -class TestTranslatePageListingButton(TestCase, WagtailTestUtils): - def setUp(self): - self.login() +class TestTranslatePageListingButton(WagtailTestUtils, TestCase): + @classmethod + def setUpTestData(cls): + cls.en_locale = Locale.objects.get() + cls.fr_locale = Locale.objects.create(language_code="fr") + cls.de_locale = Locale.objects.create(language_code="de") - self.en_locale = Locale.objects.get() - self.fr_locale = Locale.objects.create(language_code="fr") - self.de_locale = Locale.objects.create(language_code="de") + cls.en_homepage = Page.objects.get(depth=2) + cls.fr_homepage = cls.en_homepage.copy_for_translation(cls.fr_locale) + cls.de_homepage = cls.en_homepage.copy_for_translation(cls.de_locale) - self.en_homepage = Page.objects.get(depth=2) - self.fr_homepage = self.en_homepage.copy_for_translation(self.fr_locale) - self.de_homepage = self.en_homepage.copy_for_translation(self.de_locale) + cls.en_blog_index = make_test_page(cls.en_homepage, title="Blog", slug="blog") - self.en_blog_index = make_test_page(self.en_homepage, title="Blog", slug="blog") + def setUp(self): + self.login() - def test(self): + def _test_submit_for_translation_more_action( + self, parent_page_id, expected_page_id + ): response = self.client.get( - reverse("wagtailadmin_explore", args=[self.en_homepage.id]) + reverse("wagtailadmin_explore", args=[parent_page_id]) ) if WAGTAIL_VERSION >= (5, 2): self.assertContains( response, ( - f'' + f'' '' "Translate this page" @@ -95,7 +99,7 @@ def test(self): self.assertContains( response, ( - f'' '' @@ -108,7 +112,7 @@ def test(self): self.assertContains( response, ( - f'' "Translate this page" ), @@ -118,12 +122,32 @@ def test(self): self.assertContains( response, ( - f'Translate this page' ), html=True, ) + return response + + def test_submit_translation_link(self): + self._test_submit_for_translation_more_action( + self.en_homepage.pk, self.en_blog_index.pk + ) + + def test_submit_translation_shows_parent_id_for_alias_pages(self): + alias_page = self.en_blog_index.create_alias( + parent=self.fr_homepage, + update_slug="the-page-alias", + ) + response = self._test_submit_for_translation_more_action( + self.fr_homepage.pk, self.en_blog_index.pk + ) + + self.assertNotContains( + response, f"/admin/localize/submit/page/{alias_page.pk}/" + ) + def test_hides_if_page_already_translated(self): self.en_blog_index.copy_for_translation(self.fr_locale) self.en_blog_index.copy_for_translation(self.de_locale) @@ -158,26 +182,28 @@ def test_hides_if_user_doesnt_have_permission(self): ("es", "Spanish"), ], ) -class TestSubmitPageTranslation(TestCase, WagtailTestUtils): - def setUp(self): - self.login() +class TestSubmitPageTranslation(WagtailTestUtils, TestCase): + @classmethod + def setUpTestData(cls): + cls.en_locale = Locale.objects.get() + cls.fr_locale = Locale.objects.create(language_code="fr") + cls.de_locale = Locale.objects.create(language_code="de") - self.en_locale = Locale.objects.get() - self.fr_locale = Locale.objects.create(language_code="fr") - self.de_locale = Locale.objects.create(language_code="de") + cls.en_homepage = Page.objects.get(depth=2) + cls.fr_homepage = cls.en_homepage.copy_for_translation(cls.fr_locale) + cls.de_homepage = cls.en_homepage.copy_for_translation(cls.de_locale) - self.en_homepage = Page.objects.get(depth=2) - self.fr_homepage = self.en_homepage.copy_for_translation(self.fr_locale) - self.de_homepage = self.en_homepage.copy_for_translation(self.de_locale) - - self.en_blog_index = make_test_page(self.en_homepage, title="Blog", slug="blog") - self.en_blog_post = make_test_page( - self.en_blog_index, title="Blog post", slug="blog-post" + cls.en_blog_index = make_test_page(cls.en_homepage, title="Blog", slug="blog") + cls.en_blog_post = make_test_page( + cls.en_blog_index, title="Blog post", slug="blog-post" ) - self.en_blog_post_child = make_test_page( - self.en_blog_post, title="A deep page", slug="deep-page" + cls.en_blog_post_child = make_test_page( + cls.en_blog_post, title="A deep page", slug="deep-page" ) + def setUp(self): + self.login() + def test_get_submit_page_translation(self): response = self.client.get( reverse( @@ -703,19 +729,21 @@ def test_post_submit_page_translation_from_draft_source_still_draft(self): ("es", "Spanish"), ], ) -class TestTranslateSnippetListingButton(TestCase, WagtailTestUtils): - def setUp(self): - self.login() +class TestTranslateSnippetListingButton(WagtailTestUtils, TestCase): + @classmethod + def setUpTestData(cls): + cls.en_locale = Locale.objects.get() + cls.fr_locale = Locale.objects.create(language_code="fr") + cls.de_locale = Locale.objects.create(language_code="de") - self.en_locale = Locale.objects.get() - self.fr_locale = Locale.objects.create(language_code="fr") - self.de_locale = Locale.objects.create(language_code="de") + cls.en_snippet = TestSnippet.objects.create(field="Test snippet") + cls.fr_snippet = cls.en_snippet.copy_for_translation(cls.fr_locale) + cls.fr_snippet.save() - self.en_snippet = TestSnippet.objects.create(field="Test snippet") - self.fr_snippet = self.en_snippet.copy_for_translation(self.fr_locale) - self.fr_snippet.save() + cls.not_translatable_snippet = NonTranslatableSnippet.objects.create() - self.not_translatable_snippet = NonTranslatableSnippet.objects.create() + def setUp(self): + self.login() def test(self): response = self.client.get( @@ -787,17 +815,19 @@ def test_hides_if_user_doesnt_have_permission(self): ("es", "Spanish"), ], ) -class TestSubmitSnippetTranslation(TestCase, WagtailTestUtils): - def setUp(self): - self.login() +class TestSubmitSnippetTranslation(WagtailTestUtils, TestCase): + @classmethod + def setUpTestData(cls): + cls.en_locale = Locale.objects.get() + cls.fr_locale = Locale.objects.create(language_code="fr") + cls.de_locale = Locale.objects.create(language_code="de") - self.en_locale = Locale.objects.get() - self.fr_locale = Locale.objects.create(language_code="fr") - self.de_locale = Locale.objects.create(language_code="de") + cls.en_snippet = TestSnippet.objects.create(field="Test snippet") - self.en_snippet = TestSnippet.objects.create(field="Test snippet") + cls.not_translatable_snippet = NonTranslatableSnippet.objects.create() - self.not_translatable_snippet = NonTranslatableSnippet.objects.create() + def setUp(self): + self.login() def test_get_submit_snippet_translation(self): response = self.client.get( diff --git a/wagtail_localize/wagtail_hooks.py b/wagtail_localize/wagtail_hooks.py index b5483f15a..579a70420 100644 --- a/wagtail_localize/wagtail_hooks.py +++ b/wagtail_localize/wagtail_hooks.py @@ -157,7 +157,11 @@ def _page_listing_more_buttons(page: Page, user, next_url: str | None = None): ).exists() if has_locale_to_translate_to: - url = reverse("wagtail_localize:submit_page_translation", args=[page.id]) + # note: if this is an alias page, we want to use the parent page id + url = reverse( + "wagtail_localize:submit_page_translation", + args=[page.alias_of_id or page.id], + ) yield PageButton( _("Translate this page"),