mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-29 11:04:49 +00:00
Added tests for sitemaps
This commit is contained in:
parent
8e1b2cf019
commit
ee7aebc9da
3 changed files with 75 additions and 0 deletions
|
|
@ -84,6 +84,7 @@ if not settings.configured:
|
|||
'wagtail.wagtailsearch',
|
||||
'wagtail.wagtailredirects',
|
||||
'wagtail.wagtailforms',
|
||||
'wagtail.contrib.wagtailsitemaps',
|
||||
'wagtail.tests',
|
||||
],
|
||||
|
||||
|
|
|
|||
71
wagtail/contrib/wagtailsitemaps/tests.py
Normal file
71
wagtail/contrib/wagtailsitemaps/tests.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
from django.test import TestCase
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from wagtail.wagtailcore.models import Page, Site
|
||||
from wagtail.tests.models import SimplePage
|
||||
|
||||
from .sitemap_generator import Sitemap
|
||||
|
||||
|
||||
class TestSitemapGenerator(TestCase):
|
||||
def setUp(self):
|
||||
self.home_page = Page.objects.get(id=2)
|
||||
|
||||
self.child_page = self.home_page.add_child(instance=SimplePage(
|
||||
title="Hello world!",
|
||||
slug='hello-world',
|
||||
live=True,
|
||||
))
|
||||
|
||||
self.unpublished_child_page = self.home_page.add_child(instance=SimplePage(
|
||||
title="Unpublished",
|
||||
slug='unpublished',
|
||||
live=False,
|
||||
))
|
||||
|
||||
self.site = Site.objects.get(is_default_site=True)
|
||||
|
||||
def test_get_pages(self):
|
||||
sitemap = Sitemap(self.site)
|
||||
pages = sitemap.get_pages()
|
||||
|
||||
self.assertIn(self.child_page.page_ptr, pages)
|
||||
self.assertNotIn(self.unpublished_child_page.page_ptr, pages)
|
||||
|
||||
def test_get_urls(self):
|
||||
sitemap = Sitemap(self.site)
|
||||
urls = [url['location'] for url in sitemap.get_urls()]
|
||||
|
||||
self.assertIn('/', urls) # Homepage
|
||||
self.assertIn('/hello-world/', urls) # Child page
|
||||
|
||||
def test_render(self):
|
||||
sitemap = Sitemap(self.site)
|
||||
xml = sitemap.render()
|
||||
|
||||
# Check that a URL has made it into the xml
|
||||
self.assertIn('/hello-world/', xml)
|
||||
|
||||
|
||||
class TestSitemapView(TestCase):
|
||||
def test_sitemap_view(self):
|
||||
response = self.client.get('/sitemap.xml')
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtailsitemaps/sitemap.xml')
|
||||
self.assertEqual(response['Content-Type'], 'text/xml; charset=utf-8')
|
||||
|
||||
def test_sitemap_view_cache(self):
|
||||
first_response = self.client.get('/sitemap.xml')
|
||||
|
||||
self.assertEqual(first_response.status_code, 200)
|
||||
self.assertTemplateUsed(first_response, 'wagtailsitemaps/sitemap.xml')
|
||||
|
||||
# Hit the view again. Should come from the cache this time
|
||||
second_response = self.client.get('/sitemap.xml')
|
||||
|
||||
self.assertEqual(second_response.status_code, 200)
|
||||
self.assertTemplateNotUsed(second_response, 'wagtailsitemaps/sitemap.xml') # Sitemap should not be re rendered
|
||||
|
||||
# Check that the content is the same
|
||||
self.assertEqual(first_response.content, second_response.content)
|
||||
|
|
@ -4,6 +4,7 @@ from wagtail.wagtailcore import urls as wagtail_urls
|
|||
from wagtail.wagtailadmin import urls as wagtailadmin_urls
|
||||
from wagtail.wagtaildocs import urls as wagtaildocs_urls
|
||||
from wagtail.wagtailsearch.urls import frontend as wagtailsearch_frontend_urls
|
||||
from wagtail.contrib.wagtailsitemaps.views import sitemap
|
||||
|
||||
# Signal handlers
|
||||
from wagtail.wagtailsearch import register_signal_handlers as wagtailsearch_register_signal_handlers
|
||||
|
|
@ -15,6 +16,8 @@ urlpatterns = patterns('',
|
|||
url(r'^search/', include(wagtailsearch_frontend_urls)),
|
||||
url(r'^documents/', include(wagtaildocs_urls)),
|
||||
|
||||
url(r'^sitemap\.xml$', sitemap),
|
||||
|
||||
# For anything not caught by a more specific rule above, hand over to
|
||||
# Wagtail's serving mechanism
|
||||
url(r'', include(wagtail_urls)),
|
||||
|
|
|
|||
Loading…
Reference in a new issue