Skip to content

Commit

Permalink
tests: refactor tests for djangoCMS 4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
filipweidemann committed Jul 22, 2024
1 parent 1a21f40 commit f5d89f0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion djangocms_link/fields_select2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_queryset(self):
if self.site:
return Page.objects.drafts().on_site(self.site)
return Page.objects.drafts()

# django CMS >= 4
if self.site:
return Page.objects.on_site(self.site)
Expand Down
70 changes: 70 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from django.apps import apps

from cms import __version__

DJANGO_CMS4 = not (__version__ < "4")
DJANGOCMS_VERSIONING = apps.is_installed("djangocms_versioning")


class TestFixture:
"""Sets up generic setUp and tearDown methods for tests."""

def setUp(self):
self.language = "en"
self.superuser = self.get_superuser()
return super().setUp()

def tearDown(self):
if DJANGOCMS_VERSIONING:
from djangocms_versioning.models import Version

Version.objects.all().delete()
return super().tearDown()

if DJANGO_CMS4: # CMS V4

def _get_version(self, grouper, version_state, language=None):
language = language or self.language

from djangocms_versioning.models import Version

versions = Version.objects.filter_by_grouper(grouper).filter(
state=version_state
)
for version in versions:
if (
hasattr(version.content, "language")
and version.content.language == language
):
return version
return None

def publish(self, grouper, language=None):
if DJANGOCMS_VERSIONING:
from djangocms_versioning.constants import DRAFT

version = self._get_version(grouper, DRAFT, language)
if version is not None:
version.publish(self.superuser)

def unpublish(self, grouper, language=None):
if DJANGOCMS_VERSIONING:
from djangocms_versioning.constants import PUBLISHED

version = self._get_version(grouper, PUBLISHED, language)
if version is not None:
version.unpublish(self.superuser)

def get_placeholders(self, page, language=None):
return page.get_placeholders(language or self.language)

else: # CMS V3

def publish(self, page, language=None):
page.publish(language)

def unpublish(self, page, language=None):
page.unpublish(language)

def get_placeholders(self, page, language=None):
return page.get_placeholders()
2 changes: 1 addition & 1 deletion tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def test_for_missing_migrations(self):
# the "no changes" exit code is 0
status_code = '0'

if status_code == '1':
if status_code == '1' and "djangocms_link" in output:
self.fail(f'There are missing migrations:\n {output.getvalue()}')
19 changes: 10 additions & 9 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from djangocms_link.models import AbstractLink

from .helpers import get_filer_file
from .fixtures import TestFixture


class LinkPluginsTestCase(CMSTestCase):
class LinkPluginsTestCase(TestFixture, CMSTestCase):

def setUp(self):
self.file = get_filer_file()
Expand All @@ -22,19 +23,19 @@ def setUp(self):
template="page.html",
language=self.language,
)
self.home.publish(self.language)
self.publish(self.home, self.language)
self.page = create_page(
title="content",
template="page.html",
language=self.language,
)
self.page.publish(self.language)
self.publish(self.page, self.language)
self.static_page = create_page(
title='static-content',
template='static_placeholder.html',
language='en',
)
self.placeholder = self.page.placeholders.get(slot="content")
self.placeholder = self.get_placeholders(self.page, self.language).get(slot="content")
self.superuser = self.get_superuser()

def tearDown(self):
Expand Down Expand Up @@ -64,7 +65,7 @@ def test_plugin_structure(self):
internal_link=self.page,
name="Page link",
)
self.page.publish(self.language)
self.publish(self.page, self.language)
self.assertEqual(plugin.get_plugin_class_instance().name, "Link")

with self.login_user_context(self.superuser):
Expand Down Expand Up @@ -97,7 +98,7 @@ def test_optional_link(self):
AbstractLink.link_is_optional = True

plugin = add_plugin(
self.page.placeholders.get(slot='content'),
self.get_placeholders(self.page, self.language).get(slot='content'),
'LinkPlugin',
'en',
)
Expand All @@ -108,7 +109,7 @@ def test_optional_link(self):
self.assertEqual(AbstractLink.link_is_optional, False)

plugin = add_plugin(
self.page.placeholders.get(slot='content'),
self.get_placeholders(self.page, self.language).get(slot='content'),
'LinkPlugin',
'en',
)
Expand All @@ -119,7 +120,7 @@ def test_optional_link(self):

def test_in_placeholders(self):
plugin = add_plugin(
self.page.placeholders.get(slot='content'),
self.get_placeholders(self.page, self.language).get(slot='content'),
'LinkPlugin',
'en',
internal_link=self.page,
Expand Down Expand Up @@ -164,7 +165,7 @@ def test_in_placeholders(self):

def test_file(self):
plugin = add_plugin(
self.page.placeholders.get(slot="content"),
self.get_placeholders(self.page, self.language).get(slot="content"),
"LinkPlugin",
"en",
file_link=self.file,
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ deps =
-r{toxinidir}/tests/requirements/base.txt
dj32: Django>=3.2,<4.0
dj40: Django>=4.0,<4.1
dj42: Djago>=4.2,<5.0
dj42: Django>=4.2,<5.0
cms310: django-cms>=3.10,<3.11
cms311: django-cms>=3.11,<3.12
cms41: django-cms>=4.1,<4.2
Expand Down

0 comments on commit f5d89f0

Please sign in to comment.