kopia lustrzana https://github.com/wagtail/wagtail
Add tests for signal handlers
rodzic
51164ef2a1
commit
2eed6b81a6
|
@ -0,0 +1,163 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.db import connection
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from wagtail.contrib.redirects.models import Redirect
|
||||
from wagtail.core.models import Page, Site
|
||||
from wagtail.core.utils import get_dummy_request
|
||||
from wagtail.tests.testapp.models import EventIndex
|
||||
from wagtail.tests.utils import WagtailTestUtils
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
@override_settings(WAGTAILREDIRECTS_AUTO_CREATE=True)
|
||||
class TestAutocreateRedirects(TestCase, WagtailTestUtils):
|
||||
fixtures = ["test.json"]
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.site = Site.objects.select_related("root_page").get(is_default_site=True)
|
||||
cls.user = User.objects.first()
|
||||
|
||||
def setUp(self):
|
||||
self.home_page = self.site.root_page
|
||||
self.event_index = EventIndex.objects.get()
|
||||
self.other_page = Page.objects.get(url_path="/home/about-us/")
|
||||
|
||||
def trigger_page_slug_changed_signal(self, page):
|
||||
page.slug += "-extra"
|
||||
page.save(log_action="wagtail.publish", user=self.user, clean=False)
|
||||
# simulate database transaction commit:
|
||||
for _, func in connection.run_on_commit:
|
||||
func()
|
||||
|
||||
def test_golden_path(self):
|
||||
# the page we'll be triggering the change for here is...
|
||||
test_subject = self.event_index
|
||||
|
||||
# identify 'draft' pages in this section
|
||||
drafts = test_subject.get_descendants().not_live()
|
||||
self.assertEqual(len(drafts), 4)
|
||||
|
||||
# gather urls for 'live' pages in this branch
|
||||
request = get_dummy_request()
|
||||
branch_urls = []
|
||||
for page in (
|
||||
test_subject.get_descendants(inclusive=True)
|
||||
.live()
|
||||
.specific(defer=True)
|
||||
.iterator()
|
||||
):
|
||||
main_url = page.get_url(request).rstrip("/")
|
||||
branch_urls.extend(
|
||||
main_url + path.rstrip("/") for path in page.get_cached_paths()
|
||||
)
|
||||
|
||||
self.trigger_page_slug_changed_signal(test_subject)
|
||||
|
||||
# gather all of the redirects that were created
|
||||
redirects = Redirect.objects.all()
|
||||
redirect_page_ids = set(r.redirect_page_id for r in redirects)
|
||||
|
||||
# a redirect should have been created for the page itself
|
||||
self.assertIn(test_subject.id, redirect_page_ids)
|
||||
|
||||
# as well as for each of its live descendants
|
||||
for descendant in test_subject.get_descendants().live().iterator():
|
||||
self.assertIn(descendant.id, redirect_page_ids)
|
||||
|
||||
# but not for the draft pages
|
||||
for page in drafts:
|
||||
self.assertNotIn(page.id, redirect_page_ids)
|
||||
|
||||
# for each redirect created:
|
||||
for r in redirects:
|
||||
# the old_path accurately matches a url from this branch
|
||||
self.assertIn(r.old_path, branch_urls)
|
||||
# the automatically_created flag should have been set to True
|
||||
self.assertTrue(r.automatically_created)
|
||||
|
||||
def test_no_redirects_created_when_page_is_root_for_all_sites_it_belongs_to(self):
|
||||
self.trigger_page_slug_changed_signal(self.home_page)
|
||||
self.assertFalse(Redirect.objects.exists())
|
||||
|
||||
def test_handling_of_existing_redirects(self):
|
||||
# the page we'll be triggering the change for here is...
|
||||
test_subject = self.event_index
|
||||
|
||||
descendants = test_subject.get_descendants().live()
|
||||
|
||||
# but before we do, let's add some redirects that we'll expect to conflict
|
||||
# with ones created by the signal handler
|
||||
redirect1 = Redirect.objects.create(
|
||||
old_path=Redirect.normalise_path(descendants.first().specific.url),
|
||||
site=self.site,
|
||||
redirect_link="/some-place",
|
||||
automatically_created=False,
|
||||
)
|
||||
redirect2 = Redirect.objects.create(
|
||||
old_path=Redirect.normalise_path(descendants.last().specific.url),
|
||||
site=self.site,
|
||||
redirect_link="/some-other-place",
|
||||
automatically_created=True,
|
||||
)
|
||||
|
||||
self.trigger_page_slug_changed_signal(test_subject)
|
||||
|
||||
# pre-existing manually-created redirects should be preserved
|
||||
from_db = Redirect.objects.get(id=redirect1.id)
|
||||
self.assertEqual(
|
||||
(
|
||||
redirect1.old_path,
|
||||
redirect1.site_id,
|
||||
redirect1.is_permanent,
|
||||
redirect1.redirect_link,
|
||||
redirect1.redirect_page,
|
||||
),
|
||||
(
|
||||
from_db.old_path,
|
||||
from_db.site_id,
|
||||
from_db.is_permanent,
|
||||
from_db.redirect_link,
|
||||
from_db.redirect_page,
|
||||
),
|
||||
)
|
||||
|
||||
# pre-existing automatically-created redirects should be replaced completely
|
||||
self.assertFalse(Redirect.objects.filter(pk=redirect2.pk).exists())
|
||||
self.assertTrue(
|
||||
Redirect.objects.filter(
|
||||
old_path=redirect2.old_path,
|
||||
site_id=redirect2.site_id,
|
||||
).exists()
|
||||
)
|
||||
|
||||
def test_no_redirects_created_when_pages_are_moved_to_a_different_site(self):
|
||||
|
||||
# Add a new home page
|
||||
homepage_2 = Page(
|
||||
title="Second home",
|
||||
slug="second-home",
|
||||
)
|
||||
root_page = Page.objects.get(depth=1)
|
||||
root_page.add_child(instance=homepage_2)
|
||||
|
||||
# Create a site with the above as the root_page
|
||||
Site.objects.create(
|
||||
root_page=homepage_2,
|
||||
hostname="newsite.com",
|
||||
port=80,
|
||||
)
|
||||
|
||||
# Move the event index to the new site
|
||||
self.event_index.move(homepage_2, pos="last-child")
|
||||
|
||||
# No redirects should have been created
|
||||
self.assertFalse(Redirect.objects.exists())
|
||||
|
||||
@override_settings(WAGTAILREDIRECTS_AUTO_CREATE=False)
|
||||
def test_no_redirects_created_if_disabled(self):
|
||||
self.trigger_page_slug_changed_signal(self.event_index)
|
||||
self.assertFalse(Redirect.objects.exists())
|
|
@ -255,3 +255,6 @@ REST_FRAMEWORK = {
|
|||
'rest_framework.authentication.BasicAuthentication',
|
||||
]
|
||||
}
|
||||
|
||||
# Disable redirect autocreation for the majority of tests (to improve efficiency)
|
||||
WAGTAILREDIRECTS_AUTO_CREATE = False
|
||||
|
|
Ładowanie…
Reference in New Issue