Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Unit tests for lib/navigationlib.php
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2009 Sam Hemelryk
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
  24   */
  25  
  26  namespace core;
  27  
  28  use action_link;
  29  use global_navigation;
  30  use navbar;
  31  use navigation_cache;
  32  use navigation_node;
  33  use navigation_node_collection;
  34  use pix_icon;
  35  use popup_action;
  36  use settings_navigation;
  37  
  38  defined('MOODLE_INTERNAL') || die();
  39  
  40  global $CFG;
  41  require_once($CFG->libdir . '/navigationlib.php');
  42  
  43  /**
  44   * Unit tests for lib/navigationlib.php
  45   *
  46   * @package   core
  47   * @category  test
  48   * @copyright 2009 Sam Hemelryk
  49   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
  50   */
  51  class navigationlib_test extends \advanced_testcase {
  52      /**
  53       * @var navigation_node
  54       */
  55      public $node;
  56  
  57      protected function setup_node() {
  58          global $PAGE, $SITE;
  59  
  60          $PAGE->set_url('/');
  61          $PAGE->set_course($SITE);
  62  
  63          $activeurl = $PAGE->url;
  64          $inactiveurl = new \moodle_url('http://www.moodle.com/');
  65  
  66          navigation_node::override_active_url($PAGE->url);
  67  
  68          $this->node = new navigation_node('Test Node');
  69          $this->node->type = navigation_node::TYPE_SYSTEM;
  70          // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
  71          $this->node->add('first child without key', null, navigation_node::TYPE_CUSTOM);
  72          $demo1 = $this->node->add('demo1', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
  73          $demo2 = $this->node->add('demo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
  74          $demo3 = $this->node->add('demo3', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3', new pix_icon('i/course', ''));
  75          $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node::TYPE_COURSE,  null, 'demo4', new pix_icon('i/course', ''));
  76          $demo5 = $demo3->add('demo5', $activeurl, navigation_node::TYPE_COURSE, null, 'demo5', new pix_icon('i/course', ''));
  77          $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
  78          $demo6 = $demo3->add('demo6', null, navigation_node::TYPE_CONTAINER, 'container node test', 'demo6');
  79          $hiddendemo1 = $this->node->add('hiddendemo1', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
  80          $hiddendemo1->hidden = true;
  81          $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';
  82          $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
  83      }
  84  
  85      public function test_node__construct() {
  86          $this->setup_node();
  87  
  88          $fakeproperties = array(
  89              'text' => 'text',
  90              'shorttext' => 'A very silly extra long short text string, more than 25 characters',
  91              'key' => 'key',
  92              'type' => 'navigation_node::TYPE_COURSE',
  93              'action' => new \moodle_url('http://www.moodle.org/'));
  94  
  95          $node = new navigation_node($fakeproperties);
  96          $this->assertSame($fakeproperties['text'], $node->text);
  97          $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext, 0, -3)) === 0);
  98          $this->assertSame($fakeproperties['key'], $node->key);
  99          $this->assertSame($fakeproperties['type'], $node->type);
 100          $this->assertSame($fakeproperties['action'], $node->action);
 101      }
 102  
 103      public function test_node_add() {
 104          $this->setup_node();
 105  
 106          // Add a node with all args set.
 107          $node1 = $this->node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'key', new pix_icon('i/course', ''));
 108          // Add a node with the minimum args required.
 109          $node2 = $this->node->add('test_add_2', null, navigation_node::TYPE_CUSTOM, 'testadd2');
 110          $node3 = $this->node->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));
 111  
 112          $this->assertInstanceOf('navigation_node', $node1);
 113          $this->assertInstanceOf('navigation_node', $node2);
 114          $this->assertInstanceOf('navigation_node', $node3);
 115  
 116          $ref = $this->node->get('key');
 117          $this->assertSame($node1, $ref);
 118  
 119          $ref = $this->node->get($node2->key);
 120          $this->assertSame($node2, $ref);
 121  
 122          $ref = $this->node->get($node2->key, $node2->type);
 123          $this->assertSame($node2, $ref);
 124  
 125          $ref = $this->node->get($node3->key, $node3->type);
 126          $this->assertSame($node3, $ref);
 127      }
 128  
 129      public function test_node_add_before() {
 130          $this->setup_node();
 131  
 132          // Create 3 nodes.
 133          $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
 134              'test 1', 'testadd1');
 135          $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
 136              'test 2', 'testadd2');
 137          $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
 138              'test 3', 'testadd3');
 139          // Add node 2, then node 1 before 2, then node 3 at end.
 140          $this->node->add_node($node2);
 141          $this->node->add_node($node1, 'testadd2');
 142          $this->node->add_node($node3);
 143          // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
 144          foreach ($this->node->children as $child) {
 145              $keys[] = $child->key;
 146          }
 147          $this->assertSame('testadd1', $keys[count($keys)-3]);
 148          $this->assertSame('testadd2', $keys[count($keys)-2]);
 149          $this->assertSame('testadd3', $keys[count($keys)-1]);
 150      }
 151  
 152      public function test_node_add_class() {
 153          $this->setup_node();
 154  
 155          $node = $this->node->get('demo1');
 156          $this->assertInstanceOf('navigation_node', $node);
 157          if ($node !== false) {
 158              $node->add_class('myclass');
 159              $classes = $node->classes;
 160              $this->assertContains('myclass', $classes);
 161          }
 162      }
 163  
 164      public function test_node_check_if_active() {
 165          $this->setup_node();
 166  
 167          // First test the string urls
 168          // Demo1 -> action is http://www.moodle.org/, thus should be true.
 169          $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
 170          if ($this->assertInstanceOf('navigation_node', $demo5)) {
 171              $this->assertTrue($demo5->check_if_active());
 172          }
 173  
 174          // Demo2 -> action is http://www.moodle.com/, thus should be false.
 175          $demo2 = $this->node->get('demo2');
 176          if ($this->assertInstanceOf('navigation_node', $demo2)) {
 177              $this->assertFalse($demo2->check_if_active());
 178          }
 179      }
 180  
 181      public function test_node_contains_active_node() {
 182          $this->setup_node();
 183  
 184          // Demo5, and activity1 were set to active during setup.
 185          // Should be true as it contains all nodes.
 186          $this->assertTrue($this->node->contains_active_node());
 187          // Should be true as demo5 is a child of demo3.
 188          $this->assertTrue($this->node->get('demo3')->contains_active_node());
 189          // Obviously duff.
 190          $this->assertFalse($this->node->get('demo1')->contains_active_node());
 191          // Should be true as demo5 contains activity1.
 192          $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
 193          // Should be true activity1 is the active node.
 194          $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
 195          // Obviously duff.
 196          $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
 197      }
 198  
 199      public function test_node_find_active_node() {
 200          $this->setup_node();
 201  
 202          $activenode1 = $this->node->find_active_node();
 203          $activenode2 = $this->node->get('demo1')->find_active_node();
 204  
 205          if ($this->assertInstanceOf('navigation_node', $activenode1)) {
 206              $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
 207              $this->assertSame($activenode1, $ref);
 208          }
 209  
 210          $this->assertNotInstanceOf('navigation_node', $activenode2);
 211      }
 212  
 213      public function test_node_find() {
 214          $this->setup_node();
 215  
 216          $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
 217          $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
 218          $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
 219          $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
 220          $this->assertInstanceOf('navigation_node', $node1);
 221          $this->assertInstanceOf('navigation_node', $node2);
 222          $this->assertNotInstanceOf('navigation_node', $node3);
 223          $this->assertNotInstanceOf('navigation_node', $node4);
 224      }
 225  
 226      public function test_node_find_expandable() {
 227          $this->setup_node();
 228  
 229          $expandable = array();
 230          $this->node->find_expandable($expandable);
 231  
 232          $this->assertCount(0, $expandable);
 233          if (count($expandable) === 4) {
 234              $name = $expandable[0]['key'];
 235              $name .= $expandable[1]['key'];
 236              $name .= $expandable[2]['key'];
 237              $name .= $expandable[3]['key'];
 238              $this->assertSame('demo1demo2demo4hiddendemo2', $name);
 239          }
 240      }
 241  
 242      public function test_node_get() {
 243          $this->setup_node();
 244  
 245          $node1 = $this->node->get('demo1'); // Exists.
 246          $node2 = $this->node->get('demo4'); // Doesn't exist for this node.
 247          $node3 = $this->node->get('demo0'); // Doesn't exist at all.
 248          $node4 = $this->node->get(false);   // Sometimes occurs in nature code.
 249          $this->assertInstanceOf('navigation_node', $node1);
 250          $this->assertFalse($node2);
 251          $this->assertFalse($node3);
 252          $this->assertFalse($node4);
 253      }
 254  
 255      public function test_node_get_css_type() {
 256          $this->setup_node();
 257  
 258          $csstype1 = $this->node->get('demo3')->get_css_type();
 259          $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
 260          $this->node->get('demo3')->get('demo5')->type = 1000;
 261          $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
 262          $csstype4 = $this->node->get('demo3')->get('demo6')->get_css_type();
 263          $this->assertSame('type_category', $csstype1);
 264          $this->assertSame('type_course', $csstype2);
 265          $this->assertSame('type_unknown', $csstype3);
 266          $this->assertSame('type_container', $csstype4);
 267      }
 268  
 269      public function test_node_make_active() {
 270          global $CFG;
 271          $this->setup_node();
 272  
 273          $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
 274          $node2 = $this->node->add('active node 2', new \moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
 275          $node1->make_active();
 276          $this->node->get('anode2')->make_active();
 277          $this->assertTrue($node1->isactive);
 278          $this->assertTrue($this->node->get('anode2')->isactive);
 279      }
 280  
 281      public function test_node_remove() {
 282          $this->setup_node();
 283  
 284          $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
 285          $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
 286          $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
 287  
 288          $this->assertInstanceOf('navigation_node', $remove1);
 289          $this->assertInstanceOf('navigation_node', $remove2);
 290          $this->assertInstanceOf('navigation_node', $remove3);
 291  
 292          $this->assertInstanceOf('navigation_node', $this->node->get('remove1'));
 293          $this->assertInstanceOf('navigation_node', $this->node->get('remove2'));
 294          $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
 295  
 296          // Remove element and make sure this is no longer a child.
 297          $this->assertTrue($remove1->remove());
 298          $this->assertFalse($this->node->get('remove1'));
 299          $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true));
 300  
 301          // Make sure that we can insert element after removal.
 302          $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4');
 303          $this->node->add_node($insertelement, 'remove2');
 304          $this->assertNotEmpty($this->node->get('element4'));
 305  
 306          // Remove more elements.
 307          $this->assertTrue($this->node->get('remove2')->remove());
 308          $this->assertFalse($this->node->get('remove2'));
 309  
 310          // Make sure that we can add element after removal.
 311          $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5');
 312          $this->assertNotEmpty($this->node->get('element5'));
 313  
 314          $this->assertTrue($remove2->get('remove3')->remove());
 315  
 316          $this->assertFalse($this->node->get('remove1'));
 317          $this->assertFalse($this->node->get('remove2'));
 318      }
 319  
 320      public function test_node_remove_class() {
 321          $this->setup_node();
 322  
 323          $this->node->add_class('testclass');
 324          $this->assertTrue($this->node->remove_class('testclass'));
 325          $this->assertNotContains('testclass', $this->node->classes);
 326      }
 327  
 328      public function test_module_extends_navigation() {
 329          $node = new exposed_global_navigation();
 330          // Create an initial tree structure to work with.
 331          $cat1 = $node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
 332          $cat2 = $node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
 333          $cat3 = $node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
 334          $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
 335          $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
 336          $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
 337          $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
 338          $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
 339          $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
 340          $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
 341          $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
 342          $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
 343          $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
 344          $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
 345          $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
 346          $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
 347          $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
 348          $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
 349  
 350          $this->assertTrue($node->exposed_module_extends_navigation('data'));
 351          $this->assertFalse($node->exposed_module_extends_navigation('test1'));
 352      }
 353  
 354      public function test_navbar_prepend_and_add() {
 355          global $PAGE;
 356          // Unfortunate hack needed because people use global $PAGE around the place.
 357          $PAGE->set_url('/');
 358  
 359          // We need to reset after this test because we using the generator.
 360          $this->resetAfterTest();
 361  
 362          $generator = self::getDataGenerator();
 363          $cat1 = $generator->create_category();
 364          $cat2 = $generator->create_category(array('parent' => $cat1->id));
 365          $course = $generator->create_course(array('category' => $cat2->id));
 366  
 367          $page = new \moodle_page();
 368          $page->set_course($course);
 369          $page->set_url(new \moodle_url('/course/view.php', array('id' => $course->id)));
 370          $page->navbar->prepend('test 1');
 371          $page->navbar->prepend('test 2');
 372          $page->navbar->add('test 3');
 373          $page->navbar->add('test 4');
 374  
 375          $items = $page->navbar->get_items();
 376          foreach ($items as $item) {
 377              $this->assertInstanceOf('navigation_node', $item);
 378          }
 379  
 380          $i = 0;
 381          $this->assertSame('test 1', $items[$i++]->text);
 382          $this->assertSame('test 2', $items[$i++]->text);
 383          $this->assertSame('home', $items[$i++]->key);
 384          $this->assertSame('courses', $items[$i++]->key);
 385          $this->assertSame($cat1->id, $items[$i++]->key);
 386          $this->assertSame($cat2->id, $items[$i++]->key);
 387          $this->assertSame($course->id, $items[$i++]->key);
 388          $this->assertSame('test 3', $items[$i++]->text);
 389          $this->assertSame('test 4', $items[$i++]->text);
 390  
 391          return $page;
 392      }
 393  
 394      /**
 395       * @depends test_navbar_prepend_and_add
 396       * @param $node
 397       */
 398      public function test_navbar_has_items(\moodle_page $page) {
 399          $this->resetAfterTest();
 400  
 401          $this->assertTrue($page->navbar->has_items());
 402      }
 403  
 404      public function test_cache__get() {
 405          $cache = new navigation_cache('unittest_nav');
 406          $cache->anysetvariable = true;
 407  
 408          $this->assertTrue($cache->anysetvariable);
 409          $this->assertEquals($cache->notasetvariable, null);
 410      }
 411  
 412      public function test_cache__set() {
 413          $cache = new navigation_cache('unittest_nav');
 414          $cache->anysetvariable = true;
 415  
 416          $cache->myname = 'Sam Hemelryk';
 417          $this->assertTrue($cache->cached('myname'));
 418          $this->assertSame('Sam Hemelryk', $cache->myname);
 419      }
 420  
 421      public function test_cache_cached() {
 422          $cache = new navigation_cache('unittest_nav');
 423          $cache->anysetvariable = true;
 424  
 425          $this->assertTrue($cache->cached('anysetvariable'));
 426          $this->assertFalse($cache->cached('notasetvariable'));
 427      }
 428  
 429      public function test_cache_clear() {
 430          $cache = new navigation_cache('unittest_nav');
 431          $cache->anysetvariable = true;
 432  
 433          $cache = clone($cache);
 434          $this->assertTrue($cache->cached('anysetvariable'));
 435          $cache->clear();
 436          $this->assertFalse($cache->cached('anysetvariable'));
 437      }
 438  
 439      public function test_cache_set() {
 440          $cache = new navigation_cache('unittest_nav');
 441          $cache->anysetvariable = true;
 442  
 443          $cache->set('software', 'Moodle');
 444          $this->assertTrue($cache->cached('software'));
 445          $this->assertEquals($cache->software, 'Moodle');
 446      }
 447  
 448      public function test_setting___construct() {
 449          global $PAGE, $SITE;
 450  
 451          $this->resetAfterTest(false);
 452  
 453          $PAGE->set_url('/');
 454          $PAGE->set_course($SITE);
 455  
 456          $node = new exposed_settings_navigation();
 457  
 458          return $node;
 459      }
 460  
 461      /**
 462       * @depends test_setting___construct
 463       * @param mixed $node
 464       * @return mixed
 465       */
 466      public function test_setting__initialise($node) {
 467          $this->resetAfterTest(false);
 468  
 469          $node->initialise();
 470          $this->assertEquals($node->id, 'settingsnav');
 471  
 472          return $node;
 473      }
 474  
 475      /**
 476       * Test that users with the correct permissions can view the preferences page.
 477       */
 478      public function test_can_view_user_preferences() {
 479          global $PAGE, $DB, $SITE;
 480          $this->resetAfterTest();
 481  
 482          $persontoview = $this->getDataGenerator()->create_user();
 483          $persondoingtheviewing = $this->getDataGenerator()->create_user();
 484  
 485          $PAGE->set_url('/');
 486          $PAGE->set_course($SITE);
 487  
 488          // Check that a standard user can not view the preferences page.
 489          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 490          $this->getDataGenerator()->role_assign($studentrole->id, $persondoingtheviewing->id);
 491          $this->setUser($persondoingtheviewing);
 492          $settingsnav = new exposed_settings_navigation();
 493          $settingsnav->initialise();
 494          $settingsnav->extend_for_user($persontoview->id);
 495          $this->assertFalse($settingsnav->can_view_user_preferences($persontoview->id));
 496  
 497          // Set persondoingtheviewing as a manager.
 498          $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
 499          $this->getDataGenerator()->role_assign($managerrole->id, $persondoingtheviewing->id);
 500          $settingsnav = new exposed_settings_navigation();
 501          $settingsnav->initialise();
 502          $settingsnav->extend_for_user($persontoview->id);
 503          $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
 504  
 505          // Check that the admin can view the preferences page.
 506          $this->setAdminUser();
 507          $settingsnav = new exposed_settings_navigation();
 508          $settingsnav->initialise();
 509          $settingsnav->extend_for_user($persontoview->id);
 510          $preferencenode = $settingsnav->find('userviewingsettings' . $persontoview->id, null);
 511          $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
 512      }
 513  
 514      /**
 515       * @depends test_setting__initialise
 516       * @param mixed $node
 517       * @return mixed
 518       */
 519      public function test_setting_in_alternative_role($node) {
 520          $this->resetAfterTest();
 521  
 522          $this->assertFalse($node->exposed_in_alternative_role());
 523      }
 524  
 525  
 526      public function test_navigation_node_collection_remove_with_no_type() {
 527          $navigationnodecollection = new navigation_node_collection();
 528          $this->setup_node();
 529          $this->node->key = 100;
 530  
 531          // Test it's empty
 532          $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
 533  
 534          // Add a node
 535          $navigationnodecollection->add($this->node);
 536  
 537          // Test it's not empty
 538          $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
 539  
 540          // Remove a node - passing key only!
 541          $this->assertTrue($navigationnodecollection->remove(100));
 542  
 543          // Test it's empty again!
 544          $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
 545      }
 546  
 547      public function test_navigation_node_collection_remove_with_type() {
 548          $navigationnodecollection = new navigation_node_collection();
 549          $this->setup_node();
 550          $this->node->key = 100;
 551  
 552          // Test it's empty
 553          $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
 554  
 555          // Add a node
 556          $navigationnodecollection->add($this->node);
 557  
 558          // Test it's not empty
 559          $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
 560  
 561          // Remove a node - passing type
 562          $this->assertTrue($navigationnodecollection->remove(100, 1));
 563  
 564          // Test it's empty again!
 565          $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
 566      }
 567  }
 568  
 569  
 570  /**
 571   * This is a dummy object that allows us to call protected methods within the
 572   * global navigation class by prefixing the methods with `exposed_`
 573   */
 574  class exposed_global_navigation extends global_navigation {
 575      protected $exposedkey = 'exposed_';
 576      public function __construct(\moodle_page $page=null) {
 577          global $PAGE;
 578          if ($page === null) {
 579              $page = $PAGE;
 580          }
 581          parent::__construct($page);
 582          $this->cache = new navigation_cache('unittest_nav');
 583      }
 584      public function __call($method, $arguments) {
 585          if (strpos($method, $this->exposedkey) !== false) {
 586              $method = substr($method, strlen($this->exposedkey));
 587          }
 588          if (method_exists($this, $method)) {
 589              return call_user_func_array(array($this, $method), $arguments);
 590          }
 591          throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
 592      }
 593      public function set_initialised() {
 594          $this->initialised = true;
 595      }
 596  }
 597  
 598  
 599  class mock_initialise_global_navigation extends global_navigation {
 600  
 601      protected static $count = 1;
 602  
 603      public function load_for_category() {
 604          $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
 605          self::$count++;
 606          return 0;
 607      }
 608  
 609      public function load_for_course() {
 610          $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
 611          self::$count++;
 612          return 0;
 613      }
 614  
 615      public function load_for_activity() {
 616          $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
 617          self::$count++;
 618          return 0;
 619      }
 620  
 621      public function load_for_user($user=null, $forceforcontext=false) {
 622          $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
 623          self::$count++;
 624          return 0;
 625      }
 626  }
 627  
 628  /**
 629   * This is a dummy object that allows us to call protected methods within the
 630   * global navigation class by prefixing the methods with `exposed_`.
 631   */
 632  class exposed_navbar extends navbar {
 633      protected $exposedkey = 'exposed_';
 634      public function __construct(\moodle_page $page) {
 635          parent::__construct($page);
 636          $this->cache = new navigation_cache('unittest_nav');
 637      }
 638      public function __call($method, $arguments) {
 639          if (strpos($method, $this->exposedkey) !== false) {
 640              $method = substr($method, strlen($this->exposedkey));
 641          }
 642          if (method_exists($this, $method)) {
 643              return call_user_func_array(array($this, $method), $arguments);
 644          }
 645          throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
 646      }
 647  }
 648  
 649  class navigation_exposed_moodle_page extends \moodle_page {
 650      public function set_navigation(navigation_node $node) {
 651          $this->_navigation = $node;
 652      }
 653  }
 654  
 655  /**
 656   * This is a dummy object that allows us to call protected methods within the
 657   * global navigation class by prefixing the methods with `exposed_`.
 658   */
 659  class exposed_settings_navigation extends settings_navigation {
 660      protected $exposedkey = 'exposed_';
 661      public function __construct() {
 662          global $PAGE;
 663          parent::__construct($PAGE);
 664          $this->cache = new navigation_cache('unittest_nav');
 665      }
 666      public function __call($method, $arguments) {
 667          if (strpos($method, $this->exposedkey) !== false) {
 668              $method = substr($method, strlen($this->exposedkey));
 669          }
 670          if (method_exists($this, $method)) {
 671              return call_user_func_array(array($this, $method), $arguments);
 672          }
 673          throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
 674      }
 675  }