MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / lib / tests / moodle_page_test.php
CommitLineData
37ba2528
PS
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.
13//
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/>.
16
17/**
0d123bd7 18 * Tests for the moodle_page class.
37ba2528
PS
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/pagelib.php');
30require_once($CFG->libdir . '/blocklib.php');
31
32
0d123bd7 33class core_moodle_page_testcase extends advanced_testcase {
166ac0a3
SH
34
35 /**
36 * @var testable_moodle_page
37 */
37ba2528
PS
38 protected $testpage;
39
40 public function setUp() {
d59ef9c5 41 parent::setUp();
37ba2528
PS
42 $this->resetAfterTest();
43 $this->testpage = new testable_moodle_page();
44 }
45
46 public function test_course_returns_site_before_set() {
47 global $SITE;
0d123bd7 48 // Validated.
37ba2528
PS
49 $this->assertSame($SITE, $this->testpage->course);
50 }
51
52 public function test_setting_course_works() {
0d123bd7 53 // Setup fixture.
37ba2528 54 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 55 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
0d123bd7 56 // Exercise SUT.
37ba2528 57 $this->testpage->set_course($course);
0d123bd7 58 // Validated.
37ba2528
PS
59 $this->assertEquals($course, $this->testpage->course);
60 }
61
62 public function test_global_course_and_page_course_are_same_with_global_page() {
63 global $COURSE, $PAGE;
0d123bd7 64 // Setup fixture.
37ba2528 65 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 66 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
37ba2528 67 $PAGE = $this->testpage;
0d123bd7 68 // Exercise SUT.
37ba2528 69 $this->testpage->set_course($course);
0d123bd7
PS
70 // Validated.
71 $this->assertSame($COURSE, $this->testpage->course);
37ba2528
PS
72 }
73
74 public function test_global_course_not_changed_with_non_global_page() {
75 global $COURSE;
76 $originalcourse = $COURSE;
0d123bd7 77 // Setup fixture.
37ba2528 78 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 79 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
0d123bd7 80 // Exercise SUT.
37ba2528 81 $this->testpage->set_course($course);
0d123bd7 82 // Validated.
37ba2528
PS
83 $this->assertSame($originalcourse, $COURSE);
84 }
85
52f3e060
RT
86 /**
87 * @expectedException coding_exception
88 */
37ba2528 89 public function test_cannot_set_course_once_theme_set() {
0d123bd7 90 // Setup fixture.
37ba2528
PS
91 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
92 $course = $this->getDataGenerator()->create_course();
52f3e060 93
0d123bd7 94 // Exercise SUT.
37ba2528
PS
95 $this->testpage->set_course($course);
96 }
97
52f3e060
RT
98 /**
99 * @expectedException coding_exception
100 */
37ba2528 101 public function test_cannot_set_category_once_theme_set() {
0d123bd7 102 // Setup fixture.
37ba2528 103 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
52f3e060 104
0d123bd7 105 // Exercise SUT.
37ba2528
PS
106 $this->testpage->set_category_by_id(123);
107 }
108
52f3e060
RT
109 /**
110 * @expectedException coding_exception
111 */
37ba2528 112 public function test_cannot_set_category_once_course_set() {
0d123bd7 113 // Setup fixture.
37ba2528 114 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 115 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
37ba2528 116 $this->testpage->set_course($course);
52f3e060 117
0d123bd7 118 // Exercise SUT.
37ba2528
PS
119 $this->testpage->set_category_by_id(123);
120 }
121
122 public function test_categories_array_empty_for_front_page() {
123 global $SITE;
0d123bd7 124 // Setup fixture.
b0c6dc1c 125 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
37ba2528
PS
126 $this->testpage->set_course($SITE);
127 // Exercise SUT and validate.
128 $this->assertEquals(array(), $this->testpage->categories);
129 }
130
131 public function test_set_state_normal_path() {
132 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 133 $this->testpage->set_context(context_system::instance());
37ba2528
PS
134 $this->testpage->set_course($course);
135
136 $this->assertEquals(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
137
138 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
139 $this->assertEquals(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
140
141 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
142 $this->assertEquals(moodle_page::STATE_IN_BODY, $this->testpage->state);
143
144 $this->testpage->set_state(moodle_page::STATE_DONE);
145 $this->assertEquals(moodle_page::STATE_DONE, $this->testpage->state);
146 }
147
52f3e060
RT
148 /**
149 * @expectedException coding_exception
150 */
37ba2528 151 public function test_set_state_cannot_skip_one() {
0d123bd7 152 // Exercise SUT.
37ba2528
PS
153 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
154 }
155
156 public function test_header_printed_false_initially() {
0d123bd7 157 // Validated.
37ba2528
PS
158 $this->assertFalse($this->testpage->headerprinted);
159 }
160
161 public function test_header_printed_becomes_true() {
162 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 163 $this->testpage->set_context(context_system::instance());
37ba2528
PS
164 $this->testpage->set_course($course);
165
0d123bd7 166 // Exercise SUT.
37ba2528
PS
167 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
168 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
0d123bd7 169 // Validated.
37ba2528
PS
170 $this->assertTrue($this->testpage->headerprinted);
171 }
172
173 public function test_set_context() {
0d123bd7 174 // Setup fixture.
37ba2528
PS
175 $course = $this->getDataGenerator()->create_course();
176 $context = context_course::instance($course->id);
0d123bd7 177 // Exercise SUT.
37ba2528 178 $this->testpage->set_context($context);
0d123bd7 179 // Validated.
37ba2528
PS
180 $this->assertSame($context, $this->testpage->context);
181 }
182
183 public function test_pagetype_defaults_to_script() {
3a7bc16a 184 global $SCRIPT;
0d123bd7 185 // Exercise SUT and validate.
3a7bc16a
PS
186 $SCRIPT = '/index.php';
187 $this->testpage->initialise_default_pagetype();
0d123bd7 188 $this->assertSame('site-index', $this->testpage->pagetype);
37ba2528
PS
189 }
190
191 public function test_set_pagetype() {
0d123bd7 192 // Exercise SUT.
37ba2528 193 $this->testpage->set_pagetype('a-page-type');
0d123bd7
PS
194 // Validated.
195 $this->assertSame('a-page-type', $this->testpage->pagetype);
37ba2528
PS
196 }
197
198 public function test_initialise_default_pagetype() {
0d123bd7 199 // Exercise SUT.
37ba2528 200 $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php');
0d123bd7
PS
201 // Validated.
202 $this->assertSame('admin-tool-unittest-index', $this->testpage->pagetype);
37ba2528
PS
203 }
204
205 public function test_initialise_default_pagetype_fp() {
0d123bd7 206 // Exercise SUT.
37ba2528 207 $this->testpage->initialise_default_pagetype('index.php');
0d123bd7
PS
208 // Validated.
209 $this->assertSame('site-index', $this->testpage->pagetype);
37ba2528
PS
210 }
211
212 public function test_get_body_classes_empty() {
0d123bd7
PS
213 // Validated.
214 $this->assertSame('', $this->testpage->bodyclasses);
37ba2528
PS
215 }
216
217 public function test_get_body_classes_single() {
0d123bd7 218 // Exercise SUT.
37ba2528 219 $this->testpage->add_body_class('aclassname');
0d123bd7
PS
220 // Validated.
221 $this->assertSame('aclassname', $this->testpage->bodyclasses);
37ba2528
PS
222 }
223
224 public function test_get_body_classes() {
0d123bd7 225 // Exercise SUT.
37ba2528 226 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
0d123bd7
PS
227 // Validated.
228 $this->assertSame('aclassname anotherclassname', $this->testpage->bodyclasses);
37ba2528
PS
229 }
230
231 public function test_url_to_class_name() {
0d123bd7
PS
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'));
37ba2528
PS
236 }
237
238 public function test_set_docs_path() {
0d123bd7 239 // Exercise SUT.
37ba2528 240 $this->testpage->set_docs_path('a/file/path');
0d123bd7
PS
241 // Validated.
242 $this->assertSame('a/file/path', $this->testpage->docspath);
37ba2528
PS
243 }
244
245 public function test_docs_path_defaults_from_pagetype() {
0d123bd7 246 // Exercise SUT.
37ba2528 247 $this->testpage->set_pagetype('a-page-type');
0d123bd7
PS
248 // Validated.
249 $this->assertSame('a/page/type', $this->testpage->docspath);
37ba2528
PS
250 }
251
252 public function test_set_url_root() {
253 global $CFG;
0d123bd7 254 // Exercise SUT.
37ba2528 255 $this->testpage->set_url('/');
0d123bd7
PS
256 // Validated.
257 $this->assertSame($CFG->wwwroot . '/', $this->testpage->url->out());
37ba2528
PS
258 }
259
260 public function test_set_url_one_param() {
261 global $CFG;
0d123bd7 262 // Exercise SUT.
37ba2528 263 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
0d123bd7
PS
264 // Validated.
265 $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
37ba2528
PS
266 }
267
268 public function test_set_url_two_params() {
269 global $CFG;
0d123bd7 270 // Exercise SUT.
37ba2528 271 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
0d123bd7
PS
272 // Validated.
273 $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
37ba2528
PS
274 }
275
276 public function test_set_url_using_moodle_url() {
277 global $CFG;
0d123bd7 278 // Fixture setup.
37ba2528 279 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
0d123bd7 280 // Exercise SUT.
37ba2528 281 $this->testpage->set_url($url);
0d123bd7
PS
282 // Validated.
283 $this->assertSame($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
37ba2528
PS
284 }
285
286 public function test_set_url_sets_page_type() {
0d123bd7 287 // Exercise SUT.
37ba2528 288 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
0d123bd7
PS
289 // Validated.
290 $this->assertSame('mod-quiz-attempt', $this->testpage->pagetype);
37ba2528
PS
291 }
292
293 public function test_set_url_does_not_change_explicit_page_type() {
0d123bd7 294 // Setup fixture.
37ba2528 295 $this->testpage->set_pagetype('a-page-type');
0d123bd7 296 // Exercise SUT.
37ba2528 297 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
0d123bd7
PS
298 // Validated.
299 $this->assertSame('a-page-type', $this->testpage->pagetype);
37ba2528
PS
300 }
301
302 public function test_set_subpage() {
0d123bd7 303 // Exercise SUT.
37ba2528 304 $this->testpage->set_subpage('somestring');
0d123bd7
PS
305 // Validated.
306 $this->assertSame('somestring', $this->testpage->subpage);
37ba2528
PS
307 }
308
309 public function test_set_heading() {
0d123bd7 310 // Exercise SUT.
37ba2528 311 $this->testpage->set_heading('a heading');
0d123bd7
PS
312 // Validated.
313 $this->assertSame('a heading', $this->testpage->heading);
37ba2528
PS
314 }
315
316 public function test_set_title() {
0d123bd7 317 // Exercise SUT.
37ba2528 318 $this->testpage->set_title('a title');
0d123bd7
PS
319 // Validated.
320 $this->assertSame('a title', $this->testpage->title);
37ba2528
PS
321 }
322
323 public function test_default_pagelayout() {
0d123bd7
PS
324 // Exercise SUT and Validate.
325 $this->assertSame('base', $this->testpage->pagelayout);
37ba2528
PS
326 }
327
328 public function test_set_pagelayout() {
0d123bd7 329 // Exercise SUT.
37ba2528 330 $this->testpage->set_pagelayout('type');
0d123bd7
PS
331 // Validated.
332 $this->assertSame('type', $this->testpage->pagelayout);
37ba2528
PS
333 }
334
335 public function test_setting_course_sets_context() {
0d123bd7 336 // Setup fixture.
37ba2528
PS
337 $course = $this->getDataGenerator()->create_course();
338 $context = context_course::instance($course->id);
339
0d123bd7 340 // Exercise SUT.
37ba2528
PS
341 $this->testpage->set_course($course);
342
0d123bd7 343 // Validated.
37ba2528
PS
344 $this->assertSame($context, $this->testpage->context);
345 }
37ba2528
PS
346
347 public function test_set_category_top_level() {
b28bb7e8 348 global $DB;
0d123bd7 349 // Setup fixture.
37ba2528 350 $cat = $this->getDataGenerator()->create_category();
b28bb7e8 351 $catdbrecord = $DB->get_record('course_categories', array('id' => $cat->id));
0d123bd7 352 // Exercise SUT.
37ba2528 353 $this->testpage->set_category_by_id($cat->id);
0d123bd7 354 // Validated.
b28bb7e8 355 $this->assertEquals($catdbrecord, $this->testpage->category);
37ba2528
PS
356 $this->assertSame(context_coursecat::instance($cat->id), $this->testpage->context);
357 }
358
359 public function test_set_nested_categories() {
b28bb7e8 360 global $DB;
0d123bd7 361 // Setup fixture.
37ba2528 362 $topcat = $this->getDataGenerator()->create_category();
b28bb7e8 363 $topcatdbrecord = $DB->get_record('course_categories', array('id' => $topcat->id));
37ba2528 364 $subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id));
b28bb7e8 365 $subcatdbrecord = $DB->get_record('course_categories', array('id' => $subcat->id));
0d123bd7 366 // Exercise SUT.
37ba2528 367 $this->testpage->set_category_by_id($subcat->id);
0d123bd7 368 // Validated.
37ba2528 369 $categories = $this->testpage->categories;
0d123bd7 370 $this->assertCount(2, $categories);
b28bb7e8
MG
371 $this->assertEquals($topcatdbrecord, array_pop($categories));
372 $this->assertEquals($subcatdbrecord, array_pop($categories));
37ba2528 373 }
37ba2528
PS
374
375 public function test_cm_null_initially() {
0d123bd7 376 // Validated.
37ba2528
PS
377 $this->assertNull($this->testpage->cm);
378 }
379
380 public function test_set_cm() {
0d123bd7 381 // Setup fixture.
37ba2528
PS
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);
0d123bd7 385 // Exercise SUT.
37ba2528 386 $this->testpage->set_cm($cm);
0d123bd7 387 // Validated.
37ba2528
PS
388 $this->assertEquals($cm->id, $this->testpage->cm->id);
389 }
390
52f3e060
RT
391 /**
392 * @expectedException coding_exception
393 */
37ba2528 394 public function test_cannot_set_activity_record_before_cm() {
0d123bd7 395 // Setup fixture.
37ba2528
PS
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);
0d123bd7 399 // Exercise SUT.
37ba2528
PS
400 $this->testpage->set_activity_record($forum);
401 }
402
403 public function test_setting_cm_sets_context() {
0d123bd7 404 // Setup fixture.
37ba2528
PS
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);
0d123bd7 408 // Exercise SUT.
37ba2528 409 $this->testpage->set_cm($cm);
0d123bd7 410 // Validated.
37ba2528
PS
411 $this->assertSame(context_module::instance($cm->id), $this->testpage->context);
412 }
413
414 public function test_activity_record_loaded_if_not_set() {
0d123bd7 415 // Setup fixture.
37ba2528
PS
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);
0d123bd7 419 // Exercise SUT.
37ba2528 420 $this->testpage->set_cm($cm);
0d123bd7 421 // Validated.
37ba2528
PS
422 unset($forum->cmid);
423 $this->assertEquals($forum, $this->testpage->activityrecord);
424 }
425
426 public function test_set_activity_record() {
0d123bd7 427 // Setup fixture.
37ba2528
PS
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);
0d123bd7 432 // Exercise SUT.
37ba2528 433 $this->testpage->set_activity_record($forum);
0d123bd7 434 // Validated.
37ba2528
PS
435 unset($forum->cmid);
436 $this->assertEquals($forum, $this->testpage->activityrecord);
437 }
438
52f3e060
RT
439 /**
440 * @expectedException coding_exception
441 */
37ba2528 442 public function test_cannot_set_inconsistent_activity_record_course() {
0d123bd7 443 // Setup fixture.
37ba2528
PS
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);
0d123bd7 448 // Exercise SUT.
37ba2528
PS
449 $forum->course = 13;
450 $this->testpage->set_activity_record($forum);
451 }
452
52f3e060
RT
453 /**
454 * @expectedException coding_exception
455 */
37ba2528 456 public function test_cannot_set_inconsistent_activity_record_instance() {
0d123bd7 457 // Setup fixture.
37ba2528
PS
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);
0d123bd7 462 // Exercise SUT.
37ba2528
PS
463 $forum->id = 13;
464 $this->testpage->set_activity_record($forum);
465 }
466
467 public function test_setting_cm_sets_course() {
0d123bd7 468 // Setup fixture.
37ba2528
PS
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);
0d123bd7 472 // Exercise SUT.
37ba2528 473 $this->testpage->set_cm($cm);
0d123bd7 474 // Validated.
37ba2528
PS
475 $this->assertEquals($course->id, $this->testpage->course->id);
476 }
477
478 public function test_set_cm_with_course_and_activity_no_db() {
0d123bd7 479 // Setup fixture.
37ba2528
PS
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
0d123bd7 484 // Exercise SUT.
37ba2528 485 $this->testpage->set_cm($cm, $course, $forum);
0d123bd7 486 // Validated.
37ba2528
PS
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);
491 }
492
52f3e060
RT
493 /**
494 * @expectedException coding_exception
495 */
37ba2528 496 public function test_cannot_set_cm_with_inconsistent_course() {
0d123bd7 497 // Setup fixture.
37ba2528
PS
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);
0d123bd7 501 // Exercise SUT.
37ba2528
PS
502 $cm->course = 13;
503 $this->testpage->set_cm($cm, $course);
504 }
505
506 public function test_get_activity_name() {
0d123bd7 507 // Setup fixture.
37ba2528
PS
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);
0d123bd7 511 // Exercise SUT.
37ba2528 512 $this->testpage->set_cm($cm, $course, $forum);
0d123bd7
PS
513 // Validated.
514 $this->assertSame('forum', $this->testpage->activityname);
37ba2528 515 }
37ba2528 516
0d123bd7
PS
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.
37ba2528 520
0d123bd7
PS
521 // Setup fixture.
522 global $USER;
37ba2528 523
b0c6dc1c 524 $this->testpage->set_context(context_system::instance());
0d123bd7 525 $this->setAdminUser();
37ba2528 526
37ba2528 527 $USER->editing = true;
0d123bd7 528 // Validated.
37ba2528
PS
529 $this->assertTrue($this->testpage->user_is_editing());
530 }
531
37ba2528 532 public function test_user_is_editing_off() {
0d123bd7
PS
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.
535
536 // Setup fixture.
37ba2528 537 global $USER;
0d123bd7
PS
538
539 $this->testpage->set_context(context_system::instance());
540 $this->setAdminUser();
541
37ba2528 542 $USER->editing = false;
0d123bd7 543 // Validated.
37ba2528
PS
544 $this->assertFalse($this->testpage->user_is_editing());
545 }
546
547 public function test_default_editing_capabilities() {
0d123bd7
PS
548 $this->testpage->set_context(context_system::instance());
549 $this->setAdminUser();
550
551 // Validated.
37ba2528
PS
552 $this->assertEquals(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
553 }
554
555 public function test_other_block_editing_cap() {
0d123bd7
PS
556 $this->testpage->set_context(context_system::instance());
557 $this->setAdminUser();
558
559 // Exercise SUT.
37ba2528 560 $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
0d123bd7 561 // Validated.
37ba2528
PS
562 $this->assertEquals(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
563 }
564
565 public function test_other_editing_cap() {
0d123bd7
PS
566 $this->testpage->set_context(context_system::instance());
567 $this->setAdminUser();
568
569 // Exercise SUT.
37ba2528 570 $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
0d123bd7 571 // Validated.
37ba2528
PS
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));
575 }
576
577 public function test_other_editing_caps() {
0d123bd7
PS
578 $this->testpage->set_context(context_system::instance());
579 $this->setAdminUser();
580
581 // Exercise SUT.
37ba2528 582 $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
0d123bd7 583 // Validated.
37ba2528
PS
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));
587 }
37ba2528 588
166ac0a3
SH
589 /**
590 * Test getting a renderer.
591 */
592 public function test_get_renderer() {
593 global $OUTPUT, $PAGE;
594 $oldoutput = $OUTPUT;
595 $oldpage = $PAGE;
596 $PAGE = $this->testpage;
597
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));
609
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'));
612
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);
619 }
620
621 $PAGE = $oldpage;
622 $OUTPUT = $oldoutput;
623 }
624
625 /**
626 * Tests getting a renderer with a maintenance layout.
627 *
628 * This layout has special hacks in place in order to deliver a "maintenance" renderer.
629 */
630 public function test_get_renderer_maintenance() {
631 global $OUTPUT, $PAGE;
632 $oldoutput = $OUTPUT;
633 $oldpage = $PAGE;
634 $PAGE = $this->testpage;
635
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'));
650
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);
656 }
657
658 $PAGE = $oldpage;
659 $OUTPUT = $oldoutput;
660 }
34f5a2bc
RS
661
662 public function test_render_to_cli() {
663 global $OUTPUT;
664
665 $footer = $OUTPUT->footer();
666 $this->assertEmpty($footer, 'cli output does not have a footer.');
667 }
853508ee
AN
668
669 /**
670 * Validate the theme value depending on the user theme and cohorts.
671 *
672 * @dataProvider get_user_theme_provider
673 */
674 public function test_cohort_get_user_theme($usertheme, $sitetheme, $cohortthemes, $expected) {
675 global $DB, $PAGE, $USER;
676
677 $this->resetAfterTest();
678
679 // Enable cohort themes.
680 set_config('allowuserthemes', 1);
681 set_config('allowcohortthemes', 1);
682
683 $systemctx = context_system::instance();
684
685 set_config('theme', $sitetheme);
686 // Create user.
687 $user = $this->getDataGenerator()->create_user(array('theme' => $usertheme));
688
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);
696 }
697
698 // Get the theme and compare to the expected.
699 $this->setUser($user);
700
701 // Initialise user theme.
702 $USER = get_complete_user_data('id', $user->id);
703
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);
709 }
710
711 /**
712 * Some user cases for validating the expected theme depending on the cohorts, site and user values.
713 *
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.
720 * ]
721 *
722 * @return array
723 */
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',
731 ],
732 'User member of one cohort which has a theme set' => [
733 'usertheme' => '',
734 'sitetheme' => 'boost',
735 'cohorts' => [
e00f1c66 736 'classic',
853508ee 737 ],
e00f1c66 738 'expected' => 'classic',
853508ee
AN
739 ],
740 'User member of one cohort which has a theme set, and one without a theme' => [
741 'usertheme' => '',
742 'sitetheme' => 'boost',
743 'cohorts' => [
e00f1c66 744 'classic',
853508ee
AN
745 '',
746 ],
e00f1c66 747 'expected' => 'classic',
853508ee
AN
748 ],
749 'User member of one cohort which has a theme set, and one with a different theme' => [
750 'usertheme' => '',
751 'sitetheme' => 'boost',
752 'cohorts' => [
e00f1c66 753 'classic',
853508ee
AN
754 'someother',
755 ],
756 'expected' => 'boost',
757 ],
758 'User with a theme but not a member of any cohort' => [
e00f1c66 759 'usertheme' => 'classic',
853508ee
AN
760 'sitetheme' => 'boost',
761 'cohorts' => [],
e00f1c66 762 'expected' => 'classic',
853508ee
AN
763 ],
764 'User with a theme and member of one cohort which has a theme set' => [
e00f1c66 765 'usertheme' => 'classic',
853508ee
AN
766 'sitetheme' => 'boost',
767 'cohorts' => [
e00f1c66 768 'boost',
853508ee 769 ],
e00f1c66 770 'expected' => 'classic',
853508ee
AN
771 ],
772 ];
773 }
166ac0a3 774}
0d123bd7
PS
775
776/**
777 * Test-specific subclass to make some protected things public.
778 */
779class testable_moodle_page extends moodle_page {
780 public function initialise_default_pagetype($script = null) {
781 parent::initialise_default_pagetype($script);
782 }
783 public function url_to_class_name($url) {
784 return parent::url_to_class_name($url);
785 }
786 public function all_editing_caps() {
787 return parent::all_editing_caps();
788 }
789}