add menu tests
This commit is contained in:
parent
65f3086824
commit
f86ca7ad4a
3 changed files with 114 additions and 4 deletions
|
@ -30,12 +30,12 @@ menu_pool.register_menu(BlogCategoryMenu)
|
|||
|
||||
class BlogNavModifier(Modifier):
|
||||
"""
|
||||
This navigation modifier makes sure that when
|
||||
a particular blog post is viewed,
|
||||
This navigation modifier makes sure that when
|
||||
a particular blog post is viewed,
|
||||
a corresponding category is selected in menu
|
||||
"""
|
||||
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
|
||||
if not post_cut: return nodes
|
||||
if post_cut: return nodes
|
||||
if not hasattr(request, 'toolbar'):
|
||||
return nodes
|
||||
models = ('djangocms_blog.post', 'djangocms_blog.blogcategory')
|
||||
|
|
|
@ -22,6 +22,9 @@ from djangocms_blog.models import BlogCategory, Post
|
|||
|
||||
User = get_user_model()
|
||||
|
||||
def _get_cat_pk(lang, name):
|
||||
return lambda: BlogCategory.objects.translated(lang, name=name).get().pk
|
||||
|
||||
|
||||
class BaseTest(TestCase):
|
||||
"""
|
||||
|
@ -59,6 +62,22 @@ class BaseTest(TestCase):
|
|||
]
|
||||
}
|
||||
|
||||
cat_data = {
|
||||
'it': [
|
||||
{'name': u'Fortissimo'},
|
||||
{'name': u'Pianississimo'},
|
||||
{'name': u'Mezzo'},
|
||||
{'name': u'Forte', 'parent_id': _get_cat_pk('it', 'Mezzo')},
|
||||
],
|
||||
'en': [
|
||||
{'name': u'Very loud'},
|
||||
{'name': u'Very very silent'},
|
||||
{'name': u'Almost'},
|
||||
{'name': u'Loud', 'parent_id': _get_cat_pk('en', 'Almost')},
|
||||
{'name': u'Silent', 'parent_id': _get_cat_pk('en', 'Almost')},
|
||||
]
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.request_factory = RequestFactory()
|
||||
|
@ -91,6 +110,17 @@ class BaseTest(TestCase):
|
|||
original_filename=self.image_name,
|
||||
file=file_obj)
|
||||
|
||||
def _get_category(self, data, category=None, lang='en'):
|
||||
data = {k: (v() if hasattr(v, '__call__') else v) for k, v in data.items()}
|
||||
if not category:
|
||||
category = BlogCategory.objects.create(**data)
|
||||
else:
|
||||
category.set_current_language(lang)
|
||||
for attr, val in data.items():
|
||||
setattr(category, attr, val)
|
||||
category.save()
|
||||
return category
|
||||
|
||||
def _get_post(self, data, post=None, lang='en', sites=None):
|
||||
if not post:
|
||||
post_data = {
|
||||
|
@ -117,7 +147,7 @@ class BaseTest(TestCase):
|
|||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
User.objects.all().delete()
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
for post in Post.objects.all():
|
||||
post.delete()
|
||||
|
|
80
tests/test_menu.py
Normal file
80
tests/test_menu.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import copy
|
||||
from django.utils.translation import activate
|
||||
from menus.menu_pool import menu_pool
|
||||
from parler.utils.context import switch_language
|
||||
from djangocms_blog.views import PostDetailView, CategoryEntriesView
|
||||
from . import BaseTest
|
||||
|
||||
|
||||
class MenuTest(BaseTest):
|
||||
def setUp(self):
|
||||
super(MenuTest, self).setUp()
|
||||
self.cats = [self.category_1]
|
||||
for i, cat_data in enumerate(self.cat_data['en']):
|
||||
cat = self._get_category(cat_data)
|
||||
if i < len(self.cat_data['it']):
|
||||
cat = self._get_category(self.cat_data['it'][i], cat, 'it')
|
||||
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
|
||||
"""
|
||||
for lang in ('en', 'it'):
|
||||
request = self.get_page_request(None, self.user,
|
||||
r'/%s/blog/' % lang, edit=False)
|
||||
activate(lang)
|
||||
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu')
|
||||
nodes_copy = copy.deepcopy(nodes)
|
||||
for cat in self.cats:
|
||||
if not cat.has_translation(lang):
|
||||
continue
|
||||
with switch_language(cat, lang):
|
||||
# find in node list
|
||||
found = None
|
||||
for node in nodes_copy:
|
||||
if node.url == cat.get_absolute_url():
|
||||
found = node
|
||||
break
|
||||
self.assertIsNotNone(found)
|
||||
nodes_copy.remove(found)
|
||||
self.assertEqual(node.id, cat.id)
|
||||
self.assertEqual(node.title, cat.name)
|
||||
# check that all categories were found in menu
|
||||
self.assertEqual(len(nodes_copy), 0)
|
||||
|
||||
def test_modifier(self):
|
||||
"""
|
||||
Tests if correct category is selected in the menu
|
||||
according to context (view object)
|
||||
"""
|
||||
post1, post2 = self.get_posts()
|
||||
tests = (
|
||||
# view class, view kwarg, view object, category
|
||||
(PostDetailView, 'slug', post1, post1.categories.first()),
|
||||
(CategoryEntriesView, 'category', self.cats[2], self.cats[2])
|
||||
)
|
||||
for view_cls, kwarg, obj, cat in tests:
|
||||
request = self.get_page_request(None, self.user, r'/en/blog/', edit=False)
|
||||
activate('en')
|
||||
with switch_language(obj, 'en'):
|
||||
view_obj = view_cls()
|
||||
view_obj.request = request
|
||||
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, cat.get_absolute_url())
|
||||
found = True
|
||||
break
|
||||
self.assertTrue(found)
|
Loading…
Reference in a new issue