diff --git a/wagtail/wagtailcore/tests/test_streamfield.py b/wagtail/wagtailcore/tests/test_streamfield.py index c85a2df9d2..bfec0be12d 100644 --- a/wagtail/wagtailcore/tests/test_streamfield.py +++ b/wagtail/wagtailcore/tests/test_streamfield.py @@ -1,8 +1,13 @@ import json +import unittest +import django from django.apps import apps from django.test import TestCase from django.db import models +from django.template import Template, Context +from django.utils.safestring import SafeText +from django.utils.six import text_type from wagtail.tests.testapp.models import StreamModel from wagtail.wagtailcore import blocks @@ -128,3 +133,56 @@ class TestStreamValueAccess(TestCase): self.assertEqual(len(fetched_body), 1) self.assertIsInstance(fetched_body[0].value, RichText) self.assertEqual(fetched_body[0].value.source, "

hello world

") + + +class TestStreamFieldRenderingBase(TestCase): + def setUp(self): + self.image = Image.objects.create( + title='Test image', + file=get_test_image_file()) + + self.instance = StreamModel.objects.create(body=json.dumps([ + {'type': 'rich_text', 'value': '

Rich text

'}, + {'type': 'image', 'value': self.image.pk}, + {'type': 'text', 'value': 'Hello, World!'}])) + + img_tag = self.image.get_rendition('original').img_tag() + self.expected = ''.join([ + '

Rich text

', + '
{}
'.format(img_tag), + '
Hello, World!
', + ]) + + +class TestStreamFieldRendering(TestStreamFieldRenderingBase): + def test_to_string(self): + rendered = text_type(self.instance.body) + self.assertHTMLEqual(rendered, self.expected) + self.assertIsInstance(rendered, SafeText) + + +class TestStreamFieldDjangoRendering(TestStreamFieldRenderingBase): + def render(self, string, context): + return Template(string).render(Context(context)) + + def test_render(self): + rendered = self.render('{{ instance.body }}', { + 'instance': self.instance}) + self.assertHTMLEqual(rendered, self.expected) + + +@unittest.skipIf(django.VERSION < (1, 8), 'Multiple engines only supported in Django>=1.8') +class TestStreamFieldJinjaRendering(TestStreamFieldRenderingBase): + def setUp(self): + # This does not exist on Django<1.8 + super(TestStreamFieldJinjaRendering, self).setUp() + from django.template import engines + self.engine = engines['jinja2'] + + def render(self, string, context): + return self.engine.from_string(string).render(context) + + def test_render(self): + rendered = self.render('{{ instance.body }}', { + 'instance': self.instance}) + self.assertHTMLEqual(rendered, self.expected)