Merge branch 'w22_MDL-39676_m24_whereparam' of git://github.com/skodak/moodle into...
[moodle.git] / lib / tests / pagelib_test.php
bloba7158e71c8c3b42a088c73bb65e1d9892e71fe40
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.
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/>.
17 /**
18 * Tests for the block_manager class in ../blocklib.php.
20 * @package core
21 * @category phpunit
22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/pagelib.php');
30 require_once($CFG->libdir . '/blocklib.php');
33 /** Test-specific subclass to make some protected things public. */
34 class testable_moodle_page extends moodle_page {
35 public function initialise_default_pagetype($script = null) {
36 parent::initialise_default_pagetype($script);
38 public function url_to_class_name($url) {
39 return parent::url_to_class_name($url);
41 public function all_editing_caps() {
42 return parent::all_editing_caps();
47 /**
48 * Test functions that don't need to touch the database.
50 class moodle_page_test extends advanced_testcase {
51 protected $testpage;
53 public function setUp() {
54 parent::setUp();
55 $this->resetAfterTest();
56 $this->testpage = new testable_moodle_page();
59 public function test_course_returns_site_before_set() {
60 global $SITE;
61 // Validate
62 $this->assertSame($SITE, $this->testpage->course);
65 public function test_setting_course_works() {
66 // Setup fixture
67 $course = $this->getDataGenerator()->create_course();
68 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
69 // Exercise SUT
70 $this->testpage->set_course($course);
71 // Validate
72 $this->assertEquals($course, $this->testpage->course);
75 public function test_global_course_and_page_course_are_same_with_global_page() {
76 global $COURSE, $PAGE;
77 // Setup fixture
78 $course = $this->getDataGenerator()->create_course();
79 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
80 $PAGE = $this->testpage;
81 // Exercise SUT
82 $this->testpage->set_course($course);
83 // Validate
84 $this->assertSame($this->testpage->course, $COURSE);
87 public function test_global_course_not_changed_with_non_global_page() {
88 global $COURSE;
89 $originalcourse = $COURSE;
90 // Setup fixture
91 $course = $this->getDataGenerator()->create_course();
92 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
93 // Exercise SUT
94 $this->testpage->set_course($course);
95 // Validate
96 $this->assertSame($originalcourse, $COURSE);
99 public function test_cannot_set_course_once_theme_set() {
100 // Setup fixture
101 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
102 $course = $this->getDataGenerator()->create_course();
103 // Set expectation.
104 $this->setExpectedException('coding_exception');
105 // Exercise SUT
106 $this->testpage->set_course($course);
109 public function test_cannot_set_category_once_theme_set() {
110 // Setup fixture
111 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
112 // Set expectation.
113 $this->setExpectedException('coding_exception');
114 // Exercise SUT
115 $this->testpage->set_category_by_id(123);
118 public function test_cannot_set_category_once_course_set() {
119 // Setup fixture
120 $course = $this->getDataGenerator()->create_course();
121 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
122 $this->testpage->set_course($course);
123 // Set expectation.
124 $this->setExpectedException('coding_exception');
125 // Exercise SUT
126 $this->testpage->set_category_by_id(123);
129 public function test_categories_array_empty_for_front_page() {
130 global $SITE;
131 // Setup fixture
132 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
133 $this->testpage->set_course($SITE);
134 // Exercise SUT and validate.
135 $this->assertEquals(array(), $this->testpage->categories);
138 public function test_set_state_normal_path() {
139 $course = $this->getDataGenerator()->create_course();
140 $this->testpage->set_context(context_system::instance());
141 $this->testpage->set_course($course);
143 $this->assertEquals(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
145 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
146 $this->assertEquals(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
148 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
149 $this->assertEquals(moodle_page::STATE_IN_BODY, $this->testpage->state);
151 $this->testpage->set_state(moodle_page::STATE_DONE);
152 $this->assertEquals(moodle_page::STATE_DONE, $this->testpage->state);
155 public function test_set_state_cannot_skip_one() {
156 // Set expectation.
157 $this->setExpectedException('coding_exception');
158 // Exercise SUT
159 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
162 public function test_header_printed_false_initially() {
163 // Validate
164 $this->assertFalse($this->testpage->headerprinted);
167 public function test_header_printed_becomes_true() {
168 $course = $this->getDataGenerator()->create_course();
169 $this->testpage->set_context(context_system::instance());
170 $this->testpage->set_course($course);
172 // Exercise SUT
173 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
174 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
175 // Validate
176 $this->assertTrue($this->testpage->headerprinted);
179 public function test_set_context() {
180 // Setup fixture
181 $course = $this->getDataGenerator()->create_course();
182 $context = context_course::instance($course->id);
183 // Exercise SUT
184 $this->testpage->set_context($context);
185 // Validate
186 $this->assertSame($context, $this->testpage->context);
189 public function test_pagetype_defaults_to_script() {
190 global $SCRIPT;
191 // Exercise SUT and validate
192 $SCRIPT = '/index.php';
193 $this->testpage->initialise_default_pagetype();
194 $this->assertEquals('site-index', $this->testpage->pagetype);
197 public function test_set_pagetype() {
198 // Exercise SUT
199 $this->testpage->set_pagetype('a-page-type');
200 // Validate
201 $this->assertEquals('a-page-type', $this->testpage->pagetype);
204 public function test_initialise_default_pagetype() {
205 // Exercise SUT
206 $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php');
207 // Validate
208 $this->assertEquals('admin-tool-unittest-index', $this->testpage->pagetype);
211 public function test_initialise_default_pagetype_fp() {
212 // Exercise SUT
213 $this->testpage->initialise_default_pagetype('index.php');
214 // Validate
215 $this->assertEquals('site-index', $this->testpage->pagetype);
218 public function test_get_body_classes_empty() {
219 // Validate
220 $this->assertEquals('', $this->testpage->bodyclasses);
223 public function test_get_body_classes_single() {
224 // Exercise SUT
225 $this->testpage->add_body_class('aclassname');
226 // Validate
227 $this->assertEquals('aclassname', $this->testpage->bodyclasses);
230 public function test_get_body_classes() {
231 // Exercise SUT
232 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
233 // Validate
234 $this->assertEquals('aclassname anotherclassname', $this->testpage->bodyclasses);
237 public function test_url_to_class_name() {
238 $this->assertEquals('example-com', $this->testpage->url_to_class_name('http://example.com'));
239 $this->assertEquals('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
240 $this->assertEquals('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
241 $this->assertEquals('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
244 public function test_set_docs_path() {
245 // Exercise SUT
246 $this->testpage->set_docs_path('a/file/path');
247 // Validate
248 $this->assertEquals('a/file/path', $this->testpage->docspath);
251 public function test_docs_path_defaults_from_pagetype() {
252 // Exercise SUT
253 $this->testpage->set_pagetype('a-page-type');
254 // Validate
255 $this->assertEquals('a/page/type', $this->testpage->docspath);
258 public function test_set_url_root() {
259 global $CFG;
260 // Exercise SUT
261 $this->testpage->set_url('/');
262 // Validate
263 $this->assertEquals($CFG->wwwroot . '/', $this->testpage->url->out());
266 public function test_set_url_one_param() {
267 global $CFG;
268 // Exercise SUT
269 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
270 // Validate
271 $this->assertEquals($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
274 public function test_set_url_two_params() {
275 global $CFG;
276 // Exercise SUT
277 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
278 // Validate
279 $this->assertEquals($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
282 public function test_set_url_using_moodle_url() {
283 global $CFG;
284 // Fixture setup
285 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
286 // Exercise SUT
287 $this->testpage->set_url($url);
288 // Validate
289 $this->assertEquals($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
292 public function test_set_url_sets_page_type() {
293 // Exercise SUT
294 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
295 // Validate
296 $this->assertEquals('mod-quiz-attempt', $this->testpage->pagetype);
299 public function test_set_url_does_not_change_explicit_page_type() {
300 // Setup fixture
301 $this->testpage->set_pagetype('a-page-type');
302 // Exercise SUT
303 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
304 // Validate
305 $this->assertEquals('a-page-type', $this->testpage->pagetype);
308 public function test_set_subpage() {
309 // Exercise SUT
310 $this->testpage->set_subpage('somestring');
311 // Validate
312 $this->assertEquals('somestring', $this->testpage->subpage);
315 public function test_set_heading() {
316 // Exercise SUT
317 $this->testpage->set_heading('a heading');
318 // Validate
319 $this->assertEquals('a heading', $this->testpage->heading);
322 public function test_set_title() {
323 // Exercise SUT
324 $this->testpage->set_title('a title');
325 // Validate
326 $this->assertEquals('a title', $this->testpage->title);
329 public function test_default_pagelayout() {
330 // Exercise SUT and Validate
331 $this->assertEquals('base', $this->testpage->pagelayout);
334 public function test_set_pagelayout() {
335 // Exercise SUT
336 $this->testpage->set_pagelayout('type');
337 // Validate
338 $this->assertEquals('type', $this->testpage->pagelayout);
344 * Test functions that rely on the context table.
346 class moodle_page_with_context_table_test extends advanced_testcase {
347 protected $testpage;
349 protected function setUp() {
350 parent::setUp();
351 $this->testpage = new moodle_page();
352 $this->resetAfterTest();
355 public function test_setting_course_sets_context() {
356 // Setup fixture
357 $course = $this->getDataGenerator()->create_course();
358 $context = context_course::instance($course->id);
360 // Exercise SUT
361 $this->testpage->set_course($course);
363 // Validate
364 $this->assertSame($context, $this->testpage->context);
370 * Test functions that rely on the context table.
372 class moodle_page_categories_test extends advanced_testcase {
373 protected $testpage;
375 protected function setUp() {
376 parent::setUp();
377 $this->testpage = new moodle_page();
379 $this->resetAfterTest();
382 public function test_set_category_top_level() {
383 // Setup fixture
384 $cat = $this->getDataGenerator()->create_category();
385 // Exercise SUT
386 $this->testpage->set_category_by_id($cat->id);
387 // Validate
388 $this->assertEquals($cat, $this->testpage->category);
389 $this->assertSame(context_coursecat::instance($cat->id), $this->testpage->context);
392 public function test_set_nested_categories() {
393 // Setup fixture
394 $topcat = $this->getDataGenerator()->create_category();
395 $subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id));
396 // Exercise SUT
397 $this->testpage->set_category_by_id($subcat->id);
398 // Validate
399 $categories = $this->testpage->categories;
400 $this->assertEquals(2, count($categories));
401 $this->assertEquals($topcat, array_pop($categories));
402 $this->assertEquals($subcat, array_pop($categories));
408 * Test functions that rely on the context table.
410 class moodle_page_cm_test extends advanced_testcase {
411 protected $testpage;
413 protected function setUp() {
414 parent::setUp();
415 $this->testpage = new moodle_page();
416 $this->resetAfterTest();
419 public function test_cm_null_initially() {
420 // Validate
421 $this->assertNull($this->testpage->cm);
424 public function test_set_cm() {
425 // Setup fixture
426 $course = $this->getDataGenerator()->create_course();
427 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
428 $cm = get_coursemodule_from_id('forum', $forum->cmid);
429 // Exercise SUT
430 $this->testpage->set_cm($cm);
431 // Validate
432 $this->assertEquals($cm->id, $this->testpage->cm->id);
435 public function test_cannot_set_activity_record_before_cm() {
436 // Setup fixture
437 $course = $this->getDataGenerator()->create_course();
438 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
439 $cm = get_coursemodule_from_id('forum', $forum->cmid);
440 // Set expectation
441 $this->setExpectedException('coding_exception');
442 // Exercise SUT
443 $this->testpage->set_activity_record($forum);
446 public function test_setting_cm_sets_context() {
447 // Setup fixture
448 $course = $this->getDataGenerator()->create_course();
449 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
450 $cm = get_coursemodule_from_id('forum', $forum->cmid);
451 // Exercise SUT
452 $this->testpage->set_cm($cm);
453 // Validate
454 $this->assertSame(context_module::instance($cm->id), $this->testpage->context);
457 public function test_activity_record_loaded_if_not_set() {
458 // Setup fixture
459 $course = $this->getDataGenerator()->create_course();
460 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
461 $cm = get_coursemodule_from_id('forum', $forum->cmid);
462 // Exercise SUT
463 $this->testpage->set_cm($cm);
464 // Validate
465 unset($forum->cmid);
466 $this->assertEquals($forum, $this->testpage->activityrecord);
469 public function test_set_activity_record() {
470 // Setup fixture
471 $course = $this->getDataGenerator()->create_course();
472 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
473 $cm = get_coursemodule_from_id('forum', $forum->cmid);
474 $this->testpage->set_cm($cm);
475 // Exercise SUT
476 $this->testpage->set_activity_record($forum);
477 // Validate
478 unset($forum->cmid);
479 $this->assertEquals($forum, $this->testpage->activityrecord);
482 public function test_cannot_set_inconsistent_activity_record_course() {
483 // Setup fixture
484 $course = $this->getDataGenerator()->create_course();
485 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
486 $cm = get_coursemodule_from_id('forum', $forum->cmid);
487 $this->testpage->set_cm($cm);
488 // Set expectation
489 $this->setExpectedException('coding_exception');
490 // Exercise SUT
491 $forum->course = 13;
492 $this->testpage->set_activity_record($forum);
495 public function test_cannot_set_inconsistent_activity_record_instance() {
496 // Setup fixture
497 $course = $this->getDataGenerator()->create_course();
498 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
499 $cm = get_coursemodule_from_id('forum', $forum->cmid);
500 $this->testpage->set_cm($cm);
501 // Set expectation
502 $this->setExpectedException('coding_exception');
503 // Exercise SUT
504 $forum->id = 13;
505 $this->testpage->set_activity_record($forum);
508 public function test_setting_cm_sets_course() {
509 // Setup fixture
510 $course = $this->getDataGenerator()->create_course();
511 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
512 $cm = get_coursemodule_from_id('forum', $forum->cmid);
513 // Exercise SUT
514 $this->testpage->set_cm($cm);
515 // Validate
516 $this->assertEquals($course->id, $this->testpage->course->id);
519 public function test_set_cm_with_course_and_activity_no_db() {
520 // Setup fixture
521 $course = $this->getDataGenerator()->create_course();
522 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
523 $cm = get_coursemodule_from_id('forum', $forum->cmid);
524 // This only works without db if we already have modinfo cache
525 // Exercise SUT
526 $this->testpage->set_cm($cm, $course, $forum);
527 // Validate
528 $this->assertEquals($cm->id, $this->testpage->cm->id);
529 $this->assertEquals($course->id, $this->testpage->course->id);
530 unset($forum->cmid);
531 $this->assertEquals($forum, $this->testpage->activityrecord);
534 public function test_cannot_set_cm_with_inconsistent_course() {
535 // Setup fixture
536 $course = $this->getDataGenerator()->create_course();
537 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
538 $cm = get_coursemodule_from_id('forum', $forum->cmid);
539 // Set expectation
540 $this->setExpectedException('coding_exception');
541 // Exercise SUT
542 $cm->course = 13;
543 $this->testpage->set_cm($cm, $course);
546 public function test_get_activity_name() {
547 // Setup fixture
548 $course = $this->getDataGenerator()->create_course();
549 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
550 $cm = get_coursemodule_from_id('forum', $forum->cmid);
551 // Exercise SUT
552 $this->testpage->set_cm($cm, $course, $forum);
553 // Validate
554 $this->assertEquals('forum', $this->testpage->activityname);
560 * Test functions that affect filter_active table with contextid = $syscontextid.
562 class moodle_page_editing_test extends advanced_testcase {
563 protected $testpage;
564 protected $originaluserediting;
566 protected function setUp() {
567 parent::setUp();
568 $this->setAdminUser();
569 $this->testpage = new testable_moodle_page();
570 $this->testpage->set_context(context_system::instance());
571 $this->resetAfterTest();
574 // We are relying on the fact that unit tests are alwyas run by admin, to
575 // ensure the user_allows_editing call returns true.
576 public function test_user_is_editing_on() {
577 // Setup fixture
578 global $USER;
579 $USER->editing = true;
580 // Validate
581 $this->assertTrue($this->testpage->user_is_editing());
584 // We are relying on the fact that unit tests are alwyas run by admin, to
585 // ensure the user_allows_editing call returns true.
586 public function test_user_is_editing_off() {
587 // Setup fixture
588 global $USER;
589 $USER->editing = false;
590 // Validate
591 $this->assertFalse($this->testpage->user_is_editing());
594 public function test_default_editing_capabilities() {
595 // Validate
596 $this->assertEquals(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
599 public function test_other_block_editing_cap() {
600 // Exercise SUT
601 $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
602 // Validate
603 $this->assertEquals(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
606 public function test_other_editing_cap() {
607 // Exercise SUT
608 $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
609 // Validate
610 $actualcaps = $this->testpage->all_editing_caps();
611 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
612 $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
615 public function test_other_editing_caps() {
616 // Exercise SUT
617 $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
618 // Validate
619 $actualcaps = $this->testpage->all_editing_caps();
620 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
621 $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));