From 97fe5714e236a849b9829669c2bfd994d7b66ca8 Mon Sep 17 00:00:00 2001 From: fonfon Date: Tue, 9 Dec 2014 23:32:06 +0100 Subject: [PATCH] Fix subsubmenu highlighting bug and add tests --- plinth/templatetags/plinth_extras.py | 9 ++-- plinth/tests/test_templatetags.py | 69 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 plinth/tests/test_templatetags.py diff --git a/plinth/templatetags/plinth_extras.py b/plinth/templatetags/plinth_extras.py index 0b7b38698..4b8c35af6 100644 --- a/plinth/templatetags/plinth_extras.py +++ b/plinth/templatetags/plinth_extras.py @@ -39,15 +39,12 @@ def mark_active_menuitem(menu, path): for urlitem in menu['items']: urlitem['active'] = False + if not path.startswith(str(urlitem['url'])): + continue # TODO: use a more suitable function instead of os.path.commonprefix match = os.path.commonprefix([urlitem['url'], path]) - # In case of 'xx/create' and 'xx/change' we'd have 'xx/c' as prefix. - # That's a wrong match, skip it. - match_clean = match.rpartition('/')[0] - if (len(match_clean) + 1) < len(match): - continue - if len(match_clean) > len(best_match): + if len(match) > len(best_match): best_match = match best_match_item = urlitem diff --git a/plinth/tests/test_templatetags.py b/plinth/tests/test_templatetags.py new file mode 100644 index 000000000..2a2b55c56 --- /dev/null +++ b/plinth/tests/test_templatetags.py @@ -0,0 +1,69 @@ +#!/usr/bin/python3 +# -*- mode: python; mode: auto-fill; fill-column: 80 -*- +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +import unittest + +from plinth.templatetags.plinth_extras import mark_active_menuitem + + +class TestPrivileged(unittest.TestCase): + """Verify that the highlighting of the subsubmenu is working correctly""" + + def is_active_url(self, menu, url): + """Verify that only the given url is set as 'active' in the menu""" + for urlitem in menu['items']: + if urlitem['url'] == url and not urlitem['active']: + return False + if urlitem['url'] != url and urlitem['active']: + return False + return True + + def verify_active_menuitems(self, menu): + """Verify that one and only one menuitem is marked as active""" + return sum([x['active'] for x in menu['items']])==1 + + def test_highlighting(self): + """Test detection of active subsubmenu items using request.path""" + menu = { + "text": "blubb", + "items": [{'url': '/abc/123/abc/', 'text': 'abc'}, + {'url': '/abc/123/', 'text': 'overview'}, + {'url': '/abc/123/crunch/', 'text': 'crunch'}, + {'url': '/abc/123/create/', 'text': 'create'}] + } + + menu = mark_active_menuitem(menu, '/abc/123/crunch/new/') + assert(self.is_active_url(menu, '/abc/123/crunch/')) + assert(self.verify_active_menuitems(menu)) + + menu = mark_active_menuitem(menu, '/abc/123/create/') + assert(self.is_active_url(menu, '/abc/123/create/')) + assert(self.verify_active_menuitems(menu)) + + menu = mark_active_menuitem(menu, '/abc/123/nolink/') + assert(self.is_active_url(menu, '/abc/123/')) + assert(self.verify_active_menuitems(menu)) + + menu = mark_active_menuitem(menu, '/abc/123/abx/') + assert(self.is_active_url(menu, '/abc/123/')) + assert(self.verify_active_menuitems(menu)) + + menu = mark_active_menuitem(menu, '/abc/123/') + assert(self.is_active_url(menu, '/abc/123/')) + assert(self.verify_active_menuitems(menu))