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

from django.test import TestCase, Client
from django.contrib.auth.models import User
from django.core.paginator import EmptyPage
from django.utils.translation import activate
from cms.test_utils.testcases import CMSTestCase
Dylann Cordel's avatar
Dylann Cordel committed
from cms.api import create_page, add_plugin, create_title
Olivier Le Brouster's avatar
Olivier Le Brouster committed
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:
                transmodel = cls.translations.related.model
                summary_field = transmodel._meta.get_field('newsbox_summary')
            except:
                self.fail('%s is not properly configured' % cls)
Dylann Cordel's avatar
Dylann Cordel committed
            self.assertIsInstance(summary_field, HTMLField)
Olivier Le Brouster's avatar
Olivier Le Brouster committed


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', slug='home', template='page.html',
                           language='en', published=True, reverse_id='home', in_navigation=True)
Olivier Le Brouster's avatar
Olivier Le Brouster committed

        create_title('fr', 'Accueil', home, slug='accueil')
        home.publish('en')
        home.publish('fr')

Dylann Cordel's avatar
Dylann Cordel committed
        self.page = create_page(title='News page', slug='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
        create_title('fr', 'page Actualités', self.page, slug='page-actualites')
        self.placeholder = self.page.placeholders.get(slot='main')

Dylann Cordel's avatar
Dylann Cordel committed
        self.plugin_en = 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_en.save()

Dylann Cordel's avatar
Dylann Cordel committed
        self.plugin_fr = add_plugin(self.placeholder, 'NewsPlugin', 'fr',  title='Actualités',
                                    numitems=1, with_pager=True, page_link=None, )
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        self.plugin_fr.save()
Dylann Cordel's avatar
Dylann Cordel committed

Olivier Le Brouster's avatar
Olivier Le Brouster committed
        self.page.publish('en')
        self.page.publish('fr')
        self.page.reload()
Dylann Cordel's avatar
Dylann Cordel committed

Olivier Le Brouster's avatar
Olivier Le Brouster committed
        self.newsPlugin = NewsPlugin()
        self.placeholder = self.page.placeholders.get(slot='main')

    def test_01_cmsplugin_context(self):
Dylann Cordel's avatar
Dylann Cordel committed
        expected_results = {'en': 'News 7', 'fr': 'Actualité 7', }
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        for lang in expected_results:
            activate(lang)
            context = self.get_context(
                self.page.get_absolute_url(), page=self.page)
Dylann Cordel's avatar
Dylann Cordel committed
            self.newsPlugin.render(context,
                                   getattr(self, 'plugin_%s' % lang),
                                   self.placeholder)
Olivier Le Brouster's avatar
Olivier Le Brouster committed
            self.assertIn('newsset', context)
            self.assertEqual(context['newsset'][0].pk, 7)
Dylann Cordel's avatar
Dylann Cordel committed
            self.assertEqual(context['newsset'][0].newsbox_title,
                             expected_results[lang])
Olivier Le Brouster's avatar
Olivier Le Brouster committed
            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_wrong_language_404(self):
        c = Client()
        r_en = c.get('/en/news-page/2020/01/01/news-7/')
        r_fr = c.get('/fr/page-actualites/2020/01/01/actualite-7/')
        r_en_and_fr = c.get('/en/news-page/2020/01/01/actualite-7/')
        self.assertEqual(r_en.status_code, 200)
        self.assertEqual(r_fr.status_code, 200)
        self.assertEqual(r_en_and_fr.status_code, 404)

    def test_03_get_absolute_url(self):
        news7 = News.objects.get(pk=7)
Dylann Cordel's avatar
Dylann Cordel committed
        expected_results = {'en': '/en/news-page/2020/01/01/news-7/',
                            'fr': '/fr/page-actualites/2020/01/01/actualite-7/', }
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        for lang in expected_results:
            activate(lang)
            self.assertEqual(news7.get_absolute_url(), expected_results[lang])