Merge branch 'MDL-63702-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / tests / moodle_page_test.php
blob514b3b130072794901ba603718fe53cbf2c782a6
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 moodle_page class.
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 class core_moodle_page_testcase extends advanced_testcase {
35 /**
36 * @var testable_moodle_page
38 protected $testpage;
40 public function setUp() {
41 parent::setUp();
42 $this->resetAfterTest();
43 $this->testpage = new testable_moodle_page();
46 public function test_course_returns_site_before_set() {
47 global $SITE;
48 // Validated.
49 $this->assertSame($SITE, $this->testpage->course);
52 public function test_setting_course_works() {
53 // Setup fixture.
54 $course = $this->getDataGenerator()->create_course();
55 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
56 // Exercise SUT.
57 $this->testpage->set_course($course);
58 // Validated.
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;
64 // Setup fixture.
65 $course = $this->getDataGenerator()->create_course();
66 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
67 $PAGE = $this->testpage;
68 // Exercise SUT.
69 $this->testpage->set_course($course);
70 // Validated.
71 $this->assertSame($COURSE, $this->testpage->course);
74 public function test_global_course_not_changed_with_non_global_page() {
75 global $COURSE;
76 $originalcourse = $COURSE;
77 // Setup fixture.
78 $course = $this->getDataGenerator()->create_course();
79 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
80 // Exercise SUT.
81 $this->testpage->set_course($course);
82 // Validated.
83 $this->assertSame($originalcourse, $COURSE);
86 /**
87 * @expectedException coding_exception
89 public function test_cannot_set_course_once_theme_set() {
90 // Setup fixture.
91 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
92 $course = $this->getDataGenerator()->create_course();
94 // Exercise SUT.
95 $this->testpage->set_course($course);
98 /**
99 * @expectedException coding_exception
101 public function test_cannot_set_category_once_theme_set() {
102 // Setup fixture.
103 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
105 // Exercise SUT.
106 $this->testpage->set_category_by_id(123);
110 * @expectedException coding_exception
112 public function test_cannot_set_category_once_course_set() {
113 // Setup fixture.
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);
118 // Exercise SUT.
119 $this->testpage->set_category_by_id(123);
122 public function test_categories_array_empty_for_front_page() {
123 global $SITE;
124 // Setup fixture.
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() {
152 // Exercise SUT.
153 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
156 public function test_header_printed_false_initially() {
157 // Validated.
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);
166 // Exercise SUT.
167 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
168 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
169 // Validated.
170 $this->assertTrue($this->testpage->headerprinted);
173 public function test_set_context() {
174 // Setup fixture.
175 $course = $this->getDataGenerator()->create_course();
176 $context = context_course::instance($course->id);
177 // Exercise SUT.
178 $this->testpage->set_context($context);
179 // Validated.
180 $this->assertSame($context, $this->testpage->context);
183 public function test_pagetype_defaults_to_script() {
184 global $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() {
192 // Exercise SUT.
193 $this->testpage->set_pagetype('a-page-type');
194 // Validated.
195 $this->assertSame('a-page-type', $this->testpage->pagetype);
198 public function test_initialise_default_pagetype() {
199 // Exercise SUT.
200 $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php');
201 // Validated.
202 $this->assertSame('admin-tool-unittest-index', $this->testpage->pagetype);
205 public function test_initialise_default_pagetype_fp() {
206 // Exercise SUT.
207 $this->testpage->initialise_default_pagetype('index.php');
208 // Validated.
209 $this->assertSame('site-index', $this->testpage->pagetype);
212 public function test_get_body_classes_empty() {
213 // Validated.
214 $this->assertSame('', $this->testpage->bodyclasses);
217 public function test_get_body_classes_single() {
218 // Exercise SUT.
219 $this->testpage->add_body_class('aclassname');
220 // Validated.
221 $this->assertSame('aclassname', $this->testpage->bodyclasses);
224 public function test_get_body_classes() {
225 // Exercise SUT.
226 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
227 // Validated.
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() {
239 // Exercise SUT.
240 $this->testpage->set_docs_path('a/file/path');
241 // Validated.
242 $this->assertSame('a/file/path', $this->testpage->docspath);
245 public function test_docs_path_defaults_from_pagetype() {
246 // Exercise SUT.
247 $this->testpage->set_pagetype('a-page-type');
248 // Validated.
249 $this->assertSame('a/page/type', $this->testpage->docspath);
252 public function test_set_url_root() {
253 global $CFG;
254 // Exercise SUT.
255 $this->testpage->set_url('/');
256 // Validated.
257 $this->assertSame($CFG->wwwroot . '/', $this->testpage->url->out());
260 public function test_set_url_one_param() {
261 global $CFG;
262 // Exercise SUT.
263 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
264 // Validated.
265 $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
268 public function test_set_url_two_params() {
269 global $CFG;
270 // Exercise SUT.
271 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
272 // Validated.
273 $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
276 public function test_set_url_using_moodle_url() {
277 global $CFG;
278 // Fixture setup.
279 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
280 // Exercise SUT.
281 $this->testpage->set_url($url);
282 // Validated.
283 $this->assertSame($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
286 public function test_set_url_sets_page_type() {
287 // Exercise SUT.
288 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
289 // Validated.
290 $this->assertSame('mod-quiz-attempt', $this->testpage->pagetype);
293 public function test_set_url_does_not_change_explicit_page_type() {
294 // Setup fixture.
295 $this->testpage->set_pagetype('a-page-type');
296 // Exercise SUT.
297 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
298 // Validated.
299 $this->assertSame('a-page-type', $this->testpage->pagetype);
302 public function test_set_subpage() {
303 // Exercise SUT.
304 $this->testpage->set_subpage('somestring');
305 // Validated.
306 $this->assertSame('somestring', $this->testpage->subpage);
309 public function test_set_heading() {
310 // Exercise SUT.
311 $this->testpage->set_heading('a heading');
312 // Validated.
313 $this->assertSame('a heading', $this->testpage->heading);
316 public function test_set_title() {
317 // Exercise SUT.
318 $this->testpage->set_title('a title');
319 // Validated.
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() {
329 // Exercise SUT.
330 $this->testpage->set_pagelayout('type');
331 // Validated.
332 $this->assertSame('type', $this->testpage->pagelayout);
335 public function test_setting_course_sets_context() {
336 // Setup fixture.
337 $course = $this->getDataGenerator()->create_course();
338 $context = context_course::instance($course->id);
340 // Exercise SUT.
341 $this->testpage->set_course($course);
343 // Validated.
344 $this->assertSame($context, $this->testpage->context);
347 public function test_set_category_top_level() {
348 global $DB;
349 // Setup fixture.
350 $cat = $this->getDataGenerator()->create_category();
351 $catdbrecord = $DB->get_record('course_categories', array('id' => $cat->id));
352 // Exercise SUT.
353 $this->testpage->set_category_by_id($cat->id);
354 // Validated.
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() {
360 global $DB;
361 // Setup fixture.
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));
366 // Exercise SUT.
367 $this->testpage->set_category_by_id($subcat->id);
368 // Validated.
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() {
376 // Validated.
377 $this->assertNull($this->testpage->cm);
380 public function test_set_cm() {
381 // Setup fixture.
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);
385 // Exercise SUT.
386 $this->testpage->set_cm($cm);
387 // Validated.
388 $this->assertEquals($cm->id, $this->testpage->cm->id);
392 * @expectedException coding_exception
394 public function test_cannot_set_activity_record_before_cm() {
395 // Setup fixture.
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);
399 // Exercise SUT.
400 $this->testpage->set_activity_record($forum);
403 public function test_setting_cm_sets_context() {
404 // Setup fixture.
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);
408 // Exercise SUT.
409 $this->testpage->set_cm($cm);
410 // Validated.
411 $this->assertSame(context_module::instance($cm->id), $this->testpage->context);
414 public function test_activity_record_loaded_if_not_set() {
415 // Setup fixture.
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);
419 // Exercise SUT.
420 $this->testpage->set_cm($cm);
421 // Validated.
422 unset($forum->cmid);
423 $this->assertEquals($forum, $this->testpage->activityrecord);
426 public function test_set_activity_record() {
427 // Setup fixture.
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);
432 // Exercise SUT.
433 $this->testpage->set_activity_record($forum);
434 // Validated.
435 unset($forum->cmid);
436 $this->assertEquals($forum, $this->testpage->activityrecord);
440 * @expectedException coding_exception
442 public function test_cannot_set_inconsistent_activity_record_course() {
443 // Setup fixture.
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);
448 // Exercise SUT.
449 $forum->course = 13;
450 $this->testpage->set_activity_record($forum);
454 * @expectedException coding_exception
456 public function test_cannot_set_inconsistent_activity_record_instance() {
457 // Setup fixture.
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);
462 // Exercise SUT.
463 $forum->id = 13;
464 $this->testpage->set_activity_record($forum);
467 public function test_setting_cm_sets_course() {
468 // Setup fixture.
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);
472 // Exercise SUT.
473 $this->testpage->set_cm($cm);
474 // Validated.
475 $this->assertEquals($course->id, $this->testpage->course->id);
478 public function test_set_cm_with_course_and_activity_no_db() {
479 // Setup fixture.
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
484 // Exercise SUT.
485 $this->testpage->set_cm($cm, $course, $forum);
486 // Validated.
487 $this->assertEquals($cm->id, $this->testpage->cm->id);
488 $this->assertEquals($course->id, $this->testpage->course->id);
489 unset($forum->cmid);
490 $this->assertEquals($forum, $this->testpage->activityrecord);
494 * @expectedException coding_exception
496 public function test_cannot_set_cm_with_inconsistent_course() {
497 // Setup fixture.
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);
501 // Exercise SUT.
502 $cm->course = 13;
503 $this->testpage->set_cm($cm, $course);
506 public function test_get_activity_name() {
507 // Setup fixture.
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);
511 // Exercise SUT.
512 $this->testpage->set_cm($cm, $course, $forum);
513 // Validated.
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.
521 // Setup fixture.
522 global $USER;
524 $this->testpage->set_context(context_system::instance());
525 $this->setAdminUser();
527 $USER->editing = true;
528 // Validated.
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.
536 // Setup fixture.
537 global $USER;
539 $this->testpage->set_context(context_system::instance());
540 $this->setAdminUser();
542 $USER->editing = false;
543 // Validated.
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();
551 // Validated.
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();
559 // Exercise SUT.
560 $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
561 // Validated.
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();
569 // Exercise SUT.
570 $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
571 // Validated.
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();
581 // Exercise SUT.
582 $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
583 // Validated.
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;
595 $oldpage = $PAGE;
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.
614 try {
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);
621 $PAGE = $oldpage;
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;
633 $oldpage = $PAGE;
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'));
651 try {
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);
658 $PAGE = $oldpage;
659 $OUTPUT = $oldoutput;
662 public function test_render_to_cli() {
663 global $OUTPUT;
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);
686 // Create user.
687 $user = $this->getDataGenerator()->create_user(array('theme' => $usertheme));
689 // Create cohorts and add user as member.
690 $cohorts = array();
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.
722 * @return array
724 public function get_user_theme_provider() {
725 return [
726 'User not a member of any cohort' => [
727 'usertheme' => '',
728 'sitetheme' => 'boost',
729 'cohorts' => [],
730 'expected' => 'boost',
732 'User member of one cohort which has a theme set' => [
733 'usertheme' => '',
734 'sitetheme' => 'boost',
735 'cohorts' => [
736 'clean',
738 'expected' => 'clean',
740 'User member of one cohort which has a theme set, and one without a theme' => [
741 'usertheme' => '',
742 'sitetheme' => 'boost',
743 'cohorts' => [
744 'clean',
747 'expected' => 'clean',
749 'User member of one cohort which has a theme set, and one with a different theme' => [
750 'usertheme' => '',
751 'sitetheme' => 'boost',
752 'cohorts' => [
753 'clean',
754 'someother',
756 'expected' => 'boost',
758 'User with a theme but not a member of any cohort' => [
759 'usertheme' => 'more',
760 'sitetheme' => 'boost',
761 'cohorts' => [],
762 'expected' => 'more',
764 'User with a theme and member of one cohort which has a theme set' => [
765 'usertheme' => 'more',
766 'sitetheme' => 'boost',
767 'cohorts' => [
768 'clean',
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();