def test_select_node_from_deeply_nested_nodes(self):
        """
        Performance check to retrieve a page from node with and without soft_root node
        """
        page_content = factories.PageContentWithVersionFactory(
            version__created_by=self.get_superuser(),
            title="test",
            menu_title="test",
            page_title="test",
            version__state=PUBLISHED)

        menuversions = factories.MenuVersionFactory(
            state=PUBLISHED, content__language=self.language)
        factories.ChildMenuItemFactory(parent=menuversions.content.root,
                                       content=page_content.page)
        factories.ChildMenuItemFactory(parent=menuversions.content.root)
        child3 = factories.ChildMenuItemFactory(
            parent=menuversions.content.root)
        factories.ChildMenuItemFactory(parent=child3)
        max_queries = 52
        page_url = page_content.page.get_absolute_url()

        with self.assertNumQueries(FuzzyInt(3, max_queries)):
            self.client.get(page_url)

        child3.soft_root = True
        with self.assertNumQueries(FuzzyInt(3, max_queries)):
            self.client.get(page_url)
예제 #2
0
    def test_crud_for_navigation_plugin_when_versioning_enabled(self):
        # NOTE: This test is based on a similar one from django-cms:
        # https://github.com/divio/django-cms/blob/2daeb7d63cb5fee49575a834d0f23669ce46144e/cms/tests/test_plugins.py#L160

        # Set up a versioned page with one placeholder
        page_content = factories.PageContentWithVersionFactory(
            language=self.language, version__created_by=self.get_superuser())
        placeholder = factories.PlaceholderFactory(source=page_content)
        menu_content1 = factories.MenuContentWithVersionFactory(
            language=self.language)
        menu_content2 = factories.MenuContentWithVersionFactory(
            language=self.language)
        child = factories.ChildMenuItemFactory(parent=menu_content2.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())
        menu_version = menu_content2.versions.get()
        menu_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 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_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)
예제 #4
0
    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_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)
예제 #6
0
    def test_menu_cache_invalidate_after_menucontent_archive(self):
        # NOTE: This test is based on a similar one from django-cms:
        # https://github.com/divio/django-cms/blob/2daeb7d63cb5fee49575a834d0f23669ce46144e/cms/tests/test_plugins.py#L160

        menucontent = factories.MenuContentWithVersionFactory(
            language=self.language, version__state=PUBLISHED)
        pagecontent = factories.PageContentWithVersionFactory(
            language=self.language, version__created_by=self.get_superuser())
        factories.ChildMenuItemFactory(parent=menucontent.root,
                                       content=pagecontent.page)
        draft_child = factories.ChildMenuItemFactory(parent=menucontent.root,
                                                     content=pagecontent.page)
        # Set up a versioned page with one placeholder
        placeholder = factories.PlaceholderFactory(source=pagecontent)
        # grandchild
        factories.ChildMenuItemFactory(parent=draft_child)

        # TODO: Use a factory instead
        # Add nav plugin to placeholder
        self._add_nav_plugin_and_assert(placeholder, menucontent.menu,
                                        "menu/menu.html")

        # Now publish the page content containing the plugin,
        # so the page can be viewed
        version = pagecontent.versions.get()
        version.publish(self.get_superuser())

        menu_published_version = menucontent.versions.get()
        menu_content_draft = menu_published_version.copy(self.get_superuser())

        # Making sure there is no cachekey existed before rendering page
        cache_key = CacheKey.objects.all().count()
        self.assertEqual(cache_key, 0)

        # And view the page
        page_url = pagecontent.page.get_absolute_url()
        response = self.client.get(page_url)

        cache_key = CacheKey.objects.all().count()
        self.assertEqual(response.status_code, 200)
        # Rendering should generate cachekey object
        self.assertEqual(cache_key, 1)

        menu_content_draft.archive(user=self.get_superuser())

        # Version archive action should invalidate cache_key object
        cache_key = CacheKey.objects.all().count()
        self.assertEqual(cache_key, 0)
예제 #7
0
    def test_menu_cache_invalidate_after_menuitem_edit(self):
        # NOTE: This test is based on a similar one from django-cms:
        # https://github.com/divio/django-cms/blob/2daeb7d63cb5fee49575a834d0f23669ce46144e/cms/tests/test_plugins.py#L160

        # Set up a versioned page with one placeholder
        page_content = factories.PageContentWithVersionFactory(
            language=self.language, version__created_by=self.get_superuser())
        placeholder = factories.PlaceholderFactory(source=page_content)
        menu_content = factories.MenuContentWithVersionFactory(
            language=self.language)
        child = factories.ChildMenuItemFactory(parent=menu_content.root)
        grandchild = factories.ChildMenuItemFactory(parent=child)

        # TODO: Use a factory instead
        # Add nav plugin to placeholder
        self._add_nav_plugin_and_assert(placeholder, menu_content.menu,
                                        "menu/menu.html")

        # Now publish menucontent and the page content containing the plugin,
        # so the page can be viewed
        version = page_content.versions.get()
        version.publish(self.get_superuser())
        menu_version = menu_content.versions.get()
        menu_version.publish(self.get_superuser())

        # Making sure there is no cachekey existed before rendering page
        cache_key = CacheKey.objects.all().count()
        self.assertEqual(cache_key, 0)

        # And view the page
        page_url = page_content.page.get_absolute_url()
        response = self.client.get(page_url)

        cache_key = CacheKey.objects.all().count()
        # Rendering should generate cachekey object
        self.assertEqual(cache_key, 1)

        # Check http response is ok
        self.assertEqual(response.status_code, 200)
        self.assertIn(child.title, str(response.content))
        self.assertIn(grandchild.title, str(response.content))

        # Create further menu items
        factories.ChildMenuItemFactory(parent=menu_content.root)

        # MenuItem creation should be invalidated cache_key object
        cache_key = CacheKey.objects.all().count()
        self.assertEqual(cache_key, 0)
    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_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)
예제 #10
0
    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)