Test that copying pages with M2M relations doesn't break

This commit is contained in:
Matt Westcott 2015-10-29 13:17:16 +00:00
parent cd168a2b41
commit b77dfd20d7
3 changed files with 96 additions and 1 deletions

View file

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import wagtail.wagtailcore.fields
import modelcluster.fields
class Migration(migrations.Migration):
dependencies = [
('wagtailcore', '0020_add_index_on_page_first_published_at'),
('tests', '0013_iconsetting_notyetregisteredsetting_testsetting'),
]
operations = [
migrations.CreateModel(
name='BlogCategory',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True, serialize=False)),
('name', models.CharField(unique=True, max_length=80)),
],
),
migrations.CreateModel(
name='BlogCategoryBlogPage',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True, serialize=False)),
('category', models.ForeignKey(to='tests.BlogCategory', related_name='+')),
],
),
migrations.CreateModel(
name='ManyToManyBlogPage',
fields=[
('page_ptr', models.OneToOneField(primary_key=True, serialize=False, parent_link=True, auto_created=True, to='wagtailcore.Page')),
('body', wagtail.wagtailcore.fields.RichTextField(blank=True)),
('adverts', models.ManyToManyField(to='tests.Advert', blank=True)),
('blog_categories', models.ManyToManyField(to='tests.BlogCategory', through='tests.BlogCategoryBlogPage', blank=True)),
],
options={
'abstract': False,
},
bases=('wagtailcore.page',),
),
migrations.AddField(
model_name='blogcategoryblogpage',
name='page',
field=modelcluster.fields.ParentalKey(to='tests.ManyToManyBlogPage', related_name='categories'),
),
]

View file

@ -479,3 +479,27 @@ class IconSetting(BaseSetting):
class NotYetRegisteredSetting(BaseSetting):
pass
class BlogCategory(models.Model):
name = models.CharField(unique=True, max_length=80)
class BlogCategoryBlogPage(models.Model):
category = models.ForeignKey(BlogCategory, related_name="+")
page = ParentalKey('ManyToManyBlogPage', related_name='categories')
panels = [
FieldPanel('category'),
]
class ManyToManyBlogPage(Page):
"""
A page type with two different kinds of M2M relation.
We don't formally support these, but we don't want them to cause
hard breakages either.
"""
body = RichTextField(blank=True)
adverts = models.ManyToManyField(Advert, blank=True)
blog_categories = models.ManyToManyField(
BlogCategory, through=BlogCategoryBlogPage, blank=True)

View file

@ -16,7 +16,8 @@ from wagtail.wagtailcore.models import Page, Site, PAGE_MODEL_CLASSES
from wagtail.tests.testapp.models import (
SingleEventPage, EventPage, EventIndex, SimplePage,
BusinessIndex, BusinessSubIndex, BusinessChild, StandardIndex,
MTIBasePage, MTIChildPage, AbstractPage, TaggedPage)
MTIBasePage, MTIChildPage, AbstractPage, TaggedPage,
BlogCategory, BlogCategoryBlogPage, Advert, ManyToManyBlogPage)
class TestSiteRouting(TestCase):
@ -666,6 +667,27 @@ class TestCopyPage(TestCase):
for item_id in new_tagged_item_ids
]))
def test_copy_page_with_m2m_relations(self):
# create and publish a ManyToManyBlogPage under Events
event_index = Page.objects.get(url_path='/home/events/')
category = BlogCategory.objects.create(name='Birds')
advert = Advert.objects.create(url='http://www.heinz.com/', text="beanz meanz heinz")
blog_page = ManyToManyBlogPage(title='My blog page', slug='my-blog-page')
event_index.add_child(instance=blog_page)
blog_page.adverts.add(advert)
BlogCategoryBlogPage.objects.create(category=category, page=blog_page)
blog_page.save_revision().publish()
# copy to underneath homepage
homepage = Page.objects.get(url_path='/home/')
new_blog_page = blog_page.copy(to=homepage)
# M2M relations are not formally supported, so for now we're only interested in
# the copy operation as a whole succeeding, rather than the child objects being copied
self.assertNotEqual(blog_page.id, new_blog_page.id)
class TestSubpageTypeBusinessRules(TestCase):
def test_allowed_subpage_types(self):