Skip to content

Commit

Permalink
Add tests for coping with new ListBlock representation
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtoppm authored and gasman committed Jun 9, 2022
1 parent b5a56e4 commit 30251a8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ class BaseStreamBlock(StreamBlock):
stream = AnotherStreamBlock()
rich_text = RichTextBlock()
list_of_pages = ListBlock(PageChooserBlock())
list_of_captioned_pages = ListBlock(CaptionedPageLink())
Original file line number Diff line number Diff line change
@@ -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'),
),
]
24 changes: 24 additions & 0 deletions tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,20 @@ def test_import_page_with_streamfield_rich_text_block(self):

self.assertEqual(imported_streamfield, [{'type': 'rich_text', 'value': '<p>I link to a <a id="1" linktype="page">page</a>.</p>', '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
Expand Down

0 comments on commit 30251a8

Please sign in to comment.