diff --git a/wagtail/tests/fixtures/test.json b/wagtail/tests/fixtures/test.json new file mode 100644 index 0000000000..98db24215a --- /dev/null +++ b/wagtail/tests/fixtures/test.json @@ -0,0 +1,97 @@ +[ +{ + "pk": 1, + "model": "wagtailcore.page", + "fields": { + "title": "Root", + "numchild": 1, + "show_in_menus": false, + "live": true, + "depth": 1, + "content_type": [ + "wagtailcore", + "page" + ], + "path": "0001", + "url_path": "/", + "slug": "root" + } +}, + +{ + "pk": 2, + "model": "wagtailcore.page", + "fields": { + "title": "Welcome to the Wagtail test site!", + "numchild": 1, + "show_in_menus": false, + "live": true, + "depth": 2, + "content_type": ["wagtailcore", "page"], + "path": "00010001", + "url_path": "/home/", + "slug": "home" + } +}, + +{ + "pk": 3, + "model": "wagtailcore.page", + "fields": { + "title": "Events", + "numchild": 1, + "show_in_menus": true, + "live": true, + "depth": 3, + "content_type": ["tests", "simplepage"], + "path": "000100010001", + "url_path": "/home/events/", + "slug": "events" + } +}, +{ + "pk": 3, + "model": "tests.simplepage", + "fields": { + "content": "Look at our lovely events." + } +}, + +{ + "pk": 4, + "model": "wagtailcore.page", + "fields": { + "title": "Christmas", + "numchild": 1, + "show_in_menus": true, + "live": true, + "depth": 4, + "content_type": ["tests", "eventpage"], + "path": "0001000100010001", + "url_path": "/home/events/christmas/", + "slug": "christmas" + } +}, +{ + "pk": 4, + "model": "tests.eventpage", + "fields": { + "date_from": "2014-12-25", + "audience": "public", + "location": "The North Pole", + "body": "

Chestnuts roasting on an open fire

", + "cost": "Free" + } +}, + +{ + "pk": 1, + "model": "wagtailcore.site", + "fields": { + "root_page": 2, + "hostname": "localhost", + "port": 80, + "is_default_site": true + } +} +] diff --git a/wagtail/wagtailcore/tests.py b/wagtail/wagtailcore/tests.py index 95cbacbf0c..ccd91d0092 100644 --- a/wagtail/wagtailcore/tests.py +++ b/wagtail/wagtailcore/tests.py @@ -1,19 +1,40 @@ -""" -This file demonstrates writing tests using the unittest module. These will pass -when you run "manage.py test". - -Replace this with more appropriate tests for your application. -""" - -import unittest - from django.test import TestCase +from wagtail.wagtailcore.models import Page, Site -@unittest.skip("Need real tests") -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) +class TestRouting(TestCase): + fixtures = ['test.json'] + + def test_urls(self): + default_site = Site.objects.get(is_default_site=True) + homepage = Page.objects.get(url_path='/home/') + christmas_page = Page.objects.get(url_path='/home/events/christmas/') + + # Basic installation only has one site configured, so page.url will return local URLs + self.assertEqual(homepage.full_url, 'http://localhost/') + self.assertEqual(homepage.url, '/') + self.assertEqual(homepage.relative_url(default_site), '/') + + self.assertEqual(christmas_page.full_url, 'http://localhost/events/christmas/') + self.assertEqual(christmas_page.url, '/events/christmas/') + self.assertEqual(christmas_page.relative_url(default_site), '/events/christmas/') + + def test_urls_with_multiple_sites(self): + events_page = Page.objects.get(url_path='/home/events/') + events_site = Site.objects.create(hostname='events.example.com', root_page=events_page) + + default_site = Site.objects.get(is_default_site=True) + homepage = Page.objects.get(url_path='/home/') + christmas_page = Page.objects.get(url_path='/home/events/christmas/') + + # with multiple sites, page.url will return full URLs to ensure that + # they work across sites + self.assertEqual(homepage.full_url, 'http://localhost/') + self.assertEqual(homepage.url, 'http://localhost/') + self.assertEqual(homepage.relative_url(default_site), '/') + self.assertEqual(homepage.relative_url(events_site), 'http://localhost/') + + self.assertEqual(christmas_page.full_url, 'http://events.example.com/christmas/') + self.assertEqual(christmas_page.url, 'http://events.example.com/christmas/') + self.assertEqual(christmas_page.relative_url(default_site), 'http://events.example.com/christmas/') + self.assertEqual(christmas_page.relative_url(events_site), '/christmas/')