Skip to content

Commit

Permalink
Fix: Do not show empty menu to users, resolves #405 (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna-lmsace authored May 11, 2024
1 parent f9b429c commit 497bebd
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes

### Unreleased

* 2024-05-10 - Bugfix: Do not show empty smart menus to users, resolves #405
* 2024-05-09 - Bugfix: Smart menu menubar overlaid course index, resolves #607
* 2024-04-27 - Improvement: Add navigation to policy overview page, resolves #633

Expand Down
8 changes: 8 additions & 0 deletions classes/smartmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,14 @@ public function build($resetcache=false) {
$nodescache->menuitems = $menuitems;
$this->cache->set($cachekey, $nodescache);
}

// If the current menu doesn't contain any nodes, hide the menu from users.
// Verify after storing the cache to prevent rebuilding the menu items.
// This helps to verify the cached menus, too.
if (!isset($builditems) || empty($builditems)) {
return false;
}

// Remove the menu items list from nodes. it doesn't need to build the smartmenus.
if (isset($nodes->menuitems)) {
// Remove the menu items list from nodes, it doesn't need anymore.
Expand Down
50 changes: 50 additions & 0 deletions tests/behat/behat_theme_boost_union_base_smartmenus.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,56 @@ public function i_create_smartmenu_with_the_following_fields_to_these_values(Tab
$this->execute('behat_general::i_click_on', ['Save and return', 'button']);
}

/**
* Populate a smart menu using the provided form field/value data and add a default item to the menu
* (to make sure that it is not hidden as empty menu).
*
* @Given /^I create smart menu with a default item with the following fields to these values:$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param TableNode $data
*/
public function i_create_smartmenu_with_default_item_with_the_following_fields_to_these_values(TableNode $data) {
$this->execute('behat_navigation::i_navigate_to_in_site_administration',
['Appearance > Boost Union > Smart menus']);
$this->execute('behat_general::i_click_on', ['Create menu', 'button']);
$this->execute('behat_forms::i_set_the_following_fields_to_these_values', [$data]);
$this->execute('behat_general::i_click_on', ['Save and configure items', 'button']);

// Default item for the menu.
$items = new TableNode([
['Title', 'Info'],
['Menu item type', 'Heading'],
]);

$this->execute('behat_general::i_click_on', ['Add menu item', 'button']);
$this->execute('behat_forms::i_set_the_following_fields_to_these_values', [$items]);
$this->execute('behat_general::i_click_on', ['Save changes', 'button']);
$this->execute('behat_general::i_click_on_in_the', ['Smart menus', 'link', '.breadcrumb', "css_element"]);
}

/**
* Adds a static menu item to the currently opened menu.
*
* @Given /^I add a smart menu static item item "(?P<itemname>(?:[^"]|\\")*)" "(?P<url>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $item Item title.
* @param string $url URL of the static item.
*/
public function i_add_menu_static_item($item, $url) {

$items = new TableNode([
['Title', $item],
['Menu item type', 'Static'],
['URL', $url],
]);

$this->execute('behat_general::i_click_on', ['Save and configure items', 'button']);
$this->execute('behat_general::i_click_on', ['Add menu item', 'button']);
$this->execute('behat_forms::i_set_the_following_fields_to_these_values', [$items]);
$this->execute('behat_general::i_click_on', ['Save changes', 'button']);
$this->execute('behat_general::i_click_on_in_the', ['Smart menus', 'link', '.breadcrumb', "css_element"]);
}

/**
* Fills a smart menu item form with field/value data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, usi
@javascript
Scenario Outline: Smartmenus: Menu items: Dynamic courses - Compose the dynamic course list based on a enrolment role condition
When I log in as "admin"
# Empty menus are hidden from view. To prevent that the whole menu is missing and the test fails,
# a sample item is created.
And I set "List menu" smart menu items with the following fields to these values:
| Title | Info |
| Menu item type | Heading |
And I navigate to smart menu "List menu" items
And I click on ".action-edit" "css_element" in the "Dynamic courses" "table_row"
And I set the field "Dynamic courses: Enrolment role" to "<role>"
Expand Down Expand Up @@ -298,3 +303,29 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, usi
| 6 | CCC Course | BBB Course | AAA Course |
# Option: Course ID number descending
| 7 | AAA Course | BBB Course | CCC Course |

@javascript
Scenario Outline: Smartmenus: Menu items: Dynamic courses - Hide empty menus
When I log in as "admin"
And I navigate to smart menus
And I click on ".action-edit" "css_element" in the "List menu" "table_row"
And I set the field "Menu mode" to "<menumode>"
And I click on "Save and configure items" "button"
And I click on ".action-edit" "css_element" in the "Dynamic courses" "table_row"
And I set the field "Dynamic courses: Enrolment role" to "<role>"
And I set the field "Menu item mode" to "<menumode>"
And I press "Save changes"
And I log out
And I log in as "<user>"
Then I <shouldornot> see smart menu "<menutitle>" in location "Main, Menu, User, Bottom"

Examples:
| role | user | shouldornot | menutitle | menumode |
| Non-editing teacher, Teacher | student1 | should not | List menu | Submenu |
| Non-editing teacher, Teacher | teacher | should | List menu | Submenu |
| Student | student1 | should | List menu | Submenu |
| Student | teacher | should not | List menu | Submenu |
| Non-editing teacher, Teacher | student1 | should not | Course 01 | Inline |
| Non-editing teacher, Teacher | teacher | should | Course 01 | Inline |
| Student | student1 | should | Course 01 | Inline |
| Student | teacher | should not | Course 01 | Inline |
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, man
| Info |
And ".action-hide" "css_element" should exist in the "Info" "table_row"
And ".action-show" "css_element" should not exist in the "Info" "table_row"
And I click on "Add menu item" "button"
And I set the following fields to these values:
| Title | Demo item |
| Menu item type | Heading |
And I click on "Save changes" "button"
Then I should see smart menu "Quick links" item "Info" in location "Main, Menu, User, Bottom"
And I click on ".action-hide" "css_element" in the "Info" "table_row"
Then I should not see smart menu "Quick links" item "Info" in location "Main, Menu, User, Bottom"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,23 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
And ".dropdown-divider" "css_element" should exist in the ".bottom-navigation" "css_element"
# Divider in menubar.
And ".dropdown-divider" "css_element" should exist in the "nav.menubar" "css_element"

@javascript
Scenario Outline: Smartmenus: Menu items: Hide empty menus
When I log in as "admin"
And I create smart menu with the following fields to these values:
| Title | Links |
| Menu location(s) | Main, Menu, User, Bottom |
| Menu mode | <menumode> |
And I should see "Links" in the "smartmenus" "table"
And I should not see smart menu "<menutitle>" in location "Main, Menu, User, Bottom"
And I set "Links" smart menu items with the following fields to these values:
| Title | Smartmenu Resource |
| Menu item type | Static |
| URL | http://moodle.org |
Then I should see smart menu "<menutitle>" in location "Main, Menu, User, Bottom"

Examples:
| menumode | menutitle |
| Submenu | Links |
| Inline | Smartmenu Resource |
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
| Title | Resources |
| Menu item type | Static |
| URL | https://moodle.org |
# Empty menus are hidden from view. To prevent that the whole menu is missing and the test fails,
# a sample item is created.
And I set "Quick links" smart menu items with the following fields to these values:
| Title | Info |
| Menu item type | Heading |
And the following "language packs" exist:
| language |
| de |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
And I set the following fields to these values:
| Title | Links |
| Menu location(s) | Main |
And I click on "Save and return" "button"
And I add a smart menu static item item "Info" "https://moodle.org"
Then I should see smart menu "Links" in location "Main"
And I log out
And I log in as "user1"
Expand Down Expand Up @@ -49,12 +49,9 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
@javascript
Scenario: Smartmenu: Menus: Application - Show a smart menu in different locations
When I log in as "admin"
And I navigate to smart menus
And I click on "Create menu" "button"
And I set the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Links |
| Menu location(s) | Main |
And I click on "Save and return" "button"
Then I should see "Main" in the "Links" "table_row"
And I should see smart menu "Links" in location "Main"
And I click on "Create menu" "button"
Expand All @@ -76,14 +73,14 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
And I set the following fields to these values:
| Title | Certificates |
| Menu location(s) | User |
And I click on "Save and return" "button"
And I add a smart menu static item item "Course completions" "https://moodle.org"
Then I should see "User" in the "Certificates" "table_row"
And I should not see "Main" in the "Certificates" "table_row"
And I should see smart menu "Certificates" in location "User"
And I click on "Create menu" "button"
And I set the following fields to these values:
| Title | SmartMenu Policy |
| Menu location(s) | Bottom |
And I click on "Save and return" "button"
And I add a smart menu static item item "Privacy" "https://moodle.org/privacy"
Then I should see "Bottom" in the "SmartMenu Policy" "table_row"
And I should see smart menu "SmartMenu Policy" in location "Bottom"
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, man
| Links | Main |
And I should see "Links" in the "smartmenus" "table"
And ".smartmenu-actions" "css_element" should exist in the "smartmenus" "table"
And I set "Links" smart menu items with the following fields to these values:
| Title | Info |
| Menu item type | Heading |
And I should see smart menu "Links" in location "Main"

@javascript
Expand Down Expand Up @@ -74,12 +77,9 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, man
@javascript
Scenario: Smartmenus: Menus: Management - Duplicate an existing smart menu
When I log in as "admin"
And I navigate to smart menus
And I click on "Create menu" "button"
And I set the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Links |
| Menu location(s) | Main |
And I click on "Save and return" "button"
And I should see "Links" in the "smartmenus" "table"
And ".action-copy" "css_element" should exist in the "Links" "table_row"
And I click on ".action-copy" "css_element" in the "Links" "table_row"
Expand All @@ -94,12 +94,9 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, man
@javascript
Scenario: Smartmenus: Menus: Management - Modify the visibility of an existing smart menu
When I log in as "admin"
And I navigate to smart menus
And I click on "Create menu" "button"
And I set the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Links |
| Menu location(s) | Main, Menu, User, Bottom |
And I click on "Save and return" "button"
And I should see "Links" in the "smartmenus" "table"
And ".action-hide" "css_element" should exist in the "Links" "table_row"
And ".action-show" "css_element" should not exist in the "Links" "table_row"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
And I set the following fields to these values:
| Title | <span lang="en" class="multilang">Lorem ipsum</span><span lang="de" class="multilang">Dolor sit amet</span> |
| Menu location(s) | Main, Menu, User, Bottom |
And I click on "Save and return" "button"
And I add a smart menu static item item "Multilang" "https://moodle.org"
And I follow "Preferences" in the user menu
And I click on "Preferred language" "link"
And I set the field "Preferred language" to "English ‎(en)‎"
Expand Down Expand Up @@ -221,28 +221,28 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
Then I should see smart menu "Quick links" in location "Main"
And I click on "More" "link" in the ".boost-union-menubar" "css_element"
Then I should see smart menu "Quick links" in location "Menu"
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links 01 |
| Menu location(s) | Main, Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links 02 |
| Menu location(s) | Main, Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links 03 |
| Menu location(s) | Main, Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links 04 |
| Menu location(s) | Main, Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links 05 |
| Menu location(s) | Main, Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links long title 01 |
| Menu location(s) | Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links long title 02 |
| Menu location(s) | Menu |
And I create smart menu with the following fields to these values:
And I create smart menu with a default item with the following fields to these values:
| Title | Test quick demo links 06 |
| Menu location(s) | Main, Menu |
Then I change the viewport size to "1600x495"
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'theme_boost_union';
$plugin->version = 2023102036;
$plugin->version = 2023102037;
$plugin->release = 'v4.3-r12';
$plugin->requires = 2023100900;
$plugin->supported = [403, 403];
Expand Down

0 comments on commit 497bebd

Please sign in to comment.