2014-06-10 06:19:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-09-12 22:46:05 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
2015-09-06 11:40:20 +00:00
|
|
|
import os.path
|
2014-06-10 06:19:34 +00:00
|
|
|
import re
|
2015-07-18 15:33:36 +00:00
|
|
|
|
2014-06-10 06:19:34 +00:00
|
|
|
from cms.api import add_plugin
|
|
|
|
from django.core.urlresolvers import reverse
|
2014-08-13 21:59:08 +00:00
|
|
|
from django.utils.timezone import now
|
2015-09-26 16:01:22 +00:00
|
|
|
from djangocms_helper.utils import get_user_model
|
2014-06-10 06:19:34 +00:00
|
|
|
from taggit.models import Tag
|
|
|
|
|
2015-09-12 22:33:13 +00:00
|
|
|
from djangocms_blog.models import BlogCategory
|
|
|
|
|
2014-06-10 06:19:34 +00:00
|
|
|
from . import BaseTest
|
|
|
|
|
|
|
|
|
|
|
|
class PluginTest(BaseTest):
|
|
|
|
|
|
|
|
def test_plugin_latest(self):
|
2015-09-19 23:11:14 +00:00
|
|
|
pages = self.get_pages()
|
|
|
|
posts = self.get_posts()
|
|
|
|
posts[0].tags.add('tag 1')
|
|
|
|
posts[0].publish = True
|
|
|
|
posts[0].save()
|
|
|
|
ph = pages[0].placeholders.get(slot='content')
|
|
|
|
|
|
|
|
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
|
2014-06-10 06:19:34 +00:00
|
|
|
tag = Tag.objects.get(slug='tag-1')
|
|
|
|
plugin.tags.add(tag)
|
2015-09-12 19:51:40 +00:00
|
|
|
|
2015-09-19 23:11:14 +00:00
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin, edit=True)
|
2015-09-12 19:51:40 +00:00
|
|
|
rendered = plugin.render_plugin(context, ph)
|
2015-09-15 06:17:39 +00:00
|
|
|
try:
|
|
|
|
self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-1') > -1)
|
|
|
|
except AssertionError:
|
|
|
|
self.assertTrue(rendered.find('cms-plugin-djangocms_blog-post-abstract-1') > -1)
|
2014-06-10 06:19:34 +00:00
|
|
|
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
|
|
|
|
self.assertTrue(rendered.find('<p>first line</p>') > -1)
|
|
|
|
self.assertTrue(rendered.find('<article id="post-first-post"') > -1)
|
2015-09-19 23:11:14 +00:00
|
|
|
self.assertTrue(rendered.find(posts[0].get_absolute_url()) > -1)
|
2014-06-10 06:19:34 +00:00
|
|
|
|
2015-09-19 23:11:14 +00:00
|
|
|
category_2 = BlogCategory.objects.create(name='category 2', app_config=self.app_config_1)
|
2015-04-20 00:42:34 +00:00
|
|
|
category_2.set_current_language('it', initialize=True)
|
2015-09-19 23:11:14 +00:00
|
|
|
category_2.name = 'categoria 2'
|
2015-04-20 00:42:34 +00:00
|
|
|
category_2.save()
|
|
|
|
category_2.set_current_language('en')
|
2015-09-19 23:11:14 +00:00
|
|
|
posts[1].categories.add(category_2)
|
|
|
|
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
|
2015-04-20 00:42:34 +00:00
|
|
|
plugin.categories.add(category_2)
|
2015-09-12 19:51:40 +00:00
|
|
|
|
2015-09-19 23:11:14 +00:00
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin, edit=True)
|
2015-09-12 19:51:40 +00:00
|
|
|
rendered = plugin.render_plugin(context, ph)
|
2015-09-15 06:17:39 +00:00
|
|
|
try:
|
|
|
|
self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-2') > -1)
|
|
|
|
except AssertionError:
|
|
|
|
self.assertTrue(rendered.find('cms-plugin-djangocms_blog-post-abstract-2') > -1)
|
2015-04-20 00:42:34 +00:00
|
|
|
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-category', kwargs={'category': category_2.slug})) > -1)
|
|
|
|
self.assertTrue(rendered.find('<p>second post first line</p>') > -1)
|
|
|
|
self.assertTrue(rendered.find('<article id="post-second-post"') > -1)
|
2015-09-19 23:11:14 +00:00
|
|
|
self.assertTrue(rendered.find(posts[1].get_absolute_url()) > -1)
|
2015-04-20 00:42:34 +00:00
|
|
|
|
2014-06-10 06:19:34 +00:00
|
|
|
def test_plugin_authors(self):
|
2015-09-19 23:11:14 +00:00
|
|
|
pages = self.get_pages()
|
|
|
|
posts = self.get_posts()
|
|
|
|
posts[0].publish = True
|
|
|
|
posts[0].save()
|
|
|
|
posts[1].publish = True
|
|
|
|
posts[1].save()
|
|
|
|
ph = pages[0].placeholders.get(slot='content')
|
|
|
|
plugin = add_plugin(ph, 'BlogAuthorPostsPlugin', language='en', app_config=self.app_config_1)
|
2015-09-12 19:51:40 +00:00
|
|
|
|
2015-09-19 23:11:14 +00:00
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin, edit=True)
|
2015-09-12 19:51:40 +00:00
|
|
|
rendered = plugin.render_plugin(context, ph)
|
2015-09-26 16:01:22 +00:00
|
|
|
self.assertTrue(rendered.find('No article found') > -1)
|
2014-06-10 06:19:34 +00:00
|
|
|
|
|
|
|
def test_plugin_tags(self):
|
2015-09-19 23:11:14 +00:00
|
|
|
pages = self.get_pages()
|
|
|
|
posts = self.get_posts()
|
|
|
|
posts[0].tags.add('tag 1', 'tag 2', 'test tag')
|
|
|
|
posts[0].publish = True
|
|
|
|
posts[0].save()
|
|
|
|
posts[1].tags.add('test tag', 'another tag')
|
|
|
|
posts[1].publish = True
|
|
|
|
posts[1].save()
|
|
|
|
ph = pages[0].placeholders.get(slot='content')
|
|
|
|
plugin = add_plugin(ph, 'BlogTagsPlugin', language='en', app_config=self.app_config_1)
|
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin, edit=True)
|
2015-09-12 19:51:40 +00:00
|
|
|
rendered = plugin.render_plugin(context, ph)
|
2014-06-10 06:19:34 +00:00
|
|
|
for tag in Tag.objects.all():
|
|
|
|
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
|
|
|
|
if tag.slug == 'test-tag':
|
|
|
|
rf = '\s+%s\s+<span>\(\s+%s articles' % (tag.name, 2)
|
|
|
|
else:
|
|
|
|
rf = '\s+%s\s+<span>\(\s+%s article' % (tag.name, 1)
|
|
|
|
rx = re.compile(rf)
|
2014-06-11 16:48:58 +00:00
|
|
|
self.assertEqual(len(rx.findall(rendered)), 1)
|
|
|
|
|
|
|
|
def test_blog_category_plugin(self):
|
2015-09-19 23:11:14 +00:00
|
|
|
pages = self.get_pages()
|
|
|
|
posts = self.get_posts()
|
|
|
|
posts[0].publish = True
|
|
|
|
posts[0].save()
|
|
|
|
posts[1].publish = True
|
|
|
|
posts[1].save()
|
|
|
|
ph = pages[0].placeholders.get(slot='content')
|
|
|
|
plugin = add_plugin(ph, 'BlogCategoryPlugin', language='en', app_config=self.app_config_1)
|
2014-06-11 16:48:58 +00:00
|
|
|
plugin_class = plugin.get_plugin_class_instance()
|
2015-09-19 23:11:14 +00:00
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin, edit=True)
|
2015-09-12 19:51:40 +00:00
|
|
|
context = plugin_class.render(context, plugin, ph)
|
2014-06-11 16:48:58 +00:00
|
|
|
self.assertTrue(context['categories'])
|
|
|
|
self.assertEqual(list(context['categories']), [self.category_1])
|
|
|
|
|
|
|
|
def test_blog_archive_plugin(self):
|
2015-09-19 23:11:14 +00:00
|
|
|
pages = self.get_pages()
|
|
|
|
posts = self.get_posts()
|
|
|
|
posts[0].publish = True
|
|
|
|
posts[0].save()
|
|
|
|
posts[1].publish = True
|
|
|
|
posts[1].save()
|
|
|
|
ph = pages[0].placeholders.get(slot='content')
|
|
|
|
plugin = add_plugin(ph, 'BlogArchivePlugin', language='en', app_config=self.app_config_1)
|
2014-06-11 16:48:58 +00:00
|
|
|
plugin_class = plugin.get_plugin_class_instance()
|
2015-09-12 19:51:40 +00:00
|
|
|
|
2015-09-19 23:11:14 +00:00
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin, edit=True)
|
2015-09-12 19:51:40 +00:00
|
|
|
context = plugin_class.render(context, plugin, ph)
|
2014-08-13 21:59:08 +00:00
|
|
|
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
|
|
|
self.assertEqual(context['dates'][0]['count'], 2)
|
2014-06-11 16:48:58 +00:00
|
|
|
|
2015-09-19 23:11:14 +00:00
|
|
|
posts[1].publish = False
|
|
|
|
posts[1].save()
|
2015-09-12 19:51:40 +00:00
|
|
|
context = plugin_class.render(context, plugin, ph)
|
2014-08-13 21:59:08 +00:00
|
|
|
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
|
|
|
self.assertEqual(context['dates'][0]['count'], 1)
|
2015-09-06 11:40:20 +00:00
|
|
|
|
|
|
|
def test_templates(self):
|
|
|
|
posts = self.get_posts()
|
|
|
|
pages = self.get_pages()
|
|
|
|
|
|
|
|
ph = pages[0].placeholders.get(slot='content')
|
|
|
|
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
|
|
|
|
|
|
|
|
context = self.get_plugin_context(pages[0], 'en', plugin)
|
|
|
|
plugin_class = plugin.get_plugin_class_instance()
|
|
|
|
self.assertEqual(plugin_class.get_render_template(context, plugin, ph), os.path.join('djangocms_blog', plugin_class.base_render_template))
|
|
|
|
|
|
|
|
self.app_config_1.app_data.config.template_prefix = 'whatever'
|
|
|
|
self.app_config_1.save()
|
|
|
|
self.assertEqual(plugin_class.get_render_template(context, plugin, ph), os.path.join('whatever', plugin_class.base_render_template))
|
|
|
|
self.app_config_1.app_data.config.template_prefix = ''
|
|
|
|
self.app_config_1.save()
|