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