def test_models_category_get_blogposts_ordering(self): """The related blogposts should be sorted by their position in the pages tree.""" category = CategoryFactory(should_publish=True) blogpost1, blogpost2, blogpost3 = BlogPostFactory.create_batch( 3, fill_categories=[category], should_publish=True) self.assertEqual(list(category.get_blogposts()), [blogpost1, blogpost2, blogpost3]) # Move pages in the tree and check that they are returned in the new order blogpost3.extended_object.move_page(blogpost1.extended_object.node, position="left") self.assertEqual(list(category.get_blogposts()), [blogpost3, blogpost1, blogpost2]) blogpost1.extended_object.move_page(blogpost3.extended_object.node, position="left") self.assertEqual(list(category.get_blogposts()), [blogpost1, blogpost3, blogpost2])
def test_models_category_get_blogposts_several_languages(self): """ The blogposts should not be duplicated if they exist in several languages. """ category = CategoryFactory(should_publish=True) BlogPostFactory( page_title={"en": "my title", "fr": "mon titre"}, fill_categories=[category], should_publish=True, ) self.assertEqual(BlogPost.objects.count(), 2) self.assertEqual(category.get_blogposts().count(), 1)
def test_models_category_get_blogposts(self): """ It should be possible to retrieve the list of related blogposts on the category instance. The number of queries should be minimal. """ category = CategoryFactory(should_publish=True) blogposts = BlogPostFactory.create_batch( 2, page_title="my title", fill_categories=[category], should_publish=True ) retrieved_blogposts = category.get_blogposts() with self.assertNumQueries(2): self.assertEqual(set(retrieved_blogposts), set(blogposts)) with self.assertNumQueries(0): for blogpost in retrieved_blogposts: self.assertEqual( blogpost.extended_object.prefetched_titles[0].title, "my title" )
def test_models_category_get_blogposts_public_category_page(self): """ When a category is added on a draft blog post, the blog post should not be visible on the public category page until the blog post is published. """ category = CategoryFactory(should_publish=True) category_page = category.extended_object blog_post = BlogPostFactory(page_title="my title", should_publish=True) blog_post_page = blog_post.extended_object # Add a category to the blog post but don't publish the modification placeholder = blog_post_page.placeholders.get(slot="categories") add_plugin(placeholder, CategoryPlugin, "en", page=category_page) self.assertEqual(list(category.get_blogposts()), [blog_post]) self.assertEqual(list(category.public_extension.get_blogposts()), []) # Now publish the modification and check that the blog post is displayed # on the public category page blog_post.extended_object.publish("en") self.assertEqual( list(category.public_extension.get_blogposts()), [blog_post.public_extension], )
def test_models_category_get_blogposts_descendants(self): """ Related blogposts should include the blogposts 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) blogposts = BlogPostFactory.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) blogposts_child = BlogPostFactory.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) blogposts_grand_child = BlogPostFactory.create_batch( 2, fill_categories=[grand_child_category], should_publish=True) # Check that each category gets blogposts from its descendants # ...unless we pass an argument to deactivate it self.assertEqual( list(category.get_blogposts()), blogposts + blogposts_child + blogposts_grand_child, ) self.assertEqual( list(category.get_blogposts(include_descendants=False)), blogposts) self.assertEqual( list(child_category.get_blogposts()), blogposts_child + blogposts_grand_child, ) self.assertEqual( list(child_category.get_blogposts(include_descendants=False)), blogposts_child, ) self.assertEqual( list( grand_child_category.get_blogposts(include_descendants=False)), blogposts_grand_child, ) self.assertEqual(list(grand_child_category.get_blogposts()), blogposts_grand_child)