djangocms_blog/tests/test_menu.py

80 lines
3.1 KiB
Python
Raw 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
2015-07-21 04:59:23 +00:00
2015-07-20 12:31:59 +00:00
from django.utils.translation import activate
from menus.menu_pool import menu_pool
2015-09-19 23:11:14 +00:00
from parler.utils.context import switch_language, smart_override
2015-07-21 04:59:23 +00:00
2015-09-12 22:33:13 +00:00
from djangocms_blog.views import CategoryEntriesView, PostDetailView
2015-07-20 12:31:59 +00:00
from . import BaseTest
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]
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.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')]
def test_menu_nodes(self):
"""
Tests if all categories are present in the menu
"""
2015-09-19 23:11:14 +00:00
self.get_posts()
self.get_pages()
2015-07-20 12:31:59 +00:00
for lang in ('en', 'it'):
2015-09-19 23:11:14 +00:00
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang):
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu')
nodes_url = set([node.url for node in nodes])
cats_url = set([cat.get_absolute_url() for cat in self.cats if cat.has_translation(lang)])
self.assertTrue(cats_url.issubset(nodes_url))
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
posts = self.get_posts()
pages = self.get_pages()
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])
)
for view_cls, kwarg, obj, cat in tests:
2015-09-19 23:11:14 +00:00
request = self.get_page_request(pages[1], self.user, path=obj.get_absolute_url())
with smart_override('en'):
with switch_language(obj, 'en'):
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)
# check if selected menu node points to cat
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu')
found = False
for node in nodes:
if node.selected:
self.assertEqual(node.url, obj.get_absolute_url())
found = True
break
self.assertTrue(found)