Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed May 14, 2024
1 parent 9422b2e commit cdbc577
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions djangocms_versioning/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
90 changes: 90 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
BlogContentFactory,
BlogPostFactory,
BlogPostVersionFactory,
PollVersionFactory,
)
from djangocms_versioning.test_utils.incorrectly_configured_blogpost.models import (
IncorrectBlogContent,
Expand Down Expand Up @@ -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 = ('<a class="accent cms-form-post-method" '
f'href="{action_url}?next={next_url}">Publish</a>')

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 = ('<a class="accent cms-form-post-method" '
f'href="{action_url}?force_admin=1">New Draft</a>')

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'<a href="{action_url}?next={next_url}">Revert</a>'

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'<a href="{action_url}?next={next_url}">Revert</a>'

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")

0 comments on commit cdbc577

Please sign in to comment.