Newer
Older
# -*- 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.api import create_page, add_plugin, publish_page
from cms.models.fields import PlaceholderField
from djangocms_text_ckeditor.fields import HTMLField
from utils import create_tests_data, delete_tests_data
try:
from cms.plugin_rendering import render_plugin as render_plugin_base
def render_plugin(context, plugin, placeholder):
instance, plugin = plugin.get_plugin_instance()
template = plugin.get_render_template(context, instance, placeholder)
return render_plugin_base(context, plugin, placeholder, template)
except ImportError:
from django.template import RequestContext
from cms.plugin_rendering import ContentRenderer
def render_plugin(context, plugin, placeholder):
renderer = ContentRenderer(context['request'])
context = RequestContext(context['request'], context)
return renderer.render_plugin(plugin, context, placeholder)
class NewsboxCMSModelsTests(TestCase):
def setUp(self):
create_tests_data('myapp_cms')
def tearDown(self):
delete_tests_data('myapp_cms')
# fixtures = ['tests_data.json', ]
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'))
self.assertEqual((cls.__name__, returned_ids),
(cls.__name__, [1, 4, 6, 7, ]))
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)
def test_03_body_as_placeholder_field(self):
"""
Test if newsbox_summary field is PlaceholderField
"""
newsClasses = [News, NewsComplete, NewsExtended, ]
for cls in newsClasses:
try:
body_field = cls._meta.get_field('newsbox_body')
except:
self.fail('%s is not properly configured' % cls)
self.assertIsInstance(body_field, PlaceholderField)
class NewsboxPageIntegrationTests(CMSTestCase):
# fixtures = ['tests_data.json', ]
create_tests_data('myapp_cms')
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()
home = create_page(title='Home', template='page.html', language='en', published=True,
reverse_id='home', in_navigation=True)
self.hooked_news_page = create_page(title='News page', template='page.html', parent=home,
reverse_id='hooked_newspage', in_navigation=True)
self.plugin_news_page = create_page(title='Plugin News page', template='page.html', parent=home,
language='en', published=True,
reverse_id='plugin_newspage', in_navigation=True)
self.placeholder = self.plugin_news_page.placeholders.get(slot='main')
self.plugin = add_plugin(self.placeholder, 'NewsPlugin', 'en',
title='News', numitems=1, with_pager=True, page_link=None,)
self.plugin, self.newsPlugin = self.plugin.get_plugin_instance()
self.hooked_news_page = publish_page(self.hooked_news_page, self.super_user, 'en')
self.placeholder = self.plugin_news_page.placeholders.get(slot='main')
def tearDown(self):
delete_tests_data('myapp_cms')
context = self.get_context(self.plugin_news_page.get_absolute_url(),
page=self.plugin_news_page)
self.newsPlugin.render(context, self.plugin, self.placeholder)
self.assertIn('object_list', context)
self.assertEqual(context['object_list'][0].pk, 7)
self.assertEqual(context['page_obj'].number, 1)
self.assertEqual(context['paginator'].num_pages, 4)
self.assertFalse(context['page_obj'].has_previous())
self.assertTrue(context['page_obj'].has_next())
with self.assertRaises(EmptyPage):
context['page_obj'].previous_page_number()
self.assertEqual(context['page_obj'].next_page_number(), 2)
context = self.get_context(self.plugin_news_page.get_absolute_url(),
page=self.plugin_news_page)
self.plugin.with_pager = False
self.plugin.save()
self.newsPlugin.render(context, self.plugin, self.placeholder)
self.assertIn('object_list', context)
getattr(context['object_list'], 'has_previous')
getattr(context['object_list'], 'paginator')
context = self.get_context(self.plugin_news_page.get_absolute_url(),
page=self.plugin_news_page)
self.plugin.numitems = 2
self.plugin.save()
self.newsPlugin.render(context, self.plugin, self.placeholder)
self.assertIn('object_list', context)
self.assertEqual(context['paginator'].num_pages, 2)
context = self.get_context(self.plugin_news_page.get_absolute_url(),
page=self.plugin_news_page)
self.newsPlugin.render(context, self.plugin, self.placeholder)
html = render_plugin(context, self.plugin, self.placeholder)
self.assertIn('<a href="/news-page/2014/03/07/news-7/">', 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['object'].pk, 6)
Test if generic day views return a 404 with an URL with a date without
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)