Skip to content
Snippets Groups Projects
tests.py 5.47 KiB
Newer Older
Olivier Le Brouster's avatar
Olivier Le Brouster committed
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from copy import copy

from django.test import TestCase, Client
from django.contrib.auth.models import User
from django.core.paginator import EmptyPage
from cms.test_utils.testcases import CMSTestCase
from cms.plugin_rendering import render_plugin
from cms.api import create_page, add_plugin, publish_page
from djangocms_text_ckeditor.fields import HTMLField

Dylann Cordel's avatar
Dylann Cordel committed
from .models import News, NewsComplete, NewsExtended
Olivier Le Brouster's avatar
Olivier Le Brouster committed
from .cms_plugins import NewsPlugin


class NewsboxAbstractModelsTests(TestCase):

Dylann Cordel's avatar
Dylann Cordel committed
    fixtures = ['tests_data.json', ]
Olivier Le Brouster's avatar
Olivier Le Brouster committed

    def test_01_manager_published(self):
        """
        Test if we only get really published news via the manager
        """
        newsClasses = [
            News, NewsComplete, NewsExtended,
        ]
        for cls in newsClasses:
            returned_ids = list(cls.objects.published().values_list(
                'id', flat=True).order_by('id'))
Dylann Cordel's avatar
Dylann Cordel committed
            self.assertEqual((cls.__name__, returned_ids),
                             (cls.__name__, [1, 4, 6, 7, ]))
Olivier Le Brouster's avatar
Olivier Le Brouster committed

    def test_02_summary_as_html_field(self):
        """
        Test if newsbox_summary field is HTMLField
        """
        newsClasses = [
            News, NewsComplete, NewsExtended,
        ]

        for cls in newsClasses:
            try:
                summary_field = cls._meta.get_field('newsbox_summary')
            except:
                self.fail('%s is not properly configured' % cls)
            self.assertIsInstance(summary_field, HTMLField)


class NewsboxPageIntegrationTests(CMSTestCase):

Dylann Cordel's avatar
Dylann Cordel committed
    fixtures = ['tests_data.json', ]
Olivier Le Brouster's avatar
Olivier Le Brouster committed

    def setUp(self):
        self.super_user = User(username="test", is_staff=True, is_active=True, is_superuser=True)
        self.super_user.set_password("test")
        self.super_user.save()

Dylann Cordel's avatar
Dylann Cordel committed
        home = create_page(title='Home', template='page.html',  language='en', published=True,
                           reverse_id='home', in_navigation=True)

        self.page = create_page(title='News page', template='page.html', parent=home,
                                language='en', published=True, apphook='NewsApphook',
                                reverse_id='newspage', in_navigation=True)
Olivier Le Brouster's avatar
Olivier Le Brouster committed

        self.placeholder = self.page.placeholders.get(slot='main')
Dylann Cordel's avatar
Dylann Cordel committed
        self.plugin = add_plugin(self.placeholder, 'NewsPlugin', 'en',
                                 title='News', numitems=1, with_pager=True, page_link=None,)
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        self.plugin.save()
        self.newsPlugin = NewsPlugin()
        self.page = publish_page(self.page, self.super_user, 'en')
        self.placeholder = self.page.placeholders.get(slot='main')

    def test_01_cmsplugin_context(self):
        context = self.get_context(self.page.get_absolute_url(), page=self.page)
        self.newsPlugin.render(context, self.plugin, self.placeholder)
        self.assertIn('newsset', context)
        self.assertEqual(context['newsset'][0].pk, 7)
        self.assertEqual(context['newsset'].number, 1)
        self.assertEqual(context['newsset'].paginator.num_pages, 4)
        self.assertFalse(context['newsset'].has_previous())
        self.assertTrue(context['newsset'].has_next())
        self.assertRaises(EmptyPage, context['newsset'].previous_page_number)
        self.assertEqual(context['newsset'].next_page_number(), 2)

    def test_02_cmsplugin_disable_pager(self):
        context = self.get_context(self.page.get_absolute_url(), page=self.page)
        plugin = copy(self.plugin)
        plugin.with_pager = False
        self.newsPlugin.render(context, plugin, self.placeholder)
        self.assertIn('newsset', context)
        with self.assertRaises(AttributeError):
            getattr(context['newsset'], 'has_previous')
        with self.assertRaises(AttributeError):
            getattr(context['newsset'], 'paginator')

    def test_03_cmsplugin_numitems(self):
        context = self.get_context(self.page.get_absolute_url(), page=self.page)
        plugin = copy(self.plugin)
        plugin.numitems = 2
        self.newsPlugin.render(context, plugin, self.placeholder)
        self.assertIn('newsset', context)
        self.assertEqual(context['newsset'].paginator.num_pages, 2)

    def test_04_cmsplugin_html(self):
        context = self.get_context(self.page.get_absolute_url(), page=self.page)
        self.newsPlugin.render(context, self.plugin, self.placeholder)
        html = render_plugin(
Dylann Cordel's avatar
Dylann Cordel committed
            context,
            self.newsPlugin,
            self.placeholder,
Olivier Le Brouster's avatar
Olivier Le Brouster committed
            self.newsPlugin.render_template)
        self.assertIn('<a href="/news-page/2020/01/01/news-7/">News 7</a>', html)
        self.assertIn('<a class="next" href="?page=2">Next news</a>', html)

    def test_05_app_view_detail_200(self):
        """
        Test if generic day views return right informations.
        """
        c = Client()
        r = c.get('/news-page/2005/07/15/news-6/')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.context['news'].pk, 6)

    def test_06_app_view_detail_404(self):
        """
Dylann Cordel's avatar
Dylann Cordel committed
        Test if generic day views return a 404 with an URL with a date without
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        any news.
        """
        c = Client()
        r = c.get('/news-page/1985/07/02/my-birthday/')
        self.assertEqual(r.status_code, 404)

    def test_07_app_view_detail_404_not_published(self):
        """
        Test if generic day views return a 404 with an URL with a not published
        news
        """
        c = Client()
        r = c.get('/news-page/2014/02/22/news-2/')
        self.assertEqual(r.status_code, 404)