def test_related_posts_events_archive(self):
        """
        Tests whether related posts from archived events are retrieved.
        Events have no specific categories associated with them so it
        doesn't matter what that value is set to. Here, we save an
        archived event child, and thus we expect that we should retrieve
        both the original event child and the archive event child.
        We also expect that no other categories (newsroom, blog) have
        any posts in them.
        """
        helpers.save_new_page(self.events_child2, self.archive_events_parent)

        self.block_value['relate_posts'] = False
        self.block_value['relate_newsroom'] = False
        self.block_value['relate_events'] = True
        self.block_value['specific_categories'] = [
            'anything', 'can', 'be', 'here'
        ]

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertIn('Events', related_posts)
        self.assertEqual(len(related_posts['Events']), 2)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Newsroom', related_posts)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Events'][1], self.events_child2)
    def test_related_posts_all(self):
        """
        We test whether all the different posts are retrieved. This is
        basically the logical AND of the blog, newsroom, and events
        tests. We expect to retrieve all the blog, newsroom, and events
        posts (two, one, and one of each, respectively).
        """

        self.block_value['relate_posts'] = True
        self.block_value['relate_newsroom'] = True
        self.block_value['relate_events'] = True
        self.block_value['specific_categories'] = [
            'Info for Consumers', 'Policy & Compliance', 'Op-Ed'
        ]

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertIn('Blog', related_posts)
        self.assertIn('Newsroom', related_posts)
        self.assertIn('Events', related_posts)

        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(len(related_posts['Events']), 1)
        self.assertEqual(len(related_posts['Newsroom']), 1)

        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
    def test_related_posts_blog_limit(self):
        """
        Tests whether related posts from the blog from the supplied specific
        categories are retrieved, subject to supplied limit. We expect there
        to be one such post from the blog, that post should be the one with
        the most recent publication date, and no other categories
        (newsroom, events) to have, any posts in them.
        """

        self.block_value['relate_posts'] = True
        self.block_value['relate_newsroom'] = False
        self.block_value['relate_events'] = False
        self.block_value['limit'] = 1
        self.block_value['specific_categories'] = [
            'Info for Consumers', 'Policy & Compliance'
        ]

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 1)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertNotIn('Newsroom', related_posts)
        self.assertNotIn('Events', related_posts)
    def test_related_posts_events(self):
        """
        Tests whether related posts from events are retrieved. Events have
        no specific categories associated with them so it doesn't matter what
        that value is set to. We expect there to be one such post from
        events because we added one child to the events parent. We also expect
        that no other categories (newsroom, blog) have any posts in them.
        """

        self.block_value['relate_posts'] = False
        self.block_value['relate_newsroom'] = False
        self.block_value['relate_events'] = True
        self.block_value['specific_categories'] = [
            'anything', 'can', 'be', 'here'
        ]

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertIn('Events', related_posts)
        self.assertEqual(len(related_posts), 1)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Events'][0].content_type.model,
                         'eventpage')
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Newsroom', related_posts)
    def test_related_posts_blog(self):
        """
        Tests whether related posts from the blog from the supplied specific
        categories are retrieved. We expect there to be two such posts from
        the blog because we added two such posts in the setup. There should
        be no other posts in either of the other categories.
        """

        self.block_value['relate_posts'] = True
        self.block_value['relate_newsroom'] = False
        self.block_value['relate_events'] = False
        self.block_value['specific_categories'] = [
            'Info for Consumers', 'Policy & Compliance'
        ]

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
        self.assertEqual(related_posts['Blog'][0].content_type.model,
                         'blogpage')
        self.assertNotIn('Newsroom', related_posts)
        self.assertNotIn('Events', related_posts)
    def test_related_posts_newsroom(self):
        """
        Tests whether related posts from the newsroom for the supplied specific
        categories are retrieved. We expect there to be one such post from
        the newsroom; we added two newsroom children but one of them should not
        match. We also expect that no other categories (newsroom, events) have
        any posts in them.
        """

        self.block_value['relate_posts'] = False
        self.block_value['relate_newsroom'] = True
        self.block_value['relate_events'] = False
        self.block_value['specific_categories'] = ['Op-Ed']

        related_posts = RelatedPosts.related_posts(
            self.page_with_authors,
            self.block_value
        )

        self.assertIn('Newsroom', related_posts)
        self.assertEqual(len(related_posts['Newsroom']), 1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
        self.assertEqual(
            related_posts['Newsroom'][0].content_type.model, 'newsroompage'
        )
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Events', related_posts)
    def test_no_tags_url_has_no_query_string(self):
        self._create_activity_log_page()

        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)

        self.assertEqual(RelatedPosts.view_more_url(page, self.request),
                         '/activity-log/')
    def test_tags_get_appended_as_query_string(self):
        self._create_activity_log_page()

        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)
        page.tags.add('bar')
        page.tags.add('foo')

        self.assertEqual(RelatedPosts.view_more_url(page, self.request),
                         '/activity-log/?topics=bar&topics=foo')
    def test_related_posts_and_filtering_false(self):
        """
        Tests whether related posts are retrieved if, when the 'and_filtering'
        option is not checked, they match at least one of the tags on the
        calling page.
        """

        self.block_value['relate_posts'] = True
        self.block_value['and_filtering'] = False

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
    def test_related_posts_and_filtering_true(self):
        """
        Tests whether related posts are retrieved if the 'and_filtering' option
        is checked, and that the only posts retrieved match ALL of the tags on
        the calling page.
        """

        self.block_value['relate_posts'] = True
        self.block_value['relate_newsroom'] = True
        self.block_value['relate_events'] = True
        self.block_value['and_filtering'] = True

        related_posts = RelatedPosts.related_posts(self.page_with_authors,
                                                   self.block_value)

        self.assertNotIn('Blog', related_posts)
        self.assertIn('Newsroom', related_posts)
        self.assertEqual(len(related_posts['Newsroom']), 1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
        self.assertNotIn('Events', related_posts)
    def test_related_posts_rendering(self):
        block_value = {
            'and_filtering': False,
            'alternate_view_more_url': None,
            'limit': 3,
            'relate_events': True,
            'relate_newsroom': True,
            'relate_posts': True,
            'specific_categories': [],
        }

        context = {
            'page': self.page_with_authors,
            'request': RequestFactory().get('/'),
        }

        html = RelatedPosts().render(block_value, context=context)

        # Rendered HTML should contain 5 results, because the limit only
        # applies to each type of post, not the total number.
        self.assertEqual(len(re.findall('m-list_item', html)), 5)
    def test_no_activity_log_page_raises_does_not_exist(self):
        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)

        with self.assertRaises(Page.DoesNotExist):
            RelatedPosts.view_more_url(page, self.request)