2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Tests for the block_manager class in ../blocklib.php.
22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
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();
48 * Test functions that don't need to touch the database.
50 class moodle_page_test
extends advanced_testcase
{
53 public function setUp() {
55 $this->resetAfterTest();
56 $this->testpage
= new testable_moodle_page();
59 public function test_course_returns_site_before_set() {
62 $this->assertSame($SITE, $this->testpage
->course
);
65 public function test_setting_course_works() {
67 $course = $this->getDataGenerator()->create_course();
68 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
70 $this->testpage
->set_course($course);
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;
78 $course = $this->getDataGenerator()->create_course();
79 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
80 $PAGE = $this->testpage
;
82 $this->testpage
->set_course($course);
84 $this->assertSame($this->testpage
->course
, $COURSE);
87 public function test_global_course_not_changed_with_non_global_page() {
89 $originalcourse = $COURSE;
91 $course = $this->getDataGenerator()->create_course();
92 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
94 $this->testpage
->set_course($course);
96 $this->assertSame($originalcourse, $COURSE);
99 public function test_cannot_set_course_once_theme_set() {
101 $this->testpage
->force_theme(theme_config
::DEFAULT_THEME
);
102 $course = $this->getDataGenerator()->create_course();
104 $this->setExpectedException('coding_exception');
106 $this->testpage
->set_course($course);
109 public function test_cannot_set_category_once_theme_set() {
111 $this->testpage
->force_theme(theme_config
::DEFAULT_THEME
);
113 $this->setExpectedException('coding_exception');
115 $this->testpage
->set_category_by_id(123);
118 public function test_cannot_set_category_once_course_set() {
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);
124 $this->setExpectedException('coding_exception');
126 $this->testpage
->set_category_by_id(123);
129 public function test_categories_array_empty_for_front_page() {
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() {
157 $this->setExpectedException('coding_exception');
159 $this->testpage
->set_state(moodle_page
::STATE_IN_BODY
);
162 public function test_header_printed_false_initially() {
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);
173 $this->testpage
->set_state(moodle_page
::STATE_PRINTING_HEADER
);
174 $this->testpage
->set_state(moodle_page
::STATE_IN_BODY
);
176 $this->assertTrue($this->testpage
->headerprinted
);
179 public function test_set_context() {
181 $course = $this->getDataGenerator()->create_course();
182 $context = context_course
::instance($course->id
);
184 $this->testpage
->set_context($context);
186 $this->assertSame($context, $this->testpage
->context
);
189 public function test_pagetype_defaults_to_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() {
199 $this->testpage
->set_pagetype('a-page-type');
201 $this->assertEquals('a-page-type', $this->testpage
->pagetype
);
204 public function test_initialise_default_pagetype() {
206 $this->testpage
->initialise_default_pagetype('admin/tool/unittest/index.php');
208 $this->assertEquals('admin-tool-unittest-index', $this->testpage
->pagetype
);
211 public function test_initialise_default_pagetype_fp() {
213 $this->testpage
->initialise_default_pagetype('index.php');
215 $this->assertEquals('site-index', $this->testpage
->pagetype
);
218 public function test_get_body_classes_empty() {
220 $this->assertEquals('', $this->testpage
->bodyclasses
);
223 public function test_get_body_classes_single() {
225 $this->testpage
->add_body_class('aclassname');
227 $this->assertEquals('aclassname', $this->testpage
->bodyclasses
);
230 public function test_get_body_classes() {
232 $this->testpage
->add_body_classes(array('aclassname', 'anotherclassname'));
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() {
246 $this->testpage
->set_docs_path('a/file/path');
248 $this->assertEquals('a/file/path', $this->testpage
->docspath
);
251 public function test_docs_path_defaults_from_pagetype() {
253 $this->testpage
->set_pagetype('a-page-type');
255 $this->assertEquals('a/page/type', $this->testpage
->docspath
);
258 public function test_set_url_root() {
261 $this->testpage
->set_url('/');
263 $this->assertEquals($CFG->wwwroot
. '/', $this->testpage
->url
->out());
266 public function test_set_url_one_param() {
269 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
271 $this->assertEquals($CFG->wwwroot
. '/mod/quiz/attempt.php?attempt=123', $this->testpage
->url
->out());
274 public function test_set_url_two_params() {
277 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
279 $this->assertEquals($CFG->wwwroot
. '/mod/quiz/attempt.php?attempt=123&page=7', $this->testpage
->url
->out());
282 public function test_set_url_using_moodle_url() {
285 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
287 $this->testpage
->set_url($url);
289 $this->assertEquals($CFG->wwwroot
. '/mod/workshop/allocation.php?cmid=29&method=manual', $this->testpage
->url
->out());
292 public function test_set_url_sets_page_type() {
294 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
296 $this->assertEquals('mod-quiz-attempt', $this->testpage
->pagetype
);
299 public function test_set_url_does_not_change_explicit_page_type() {
301 $this->testpage
->set_pagetype('a-page-type');
303 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
305 $this->assertEquals('a-page-type', $this->testpage
->pagetype
);
308 public function test_set_subpage() {
310 $this->testpage
->set_subpage('somestring');
312 $this->assertEquals('somestring', $this->testpage
->subpage
);
315 public function test_set_heading() {
317 $this->testpage
->set_heading('a heading');
319 $this->assertEquals('a heading', $this->testpage
->heading
);
322 public function test_set_title() {
324 $this->testpage
->set_title('a title');
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() {
336 $this->testpage
->set_pagelayout('type');
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
{
349 protected function setUp() {
351 $this->testpage
= new moodle_page();
352 $this->resetAfterTest();
355 public function test_setting_course_sets_context() {
357 $course = $this->getDataGenerator()->create_course();
358 $context = context_course
::instance($course->id
);
361 $this->testpage
->set_course($course);
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
{
375 protected function setUp() {
377 $this->testpage
= new moodle_page();
379 $this->resetAfterTest();
382 public function test_set_category_top_level() {
384 $cat = $this->getDataGenerator()->create_category();
386 $this->testpage
->set_category_by_id($cat->id
);
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() {
394 $topcat = $this->getDataGenerator()->create_category();
395 $subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id
));
397 $this->testpage
->set_category_by_id($subcat->id
);
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
{
413 protected function setUp() {
415 $this->testpage
= new moodle_page();
416 $this->resetAfterTest();
419 public function test_cm_null_initially() {
421 $this->assertNull($this->testpage
->cm
);
424 public function test_set_cm() {
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
);
430 $this->testpage
->set_cm($cm);
432 $this->assertEquals($cm->id
, $this->testpage
->cm
->id
);
435 public function test_cannot_set_activity_record_before_cm() {
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
);
441 $this->setExpectedException('coding_exception');
443 $this->testpage
->set_activity_record($forum);
446 public function test_setting_cm_sets_context() {
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
);
452 $this->testpage
->set_cm($cm);
454 $this->assertSame(context_module
::instance($cm->id
), $this->testpage
->context
);
457 public function test_activity_record_loaded_if_not_set() {
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
);
463 $this->testpage
->set_cm($cm);
466 $this->assertEquals($forum, $this->testpage
->activityrecord
);
469 public function test_set_activity_record() {
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);
476 $this->testpage
->set_activity_record($forum);
479 $this->assertEquals($forum, $this->testpage
->activityrecord
);
482 public function test_cannot_set_inconsistent_activity_record_course() {
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);
489 $this->setExpectedException('coding_exception');
492 $this->testpage
->set_activity_record($forum);
495 public function test_cannot_set_inconsistent_activity_record_instance() {
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);
502 $this->setExpectedException('coding_exception');
505 $this->testpage
->set_activity_record($forum);
508 public function test_setting_cm_sets_course() {
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
);
514 $this->testpage
->set_cm($cm);
516 $this->assertEquals($course->id
, $this->testpage
->course
->id
);
519 public function test_set_cm_with_course_and_activity_no_db() {
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
526 $this->testpage
->set_cm($cm, $course, $forum);
528 $this->assertEquals($cm->id
, $this->testpage
->cm
->id
);
529 $this->assertEquals($course->id
, $this->testpage
->course
->id
);
531 $this->assertEquals($forum, $this->testpage
->activityrecord
);
534 public function test_cannot_set_cm_with_inconsistent_course() {
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
);
540 $this->setExpectedException('coding_exception');
543 $this->testpage
->set_cm($cm, $course);
546 public function test_get_activity_name() {
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
);
552 $this->testpage
->set_cm($cm, $course, $forum);
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
{
564 protected $originaluserediting;
566 protected function 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() {
579 $USER->editing
= true;
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() {
589 $USER->editing
= false;
591 $this->assertFalse($this->testpage
->user_is_editing());
594 public function test_default_editing_capabilities() {
596 $this->assertEquals(array('moodle/site:manageblocks'), $this->testpage
->all_editing_caps());
599 public function test_other_block_editing_cap() {
601 $this->testpage
->set_blocks_editing_capability('moodle/my:manageblocks');
603 $this->assertEquals(array('moodle/my:manageblocks'), $this->testpage
->all_editing_caps());
606 public function test_other_editing_cap() {
608 $this->testpage
->set_other_editing_capability('moodle/course:manageactivities');
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() {
617 $this->testpage
->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
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));