diff --git a/wagtail/core/models.py b/wagtail/core/models.py index 078f7fc7c0..c5f581b3a2 100644 --- a/wagtail/core/models.py +++ b/wagtail/core/models.py @@ -1866,6 +1866,12 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): page_copy.first_published_at = latest_revision_as_page_revision.created_at page_copy.save(clean=False) + if page_copy.live: + page_published.send( + sender=page_copy.specific_class, instance=page_copy, + revision=latest_revision_as_page_revision + ) + # Log if log_action: parent = specific_self.get_parent() diff --git a/wagtail/core/tests/test_page_model.py b/wagtail/core/tests/test_page_model.py index ff78b5d142..d6e6148687 100644 --- a/wagtail/core/tests/test_page_model.py +++ b/wagtail/core/tests/test_page_model.py @@ -17,6 +17,7 @@ from freezegun import freeze_time from wagtail.core.models import ( Locale, Page, PageManager, ParentNotTranslatedError, Site, get_page_models, get_translatable_models) +from wagtail.core.signals import page_published from wagtail.tests.testapp.models import ( AbstractPage, Advert, AlwaysShowInMenusPage, BlogCategory, BlogCategoryBlogPage, BusinessChild, BusinessIndex, BusinessNowherePage, BusinessSubIndex, CustomManager, CustomManagerPage, @@ -1490,6 +1491,59 @@ class TestCopyPage(TestCase): with self.assertRaises(RuntimeError): new_page.copy() + def test_copy_published_emits_signal(self): + """Test that copying of a published page emits a page_published signal.""" + christmas_page = EventPage.objects.get(url_path='/home/events/christmas/') + + signal_fired = False + signal_page = None + + def page_published_handler(sender, instance, **kwargs): + nonlocal signal_fired + nonlocal signal_page + signal_fired = True + signal_page = instance + + page_published.connect(page_published_handler) + copy_page = christmas_page.copy( + update_attrs={'title': "New christmas", 'slug': 'new-christmas'}, + ) + + self.assertTrue(signal_fired) + self.assertEqual(signal_page, copy_page) + + def test_copy_unpublished_not_emits_signal(self): + """Test that copying of an unpublished page not emits a page_published signal.""" + homepage = Page.objects.get(url_path='/home/') + homepage.live = False + homepage.save() + + signal_fired = False + + def page_published_handler(sender, instance, **kwargs): + nonlocal signal_fired + signal_fired = True + page_published.connect(page_published_handler) + + homepage.copy(update_attrs={'slug': 'new_slug'}) + self.assertFalse(signal_fired) + + def test_copy_keep_live_false_not_emits_signal(self): + """Test that copying of a live page with keep_live=False not emits a page_published signal.""" + homepage = Page.objects.get(url_path='/home/') + signal_fired = False + + def page_published_handler(sender, instance, **kwargs): + nonlocal signal_fired + signal_fired = True + page_published.connect(page_published_handler) + + homepage.copy( + keep_live=False, + update_attrs={'slug': 'new_slug'} + ) + self.assertFalse(signal_fired) + class TestCopyForTranslation(TestCase): fixtures = ['test.json']