def test_wizard_create_atomic(self): # Ref: https://github.com/divio/django-cms/issues/5652 # We'll simulate a scenario where a user creates a page with an # invalid template which causes Django to throw an error when the # template is scanned for placeholders and thus short circuits the # creation mechanism. superuser = self.get_superuser() data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, } form = CreateCMSPageForm(data=data) form.page = None form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) self.assertFalse( Page.objects.filter(template=TEMPLATE_INHERITANCE_MAGIC).exists()) with self.settings(CMS_TEMPLATES=[("col_invalid.html", "notvalid")]): self.assertRaises(TemplateSyntaxError, form.save) # The template raised an exception which should cause the database to roll back # instead of committing a page in a partial state. self.assertFalse( Page.objects.filter( template=TEMPLATE_INHERITANCE_MAGIC).exists())
def test_wizard_create_atomic(self): # Ref: https://github.com/divio/django-cms/issues/5652 # We'll simulate a scenario where a user creates a page with an # invalid template which causes Django to throw an error when the # template is scanned for placeholders and thus short circuits the # creation mechanism. superuser = self.get_superuser() data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, } form = CreateCMSPageForm(data=data) form.page = None form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) self.assertFalse(Page.objects.filter(template=TEMPLATE_INHERITANCE_MAGIC).exists()) with self.settings(CMS_TEMPLATES=[("col_invalid.html", "notvalid")]): self.assertRaises(TemplateSyntaxError, form.save) # The template raised an exception which should cause the database to roll back # instead of committing a page in a partial state. self.assertFalse(Page.objects.filter(template=TEMPLATE_INHERITANCE_MAGIC).exists())
def test_wizard_content_placeholder_setting(self): """ Tests that the PageWizard respects the CMS_PAGE_WIZARD_CONTENT_PLACEHOLDER setting. """ templates = get_cms_setting('TEMPLATES') # NOTE, there are 4 placeholders on this template, defined in this # order: 'header', 'content', 'sub-content', 'footer'. # 'footer' is a static-placeholder. templates.append(( 'page_wizard.html', 'page_wizard.html', )) settings = { 'CMS_TEMPLATES': templates, 'CMS_PAGE_WIZARD_DEFAULT_TEMPLATE': 'page_wizard.html', 'CMS_PAGE_WIZARD_CONTENT_PLACEHOLDER': 'sub-content', } with override_settings(**settings): superuser = self.get_superuser() page = create_page("wizard home", "page_wizard.html", "en") publish_page(page, superuser, "en") content = '<p>sub-content content.</p>' data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, 'content': content, } form = CreateCMSPageForm(data=data) form.page = page form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) page = form.save() page.publish('en') with self.login_user_context(superuser): url = page.get_absolute_url('en') expected = '<div class="sub-content">{0}</div>'.format(content) unexpected = '<div class="content">{0}</div>'.format(content) response = self.client.get(url) self.assertContains(response, expected, status_code=200) self.assertNotContains(response, unexpected, status_code=200)
def test_wizard_content_placeholder_bad_setting(self): """ Tests that the PageWizard won't respect a 'bad' setting such as targeting a static-placeholder. In this case, will just fail to add the content (without error). """ templates = get_cms_setting('TEMPLATES') # NOTE, there are 4 placeholders on this template, defined in this # order: 'header', 'content', 'sub-content', 'footer'. # 'footer' is a static-placeholder. templates.append(( 'page_wizard.html', 'page_wizard.html', )) settings = { 'CMS_TEMPLATES': templates, 'CMS_PAGE_WIZARD_DEFAULT_TEMPLATE': 'page_wizard.html', # This is a bad setting. 'CMS_PAGE_WIZARD_CONTENT_PLACEHOLDER': 'footer', } with override_settings(**settings): superuser = self.get_superuser() page = create_page("wizard home", "page_wizard.html", "en") publish_page(page, superuser, "en") content = '<p>footer content.</p>' data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, 'content': content, } form = CreateCMSPageForm(data=data) form.page = page form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) page = form.save() page.publish('en') with self.login_user_context(superuser): url = page.get_absolute_url('en') response = self.client.get(url) self.assertNotContains(response, content, status_code=200)
def test_wizard_content_placeholder_setting(self): """ Tests that the PageWizard respects the CMS_PAGE_WIZARD_CONTENT_PLACEHOLDER setting. """ templates = get_cms_setting('TEMPLATES') # NOTE, there are 4 placeholders on this template, defined in this # order: 'header', 'content', 'sub-content', 'footer'. # 'footer' is a static-placeholder. templates.append(('page_wizard.html', 'page_wizard.html', )) settings = { 'CMS_TEMPLATES': templates, 'CMS_PAGE_WIZARD_DEFAULT_TEMPLATE': 'page_wizard.html', 'CMS_PAGE_WIZARD_CONTENT_PLACEHOLDER': 'sub-content', } with override_settings(**settings): superuser = self.get_superuser() page = create_page("wizard home", "page_wizard.html", "en") publish_page(page, superuser, "en") content = '<p>sub-content content.</p>' data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, 'content': content, } form = CreateCMSPageForm(data=data) form.page = page form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) page = form.save() page.publish('en') with self.login_user_context(superuser): url = page.get_absolute_url('en') expected = '<div class="sub-content">{0}</div>'.format(content) unexpected = '<div class="content">{0}</div>'.format(content) response = self.client.get(url) self.assertContains(response, expected, status_code=200) self.assertNotContains(response, unexpected, status_code=200)
def test_wizard_content_placeholder_bad_setting(self): """ Tests that the PageWizard won't respect a 'bad' setting such as targeting a static-placeholder. In this case, will just fail to add the content (without error). """ templates = get_cms_setting('TEMPLATES') # NOTE, there are 4 placeholders on this template, defined in this # order: 'header', 'content', 'sub-content', 'footer'. # 'footer' is a static-placeholder. templates.append(('page_wizard.html', 'page_wizard.html', )) settings = { 'CMS_TEMPLATES': templates, 'CMS_PAGE_WIZARD_DEFAULT_TEMPLATE': 'page_wizard.html', # This is a bad setting. 'CMS_PAGE_WIZARD_CONTENT_PLACEHOLDER': 'footer', } with override_settings(**settings): superuser = self.get_superuser() page = create_page("wizard home", "page_wizard.html", "en") publish_page(page, superuser, "en") content = '<p>footer content.</p>' data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, 'content': content, } form = CreateCMSPageForm(data=data) form.page = page form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) page = form.save() page.publish('en') with self.login_user_context(superuser): url = page.get_absolute_url('en') response = self.client.get(url) self.assertNotContains(response, content, status_code=200)
def test_wizard_first_page_published(self): superuser = self.get_superuser() data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, } form = CreateCMSPageForm(data=data) form.page = None form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) page = form.save() self.assertTrue(page.is_published('en')) with self.login_user_context(superuser): url = page.get_absolute_url('en') response = self.client.get(url) self.assertEqual(response.status_code, 200)
def test_wizard_first_page_published(self): superuser = self.get_superuser() data = { 'title': 'page 1', 'slug': 'page_1', 'page_type': None, } form = CreateCMSPageForm(data=data) form.page = None form.language_code = 'en' form.user = superuser self.assertTrue(form.is_valid()) page = form.save() self.assertTrue(page.is_published('en')) with self.login_user_context(superuser): url = page.get_absolute_url('en') response = self.client.get(url) self.assertEqual(response.status_code, 200)