Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * Tests user menu functionality.
  19   *
  20   * @package    core
  21   * @copyright  2015 Jetha Chan <jetha@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  class core_user_menu_testcase extends advanced_testcase {
  25  
  26      /**
  27       * Custom user menu data for the test_custom_user_menu test.
  28       *
  29       * @return array containing testing data
  30       */
  31      public function custom_user_menu_data() {
  32          return array(
  33              // These are fillers only.
  34              array('###', 0, 1),
  35              array('#####', 0, 1),
  36  
  37              // These are invalid and will not generate any entry or filler.
  38              array('-----', 0, 0),
  39              array('_____', 0, 0),
  40              array('test', 0, 0),
  41              array('#Garbage#', 0, 0),
  42  
  43              // These are valid but have an invalid string identifiers or components. They will still produce a menu
  44              // item, and no exception should be thrown.
  45              array('#my1files,moodle|/user/files.php|download', 1, 0),
  46              array('#my1files,moodleakjladf|/user/files.php|download', 1, 0),
  47              array('#my1files,a/b|/user/files.php|download', 1, 0),
  48              array('#my1files,#b|/user/files.php|download', 1, 0),
  49  
  50              // These are unusual, but valid and will generate a menu entry (no filler).
  51              array('-|-|-|-', 1, 0),
  52              array('-|-|-', 1, 0),
  53              array('-|-', 1, 0),
  54              array('#f234|2', 1, 0),
  55  
  56              // This is a pretty typical entry.
  57              array('messages,message|/message/index.php|message', 1, 0),
  58  
  59              // And these are combinations containing both valid and invalid.
  60              array('messages,message|/message/index.php|message
  61  privatefiles,moodle|/user/files.php|download
  62  ###
  63  badges,badges|/badges/mybadges.php|award
  64  -|-|-
  65  test
  66  -
  67  #####
  68  #f234|2', 5, 2),
  69          );
  70      }
  71  
  72      /**
  73       * Test the custom user menu.
  74       *
  75       * @dataProvider custom_user_menu_data
  76       * @param string $input The menu text to test
  77       * @param int $entrycount The numbers of entries expected
  78       */
  79      public function test_custom_user_menu($data, $entrycount, $dividercount) {
  80          global $CFG, $OUTPUT, $USER, $PAGE;
  81  
  82          // Must reset because of config and user modifications.
  83          $this->resetAfterTest(true);
  84  
  85          // Test using an admin user at the root of Moodle; this way we don't have to create a test user with avatar.
  86          $this->setAdminUser();
  87          $PAGE->set_url('/');
  88          $CFG->theme = 'classic';
  89          $PAGE->reset_theme_and_output();
  90          $PAGE->initialise_theme_and_output();
  91  
  92          // Set the configuration.
  93          set_config('customusermenuitems', $data);
  94  
  95          // We always add two dividers as standard.
  96          $dividercount += 2;
  97  
  98          // The basic entry count will additionally include the wrapper menu, Dashboard, Profile, Logout and switch roles link.
  99          $entrycount += 4;
 100  
 101          $output = $OUTPUT->user_menu($USER);
 102          preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);
 103          $this->assertCount($entrycount, $results[0]);
 104  
 105          preg_match_all('/<span class="filler">/', $output, $results);
 106          $this->assertCount($dividercount, $results[0]);
 107      }
 108  
 109  }