MDL-38497 Add unit tests for the admin tree behaviour
[moodle.git] / lib / tests / admintree_test.php
CommitLineData
77043fd6
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Unit tests for those parts of adminlib.php that implement the admin tree
20 * functionality.
21 *
22 * @package core
23 * @category test
24 * @copyright 2013 David Mudrak <david@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->libdir.'/adminlib.php');
32
33/**
34 * Provides the unit tests for admin tree functionality.
35 */
36class admintree_testcase extends advanced_testcase {
37
38 /**
39 * Adding nodes into the admin tree
40 */
41 public function test_add_nodes() {
42
43 $tree = new admin_root(true);
44 $tree->add('root', $one = new admin_category('one', 'One'));
45 $tree->add('root', new admin_category('three', 'Three'));
46 $tree->add('one', new admin_category('one-one', 'One-one'));
47 $tree->add('one', new admin_category('one-three', 'One-three'));
48
49 // Check the order of nodes in the root.
50 $map = array();
51 foreach ($tree->children as $child) {
52 $map[] = $child->name;
53 }
54 $this->assertEquals(array('one', 'three'), $map);
55
56 // Insert a node into the middle.
57 $tree->add('root', new admin_category('two', 'Two'), 'three');
58 $map = array();
59 foreach ($tree->children as $child) {
60 $map[] = $child->name;
61 }
62 $this->assertEquals(array('one', 'two', 'three'), $map);
63
64 // Non-existing sibling.
65 $tree->add('root', new admin_category('four', 'Four'), 'five');
66 $this->assertDebuggingCalled('Sibling five not found', DEBUG_DEVELOPER);
67
68 $tree->add('root', new admin_category('five', 'Five'));
69 $map = array();
70 foreach ($tree->children as $child) {
71 $map[] = $child->name;
72 }
73 $this->assertEquals(array('one', 'two', 'three', 'four', 'five'), $map);
74
75 // Insert a node into the middle of the subcategory
76 $tree->add('one', new admin_category('one-two', 'One-two'), 'one-three');
77 $map = array();
78 foreach ($one->children as $child) {
79 $map[] = $child->name;
80 }
81 $this->assertEquals(array('one-one', 'one-two', 'one-three'), $map);
82
83 // Check just siblings, not parents or children.
84 $tree->add('one', new admin_category('one-four', 'One-four'), 'one');
85 $this->assertDebuggingCalled('Sibling one not found', DEBUG_DEVELOPER);
86
87 $tree->add('root', new admin_category('six', 'Six'), 'one-two');
88 $this->assertDebuggingCalled('Sibling one-two not found', DEBUG_DEVELOPER);
89
90 // Me! Me! I wanna be first!
91 $tree->add('root', new admin_externalpage('zero', 'Zero', 'http://foo.bar'), 'one');
92 $map = array();
93 foreach ($tree->children as $child) {
94 $map[] = $child->name;
95 }
96 $this->assertEquals(array('zero', 'one', 'two', 'three', 'four', 'five', 'six'), $map);
97 }
98
99 /**
100 * @expectedException coding_exception
101 */
102 public function test_add_nodes_before_invalid1() {
103 $tree = new admin_root(true);
104 $tree->add('root', new admin_externalpage('foo', 'Foo', 'http://foo.bar'), array('moodle:site/config'));
105 }
106
107 /**
108 * @expectedException coding_exception
109 */
110 public function test_add_nodes_before_invalid2() {
111 $tree = new admin_root(true);
112 $tree->add('root', new admin_category('bar', 'Bar'), '');
113 }
114}