From 30251a85f4dd3905104ff00a95d22a5025f161b2 Mon Sep 17 00:00:00 2001 From: jacobtoppm Date: Thu, 9 Jun 2022 16:20:03 +0100 Subject: [PATCH] Add tests for coping with new ListBlock representation --- tests/blocks.py | 1 + ...withrelatedpages_related_pages_and_more.py | 25 +++++++++++++++++++ tests/tests/test_api.py | 24 ++++++++++++++++++ tests/tests/test_import.py | 14 +++++++++++ 4 files changed, 64 insertions(+) create mode 100644 tests/migrations/0018_alter_pagewithrelatedpages_related_pages_and_more.py diff --git a/tests/blocks.py b/tests/blocks.py index 712df3b..9e4e098 100644 --- a/tests/blocks.py +++ b/tests/blocks.py @@ -23,3 +23,4 @@ class BaseStreamBlock(StreamBlock): stream = AnotherStreamBlock() rich_text = RichTextBlock() list_of_pages = ListBlock(PageChooserBlock()) + list_of_captioned_pages = ListBlock(CaptionedPageLink()) diff --git a/tests/migrations/0018_alter_pagewithrelatedpages_related_pages_and_more.py b/tests/migrations/0018_alter_pagewithrelatedpages_related_pages_and_more.py new file mode 100644 index 0000000..d5bc3b4 --- /dev/null +++ b/tests/migrations/0018_alter_pagewithrelatedpages_related_pages_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 4.0 on 2022-06-09 15:17 + +from django.db import migrations, models +import wagtail.core.blocks +import wagtail.core.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('tests', '0017_correctmodeltypes'), + ] + + operations = [ + migrations.AlterField( + model_name='pagewithrelatedpages', + name='related_pages', + field=models.ManyToManyField(related_name='+', to='wagtailcore.Page'), + ), + migrations.AlterField( + model_name='pagewithstreamfield', + name='body', + field=wagtail.core.fields.StreamField([('link_block', wagtail.core.blocks.StructBlock([('page', wagtail.core.blocks.PageChooserBlock(required=False)), ('text', wagtail.core.blocks.CharBlock(max_length=250))])), ('integer', wagtail.core.blocks.IntegerBlock(required=True)), ('page', wagtail.core.blocks.PageChooserBlock()), ('stream', wagtail.core.blocks.StreamBlock([('page', wagtail.core.blocks.PageChooserBlock())])), ('rich_text', wagtail.core.blocks.RichTextBlock()), ('list_of_pages', wagtail.core.blocks.ListBlock(wagtail.core.blocks.PageChooserBlock())), ('list_of_captioned_pages', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('page', wagtail.core.blocks.PageChooserBlock(required=False)), ('text', wagtail.core.blocks.CharBlock(max_length=250))])))], blank=True, verbose_name='Page body'), + ), + ] diff --git a/tests/tests/test_api.py b/tests/tests/test_api.py index 4358bb8..937fd31 100644 --- a/tests/tests/test_api.py +++ b/tests/tests/test_api.py @@ -301,6 +301,30 @@ def test_rich_text_with_image_embed(self): for model, pk, uid in data['mappings'] )) + def test_streamfield_with_page_links_in_new_listblock_format(self): + page = PageWithStreamField(title="I have a streamfield", + body=json.dumps([ + {'type': 'list_of_captioned_pages', + 'value': + [{'type': 'item', + 'value': { + 'page': 5, + 'text': 'a caption' + }, + 'id': '8c0d7de7-4f77-4477-be67-7d990d0bfb82'}], + 'id': '21ffe52a-c0fc-4ecc-92f1-17b356c9cc94'}, + ])) + parent_page = Page.objects.get(url_path='/home/existing-child-page/') + parent_page.add_child(instance=page) + + digest = digest_for_source('local', str(page.id)) + response = self.client.get('/wagtail-transfer/api/pages/%d/?digest=%s' % (page.id, digest)) + + data = json.loads(response.content) + + # test PageChooserBlock in ListBlock + self.assertIn(['wagtailcore.page', 5, "00017017-5555-5555-5555-555555555555"], data['mappings']) + def test_streamfield_with_page_links(self): # Check that page links in a complex nested StreamField - with StreamBlock, StructBlock, and ListBlock - # are all picked up in mappings diff --git a/tests/tests/test_import.py b/tests/tests/test_import.py index 389ab1b..cef8cad 100644 --- a/tests/tests/test_import.py +++ b/tests/tests/test_import.py @@ -713,6 +713,20 @@ def test_import_page_with_streamfield_rich_text_block(self): self.assertEqual(imported_streamfield, [{'type': 'rich_text', 'value': '

I link to a page.

', 'id': '7d4ee3d4-9213-4319-b984-45be4ded8853'}]) + def test_import_page_with_new_list_block_format(self): + # Check that ids in a ListBlock with the uuid format within a StreamField are converted properly + + data = """{"ids_for_import": [["wagtailcore.page", 6]], "mappings": [["wagtailcore.page", 6, "a231303a-1754-11ea-8000-0800278dc04d"], ["wagtailcore.page", 100, "11111111-1111-1111-1111-111111111111"]], "objects": [{"model": "tests.pagewithstreamfield", "pk": 6, "fields": {"title": "My streamfield list block has a link", "slug": "my-streamfield-block-has-a-link", "wagtail_admin_comments": [], "live": true, "seo_title": "", "show_in_menus": false, "search_description": "", "body": "[{\\"type\\": \\"list_of_captioned_pages\\", \\"value\\": [{\\"type\\": \\"item\\", \\"value\\": {\\"page\\": 100, \\"text\\": \\"a caption\\"}, \\"id\\": \\"8c0d7de7-4f77-4477-be67-7d990d0bfb82\\"}], \\"id\\": \\"21ffe52a-c0fc-4ecc-92f1-17b356c9cc94\\"}]"}, "parent_id": 100}]}""" + importer = ImportPlanner(root_page_source_pk=1, destination_parent_id=None) + importer.add_json(data) + importer.run() + + page = PageWithStreamField.objects.get(slug="my-streamfield-block-has-a-link") + + imported_streamfield = page.body.stream_block.get_prep_value(page.body) + + self.assertEqual(imported_streamfield, [{'type': 'list_of_captioned_pages', 'value': [{'type': 'item', 'value': {'page': 1, 'text': 'a caption'}, 'id': '8c0d7de7-4f77-4477-be67-7d990d0bfb82'}], 'id': '21ffe52a-c0fc-4ecc-92f1-17b356c9cc94'}]) + @mock.patch('requests.get') def test_import_image_with_file(self, get): get.return_value.status_code = 200