def test_crud_for_navigation_plugin_when_versioning_disabled(self): # The page content here is versioned because we're only disabling # versioning for navigation (i.e. MenuContent) page_content = factories.PageContentWithVersionFactory( language=self.language, version__created_by=self.get_superuser()) placeholder = factories.PlaceholderFactory(source=page_content) menu_content1 = factories.MenuContentFactory(language=self.language) menu_content2 = factories.MenuContentFactory(language=self.language) child = factories.ChildMenuItemFactory(parent=menu_content1.root) grandchild = factories.ChildMenuItemFactory(parent=child) # Patch the choices on the template field, so we don't get # form validation errors template_field = [ field for field in NavigationPlugin._meta.fields if field.name == "template" ][0] patched_choices = [ ("menu/menu.html", "Default"), ("menu/menuismo.html", "Menuismo"), ] with patch.object(template_field, "choices", patched_choices): # First add the plugin and assert # The added plugin will have the template menu/menuismo.html # and the menu from menu1 created_plugin = self._add_nav_plugin_and_assert( placeholder, menu_content1.menu, "menu/menuismo.html") # Now edit the plugin and assert # After editing the plugin will have the template menu/menu.html # and the menu from menu2 self._edit_nav_plugin_and_assert(created_plugin, menu_content2.menu, "menu/menu.html") # Now publish the page content containing the plugin, # so the page can be viewed version = page_content.versions.get() version.publish(self.get_superuser()) # And view the page page_url = page_content.page.get_absolute_url() response = self.client.get(page_url) self.assertIn(child.title, str(response.content)) self.assertIn(grandchild.title, str(response.content)) self.assertEqual(response.status_code, 200) # To delete, the version (of the PageContent object) has to be a draft new_version = version.copy(self.get_superuser()) new_placeholder = new_version.content.placeholders.first() new_plugin = new_placeholder.get_plugins()[0] # Now delete the plugin from the page and assert self._delete_nav_plugin_and_assert(new_placeholder, new_plugin)
def test_get_roots_with_versioning_disabled(self): """This test will check while versioning disabled it should assert against all menu content created """ menucontent_1 = factories.MenuContentFactory() menucontent_2 = factories.MenuContentFactory() menucontent_3 = factories.MenuContentFactory() child1 = factories.ChildMenuItemFactory(parent=menucontent_1.root) child2 = factories.ChildMenuItemFactory(parent=menucontent_2.root) grandchild = factories.ChildMenuItemFactory(parent=child1) roots = self.menu.get_roots(self.request) self.assertEqual(roots.count(), 3) self.assertListEqual( list(roots), [menucontent_1.root, menucontent_2.root, menucontent_3.root])
def test_to_search_content_object_with_nodes_mapped_to_diff_content_objects_in_node_tree( self): """ Multiple different content types mixed in a navigation tree can be found correctly by the helper. """ poll = Poll.objects.create(name="Test poll") poll_content = PollContent.objects.create(poll=poll, language="en", text="example") page_content = factories.PageContentWithVersionFactory( language=self.language, version__created_by=self.get_superuser()) menu_contents = factories.MenuContentFactory() child1 = factories.ChildMenuItemFactory(parent=menu_contents.root) factories.ChildMenuItemFactory(parent=menu_contents.root) grandchild = factories.ChildMenuItemFactory(parent=child1, content=page_content.page) grandchild1 = factories.ChildMenuItemFactory(parent=grandchild) grandchild2 = factories.ChildMenuItemFactory(parent=grandchild1, content=poll_content) result = get_navigation_node_for_content_object( menu_contents, page_content.page) self.assertEqual(result, grandchild) result = get_navigation_node_for_content_object( menu_contents, poll_content) self.assertEqual(result, grandchild2)
def test_change_view_redirects_to_login_if_non_staff_user(self): user = factories.UserFactory(is_superuser=False, is_staff=False) self.client.force_login(user) menu_content = factories.MenuContentFactory() item = factories.ChildMenuItemFactory(parent=menu_content.root) change_url = reverse( "admin:djangocms_navigation_menuitem_change", kwargs={"menu_content_id": menu_content.pk, "object_id": item.pk}, ) content_type = ContentType.objects.get(app_label="cms", model="page") page = factories.PageContentFactory().page data = { "title": "My new Title", "content_type": content_type.pk, "object_id": page.pk, "_ref_node_id": menu_content.root.id, "numchild": 1, "link_target": "_blank", "_position": "first-child", } # For POST response = self.client.post(change_url, data) redirect_url = reverse("admin:login") + "?next=" + change_url self.assertRedirects(response, redirect_url) # For GET response = self.client.get(change_url) self.assertRedirects(response, redirect_url)
def test_menuitem_add_view_smoketest_for_versioning_disabled(self): menu_content = factories.MenuContentFactory() add_url = reverse( "admin:djangocms_navigation_menuitem_add", args=(menu_content.id,) ) content_type = ContentType.objects.get(app_label="cms", model="page") page = factories.PageContentFactory().page data = { "title": "My new Title", "content_type": content_type.pk, "object_id": page.pk, "_ref_node_id": menu_content.root.id, "numchild": 1, "link_target": "_blank", "_position": "first-child", } response = self.client.post(add_url, data) self.assertRedirects( response, reverse( "admin:djangocms_navigation_menuitem_list", args=(menu_content.id,) ), )
def test_add_view_redirects_to_login_if_anonymous_user(self): menu_content = factories.MenuContentFactory() add_url = reverse( "admin:djangocms_navigation_menuitem_add", args=(menu_content.id,) ) content_type = ContentType.objects.get(app_label="cms", model="page") page = factories.PageContentFactory().page data = { "title": "My new Title", "content_type": content_type.pk, "object_id": page.pk, "_ref_node_id": menu_content.root.id, "numchild": 1, "link_target": "_blank", "_position": "first-child", } # For POST response = self.client.post(add_url, data) redirect_url = reverse("admin:login") + "?next=" + add_url self.assertRedirects(response, redirect_url) # For GET response = self.client.get(add_url) self.assertRedirects(response, redirect_url)
def test_for_url_for_result(self): menu_content = factories.MenuContentFactory(root__title="My Title") changelist = self._get_changelist_instance(menu_content) url = changelist.url_for_result(menu_content.root) expected_url = "/en/admin/djangocms_navigation/menuitem/{}/{}/change/".format( menu_content.pk, menu_content.root.pk) self.assertEqual(url, expected_url)
def test_menuitem_changelist_view_smoketest_for_versioning_disabled(self): menu_content = factories.MenuContentFactory() factories.ChildMenuItemFactory.create_batch(5, parent=menu_content.root) list_url = reverse( "admin:djangocms_navigation_menuitem_list", args=(menu_content.id,) ) response = self.client.get(list_url) self.assertEqual(response.status_code, 200)
def test_changelist_view_redirects_to_login_if_anonymous_user(self): menu_content = factories.MenuContentFactory() factories.ChildMenuItemFactory.create_batch(5, parent=menu_content.root) list_url = reverse( "admin:djangocms_navigation_menuitem_list", args=(menu_content.id,) ) response = self.client.get(list_url) redirect_url = reverse("admin:login") + "?next=" + list_url self.assertRedirects(response, redirect_url)
def test_changelist_view_redirects_to_login_if_non_staff_user(self): user = factories.UserFactory(is_superuser=False, is_staff=False) self.client.force_login(user) menu_content = factories.MenuContentFactory() factories.ChildMenuItemFactory.create_batch(5, parent=menu_content.root) list_url = reverse( "admin:djangocms_navigation_menuitem_list", args=(menu_content.id,) ) response = self.client.get(list_url) redirect_url = reverse("admin:login") + "?next=" + list_url self.assertRedirects(response, redirect_url)
def test_page_content_type_in_node_tree(self): """ The correct node mapped to page content object is returned by the helper """ menu_contents = factories.MenuContentFactory() factories.ChildMenuItemFactory(parent=menu_contents.root) page_content = factories.PageContentWithVersionFactory( language=self.language, version__created_by=self.get_superuser()) child2 = factories.ChildMenuItemFactory(parent=menu_contents.root, content=page_content.page) result = get_navigation_node_for_content_object( menu_contents, page_content.page) self.assertEqual(result, child2)
def test_menuitem_move_node_view_smoketest_for_versioning_disabled(self): menu_content = factories.MenuContentFactory() child = factories.ChildMenuItemFactory(parent=menu_content.root) child_of_child = factories.ChildMenuItemFactory(parent=child) move_url = reverse("admin:djangocms_navigation_menuitem_move_node", args=(menu_content.id, )) data = { "node_id": child_of_child.pk, "sibling_id": menu_content.root.pk, "as_child": 1, } response = self.client.post(move_url, data=data) self.assertEqual(response.status_code, 200)
def test_to_search_pollcontent_in_node_tree(self): """ The correct node mapped to poll content is returned from node tree search """ poll = Poll.objects.create(name="Test poll") poll_content = PollContent.objects.create(poll=poll, language="en", text="example") menu_contents = factories.MenuContentFactory() factories.ChildMenuItemFactory(parent=menu_contents.root) child2 = factories.ChildMenuItemFactory(parent=menu_contents.root, content=poll_content) result = get_navigation_node_for_content_object( menu_contents, poll_content) self.assertEqual(result, child2)
def test_to_search_content_object_in_nested_node_tree_search(self): """ Correct node mapped is returned by the helper in nested node tree """ menu_contents = factories.MenuContentFactory() child1 = factories.ChildMenuItemFactory(parent=menu_contents.root) page_content = factories.PageContentWithVersionFactory( language=self.language, version__created_by=self.get_superuser()) factories.ChildMenuItemFactory(parent=menu_contents.root) grandchild = factories.ChildMenuItemFactory(parent=child1) grandchild1 = factories.ChildMenuItemFactory(parent=grandchild) grandchild2 = factories.ChildMenuItemFactory(parent=grandchild1, content=page_content.page) result = get_navigation_node_for_content_object( menu_contents, page_content.page) self.assertEqual(result, grandchild2)
def test_move_node_view_redirects_to_login_if_anonymous_user(self): menu_content = factories.MenuContentFactory() child = factories.ChildMenuItemFactory(parent=menu_content.root) child_of_child = factories.ChildMenuItemFactory(parent=child) move_url = reverse("admin:djangocms_navigation_menuitem_move_node", args=(menu_content.id, )) data = { "node_id": child_of_child.pk, "sibling_id": menu_content.root.pk, "as_child": 1, } # For POST response = self.client.post(move_url, data=data) redirect_url = reverse("admin:login") + "?next=" + move_url self.assertRedirects(response, redirect_url) # For GET response = self.client.get(move_url) self.assertRedirects(response, redirect_url)
def test_content_object_mapped_to_more_than_node(self): """ The First node found is returned by helper when more than one node is mapped to content object in node tree """ menu_contents = factories.MenuContentFactory() child1 = factories.ChildMenuItemFactory(parent=menu_contents.root) page_content = factories.PageContentWithVersionFactory( language=self.language, version__created_by=self.get_superuser()) factories.ChildMenuItemFactory(parent=menu_contents.root) grandchild = factories.ChildMenuItemFactory(parent=child1, content=page_content.page) grandchild1 = factories.ChildMenuItemFactory(parent=grandchild) factories.ChildMenuItemFactory(parent=grandchild1, content=page_content.page) result = get_navigation_node_for_content_object( menu_contents, page_content.page) self.assertEqual(result, grandchild)
def test_content_object_not_found_in_node_tree(self): """ False is returned by the helper when no node is mapped to content in node tree """ menu_contents = factories.MenuContentFactory() child1 = factories.ChildMenuItemFactory(parent=menu_contents.root) page_content = factories.PageContentWithVersionFactory( language=self.language, version__created_by=self.get_superuser()) factories.ChildMenuItemFactory(parent=menu_contents.root) result = get_navigation_node_for_content_object( menu_contents, page_content.page) self.assertFalse(result) grandchild = factories.ChildMenuItemFactory(parent=child1) grandchild1 = factories.ChildMenuItemFactory(parent=grandchild) factories.ChildMenuItemFactory(parent=grandchild1) result = get_navigation_node_for_content_object( menu_contents, page_content.page) self.assertFalse(result)
def test_title(self): menu_content = factories.MenuContentFactory(root__title="My Title") self.assertEqual(menu_content.title, "My Title")
def test_string_representation(self): menu_content = factories.MenuContentFactory(root__title="My Title") self.assertEqual(str(menu_content), menu_content.title)