Skip to content

Commit

Permalink
Ensure alias page uses parent id in "translate this page"
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Feb 10, 2024
1 parent 7d9fc03 commit 988e33b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 62 deletions.
26 changes: 14 additions & 12 deletions wagtail_localize/tests/test_convert_to_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@
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",
)
)

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}
Expand Down Expand Up @@ -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
Expand Down
128 changes: 79 additions & 49 deletions wagtail_localize/tests/test_submit_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<a href="/admin/localize/submit/page/{self.en_blog_index.id}/">'
f'<a href="/admin/localize/submit/page/{expected_page_id}/">'
'<svg class="icon icon-wagtail-localize-language icon" aria-hidden="true">'
'<use href="#icon-wagtail-localize-language"></use></svg>'
"Translate this page"
Expand All @@ -95,7 +99,7 @@ def test(self):
self.assertContains(
response,
(
f'<a href="/admin/localize/submit/page/{self.en_blog_index.id}/" '
f'<a href="/admin/localize/submit/page/{expected_page_id}/" '
'aria-label="" class="button button-small button-secondary">'
'<svg class="icon icon-wagtail-localize-language icon" aria-hidden="true">'
'<use href="#icon-wagtail-localize-language"></use></svg>'
Expand All @@ -108,7 +112,7 @@ def test(self):
self.assertContains(
response,
(
f'<a href="/admin/localize/submit/page/{self.en_blog_index.id}/" '
f'<a href="/admin/localize/submit/page/{expected_page_id}/" '
'aria-label="" class="u-link is-live button-secondary button-small button">'
"Translate this page</a>"
),
Expand All @@ -118,12 +122,32 @@ def test(self):
self.assertContains(
response,
(
f'<a href="/admin/localize/submit/page/{self.en_blog_index.id}/" '
f'<a href="/admin/localize/submit/page/{expected_page_id}/" '
'aria-label="" class="u-link is-live ">Translate this page</a>'
),
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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion wagtail_localize/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 988e33b

Please sign in to comment.