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 ready(self): from wagtail.core.signals import page_slug_changed, post_page_move from .signal_handlers import (autocreate_redirects_on_page_move, autocreate_redirects_on_slug_change) post_page_move.connect(autocreate_redirects_on_page_move) page_slug_changed.connect(autocreate_redirects_on_slug_change)
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)