def test_models_glimpse_link(self): """ The glimpse model methods "get_link" and "is_blank_target" should return the right value depending glimpse has no link, an internal link or an external link. """ page = PageFactory(title__title="Internal link") glimpse_no_link = GlimpseFactory() glimpse_external_link = GlimpseFactory(link_url="http://perdu.com") glimpse_internal_link = GlimpseFactory(link_page=page) glimpse_both_link = GlimpseFactory(link_url="http://perdu.com", link_page=page) # Check expected page url self.assertEqual(page.get_absolute_url(), "/en/internal-link/") # Check the plugin url self.assertEqual(glimpse_no_link.get_link(), None) self.assertEqual(glimpse_external_link.get_link(), "http://perdu.com") self.assertEqual(glimpse_internal_link.get_link(), page.get_absolute_url()) self.assertEqual(glimpse_both_link.get_link(), "http://perdu.com") # Check the link target self.assertEqual(glimpse_no_link.is_blank_target(), False) self.assertEqual(glimpse_external_link.is_blank_target(), True) self.assertEqual(glimpse_internal_link.is_blank_target(), False) self.assertEqual(glimpse_both_link.is_blank_target(), True)
def test_template_person_detail_without_person(self): """ A person template page without attached person should show an error banner explaining to the user that he/she is misusing the template. """ page = PageFactory( template="courses/cms/person_detail.html", title__language="en", should_publish=True, ) with self.assertTemplateUsed( "courses/cms/fragment_error_detail_template_banner.html"): response = self.client.get(page.get_absolute_url()) self.assertEqual(response.status_code, 200) self.assertContains( response, ('<div class="banner banner--error banner--rounded" role="alert">' '<svg class="banner__icon"><use href="#icon-cross" /></svg>' '<p class="banner__message">' "A person object is missing on this person page. " "Please select another page template." "<br />" "If what you need is a person page, you need to create it " 'via the wizard and choose "New person page".' "</p>" "</div>"), html=True, )
def test_context_processors_retrieve_privacy_page_url_if_exists(self): """ If a privacy policy page exists, tarteaucitron context should contains its url. """ page = PageFactory( should_publish=True, template="richie/single_column.html", title__language="fr", languages="fr", ) response = self.client.get(page.get_public_url()) self.assertEqual(response.status_code, 200) self.assertContains(response, '"tarteaucitron":') self.assertContains(response, '"privacyUrl": null') privacy_page = PageFactory( title__title="Privacy policy", should_publish=True, reverse_id="annex__privacy", title__language="fr", languages="fr", ) response = self.client.get(page.get_public_url()) self.assertEqual(response.status_code, 200) self.assertContains( response, f'"privacyUrl": "{privacy_page.get_public_url()}"')
def test_page_includes_frontend_context(self): """ Create a page and make sure it includes the frontend context as included in `base.html`. All characters for use in javascript string should be escaped (i.e: '"' should be escaped with \u0022 and '\' with \u005C) to prevent an issue when this variable contains unescaped characters for javascript (e.g: '\'). ⚠️ If this test fails, before fixing it, identify if this change has had ⚠️ an impact on frontend and update frontend accordingly. """ page = PageFactory(should_publish=True, template="richie/single_column.html") response = self.client.get(page.get_public_url()) self.assertContains( response, r"\u0022environment\u0022: \u0022test_pages\u0022") self.assertContains(response, r"\u0022release\u0022: \u00229.8.7\u0022") self.assertContains( response, r"\u0022sentry_dsn\u0022: \u0022https://example.com/sentry/dsn\u0022", ) self.assertContains(response, r"\u0022lms_backends\u0022: [") self.assertContains( response, r"\u0022endpoint\u0022: \u0022https://lms.example.com\u0022") self.assertContains(response, r"\u0022backend\u0022: \u0022base\u0022") self.assertContains( response, ( r"\u0022course_regexp\u0022: " r"\u0022^https://lms\u005C\u005C.example\u005C\u005C.com/courses/(.*)/course/?$\u0022" # noqa pylint: disable=line-too-long ), )
def test_templates_blogpost_list_related_categories(self): """ The top of the page should list all categories related to at least one of the blog posts on the blog posts list page. """ page = PageFactory( template="courses/cms/blogpost_list.html", title__language="en", should_publish=True, ) post1, post2 = BlogPostFactory.create_batch(2, page_parent=page, should_publish=True) category1, category2, category12, category_alone = CategoryFactory.create_batch( 4, should_publish=True) # Attach categories to post1 placeholder = post1.extended_object.get_public_object( ).placeholders.all()[0] add_plugin(placeholder, "CategoryPlugin", "en", page=category1.extended_object) add_plugin(placeholder, "CategoryPlugin", "en", page=category12.extended_object) # Attach categories to post2 placeholder = post2.extended_object.get_public_object( ).placeholders.all()[0] add_plugin(placeholder, "CategoryPlugin", "en", page=category2.extended_object) add_plugin(placeholder, "CategoryPlugin", "en", page=category12.extended_object) response = self.client.get(page.get_absolute_url()) self.assertEqual(response.status_code, 200) for category in [category1, category2, category12]: self.assertContains( response, ('<a class="category-tag" href="{slug:s}">' '<span class="category-tag__title">{title:s}</span>' "</a>").format( slug=category.extended_object.get_absolute_url(), title=category.extended_object.get_title(), ), html=True, ) self.assertNotContains( response, category_alone.extended_object.get_absolute_url())
def test_page_includes_frontend_context(self): """ Create a page and make sure it includes the frontend context as included in `base.html`. """ page = PageFactory(should_publish=True, template="richie/single_column.html") response = self.client.get(page.get_public_url()) self.assertContains(response, '"environment": "test_pages"') self.assertContains(response, '"release": "9.8.7"') self.assertContains(response, '"sentry_dsn": "https://example.com/sentry/dsn"')
def test_page_includes_frontend_context(self): """ Create a page and make sure it includes the frontend context as included in `base.html`. ⚠️ If this test fails, before fixing it, identify if this change has had ⚠️ an impact on frontend and update frontend accordingly. """ page = PageFactory(should_publish=True, template="richie/single_column.html") response = self.client.get(page.get_public_url()) self.assertContains(response, '"environment": "test_pages"') self.assertContains(response, '"release": "9.8.7"') self.assertContains(response, '"sentry_dsn": "https://example.com/sentry/dsn"')
def create_page_tree(parent_kwargs=None): """ Not a test. Create a minimal site structure on which to test the sitemap plugin. """ root = PageFactory(title__title="Root") parent = PageFactory( title__title="Parent", parent=root, **(parent_kwargs or {}) ) page = PageFactory(title__title="Uncle", parent=root) PageFactory(title__title="Page", parent=parent) PageFactory(title__title="Sibling", parent=parent) return root, parent, page
def test_cms_plugins_htmlsitemap_no_current_page(self): """ A sitemap plugin inserted on a page with no current_page in its context should display all published pages from its root """ self.create_page_tree() root = PageFactory( title__title="Root", title__language="en", should_publish=True ) PageFactory( title__title="Parent", parent=root, title__language="en", should_publish=True, ) PageFactory( title__title="Uncle", parent=root, title__language="en", should_publish=True ) self.assertEqual(StaticPlaceholder.objects.count(), 1) placeholder = StaticPlaceholder.objects.get() context = self.get_practical_plugin_context() parent_instance = add_plugin(placeholder.draft, HTMLSitemapPlugin, "en") add_plugin( placeholder.draft, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, ) html = context["cms_content_renderer"].render_placeholder( placeholder.draft, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/root/parent/">Parent</a></li> <li><a href="/en/root/uncle/">Uncle</a></li> </ul> </li> </ul> </div> """, )
def test_context_processors_add_xiti_settings_if_exists(self): """ Create a page and make sure it includes web analytics context as included in `base.html`. """ page = PageFactory( should_publish=True, template="richie/single_column.html", ) response = self.client.get(page.get_public_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertContains(response, '"provider": "xiti"') self.assertContains(response, '"id": "123456"') self.assertContains(response, f'"root_page_id": "{page.node.get_root().pk}"')
def test_templates_course_run_detail_breadcrumb_below_snapshot(self): """ Validate the format of the breadcrumb on a course run placed below a snapshot. """ home_page = PageFactory(title__title="home", title__language="en", should_publish=True) search_page = PageFactory( title__title="courses", title__language="en", parent=home_page, should_publish=True, ) course = CourseFactory( page_title="course name", page_parent=search_page, page_in_navigation=True, should_publish=True, ) snapshot = CourseFactory( page_title="snapshot name", page_parent=course.extended_object, page_in_navigation=True, should_publish=True, ) course_run = CourseRunFactory( page_title="session 42", page_parent=snapshot.extended_object, should_publish=True, ) response = self.client.get( course_run.extended_object.get_absolute_url()) self.assertEqual(response.status_code, 200) self.assertContains( response, ('<ul class="breadcrumbs__list">' ' <li class="breadcrumbs__item">You are here:</li>' ' <li class="breadcrumbs__item"><a href="/en/home/">home</a></li>' ' <li class="breadcrumbs__item"><a href="/en/home/courses/">courses</a></li>' ' <li class="breadcrumbs__item">' ' <a href="/en/home/courses/course-name/">course name</a>' " </li>" ' <li class="breadcrumbs__item"><span class="active">session 42</span></li>' "</ul>"), html=True, )
def test_context_processors_do_not_add_xiti_settings_if_marketing_site_id_is_not_defined( self, ): """ If WEB_ANALYTICS_ID setting is not defined, frontend context should contains analytics data """ page = PageFactory( should_publish=True, template="richie/single_column.html", ) response = self.client.get(page.get_public_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertNotContains(response, '"provider": "') self.assertNotContains(response, '"id": "') self.assertNotContains(response, f'"root_page_id": "{page.node.get_root().pk}"')
def test_models_course_create_page_role(self, *_): """ If the CMS_PERMISSIONS settings is True, a page role should be created when saving a course. """ role_dict = { "django_permissions": ["cms.change_page"], "course_page_permissions": { "can_change": random.choice([True, False]), "can_add": random.choice([True, False]), "can_delete": random.choice([True, False]), "can_change_advanced_settings": random.choice([True, False]), "can_publish": random.choice([True, False]), "can_change_permissions": random.choice([True, False]), "can_move_page": random.choice([True, False]), "can_view": False, # can_view = True would make it a view restriction... "grant_on": random.randint(1, 5), }, "course_folder_permissions": { "can_read": random.choice([True, False]), "can_edit": random.choice([True, False]), "can_add_children": random.choice([True, False]), "type": random.randint(0, 2), }, } page = PageFactory(title__title="My title") course = CourseFactory(extended_object=page) self.assertFalse(page.roles.exists()) with mock.patch.dict(defaults.COURSE_ADMIN_ROLE, role_dict): course.create_page_role() # A page role should have been created self.assertEqual(page.roles.count(), 1) role = page.roles.get(role="ADMIN") self.assertEqual(role.group.name, "Admin | My title") self.assertEqual(role.group.permissions.count(), 1) self.assertEqual(role.folder.name, "Admin | My title") # All expected permissions should have been assigned to the group: # - Django permissions self.assertEqual(role.group.permissions.first().codename, "change_page") # - DjangoCMS page permissions self.assertEqual( PagePermission.objects.filter(group=role.group).count(), 1) page_permission = PagePermission.objects.get(group=role.group) for key, value in role_dict["course_page_permissions"].items(): self.assertEqual(getattr(page_permission, key), value) # The Django Filer folder permissions self.assertEqual( FolderPermission.objects.filter(group_id=role.group_id).count(), 1) folder_permission = FolderPermission.objects.get( group_id=role.group_id) for key, value in role_dict["course_folder_permissions"].items(): self.assertEqual(getattr(folder_permission, key), value)
def test_models_organization_str_code_none(self): """ A organization with a nill code should not raise an error when printing the object. """ page = PageFactory(title__title="An organization name") organization = OrganizationFactory(extended_object=page, should_publish=True, code=None) self.assertEqual("Organization: An organization name", str(organization))
def test_context_processors_do_not_add_xiti_settings_if_page_is_draft( self, ): """ If the current page is a draft, frontend context should contains an empty xiti object """ user = UserFactory(is_staff=True, is_superuser=True) self.client.login(username=user.username, password="******") # nosec page = PageFactory( should_publish=False, template="richie/single_column.html", ) response = self.client.get(page.get_absolute_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertNotContains(response, '"provider": "xiti"') self.assertNotContains(response, '"id": "123456"') self.assertNotContains(response, f'"root_page_id": "{page.node.get_root().pk}"')
def test_templates_category_list_cms_content(self): """ Validate that the public website only displays categories that are currently published, while staff users can see draft and unpublished categories. """ page = PageFactory( template="courses/cms/category_list.html", title__language="en", should_publish=True, ) CategoryFactory(page_parent=page, page_title="First category") CategoryFactory(page_parent=page, page_title="Second category", should_publish=True) # Publish with a publication date in the future future = timezone.now() + timedelta(hours=1) with mock.patch("cms.models.pagemodel.now", return_value=future): CategoryFactory(page_parent=page, page_title="Third category", should_publish=True) # Anonymous users should only see published categories response = self.client.get(page.get_absolute_url()) self.assertEqual(response.status_code, 200) self.assertNotContains(response, "First") self.assertContains(response, "Second") self.assertNotContains(response, "Third") # Staff users can see draft and unpublished categories user = UserFactory(is_staff=True, is_superuser=True) self.client.login(username=user.username, password="******") response = self.client.get(page.get_absolute_url()) self.assertEqual(response.status_code, 200) for title in ["First", "Second", "Third"]: self.assertContains(response, title)
def test_cms_plugins_htmlsitemap_no_exclusion(self): """ Non regression test. A sitemap configuration that does not trigger any exclusion should not be empty. """ self.create_page_tree(parent_kwargs={"login_required": True}) page = PageFactory(title__title="Sitemap") placeholder = Placeholder.objects.create(slot="maincontent") page.placeholders.add(placeholder) context = self.get_practical_plugin_context() request = RequestFactory() request.current_page = page request.user = UserFactory() context["request"] = request parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/parent/">Parent</a> <ul> <li><a href="/en/page/">Page</a></li> <li><a href="/en/sibling/">Sibling</a></li> </ul> </li> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, )
def test_cms_plugins_organizations_by_category_form_page_choices(self): """ The form to create an organizations by category plugin should only list category pages in the select box. There shouldn't be any duplicate because of published status. """ class OrganizationsByCategoryPluginModelForm(forms.ModelForm): """A form for testing the choices in the select box""" class Meta: model = OrganizationsByCategoryPluginModel fields = ["page"] category = CategoryFactory(should_publish=True) PageFactory(title__title="other page", should_publish=True) plugin_form = OrganizationsByCategoryPluginModelForm() rendered_form = plugin_form.as_table() self.assertEqual( rendered_form.count(category.extended_object.get_title()), 1) self.assertNotIn("other", plugin_form.as_table())
def test_cms_plugins_htmlsitemap_no_root_page(self): """A sitemap page not targeted to a root page should display the whole site structure.""" self.create_page_tree() page = PageFactory(title__title="Sitemap") placeholder = Placeholder.objects.create(slot="maincontent") page.placeholders.add(placeholder) context = self.get_practical_plugin_context() parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/parent/">Parent</a> <ul> <li><a href="/en/page/">Page</a></li> <li><a href="/en/sibling/">Sibling</a></li> </ul> </li> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, )
def test_cms_plugins_htmlsitemap_static_placeholder(self): """A sitemap page placed in a static placeholder should work.""" self.create_page_tree() page = PageFactory(title__title="Sitemap") self.assertEqual(StaticPlaceholder.objects.count(), 1) placeholder = StaticPlaceholder.objects.get() context = self.get_practical_plugin_context({"current_page": page}) parent_instance = add_plugin(placeholder.draft, HTMLSitemapPlugin, "en") add_plugin( placeholder.draft, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, ) html = context["cms_content_renderer"].render_placeholder( placeholder.draft, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/root/parent/">Parent</a> <ul> <li><a href="/en/root/parent/page/">Page</a></li> <li><a href="/en/root/parent/sibling/">Sibling</a></li> </ul> </li> <li><a href="/en/root/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, )
def test_cms_plugins_htmlsitemap_root_page_include_max_depth(self): """ A site map page targeted to a root page in "include" mode can be limited in depth (number of nesting levels displayed). """ root_page, _parent, _page = self.create_page_tree() page = PageFactory(title__title="Sitemap") placeholder = Placeholder.objects.create(slot="maincontent") page.placeholders.add(placeholder) context = self.get_practical_plugin_context() parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, root_page=root_page, max_depth=2, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/parent/">Parent</a></li> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> </ul> </div> """, )
def test_cms_plugins_htmlsitemap_root_page_include(self): """ A sitemap page targeted to a root page in "include" mode should display the root page and its descendants. """ _root, parent_page, _page = self.create_page_tree() page = PageFactory(title__title="Sitemap") placeholder = Placeholder.objects.create(slot="maincontent") page.placeholders.add(placeholder) context = self.get_practical_plugin_context({"current_page": page}) parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, root_page=parent_page, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/parent/">Parent</a> <ul> <li><a href="/en/root/parent/page/">Page</a></li> <li><a href="/en/root/parent/sibling/">Sibling</a></li> </ul> </li> </ul> </div> """, )
def test_views_redirect_edx_courses_fallback_search_page(self): """ OpenEdX course urls are redirected to the search page if neither the course page nor the organization page can be found. """ PageFactory( reverse_id="courses", template="search/search.html", title__title="Recherche", title__language="fr", should_publish=True, ) response = self.client.get( "/courses/course-v1:sorbonne+abc+001/about/") self.assertRedirects( response, "/fr/recherche/", status_code=301, target_status_code=200, fetch_redirect_response=True, )
def test_cms_plugins_htmlsitemap_in_navigation(self): """Pages excluded from navigation can be excluded from a sitemap page.""" self.create_page_tree(parent_kwargs={"in_navigation": False}) page = PageFactory(title__title="Sitemap") placeholder = Placeholder.objects.create(slot="maincontent") page.placeholders.add(placeholder) context = self.get_practical_plugin_context() parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, in_navigation=True, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, )
def test_models_organization_create_page_role(self, *_): """ If the CMS_PERMISSIONS settings is True, a page role should be created when saving an organization. Calling the method several times should not duplicate permissions nor update the permissions. """ def get_random_role_dict(): return { "django_permissions": ["cms.change_page"], "organization_page_permissions": { "can_change": random.choice([True, False]), "can_add": random.choice([True, False]), "can_delete": random.choice([True, False]), "can_change_advanced_settings": random.choice([True, False]), "can_publish": random.choice([True, False]), "can_change_permissions": random.choice([True, False]), "can_move_page": random.choice([True, False]), "can_view": False, # can_view = True would make it a view restriction... "grant_on": random.randint(1, 5), }, "organization_folder_permissions": { "can_read": random.choice([True, False]), "can_edit": random.choice([True, False]), "can_add_children": random.choice([True, False]), "type": random.randint(0, 2), }, } page = PageFactory(title__title="My title") organization = OrganizationFactory(extended_object=page) self.assertFalse(page.roles.exists()) role_dict = get_random_role_dict() with mock.patch.dict(defaults.ORGANIZATION_ADMIN_ROLE, role_dict): organization.create_page_role() # Call the method another time with different permissions to check it has no effect with mock.patch.dict(defaults.ORGANIZATION_ADMIN_ROLE, get_random_role_dict()): organization.create_page_role() # A page role should have been created self.assertEqual(page.roles.count(), 1) role = page.roles.get(role="ADMIN") self.assertEqual(role.group.name, "Admin | My title") self.assertEqual(role.group.permissions.count(), 1) self.assertEqual(role.folder.name, "Admin | My title") # All expected permissions should have been assigned to the group: # - Django permissions self.assertEqual(role.group.permissions.first().codename, "change_page") # - DjangoCMS page permissions self.assertEqual( PagePermission.objects.filter(group=role.group).count(), 1) page_permission = PagePermission.objects.get(group=role.group) for key, value in role_dict["organization_page_permissions"].items(): self.assertEqual(getattr(page_permission, key), value) # The Django Filer folder permissions self.assertEqual( FolderPermission.objects.filter(group_id=role.group_id).count(), 1) folder_permission = FolderPermission.objects.get( group_id=role.group_id) for key, value in role_dict["organization_folder_permissions"].items(): self.assertEqual(getattr(folder_permission, key), value)
def test_templatetags_get_related_category_pages_draft(self): """ On a draft page, the "get_related_category_pages" template tag should inject in the context, all categories related to a queryset of pages via a CategoryPlugin. """ page_main = PageFactory(template="richie/single_column.html", should_publish=True) page1, page2 = PageFactory.create_batch( 2, template="richie/single_column.html", should_publish=True) ( category_draft_page1_draft, category_draft_page1_published, category_draft_page2_draft, category_draft_page2_published, ) = [c.extended_object for c in CategoryFactory.create_batch(4)] ( category_published_page1_draft, category_published_page1_published, category_published_page2_draft, category_published_page2_published, ) = [ c.extended_object for c in CategoryFactory.create_batch(4, should_publish=True) ] self._attach_categories(page1, category_draft_page1_draft, category_published_page1_draft) self._attach_categories( page1.get_public_object(), category_draft_page1_published, category_published_page1_published, ) self._attach_categories(page2, category_draft_page2_draft, category_published_page2_draft) self._attach_categories( page2.get_public_object(), category_draft_page2_published, category_published_page2_published, ) request = RequestFactory().get("/") template = ( "{% load cms_tags category_tags %}" "{% get_related_category_pages pages as categories %}" "{% for category in categories %}{{ category.extended_object.id }}{% endfor %}" ) # 1. Test categories present on the draft page # - Linked with one of the pages with self.assertNumQueries(1): output = self.render_template_obj(template, { "current_page": page_main, "pages": [page1] }, request) expected = [category_draft_page1_draft, category_published_page1_draft] self.assertEqual(output, "".join([str(c.id) for c in expected])) # - Linked with either of the 2 pages with self.assertNumQueries(1): output = self.render_template_obj(template, { "current_page": page_main, "pages": [page1, page2] }, request) expected = [ category_draft_page1_draft, category_draft_page2_draft, category_published_page1_draft, category_published_page2_draft, ] self.assertEqual(output, "".join([str(c.id) for c in expected])) # - Linked with a page in a different publication status with self.assertNumQueries(1): output = self.render_template_obj( template, { "current_page": page_main, "pages": [page1.get_public_object()] }, request, ) expected = [category_draft_page1_draft, category_published_page1_draft] self.assertEqual(output, "") # 2. Test categories on the public page current_page = page_main.get_public_object() # - Linked with one of the pages with self.assertNumQueries(1): output = self.render_template_obj( template, { "current_page": current_page, "pages": [page1.get_public_object()] }, request, ) self.assertEqual( output, str(category_published_page1_published.get_public_object().id)) # - Linked with either of the 2 pages with self.assertNumQueries(1): output = self.render_template_obj( template, { "current_page": current_page, "pages": [page1.get_public_object(), page2.get_public_object()], }, request, ) expected = [ category_published_page1_published.get_public_object(), category_published_page2_published.get_public_object(), ] self.assertEqual(output, "".join([str(c.id) for c in expected])) # - Linked with a page in a different publication status with self.assertNumQueries(1): output = self.render_template_obj( template, { "current_page": current_page, "pages": [page1] }, request, ) self.assertEqual(output, "")
def test_models_course_field_duration_default(self): """The duration field should default to None.""" course = Course.objects.create(extended_object=PageFactory()) self.assertIsNone(course.duration)
def test_models_course_field_effort_default(self): """The effort field should default to None.""" course = Course.objects.create(extended_object=PageFactory()) self.assertIsNone(course.effort)
def test_cms_plugins_htmlsitemap_login_required(self): """ Pages requiring login can be excluded from a sitemap page for an anonymous user. """ self.create_page_tree(parent_kwargs={"login_required": True}) page = PageFactory(title__title="Sitemap") placeholder = Placeholder.objects.create(slot="maincontent") page.placeholders.add(placeholder) context = self.get_practical_plugin_context() parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, ) # Check that a logged in user can see the page request = RequestFactory() request.current_page = page request.user = UserFactory() context["request"] = request html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/parent/">Parent</a> <ul> <li><a href="/en/page/">Page</a></li> <li><a href="/en/sibling/">Sibling</a></li> </ul> </li> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, )
def test_cms_plugins_htmlsitemap_public_page(self): """Unpublished or draft pages should be excluded from a public sitemap page.""" root = PageFactory( title__title="Root", title__language="en", should_publish=True ) parent = PageFactory( title__title="Parent", parent=root, title__language="en", should_publish=True, ) PageFactory( title__title="Uncle", parent=root, title__language="en", should_publish=True ) PageFactory( title__title="Page", parent=parent, title__language="en", should_publish=True, ) PageFactory( title__title="Sibling", parent=parent, title__language="en", should_publish=True, ) self.assertTrue(parent.unpublish("en")) root_title = root.title_set.first() root_title.title = "modified title" root_title.save() page = PageFactory( title__title="Sitemap", title__language="en", should_publish=True ) self.assertTrue(page.is_published("en")) placeholder = Placeholder.objects.create(slot="maincontent") page.get_public_object().placeholders.add(placeholder) context = self.get_practical_plugin_context() parent_instance = add_plugin(placeholder, HTMLSitemapPlugin, "en") add_plugin( placeholder, plugin_type="HTMLSitemapPagePlugin", language="en", target=parent_instance, ) html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">Root</a> <ul> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, ) # Check that after publication, the modification is shown indeed root.publish("en") html = context["cms_content_renderer"].render_placeholder( placeholder, context=context, language="en" ) self.assertHTMLEqual( html, """ <div class="sitemap"> <ul> <li><a href="/en/root/">modified title</a> <ul> <li><a href="/en/uncle/">Uncle</a></li> </ul> </li> <li><a href="/en/sitemap/">Sitemap</a></li> </ul> </div> """, )