diff --git a/wagtail/admin/rich_text/converters/editor_html.py b/wagtail/admin/rich_text/converters/editor_html.py index 6d99be2c34..115bd0477b 100644 --- a/wagtail/admin/rich_text/converters/editor_html.py +++ b/wagtail/admin/rich_text/converters/editor_html.py @@ -174,6 +174,6 @@ class PageLinkHandler: if parent_page: attrs += 'data-parent-id="%d" ' % parent_page.id - return '' % (attrs, escape(page.specific.url)) + return '' % (attrs, escape(page.localized.specific.url)) except Page.DoesNotExist: return "" diff --git a/wagtail/core/rich_text/pages.py b/wagtail/core/rich_text/pages.py index 635553c743..69a5859175 100644 --- a/wagtail/core/rich_text/pages.py +++ b/wagtail/core/rich_text/pages.py @@ -19,6 +19,6 @@ class PageLinkHandler(LinkHandler): def expand_db_attributes(cls, attrs): try: page = cls.get_instance(attrs) - return '' % escape(page.specific.url) + return '' % escape(page.localized.specific.url) except Page.DoesNotExist: return "" diff --git a/wagtail/core/tests/test_rich_text.py b/wagtail/core/tests/test_rich_text.py index 7ff274e2ab..e189f99633 100644 --- a/wagtail/core/tests/test_rich_text.py +++ b/wagtail/core/tests/test_rich_text.py @@ -1,7 +1,9 @@ from unittest.mock import patch -from django.test import TestCase +from django.test import TestCase, override_settings +from django.utils import translation +from wagtail.core.models import Locale, Page from wagtail.core.rich_text import RichText, expand_db_html from wagtail.core.rich_text.feature_registry import FeatureRegistry from wagtail.core.rich_text.pages import PageLinkHandler @@ -12,6 +14,10 @@ from wagtail.tests.testapp.models import EventPage class TestPageLinktypeHandler(TestCase): fixtures = ['test.json'] + def test_expand_db_attributes(self): + result = PageLinkHandler.expand_db_attributes({'id': Page.objects.get(url_path='/home/events/christmas/').id}) + self.assertEqual(result, '') + def test_expand_db_attributes_page_does_not_exist(self): result = PageLinkHandler.expand_db_attributes({'id': 0}) self.assertEqual(result, '') @@ -21,6 +27,46 @@ class TestPageLinktypeHandler(TestCase): self.assertEqual(result, '') +@override_settings( + WAGTAIL_I18N_ENABLED=True, + WAGTAIL_CONTENT_LANGUAGES=[ + ('en', 'English'), + ('fr', 'French'), + ], + ROOT_URLCONF='wagtail.tests.urls_multilang' +) +class TestPageLinktypeHandlerWithI18N(TestCase): + fixtures = ['test.json'] + + def setUp(self): + self.fr_locale = Locale.objects.create(language_code="fr") + self.event_page = Page.objects.get(url_path='/home/events/christmas/') + self.fr_event_page = self.event_page.copy_for_translation(self.fr_locale, copy_parents=True) + self.fr_event_page.slug = 'noel' + self.fr_event_page.save(update_fields=['slug']) + self.fr_event_page.save_revision().publish() + + def test_expand_db_attributes(self): + result = PageLinkHandler.expand_db_attributes({'id': self.event_page.id}) + self.assertEqual(result, '') + + def test_expand_db_attributes_autolocalizes(self): + # Even though it's linked to the english page in rich text. + # The link should be to the local language version if it's available + with translation.override("fr"): + result = PageLinkHandler.expand_db_attributes({'id': self.event_page.id}) + self.assertEqual(result, '') + + def test_expand_db_attributes_doesnt_autolocalize_unpublished_page(self): + # We shouldn't autolocalize if the translation is unpublished + self.fr_event_page.unpublish() + self.fr_event_page.save() + + with translation.override("fr"): + result = PageLinkHandler.expand_db_attributes({'id': self.event_page.id}) + self.assertEqual(result, '') + + class TestExtractAttrs(TestCase): def test_extract_attr(self): html = 'snowman'