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_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_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)
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)