def test_cache_clears_when_site_root_moves(self): """ This tests for an issue where if a site root page was moved, all the page urls in that site would change to None. The issue was caused by the 'wagtail_site_root_paths' cache variable not being cleared when a site root page was moved. Which left all the child pages thinking that they are no longer in the site and return None as their url. Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 Discussion: https://github.com/wagtail/wagtail/issues/7 """ # Get homepage, root page and site root_page = Page.objects.get(id=1) homepage = Page.objects.get(url_path="/home/") default_site = Site.objects.get(is_default_site=True) # Create a new homepage under current homepage new_homepage = SimplePage( title="New Homepage", slug="new-homepage", content="hello" ) homepage.add_child(instance=new_homepage) # Set new homepage as the site root page default_site.root_page = new_homepage default_site.save() # Warm up the cache by getting the url _ = homepage.url # noqa # Move new homepage to root new_homepage.move(root_page, pos="last-child") # Get fresh instance of new_homepage new_homepage = Page.objects.get(id=new_homepage.id) # Check url self.assertEqual(new_homepage.url, "/")
class TestPageSlugChangedSignal(TestCase, WagtailTestUtils): """ Tests for the `wagtail.signals.page_slug_changed` signal """ def setUp(self): # Find root page site = Site.objects.select_related("root_page").get(is_default_site=True) root_page = site.root_page # Create two sections self.section_a = SimplePage( title="Section A", slug="section-a", content="hello" ) root_page.add_child(instance=self.section_a) self.section_b = SimplePage( title="Section B", slug="section-b", content="hello" ) root_page.add_child(instance=self.section_b) # Add test page to section A self.test_page = SimplePage( title="Hello world! A", slug="hello-world-a", content="hello" ) self.section_a.add_child(instance=self.test_page) def test_signal_emitted_on_slug_change(self): # Connect a mock signal handler to the signal handler = mock.MagicMock() page_slug_changed.connect(handler) old_page = SimplePage.objects.get(id=self.test_page.id) try: self.test_page.slug = "updated" self.test_page.save() # TODO: When Django 3.1< support is dropped, wrap save in # self.captureOnCommitCallbacks and remove this code for _, func in connection.run_on_commit: func() finally: # Disconnect mock handler to prevent cross-test pollution page_slug_changed.disconnect(handler) # Check the signal was fired self.assertEqual(handler.call_count, 1) self.assertTrue( handler.called_with( sender=SimplePage, instance=self.test_page, instance_before=old_page, ) ) def test_signal_not_emitted_on_title_change(self): # Connect a mock signal handler to the signal handler = mock.MagicMock() page_slug_changed.connect(handler) try: self.test_page.title = "Goodnight Moon!" self.test_page.save() # NOTE: Even though we're not expecting anything to happen here, # we need to invoke the callbacks in run_on_commit the same way # the same way we do in ``test_signal_emitted_on_slug_change``, # otherwise this test wouldn't prove anything. for _, func in connection.run_on_commit: func() finally: # Disconnect mock handler to prevent cross-test pollution page_slug_changed.disconnect(handler) # Check the signal was NOT fired self.assertEqual(handler.call_count, 0) def test_signal_not_emitted_on_page_move(self): # Connect a mock signal handler to the signal handler = mock.MagicMock() page_slug_changed.connect(handler) try: self.test_page.move(self.section_b, pos="last-child") # NOTE: Even though we're not expecting anything to happen here, # we need to invoke the callbacks in run_on_commit the same way # the same way we do in ``test_signal_emitted_on_slug_change``, # otherwise this test wouldn't prove anything. for _, func in connection.run_on_commit: func() finally: # Disconnect mock handler to prevent cross-test pollution page_slug_changed.disconnect(handler) # Check the signal was NOT fired self.assertEqual(handler.call_count, 0)