Repository: wagtail/wagtail
File: wagtail/admin/tests/test_userbar.py (Python)

Code before:
        french = Locale.objects.create(language_code="fr")
        self.homepage.copy_for_translation(french)
        french_page = self.page.copy_for_translation(french)

        with translation.override("fr"):
            response = french_page.serve(self.request)
            response.render()

        self.assertContains(response, "Go to Wagtail admin")

    @override_settings(
        WAGTAIL_I18N_ENABLED=True,
        WAGTAIL_CONTENT_LANGUAGES=[("en", "English"), ("fr", "French")],
        LANGUAGE_CODE="en",
    )
    def test_userbar_rendered_in_active_language_if_admin_permitted(self):
        french = Locale.objects.create(language_code="fr")
        self.homepage.copy_for_translation(french)
        french_page = self.page.copy_for_translation(french)

        with translation.override("fr"):
            response = french_page.serve(self.request)
            response.render()

        with translation.override("fr"):
            expected_text = gettext("Go to Wagtail admin")
        self.assertContains(response, expected_text)

    @override_settings(LANGUAGE_CODE="en")
    def test_userbar_rendered_in_user_preferred_language(self):
        profile = UserProfile.get_for_user(self.user)
        profile.preferred_language = "fr"
        profile.save()

        response = self.page.serve(self.request)
        response.render()

        with translation.override("fr"):
            expected_text = gettext("Go to Wagtail admin")
        self.assertContains(response, expected_text)


class TestUserbarHooksForChecksPanel(WagtailTestUtils, TestCase):
    def setUp(self):
        self.user = self.login()
        self.homepage = Page.objects.get(id=2).specific

    def test_construct_wagtail_userbar_hook_passes_page(self):
        kwargs = {}

        def construct_wagtail_userbar(request, items, page):

Review comment (suggestion):
note (non-blocking): I like this and I wish you could make it more explicit. Other than `self.assertNotEqual(expected_text, "Go to Wagtail admin")` I can't think of a nice way to do this.

Code after:
        french = Locale.objects.create(language_code="fr")
        self.homepage.copy_for_translation(french)
        french_page = self.page.copy_for_translation(french)

        with translation.override("fr"):
            response = french_page.serve(self.request)
            response.render()

        self.assertContains(response, "Go to Wagtail admin")

    @override_settings(
        WAGTAIL_I18N_ENABLED=True,
        WAGTAIL_CONTENT_LANGUAGES=[("en", "English"), ("fr", "French")],
        LANGUAGE_CODE="en",
    )
    def test_userbar_rendered_in_active_language_if_admin_permitted(self):
        french = Locale.objects.create(language_code="fr")
        self.homepage.copy_for_translation(french)
        french_page = self.page.copy_for_translation(french)

        with translation.override("fr"):
            response = french_page.serve(self.request)
            response.render()

        with translation.override("fr"):
            expected_text = gettext("Go to Wagtail admin")
        self.assertContains(response, expected_text)

    @override_settings(LANGUAGE_CODE="en")
    def test_userbar_rendered_in_user_preferred_language(self):
        profile = UserProfile.get_for_user(self.user)
        profile.preferred_language = "fr"
        profile.save()

        response = self.page.serve(self.request)
        response.render()

        # Ensure we have a French string without tests failing if translation changes
        with translation.override("fr"):
            expected_text = gettext("Go to Wagtail admin")
        self.assertNotEqual(expected_text, "Go to Wagtail admin")
        self.assertContains(response, expected_text)


class TestUserbarHooksForChecksPanel(WagtailTestUtils, TestCase):
    def setUp(self):
        self.user = self.login()
        self.homepage = Page.objects.get(id=2).specific

    def test_construct_wagtail_userbar_hook_passes_page(self):
        kwargs = {}