mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
Fix subsubmenu highlighting bug and add tests
This commit is contained in:
parent
4b3b3c666a
commit
97fe5714e2
@ -39,15 +39,12 @@ def mark_active_menuitem(menu, path):
|
|||||||
|
|
||||||
for urlitem in menu['items']:
|
for urlitem in menu['items']:
|
||||||
urlitem['active'] = False
|
urlitem['active'] = False
|
||||||
|
if not path.startswith(str(urlitem['url'])):
|
||||||
|
continue
|
||||||
# TODO: use a more suitable function instead of os.path.commonprefix
|
# TODO: use a more suitable function instead of os.path.commonprefix
|
||||||
match = os.path.commonprefix([urlitem['url'], path])
|
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 = match
|
||||||
best_match_item = urlitem
|
best_match_item = urlitem
|
||||||
|
|
||||||
|
|||||||
69
plinth/tests/test_templatetags.py
Normal file
69
plinth/tests/test_templatetags.py
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
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))
|
||||||
Loading…
x
Reference in New Issue
Block a user