|
@@ -7,7 +7,6 @@ from django.template import Context, Template, TemplateSyntaxError
|
|
|
from django.test import SimpleTestCase, TestCase
|
|
|
from django.test.utils import override_settings
|
|
|
from django.utils import timezone
|
|
|
-from django.utils.html import format_html
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
from wagtail.admin.staticfiles import versioned_static
|
|
@@ -20,7 +19,6 @@ from wagtail.admin.templatetags.wagtailadmin_tags import (
|
|
|
timesince_simple,
|
|
|
)
|
|
|
from wagtail.admin.templatetags.wagtailadmin_tags import locales as locales_tag
|
|
|
-from wagtail.admin.ui.components import Component
|
|
|
from wagtail.images.tests.utils import get_test_image_file
|
|
|
from wagtail.models import Locale
|
|
|
from wagtail.test.utils import WagtailTestUtils
|
|
@@ -222,89 +220,6 @@ class TestTimesinceTags(SimpleTestCase):
|
|
|
self.assertIn('data-w-tooltip-placement-value="bottom"', html)
|
|
|
|
|
|
|
|
|
-class TestComponentTag(SimpleTestCase):
|
|
|
- def test_passing_context_to_component(self):
|
|
|
- class MyComponent(Component):
|
|
|
- def render_html(self, parent_context):
|
|
|
- return format_html(
|
|
|
- "<h1>{} was here</h1>", parent_context.get("first_name", "nobody")
|
|
|
- )
|
|
|
-
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}{% with first_name='Kilroy' %}{% component my_component %}{% endwith %}"
|
|
|
- )
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(html, "<h1>Kilroy was here</h1>")
|
|
|
-
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}{% component my_component with first_name='Kilroy' %}"
|
|
|
- )
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(html, "<h1>Kilroy was here</h1>")
|
|
|
-
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}{% with first_name='Kilroy' %}{% component my_component with surname='Silk' only %}{% endwith %}"
|
|
|
- )
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(html, "<h1>nobody was here</h1>")
|
|
|
-
|
|
|
- def test_fallback_render_method(self):
|
|
|
- class MyComponent(Component):
|
|
|
- def render_html(self, parent_context):
|
|
|
- return format_html("<h1>I am a component</h1>")
|
|
|
-
|
|
|
- class MyNonComponent:
|
|
|
- def render(self):
|
|
|
- return format_html("<h1>I am not a component</h1>")
|
|
|
-
|
|
|
- template = Template("{% load wagtailadmin_tags %}{% component my_component %}")
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(html, "<h1>I am a component</h1>")
|
|
|
- with self.assertRaises(ValueError):
|
|
|
- template.render(Context({"my_component": MyNonComponent()}))
|
|
|
-
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}{% component my_component fallback_render_method=True %}"
|
|
|
- )
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(html, "<h1>I am a component</h1>")
|
|
|
- html = template.render(Context({"my_component": MyNonComponent()}))
|
|
|
- self.assertEqual(html, "<h1>I am not a component</h1>")
|
|
|
-
|
|
|
- def test_component_escapes_unsafe_strings(self):
|
|
|
- class MyComponent(Component):
|
|
|
- def render_html(self, parent_context):
|
|
|
- return "Look, I'm running with scissors! 8< 8< 8<"
|
|
|
-
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}<h1>{% component my_component %}</h1>"
|
|
|
- )
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(
|
|
|
- html, "<h1>Look, I'm running with scissors! 8< 8< 8<</h1>"
|
|
|
- )
|
|
|
-
|
|
|
- def test_error_on_rendering_non_component(self):
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}<h1>{% component my_component %}</h1>"
|
|
|
- )
|
|
|
-
|
|
|
- with self.assertRaises(ValueError) as cm:
|
|
|
- template.render(Context({"my_component": "hello"}))
|
|
|
- self.assertEqual(str(cm.exception), "Cannot render 'hello' as a component")
|
|
|
-
|
|
|
- def test_render_as_var(self):
|
|
|
- class MyComponent(Component):
|
|
|
- def render_html(self, parent_context):
|
|
|
- return format_html("<h1>I am a component</h1>")
|
|
|
-
|
|
|
- template = Template(
|
|
|
- "{% load wagtailadmin_tags %}{% component my_component as my_html %}The result was: {{ my_html }}"
|
|
|
- )
|
|
|
- html = template.render(Context({"my_component": MyComponent()}))
|
|
|
- self.assertEqual(html, "The result was: <h1>I am a component</h1>")
|
|
|
-
|
|
|
-
|
|
|
@override_settings(
|
|
|
WAGTAIL_CONTENT_LANGUAGES=[
|
|
|
("en", "English"),
|