weekly release 2.7dev
[moodle.git] / lib / tests / admintree_test.php
CommitLineData
77043fd6 1<?php
77043fd6
DM
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 those parts of adminlib.php that implement the admin tree
19 * functionality.
20 *
21 * @package core
05fb92e9 22 * @category phpunit
77043fd6
DM
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->libdir.'/adminlib.php');
31
32/**
33 * Provides the unit tests for admin tree functionality.
34 */
05fb92e9 35class core_admintree_testcase extends advanced_testcase {
77043fd6
DM
36
37 /**
05fb92e9 38 * Adding nodes into the admin tree.
77043fd6
DM
39 */
40 public function test_add_nodes() {
41
42 $tree = new admin_root(true);
43 $tree->add('root', $one = new admin_category('one', 'One'));
44 $tree->add('root', new admin_category('three', 'Three'));
45 $tree->add('one', new admin_category('one-one', 'One-one'));
46 $tree->add('one', new admin_category('one-three', 'One-three'));
47
48 // Check the order of nodes in the root.
49 $map = array();
50 foreach ($tree->children as $child) {
51 $map[] = $child->name;
52 }
53 $this->assertEquals(array('one', 'three'), $map);
54
55 // Insert a node into the middle.
56 $tree->add('root', new admin_category('two', 'Two'), 'three');
57 $map = array();
58 foreach ($tree->children as $child) {
59 $map[] = $child->name;
60 }
61 $this->assertEquals(array('one', 'two', 'three'), $map);
62
63 // Non-existing sibling.
64 $tree->add('root', new admin_category('four', 'Four'), 'five');
65 $this->assertDebuggingCalled('Sibling five not found', DEBUG_DEVELOPER);
66
67 $tree->add('root', new admin_category('five', 'Five'));
68 $map = array();
69 foreach ($tree->children as $child) {
70 $map[] = $child->name;
71 }
72 $this->assertEquals(array('one', 'two', 'three', 'four', 'five'), $map);
73
05fb92e9 74 // Insert a node into the middle of the subcategory.
77043fd6
DM
75 $tree->add('one', new admin_category('one-two', 'One-two'), 'one-three');
76 $map = array();
77 foreach ($one->children as $child) {
78 $map[] = $child->name;
79 }
80 $this->assertEquals(array('one-one', 'one-two', 'one-three'), $map);
81
82 // Check just siblings, not parents or children.
83 $tree->add('one', new admin_category('one-four', 'One-four'), 'one');
84 $this->assertDebuggingCalled('Sibling one not found', DEBUG_DEVELOPER);
85
86 $tree->add('root', new admin_category('six', 'Six'), 'one-two');
87 $this->assertDebuggingCalled('Sibling one-two not found', DEBUG_DEVELOPER);
88
89 // Me! Me! I wanna be first!
90 $tree->add('root', new admin_externalpage('zero', 'Zero', 'http://foo.bar'), 'one');
91 $map = array();
92 foreach ($tree->children as $child) {
93 $map[] = $child->name;
94 }
95 $this->assertEquals(array('zero', 'one', 'two', 'three', 'four', 'five', 'six'), $map);
96 }
97
98 /**
99 * @expectedException coding_exception
100 */
101 public function test_add_nodes_before_invalid1() {
102 $tree = new admin_root(true);
103 $tree->add('root', new admin_externalpage('foo', 'Foo', 'http://foo.bar'), array('moodle:site/config'));
104 }
105
106 /**
107 * @expectedException coding_exception
108 */
109 public function test_add_nodes_before_invalid2() {
110 $tree = new admin_root(true);
111 $tree->add('root', new admin_category('bar', 'Bar'), '');
112 }
113}