def test_models_category_get_organizations_public_category_page(self): """ When a category is added on a draft organization, the organization should not be visible on the public category page until the organization is published. """ category = CategoryFactory(should_publish=True) category_page = category.extended_object organization = OrganizationFactory(page_title="my title", should_publish=True) organization_page = organization.extended_object # Add a category to the organization but don't publish the modification placeholder = organization_page.placeholders.get(slot="categories") add_plugin(placeholder, CategoryPlugin, "en", page=category_page) self.assertEqual(list(category.get_organizations()), [organization]) self.assertEqual(list(category.public_extension.get_organizations()), []) # Now publish the modification and check that the organization is displayed # on the public category page organization.extended_object.publish("en") self.assertEqual( list(category.public_extension.get_organizations()), [organization.public_extension], )
def test_models_category_get_organizations_several_languages(self): """ The organizations should not be duplicated if they exist in several languages. """ category = CategoryFactory(should_publish=True) OrganizationFactory( page_title={ "en": "my title", "fr": "mon titre" }, fill_categories=[category], should_publish=True, ) self.assertEqual(Organization.objects.count(), 2) self.assertEqual(category.get_organizations().count(), 1)
def test_models_category_get_organizations_ordering(self): """The related organizations should be sorted by their position in the pages tree.""" category = CategoryFactory(should_publish=True) organization1, organization2, organization3 = OrganizationFactory.create_batch( 3, fill_categories=[category], should_publish=True) self.assertEqual( list(category.get_organizations()), [organization1, organization2, organization3], ) # Move pages in the tree and check that they are returned in the new order organization3.extended_object.move_page( organization1.extended_object.node, position="left") self.assertEqual( list(category.get_organizations()), [organization3, organization1, organization2], ) organization1.extended_object.move_page( organization3.extended_object.node, position="left") self.assertEqual( list(category.get_organizations()), [organization1, organization3, organization2], )
def test_models_category_get_organizations(self): """ It should be possible to retrieve the list of related organizations on the category instance. The number of queries should be minimal. """ category = CategoryFactory(should_publish=True) organizations = OrganizationFactory.create_batch( 2, page_title="my title", fill_categories=[category], should_publish=True) retrieved_organizations = category.get_organizations() with self.assertNumQueries(2): self.assertEqual(set(retrieved_organizations), set(organizations)) with self.assertNumQueries(0): for organization in retrieved_organizations: self.assertEqual( organization.extended_object.prefetched_titles[0].title, "my title")
def test_models_category_get_organizations_descendants(self): """ Related organizations should include the organizations linked to the category's descendants, unless specifically deactivated by the "include_descendants" argument. """ category_page = create_page("Subjects", "courses/cms/category_detail.html", "en", published=True) category = CategoryFactory(extended_object=category_page, should_publish=True) organizations = OrganizationFactory.create_batch( 2, fill_categories=[category], should_publish=True) child_category_page = create_page( "Literature", "courses/cms/category_detail.html", "en", parent=category_page, published=True, ) child_category = CategoryFactory(extended_object=child_category_page, should_publish=True) organizations_child = OrganizationFactory.create_batch( 2, fill_categories=[child_category], should_publish=True) grand_child_category_page = create_page( "Literature", "courses/cms/category_detail.html", "en", parent=child_category_page, published=True, ) grand_child_category = CategoryFactory( extended_object=grand_child_category_page, should_publish=True) organizations_grand_child = OrganizationFactory.create_batch( 2, fill_categories=[grand_child_category], should_publish=True) # Check that each category gets organizations from its descendants # ...unless we pass an argument to deactivate it self.assertEqual( list(category.get_organizations()), organizations + organizations_child + organizations_grand_child, ) self.assertEqual( list(category.get_organizations(include_descendants=False)), organizations) self.assertEqual( list(child_category.get_organizations()), organizations_child + organizations_grand_child, ) self.assertEqual( list(child_category.get_organizations(include_descendants=False)), organizations_child, ) self.assertEqual( list( grand_child_category.get_organizations( include_descendants=False)), organizations_grand_child, ) self.assertEqual(list(grand_child_category.get_organizations()), organizations_grand_child)