def test_template_filter(self): template_1 = get_cms_setting('TEMPLATES')[0][0] template_2 = get_cms_setting('TEMPLATES')[1][0] template_1_pages = PageContentWithVersionFactory.create_batch( 3, template=template_1, language="en") template_2_pages = PageContentWithVersionFactory.create_batch( 3, template=template_2, language="en") base_url = self.get_admin_url(PageContent, "changelist") with self.login_user_context(self.get_superuser()): # All / No templates filtered is the default response_default = self.client.get(base_url) # fullwidth template set response_template_1 = self.client.get( base_url + "?template={}".format(template_1)) # page template set response_template_2 = self.client.get( base_url + "?template={}".format(template_2)) self.assertSetEqual(set(response_default.context["cl"].queryset), set(template_1_pages) | set(template_2_pages)) self.assertSetEqual(set(response_template_1.context["cl"].queryset), set(template_1_pages)) self.assertSetEqual(set(response_template_2.context["cl"].queryset), set(template_2_pages))
def test_post_with_parent(self): pagecontent1 = PageContentWithVersionFactory( template="page.html", page__node__depth=0, page__node__path="0001", page__node__numchild=1, ) PageUrl.objects.create( slug="foo", path="foo", language=pagecontent1.language, page=pagecontent1.page, ) pagecontent2 = PageContentWithVersionFactory( template="page.html", language=pagecontent1.language, page__node__parent_id=pagecontent1.page.node_id, page__node__depth=1, page__node__path="00010001", ) placeholder = PlaceholderFactory(slot="content", source=pagecontent2) add_plugin(placeholder, "TextPlugin", pagecontent2.language, body="Test text") with self.login_user_context(self.get_superuser()): response = self.client.post( self.get_admin_url(PageContent, "duplicate_content", pagecontent2.pk), data={ "site": Site.objects.first().pk, "slug": "bar" }, follow=True, ) self.assertRedirects(response, self.get_admin_url(PageContent, "changelist")) new_pagecontent = PageContent._base_manager.latest("pk") new_placeholder = new_pagecontent.placeholders.get(slot="content") self.assertEqual(PageContent._base_manager.count(), 3) self.assertNotEqual(pagecontent2, new_pagecontent) self.assertNotEqual(pagecontent2.page, new_pagecontent.page) self.assertEqual(pagecontent2.language, new_pagecontent.language) self.assertEqual( new_pagecontent.page.get_path(new_pagecontent.language), "foo/bar") new_plugins = list(downcast_plugins( new_placeholder.get_plugins_list())) self.assertEqual(len(new_plugins), 1) self.assertEqual(new_plugins[0].plugin_type, "TextPlugin") self.assertEqual(new_plugins[0].body, "Test text")
def test_unpublished_filter(self): expected = PageContentWithVersionFactory.create_batch(3, language="en") expected_unpublished = PageContentWithVersionFactory.create_batch( 2, language="en", version__state=UNPUBLISHED) model = PageContent base_url = self.get_admin_url(model, "changelist") with self.login_user_context(self.get_superuser()): # en is the default language configured for the site response_default = self.client.get(base_url) qs_default = response_default.context["cl"].queryset response_unpublished = self.client.get(base_url + "?unpublished=1") qs_unpublished = response_unpublished.context["cl"].queryset self.assertEqual(set(qs_default), set(expected)) self.assertEqual(set(qs_unpublished), set(expected_unpublished))
def test_cms_page_toolbar_pages_link_doesnt_filter_the_page_list_by_page_id( self): """ The PageToolbar "Pages" link sends a parameter "page_id=X" which filters the page admin changelist. This is not desired behaviour for this package, the page_id for the standard cms is designed to allow the page tree to show a tree from a page i.e. expand any children below it. The page admin for this package does not use a page tree so this functionality breaks the admin changelist by only showing the page with the matching id. The PageAdmin page list should not be affected by the page_id parameter. """ template_1 = get_cms_setting('TEMPLATES')[0][0] pages = PageContentWithVersionFactory.create_batch(6, template=template_1, language="en") base_url = self.get_admin_url(PageContent, "changelist") simulated_toolbar_pages_link = "/en/admin/cms/pagecontent/?language=en&page_id=1" with self.login_user_context(self.get_superuser()): # Default changelist link response = self.client.get(base_url) # Toolbar pages link simulation response_with_page_id = self.client.get( simulated_toolbar_pages_link) self.assertSetEqual(set(response.context["cl"].queryset), set(pages)) self.assertSetEqual(set(response_with_page_id.context["cl"].queryset), set(pages))
def test_post(self): """the slot for content is always there, the slot for navigation needs to be created""" pagecontent = PageContentWithVersionFactory(template="page.html") placeholder = PlaceholderFactory(slot="content", source=pagecontent) PlaceholderFactory(slot="navigation", source=pagecontent) add_plugin(placeholder, "TextPlugin", pagecontent.language, body="Test text") with self.login_user_context(self.get_superuser()): response = self.client.post( self.get_admin_url(PageContent, "duplicate", pagecontent.pk), data={ "site": Site.objects.first().pk, "slug": "foo bar" }, follow=True, ) self.assertRedirects(response, self.get_admin_url(PageContent, "changelist")) new_pagecontent = PageContent._base_manager.latest("pk") new_placeholder = new_pagecontent.placeholders.get(slot="content") self.assertEqual(PageContent._base_manager.count(), 2) self.assertNotEqual(pagecontent, new_pagecontent) self.assertNotEqual(pagecontent.page, new_pagecontent.page) self.assertEqual(pagecontent.language, new_pagecontent.language) self.assertEqual( new_pagecontent.page.get_slug(new_pagecontent.language), "foo-bar") new_plugins = list(downcast_plugins( new_placeholder.get_plugins_list())) self.assertEqual(len(new_plugins), 1) self.assertEqual(new_plugins[0].plugin_type, "TextPlugin") self.assertEqual(new_plugins[0].body, "Test text")
def test_get(self): pagecontent = PageContentWithVersionFactory() with self.login_user_context(self.get_superuser()): response = self.client.get( self.get_admin_url(PageContent, "duplicate", pagecontent.pk)) self.assertEqual(response.status_code, 200) self.assertEqual(PageContent._base_manager.count(), 1)
def test_language_filter(self): expected_en = PageContentWithVersionFactory.create_batch(3, language="en") expected_de = PageContentWithVersionFactory.create_batch(3, language="de") model = PageContent base_url = self.get_admin_url(model, "changelist") with self.login_user_context(self.get_superuser()): # en is the default language configured for the site response_default = self.client.get(base_url) qs_default = response_default.context["cl"].queryset response_en = self.client.get(base_url + "?language=en") qs_en = response_en.context["cl"].queryset response_de = self.client.get(base_url + "?language=de") qs_de = response_de.context["cl"].queryset self.assertEqual(set(qs_default), set(expected_en)) self.assertEqual(set(qs_en), set(expected_en)) self.assertEqual(set(qs_de), set(expected_de))
def test_get_method_is_not_allowed(self): pagecontent = PageContentWithVersionFactory() with self.login_user_context(self.get_superuser()): response = self.client.get( self.get_admin_url(PageContent, "set_home_content", pagecontent.pk)) pagecontent.page.refresh_from_db() self.assertEqual(response.status_code, 405) self.assertFalse(pagecontent.page.is_home)
def test_edit_link_not_shown(self): pagecontent = PageContentWithVersionFactory(version__state=ARCHIVED) func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-edit"}) self.assertIsNot( element, "Element a.cms-page-admin-action-edit is shown when it shouldn't")
def test_preview_link(self): pagecontent = PageContentWithVersionFactory() func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-preview"}) self.assertIsNotNone(element, "Missing a.cms-page-admin-action-preview element") self.assertEqual(element["title"], "Preview") self.assertEqual(element["href"], get_object_preview_url(pagecontent))
def test_edit_link_inactive(self): pagecontent = PageContentWithVersionFactory() func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-edit"}) self.assertIsNotNone(element, "Missing a.cms-page-admin-action-edit element") self.assertEqual(element["title"], "Edit") self.assertIn("inactive", element["class"]) self.assertNotIn("href", element)
def test_queryset_is_filtered_by_current_site(self): site1 = SiteFactory() site2 = SiteFactory() site1_pagecontents = PageContentWithVersionFactory.create_batch( 2, page__node__site=site1, language="en" ) site2_pagecontents = [ PageContentWithVersionFactory(page__node__site=site2, language="en") ] model = PageContent url = self.get_admin_url(model, "changelist") with self.login_user_context(self.get_superuser()): with self.settings(SITE_ID=site1.pk): response1 = self.client.get(url) with self.settings(SITE_ID=site2.pk): response2 = self.client.get(url) self.assertEqual(response1.status_code, 200) self.assertEqual(response2.status_code, 200) self.assertEqual(set(site1_pagecontents), set(response1.context["cl"].queryset)) self.assertEqual(set(site2_pagecontents), set(response2.context["cl"].queryset))
def test_post_empty_slug(self): pagecontent = PageContentWithVersionFactory() with self.login_user_context(self.get_superuser()): response = self.client.post( self.get_admin_url(PageContent, "duplicate", pagecontent.pk), data={"slug": ""}, ) form = response.context["form"] self.assertEqual(response.status_code, 200) self.assertFalse(form.is_valid()) self.assertIn("slug", form.errors) self.assertEqual(form.errors["slug"], ["This field is required."])
def test_post_empty_slug_after_slugify(self): pagecontent = PageContentWithVersionFactory() with self.login_user_context(self.get_superuser()): response = self.client.post( self.get_admin_url(PageContent, "duplicate", pagecontent.pk), data={"site": Site.objects.first().pk, "slug": "£"}, ) form = response.context["form"] self.assertEqual(response.status_code, 200) self.assertFalse(form.is_valid()) self.assertIn("slug", form.errors) self.assertEqual(form.errors["slug"], ["Slug must not be empty."])
def test_basic_settings_link(self): pagecontent = PageContentWithVersionFactory() func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-basic-settings"}) self.assertIsNotNone( element, "Missing a.cms-page-admin-action-basic-settings element") self.assertEqual(element["title"], "Basic settings") self.assertEqual( element["href"], reverse("admin:cms_pagecontent_change", args=(pagecontent.pk, )), )