def test_models_organization_get_courses_public_organization_page(self): """ When a organization is added on a draft course, the course should not be visible on the public organization page until the course is published. """ organization = OrganizationFactory(should_publish=True) organization_page = organization.extended_object course = CourseFactory(page_title="my title", should_publish=True) course_page = course.extended_object # Add a organization to the course but don't publish the modification placeholder = course_page.placeholders.get(slot="course_organizations") add_plugin(placeholder, OrganizationPlugin, "en", page=organization_page) self.assertEqual(list(organization.get_courses()), [course]) self.assertEqual(list(organization.public_extension.get_courses()), []) # Now publish the modification and check that the course is displayed # on the public organization page course.extended_object.publish("en") self.assertEqual(list(organization.public_extension.get_courses()), [course.public_extension]) # If the course is unpublished, it should not be displayed on the public # page anymore course_page.unpublish("en") self.assertEqual(list(organization.get_courses()), [course]) self.assertEqual(list(organization.public_extension.get_courses()), [])
def test_models_organization_get_courses_snapshots(self): """ Snapshot courses should be excluded from the list of courses returned. The new filter query we added to exclude snapshots should not create duplicates. Indeed, we had to add a "distinct" clause to the query so this test enforces it. """ # We create a root page because it was responsible for duplicate results when the # distinct clause is not applied. # This is because of the clause "extended_object__node__parent__cms_pages__..." # which is there to exclude snapshots but also acts on the main course page and # checks its parent (so the root page) and the duplicate comes from the fact that # the parent has a draft and a public page... so "cms_pages" has a cardinality of 2 root_page = create_i18n_page("my title", published=True) organization = OrganizationFactory(should_publish=True) course = CourseFactory( page_parent=root_page, fill_organizations=[organization], should_publish=True, ) CourseFactory( page_parent=course.extended_object, fill_organizations=[organization], should_publish=True, ) self.assertEqual(Course.objects.count(), 4) self.assertEqual(organization.get_courses().count(), 1) self.assertEqual(organization.public_extension.get_courses().count(), 1)
def test_models_organization_get_courses_several_languages(self): """ The courses should not be duplicated if they exist in several languages. """ organization = OrganizationFactory(should_publish=True) CourseFactory( page_title={ "en": "my title", "fr": "mon titre" }, fill_organizations=[organization], should_publish=True, ) self.assertEqual(Course.objects.count(), 2) self.assertEqual(organization.get_courses().count(), 1)
def test_templates_organization_detail_cms_published_content_max_courses( self, _mock_page_url): """ Make sure the organization detail page does not display too many courses, even when a large number are related to the current organization, as this can cause the page to load very slowly and is not a great experience for the user anyway. """ # Create our dummy organization and the 3 courses we'll attach to it organization = OrganizationFactory(should_publish=True) courses = CourseFactory.create_batch(3, fill_organizations=[organization], should_publish=True) # Link the 3 courses with our organization through the relevant placeholder for course in courses: add_plugin( course.extended_object.placeholders.get( slot="course_organizations"), OrganizationPlugin, "en", page=organization.extended_object, ) # Make sure we do have 3 courses on the organization self.assertEqual(organization.get_courses().count(), 3) # Only the first two are rendered in the template response = self.client.get( organization.extended_object.get_absolute_url()) self.assertContains(response, courses[0].extended_object.get_title()) self.assertContains(response, courses[1].extended_object.get_title()) self.assertNotContains(response, courses[2].extended_object.get_title()) # There is a link to view more related courses directly in the Search view self.assertContains( response, f'href="/the/courses/?organizations={organization.get_es_id():s}"') self.assertContains( response, f"See all courses related to {organization.extended_object.get_title():s}", )
def test_models_organization_get_courses(self): """ It should be possible to retrieve the list of related courses on the organization instance. The number of queries should be minimal. """ organization = OrganizationFactory(should_publish=True) courses = CourseFactory.create_batch( 3, page_title="my title", fill_organizations=[organization], should_publish=True, ) retrieved_courses = organization.get_courses() with self.assertNumQueries(2): self.assertEqual(set(retrieved_courses), set(courses)) with self.assertNumQueries(0): for course in retrieved_courses: self.assertEqual( course.extended_object.prefetched_titles[0].title, "my title")