From f5d89f0d54379d8587ee369aa128aace266df200 Mon Sep 17 00:00:00 2001 From: Filip Weidemann Date: Mon, 22 Jul 2024 11:10:18 +0200 Subject: [PATCH] tests: refactor tests for djangoCMS 4 support --- djangocms_link/fields_select2.py | 2 +- tests/fixtures.py | 70 ++++++++++++++++++++++++++++++++ tests/test_migrations.py | 2 +- tests/test_plugins.py | 19 +++++---- tox.ini | 2 +- 5 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures.py diff --git a/djangocms_link/fields_select2.py b/djangocms_link/fields_select2.py index d843f785..29a3e55d 100644 --- a/djangocms_link/fields_select2.py +++ b/djangocms_link/fields_select2.py @@ -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) diff --git a/tests/fixtures.py b/tests/fixtures.py new file mode 100644 index 00000000..ec8ffdc2 --- /dev/null +++ b/tests/fixtures.py @@ -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() diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 983fa86c..50d7bc96 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -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()}') diff --git a/tests/test_plugins.py b/tests/test_plugins.py index c3c8d68b..e1ef543b 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -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() @@ -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): @@ -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): @@ -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', ) @@ -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', ) @@ -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, @@ -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, diff --git a/tox.ini b/tox.ini index 6f2f65b2..838fcd7b 100644 --- a/tox.ini +++ b/tox.ini @@ -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