From cdbc577283d9d5114559a511c17e7a47edf0133f Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Wed, 15 May 2024 00:11:14 +0200 Subject: [PATCH] Add tests --- djangocms_versioning/cms_toolbars.py | 3 + tests/test_admin.py | 90 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/djangocms_versioning/cms_toolbars.py b/djangocms_versioning/cms_toolbars.py index 62a5231a..73d18594 100644 --- a/djangocms_versioning/cms_toolbars.py +++ b/djangocms_versioning/cms_toolbars.py @@ -292,6 +292,9 @@ def get_page_content(self, language=None): if not language: language = self.current_lang + toolbar_obj = self.toolbar.get_object() + if toolbar_obj and toolbar_obj.language == language: + return self.toolbar.get_object() return get_latest_admin_viewable_content(self.page, language=language) def populate(self): diff --git a/tests/test_admin.py b/tests/test_admin.py index 56560c9e..04d9050d 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -51,6 +51,7 @@ BlogContentFactory, BlogPostFactory, BlogPostVersionFactory, + PollVersionFactory, ) from djangocms_versioning.test_utils.incorrectly_configured_blogpost.models import ( IncorrectBlogContent, @@ -3171,3 +3172,92 @@ def test_fake_back_link(self): self.assertNotContains(response, "hijack_url") self.assertContains(response, version_list_url(version.content)) +class VersioningAdminButtonsTestCase(CMSTestCase): + def _get_publish_url(self, version, versionable=PollsCMSConfig.versioning[0]): + """Helper method to return the expected publish url + """ + admin_url = self.get_admin_url( + versionable.version_model_proxy, "publish", version.pk + ) + return admin_url + + def _get_edit_url(self, version, versionable=PollsCMSConfig.versioning[0]): + """Helper method to return the expected edit redirect url + """ + admin_url = self.get_admin_url( + versionable.version_model_proxy, "edit_redirect", version.pk + ) + return admin_url + + def _get_revert_url(self, version, versionable=PollsCMSConfig.versioning[0]): + """Helper method to return the expected publish url + """ + admin_url = self.get_admin_url( + versionable.version_model_proxy, "revert", version.pk + ) + return admin_url + + def get_change_view_url(self, content): + return self.get_admin_url( + content.__class__, + "change", + content.pk, + ) + + def test_buttons_in_draft_changeview(self): + """Only publish button should be visible in draft mode""" + version = PollVersionFactory(state=constants.DRAFT) + action_url = self._get_publish_url(version) + next_url = self.get_change_view_url(version.content) + expected_button = ('Publish') + + with self.login_user_context(self.get_superuser()): + response = self.client.get(self.get_change_view_url(version.content)) + + self.assertContains(response, expected_button) + self.assertNotContains(response, "Revert") + self.assertNotContains(response, "New Draft") + + def test_buttons_in_published_changeview(self): + """Only revert button should be visible in published mode""" + version = PollVersionFactory(state=constants.PUBLISHED) + action_url = self._get_edit_url(version) + expected_button = ('New Draft') + + with self.login_user_context(self.get_superuser()): + response = self.client.get(self.get_change_view_url(version.content)) + + self.assertContains(response, expected_button) + self.assertNotContains(response, "Revert") + self.assertNotContains(response, "Publish") + + def test_buttons_in_unpublished_changeview(self): + """Only revert button should be visible in unpublished mode""" + version = PollVersionFactory(state=constants.UNPUBLISHED) + action_url = self._get_revert_url(version) + next_url = self.get_change_view_url(version.content) + expected_button = f'Revert' + + with self.login_user_context(self.get_superuser()): + response = self.client.get(self.get_change_view_url(version.content)) + + self.assertContains(response, expected_button) + self.assertNotContains(response, "New Draft") + self.assertNotContains(response, "Publish") + + def test_buttons_in_archived_changeview(self): + """Only revert button should be visible in archived mode""" + version = PollVersionFactory(state=constants.ARCHIVED) + action_url = self._get_revert_url(version) + next_url = self.get_change_view_url(version.content) + expected_button = f'Revert' + + with self.login_user_context(self.get_superuser()): + response = self.client.get(self.get_change_view_url(version.content)) + + self.assertContains(response, expected_button) + self.assertNotContains(response, "New Draft") + self.assertNotContains(response, "Publish") +