diff --git a/plinth/menu.py b/plinth/menu.py index 256c64323..959730801 100644 --- a/plinth/menu.py +++ b/plinth/menu.py @@ -23,11 +23,11 @@ from plinth.utils import format_lazy class Menu(object): """One menu item.""" - def __init__(self, name="", short_description="", label="", icon="", - url="#", order=50): - """label is the text that is displayed on the menu. + def __init__(self, name="", short_description="", icon="", url="#", + order=50): + """Initialize a new menu item with basic properties. - icon is the icon to be displayed next to the label. + icon is the icon to be displayed for the menu item. Choose from the Fork Awesome set: https://forkawesome.github.io/Fork-Awesome/icons/ @@ -44,7 +44,6 @@ class Menu(object): """ self.name = name self.short_description = short_description - self.label = label self.icon = icon self.url = url self.order = order @@ -74,20 +73,9 @@ class Menu(object): url_args and url_kwargs will be passed on to Django reverse(). """ - if short_description: - label = format_lazy('{0} ({1})', short_description, name) - else: - label = name url = reverse_lazy(urlname, args=url_args, kwargs=url_kwargs) - return self.add_item(name, short_description, label, icon, url, order) - - def add_item(self, name, short_description, label, icon, url, order=50): - """Create a new menu item with given parameters, add it to this menu and - return it. - - """ - item = Menu(name=name, short_description=short_description, - label=label, icon=icon, url=url, order=order) + item = Menu(name=name, short_description=short_description, icon=icon, + url=url, order=order) self.items.append(item) return item diff --git a/plinth/tests/data/django_test_settings.py b/plinth/tests/data/django_test_settings.py index 14e074b15..02cd29399 100644 --- a/plinth/tests/data/django_test_settings.py +++ b/plinth/tests/data/django_test_settings.py @@ -48,6 +48,6 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.messages.middleware.MessageMiddleware', ) -ROOT_URLCONF = 'plinth.urls' +ROOT_URLCONF = 'plinth.tests.data.urls' SECRET_KEY = 'django_tests_secret_key' diff --git a/plinth/templates/menu.html b/plinth/tests/data/urls.py similarity index 56% rename from plinth/templates/menu.html rename to plinth/tests/data/urls.py index 4b7e9cb00..ba364b75b 100644 --- a/plinth/templates/menu.html +++ b/plinth/tests/data/urls.py @@ -1,4 +1,3 @@ -{% comment %} # # This file is part of FreedomBox. # @@ -15,27 +14,17 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # -{% endcomment %} +""" +Django URL patterns for running tests. +""" -{% load i18n %} +from django.conf.urls import url +from django.views.generic import TemplateView - +_test_view = TemplateView.as_view(template_name='index.html') +urlpatterns = [ + url(r'^$', _test_view, name='index'), + url(r'^apps/$', _test_view, name='apps'), + url(r'^sys/$', _test_view, name='system'), + url(r'^test/(?P\d+)/(?P\d+)/(?P\d+)/$', _test_view, name='test'), +] diff --git a/plinth/tests/test_menu.py b/plinth/tests/test_menu.py index 5a3b8d890..53b1c1a1c 100644 --- a/plinth/tests/test_menu.py +++ b/plinth/tests/test_menu.py @@ -27,7 +27,7 @@ from django.urls import reverse from plinth import menu as menu_module from plinth.menu import Menu -URL_TEMPLATE = '/a{}/b{}/c{}/' +URL_TEMPLATE = '/test/{}/{}/{}/' # Test helper methods @@ -35,30 +35,26 @@ URL_TEMPLATE = '/a{}/b{}/c{}/' def build_menu(size=5): """Build a menu with the specified number of items.""" random.seed() - item_data = [] - for index in range(1, size + 1): - item_data.append([ - 'Name' + str(index), 'ShortDescription' + str(index), - 'Item' + str(index), 'Icon' + str(index), - URL_TEMPLATE.format(index, index, index), - random.randint(0, 1000) - ]) menu = Menu() - for data in item_data: - menu.add_item(data[0], data[1], data[2], data[3], data[4], data[5]) + + for index in range(1, size + 1): + args = [ + 'Name' + str(index), 'Icon' + str(index), 'test', + 'ShortDescription' + str(index), + random.randint(0, 1000) + ] + kwargs = {'url_kwargs': {'a': index, 'b': index, 'c': index}} + + menu.add_urlname(*args, **kwargs) return menu -def dump_menu(menu): - """Dump the specified menu URL hierarchy to the console.""" - print() - print('# # # # # # # # # #') - print('Top level URL: %s' % menu.url) - for item in menu.items: - print(' Item URL: %s' % item.url) - print('# # # # # # # # # #') +@pytest.fixture(name='empty_menus', autouse=True) +def fixture_empty_menus(): + """Remove all menu entries before starting a test.""" + Menu._all_menus = {} def test_init(): @@ -68,12 +64,10 @@ def test_init(): assert isinstance(main_menu, Menu) apps_menu = main_menu.get('apps') - assert apps_menu.label == '' assert apps_menu.icon == 'fa-download' assert str(apps_menu.url) == '/apps/' system_menu = main_menu.get('system') - assert system_menu.label == '' assert system_menu.icon == 'fa-cog' assert str(system_menu.url) == '/sys/' @@ -81,7 +75,6 @@ def test_init(): def test_menu_creation_without_arguments(): """Verify the Menu state without initialization parameters.""" menu = Menu() - assert menu.label == '' assert menu.icon == '' assert menu.url == '#' assert menu.order == 50 @@ -92,16 +85,14 @@ def test_menu_creation_with_arguments(): """Verify the Menu state with initialization parameters.""" expected_name = 'Name' expected_short_description = 'ShortDescription' - expected_label = 'Label' expected_icon = 'Icon' expected_url = '/aaa/bbb/ccc/' expected_order = 42 - menu = Menu(expected_name, expected_short_description, expected_label, - expected_icon, expected_url, expected_order) + menu = Menu(expected_name, expected_short_description, expected_icon, + expected_url, expected_order) assert expected_name == menu.name assert expected_short_description == menu.short_description - assert expected_label == menu.label assert expected_icon == menu.icon assert expected_url == menu.url assert expected_order == menu.order @@ -112,20 +103,18 @@ def test_get(): """Verify that a menu item can be correctly retrieved.""" expected_name = 'Name2' expected_short_description = 'ShortDescription2' - expected_label = 'Label2' expected_icon = 'Icon2' expected_url = 'index' reversed_url = reverse(expected_url) expected_order = 2 menu = Menu() - menu.add_item(expected_name, expected_short_description, expected_label, - expected_icon, reversed_url, expected_order) + menu.add_urlname(expected_name, expected_icon, expected_url, + expected_short_description, expected_order) actual_item = menu.get(expected_url) assert actual_item is not None assert expected_name == actual_item.name assert expected_short_description == actual_item.short_description - assert expected_label == actual_item.label assert expected_icon == actual_item.icon assert reversed_url == actual_item.url assert expected_order == actual_item.order @@ -136,16 +125,15 @@ def test_get_with_item_not_found(): """Verify that a KeyError is raised if a menu item is not found.""" expected_name = 'Name3' expected_short_description = 'ShortDescription3' - expected_label = 'Label3' expected_icon = 'Icon3' expected_url = 'index' expected_order = 3 menu = Menu() - menu.add_item(expected_name, expected_short_description, expected_label, - expected_icon, expected_url, expected_order) + menu.add_urlname(expected_name, expected_icon, expected_url, + expected_short_description, expected_order) with pytest.raises(KeyError): - menu.get(expected_url) + menu.get('apps') def test_sort_items(): @@ -163,56 +151,31 @@ def test_sort_items(): for index in range(1, size): assert items[index].order >= items[index - 1].order if items[index].order == items[index - 1].order: - assert items[index].label >= items[index - 1].label + assert items[index].name >= items[index - 1].name def test_add_urlname(): """Verify that a named URL can be added to a menu correctly.""" - expected_title = 'Label4' + expected_name = 'Name4' expected_short_description = 'Description4' expected_icon = 'Icon4' expected_url = 'index' reversed_url = reverse(expected_url) expected_order = 4 menu = Menu() - actual_item = menu.add_urlname(expected_title, expected_icon, expected_url, + actual_item = menu.add_urlname(expected_name, expected_icon, expected_url, expected_short_description, expected_order) assert len(menu.items) == 1 assert actual_item is not None assert actual_item == menu.items[0] - assert actual_item.label == 'Description4 (Label4)' + assert expected_name == actual_item.name assert expected_icon == actual_item.icon assert reversed_url == actual_item.url assert expected_order == actual_item.order assert not actual_item.items -def test_add_item(): - """Verify that a menu item can be correctly added.""" - expected_name = 'Name5' - expected_short_description = 'ShortDescription5' - expected_label = 'Label5' - expected_icon = 'Icon5' - expected_url = '/jjj/kkk/lll/' - expected_order = 5 - menu = Menu() - actual_item = menu.add_item(expected_name, expected_short_description, - expected_label, expected_icon, expected_url, - expected_order) - - assert len(menu.items) == 1 - assert actual_item is not None - assert actual_item == menu.items[0] - assert expected_name == actual_item.name - assert expected_short_description == actual_item.short_description - assert expected_label == actual_item.label - assert expected_icon == actual_item.icon - assert expected_url == actual_item.url - assert expected_order == actual_item.order - assert not actual_item.items - - def test_active_item(): """Verify that an active menu item can be correctly retrieved.""" menu = build_menu() @@ -222,7 +185,6 @@ def test_active_item(): request.path = URL_TEMPLATE.format(index, index, index) item = menu.active_item(request) if index <= 5: - assert 'Item' + str(index) == item.label assert request.path == item.url else: assert item is None @@ -235,5 +197,4 @@ def test_active_item_when_inside_subpath(): request = HttpRequest() request.path = expected_url + 'd/e/f/' item = menu.active_item(request) - assert item.label == 'Item1' assert expected_url == item.url