djangocms_blog/tests/test_menu.py

228 lines
10 KiB
Python
Raw Permalink Normal View History

2015-07-20 12:31:59 +00:00
# -*- coding: utf-8 -*-
2015-09-12 22:46:05 +00:00
from __future__ import absolute_import, print_function, unicode_literals
2015-09-19 23:11:14 +00:00
from aldryn_apphooks_config.utils import get_app_instance
2016-09-11 18:58:15 +00:00
from django.utils.translation import activate
2015-07-20 12:31:59 +00:00
from menus.menu_pool import menu_pool
2015-09-07 07:18:17 +00:00
from parler.utils.context import smart_override, switch_language
2015-07-21 04:59:23 +00:00
from djangocms_blog.models import BlogCategory
from djangocms_blog.settings import (
MENU_TYPE_CATEGORIES, MENU_TYPE_COMPLETE, MENU_TYPE_NONE, MENU_TYPE_POSTS,
)
2015-10-17 14:23:15 +00:00
from djangocms_blog.views import CategoryEntriesView, PostDetailView
2015-09-12 22:33:13 +00:00
2015-10-18 11:16:43 +00:00
from .base import BaseTest
2015-07-20 12:31:59 +00:00
class MenuTest(BaseTest):
2015-09-19 23:11:14 +00:00
cats = []
2015-07-20 12:31:59 +00:00
def setUp(self):
super(MenuTest, self).setUp()
self.cats = [self.category_1]
2016-09-11 18:58:15 +00:00
self._reset_menus()
2015-09-19 23:11:14 +00:00
for i, lang_data in enumerate(self._categories_data):
cat = self._get_category(lang_data['en'])
if 'it' in lang_data:
cat = self._get_category(lang_data['it'], cat, 'it')
2015-07-20 12:31:59 +00:00
self.cats.append(cat)
activate('en')
menu_pool.clear(all=True)
2015-07-20 12:31:59 +00:00
menu_pool.discover_menus()
# All cms menu modifiers should be removed from menu_pool.modifiers
# so that they do not interfere with our menu nodes
menu_pool.modifiers = [m for m in menu_pool.modifiers if m.__module__.startswith('djangocms_blog')]
2016-09-11 18:58:15 +00:00
self._reset_menus()
2015-07-20 12:31:59 +00:00
def test_menu_nodes(self):
"""
Tests if all categories are present in the menu
"""
2016-02-07 11:16:24 +00:00
pages = self.get_pages()
posts = self.get_posts()
2016-02-07 12:07:32 +00:00
self.reload_urlconf()
2015-09-19 23:11:14 +00:00
2015-07-20 12:31:59 +00:00
for lang in ('en', 'it'):
2015-09-19 23:11:14 +00:00
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
request = self.get_page_request(pages[1], self.user, pages[1].get_absolute_url(lang), edit=True)
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
self.assertTrue(len(nodes), BlogCategory.objects.all().count() + len(pages))
nodes_url = set([node.get_absolute_url() for node in nodes])
2015-09-19 23:11:14 +00:00
cats_url = set([cat.get_absolute_url() for cat in self.cats if cat.has_translation(lang)])
self.assertTrue(cats_url.issubset(nodes_url))
2016-09-11 18:58:15 +00:00
self._reset_menus()
posts[0].categories.clear()
for lang in ('en', 'it'):
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
request = self.get_page_request(pages[1].get_draft_object(), self.user, pages[1].get_draft_object().get_absolute_url(lang))
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
urls = [node.get_absolute_url() for node in nodes]
nodes_url = [node.get_absolute_url() for node in nodes]
self.assertTrue(len(nodes_url), BlogCategory.objects.all().count() + len(pages))
2016-02-07 12:07:32 +00:00
self.assertFalse(posts[0].get_absolute_url(lang) in nodes_url)
self.assertTrue(posts[1].get_absolute_url(lang) in nodes_url)
2015-07-20 12:31:59 +00:00
def test_menu_options(self):
"""
Tests menu structure based on menu_structure configuration
"""
self.get_pages()
posts = self.get_posts()
cats_url = {}
cats_with_post_url = {}
cats_without_post_url = {}
posts_url = {}
languages = ('en', 'it')
for lang in languages:
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
cats_url[lang] = set([cat.get_absolute_url() for cat in self.cats if cat.has_translation(lang)])
cats_with_post_url[lang] = set([cat.get_absolute_url() for cat in self.cats if cat.has_translation(lang) and cat.blog_posts.published().exists()])
cats_without_post_url[lang] = cats_url[lang].difference(cats_with_post_url[lang])
posts_url[lang] = set([post.get_absolute_url(lang) for post in posts if post.has_translation(lang) and post.app_config == self.app_config_1])
# No item in the menu
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_NONE
self.app_config_1.save()
2016-09-11 18:58:15 +00:00
self._reset_menus()
for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertFalse(cats_url[lang].issubset(nodes_url))
self.assertFalse(posts_url[lang].issubset(nodes_url))
# Only posts in the menu
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_POSTS
self.app_config_1.save()
2016-09-11 18:58:15 +00:00
self._reset_menus()
for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertFalse(cats_url[lang].issubset(nodes_url))
self.assertTrue(posts_url[lang].issubset(nodes_url))
# Only categories in the menu
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_CATEGORIES
self.app_config_1.save()
2016-09-11 18:58:15 +00:00
self._reset_menus()
for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertTrue(cats_url[lang].issubset(nodes_url))
self.assertFalse(posts_url[lang].issubset(nodes_url))
# Both types in the menu
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_COMPLETE
self.app_config_1.save()
2016-09-11 18:58:15 +00:00
self._reset_menus()
for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertTrue(cats_url[lang].issubset(nodes_url))
self.assertTrue(posts_url[lang].issubset(nodes_url))
# Both types in the menu
self.app_config_1.app_data.config.menu_empty_categories = False
self.app_config_1.save()
self.app_config_2.app_data.config.menu_empty_categories = False
self.app_config_2.save()
2016-09-12 21:20:06 +00:00
self._reset_menus()
for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang):
2017-05-07 02:51:51 +00:00
self._reset_menus()
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
nodes_url = set([node.url for node in nodes])
self.assertTrue(cats_with_post_url[lang].issubset(nodes_url))
self.assertFalse(cats_without_post_url[lang].intersection(nodes_url))
self.assertTrue(posts_url[lang].issubset(nodes_url))
# Both types in the menu
self.app_config_1.app_data.config.menu_empty_categories = True
self.app_config_1.save()
self.app_config_2.app_data.config.menu_empty_categories = True
self.app_config_2.save()
2016-09-12 21:20:06 +00:00
self._reset_menus()
2015-07-20 12:31:59 +00:00
def test_modifier(self):
"""
Tests if correct category is selected in the menu
according to context (view object)
"""
2015-09-19 23:11:14 +00:00
pages = self.get_pages()
posts = self.get_posts()
2015-09-19 23:11:14 +00:00
2015-07-20 12:31:59 +00:00
tests = (
# view class, view kwarg, view object, category
2015-09-19 23:11:14 +00:00
(PostDetailView, 'slug', posts[0], posts[0].categories.first()),
2015-07-20 12:31:59 +00:00
(CategoryEntriesView, 'category', self.cats[2], self.cats[2])
)
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_COMPLETE
self.app_config_1.save()
2015-07-20 12:31:59 +00:00
for view_cls, kwarg, obj, cat in tests:
2015-09-19 23:11:14 +00:00
with smart_override('en'):
with switch_language(obj, 'en'):
2016-02-15 22:16:34 +00:00
request = self.get_page_request(
pages[1], self.user, path=obj.get_absolute_url()
)
2016-09-11 18:58:15 +00:00
self._reset_menus()
2016-02-15 22:16:34 +00:00
menu_pool.clear(all=True)
2015-09-19 23:11:14 +00:00
view_obj = view_cls()
view_obj.request = request
view_obj.namespace, view_obj.config = get_app_instance(request)
view_obj.app_config = self.app_config_1
view_obj.kwargs = {kwarg: obj.slug}
view_obj.get(request)
view_obj.get_context_data()
2015-09-19 23:11:14 +00:00
# check if selected menu node points to cat
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
2016-02-15 22:16:34 +00:00
found = []
2015-09-19 23:11:14 +00:00
for node in nodes:
if node.selected:
2016-02-15 22:16:34 +00:00
found.append(node.get_absolute_url())
self.assertTrue(obj.get_absolute_url() in found)
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_CATEGORIES
self.app_config_1.save()
for view_cls, kwarg, obj, cat in tests:
with smart_override('en'):
with switch_language(obj, 'en'):
2016-02-15 22:16:34 +00:00
request = self.get_page_request(
pages[1], self.user, path=obj.get_absolute_url()
)
2016-09-11 18:58:15 +00:00
self._reset_menus()
2016-02-15 22:16:34 +00:00
menu_pool.clear(all=True)
view_obj = view_cls()
view_obj.request = request
view_obj.namespace, view_obj.config = get_app_instance(request)
view_obj.app_config = self.app_config_1
view_obj.kwargs = {kwarg: obj.slug}
view_obj.get(request)
view_obj.get_context_data()
# check if selected menu node points to cat
2018-01-14 11:54:44 +00:00
nodes = self.get_nodes(menu_pool, request)
found = [node.get_absolute_url() for node in nodes if node.selected]
2016-02-15 22:16:34 +00:00
self.assertTrue(cat.get_absolute_url() in found)
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_COMPLETE
self.app_config_1.save()