[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> user_menu_test.php (source)

   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  
  89          // Set the configuration.
  90          set_config('customusermenuitems', $data);
  91  
  92          // We always add two dividers as standard.
  93          $dividercount += 2;
  94  
  95          // The basic entry count will additionally include the wrapper menu, Dashboard, Profile, and the Logout link.
  96          $entrycount += 4;
  97  
  98          $output = $OUTPUT->user_menu($USER);
  99          preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);
 100          $this->assertCount($entrycount, $results[0]);
 101  
 102          preg_match_all('/<span class="filler">/', $output, $results);
 103          $this->assertCount($dividercount, $results[0]);
 104      }
 105  
 106  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1