diff --git a/tests/requirements/base.txt b/tests/requirements/base.txt index 57b4cba1..e27c327c 100644 --- a/tests/requirements/base.txt +++ b/tests/requirements/base.txt @@ -1,7 +1,7 @@ # requirements from setup.py django-filer>=1.4.0 djangocms-picture>=2.1.0 -djangocms-link>=2.2.1 +djangocms-link>=5.0.0 django-polymorphic>=2.0.3 Pillow html5lib>=0.999999999 diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 9c031ed5..9c4d2e33 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -20,7 +20,11 @@ from djangocms_text.cms_plugins import TextPlugin from djangocms_text.models import Text from djangocms_text.utils import ( - _plugin_tags_to_html, _render_cms_plugin, plugin_tags_to_admin_html, plugin_tags_to_id_list, plugin_to_tag, + _plugin_tags_to_html, + _render_cms_plugin, + plugin_tags_to_admin_html, + plugin_tags_to_id_list, + plugin_to_tag, ) from tests.test_app.cms_plugins import DummyChildPlugin, DummyParentPlugin @@ -30,12 +34,14 @@ try: from djangocms_transfer.exporter import export_page + HAS_DJANGOCMS_TRANSFER = True except ImportError: HAS_DJANGOCMS_TRANSFER = False try: import djangocms_translations # noqa + HAS_DJANGOCMS_TRANSLATIONS = True except ImportError: HAS_DJANGOCMS_TRANSLATIONS = False @@ -45,57 +51,57 @@ class PluginActionsTestCase(TestFixture, BaseTestCase): - def get_custom_admin_url(self, plugin_class, name): plugin_type = plugin_class.__name__.lower() - url_name = f'{plugin_class.model._meta.app_label}_{plugin_type}_{name}' + url_name = f"{plugin_class.model._meta.app_label}_{plugin_type}_{name}" return admin_reverse(url_name) - def _add_child_plugin(self, text_plugin, plugin_type='PicturePlugin', data_suffix=None): - name = f'{plugin_type} record' + def _add_child_plugin(self, text_plugin, plugin_type="PicturePlugin", data_suffix=None): + name = f"{plugin_type} record" if data_suffix is not None: - name = f'{name} {data_suffix}' + name = f"{name} {data_suffix}" basic_plugins = { - 'LinkPlugin': { - 'name': name, - 'external_link': 'https://www.django-cms.org', + "LinkPlugin": { + "name": name, + "link": {"external_link": "https://www.django-cms.org"}, }, - 'PreviewDisabledPlugin': {}, - 'SekizaiPlugin': {}, + "PreviewDisabledPlugin": {}, + "SekizaiPlugin": {}, } - if plugin_type == 'PicturePlugin': - data = {'caption_text': name, 'picture': self.create_filer_image_object()} + if plugin_type == "PicturePlugin": + data = {"caption_text": name, "picture": self.create_filer_image_object()} else: data = basic_plugins[plugin_type] plugin = add_plugin( text_plugin.placeholder, plugin_type, - 'en', + "en", target=text_plugin, **data, ) return plugin - def _add_text_plugin(self, placeholder, plugin_type='TextPlugin'): + def _add_text_plugin(self, placeholder, plugin_type="TextPlugin"): text_plugin = add_plugin( placeholder, plugin_type, - 'en', - body='Hello World', + "en", + body="Hello World", ) return text_plugin def _replace_plugin_contents(self, text, new_plugin_content): def _do_replace(obj, match): return plugin_to_tag(obj, content=new_plugin_content) + return _plugin_tags_to_html(text, output_func=_do_replace) def add_plugin_to_text(self, text_plugin, plugin): - text_plugin.body = f'{text_plugin.body} {plugin_to_tag(plugin)}' + text_plugin.body = f"{text_plugin.body} {plugin_to_tag(plugin)}" text_plugin.save() return text_plugin @@ -104,7 +110,7 @@ def _give_permission(self, user, model, permission_type, save=True): user.user_permissions.add(Permission.objects.get(codename=codename)) def _give_cms_permissions(self, user): - for perm_type in ['add', 'change', 'delete']: + for perm_type in ["add", "change", "delete"]: for model in [Page]: self._give_permission(user, model, perm_type) @@ -119,24 +125,24 @@ def get_plugin_id_from_response(self, response): url = unquote(response.url) # Ideal case, this looks like: # /en/admin/cms/page/edit-plugin/1/ - return re.findall(r'\d+', url)[0] + return re.findall(r"\d+", url)[0] def test_add_and_edit_plugin(self): """ Test that you can add a text plugin """ admin = self.get_superuser() - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") - endpoint = self.get_add_plugin_uri(simple_placeholder, 'TextPlugin') + endpoint = self.get_add_plugin_uri(simple_placeholder, "TextPlugin") with self.login_user_context(admin): response = self.client.get(endpoint) text_plugin_pk = self.get_plugin_id_from_response(response) - self.assertIn('?revert-on-cancel', response.url) + self.assertIn("?revert-on-cancel", response.url) self.assertEqual(response.status_code, 302) # Assert "ghost" plugin has been created @@ -161,7 +167,7 @@ def test_add_and_edit_plugin(self): self.assertContains(response, action_token) with self.login_user_context(admin): - data = {'body': 'Hello world'} + data = {"body": "Hello world"} response = self.client.post(add_url, data) self.assertEqual(response.status_code, 200) @@ -172,16 +178,16 @@ def test_add_and_edit_plugin(self): text_plugin = Text.objects.get(pk=text_plugin_pk) # Assert the text was correctly saved - self.assertEqual(text_plugin.body, 'Hello world') + self.assertEqual(text_plugin.body, "Hello world") def test_add_and_cancel_plugin(self): """ Test that you can add a text plugin """ - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") - endpoint = self.get_add_plugin_uri(simple_placeholder, 'TextPlugin') + endpoint = self.get_add_plugin_uri(simple_placeholder, "TextPlugin") with self.login_user_context(self.get_superuser()): response = self.client.get(endpoint) @@ -199,7 +205,7 @@ def test_add_and_cancel_plugin(self): with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, cms_plugin) - data = {'token': action_token} + data = {"token": action_token} request = self.get_post_request(data) response = text_plugin_class.revert_on_cancel(request) self.assertEqual(response.status_code, 204) @@ -213,15 +219,15 @@ def test_add_and_cancel_plugin(self): # Assert user can't delete a non "ghost" plugin text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin) - data = {'token': action_token} + data = {"token": action_token} request = self.get_post_request(data) response = text_plugin_class.revert_on_cancel(request) self.assertEqual(response.status_code, 204) @@ -232,15 +238,15 @@ def test_copy_referenced_plugins(self): Test that copy+pasting a child plugin between text editors creates proper copies of the child plugin and messes no other data up """ - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") def _get_text_plugin_with_children(): text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', - body='Text plugin we copy child plugins to', + "TextPlugin", + "en", + body="Text plugin we copy child plugins to", ) _add_child_plugins_to_text_plugin(text_plugin) return text_plugin @@ -248,26 +254,26 @@ def _get_text_plugin_with_children(): def _add_child_plugins_to_text_plugin(text_plugin): child_plugin_1 = add_plugin( simple_placeholder, - 'PicturePlugin', - 'en', + "PicturePlugin", + "en", target=text_plugin, picture=self.create_filer_image_object(), - caption_text='Child plugin one', + caption_text="Child plugin one", ) child_plugin_2 = add_plugin( simple_placeholder, - 'PicturePlugin', - 'en', + "PicturePlugin", + "en", target=text_plugin, picture=self.create_filer_image_object(), - caption_text='Child plugin two', + caption_text="Child plugin two", ) self.add_plugin_to_text(text_plugin, child_plugin_1) self.add_plugin_to_text(text_plugin, child_plugin_2) def _copy_child_plugins_from_text(text_plugin_source, text_plugin_destination): for child_plugin in text_plugin_source.cmsplugin_set.all(): - text_plugin_destination.body += ' ' + plugin_to_tag(child_plugin) + text_plugin_destination.body += " " + plugin_to_tag(child_plugin) text_plugin_destination.save() _run_clean_and_copy(text_plugin_destination) @@ -300,13 +306,13 @@ def test_add_and_cancel_child_plugin(self): Test that you can add a text plugin """ admin = self.get_superuser() - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) @@ -314,35 +320,35 @@ def test_add_and_cancel_child_plugin(self): child_plugin_1 = add_plugin( simple_placeholder, - 'PicturePlugin', - 'en', + "PicturePlugin", + "en", target=text_plugin, picture=self.create_filer_image_object(), - caption_text='Foo', + caption_text="Foo", ) child_plugin_2 = add_plugin( simple_placeholder, - 'PicturePlugin', - 'en', + "PicturePlugin", + "en", target=text_plugin, picture=self.create_filer_image_object(), - caption_text='Foo', + caption_text="Foo", ) child_plugin_3 = add_plugin( simple_placeholder, - 'PicturePlugin', - 'en', + "PicturePlugin", + "en", target=text_plugin, picture=self.create_filer_image_object(), - caption_text='Foo', + caption_text="Foo", ) child_plugin_4 = add_plugin( simple_placeholder, - 'PicturePlugin', - 'en', + "PicturePlugin", + "en", target=text_plugin, picture=self.create_filer_image_object(), - caption_text='Foo', + caption_text="Foo", ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_1) @@ -353,7 +359,7 @@ def test_add_and_cancel_child_plugin(self): action_token = text_plugin_class.get_action_token(request, text_plugin) # Assert user is unable to delete a saved child plugin - data = {'token': action_token, 'child_plugins': [child_plugin_1.pk]} + data = {"token": action_token, "child_plugins": [child_plugin_1.pk]} request = self.get_post_request(data) response = text_plugin_class.revert_on_cancel(request) self.assertEqual(response.status_code, 204) @@ -367,7 +373,7 @@ def test_add_and_cancel_child_plugin(self): child_plugin_3.pk, child_plugin_4.pk, ] - data = {'token': action_token, 'child_plugins': plugin_ids} + data = {"token": action_token, "child_plugins": plugin_ids} request = self.get_post_request(data) response = text_plugin_class.revert_on_cancel(request) self.assertEqual(response.status_code, 204) @@ -380,7 +386,7 @@ def test_add_and_cancel_child_plugin(self): child_plugin_2.pk, child_plugin_3.pk, ] - data = {'token': action_token, 'child_plugins': plugin_ids} + data = {"token": action_token, "child_plugins": plugin_ids} request = self.get_post_request(data) response = text_plugin_class.revert_on_cancel(request) self.assertEqual(response.status_code, 204) @@ -391,13 +397,13 @@ def test_add_and_cancel_child_plugin(self): def test_action_token_per_session(self): # Assert that a cancel token for the same plugin # is different per user session. - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) @@ -414,10 +420,10 @@ def test_action_token_per_session(self): self.assertNotEqual(action_token_1, action_token_2) def test_add_and_cancel_plugin_permissions(self): - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") - endpoint = self.get_add_plugin_uri(simple_placeholder, 'TextPlugin') + endpoint = self.get_add_plugin_uri(simple_placeholder, "TextPlugin") with self.login_user_context(self.superuser): response = self.client.post(endpoint, {}) @@ -428,25 +434,25 @@ def test_add_and_cancel_plugin_permissions(self): cms_plugin = CMSPlugin.objects.get(pk=text_plugin_pk) text_plugin_class = cms_plugin.get_plugin_class_instance() - endpoint = self.get_custom_admin_url(TextPlugin, 'revert_on_cancel') + endpoint = self.get_custom_admin_url(TextPlugin, "revert_on_cancel") # Assert a standard user (no staff) can't delete ghost plugin with self.login_user_context(self.get_standard_user()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, cms_plugin) - data = {'token': action_token} + data = {"token": action_token} response = self.client.post(endpoint, data) self.assertEqual(response.status_code, 403) - staff_user = self._create_user('addonly-staff', is_staff=True, is_superuser=False) + staff_user = self._create_user("addonly-staff", is_staff=True, is_superuser=False) self._give_cms_permissions(staff_user) - self._give_permission(staff_user, text_plugin_class.model, 'add') + self._give_permission(staff_user, text_plugin_class.model, "add") with self.login_user_context(staff_user): request = self.get_request() action_token = text_plugin_class.get_action_token(request, cms_plugin) - data = {'token': action_token} + data = {"token": action_token} response = self.client.post(endpoint, data) self.assertEqual(response.status_code, 204) @@ -456,13 +462,13 @@ def test_change_form_has_rendered_plugin_content(self): the child plugins are rendered as their contents passed as initial data to the text field. """ - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) @@ -477,7 +483,7 @@ def test_change_form_has_rendered_plugin_content(self): with self.login_user_context(self.get_superuser()): request = self.get_request() context = RequestContext(request) - context['request'] = request + context["request"] = request text_with_rendered_plugins = plugin_tags_to_admin_html( text=text_plugin.body, context=context, @@ -488,7 +494,7 @@ def test_change_form_has_rendered_plugin_content(self): self.assertEqual(response.status_code, 200) self.assertEqual( - response.context['adminform'].form['body'].value(), + response.context["adminform"].form["body"].value(), text_with_rendered_plugins, ) self.assertContains( @@ -501,13 +507,13 @@ def test_only_inline_editing_has_rendered_plugin_content(self): """ Tests of child plugins of a TextPlugin are rendered correctly in edit mode """ - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") # import pdb; pdb.set_trace() text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="
I'm the first
", ) @@ -520,12 +526,10 @@ def test_only_inline_editing_has_rendered_plugin_content(self): from djangocms_versioning.constants import DRAFT edit_endpoint = get_object_edit_url( - PageContent._original_manager.filter( - page=simple_page, language='en', version__state=DRAFT - ).first() + PageContent._original_manager.filter(page=simple_page, language="en", version__state=DRAFT).first() ) else: - edit_endpoint = get_object_edit_url(simple_page.get_content_obj(language='en')) + edit_endpoint = get_object_edit_url(simple_page.get_content_obj(language="en")) else: edit_endpoint = simple_page.get_absolute_url() with self.login_user_context(self.get_superuser()): @@ -543,13 +547,13 @@ def test_user_cant_edit_child_plugins_directly(self): No user regardless of permissions can modify the contents of a child plugin directly in the text plugin text. """ - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) @@ -572,36 +576,36 @@ def test_user_cant_edit_child_plugins_directly(self): ) endpoint = self.get_change_plugin_uri(text_plugin) - response = self.client.post(endpoint, {'body': overridden_text}) + response = self.client.post(endpoint, {"body": overridden_text}) text_plugin.refresh_from_db() self.assertEqual(response.status_code, 200) self.assertXMLEqual(text_plugin.body, expected_text) def test_render_child_plugin_endpoint(self): - simple_page = self.create_page('test page', template='page.html', language='en') - simple_placeholder = self.get_placeholders(simple_page, 'en').get(slot='content') + simple_page = self.create_page("test page", template="page.html", language="en") + simple_placeholder = self.get_placeholders(simple_page, "en").get(slot="content") text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) text_plugin_class = text_plugin.get_plugin_class_instance() - child_plugin = self._add_child_plugin(text_plugin, 'LinkPlugin') + child_plugin = self._add_child_plugin(text_plugin, "LinkPlugin") text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin) - endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') - endpoint += f'?token={action_token}&plugin={child_plugin.pk}' + endpoint = self.get_custom_admin_url(TextPlugin, "render_plugin") + endpoint += f"?token={action_token}&plugin={child_plugin.pk}" response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) context = RequestContext(request) - context['request'] = request + context["request"] = request rendered_content = _render_cms_plugin(child_plugin, context) rendered_child_plugin = plugin_to_tag( child_plugin, @@ -611,55 +615,57 @@ def test_render_child_plugin_endpoint(self): self.maxDiff = None self.assertEqual(force_str(response.content), rendered_child_plugin) - child_plugin = self._add_child_plugin(text_plugin, plugin_type='PreviewDisabledPlugin') + child_plugin = self._add_child_plugin(text_plugin, plugin_type="PreviewDisabledPlugin") text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin) - endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') - endpoint += f'?token={action_token}&plugin={child_plugin.pk}' + endpoint = self.get_custom_admin_url(TextPlugin, "render_plugin") + endpoint += f"?token={action_token}&plugin={child_plugin.pk}" response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) # it is important that we do not add any extra whitespace inside of #Please CLICK ON LINK1 to go to link1.
' - add_plugin(self.placeholder, 'TextPlugin', 'en', body=raw_content) + add_plugin(self.placeholder, "TextPlugin", "en", body=raw_content) - plugin = self._export_page()[0]['plugins'][0] - result, children_included_in_this_content = TextPlugin.get_translation_export_content('body', plugin['data']) + plugin = self._export_page()[0]["plugins"][0] + result, children_included_in_this_content = TextPlugin.get_translation_export_content("body", plugin["data"]) self.assertEqual(result, raw_content) self.assertEqual(children_included_in_this_content, []) @@ -1021,8 +1036,8 @@ def test_textfield_without_children(self): self.assertDictEqual(result, {}) def test_textfield_with_children(self): - parent = add_plugin(self.placeholder, 'TextPlugin', 'en', body='') - child1 = add_plugin(self.placeholder, 'DummyLinkPlugin', 'en', target=parent, label='CLICK ON LINK1') + parent = add_plugin(self.placeholder, "TextPlugin", "en", body="") + child1 = add_plugin(self.placeholder, "DummyLinkPlugin", "en", target=parent, label="CLICK ON LINK1") parent_body = ( 'Please
Please
Please
Please to go to link1 ' + "
Please to go to link1 "
'or
This is cool