def test_forum_search_paginated_links(self): """ Check pagination has well formed links """ user = UserFactory() post = PostFactory(text="a5g3g6k75") PostFactory.create_batch(100, text="a5g3g6k75", topic=post.topic) assign_perm("can_read_forum", user, post.topic.forum) # Index 101 posts in Elasticsearch call_command("rebuild_index", interactive=False) self.client.force_login(user) response = self.client.get("/forum/search/?q=a5g3") self.assertContains(response, "Your search has returned <b>101</b> results", html=True) self.assertContains(response, post, html=True) # 20 results per page and 2 panels of pagination # check all li tags are well closed self.assertContains(response, '<li class="page-item">', 12) self.assertContains(response, '<li class="page-item active">', 2) self.assertContains(response, '<li class="page-item disabled">', 4) # check all links are well formed self.assertContains( response, '<li class="page-item active">' '<a href="?q=a5g3&page=1" class="page-link">1</a>' "</li>", ) for page in range(2, 6): self.assertContains(response, f"?q=a5g3&page={page}") self.assertContains( response, f'<li class="page-item"><a href="?q=a5g3&page={page}" ' f'class="page-link">{page}</a></li>', ) self.assertNotContains(response, "?q=a5g3&page=7")
def test_forum_search_show_direct_post_with_right_pagination(self, ): """ Check that the link of the results targets directly the post in the topic. Using this link, the user is directly at the right page of the topic the post is related to. """ user = UserFactory() post = PostFactory(text="azatofind") PostFactory.create_batch(15, topic=post.topic) # Create a specific post between two batchs post_second_page = PostFactory(text="azatofind", topic=post.topic) PostFactory.create_batch(15, topic=post.topic) post_last_page = PostFactory(text="azatofind", topic=post.topic) assign_perm("can_read_forum", user, post.topic.forum) # Index posts in Elasticsearch call_command("rebuild_index", interactive=False) self.client.force_login(user) response = self.client.get("/forum/search/?q=azatofind") self.assertEqual(response.status_code, 200) self.assertContains(response, "Your search has returned <b>3</b> results", html=True) url_topic = ( f"/forum/forum/{post.topic.forum.slug}-" f"{post.topic.forum.id}/topic/{post.topic.slug}-{post.topic.id}/") direct_link_initial_post = f"{url_topic}?post={ post.pk }#{ post.pk }" direct_link_post_second_page = ( f"{url_topic}?post={ post_second_page.pk }#{ post_second_page.pk }" ) direct_link_post_last_page = ( f"{url_topic}?post={ post_last_page.pk }#{ post_last_page.pk }") # links of three posts are present self.assertContains(response, direct_link_initial_post) self.assertContains(response, direct_link_post_second_page) self.assertContains(response, direct_link_post_last_page) # confirm the last page of the topic is page 3 response = self.client.get(url_topic) self.assertContains(response, '<a href="?page=3" class="page-link">3</a>') self.assertNotContains(response, '<a href="?page=4" class="page-link">4</a>') response = self.client.get(direct_link_initial_post) # first post is at page 1 self.assertContains( response, '<li class="page-item active">' '<a href="?page=1" class="page-link">1</a>' "</li>", ) # second post is at page 2 directly response = self.client.get(direct_link_post_second_page) self.assertContains( response, '<li class="page-item active">' '<a href="?page=2" class="page-link">2</a>' "</li>", ) # third post is at last page directly response = self.client.get(direct_link_post_last_page) # third post is at page 3 self.assertContains( response, '<li class="page-item active">' '<a href="?page=3" class="page-link">3</a>' "</li>", )