Skip to content

Commit

Permalink
Add tests for link widget and compiled translations
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 27, 2024
1 parent 49531b8 commit 9f27a4c
Show file tree
Hide file tree
Showing 67 changed files with 129 additions and 13 deletions.
1 change: 0 additions & 1 deletion djangocms_link/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class DjangoCmsLinkConfig(AppConfig):

def ready(self):
# Only scan admins after all apps are loaded

from django.contrib import admin

from djangocms_link import admin as link_admin
Expand Down
9 changes: 6 additions & 3 deletions djangocms_link/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class SiteAutocompleteSelect(AutocompleteSelect):
no_sites = None

def __init__(self, attrs=None):
# Hack: Pretend that the user is selecting a site for a Page object
try:
from cms.models.pagemodel import TreeNode

Expand Down Expand Up @@ -151,6 +152,7 @@ class LinkWidget(MultiWidget):
template_name = "djangocms_link/admin/link_widget.html"
data_pos = {}
number_sites = None
default_site_selector = getattr(settings, "DJANGOCMS_LINK_SITE_SELECTOR", False)

class Media:
js = ("djangocms_link/link-widget.js",)
Expand All @@ -159,7 +161,7 @@ class Media:
def __init__(self, site_selector=None):

if site_selector is None:
site_selector = getattr(settings, "DJANGOCMS_LINK_SITE_SELECTOR", False)
site_selector = LinkWidget.default_site_selector

widgets = [
Select(
Expand Down Expand Up @@ -254,7 +256,8 @@ def __init__(self, *args, **kwargs):
def prepare_value(self, value):
if isinstance(value, list):
return value

Check warning on line 258 in djangocms_link/fields.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/fields.py#L258

Added line #L258 was not covered by tests

if value is None:
value = {}
multi_value = len(self.widget.widgets) * [None]
if "external_link" in value:
pos = self._get_pos("external_link")
Expand Down Expand Up @@ -309,7 +312,7 @@ class LinkField(JSONField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("default", dict)
kwargs.setdefault("blank", True)
kwargs.setdefault("help_text", "-")
kwargs.setdefault("help_text", "-") # Help text is set by the widget
super().__init__(*args, **kwargs)

def formfield(self, **kwargs):
Expand Down
Binary file added djangocms_link/locale/af/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ar/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/be/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/bg/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/bn/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ca/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/cmn/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/cs/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/cy/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/da/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/el/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/en/LC_MESSAGES/django.mo
Binary file not shown.
16 changes: 8 additions & 8 deletions djangocms_link/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ msgstr ""

#: djangocms_link/cms_plugins.py:15 djangocms_link/models.py:73
msgid "Link"
msgstr ""
msgstr "Link"

#: djangocms_link/cms_plugins.py:27
msgid "Advanced settings"
msgstr ""
msgstr "Advanced settings"

#: djangocms_link/fields.py:26 djangocms_link/forms.py:15
msgid "Internal link"
msgstr ""
msgstr "Internal link"

#: djangocms_link/fields.py:27
msgid "External link/anchor"
Expand All @@ -47,7 +47,7 @@ msgstr ""

#: djangocms_link/fields.py:175
msgid "https://example.com or #anchor"
msgstr ""
msgstr "https://example.com or #anchor"

#: djangocms_link/fields.py:177
msgid ""
Expand Down Expand Up @@ -80,7 +80,7 @@ msgstr ""

#: djangocms_link/fields.py:215
msgid "Select site"
msgstr ""
msgstr "Select site"

#: djangocms_link/fields.py:249
msgid "Select a link type and provide a link."
Expand Down Expand Up @@ -128,12 +128,12 @@ msgstr ""

#: djangocms_link/models.py:118
msgid "Link is required."
msgstr ""
msgstr "Link is required."

#: djangocms_link/validators.py:50
msgid "Enter a valid anchor"
msgstr ""
msgstr "Enter a valid anchor"

#: djangocms_link/validators.py:84
msgid "Enter a valid phone number"
msgstr ""
msgstr "Enter a valid phone number"
Binary file added djangocms_link/locale/en_GB/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/en_US/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/eo/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/es/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/et/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/eu/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/fa/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/fi/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ga/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/gl/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/gu/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/he/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/hi/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/hr/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/hu/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/id/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/is/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/it/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ja/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ka/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/kk/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/km/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ko/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/lt/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/lv/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/mn/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ms/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/mt/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/nb/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/nl/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/no/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/pl/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/pt/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ro/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/sk/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/sl/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/sq/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/sr/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/sv/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ta/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/th/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/tr/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ug/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/uk/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/ur/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/vi/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/zh-Hans/LC_MESSAGES/django.mo
Binary file not shown.
Binary file added djangocms_link/locale/zh/LC_MESSAGES/django.mo
Binary file not shown.
116 changes: 115 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,119 @@
from django import forms
from django.test import TestCase

from cms.api import create_page

from djangocms_link.fields import LinkFormField, LinkWidget
from tests.helpers import get_filer_file


class LinkFieldTestCase(TestCase):
pass
def setUp(self):
self.page = create_page(
title='django CMS is fun',
template='page.html',
language='en',
)
self.file = get_filer_file()

def test_form_field_rendering(self):
class LinkForm(forms.Form):
link_field = LinkFormField(required=True)

form = LinkForm()
form_html = str(form)

# Render widget
self.assertIn(
'<div name="link_field" class="link-widget widget" required id="id_link_field">',
form_html
)
# Render selector
self.assertIn(
'<select name="link_field_0" class="js-link-widget-selector" data-help="No destination selected. '
'Use the dropdown to select a destination." required id="id_link_field_0">'
'<option value="internal_link">Internal link</option>'
'<option value="external_link">External link/anchor</option>'
'<option value="file_link">File link</option></select>',
form_html
)
# Render internal URL field
self.assertIn(
'<select name="link_field_2" widget="internal_link" '
'data-help="Select from available internal destinations. Optionally, add an anchor to scroll to." '
'data-placeholder="" required id="id_link_field_2" class="admin-autocomplete" data-ajax--cache="true" '
'data-ajax--delay="250" data-ajax--type="GET" data-ajax--url="/en/admin/djangocms_link/link/urls" '
'data-theme="admin-autocomplete" data-allow-clear="true" lang="en">'
'<option value=""></option><option value="" selected>None</option>'
'</select>',
form_html
)
# Render external URL field
self.assertIn(
'<input type="url" name="link_field_1" widget="external_link" '
'placeholder="https://example.com or #anchor" '
'data-help="Provide a link to an external URL, including the schema such as &#x27;https://&#x27;, '
'&#x27;tel:&#x27;, or &#x27;mailto:&#x27;. Optionally, add an #anchor (including the #) to scroll to." '
'required id="id_link_field_1">',
form_html,
)

class LinkNotRequiredForm(forms.Form):
link_field = LinkFormField(required=False)

# Render selector with empty option
self.assertIn(
'<select name="link_field_0" class="js-link-widget-selector" data-help="No destination selected. '
'Use the dropdown to select a destination." id="id_link_field_0">'
'<option value="empty">---------</option>'
'<option value="internal_link">Internal link</option>'
'<option value="external_link">External link/anchor</option>'
'<option value="file_link">File link</option></select>',
str(LinkNotRequiredForm())
)

def prepare_value(self, form, value):
return form.fields["link_field"].prepare_value(value)

def test_form_field_transparency(self):
class LinkForm(forms.Form):
link_field = LinkFormField(required=False)

def check_value(value):
# Creates submission data for value and checks if the form returns the submitted value
data = {f"link_field_{i}": item for i, item in enumerate(self.prepare_value(LinkForm(), value))}
form = LinkForm(data=data)
self.assertTrue(form.is_valid(), form.errors)
self.assertEqual(form.cleaned_data["link_field"], value)

check_value({"internal_link": f"cms.page:{self.page.id}", "anchor": "#anchor"})
check_value({"external_link": "https://example.com"})
check_value({"external_link": "#anchor"})
check_value({"file_link": str(self.file.id)})
check_value({})

def test_widget_renders_selection(self):
widget = LinkWidget()
pre_select_page = len(widget.widgets) * [None]
pre_select_page[0] = "internal_link"
pre_select_page[2] = f"cms.page:{self.page.id}"
rendered_widget = widget.render("link_field", pre_select_page, attrs={"id": "id_link_field"})

self.assertIn('<option value="cms.page:1" selected>django CMS is fun</option>', rendered_widget)

def test_widget_renders_site_selector(self):
widget = LinkWidget(site_selector=True)
pre_select_page = len(widget.widgets) * [None]
pre_select_page[0] = "internal_link"
pre_select_page[2] = f"cms.page:{self.page.id}"
rendered_widget = widget.render("link_field", pre_select_page, attrs={"id": "id_link_field"})

# Subwidget is present
self.assertIn(
'<select name="link_field_2" class="js-link-site-widget admin-autocomplete" widget="site"',
rendered_widget
)
# Current site is pre-selected
self.assertIn('<option value="1" selected>example.com</option>', rendered_widget)
# Site selector uses django admin autocomplete
self.assertIn('data-ajax--url="/en/admin/autocomplete/"', rendered_widget)

0 comments on commit 9f27a4c

Please sign in to comment.