def test_when_new_homepage_tree_has_apphooks_should_trigger_signal(self): PageVersionFactory( content__page__node__depth=1, content__page__is_home=1, state=PUBLISHED ) version = PageVersionFactory(content__page__node__depth=1, state=PUBLISHED) to_be_homepage = version.content with self.login_user_context(self.get_superuser()), patch( "cms.models.query.PageQuerySet.has_apphooks", return_value=True ), patch("djangocms_pageadmin.admin.set_restart_trigger") as mock_handler: self.client.post( self.get_admin_url(PageContent, "set_home_content", to_be_homepage.pk) ) mock_handler.assert_called_once_with()
def test_set_home_is_wrapped_in_db_transaction(self): class FakeError(Exception): pass version = PageVersionFactory(content__page__node__depth=1, state=PUBLISHED) page_content = version.content # Asserting to make sure page is not set as homepage self.assertFalse(page_content.page.is_home) # Patching has_apphooks which is get called after setting home on view so transaction # should roll back in event of error with patch("cms.models.query.PageQuerySet.has_apphooks", side_effect=FakeError): try: self.client.post( reverse("admin:cms_pagecontent_set_home_content", args=[page_content.pk])) except FakeError: pass # Refresh object from db page_content.page.refresh_from_db() self.assertFalse(page_content.page.is_home)
def test_submenu_does_not_cause_crash_in_change_admin_menu(self): """ change_admin_menu should handle SubMenus as the first item. """ # Earlier versions of change_admin_menu assumed every menu item # had a url attribute, which is not true for SubMenu. user = self.get_superuser() pagecontent = PageVersionFactory(content__template="") toolbar = get_toolbar( pagecontent.content, user=user, toolbar_class=PageAdminToolbar, preview_mode=True, ) menu = toolbar.toolbar.get_menu("admin-menu") menu.get_or_create_menu( key='sub_menu_key', verbose_name='My sub-menu', position=0, ) try: toolbar.change_admin_menu() except AttributeError as e: if e.args and e.args[0] == "'SubMenu' object has no attribute 'url'": self.fail('change_admin_menu should handle SubMenu as the first item.') raise
def test_unpublish_not_shown(self): version = PageVersionFactory() pagecontent = version.content func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-unpublish"}) self.assertIsNone( element, "Element a.cms-page-admin-action-unpublish is shown when it shouldn't", )
def test_non_root_page_should_not_allowed_to_set_home(self): version = PageVersionFactory(content__page__node__depth=2, state=PUBLISHED) pagecontent = version.content with self.login_user_context(self.get_superuser()): response = self.client.post( self.get_admin_url(PageContent, "set_home_content", pagecontent.pk)) self.assertEqual(response.status_code, 400) self.assertEqual(response.content.decode("utf-8"), "The page is not eligible to be home.") self.assertFalse(pagecontent.page.is_home)
def test_when_old_home_tree_is_none_should_not_trigger_signal(self): version = PageVersionFactory(content__page__node__depth=1, state=PUBLISHED) pagecontent = version.content with self.login_user_context(self.get_superuser()), patch( "djangocms_pageadmin.admin.set_restart_trigger" ) as mock_handler: self.client.post( self.get_admin_url(PageContent, "set_home_content", pagecontent.pk) ) mock_handler.assert_not_called()
def test_set_home_link(self): version = PageVersionFactory(state=PUBLISHED) pagecontent = version.content func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-set-home"}) self.assertEqual(element["title"], "Set as a home") self.assertEqual( element["href"], reverse("admin:cms_pagecontent_set_home_content", args=(version.pk,)), )
def test_root_page_is_allowed_to_set_home(self): version = PageVersionFactory(content__page__node__depth=1, state=PUBLISHED) pagecontent = version.content with self.login_user_context(self.get_superuser()): response = self.client.post( self.get_admin_url(PageContent, "set_home_content", pagecontent.pk) ) url = reverse("admin:cms_pagecontent_changelist") self.assertEqual(response.status_code, 302) self.assertEqual(response.url, url) pagecontent.page.refresh_from_db() self.assertTrue(pagecontent.page.is_home)
def test_duplicate_link(self): version = PageVersionFactory(state=PUBLISHED) pagecontent = version.content func = self.modeladmin._list_actions(self.get_request("/")) response = func(pagecontent) soup = parse_html(response) element = soup.find("a", {"class": "cms-page-admin-action-duplicate"}) self.assertIsNotNone( element, "Missing a.cms-page-admin-action-duplicate element") self.assertEqual(element["title"], "Duplicate") self.assertEqual( element["href"], reverse("admin:cms_pagecontent_duplicate", args=(version.pk, )), )
def test_permission_to_set_home_page(self): version = PageVersionFactory(content__page__node__depth=1, state=PUBLISHED) pagecontent = version.content with self.login_user_context(self.get_superuser()), patch( "cms.models.pagemodel.Page.has_change_permission", return_value=False ) as mock: response = self.client.post( self.get_admin_url(PageContent, "set_home_content", pagecontent.pk) ) self.assertEqual(mock.call_count, 1) self.assertEqual(response.status_code, 403) pagecontent.page.refresh_from_db() self.assertFalse(pagecontent.page.is_home)
def test_pages_menu_item_url_has_no_params(self): """ Create a page and get the toolbar for that page's preview Then check that the page menu item URL does not have an id parameter in the query string, so as not to trigger filters """ user = self.get_superuser() pagecontent = PageVersionFactory(content__template="") toolbar = get_toolbar( pagecontent.content, user=user, toolbar_class=PageAdminToolbar, preview_mode=True, ) toolbar.post_template_populate() menu = toolbar.toolbar.get_menu("admin-menu") pagemenu = menu.get_items()[0] self.assertEqual("/en/admin/cms/pagecontent/", pagemenu.url)
def test_edit_link(self): user = UserFactory() version = PageVersionFactory(created_by=user) pagecontent = version.content request = self.get_request("/") request.user = user func = self.modeladmin._list_actions(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.assertEqual( element["href"], reverse( "admin:djangocms_versioning_pagecontentversion_edit_redirect", args=(version.pk,), ), )