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 moodle_page class.
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 class core_moodle_page_testcase
extends advanced_testcase
{
36 * @var testable_moodle_page
40 public function setUp() {
42 $this->resetAfterTest();
43 $this->testpage
= new testable_moodle_page();
46 public function test_course_returns_site_before_set() {
49 $this->assertSame($SITE, $this->testpage
->course
);
52 public function test_setting_course_works() {
54 $course = $this->getDataGenerator()->create_course();
55 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
57 $this->testpage
->set_course($course);
59 $this->assertEquals($course, $this->testpage
->course
);
62 public function test_global_course_and_page_course_are_same_with_global_page() {
63 global $COURSE, $PAGE;
65 $course = $this->getDataGenerator()->create_course();
66 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
67 $PAGE = $this->testpage
;
69 $this->testpage
->set_course($course);
71 $this->assertSame($COURSE, $this->testpage
->course
);
74 public function test_global_course_not_changed_with_non_global_page() {
76 $originalcourse = $COURSE;
78 $course = $this->getDataGenerator()->create_course();
79 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
81 $this->testpage
->set_course($course);
83 $this->assertSame($originalcourse, $COURSE);
87 * @expectedException coding_exception
89 public function test_cannot_set_course_once_theme_set() {
91 $this->testpage
->force_theme(theme_config
::DEFAULT_THEME
);
92 $course = $this->getDataGenerator()->create_course();
95 $this->testpage
->set_course($course);
99 * @expectedException coding_exception
101 public function test_cannot_set_category_once_theme_set() {
103 $this->testpage
->force_theme(theme_config
::DEFAULT_THEME
);
106 $this->testpage
->set_category_by_id(123);
110 * @expectedException coding_exception
112 public function test_cannot_set_category_once_course_set() {
114 $course = $this->getDataGenerator()->create_course();
115 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
116 $this->testpage
->set_course($course);
119 $this->testpage
->set_category_by_id(123);
122 public function test_categories_array_empty_for_front_page() {
125 $this->testpage
->set_context(context_system
::instance()); // Avoid trying to set the context.
126 $this->testpage
->set_course($SITE);
127 // Exercise SUT and validate.
128 $this->assertEquals(array(), $this->testpage
->categories
);
131 public function test_set_state_normal_path() {
132 $course = $this->getDataGenerator()->create_course();
133 $this->testpage
->set_context(context_system
::instance());
134 $this->testpage
->set_course($course);
136 $this->assertEquals(moodle_page
::STATE_BEFORE_HEADER
, $this->testpage
->state
);
138 $this->testpage
->set_state(moodle_page
::STATE_PRINTING_HEADER
);
139 $this->assertEquals(moodle_page
::STATE_PRINTING_HEADER
, $this->testpage
->state
);
141 $this->testpage
->set_state(moodle_page
::STATE_IN_BODY
);
142 $this->assertEquals(moodle_page
::STATE_IN_BODY
, $this->testpage
->state
);
144 $this->testpage
->set_state(moodle_page
::STATE_DONE
);
145 $this->assertEquals(moodle_page
::STATE_DONE
, $this->testpage
->state
);
149 * @expectedException coding_exception
151 public function test_set_state_cannot_skip_one() {
153 $this->testpage
->set_state(moodle_page
::STATE_IN_BODY
);
156 public function test_header_printed_false_initially() {
158 $this->assertFalse($this->testpage
->headerprinted
);
161 public function test_header_printed_becomes_true() {
162 $course = $this->getDataGenerator()->create_course();
163 $this->testpage
->set_context(context_system
::instance());
164 $this->testpage
->set_course($course);
167 $this->testpage
->set_state(moodle_page
::STATE_PRINTING_HEADER
);
168 $this->testpage
->set_state(moodle_page
::STATE_IN_BODY
);
170 $this->assertTrue($this->testpage
->headerprinted
);
173 public function test_set_context() {
175 $course = $this->getDataGenerator()->create_course();
176 $context = context_course
::instance($course->id
);
178 $this->testpage
->set_context($context);
180 $this->assertSame($context, $this->testpage
->context
);
183 public function test_pagetype_defaults_to_script() {
185 // Exercise SUT and validate.
186 $SCRIPT = '/index.php';
187 $this->testpage
->initialise_default_pagetype();
188 $this->assertSame('site-index', $this->testpage
->pagetype
);
191 public function test_set_pagetype() {
193 $this->testpage
->set_pagetype('a-page-type');
195 $this->assertSame('a-page-type', $this->testpage
->pagetype
);
198 public function test_initialise_default_pagetype() {
200 $this->testpage
->initialise_default_pagetype('admin/tool/unittest/index.php');
202 $this->assertSame('admin-tool-unittest-index', $this->testpage
->pagetype
);
205 public function test_initialise_default_pagetype_fp() {
207 $this->testpage
->initialise_default_pagetype('index.php');
209 $this->assertSame('site-index', $this->testpage
->pagetype
);
212 public function test_get_body_classes_empty() {
214 $this->assertSame('', $this->testpage
->bodyclasses
);
217 public function test_get_body_classes_single() {
219 $this->testpage
->add_body_class('aclassname');
221 $this->assertSame('aclassname', $this->testpage
->bodyclasses
);
224 public function test_get_body_classes() {
226 $this->testpage
->add_body_classes(array('aclassname', 'anotherclassname'));
228 $this->assertSame('aclassname anotherclassname', $this->testpage
->bodyclasses
);
231 public function test_url_to_class_name() {
232 $this->assertSame('example-com', $this->testpage
->url_to_class_name('http://example.com'));
233 $this->assertSame('example-com--80', $this->testpage
->url_to_class_name('http://example.com:80'));
234 $this->assertSame('example-com--moodle', $this->testpage
->url_to_class_name('https://example.com/moodle'));
235 $this->assertSame('example-com--8080--nested-moodle', $this->testpage
->url_to_class_name('https://example.com:8080/nested/moodle'));
238 public function test_set_docs_path() {
240 $this->testpage
->set_docs_path('a/file/path');
242 $this->assertSame('a/file/path', $this->testpage
->docspath
);
245 public function test_docs_path_defaults_from_pagetype() {
247 $this->testpage
->set_pagetype('a-page-type');
249 $this->assertSame('a/page/type', $this->testpage
->docspath
);
252 public function test_set_url_root() {
255 $this->testpage
->set_url('/');
257 $this->assertSame($CFG->wwwroot
. '/', $this->testpage
->url
->out());
260 public function test_set_url_one_param() {
263 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
265 $this->assertSame($CFG->wwwroot
. '/mod/quiz/attempt.php?attempt=123', $this->testpage
->url
->out());
268 public function test_set_url_two_params() {
271 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
273 $this->assertSame($CFG->wwwroot
. '/mod/quiz/attempt.php?attempt=123&page=7', $this->testpage
->url
->out());
276 public function test_set_url_using_moodle_url() {
279 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
281 $this->testpage
->set_url($url);
283 $this->assertSame($CFG->wwwroot
. '/mod/workshop/allocation.php?cmid=29&method=manual', $this->testpage
->url
->out());
286 public function test_set_url_sets_page_type() {
288 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
290 $this->assertSame('mod-quiz-attempt', $this->testpage
->pagetype
);
293 public function test_set_url_does_not_change_explicit_page_type() {
295 $this->testpage
->set_pagetype('a-page-type');
297 $this->testpage
->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
299 $this->assertSame('a-page-type', $this->testpage
->pagetype
);
302 public function test_set_subpage() {
304 $this->testpage
->set_subpage('somestring');
306 $this->assertSame('somestring', $this->testpage
->subpage
);
309 public function test_set_heading() {
311 $this->testpage
->set_heading('a heading');
313 $this->assertSame('a heading', $this->testpage
->heading
);
316 public function test_set_title() {
318 $this->testpage
->set_title('a title');
320 $this->assertSame('a title', $this->testpage
->title
);
323 public function test_default_pagelayout() {
324 // Exercise SUT and Validate.
325 $this->assertSame('base', $this->testpage
->pagelayout
);
328 public function test_set_pagelayout() {
330 $this->testpage
->set_pagelayout('type');
332 $this->assertSame('type', $this->testpage
->pagelayout
);
335 public function test_setting_course_sets_context() {
337 $course = $this->getDataGenerator()->create_course();
338 $context = context_course
::instance($course->id
);
341 $this->testpage
->set_course($course);
344 $this->assertSame($context, $this->testpage
->context
);
347 public function test_set_category_top_level() {
350 $cat = $this->getDataGenerator()->create_category();
351 $catdbrecord = $DB->get_record('course_categories', array('id' => $cat->id
));
353 $this->testpage
->set_category_by_id($cat->id
);
355 $this->assertEquals($catdbrecord, $this->testpage
->category
);
356 $this->assertSame(context_coursecat
::instance($cat->id
), $this->testpage
->context
);
359 public function test_set_nested_categories() {
362 $topcat = $this->getDataGenerator()->create_category();
363 $topcatdbrecord = $DB->get_record('course_categories', array('id' => $topcat->id
));
364 $subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id
));
365 $subcatdbrecord = $DB->get_record('course_categories', array('id' => $subcat->id
));
367 $this->testpage
->set_category_by_id($subcat->id
);
369 $categories = $this->testpage
->categories
;
370 $this->assertCount(2, $categories);
371 $this->assertEquals($topcatdbrecord, array_pop($categories));
372 $this->assertEquals($subcatdbrecord, array_pop($categories));
375 public function test_cm_null_initially() {
377 $this->assertNull($this->testpage
->cm
);
380 public function test_set_cm() {
382 $course = $this->getDataGenerator()->create_course();
383 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
384 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
386 $this->testpage
->set_cm($cm);
388 $this->assertEquals($cm->id
, $this->testpage
->cm
->id
);
392 * @expectedException coding_exception
394 public function test_cannot_set_activity_record_before_cm() {
396 $course = $this->getDataGenerator()->create_course();
397 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
398 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
400 $this->testpage
->set_activity_record($forum);
403 public function test_setting_cm_sets_context() {
405 $course = $this->getDataGenerator()->create_course();
406 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
407 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
409 $this->testpage
->set_cm($cm);
411 $this->assertSame(context_module
::instance($cm->id
), $this->testpage
->context
);
414 public function test_activity_record_loaded_if_not_set() {
416 $course = $this->getDataGenerator()->create_course();
417 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
418 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
420 $this->testpage
->set_cm($cm);
423 $this->assertEquals($forum, $this->testpage
->activityrecord
);
426 public function test_set_activity_record() {
428 $course = $this->getDataGenerator()->create_course();
429 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
430 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
431 $this->testpage
->set_cm($cm);
433 $this->testpage
->set_activity_record($forum);
436 $this->assertEquals($forum, $this->testpage
->activityrecord
);
440 * @expectedException coding_exception
442 public function test_cannot_set_inconsistent_activity_record_course() {
444 $course = $this->getDataGenerator()->create_course();
445 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
446 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
447 $this->testpage
->set_cm($cm);
450 $this->testpage
->set_activity_record($forum);
454 * @expectedException coding_exception
456 public function test_cannot_set_inconsistent_activity_record_instance() {
458 $course = $this->getDataGenerator()->create_course();
459 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
460 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
461 $this->testpage
->set_cm($cm);
464 $this->testpage
->set_activity_record($forum);
467 public function test_setting_cm_sets_course() {
469 $course = $this->getDataGenerator()->create_course();
470 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
471 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
473 $this->testpage
->set_cm($cm);
475 $this->assertEquals($course->id
, $this->testpage
->course
->id
);
478 public function test_set_cm_with_course_and_activity_no_db() {
480 $course = $this->getDataGenerator()->create_course();
481 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
482 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
483 // This only works without db if we already have modinfo cache
485 $this->testpage
->set_cm($cm, $course, $forum);
487 $this->assertEquals($cm->id
, $this->testpage
->cm
->id
);
488 $this->assertEquals($course->id
, $this->testpage
->course
->id
);
490 $this->assertEquals($forum, $this->testpage
->activityrecord
);
494 * @expectedException coding_exception
496 public function test_cannot_set_cm_with_inconsistent_course() {
498 $course = $this->getDataGenerator()->create_course();
499 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
500 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
503 $this->testpage
->set_cm($cm, $course);
506 public function test_get_activity_name() {
508 $course = $this->getDataGenerator()->create_course();
509 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
510 $cm = get_coursemodule_from_id('forum', $forum->cmid
);
512 $this->testpage
->set_cm($cm, $course, $forum);
514 $this->assertSame('forum', $this->testpage
->activityname
);
517 public function test_user_is_editing_on() {
518 // We are relying on the fact that unit tests are always run by admin, to
519 // ensure the user_allows_editing call returns true.
524 $this->testpage
->set_context(context_system
::instance());
525 $this->setAdminUser();
527 $USER->editing
= true;
529 $this->assertTrue($this->testpage
->user_is_editing());
532 public function test_user_is_editing_off() {
533 // We are relying on the fact that unit tests are always run by admin, to
534 // ensure the user_allows_editing call returns true.
539 $this->testpage
->set_context(context_system
::instance());
540 $this->setAdminUser();
542 $USER->editing
= false;
544 $this->assertFalse($this->testpage
->user_is_editing());
547 public function test_default_editing_capabilities() {
548 $this->testpage
->set_context(context_system
::instance());
549 $this->setAdminUser();
552 $this->assertEquals(array('moodle/site:manageblocks'), $this->testpage
->all_editing_caps());
555 public function test_other_block_editing_cap() {
556 $this->testpage
->set_context(context_system
::instance());
557 $this->setAdminUser();
560 $this->testpage
->set_blocks_editing_capability('moodle/my:manageblocks');
562 $this->assertEquals(array('moodle/my:manageblocks'), $this->testpage
->all_editing_caps());
565 public function test_other_editing_cap() {
566 $this->testpage
->set_context(context_system
::instance());
567 $this->setAdminUser();
570 $this->testpage
->set_other_editing_capability('moodle/course:manageactivities');
572 $actualcaps = $this->testpage
->all_editing_caps();
573 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
574 $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
577 public function test_other_editing_caps() {
578 $this->testpage
->set_context(context_system
::instance());
579 $this->setAdminUser();
582 $this->testpage
->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
584 $actualcaps = $this->testpage
->all_editing_caps();
585 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
586 $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
590 * Test getting a renderer.
592 public function test_get_renderer() {
593 global $OUTPUT, $PAGE;
594 $oldoutput = $OUTPUT;
596 $PAGE = $this->testpage
;
598 $this->testpage
->set_pagelayout('standard');
599 $this->assertEquals('standard', $this->testpage
->pagelayout
);
600 // Initialise theme and output for the next tests.
601 $this->testpage
->initialise_theme_and_output();
602 // Check the generated $OUTPUT object is a core renderer.
603 $this->assertInstanceOf('core_renderer', $OUTPUT);
604 // Check we can get a core renderer if we explicitly request one (no component).
605 $this->assertInstanceOf('core_renderer', $this->testpage
->get_renderer('core'));
606 // Check we get a CLI renderer if we request a maintenance renderer. The CLI target should take precedence.
607 $this->assertInstanceOf('core_renderer_cli',
608 $this->testpage
->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE
));
610 // Check we can get a coures renderer if we explicitly request one (valid component).
611 $this->assertInstanceOf('core_course_renderer', $this->testpage
->get_renderer('core', 'course'));
613 // Check a properly invalid component.
615 $this->testpage
->get_renderer('core', 'monkeys');
616 $this->fail('Request for renderer with invalid component didn\'t throw expected exception.');
617 } catch (coding_exception
$exception) {
618 $this->assertEquals('monkeys', $exception->debuginfo
);
622 $OUTPUT = $oldoutput;
626 * Tests getting a renderer with a maintenance layout.
628 * This layout has special hacks in place in order to deliver a "maintenance" renderer.
630 public function test_get_renderer_maintenance() {
631 global $OUTPUT, $PAGE;
632 $oldoutput = $OUTPUT;
634 $PAGE = $this->testpage
;
636 $this->testpage
->set_pagelayout('maintenance');
637 $this->assertEquals('maintenance', $this->testpage
->pagelayout
);
638 // Initialise theme and output for the next tests.
639 $this->testpage
->initialise_theme_and_output();
640 // Check the generated $OUTPUT object is a core cli renderer.
641 // It shouldn't be maintenance because there the cli target should take greater precedence.
642 $this->assertInstanceOf('core_renderer_cli', $OUTPUT);
643 // Check we can get a core renderer if we explicitly request one (no component).
644 $this->assertInstanceOf('core_renderer', $this->testpage
->get_renderer('core'));
645 // Check we get a CLI renderer if we request a maintenance renderer. The CLI target should take precedence.
646 $this->assertInstanceOf('core_renderer_cli',
647 $this->testpage
->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE
));
648 // Check we can get a coures renderer if we explicitly request one (valid component).
649 $this->assertInstanceOf('core_course_renderer', $this->testpage
->get_renderer('core', 'course'));
652 $this->testpage
->get_renderer('core', 'monkeys');
653 $this->fail('Request for renderer with invalid component didn\'t throw expected exception.');
654 } catch (coding_exception
$exception) {
655 $this->assertEquals('monkeys', $exception->debuginfo
);
659 $OUTPUT = $oldoutput;
662 public function test_render_to_cli() {
665 $footer = $OUTPUT->footer();
666 $this->assertEmpty($footer, 'cli output does not have a footer.');
670 * Validate the theme value depending on the user theme and cohorts.
672 * @dataProvider get_user_theme_provider
674 public function test_cohort_get_user_theme($usertheme, $sitetheme, $cohortthemes, $expected) {
675 global $DB, $PAGE, $USER;
677 $this->resetAfterTest();
679 // Enable cohort themes.
680 set_config('allowuserthemes', 1);
681 set_config('allowcohortthemes', 1);
683 $systemctx = context_system
::instance();
685 set_config('theme', $sitetheme);
687 $user = $this->getDataGenerator()->create_user(array('theme' => $usertheme));
689 // Create cohorts and add user as member.
691 foreach ($cohortthemes as $cohorttheme) {
692 $cohort = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id
, 'name' => 'Cohort',
693 'idnumber' => '', 'description' => '', 'theme' => $cohorttheme));
694 $cohorts[] = $cohort;
695 cohort_add_member($cohort->id
, $user->id
);
698 // Get the theme and compare to the expected.
699 $this->setUser($user);
701 // Initialise user theme.
702 $USER = get_complete_user_data('id', $user->id
);
704 // Initialise site theme.
705 $PAGE->reset_theme_and_output();
706 $PAGE->initialise_theme_and_output();
707 $result = $PAGE->theme
->name
;
708 $this->assertEquals($expected, $result);
712 * Some user cases for validating the expected theme depending on the cohorts, site and user values.
714 * The result is an array of:
715 * 'User case description' => [
716 * 'usertheme' => '', // User theme.
717 * 'sitetheme' => '', // Site theme.
718 * 'cohorts' => [], // Cohort themes.
719 * 'expected' => '', // Expected value returned by cohort_get_user_cohort_theme.
724 public function get_user_theme_provider() {
726 'User not a member of any cohort' => [
728 'sitetheme' => 'boost',
730 'expected' => 'boost',
732 'User member of one cohort which has a theme set' => [
734 'sitetheme' => 'boost',
738 'expected' => 'clean',
740 'User member of one cohort which has a theme set, and one without a theme' => [
742 'sitetheme' => 'boost',
747 'expected' => 'clean',
749 'User member of one cohort which has a theme set, and one with a different theme' => [
751 'sitetheme' => 'boost',
756 'expected' => 'boost',
758 'User with a theme but not a member of any cohort' => [
759 'usertheme' => 'more',
760 'sitetheme' => 'boost',
762 'expected' => 'more',
764 'User with a theme and member of one cohort which has a theme set' => [
765 'usertheme' => 'more',
766 'sitetheme' => 'boost',
770 'expected' => 'more',
777 * Test-specific subclass to make some protected things public.
779 class testable_moodle_page
extends moodle_page
{
780 public function initialise_default_pagetype($script = null) {
781 parent
::initialise_default_pagetype($script);
783 public function url_to_class_name($url) {
784 return parent
::url_to_class_name($url);
786 public function all_editing_caps() {
787 return parent
::all_editing_caps();