[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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


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