', result)
+ self.assertIn('alt="Test image 1"', result)
+ self.assertIn('
', result)
+ self.assertIn('alt="Test image 2"', result)
+
+ self.assertIsInstance(result, SafeString)
+ self.assertTrue(comparison.has_changed())
+
+
+class TestChoiceFieldComparison(TestCase):
+ comparison_class = compare.ChoiceFieldComparison
+
+ def test_hasnt_changed(self):
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("audience"),
+ EventPage(audience="public"),
+ EventPage(audience="public"),
+ )
+
+ self.assertTrue(comparison.is_field)
+ self.assertFalse(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Audience")
+ self.assertEqual(comparison.htmldiff(), "Public")
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertFalse(comparison.has_changed())
+
+ def test_has_changed(self):
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("audience"),
+ EventPage(audience="public"),
+ EventPage(audience="private"),
+ )
+
+ self.assertEqual(
+ comparison.htmldiff(),
+ '
PublicPrivate',
+ )
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertTrue(comparison.has_changed())
+
+ def test_from_none_to_value_only_shows_addition(self):
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("audience"),
+ EventPage(audience=None),
+ EventPage(audience="private"),
+ )
+
+ self.assertEqual(comparison.htmldiff(), '
Private')
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertTrue(comparison.has_changed())
+
+ def test_from_value_to_none_only_shows_deletion(self):
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("audience"),
+ EventPage(audience="public"),
+ EventPage(audience=None),
+ )
+
+ self.assertEqual(comparison.htmldiff(), '
Public')
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertTrue(comparison.has_changed())
+
+
+class TestTagsFieldComparison(TestCase):
+ comparison_class = compare.TagsFieldComparison
+
+ def test_hasnt_changed(self):
+ a = TaggedPage()
+ a.tags.add("wagtail")
+ a.tags.add("bird")
+
+ b = TaggedPage()
+ b.tags.add("wagtail")
+ b.tags.add("bird")
+
+ comparison = self.comparison_class(TaggedPage._meta.get_field("tags"), a, b)
+
+ self.assertTrue(comparison.is_field)
+ self.assertFalse(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Tags")
+ self.assertEqual(comparison.htmldiff(), "wagtail, bird")
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertFalse(comparison.has_changed())
+
+ def test_has_changed(self):
+ a = TaggedPage()
+ a.tags.add("wagtail")
+ a.tags.add("bird")
+
+ b = TaggedPage()
+ b.tags.add("wagtail")
+ b.tags.add("motacilla")
+
+ comparison = self.comparison_class(TaggedPage._meta.get_field("tags"), a, b)
+
+ self.assertEqual(
+ comparison.htmldiff(),
+ 'wagtail,
bird,
motacilla',
+ )
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertTrue(comparison.has_changed())
+
+
+class TestM2MFieldComparison(TestCase):
+ fixtures = ["test.json"]
+ comparison_class = compare.M2MFieldComparison
+
+ def setUp(self):
+ self.meetings_category = EventCategory.objects.create(name="Meetings")
+ self.parties_category = EventCategory.objects.create(name="Parties")
+ self.holidays_category = EventCategory.objects.create(name="Holidays")
+
+ def test_hasnt_changed(self):
+ christmas_event = EventPage.objects.get(url_path="/home/events/christmas/")
+ saint_patrick_event = EventPage.objects.get(
+ url_path="/home/events/saint-patrick/"
+ )
+
+ christmas_event.categories = [self.meetings_category, self.parties_category]
+ saint_patrick_event.categories = [self.meetings_category, self.parties_category]
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("categories"),
+ christmas_event,
+ saint_patrick_event,
+ )
+
+ self.assertTrue(comparison.is_field)
+ self.assertFalse(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Categories")
+ self.assertFalse(comparison.has_changed())
+ self.assertEqual(comparison.htmldiff(), "Meetings, Parties")
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+
+ def test_has_changed(self):
+ christmas_event = EventPage.objects.get(url_path="/home/events/christmas/")
+ saint_patrick_event = EventPage.objects.get(
+ url_path="/home/events/saint-patrick/"
+ )
+
+ christmas_event.categories = [self.meetings_category, self.parties_category]
+ saint_patrick_event.categories = [
+ self.meetings_category,
+ self.holidays_category,
+ ]
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("categories"),
+ christmas_event,
+ saint_patrick_event,
+ )
+
+ self.assertTrue(comparison.has_changed())
+ self.assertEqual(
+ comparison.htmldiff(),
+ 'Meetings,
Parties,
Holidays',
+ )
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+
+
+class TestForeignObjectComparison(TestCase):
+ comparison_class = compare.ForeignObjectComparison
+
+ @classmethod
+ def setUpTestData(cls):
+ image_model = get_image_model()
+ cls.test_image_1 = image_model.objects.create(
+ title="Test image 1",
+ file=get_test_image_file(),
+ )
+ cls.test_image_2 = image_model.objects.create(
+ title="Test image 2",
+ file=get_test_image_file(),
+ )
+
+ def test_hasnt_changed(self):
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("feed_image"),
+ EventPage(feed_image=self.test_image_1),
+ EventPage(feed_image=self.test_image_1),
+ )
+
+ self.assertTrue(comparison.is_field)
+ self.assertFalse(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Feed image")
+ self.assertEqual(comparison.htmldiff(), "Test image 1")
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertFalse(comparison.has_changed())
+
+ def test_has_changed(self):
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("feed_image"),
+ EventPage(feed_image=self.test_image_1),
+ EventPage(feed_image=self.test_image_2),
+ )
+
+ self.assertEqual(
+ comparison.htmldiff(),
+ '
Test image 1Test image 2',
+ )
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertTrue(comparison.has_changed())
+
+
+class TestForeignObjectComparisonWithCustomPK(TestCase):
+ """ForeignObjectComparison works with models declaring a custom primary key field"""
+
+ comparison_class = compare.ForeignObjectComparison
+
+ @classmethod
+ def setUpTestData(cls):
+ ad1 = AdvertWithCustomPrimaryKey.objects.create(
+ advert_id="ad1", text="Advert 1"
+ )
+ ad2 = AdvertWithCustomPrimaryKey.objects.create(
+ advert_id="ad2", text="Advert 2"
+ )
+ cls.test_obj_1 = SnippetChooserModelWithCustomPrimaryKey.objects.create(
+ advertwithcustomprimarykey=ad1
+ )
+ cls.test_obj_2 = SnippetChooserModelWithCustomPrimaryKey.objects.create(
+ advertwithcustomprimarykey=ad2
+ )
+
+ def test_hasnt_changed(self):
+ comparison = self.comparison_class(
+ SnippetChooserModelWithCustomPrimaryKey._meta.get_field(
+ "advertwithcustomprimarykey"
+ ),
+ self.test_obj_1,
+ self.test_obj_1,
+ )
+
+ self.assertTrue(comparison.is_field)
+ self.assertFalse(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Advertwithcustomprimarykey")
+ self.assertEqual(comparison.htmldiff(), "Advert 1")
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertFalse(comparison.has_changed())
+
+ def test_has_changed(self):
+ comparison = self.comparison_class(
+ SnippetChooserModelWithCustomPrimaryKey._meta.get_field(
+ "advertwithcustomprimarykey"
+ ),
+ self.test_obj_1,
+ self.test_obj_2,
+ )
+
+ self.assertEqual(
+ comparison.htmldiff(),
+ '
Advert 1Advert 2',
+ )
+ self.assertIsInstance(comparison.htmldiff(), SafeString)
+ self.assertTrue(comparison.has_changed())
+
+
+class TestChildRelationComparison(TestCase):
+ field_comparison_class = compare.FieldComparison
+ comparison_class = compare.ChildRelationComparison
+
+ def test_hasnt_changed(self):
+ # Two event pages with speaker called "Father Christmas". Neither of
+ # the speaker objects have an ID so this tests that the code can match
+ # the two together by field content.
+ event_page = EventPage(title="Event page", slug="event")
+ event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+ )
+
+ modified_event_page = EventPage(title="Event page", slug="event")
+ modified_event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("speaker"),
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ event_page,
+ modified_event_page,
+ )
+
+ self.assertFalse(comparison.is_field)
+ self.assertTrue(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Speaker")
+ self.assertFalse(comparison.has_changed())
+
+ # Check mapping
+ objs_a = list(comparison.val_a.all())
+ objs_b = list(comparison.val_b.all())
+ map_forwards, map_backwards, added, deleted = comparison.get_mapping(
+ objs_a, objs_b
+ )
+ self.assertEqual(map_forwards, {0: 0})
+ self.assertEqual(map_backwards, {0: 0})
+ self.assertEqual(added, [])
+ self.assertEqual(deleted, [])
+
+ def test_has_changed(self):
+ # Father Christmas renamed to Santa Claus. And Father Ted added.
+ # Father Christmas should be mapped to Father Ted because they
+ # are most alike. Santa claus should be displayed as "new"
+ event_page = EventPage(title="Event page", slug="event")
+ event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ sort_order=0,
+ )
+ )
+
+ modified_event_page = EventPage(title="Event page", slug="event")
+ modified_event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Santa",
+ last_name="Claus",
+ sort_order=0,
+ )
+ )
+ modified_event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Father",
+ last_name="Ted",
+ sort_order=1,
+ )
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("speaker"),
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ event_page,
+ modified_event_page,
+ )
+
+ self.assertFalse(comparison.is_field)
+ self.assertTrue(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Speaker")
+ self.assertTrue(comparison.has_changed())
+
+ # Check mapping
+ objs_a = list(comparison.val_a.all())
+ objs_b = list(comparison.val_b.all())
+ map_forwards, map_backwards, added, deleted = comparison.get_mapping(
+ objs_a, objs_b
+ )
+ self.assertEqual(map_forwards, {0: 1}) # Map Father Christmas to Father Ted
+ self.assertEqual(map_backwards, {1: 0}) # Map Father Ted to Father Christmas
+ self.assertEqual(added, [0]) # Add Santa Claus
+ self.assertEqual(deleted, [])
+
+ def test_has_changed_with_same_id(self):
+ # Father Christmas renamed to Santa Claus, but this time the ID of the
+ # child object remained the same. It should now be detected as the same
+ # object
+ event_page = EventPage(title="Event page", slug="event")
+ event_page.speakers.add(
+ EventPageSpeaker(
+ id=1,
+ first_name="Father",
+ last_name="Christmas",
+ sort_order=0,
+ )
+ )
+
+ modified_event_page = EventPage(title="Event page", slug="event")
+ modified_event_page.speakers.add(
+ EventPageSpeaker(
+ id=1,
+ first_name="Santa",
+ last_name="Claus",
+ sort_order=0,
+ )
+ )
+ modified_event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Father",
+ last_name="Ted",
+ sort_order=1,
+ )
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("speaker"),
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ event_page,
+ modified_event_page,
+ )
+
+ self.assertFalse(comparison.is_field)
+ self.assertTrue(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Speaker")
+ self.assertTrue(comparison.has_changed())
+
+ # Check mapping
+ objs_a = list(comparison.val_a.all())
+ objs_b = list(comparison.val_b.all())
+ map_forwards, map_backwards, added, deleted = comparison.get_mapping(
+ objs_a, objs_b
+ )
+ self.assertEqual(map_forwards, {0: 0}) # Map Father Christmas to Santa Claus
+ self.assertEqual(map_backwards, {0: 0}) # Map Santa Claus to Father Christmas
+ self.assertEqual(added, [1]) # Add Father Ted
+ self.assertEqual(deleted, [])
+
+ def test_hasnt_changed_with_different_id(self):
+ # Both of the child objects have the same field content but have a
+ # different ID so they should be detected as separate objects
+ event_page = EventPage(title="Event page", slug="event")
+ event_page.speakers.add(
+ EventPageSpeaker(
+ id=1,
+ first_name="Father",
+ last_name="Christmas",
+ )
+ )
+
+ modified_event_page = EventPage(title="Event page", slug="event")
+ modified_event_page.speakers.add(
+ EventPageSpeaker(
+ id=2,
+ first_name="Father",
+ last_name="Christmas",
+ )
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("speaker"),
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ event_page,
+ modified_event_page,
+ )
+
+ self.assertFalse(comparison.is_field)
+ self.assertTrue(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Speaker")
+ self.assertTrue(comparison.has_changed())
+
+ # Check mapping
+ objs_a = list(comparison.val_a.all())
+ objs_b = list(comparison.val_b.all())
+ map_forwards, map_backwards, added, deleted = comparison.get_mapping(
+ objs_a, objs_b
+ )
+ self.assertEqual(map_forwards, {})
+ self.assertEqual(map_backwards, {})
+ self.assertEqual(added, [0]) # Add new Father Christmas
+ self.assertEqual(deleted, [0]) # Delete old Father Christmas
+
+ def test_panel_label_as_field_label(self):
+ # Just to check whether passing `label` changes field_label
+ event_page = EventPage(title="Event page", slug="event")
+ event_page.speakers.add(
+ EventPageSpeaker(
+ first_name="Father",
+ )
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("speaker"),
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ )
+ ],
+ event_page,
+ event_page,
+ label="Speakers",
+ )
+
+ self.assertEqual(comparison.field_label(), "Speakers")
+
+
+class TestChildObjectComparison(TestCase):
+ field_comparison_class = compare.FieldComparison
+ comparison_class = compare.ChildObjectComparison
+
+ def test_same_object(self):
+ obj_a = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+
+ obj_b = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+
+ comparison = self.comparison_class(
+ EventPageSpeaker,
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ obj_a,
+ obj_b,
+ )
+
+ self.assertFalse(comparison.is_addition())
+ self.assertFalse(comparison.is_deletion())
+ self.assertFalse(comparison.has_changed())
+ self.assertEqual(comparison.get_position_change(), 0)
+ self.assertEqual(comparison.get_num_differences(), 0)
+
+ def test_different_object(self):
+ obj_a = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+
+ obj_b = EventPageSpeaker(
+ first_name="Santa",
+ last_name="Claus",
+ )
+
+ comparison = self.comparison_class(
+ EventPageSpeaker,
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ obj_a,
+ obj_b,
+ )
+
+ self.assertFalse(comparison.is_addition())
+ self.assertFalse(comparison.is_deletion())
+ self.assertTrue(comparison.has_changed())
+ self.assertEqual(comparison.get_position_change(), 0)
+ self.assertEqual(comparison.get_num_differences(), 2)
+
+ def test_moved_object(self):
+ obj_a = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ sort_order=1,
+ )
+
+ obj_b = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ sort_order=5,
+ )
+
+ comparison = self.comparison_class(
+ EventPageSpeaker,
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ obj_a,
+ obj_b,
+ )
+
+ self.assertFalse(comparison.is_addition())
+ self.assertFalse(comparison.is_deletion())
+ self.assertFalse(comparison.has_changed())
+ self.assertEqual(comparison.get_position_change(), 4)
+ self.assertEqual(comparison.get_num_differences(), 0)
+
+ def test_addition(self):
+ obj = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+
+ comparison = self.comparison_class(
+ EventPageSpeaker,
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ None,
+ obj,
+ )
+
+ self.assertTrue(comparison.is_addition())
+ self.assertFalse(comparison.is_deletion())
+ self.assertFalse(comparison.has_changed())
+ self.assertIsNone(comparison.get_position_change(), 0)
+ self.assertEqual(comparison.get_num_differences(), 0)
+
+ def test_deletion(self):
+ obj = EventPageSpeaker(
+ first_name="Father",
+ last_name="Christmas",
+ )
+
+ comparison = self.comparison_class(
+ EventPageSpeaker,
+ [
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("first_name"),
+ ),
+ partial(
+ self.field_comparison_class,
+ EventPageSpeaker._meta.get_field("last_name"),
+ ),
+ ],
+ obj,
+ None,
+ )
+
+ self.assertFalse(comparison.is_addition())
+ self.assertTrue(comparison.is_deletion())
+ self.assertFalse(comparison.has_changed())
+ self.assertIsNone(comparison.get_position_change())
+ self.assertEqual(comparison.get_num_differences(), 0)
+
+
+class TestChildRelationComparisonUsingPK(TestCase):
+ """Test related objects can be compred if they do not use id for primary key"""
+
+ field_comparison_class = compare.FieldComparison
+ comparison_class = compare.ChildRelationComparison
+
+ def test_has_changed_with_same_id(self):
+ # Head Count was changed but the PK of the child object remained the same.
+ # It should be detected as the same object
+
+ event_page = EventPage(title="Semi Finals", slug="semi-finals-2018")
+ event_page.head_counts.add(
+ HeadCountRelatedModelUsingPK(
+ custom_id=1,
+ head_count=22,
+ )
+ )
+
+ modified_event_page = EventPage(title="Semi Finals", slug="semi-finals-2018")
+ modified_event_page.head_counts.add(
+ HeadCountRelatedModelUsingPK(
+ custom_id=1,
+ head_count=23,
+ )
+ )
+ modified_event_page.head_counts.add(
+ HeadCountRelatedModelUsingPK(
+ head_count=25,
+ )
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("head_counts"),
+ [
+ partial(
+ self.field_comparison_class,
+ HeadCountRelatedModelUsingPK._meta.get_field("head_count"),
+ )
+ ],
+ event_page,
+ modified_event_page,
+ )
+
+ self.assertFalse(comparison.is_field)
+ self.assertTrue(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Head counts")
+ self.assertTrue(comparison.has_changed())
+
+ # Check mapping
+ objs_a = list(comparison.val_a.all())
+ objs_b = list(comparison.val_b.all())
+ map_forwards, map_backwards, added, deleted = comparison.get_mapping(
+ objs_a, objs_b
+ )
+ self.assertEqual(map_forwards, {0: 0}) # map head count 22 to 23
+ self.assertEqual(map_backwards, {0: 0}) # map head count 23 to 22
+ self.assertEqual(added, [1]) # add second head count
+ self.assertEqual(deleted, [])
+
+ def test_hasnt_changed_with_different_id(self):
+ # Both of the child objects have the same field content but have a
+ # different PK (ID) so they should be detected as separate objects
+ event_page = EventPage(title="Finals", slug="finals-event-abc")
+ event_page.head_counts.add(
+ HeadCountRelatedModelUsingPK(custom_id=1, head_count=220)
+ )
+
+ modified_event_page = EventPage(title="Finals", slug="finals-event-abc")
+ modified_event_page.head_counts.add(
+ HeadCountRelatedModelUsingPK(custom_id=2, head_count=220)
+ )
+
+ comparison = self.comparison_class(
+ EventPage._meta.get_field("head_counts"),
+ [
+ partial(
+ self.field_comparison_class,
+ HeadCountRelatedModelUsingPK._meta.get_field("head_count"),
+ )
+ ],
+ event_page,
+ modified_event_page,
+ )
+
+ self.assertFalse(comparison.is_field)
+ self.assertTrue(comparison.is_child_relation)
+ self.assertEqual(comparison.field_label(), "Head counts")
+ self.assertTrue(comparison.has_changed())
+
+ # Check mapping
+ objs_a = list(comparison.val_a.all())
+ objs_b = list(comparison.val_b.all())
+ map_forwards, map_backwards, added, deleted = comparison.get_mapping(
+ objs_a, objs_b
+ )
+ self.assertEqual(map_forwards, {})
+ self.assertEqual(map_backwards, {})
+ self.assertEqual(added, [0]) # Add new head count
+ self.assertEqual(deleted, [0]) # Delete old head count
diff --git a/testbed/wagtail__wagtail/wagtail/admin/tests/test_contentstate.py b/testbed/wagtail__wagtail/wagtail/admin/tests/test_contentstate.py
new file mode 100644
index 0000000000000000000000000000000000000000..e042f36943464e950f5587ccf26d69b33e2b56aa
--- /dev/null
+++ b/testbed/wagtail__wagtail/wagtail/admin/tests/test_contentstate.py
@@ -0,0 +1,2085 @@
+import json
+from unittest.mock import patch
+
+from django.test import TestCase
+from draftjs_exporter.dom import DOM
+from draftjs_exporter.html import HTML as HTMLExporter
+
+from wagtail.admin.rich_text.converters.contentstate import (
+ ContentstateConverter,
+ persist_key_for_block,
+)
+from wagtail.embeds.models import Embed
+
+
+def content_state_equal(v1, v2, match_keys=False):
+ "Test whether two contentState structures are equal, ignoring 'key' properties if match_keys=False"
+ if type(v1) != type(v2):
+ return False
+
+ if isinstance(v1, dict):
+ if set(v1.keys()) != set(v2.keys()):
+ return False
+ return all(
+ (k == "key" and not match_keys)
+ or content_state_equal(v, v2[k], match_keys=match_keys)
+ for k, v in v1.items()
+ )
+ elif isinstance(v1, list):
+ if len(v1) != len(v2):
+ return False
+ return all(
+ content_state_equal(a, b, match_keys=match_keys) for a, b in zip(v1, v2)
+ )
+ else:
+ return v1 == v2
+
+
+class TestHtmlToContentState(TestCase):
+ fixtures = ["test.json"]
+
+ def assertContentStateEqual(self, v1, v2, match_keys=False):
+ "Assert that two contentState structures are equal, ignoring 'key' properties if match_keys is False"
+ self.assertTrue(
+ content_state_equal(v1, v2, match_keys=match_keys),
+ "%s does not match %s"
+ % (json.dumps(v1, indent=4), json.dumps(v2, indent=4)),
+ )
+
+ def test_paragraphs(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
Hello world!
+
Goodbye world!
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Hello world!",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Goodbye world!",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00001",
+ "entityRanges": [],
+ },
+ ],
+ },
+ match_keys=True,
+ )
+
+ def test_unknown_block_becomes_paragraph(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
Hello world!
+
I said hello world!
+
Goodbye world!
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Hello world!",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "I said hello world!",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Goodbye world!",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_bare_text_becomes_paragraph(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+ before
+
paragraph
+ between
+
paragraph
+ after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "before",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "paragraph",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "between",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "paragraph",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "after",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_ignore_unrecognised_tags_in_blocks(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
Hello frabjuous world!
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Hello frabjuous world!",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_inline_styles(self):
+ converter = ContentstateConverter(features=["bold", "italic"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
You do not talk about Fight Club.
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [
+ {"offset": 4, "length": 11, "style": "BOLD"},
+ {"offset": 7, "length": 3, "style": "ITALIC"},
+ ],
+ "text": "You do not talk about Fight Club.",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_inline_styles_at_top_level(self):
+ converter = ContentstateConverter(features=["bold", "italic"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+ You
do not talk about Fight Club.
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [
+ {"offset": 4, "length": 11, "style": "BOLD"},
+ {"offset": 7, "length": 3, "style": "ITALIC"},
+ ],
+ "text": "You do not talk about Fight Club.",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_inline_styles_at_start_of_bare_block(self):
+ converter = ContentstateConverter(features=["bold", "italic"])
+ result = json.loads(
+ converter.from_database_format(
+ """
Seriously, stop talking about
Fight Club already."""
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [
+ {"offset": 0, "length": 9, "style": "BOLD"},
+ {"offset": 30, "length": 10, "style": "ITALIC"},
+ ],
+ "text": "Seriously, stop talking about Fight Club already.",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_inline_styles_depend_on_features(self):
+ converter = ContentstateConverter(features=["italic", "just-made-it-up"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
You do not talk about Fight Club.
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [
+ {"offset": 7, "length": 3, "style": "ITALIC"}
+ ],
+ "text": "You do not talk about Fight Club.",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_ordered_list(self):
+ converter = ContentstateConverter(features=["h1", "ol", "bold", "italic"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
The rules of Fight Club
+
+ - You do not talk about Fight Club.
+ - You do not talk about Fight Club.
+
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "The rules of Fight Club",
+ "depth": 0,
+ "type": "header-one",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "You do not talk about Fight Club.",
+ "depth": 0,
+ "type": "ordered-list-item",
+ "key": "00001",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [
+ {"offset": 4, "length": 11, "style": "BOLD"},
+ {"offset": 7, "length": 3, "style": "ITALIC"},
+ ],
+ "text": "You do not talk about Fight Club.",
+ "depth": 0,
+ "type": "ordered-list-item",
+ "key": "00002",
+ "entityRanges": [],
+ },
+ ],
+ },
+ match_keys=True,
+ )
+
+ def test_nested_list(self):
+ converter = ContentstateConverter(features=["h1", "ul"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
Shopping list
+
+ - Milk
+ -
+ Flour
+
+
+ - Eggs
+
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Shopping list",
+ "depth": 0,
+ "type": "header-one",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Milk",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00001",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Flour",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00002",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Plain",
+ "depth": 1,
+ "type": "unordered-list-item",
+ "key": "00003",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Self-raising",
+ "depth": 1,
+ "type": "unordered-list-item",
+ "key": "00004",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Eggs",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00005",
+ "entityRanges": [],
+ },
+ ],
+ },
+ match_keys=True,
+ )
+
+ def test_external_link(self):
+ converter = ContentstateConverter(features=["link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
an external link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {"url": "http://wagtail.org"},
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "an external link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 3, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_link_in_bare_text(self):
+ converter = ContentstateConverter(features=["link"])
+ result = json.loads(
+ converter.from_database_format(
+ """an
external link"""
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {"url": "http://wagtail.org"},
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "an external link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 3, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_link_at_start_of_bare_text(self):
+ converter = ContentstateConverter(features=["link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
an external link and
another"""
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {"url": "http://wagtail.org"},
+ },
+ "1": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {"url": "http://torchbox.com"},
+ },
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "an external link and another",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [
+ {"offset": 0, "length": 16, "key": 0},
+ {"offset": 21, "length": 7, "key": 1},
+ ],
+ },
+ ],
+ },
+ )
+
+ def test_page_link(self):
+ converter = ContentstateConverter(features=["link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
an internal link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {"id": 3, "url": "/events/", "parentId": 2},
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "an internal link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 3, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_broken_page_link(self):
+ converter = ContentstateConverter(features=["link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
an internal link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {
+ "id": 9999,
+ "url": None,
+ "parentId": None,
+ },
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "an internal link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 3, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_link_to_root_page(self):
+ converter = ContentstateConverter(features=["link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
an internal link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "LINK",
+ "data": {"id": 1, "url": None, "parentId": None},
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "an internal link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 3, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_document_link(self):
+ converter = ContentstateConverter(features=["document-link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
a document link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "DOCUMENT",
+ "data": {
+ "id": 1,
+ "url": "/documents/1/test.pdf",
+ "filename": "test.pdf",
+ },
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "a document link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 2, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_broken_document_link(self):
+ converter = ContentstateConverter(features=["document-link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
a document link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "mutability": "MUTABLE",
+ "type": "DOCUMENT",
+ "data": {"id": 9999},
+ }
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "a document link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 2, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_document_link_with_missing_id(self):
+ converter = ContentstateConverter(features=["document-link"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
a document link
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {"mutability": "MUTABLE", "type": "DOCUMENT", "data": {}}
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "a document link",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [{"offset": 2, "length": 8, "key": 0}],
+ },
+ ],
+ },
+ )
+
+ def test_image_embed(self):
+ converter = ContentstateConverter(features=["image"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
+
+
after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {
+ "format": "left",
+ "alt": "an image",
+ "id": "1",
+ "src": "/media/not-found",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "IMAGE",
+ }
+ },
+ },
+ )
+
+ def test_add_spacer_paragraph_between_image_embeds(self):
+ converter = ContentstateConverter(features=["image"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
+
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 1, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {
+ "format": "left",
+ "alt": "an image",
+ "id": "1",
+ "src": "/media/not-found",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "IMAGE",
+ },
+ "1": {
+ "data": {
+ "format": "left",
+ "alt": "an image",
+ "id": "1",
+ "src": "/media/not-found",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "IMAGE",
+ },
+ },
+ },
+ )
+
+ def test_image_after_list(self):
+ """
+ There should be no spacer paragraph inserted between a list and an image
+ """
+ converter = ContentstateConverter(features=["ul", "image"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
+
+
+ - More milk
+ - More eggs
+
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {
+ "0": {
+ "data": {
+ "format": "left",
+ "alt": "an image",
+ "id": "1",
+ "src": "/media/not-found",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "IMAGE",
+ },
+ },
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Milk",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Eggs",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 1, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "More milk",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "More eggs",
+ "depth": 0,
+ "type": "unordered-list-item",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ @patch("wagtail.embeds.embeds.get_embed")
+ def test_media_embed(self, get_embed):
+ get_embed.return_value = Embed(
+ url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw",
+ max_width=None,
+ type="video",
+ html="test html",
+ title="what are birds",
+ author_name="look around you",
+ provider_name="YouTube",
+ thumbnail_url="http://test/thumbnail.url",
+ width=1000,
+ height=1000,
+ )
+
+ converter = ContentstateConverter(features=["embed"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
+
+
after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {
+ "thumbnail": "http://test/thumbnail.url",
+ "embedType": "video",
+ "providerName": "YouTube",
+ "title": "what are birds",
+ "authorName": "look around you",
+ "url": "https://www.youtube.com/watch?v=Kh0Y2hVe_bw",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "EMBED",
+ }
+ },
+ },
+ )
+
+ @patch("wagtail.embeds.embeds.get_embed")
+ def test_add_spacer_paras_between_media_embeds(self, get_embed):
+ get_embed.return_value = Embed(
+ url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw",
+ max_width=None,
+ type="video",
+ html="test html",
+ title="what are birds",
+ author_name="look around you",
+ provider_name="YouTube",
+ thumbnail_url="http://test/thumbnail.url",
+ width=1000,
+ height=1000,
+ )
+
+ converter = ContentstateConverter(features=["embed"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
+
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 1, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {
+ "thumbnail": "http://test/thumbnail.url",
+ "embedType": "video",
+ "providerName": "YouTube",
+ "title": "what are birds",
+ "authorName": "look around you",
+ "url": "https://www.youtube.com/watch?v=Kh0Y2hVe_bw",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "EMBED",
+ },
+ "1": {
+ "data": {
+ "thumbnail": "http://test/thumbnail.url",
+ "embedType": "video",
+ "providerName": "YouTube",
+ "title": "what are birds",
+ "authorName": "look around you",
+ "url": "https://www.youtube.com/watch?v=Kh0Y2hVe_bw",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "EMBED",
+ },
+ },
+ },
+ )
+
+ def test_hr(self):
+ converter = ContentstateConverter(features=["hr"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
+
+
after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {},
+ "mutability": "IMMUTABLE",
+ "type": "HORIZONTAL_RULE",
+ }
+ },
+ },
+ )
+
+ def test_add_spacer_paragraph_between_hrs(self):
+ converter = ContentstateConverter(features=["hr"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
+
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 1, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {},
+ "mutability": "IMMUTABLE",
+ "type": "HORIZONTAL_RULE",
+ },
+ "1": {
+ "data": {},
+ "mutability": "IMMUTABLE",
+ "type": "HORIZONTAL_RULE",
+ },
+ },
+ },
+ )
+
+ def test_block_element_in_paragraph(self):
+ converter = ContentstateConverter(features=["hr"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {},
+ "mutability": "IMMUTABLE",
+ "type": "HORIZONTAL_RULE",
+ }
+ },
+ },
+ )
+
+ def test_br_element_in_paragraph(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before\nafter",
+ "type": "unstyled",
+ }
+ ],
+ },
+ )
+
+ def test_br_element_between_paragraphs(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
+
+
after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ },
+ )
+
+ def test_block_element_in_empty_paragraph(self):
+ converter = ContentstateConverter(features=["hr"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
+ """
+ )
+ )
+ # ignoring the paragraph completely would probably be better,
+ # but we'll settle for an empty preceding paragraph and not crashing as the next best thing...
+ # (and if it's the first/last block we actually do want a spacer paragraph anyhow)
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {},
+ "mutability": "IMMUTABLE",
+ "type": "HORIZONTAL_RULE",
+ }
+ },
+ },
+ )
+
+ def test_html_entities(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
Arthur "two sheds" Jackson <the third> & his wife
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": 'Arthur "two sheds" Jackson
& his wife',
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_collapse_targeted_whitespace_characters(self):
+ # We expect all targeted whitespace characters (one or more consecutively)
+ # to be replaced by a single space. (\xa0 is a non-breaking whitespace)
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+ Multiple whitespaces: should be reduced
+ Multiple non-breaking whitespace characters: \xa0\xa0\xa0 should be preserved
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Multiple whitespaces: should be reduced",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ {
+ "inlineStyleRanges": [],
+ "text": "Multiple non-breaking whitespace characters: \xa0\xa0\xa0 should be preserved",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_extra_end_tag_before(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
+ Before
+ """
+ )
+ )
+ # The leading tag should be ignored instead of blowing up with a
+ # pop from empty list error
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "Before",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_extra_end_tag_after(self):
+ converter = ContentstateConverter(features=[])
+ result = json.loads(
+ converter.from_database_format(
+ """
+ After
+
+ """
+ )
+ )
+ # The tailing tag should be ignored instead of blowing up with a
+ # pop from empty list error
+ self.assertContentStateEqual(
+ result,
+ {
+ "entityMap": {},
+ "blocks": [
+ {
+ "inlineStyleRanges": [],
+ "text": "After",
+ "depth": 0,
+ "type": "unstyled",
+ "key": "00000",
+ "entityRanges": [],
+ },
+ ],
+ },
+ )
+
+ def test_p_with_class(self):
+ # Test support for custom conversion rules which require correct treatment of
+ # CSS precedence in HTMLRuleset. Here, should match the
+ # 'p[class="intro"]' rule rather than 'p' and thus become an 'intro-paragraph' block
+ converter = ContentstateConverter(features=["intro"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+
before
+ after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "intro-paragraph",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {},
+ },
+ )
+
+ def test_image_inside_paragraph(self):
+ # In Draftail's data model, images are block-level elements and therefore
+ # split up preceding / following text into their own paragraphs
+ converter = ContentstateConverter(features=["image"])
+ result = json.loads(
+ converter.from_database_format(
+ """
+ before after
+ """
+ )
+ )
+ self.assertContentStateEqual(
+ result,
+ {
+ "blocks": [
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "before",
+ "type": "unstyled",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [{"key": 0, "offset": 0, "length": 1}],
+ "depth": 0,
+ "text": " ",
+ "type": "atomic",
+ },
+ {
+ "key": "00000",
+ "inlineStyleRanges": [],
+ "entityRanges": [],
+ "depth": 0,
+ "text": "after",
+ "type": "unstyled",
+ },
+ ],
+ "entityMap": {
+ "0": {
+ "data": {
+ "format": "left",
+ "alt": "an image",
+ "id": "1",
+ "src": "/media/not-found",
+ },
+ "mutability": "IMMUTABLE",
+ "type": "IMAGE",
+ }
+ },
+ },
+ )
+
+ def test_image_inside_style(self):
+ # https://github.com/wagtail/wagtail/issues/4602 - ensure that an