Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core;
  18  
  19  /**
  20   * Tests user menu functionality.
  21   *
  22   * @package    core
  23   * @copyright  2015 Jetha Chan <jetha@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class user_menu_test extends \advanced_testcase {
  27  
  28      /**
  29       * Custom user menu data for the test_custom_user_menu test.
  30       *
  31       * @return array containing testing data
  32       */
  33      public function custom_user_menu_data() {
  34          return array(
  35              // These are fillers only.
  36              array('###', 0, 1),
  37              array('#####', 0, 1),
  38  
  39              // These are invalid and will not generate any entry or filler.
  40              array('-----', 0, 0),
  41              array('_____', 0, 0),
  42              array('test', 0, 0),
  43              array('#Garbage#', 0, 0),
  44  
  45              // These are valid but have an invalid string identifiers or components. They will still produce a menu
  46              // item, and no exception should be thrown.
  47              array('#my1files,moodle|/user/files.php', 1, 1),
  48              array('#my1files,moodleakjladf|/user/files.php', 1, 1),
  49              array('#my1files,a/b|/user/files.php', 1, 1),
  50              array('#my1files,#b|/user/files.php', 1, 1),
  51  
  52              // These are unusual, but valid and will generate a menu entry (no filler).
  53              array('-|-|-|-', 1, 1),
  54              array('-|-|-', 1, 1),
  55              array('-|-', 1, 1),
  56              array('#f234|2', 1, 1),
  57  
  58              // This is a pretty typical entry.
  59              array('messages,message|/message/index.php', 1, 1),
  60  
  61              // And these are combinations containing both valid and invalid.
  62              array('messages,message|/message/index.php
  63  privatefiles,moodle|/user/files.php
  64  ###
  65  badges,badges|/badges/mybadges.php
  66  -|-
  67  test
  68  -
  69  #####
  70  #f234|2', 5, 3),
  71          );
  72      }
  73  
  74      /**
  75       * Test the custom user menu.
  76       *
  77       * @dataProvider custom_user_menu_data
  78       * @param string $input The menu text to test
  79       * @param int $entrycount The numbers of entries expected
  80       */
  81      public function test_custom_user_menu($data, $entrycount, $dividercount) {
  82          global $CFG, $OUTPUT, $USER, $PAGE;
  83  
  84          // Must reset because of config and user modifications.
  85          $this->resetAfterTest(true);
  86  
  87          // Test using an admin user at the root of Moodle; this way we don't have to create a test user with avatar.
  88          $this->setAdminUser();
  89          $PAGE->set_url('/');
  90          $CFG->theme = 'classic';
  91          $PAGE->reset_theme_and_output();
  92          $PAGE->initialise_theme_and_output();
  93  
  94          // Set the configuration.
  95          set_config('customusermenuitems', $data);
  96  
  97          // We always add two dividers as standard.
  98          $dividercount += 2;
  99  
 100          // The basic entry count will additionally include the wrapper menu, Preferences, Logout and switch roles link.
 101          $entrycount += 3;
 102  
 103          $output = $OUTPUT->user_menu($USER);
 104          preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);
 105          $this->assertCount($entrycount, $results[0]);
 106  
 107          preg_match_all('/<span class="filler">/', $output, $results);
 108          $this->assertCount($dividercount, $results[0]);
 109      }
 110  
 111  }