def test_models_person_get_courses_public_person_page(self): """ When a person is added on a draft course, the course should not be visible on the public person page until the course is published. """ person = PersonFactory(should_publish=True) person_page = person.extended_object course = CourseFactory(page_title="my title", should_publish=True) course_page = course.extended_object # Add a person to the course but don't publish the modification placeholder = course_page.placeholders.get(slot="course_team") add_plugin(placeholder, PersonPlugin, "en", page=person_page) self.assertEqual(list(person.get_courses()), [course]) self.assertEqual(list(person.public_extension.get_courses()), []) # Now publish the modification and check that the course is displayed # on the public person page course.extended_object.publish("en") self.assertEqual(list(person.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(person.get_courses()), [course]) self.assertEqual(list(person.public_extension.get_courses()), [])
def test_models_person_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) person = PersonFactory(should_publish=True) course = CourseFactory(page_parent=root_page, fill_team=[person], should_publish=True) CourseFactory(page_parent=course.extended_object, fill_team=[person], should_publish=True) self.assertEqual(Course.objects.count(), 4) self.assertEqual(person.get_courses().count(), 1) self.assertEqual(person.public_extension.get_courses().count(), 1)
def test_models_person_get_courses_several_languages(self): """ The courses should not be duplicated if they exist in several languages. """ person = PersonFactory(should_publish=True) CourseFactory( page_title={ "en": "my title", "fr": "mon titre" }, fill_team=[person], should_publish=True, ) self.assertEqual(Course.objects.count(), 2) self.assertEqual(person.get_courses().count(), 1)
def test_models_person_get_courses(self): """ It should be possible to retrieve the list of related courses on the person instance. The number of queries should be minimal. """ person = PersonFactory(should_publish=True) courses = CourseFactory.create_batch(3, page_title="my title", fill_team=[person], should_publish=True) retrieved_courses = person.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")
def test_templates_person_detail_related_max_courses(self, _mock_page_url): """ Make sure the person detail page does not display too many courses, even when a large number are related to the current person, as this can cause the page to load very slowly and is not a great experience for the user anyway. """ # Create our dummy person and the 3 courses we'll attach to it person = PersonFactory(should_publish=True) courses = CourseFactory.create_batch(3, fill_team=[person], should_publish=True) # Link the 3 courses with our person through the relevant placeholder for course in courses: add_plugin( course.extended_object.placeholders.get(slot="course_team"), PersonPlugin, "en", page=person.extended_object, ) # Make sure we do have 3 courses on the person self.assertEqual(person.get_courses().count(), 3) # Only the first two are rendered in the template response = self.client.get(person.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/?persons={person.public_extension.extended_object_id}"', ) self.assertContains( response, f"See all courses related to {person.extended_object.get_title():s}", )