def test_assert_can_create_with_form_helpers(self): # same as test_assert_can_create, but using the helpers from wagtail.test.utils.form_data # as an end-to-end test self.assertFalse(EventIndex.objects.exists()) self.assertCanCreate( self.root, EventIndex, nested_form_data({ "title": "Event Index", "intro": rich_text("<p>Event intro</p>") }), ) self.assertTrue(EventIndex.objects.exists()) self.assertCanCreate( self.root, StreamPage, nested_form_data({ "title": "Flierp", "body": streamfield([ ("text", "Dit is onze mooie text"), ( "rich_text", rich_text( "<p>Dit is onze mooie text in een ferrari</p>"), ), ("product", { "name": "pegs", "price": "a pound" }), ]), }), ) self.assertCanCreate( self.root, SectionedRichTextPage, nested_form_data({ "title": "Fight Club", "sections": inline_formset([ { "body": rich_text( "<p>Rule 1: You do not talk about Fight Club</p>") }, { "body": rich_text( "<p>Rule 2: You DO NOT talk about Fight Club</p>") }, ]), }), )
def test_rich_text_with_custom_features(self): # feature list doesn't allow <h2>, so it should become an unstyled paragraph block result = rich_text("<h2>title</h2><p>para</p>", features=["p"]) self.assertTrue( content_state_equal( json.loads(result), { "entityMap": {}, "blocks": [ { "inlineStyleRanges": [], "text": "title", "depth": 0, "type": "unstyled", "key": "00000", "entityRanges": [], }, { "inlineStyleRanges": [], "text": "para", "depth": 0, "type": "unstyled", "key": "00000", "entityRanges": [], }, ], }, ))
def test_default_rich_text(self): result = rich_text("<h2>title</h2><p>para</p>") self.assertTrue( content_state_equal( json.loads(result), { "entityMap": {}, "blocks": [ { "inlineStyleRanges": [], "text": "title", "depth": 0, "type": "header-two", "key": "00000", "entityRanges": [], }, { "inlineStyleRanges": [], "text": "para", "depth": 0, "type": "unstyled", "key": "00000", "entityRanges": [], }, ], }, ))
def test_max_length_validation(self): EventIndexForm = modelform_factory(model=EventIndex, fields=["intro"]) form = EventIndexForm( {"intro": rich_text("<p><i>less</i> than 50 characters</p>")}) self.assertTrue(form.is_valid()) form = EventIndexForm({ "intro": rich_text( "<p>a piece of text that is considerably longer than the limit of fifty characters of text</p>" ) }) self.assertFalse(form.is_valid()) form = EventIndexForm({ "intro": rich_text( '<p><a href="http://a-domain-name-that-would-put-us-over-the-limit-if-we-were-counting-it.example.com/">less</a> than 50 characters</p>' ) }) self.assertTrue(form.is_valid())
def test_rich_text_with_alternative_editor(self): result = rich_text("<h2>title</h2><p>para</p>", editor="custom") self.assertEqual(result, "<h2>title</h2><p>para</p>")