Commit | Line | Data |
---|---|---|
7d2a0492 | 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 lib/navigationlib.php | |
20 | * | |
21 | * @package moodlecore | |
22 | * @copyright 2009 Sam Hemelryk | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5) | |
24 | */ | |
25 | ||
26 | if (!defined('MOODLE_INTERNAL')) { | |
27 | die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page | |
28 | } | |
29 | require_once($CFG->libdir . '/navigationlib.php'); | |
30 | ||
31 | class navigation_node_test extends UnitTestCase { | |
32 | protected $tree; | |
33 | public static $includecoverage = array('./lib/navigationlib.php'); | |
34 | public static $excludecoverage = array(); | |
95b97515 | 35 | protected $fakeproperties = array( |
36 | 'text' => 'text', | |
37 | 'shorttext' => 'A very silly extra long short text string, more than 25 characters', | |
38 | 'key' => 'key', | |
39 | 'type' => 'navigation_node::TYPE_COURSE', | |
40 | 'action' => 'http://www.moodle.org/'); | |
c705a24e | 41 | protected $activeurl = null; |
42 | protected $inactivenode = null; | |
7d2a0492 | 43 | |
3406acde SH |
44 | /** |
45 | * @var navigation_node | |
46 | */ | |
47 | public $node; | |
48 | ||
7d2a0492 | 49 | public function setUp() { |
c705a24e | 50 | global $CFG, $PAGE; |
7d2a0492 | 51 | parent::setUp(); |
c705a24e | 52 | |
53 | $this->activeurl = $PAGE->url; | |
460232f3 SH |
54 | navigation_node::override_active_url($this->activeurl); |
55 | ||
c705a24e | 56 | $this->inactiveurl = new moodle_url('http://www.moodle.com/'); |
57 | $this->fakeproperties['action'] = $this->inactiveurl; | |
58 | ||
7d2a0492 | 59 | $this->node = new navigation_node('Test Node'); |
60 | $this->node->type = navigation_node::TYPE_SYSTEM; | |
3406acde SH |
61 | $demo1 = $this->node->add('demo1', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', '')); |
62 | $demo2 = $this->node->add('demo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', '')); | |
63 | $demo3 = $this->node->add('demo3', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3',new pix_icon('i/course', '')); | |
64 | $demo4 = $demo3->add('demo4', $this->inactiveurl,navigation_node::TYPE_COURSE, null, 'demo4', new pix_icon('i/course', '')); | |
65 | $demo5 = $demo3->add('demo5', $this->activeurl, navigation_node::TYPE_COURSE, null, 'demo5',new pix_icon('i/course', '')); | |
66 | $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active(); | |
67 | $hiddendemo1 = $this->node->add('hiddendemo1', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', '')); | |
68 | $hiddendemo1->hidden = true; | |
69 | $hiddendemo1->add('hiddendemo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';; | |
70 | $hiddendemo1->add('hiddendemo3', $this->inactiveurl, navigation_node::TYPE_COURSE,null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false; | |
7d2a0492 | 71 | } |
8926f844 | 72 | |
7d2a0492 | 73 | public function test___construct() { |
74 | global $CFG; | |
95b97515 | 75 | $node = new navigation_node($this->fakeproperties); |
76 | $this->assertEqual($node->text, $this->fakeproperties['text']); | |
77 | $this->assertEqual($node->title, $this->fakeproperties['text']); | |
78 | $this->assertTrue(strpos($this->fakeproperties['shorttext'], substr($node->shorttext,0, -3))===0); | |
79 | $this->assertEqual($node->key, $this->fakeproperties['key']); | |
80 | $this->assertEqual($node->type, $this->fakeproperties['type']); | |
81 | $this->assertEqual($node->action, $this->fakeproperties['action']); | |
82 | } | |
7d2a0492 | 83 | public function test_add() { |
84 | global $CFG; | |
85 | // Add a node with all args set | |
3406acde | 86 | $node1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',new pix_icon('i/course', '')); |
7d2a0492 | 87 | // Add a node with the minimum args required |
3406acde SH |
88 | $node2 = $this->node->add('test_add_2',null, navigation_node::TYPE_CUSTOM,'testadd2'); |
89 | $node3 = $this->node->add(str_repeat('moodle ', 15),str_repeat('moodle', 15)); | |
90 | ||
91 | $this->assertIsA($node1, 'navigation_node'); | |
92 | $this->assertIsA($node2, 'navigation_node'); | |
93 | $this->assertIsA($node3, 'navigation_node'); | |
94 | ||
95 | $this->assertReference($node1, $this->node->get('key')); | |
96 | $this->assertReference($node2, $this->node->get($node2->key)); | |
97 | $this->assertReference($node2, $this->node->get($node2->key, $node2->type)); | |
98 | $this->assertReference($node3, $this->node->get($node3->key, $node3->type)); | |
7d2a0492 | 99 | } |
100 | ||
101 | public function test_add_class() { | |
102 | $node = $this->node->get('demo1'); | |
103 | $this->assertIsA($node, 'navigation_node'); | |
104 | if ($node !== false) { | |
105 | $node->add_class('myclass'); | |
106 | $classes = $node->classes; | |
107 | $this->assertTrue(in_array('myclass', $classes)); | |
108 | } | |
109 | } | |
110 | ||
7d2a0492 | 111 | |
112 | public function test_check_if_active() { | |
7d2a0492 | 113 | // First test the string urls |
7d2a0492 | 114 | // demo1 -> action is http://www.moodle.org/, thus should be true |
3406acde SH |
115 | $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE); |
116 | if ($this->assertIsA($demo5, 'navigation_node')) { | |
117 | $this->assertTrue($demo5->check_if_active()); | |
118 | } | |
119 | ||
7d2a0492 | 120 | // demo2 -> action is http://www.moodle.com/, thus should be false |
3406acde SH |
121 | $demo2 = $this->node->get('demo2'); |
122 | if ($this->assertIsA($demo2, 'navigation_node')) { | |
123 | $this->assertFalse($demo2->check_if_active()); | |
124 | } | |
7d2a0492 | 125 | } |
126 | ||
127 | public function test_contains_active_node() { | |
128 | // demo5, and activity1 were set to active during setup | |
129 | // Should be true as it contains all nodes | |
130 | $this->assertTrue($this->node->contains_active_node()); | |
131 | // Should be true as demo5 is a child of demo3 | |
132 | $this->assertTrue($this->node->get('demo3')->contains_active_node()); | |
133 | // Obviously duff | |
134 | $this->assertFalse($this->node->get('demo1')->contains_active_node()); | |
135 | // Should be true as demo5 contains activity1 | |
136 | $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node()); | |
3406acde SH |
137 | // Should be true activity1 is the active node |
138 | $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node()); | |
7d2a0492 | 139 | // Obviously duff |
140 | $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node()); | |
141 | } | |
142 | ||
7d2a0492 | 143 | public function test_find_active_node() { |
144 | $activenode1 = $this->node->find_active_node(); | |
3406acde SH |
145 | $activenode2 = $this->node->get('demo1')->find_active_node(); |
146 | ||
147 | if ($this->assertIsA($activenode1, 'navigation_node')) { | |
148 | $this->assertReference($activenode1, $this->node->get('demo3')->get('demo5')->get('activity1')); | |
7d2a0492 | 149 | } |
3406acde SH |
150 | |
151 | $this->assertNotA($activenode2, 'navigation_node'); | |
7d2a0492 | 152 | } |
153 | ||
3406acde SH |
154 | public function test_find() { |
155 | $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE); | |
156 | $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE); | |
157 | $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY); | |
158 | $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE); | |
7d2a0492 | 159 | $this->assertIsA($node1, 'navigation_node'); |
160 | $this->assertIsA($node2, 'navigation_node'); | |
161 | $this->assertNotA($node3, 'navigation_node'); | |
162 | $this->assertNotA($node4, 'navigation_node'); | |
163 | } | |
164 | ||
7d2a0492 | 165 | public function test_find_expandable() { |
166 | $expandable = array(); | |
167 | $this->node->find_expandable($expandable); | |
168 | $this->assertEqual(count($expandable), 5); | |
169 | if (count($expandable) === 5) { | |
170 | $name = $expandable[0]['branchid']; | |
171 | $name .= $expandable[1]['branchid']; | |
172 | $name .= $expandable[2]['branchid']; | |
173 | $name .= $expandable[3]['branchid']; | |
174 | $name .= $expandable[4]['branchid']; | |
3406acde | 175 | $this->assertEqual($name, 'demo1demo2demo4hiddendemo2hiddendemo3'); |
7d2a0492 | 176 | } |
177 | } | |
178 | ||
179 | public function test_get() { | |
180 | $node1 = $this->node->get('demo1'); // Exists | |
181 | $node2 = $this->node->get('demo4'); // Doesn't exist for this node | |
182 | $node3 = $this->node->get('demo0'); // Doesn't exist at all | |
183 | $node4 = $this->node->get(false); // Sometimes occurs in nature code | |
184 | $this->assertIsA($node1, 'navigation_node'); | |
185 | $this->assertFalse($node2); | |
186 | $this->assertFalse($node3); | |
187 | $this->assertFalse($node4); | |
188 | } | |
189 | ||
7d2a0492 | 190 | public function test_get_css_type() { |
191 | $csstype1 = $this->node->get('demo3')->get_css_type(); | |
192 | $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type(); | |
193 | $this->node->get('demo3')->get('demo5')->type = 1000; | |
194 | $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type(); | |
195 | $this->assertEqual($csstype1, 'type_category'); | |
196 | $this->assertEqual($csstype2, 'type_course'); | |
197 | $this->assertEqual($csstype3, 'type_unknown'); | |
198 | } | |
199 | ||
200 | public function test_make_active() { | |
201 | global $CFG; | |
3406acde SH |
202 | $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1'); |
203 | $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2'); | |
204 | $node1->make_active(); | |
205 | $this->node->get('anode2')->make_active(); | |
206 | $this->assertTrue($node1->isactive); | |
207 | $this->assertTrue($this->node->get('anode2')->isactive); | |
208 | } | |
209 | public function test_remove() { | |
210 | $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1'); | |
211 | $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2'); | |
212 | $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3'); | |
213 | ||
214 | $this->assertIsA($remove1, 'navigation_node'); | |
215 | $this->assertIsA($remove2, 'navigation_node'); | |
216 | $this->assertIsA($remove3, 'navigation_node'); | |
217 | ||
7d2a0492 | 218 | $this->assertIsA($this->node->get('remove1'), 'navigation_node'); |
3406acde SH |
219 | $this->assertIsA($this->node->get('remove2'), 'navigation_node'); |
220 | $this->assertIsA($remove2->get('remove3'), 'navigation_node'); | |
221 | ||
222 | $this->assertTrue($remove1->remove()); | |
223 | $this->assertTrue($this->node->get('remove2')->remove()); | |
224 | $this->assertTrue($remove2->get('remove3')->remove()); | |
225 | ||
226 | $this->assertFalse($this->node->get('remove1')); | |
227 | $this->assertFalse($this->node->get('remove2')); | |
7d2a0492 | 228 | } |
229 | public function test_remove_class() { | |
230 | $this->node->add_class('testclass'); | |
231 | $this->assertTrue($this->node->remove_class('testclass')); | |
232 | $this->assertFalse(in_array('testclass', $this->node->classes)); | |
233 | } | |
7d2a0492 | 234 | } |
235 | ||
236 | /** | |
237 | * This is a dummy object that allows us to call protected methods within the | |
238 | * global navigation class by prefixing the methods with `exposed_` | |
239 | */ | |
240 | class exposed_global_navigation extends global_navigation { | |
241 | protected $exposedkey = 'exposed_'; | |
242 | function __construct() { | |
3406acde SH |
243 | global $PAGE; |
244 | parent::__construct($PAGE); | |
7d2a0492 | 245 | $this->cache = new navigation_cache('simpletest_nav'); |
246 | } | |
247 | function __call($method, $arguments) { | |
248 | if (strpos($method,$this->exposedkey) !== false) { | |
249 | $method = substr($method, strlen($this->exposedkey)); | |
250 | } | |
251 | if (method_exists($this, $method)) { | |
252 | return call_user_func_array(array($this, $method), $arguments); | |
253 | } | |
254 | throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); | |
255 | } | |
256 | } | |
257 | ||
95b97515 | 258 | class mock_initialise_global_navigation extends global_navigation { |
259 | ||
260 | static $count = 1; | |
261 | ||
262 | public function load_for_category() { | |
263 | $this->add('load_for_category', null, null, null, 'initcall'.self::$count); | |
264 | self::$count++; | |
265 | return 0; | |
266 | } | |
267 | ||
268 | public function load_for_course() { | |
269 | $this->add('load_for_course', null, null, null, 'initcall'.self::$count); | |
270 | self::$count++; | |
271 | return 0; | |
272 | } | |
273 | ||
274 | public function load_for_activity() { | |
275 | $this->add('load_for_activity', null, null, null, 'initcall'.self::$count); | |
276 | self::$count++; | |
277 | return 0; | |
278 | } | |
279 | ||
280 | public function load_for_user() { | |
281 | $this->add('load_for_user', null, null, null, 'initcall'.self::$count); | |
282 | self::$count++; | |
283 | return 0; | |
284 | } | |
285 | } | |
286 | ||
7d2a0492 | 287 | class global_navigation_test extends UnitTestCase { |
288 | /** | |
289 | * @var global_navigation | |
290 | */ | |
291 | public $node; | |
292 | protected $cache; | |
3b7a763c | 293 | protected $modinfo5 = 'O:6:"object":6:{s:8:"courseid";s:1:"5";s:6:"userid";s:1:"2";s:8:"sections";a:1:{i:0;a:1:{i:0;s:3:"288";}}s:3:"cms";a:1:{i:288;O:6:"object":17:{s:2:"id";s:3:"288";s:8:"instance";s:2:"19";s:6:"course";s:1:"5";s:7:"modname";s:5:"forum";s:4:"name";s:10:"News forum";s:7:"visible";s:1:"1";s:10:"sectionnum";s:1:"0";s:9:"groupmode";s:1:"0";s:10:"groupingid";s:1:"0";s:16:"groupmembersonly";s:1:"0";s:6:"indent";s:1:"0";s:10:"completion";s:1:"0";s:5:"extra";s:0:"";s:4:"icon";s:0:"";s:11:"uservisible";b:1;s:9:"modplural";s:6:"Forums";s:9:"available";b:1;}}s:9:"instances";a:1:{s:5:"forum";a:1:{i:19;R:8;}}s:6:"groups";N;}'; |
294 | protected $coursesections5 = 'a:5:{i:0;O:8:"stdClass":6:{s:7:"section";s:1:"0";s:2:"id";s:2:"14";s:6:"course";s:1:"5";s:7:"summary";N;s:8:"sequence";s:3:"288";s:7:"visible";s:1:"1";}i:1;O:8:"stdClass":6:{s:7:"section";s:1:"1";s:2:"id";s:2:"97";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:2;O:8:"stdClass":6:{s:7:"section";s:1:"2";s:2:"id";s:2:"98";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:3;O:8:"stdClass":6:{s:7:"section";s:1:"3";s:2:"id";s:2:"99";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:4;O:8:"stdClass":6:{s:7:"section";s:1:"4";s:2:"id";s:3:"100";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}}'; | |
7d2a0492 | 295 | public static $includecoverage = array('./lib/navigationlib.php'); |
296 | public static $excludecoverage = array(); | |
8926f844 | 297 | |
7d2a0492 | 298 | public function setUp() { |
299 | $this->cache = new navigation_cache('simpletest_nav'); | |
300 | $this->node = new exposed_global_navigation(); | |
301 | // Create an initial tree structure to work with | |
3406acde SH |
302 | $cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1'); |
303 | $cat2 = $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2'); | |
304 | $cat3 = $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3'); | |
305 | $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1'); | |
306 | $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2'); | |
307 | $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3'); | |
308 | $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1'); | |
309 | $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2'); | |
310 | $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3'); | |
311 | $section1 = $course2->add('section 1', null, navigation_node::TYPE_COURSE, null, 'sec1'); | |
312 | $section2 = $course2->add('section 2', null, navigation_node::TYPE_COURSE, null, 'sec2'); | |
313 | $section3 = $course2->add('section 3', null, navigation_node::TYPE_COURSE, null, 'sec3'); | |
314 | $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1'); | |
315 | $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2'); | |
316 | $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3'); | |
317 | $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1'); | |
318 | $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2'); | |
319 | $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3'); | |
7d2a0492 | 320 | |
321 | $this->cache->clear(); | |
3b7a763c | 322 | $this->cache->modinfo5 = unserialize($this->modinfo5); |
323 | $this->cache->coursesections5 = unserialize($this->coursesections5); | |
7d2a0492 | 324 | $this->cache->canviewhiddenactivities = true; |
325 | $this->cache->canviewhiddensections = true; | |
326 | $this->cache->canviewhiddencourses = true; | |
3406acde | 327 | $sub2->add('Test Course 5', new moodle_url('http://moodle.org'),navigation_node::TYPE_COURSE,null,'5'); |
7d2a0492 | 328 | } |
3406acde SH |
329 | public function test_load_generic_course_sections() { |
330 | $coursenode = $this->node->find('5', navigation_node::TYPE_COURSE); | |
7d2a0492 | 331 | $course = new stdClass; |
332 | $course->id = '5'; | |
3b7a763c | 333 | $course->numsections = 10; |
334 | $course->modinfo = $this->modinfo5; | |
3406acde SH |
335 | $this->node->load_generic_course_sections($course, $coursenode, 'topic', 'topic'); |
336 | $this->assertEqual($coursenode->children->count(),4); | |
7d2a0492 | 337 | } |
338 | public function test_format_display_course_content() { | |
339 | $this->assertTrue($this->node->exposed_format_display_course_content('topic')); | |
340 | $this->assertFalse($this->node->exposed_format_display_course_content('scorm')); | |
341 | $this->assertTrue($this->node->exposed_format_display_course_content('dummy')); | |
342 | } | |
7d2a0492 | 343 | public function test_load_section_activities() { |
3406acde | 344 | $node = $this->node->find('5', navigation_node::TYPE_COURSE); |
7d2a0492 | 345 | $course = new stdClass; |
346 | $course->id = '5'; | |
3b7a763c | 347 | $course->numsections = 10; |
3406acde | 348 | $section = $node->add('Test Section 1', null, navigation_node::TYPE_SECTION, null, $this->cache->coursesections5[1]->id); |
7d2a0492 | 349 | $modinfo = $this->cache->modinfo5; |
350 | $modinfo->sections[1] = array(289, 290); | |
351 | $modinfo->cms[289] = clone($modinfo->cms[288]); | |
352 | $modinfo->cms[289]->id = 289; | |
353 | $modinfo->cms[289]->sectionnum = 1; | |
354 | $modinfo->cms[290]->modname = 'forum'; | |
355 | $modinfo->cms[289]->instance = 20; | |
356 | $modinfo->cms[290] = clone($modinfo->cms[288]); | |
357 | $modinfo->cms[290]->id = 290; | |
358 | $modinfo->cms[290]->modname = 'resource'; | |
359 | $modinfo->cms[290]->sectionnum = 1; | |
360 | $modinfo->cms[290]->instance = 21; | |
361 | $modinfo->instances['forum'][20] = clone($modinfo->instances['forum'][19]); | |
362 | $modinfo->instances['forum'][20]->id = 20; | |
363 | $modinfo->instances['resource'] = array(); | |
364 | $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]); | |
365 | $modinfo->instances['resource'][21]->id = 21; | |
366 | $this->cache->modinfo5 = $modinfo; | |
3b7a763c | 367 | $course->modinfo = serialize($modinfo); |
3406acde SH |
368 | $activities = $this->node->exposed_load_section_activities($section, 1, $modinfo); |
369 | foreach ($activities as $activity) { | |
370 | if ($this->assertIsA($activity, 'navigation_node')) { | |
371 | $this->assertEqual($activity->type, navigation_node::TYPE_ACTIVITY); | |
372 | $this->assertReference($activity, $section->get($activity->key)); | |
373 | } | |
374 | } | |
7d2a0492 | 375 | } |
376 | public function test_module_extends_navigation() { | |
377 | $this->cache->test1_extends_navigation = true; | |
378 | $this->cache->test2_extends_navigation = false; | |
379 | $this->assertTrue($this->node->exposed_module_extends_navigation('forum')); | |
380 | $this->assertTrue($this->node->exposed_module_extends_navigation('test1')); | |
381 | $this->assertFalse($this->node->exposed_module_extends_navigation('test2')); | |
382 | $this->assertFalse($this->node->exposed_module_extends_navigation('test3')); | |
383 | } | |
384 | } | |
385 | ||
386 | /** | |
387 | * This is a dummy object that allows us to call protected methods within the | |
388 | * global navigation class by prefixing the methods with `exposed_` | |
389 | */ | |
390 | class exposed_navbar extends navbar { | |
391 | protected $exposedkey = 'exposed_'; | |
392 | function __construct() { | |
393 | global $PAGE; | |
394 | parent::__construct($PAGE); | |
395 | $this->cache = new navigation_cache('simpletest_nav'); | |
396 | } | |
397 | function __call($method, $arguments) { | |
398 | if (strpos($method,$this->exposedkey) !== false) { | |
399 | $method = substr($method, strlen($this->exposedkey)); | |
400 | } | |
401 | if (method_exists($this, $method)) { | |
402 | return call_user_func_array(array($this, $method), $arguments); | |
403 | } | |
404 | throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); | |
405 | } | |
406 | } | |
407 | ||
408 | class navbar_test extends UnitTestCase { | |
409 | protected $node; | |
410 | protected $oldnav; | |
411 | ||
412 | public static $includecoverage = array('./lib/navigationlib.php'); | |
413 | public static $excludecoverage = array(); | |
414 | ||
415 | public function setUp() { | |
416 | global $PAGE; | |
417 | $this->oldnav = $PAGE->navigation; | |
418 | $this->cache = new navigation_cache('simpletest_nav'); | |
419 | $this->node = new exposed_navbar(); | |
420 | $temptree = new global_navigation_test(); | |
421 | $temptree->setUp(); | |
3406acde | 422 | $temptree->node->find('course2', navigation_node::TYPE_COURSE)->make_active(); |
7d2a0492 | 423 | $PAGE->navigation = $temptree->node; |
424 | } | |
425 | public function tearDown() { | |
426 | global $PAGE; | |
427 | $PAGE->navigation = $this->oldnav; | |
428 | } | |
429 | public function test_add() { | |
430 | global $CFG; | |
431 | // Add a node with all args set | |
a3bbac8b | 432 | $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',new pix_icon('i/course', '')); |
7d2a0492 | 433 | // Add a node with the minimum args required |
3406acde | 434 | $this->node->add('test_add_2','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd2','testadd2',new pix_icon('i/course', '')); |
7d2a0492 | 435 | $this->assertIsA($this->node->get('testadd1'), 'navigation_node'); |
3406acde | 436 | $this->assertIsA($this->node->get('testadd2'), 'navigation_node'); |
7d2a0492 | 437 | } |
438 | public function test_has_items() { | |
439 | global $PAGE; | |
440 | $this->assertTrue($this->node->has_items()); | |
7d2a0492 | 441 | } |
7d2a0492 | 442 | } |
443 | ||
444 | class navigation_cache_test extends UnitTestCase { | |
445 | protected $cache; | |
446 | ||
447 | public static $includecoverage = array('./lib/navigationlib.php'); | |
448 | public static $excludecoverage = array(); | |
449 | ||
450 | public function setUp() { | |
451 | $this->cache = new navigation_cache('simpletest_nav'); | |
452 | $this->cache->anysetvariable = true; | |
453 | } | |
454 | public function test___get() { | |
455 | $this->assertTrue($this->cache->anysetvariable); | |
456 | $this->assertEqual($this->cache->notasetvariable, null); | |
457 | } | |
458 | public function test___set() { | |
459 | $this->cache->myname = 'Sam Hemelryk'; | |
460 | $this->assertTrue($this->cache->cached('myname')); | |
461 | $this->assertEqual($this->cache->myname, 'Sam Hemelryk'); | |
462 | } | |
463 | public function test_cached() { | |
464 | $this->assertTrue($this->cache->cached('anysetvariable')); | |
465 | $this->assertFalse($this->cache->cached('notasetvariable')); | |
466 | } | |
467 | public function test_clear() { | |
468 | $cache = clone($this->cache); | |
469 | $this->assertTrue($cache->cached('anysetvariable')); | |
470 | $cache->clear(); | |
471 | $this->assertFalse($cache->cached('anysetvariable')); | |
472 | } | |
473 | public function test_set() { | |
474 | $this->cache->set('software', 'Moodle'); | |
475 | $this->assertTrue($this->cache->cached('software')); | |
476 | $this->assertEqual($this->cache->software, 'Moodle'); | |
477 | } | |
478 | } | |
479 | ||
480 | /** | |
481 | * This is a dummy object that allows us to call protected methods within the | |
482 | * global navigation class by prefixing the methods with `exposed_` | |
483 | */ | |
484 | class exposed_settings_navigation extends settings_navigation { | |
485 | protected $exposedkey = 'exposed_'; | |
486 | function __construct() { | |
487 | global $PAGE; | |
488 | parent::__construct($PAGE); | |
489 | $this->cache = new navigation_cache('simpletest_nav'); | |
490 | } | |
491 | function __call($method, $arguments) { | |
492 | if (strpos($method,$this->exposedkey) !== false) { | |
493 | $method = substr($method, strlen($this->exposedkey)); | |
494 | } | |
495 | if (method_exists($this, $method)) { | |
496 | return call_user_func_array(array($this, $method), $arguments); | |
497 | } | |
498 | throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); | |
499 | } | |
500 | } | |
501 | ||
502 | class settings_navigation_test extends UnitTestCase { | |
503 | protected $node; | |
504 | protected $cache; | |
505 | ||
506 | public static $includecoverage = array('./lib/navigationlib.php'); | |
507 | public static $excludecoverage = array(); | |
508 | ||
509 | public function setUp() { | |
510 | global $PAGE; | |
511 | $this->cache = new navigation_cache('simpletest_nav'); | |
512 | $this->node = new exposed_settings_navigation(); | |
513 | } | |
514 | public function test___construct() { | |
515 | $this->node = new exposed_settings_navigation(); | |
516 | } | |
517 | public function test___initialise() { | |
518 | $this->node->initialise(); | |
519 | $this->assertEqual($this->node->id, 'settingsnav'); | |
520 | } | |
7d2a0492 | 521 | public function test_in_alternative_role() { |
522 | $this->assertFalse($this->node->exposed_in_alternative_role()); | |
523 | } | |
95b97515 | 524 | } |