from cms.test_utils.testcases import CMSTestCase


class PluginTest(CMSTestCase):
    pass

from bs4 import BeautifulSoup
from cms.test_utils.testcases import CMSTestCase
from cms.api import create_page, add_plugin
from django.template import RequestContext


class PluginTest(CMSTestCase):
    def _create_page(self, title, **kwargs):
        data = {
            'title': title,
            'template': 'dummy.html',
            'language': 'en',
            'published': True,
        }
        data.update(kwargs)
        return create_page(**data)

    def _create_blog(self, title, limit=10, **kwargs):
        page = self._create_page(title, **kwargs)
        placeholder = page.placeholders.create(slot='body')
        add_plugin(placeholder, 'ArticlesPlugin', 'en', limit=limit)
        return page, placeholder

    def test_articles(self):
        blog1, body = self._create_blog('Blog 1')
        self._create_page('Article 1', parent=blog1)
        self._create_page('Article 2', parent=blog1)
        blog1 = blog1.reload()
        self.assertEqual(len(blog1.get_children()), 2)

        blog2, _ = self._create_blog('Blog 2')
        self._create_page('Article 3', parent=blog2)
        self.assertEqual(len(blog2.reload().get_children()), 1)

        request = self.get_request(language='en', page=blog1)
        request_context = RequestContext(request)
        html = body.render(request_context, None)
        soup = BeautifulSoup(html, 'html.parser')
        titles = {h.string for h in soup.find_all('h2')}
        self.assertEqual(titles, {'Article 1', 'Article 2'})

test articles list