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 # - rendered_child_plugin = ('' - 'Preview is disabled for this plugin' - '') + rendered_child_plugin = ( + "' + "Preview is disabled for this plugin" + "" + ) self.assertEqual(force_str(response.content), rendered_child_plugin) def test_render_child_plugin_endpoint_calls_context_processors(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, - plugin_type='SekizaiPlugin', + plugin_type="SekizaiPlugin", ) 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, @@ -674,12 +680,12 @@ def test_render_child_plugin_permissions(self): Users can't render a child plugin without change permissions on the placeholder attached object and the 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") text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the first", ) text_plugin_class = text_plugin.get_plugin_class_instance() @@ -689,11 +695,11 @@ def test_render_child_plugin_permissions(self): with self.login_user_context(self.get_standard_user()): 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.assertContains(response, '

403 Forbidden

', status_code=403, html=True) + self.assertContains(response, "

403 Forbidden

", status_code=403, html=True) def test_render_child_plugin_token_validation(self): """ @@ -701,12 +707,12 @@ def test_render_child_plugin_token_validation(self): was created in the current session and it's text plugin matches the child plugin parent. """ - 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() @@ -722,17 +728,17 @@ def test_render_child_plugin_token_validation(self): with self.login_user_context(self.get_superuser()): 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, 400) - self.assertEqual(force_str(response.content), 'Unable to process your request. Invalid token.') + self.assertEqual(force_str(response.content), "Unable to process your request. Invalid token.") text_plugin_2 = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', + "TextPlugin", + "en", body="I'm the second", ) @@ -742,34 +748,34 @@ def test_render_child_plugin_token_validation(self): with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin_2) - 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, 204) - self.assertEqual(force_str(response.content), '') + self.assertEqual(force_str(response.content), "") def test_custom_ckeditor_body_css_classes(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") parent_plugin = add_plugin( simple_placeholder, DummyParentPlugin, - 'en', + "en", label=DummyParentPlugin._ckeditor_body_class_label_trigger, ) child_plugin = add_plugin( simple_placeholder, DummyChildPlugin, - 'en', + "en", target=parent_plugin, ) text_plugin = add_plugin( simple_placeholder, - 'TextPlugin', - 'en', - body='Content', + "TextPlugin", + "en", + body="Content", target=child_plugin, ) @@ -780,14 +786,14 @@ def test_custom_ckeditor_body_css_classes(self): self.assertContains(response, DummyChildPlugin.child_ckeditor_body_css_class) def test_render_plugin(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 = self._add_text_plugin(simple_placeholder) for i in range(0, 10): plugin = self._add_child_plugin( text_plugin, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", data_suffix=i, ) @@ -795,21 +801,21 @@ def test_render_plugin(self): with self.assertNumQueries(2): request = self.get_request() context = RequestContext(request) - context['request'] = request + context["request"] = request rendered = _render_cms_plugin(text_plugin, context) for i in range(0, 10): - self.assertTrue('LinkPlugin record %d' % i in rendered) + self.assertTrue("LinkPlugin record %d" % i in rendered) def test_render_extended_plugin(self): - 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 = self._add_text_plugin(simple_placeholder, 'ExtendedTextPlugin') + 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 = self._add_text_plugin(simple_placeholder, "ExtendedTextPlugin") for i in range(0, 10): plugin = self._add_child_plugin( text_plugin, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", data_suffix=i, ) @@ -818,60 +824,60 @@ def test_render_extended_plugin(self): with self.assertNumQueries(2): request = self.get_request() context = RequestContext(request) - context['request'] = request + context["request"] = request rendered = _render_cms_plugin(text_plugin, context) for i in range(0, 10): - self.assertTrue('LinkPlugin record %d' % i in rendered) + self.assertTrue("LinkPlugin record %d" % i in rendered) def test_copy_plugin_integrity(self): """ Test that copying of textplugins replaces references to copied plugins """ - 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 = self._add_text_plugin(simple_placeholder) child_plugin_1 = self._add_child_plugin( text_plugin, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_1) child_plugin_2 = self._add_child_plugin( text_plugin, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_2) # create a page translation to copy plugins to translation = create_title( - 'fr', - 'test-page-fr', + "fr", + "test-page-fr", simple_page, - slug='test-page-fr', + slug="test-page-fr", ) - self.assertEqual(CMSPlugin.objects.filter(language='en').count(), 3) + self.assertEqual(CMSPlugin.objects.filter(language="en").count(), 3) self.assertEqual(CMSPlugin.objects.filter(language=translation.language).count(), 0) data = { - 'source_placeholder_id': simple_placeholder.pk, - 'target_placeholder_id': simple_placeholder.pk, - 'target_language': translation.language, - 'source_language': 'en', + "source_placeholder_id": simple_placeholder.pk, + "target_placeholder_id": simple_placeholder.pk, + "target_language": translation.language, + "source_language": "en", } - endpoint = self.get_admin_url(Placeholder if DJANGO_CMS4 else Page, 'copy_plugins') - endpoint += '?' + urlencode({'cms_path': '/en/'}) + endpoint = self.get_admin_url(Placeholder if DJANGO_CMS4 else Page, "copy_plugins") + endpoint += "?" + urlencode({"cms_path": "/en/"}) with self.login_user_context(self.superuser): response = self.client.post(endpoint, data) self.assertEqual(response.status_code, 200) - self.assertEqual(CMSPlugin.objects.filter(language='en').count(), 3) + self.assertEqual(CMSPlugin.objects.filter(language="en").count(), 3) self.assertEqual(CMSPlugin.objects.filter(language=translation.language).count(), 3) plugins = list(CMSPlugin.objects.order_by("id")) # Look at the order of creation @@ -881,21 +887,21 @@ def test_copy_plugin_integrity(self): self.assertEqual(idlist, expected) def test_copy_plugin_callback(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_1 = self._add_text_plugin(simple_placeholder) child_plugin_1_a = self._add_child_plugin( text_plugin_1, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", ) text_plugin_1 = self.add_plugin_to_text(text_plugin_1, child_plugin_1_a) child_plugin_1_b = self._add_child_plugin( text_plugin_1, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", ) text_plugin_1 = self.add_plugin_to_text(text_plugin_1, child_plugin_1_b) @@ -906,11 +912,11 @@ def test_copy_plugin_callback(self): child_plugin_2_a = self._add_child_plugin( text_plugin_2, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", ) child_plugin_2_b = self._add_child_plugin( text_plugin_2, - plugin_type='LinkPlugin', + plugin_type="LinkPlugin", ) source_map = { child_plugin_1_a.pk: child_plugin_2_a, @@ -934,85 +940,94 @@ def test_plugin_tags_to_id_list(self): self.assertEqual(plugin_tags_to_id_list(markup), expected) def test_text_plugin_xss(self): - page = self.create_page('test page', template='page.html', language='en') - placeholder = self.get_placeholders(page, 'en').get(slot='content') - plugin = add_plugin(placeholder, 'TextPlugin', 'en', body='body') + page = self.create_page("test page", template="page.html", language="en") + placeholder = self.get_placeholders(page, "en").get(slot="content") + plugin = add_plugin(placeholder, "TextPlugin", "en", body="body") endpoint = self.get_change_plugin_uri(plugin) with self.login_user_context(self.superuser): data = { - 'body': ( + "body": ( '
divcontent
acontent' ), } response = self.client.post(endpoint, data) self.assertEqual(response.status_code, 200) - self.assertEqual(self.reload(plugin).body, '
divcontent
acontent') + self.assertEqual(self.reload(plugin).body, "
divcontent
acontent") def test_url_resolution(self): - page = self.create_page('test page', template='page.html', language='en') - endpoint = admin_reverse('djangocms_text_textplugin_get_available_urls') + page = self.create_page("test page", template="page.html", language="en") + endpoint = admin_reverse("djangocms_text_textplugin_get_available_urls") with self.login_user_context(self.superuser): - result = self.client.get(endpoint + f'?g=cms.page:{page.pk}') + result = self.client.get(endpoint + f"?g=cms.page:{page.pk}") self.assertEqual(result.status_code, 200) self.assertEqual(result.json()["url"], page.get_absolute_url()) def test_failed_url_resolution(self): - page = self.create_page('test page', template='page.html', language='en') - endpoint = admin_reverse('djangocms_text_textplugin_get_available_urls') + page = self.create_page("test page", template="page.html", language="en") + endpoint = admin_reverse("djangocms_text_textplugin_get_available_urls") with self.login_user_context(self.superuser): - result = self.client.get(endpoint + f'?g=cms.page:{page.pk + 1}') + result = self.client.get(endpoint + f"?g=cms.page:{page.pk + 1}") self.assertEqual(result.status_code, 200) self.assertEqual(result.json(), {"error": "Page matching query does not exist."}) def test_url_query(self): - page = self.create_page('test page', template='page.html', language='en') - endpoint = admin_reverse('djangocms_text_textplugin_get_available_urls') + page = self.create_page("test page", template="page.html", language="en") + endpoint = admin_reverse("djangocms_text_textplugin_get_available_urls") with self.login_user_context(self.superuser): - result = self.client.get(endpoint + '?q=test') + result = self.client.get(endpoint + "?q=test") self.assertEqual( result.json()["results"], - [{'text': 'Pages', 'children': [{ - 'text': 'test page', - 'url': '/en/test-page/', - 'id': f'cms.page:{page.pk}', - 'verbose': 'test page'}]}]) + [ + { + "text": "Pages", + "children": [ + { + "text": "test page", + "url": "/en/test-page/", + "id": f"cms.page:{page.pk}", + "verbose": "test page", + } + ], + } + ], + ) def test_get_messages(self): - endpoint = admin_reverse('djangocms_text_textplugin_get_messages') + endpoint = admin_reverse("djangocms_text_textplugin_get_messages") with self.login_user_context(self.superuser): result = self.client.get(endpoint) # Just see that it returns a 200 - self.assertEqual(result.json(), {'messages': []}) + self.assertEqual(result.json(), {"messages": []}) @unittest.skipUnless( HAS_DJANGOCMS_TRANSLATIONS and HAS_DJANGOCMS_TRANSFER, - 'Optional dependencies for tests are not installed.', + "Optional dependencies for tests are not installed.", ) class DjangoCMSTranslationsIntegrationTestCase(BaseTestCase): def setUp(self): super().setUp() - self.page = self.create_page('test page', template='page.html', language='en') - self.placeholder = self.get_placeholders(self.page, 'en').get(slot='content') + self.page = self.create_page("test page", template="page.html", language="en") + self.placeholder = self.get_placeholders(self.page, "en").get(slot="content") def _export_page(self): - return json.loads(export_page(self.page, 'en')) + return json.loads(export_page(self.page, "en")) def test_textfield_without_children(self): raw_content = '

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 to go to link1.

' @@ -1030,23 +1045,20 @@ def test_textfield_with_children(self): parent.body = parent_body parent.save() - 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"]) - expected = ( - parent_body - .replace('>', '>CLICK ON LINK1', 1) - ) + expected = parent_body.replace(">", ">CLICK ON LINK1", 1) self.assertEqual(result, expected) self.assertEqual(children_included_in_this_content, [child1.pk]) result = TextPlugin.set_translation_import_content(result, plugin) - self.assertDictEqual(result, {child1.pk: 'CLICK ON LINK1'}) + self.assertDictEqual(result, {child1.pk: "CLICK ON LINK1"}) def test_textfield_with_multiple_children(self): - parent = add_plugin(self.placeholder, 'TextPlugin', 'en', body='') - child1 = add_plugin(self.placeholder, 'DummyLinkPlugin', 'en', target=parent, label='CLICK ON LINK1') - child2 = add_plugin(self.placeholder, 'DummyLinkPlugin', 'en', target=parent, label='CLICK ON LINK2') + parent = add_plugin(self.placeholder, "TextPlugin", "en", body="") + child1 = add_plugin(self.placeholder, "DummyLinkPlugin", "en", target=parent, label="CLICK ON LINK1") + child2 = add_plugin(self.placeholder, "DummyLinkPlugin", "en", target=parent, label="CLICK ON LINK2") parent_body = ( '

Please to go to link1 ' @@ -1056,24 +1068,22 @@ def test_textfield_with_multiple_children(self): parent.body = parent_body parent.save() - 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"]) - expected = ( - parent_body - .replace('>', '>CLICK ON LINK1', 1) - .replace('>', '>CLICK ON LINK2', 1) + expected = parent_body.replace(">", ">CLICK ON LINK1", 1).replace( + ">", ">CLICK ON LINK2", 1 ) self.assertEqual(result, expected) self.assertEqual(children_included_in_this_content, [child1.pk, child2.pk]) result = TextPlugin.set_translation_import_content(result, plugin) - self.assertDictEqual(result, {child1.pk: 'CLICK ON LINK1', child2.pk: 'CLICK ON LINK2'}) + self.assertDictEqual(result, {child1.pk: "CLICK ON LINK1", child2.pk: "CLICK ON LINK2"}) def test_textfield_with_multiple_children_one_deleted(self): - parent = add_plugin(self.placeholder, 'TextPlugin', 'en', body='') - child1 = add_plugin(self.placeholder, 'DummyLinkPlugin', 'en', target=parent, label='CLICK ON LINK1') - child2 = add_plugin(self.placeholder, 'DummyLinkPlugin', 'en', target=parent, label='CLICK ON LINK2') + parent = add_plugin(self.placeholder, "TextPlugin", "en", body="") + child1 = add_plugin(self.placeholder, "DummyLinkPlugin", "en", target=parent, label="CLICK ON LINK1") + child2 = add_plugin(self.placeholder, "DummyLinkPlugin", "en", target=parent, label="CLICK ON LINK2") parent_body = ( '

Please to go to link1 ' @@ -1083,14 +1093,14 @@ def test_textfield_with_multiple_children_one_deleted(self): parent.body = parent_body parent.save() - plugin = self._export_page()[0]['plugins'][0] + plugin = self._export_page()[0]["plugins"][0] child1.delete() - result, children_included_in_this_content = TextPlugin.get_translation_export_content('body', plugin['data']) + result, children_included_in_this_content = TextPlugin.get_translation_export_content("body", plugin["data"]) expected = ( - '

Please to go to link1 ' + "

Please to go to link1 " 'or CLICK ON LINK2 to go to link2.

' ).format(child2.pk) @@ -1098,11 +1108,11 @@ def test_textfield_with_multiple_children_one_deleted(self): self.assertEqual(children_included_in_this_content, [child2.pk]) result = TextPlugin.set_translation_import_content(result, plugin) - self.assertDictEqual(result, {child2.pk: 'CLICK ON LINK2'}) + self.assertDictEqual(result, {child2.pk: "CLICK ON LINK2"}) def test_textfield_with_untranslatable_children(self): - parent = add_plugin(self.placeholder, 'TextPlugin', 'en', body='') - child1 = add_plugin(self.placeholder, 'DummySpacerPlugin', 'en', target=parent) + parent = add_plugin(self.placeholder, "TextPlugin", "en", body="") + child1 = add_plugin(self.placeholder, "DummySpacerPlugin", "en", target=parent) parent_body = ( '

This is cool this is nice

' @@ -1110,14 +1120,12 @@ def test_textfield_with_untranslatable_children(self): parent.body = parent_body parent.save() - 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"]) - expected = ( - parent_body - ) + expected = parent_body self.assertEqual(result, expected) self.assertEqual(children_included_in_this_content, [child1.pk]) result = TextPlugin.set_translation_import_content(result, plugin) - self.assertDictEqual(result, {child1.pk: ''}) + self.assertDictEqual(result, {child1.pk: ""})