diff --git a/tests/tests/test_import.py b/tests/tests/test_import.py index 986e930..db03280 100644 --- a/tests/tests/test_import.py +++ b/tests/tests/test_import.py @@ -32,6 +32,11 @@ def tearDown(self): shutil.rmtree(TEST_MEDIA_DIR, ignore_errors=True) def test_import_pages(self): + # make a draft edit to the homepage + home = SimplePage.objects.get(slug='home') + home.title = "Draft home" + home.save_revision() + data = """{ "ids_for_import": [ ["wagtailcore.page", 12], @@ -59,7 +64,7 @@ def test_import_pages(self): "pk": 12, "parent_id": 1, "fields": { - "title": "Home", + "title": "New home", "show_in_menus": false, "live": true, "slug": "home", @@ -75,9 +80,20 @@ def test_import_pages(self): updated_page = SimplePage.objects.get(url_path='/home/') self.assertEqual(updated_page.intro, "This is the updated homepage") + self.assertEqual(updated_page.title, "New home") + self.assertEqual(updated_page.draft_title, "New home") + + # get_latest_revision (as used in the edit-page view) should also reflect the imported content + updated_page_revision = updated_page.get_latest_revision_as_page() + self.assertEqual(updated_page_revision.intro, "This is the updated homepage") + self.assertEqual(updated_page_revision.title, "New home") created_page = SimplePage.objects.get(url_path='/home/imported-child-page/') self.assertEqual(created_page.intro, "This page is imported from the source site") + # An initial page revision should also be created + self.assertTrue(created_page.get_latest_revision()) + created_page_revision = created_page.get_latest_revision_as_page() + self.assertEqual(created_page_revision.intro, "This page is imported from the source site") def test_import_pages_with_fk(self): data = """{ diff --git a/wagtail_transfer/operations.py b/wagtail_transfer/operations.py index 68e1086..32cc9f5 100644 --- a/wagtail_transfer/operations.py +++ b/wagtail_transfer/operations.py @@ -670,6 +670,10 @@ def _save(self, context): # Add the page to the database as a child of parent parent.add_child(instance=self.instance) + if isinstance(self.instance, Page): + # Also save this as a revision, so that it exists in revision history + self.instance.save_revision(changed=False) + class UpdateModel(SaveOperationMixin, Operation): def __init__(self, instance, object_data): @@ -682,6 +686,15 @@ def run(self, context): self._save(context) self._populate_many_to_many_fields(context) + def _save(self, context): + super()._save(context) + if isinstance(self.instance, Page): + # Also save this as a revision, so that: + # * the edit-page view will pick up this imported version rather than any currently-existing drafts + # * it exists in revision history + # * the Page.draft_title field (as used in page listings in the admin) is updated to match the real title + self.instance.save_revision(changed=False) + class DeleteModel(Operation): def __init__(self, instance):