MDL-47806: Add footer function for command line output.
[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
86 public function test_cannot_set_course_once_theme_set() {
0d123bd7 87 // Setup fixture.
37ba2528
PS
88 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
89 $course = $this->getDataGenerator()->create_course();
90 // Set expectation.
91 $this->setExpectedException('coding_exception');
0d123bd7 92 // Exercise SUT.
37ba2528
PS
93 $this->testpage->set_course($course);
94 }
95
96 public function test_cannot_set_category_once_theme_set() {
0d123bd7 97 // Setup fixture.
37ba2528
PS
98 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
99 // Set expectation.
100 $this->setExpectedException('coding_exception');
0d123bd7 101 // Exercise SUT.
37ba2528
PS
102 $this->testpage->set_category_by_id(123);
103 }
104
105 public function test_cannot_set_category_once_course_set() {
0d123bd7 106 // Setup fixture.
37ba2528 107 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 108 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
37ba2528
PS
109 $this->testpage->set_course($course);
110 // Set expectation.
111 $this->setExpectedException('coding_exception');
0d123bd7 112 // Exercise SUT.
37ba2528
PS
113 $this->testpage->set_category_by_id(123);
114 }
115
116 public function test_categories_array_empty_for_front_page() {
117 global $SITE;
0d123bd7 118 // Setup fixture.
b0c6dc1c 119 $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
37ba2528
PS
120 $this->testpage->set_course($SITE);
121 // Exercise SUT and validate.
122 $this->assertEquals(array(), $this->testpage->categories);
123 }
124
125 public function test_set_state_normal_path() {
126 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 127 $this->testpage->set_context(context_system::instance());
37ba2528
PS
128 $this->testpage->set_course($course);
129
130 $this->assertEquals(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
131
132 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
133 $this->assertEquals(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
134
135 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
136 $this->assertEquals(moodle_page::STATE_IN_BODY, $this->testpage->state);
137
138 $this->testpage->set_state(moodle_page::STATE_DONE);
139 $this->assertEquals(moodle_page::STATE_DONE, $this->testpage->state);
140 }
141
142 public function test_set_state_cannot_skip_one() {
143 // Set expectation.
144 $this->setExpectedException('coding_exception');
0d123bd7 145 // Exercise SUT.
37ba2528
PS
146 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
147 }
148
149 public function test_header_printed_false_initially() {
0d123bd7 150 // Validated.
37ba2528
PS
151 $this->assertFalse($this->testpage->headerprinted);
152 }
153
154 public function test_header_printed_becomes_true() {
155 $course = $this->getDataGenerator()->create_course();
b0c6dc1c 156 $this->testpage->set_context(context_system::instance());
37ba2528
PS
157 $this->testpage->set_course($course);
158
0d123bd7 159 // Exercise SUT.
37ba2528
PS
160 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
161 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
0d123bd7 162 // Validated.
37ba2528
PS
163 $this->assertTrue($this->testpage->headerprinted);
164 }
165
166 public function test_set_context() {
0d123bd7 167 // Setup fixture.
37ba2528
PS
168 $course = $this->getDataGenerator()->create_course();
169 $context = context_course::instance($course->id);
0d123bd7 170 // Exercise SUT.
37ba2528 171 $this->testpage->set_context($context);
0d123bd7 172 // Validated.
37ba2528
PS
173 $this->assertSame($context, $this->testpage->context);
174 }
175
176 public function test_pagetype_defaults_to_script() {
3a7bc16a 177 global $SCRIPT;
0d123bd7 178 // Exercise SUT and validate.
3a7bc16a
PS
179 $SCRIPT = '/index.php';
180 $this->testpage->initialise_default_pagetype();
0d123bd7 181 $this->assertSame('site-index', $this->testpage->pagetype);
37ba2528
PS
182 }
183
184 public function test_set_pagetype() {
0d123bd7 185 // Exercise SUT.
37ba2528 186 $this->testpage->set_pagetype('a-page-type');
0d123bd7
PS
187 // Validated.
188 $this->assertSame('a-page-type', $this->testpage->pagetype);
37ba2528
PS
189 }
190
191 public function test_initialise_default_pagetype() {
0d123bd7 192 // Exercise SUT.
37ba2528 193 $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php');
0d123bd7
PS
194 // Validated.
195 $this->assertSame('admin-tool-unittest-index', $this->testpage->pagetype);
37ba2528
PS
196 }
197
198 public function test_initialise_default_pagetype_fp() {
0d123bd7 199 // Exercise SUT.
37ba2528 200 $this->testpage->initialise_default_pagetype('index.php');
0d123bd7
PS
201 // Validated.
202 $this->assertSame('site-index', $this->testpage->pagetype);
37ba2528
PS
203 }
204
205 public function test_get_body_classes_empty() {
0d123bd7
PS
206 // Validated.
207 $this->assertSame('', $this->testpage->bodyclasses);
37ba2528
PS
208 }
209
210 public function test_get_body_classes_single() {
0d123bd7 211 // Exercise SUT.
37ba2528 212 $this->testpage->add_body_class('aclassname');
0d123bd7
PS
213 // Validated.
214 $this->assertSame('aclassname', $this->testpage->bodyclasses);
37ba2528
PS
215 }
216
217 public function test_get_body_classes() {
0d123bd7 218 // Exercise SUT.
37ba2528 219 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
0d123bd7
PS
220 // Validated.
221 $this->assertSame('aclassname anotherclassname', $this->testpage->bodyclasses);
37ba2528
PS
222 }
223
224 public function test_url_to_class_name() {
0d123bd7
PS
225 $this->assertSame('example-com', $this->testpage->url_to_class_name('http://example.com'));
226 $this->assertSame('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
227 $this->assertSame('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
228 $this->assertSame('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
37ba2528
PS
229 }
230
231 public function test_set_docs_path() {
0d123bd7 232 // Exercise SUT.
37ba2528 233 $this->testpage->set_docs_path('a/file/path');
0d123bd7
PS
234 // Validated.
235 $this->assertSame('a/file/path', $this->testpage->docspath);
37ba2528
PS
236 }
237
238 public function test_docs_path_defaults_from_pagetype() {
0d123bd7 239 // Exercise SUT.
37ba2528 240 $this->testpage->set_pagetype('a-page-type');
0d123bd7
PS
241 // Validated.
242 $this->assertSame('a/page/type', $this->testpage->docspath);
37ba2528
PS
243 }
244
245 public function test_set_url_root() {
246 global $CFG;
0d123bd7 247 // Exercise SUT.
37ba2528 248 $this->testpage->set_url('/');
0d123bd7
PS
249 // Validated.
250 $this->assertSame($CFG->wwwroot . '/', $this->testpage->url->out());
37ba2528
PS
251 }
252
253 public function test_set_url_one_param() {
254 global $CFG;
0d123bd7 255 // Exercise SUT.
37ba2528 256 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
0d123bd7
PS
257 // Validated.
258 $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
37ba2528
PS
259 }
260
261 public function test_set_url_two_params() {
262 global $CFG;
0d123bd7 263 // Exercise SUT.
37ba2528 264 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
0d123bd7
PS
265 // Validated.
266 $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
37ba2528
PS
267 }
268
269 public function test_set_url_using_moodle_url() {
270 global $CFG;
0d123bd7 271 // Fixture setup.
37ba2528 272 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
0d123bd7 273 // Exercise SUT.
37ba2528 274 $this->testpage->set_url($url);
0d123bd7
PS
275 // Validated.
276 $this->assertSame($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
37ba2528
PS
277 }
278
279 public function test_set_url_sets_page_type() {
0d123bd7 280 // Exercise SUT.
37ba2528 281 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
0d123bd7
PS
282 // Validated.
283 $this->assertSame('mod-quiz-attempt', $this->testpage->pagetype);
37ba2528
PS
284 }
285
286 public function test_set_url_does_not_change_explicit_page_type() {
0d123bd7 287 // Setup fixture.
37ba2528 288 $this->testpage->set_pagetype('a-page-type');
0d123bd7 289 // Exercise SUT.
37ba2528 290 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
0d123bd7
PS
291 // Validated.
292 $this->assertSame('a-page-type', $this->testpage->pagetype);
37ba2528
PS
293 }
294
295 public function test_set_subpage() {
0d123bd7 296 // Exercise SUT.
37ba2528 297 $this->testpage->set_subpage('somestring');
0d123bd7
PS
298 // Validated.
299 $this->assertSame('somestring', $this->testpage->subpage);
37ba2528
PS
300 }
301
302 public function test_set_heading() {
0d123bd7 303 // Exercise SUT.
37ba2528 304 $this->testpage->set_heading('a heading');
0d123bd7
PS
305 // Validated.
306 $this->assertSame('a heading', $this->testpage->heading);
37ba2528
PS
307 }
308
309 public function test_set_title() {
0d123bd7 310 // Exercise SUT.
37ba2528 311 $this->testpage->set_title('a title');
0d123bd7
PS
312 // Validated.
313 $this->assertSame('a title', $this->testpage->title);
37ba2528
PS
314 }
315
316 public function test_default_pagelayout() {
0d123bd7
PS
317 // Exercise SUT and Validate.
318 $this->assertSame('base', $this->testpage->pagelayout);
37ba2528
PS
319 }
320
321 public function test_set_pagelayout() {
0d123bd7 322 // Exercise SUT.
37ba2528 323 $this->testpage->set_pagelayout('type');
0d123bd7
PS
324 // Validated.
325 $this->assertSame('type', $this->testpage->pagelayout);
37ba2528
PS
326 }
327
328 public function test_setting_course_sets_context() {
0d123bd7 329 // Setup fixture.
37ba2528
PS
330 $course = $this->getDataGenerator()->create_course();
331 $context = context_course::instance($course->id);
332
0d123bd7 333 // Exercise SUT.
37ba2528
PS
334 $this->testpage->set_course($course);
335
0d123bd7 336 // Validated.
37ba2528
PS
337 $this->assertSame($context, $this->testpage->context);
338 }
37ba2528
PS
339
340 public function test_set_category_top_level() {
b28bb7e8 341 global $DB;
0d123bd7 342 // Setup fixture.
37ba2528 343 $cat = $this->getDataGenerator()->create_category();
b28bb7e8 344 $catdbrecord = $DB->get_record('course_categories', array('id' => $cat->id));
0d123bd7 345 // Exercise SUT.
37ba2528 346 $this->testpage->set_category_by_id($cat->id);
0d123bd7 347 // Validated.
b28bb7e8 348 $this->assertEquals($catdbrecord, $this->testpage->category);
37ba2528
PS
349 $this->assertSame(context_coursecat::instance($cat->id), $this->testpage->context);
350 }
351
352 public function test_set_nested_categories() {
b28bb7e8 353 global $DB;
0d123bd7 354 // Setup fixture.
37ba2528 355 $topcat = $this->getDataGenerator()->create_category();
b28bb7e8 356 $topcatdbrecord = $DB->get_record('course_categories', array('id' => $topcat->id));
37ba2528 357 $subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id));
b28bb7e8 358 $subcatdbrecord = $DB->get_record('course_categories', array('id' => $subcat->id));
0d123bd7 359 // Exercise SUT.
37ba2528 360 $this->testpage->set_category_by_id($subcat->id);
0d123bd7 361 // Validated.
37ba2528 362 $categories = $this->testpage->categories;
0d123bd7 363 $this->assertCount(2, $categories);
b28bb7e8
MG
364 $this->assertEquals($topcatdbrecord, array_pop($categories));
365 $this->assertEquals($subcatdbrecord, array_pop($categories));
37ba2528 366 }
37ba2528
PS
367
368 public function test_cm_null_initially() {
0d123bd7 369 // Validated.
37ba2528
PS
370 $this->assertNull($this->testpage->cm);
371 }
372
373 public function test_set_cm() {
0d123bd7 374 // Setup fixture.
37ba2528
PS
375 $course = $this->getDataGenerator()->create_course();
376 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
377 $cm = get_coursemodule_from_id('forum', $forum->cmid);
0d123bd7 378 // Exercise SUT.
37ba2528 379 $this->testpage->set_cm($cm);
0d123bd7 380 // Validated.
37ba2528
PS
381 $this->assertEquals($cm->id, $this->testpage->cm->id);
382 }
383
384 public function test_cannot_set_activity_record_before_cm() {
0d123bd7 385 // Setup fixture.
37ba2528
PS
386 $course = $this->getDataGenerator()->create_course();
387 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
388 $cm = get_coursemodule_from_id('forum', $forum->cmid);
0d123bd7 389 // Set expectation.
37ba2528 390 $this->setExpectedException('coding_exception');
0d123bd7 391 // Exercise SUT.
37ba2528
PS
392 $this->testpage->set_activity_record($forum);
393 }
394
395 public function test_setting_cm_sets_context() {
0d123bd7 396 // Setup fixture.
37ba2528
PS
397 $course = $this->getDataGenerator()->create_course();
398 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
399 $cm = get_coursemodule_from_id('forum', $forum->cmid);
0d123bd7 400 // Exercise SUT.
37ba2528 401 $this->testpage->set_cm($cm);
0d123bd7 402 // Validated.
37ba2528
PS
403 $this->assertSame(context_module::instance($cm->id), $this->testpage->context);
404 }
405
406 public function test_activity_record_loaded_if_not_set() {
0d123bd7 407 // Setup fixture.
37ba2528
PS
408 $course = $this->getDataGenerator()->create_course();
409 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
410 $cm = get_coursemodule_from_id('forum', $forum->cmid);
0d123bd7 411 // Exercise SUT.
37ba2528 412 $this->testpage->set_cm($cm);
0d123bd7 413 // Validated.
37ba2528
PS
414 unset($forum->cmid);
415 $this->assertEquals($forum, $this->testpage->activityrecord);
416 }
417
418 public function test_set_activity_record() {
0d123bd7 419 // Setup fixture.
37ba2528
PS
420 $course = $this->getDataGenerator()->create_course();
421 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
422 $cm = get_coursemodule_from_id('forum', $forum->cmid);
423 $this->testpage->set_cm($cm);
0d123bd7 424 // Exercise SUT.
37ba2528 425 $this->testpage->set_activity_record($forum);
0d123bd7 426 // Validated.
37ba2528
PS
427 unset($forum->cmid);
428 $this->assertEquals($forum, $this->testpage->activityrecord);
429 }
430
431 public function test_cannot_set_inconsistent_activity_record_course() {
0d123bd7 432 // Setup fixture.
37ba2528
PS
433 $course = $this->getDataGenerator()->create_course();
434 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
435 $cm = get_coursemodule_from_id('forum', $forum->cmid);
436 $this->testpage->set_cm($cm);
0d123bd7 437 // Set expectation.
37ba2528 438 $this->setExpectedException('coding_exception');
0d123bd7 439 // Exercise SUT.
37ba2528
PS
440 $forum->course = 13;
441 $this->testpage->set_activity_record($forum);
442 }
443
444 public function test_cannot_set_inconsistent_activity_record_instance() {
0d123bd7 445 // Setup fixture.
37ba2528
PS
446 $course = $this->getDataGenerator()->create_course();
447 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
448 $cm = get_coursemodule_from_id('forum', $forum->cmid);
449 $this->testpage->set_cm($cm);
0d123bd7 450 // Set expectation.
37ba2528 451 $this->setExpectedException('coding_exception');
0d123bd7 452 // Exercise SUT.
37ba2528
PS
453 $forum->id = 13;
454 $this->testpage->set_activity_record($forum);
455 }
456
457 public function test_setting_cm_sets_course() {
0d123bd7 458 // Setup fixture.
37ba2528
PS
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);
0d123bd7 462 // Exercise SUT.
37ba2528 463 $this->testpage->set_cm($cm);
0d123bd7 464 // Validated.
37ba2528
PS
465 $this->assertEquals($course->id, $this->testpage->course->id);
466 }
467
468 public function test_set_cm_with_course_and_activity_no_db() {
0d123bd7 469 // Setup fixture.
37ba2528
PS
470 $course = $this->getDataGenerator()->create_course();
471 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
472 $cm = get_coursemodule_from_id('forum', $forum->cmid);
473 // This only works without db if we already have modinfo cache
0d123bd7 474 // Exercise SUT.
37ba2528 475 $this->testpage->set_cm($cm, $course, $forum);
0d123bd7 476 // Validated.
37ba2528
PS
477 $this->assertEquals($cm->id, $this->testpage->cm->id);
478 $this->assertEquals($course->id, $this->testpage->course->id);
479 unset($forum->cmid);
480 $this->assertEquals($forum, $this->testpage->activityrecord);
481 }
482
483 public function test_cannot_set_cm_with_inconsistent_course() {
0d123bd7 484 // Setup fixture.
37ba2528
PS
485 $course = $this->getDataGenerator()->create_course();
486 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
487 $cm = get_coursemodule_from_id('forum', $forum->cmid);
0d123bd7 488 // Set expectation.
37ba2528 489 $this->setExpectedException('coding_exception');
0d123bd7 490 // Exercise SUT.
37ba2528
PS
491 $cm->course = 13;
492 $this->testpage->set_cm($cm, $course);
493 }
494
495 public function test_get_activity_name() {
0d123bd7 496 // Setup fixture.
37ba2528
PS
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);
0d123bd7 500 // Exercise SUT.
37ba2528 501 $this->testpage->set_cm($cm, $course, $forum);
0d123bd7
PS
502 // Validated.
503 $this->assertSame('forum', $this->testpage->activityname);
37ba2528 504 }
37ba2528 505
0d123bd7
PS
506 public function test_user_is_editing_on() {
507 // We are relying on the fact that unit tests are always run by admin, to
508 // ensure the user_allows_editing call returns true.
37ba2528 509
0d123bd7
PS
510 // Setup fixture.
511 global $USER;
37ba2528 512
b0c6dc1c 513 $this->testpage->set_context(context_system::instance());
0d123bd7 514 $this->setAdminUser();
37ba2528 515
37ba2528 516 $USER->editing = true;
0d123bd7 517 // Validated.
37ba2528
PS
518 $this->assertTrue($this->testpage->user_is_editing());
519 }
520
37ba2528 521 public function test_user_is_editing_off() {
0d123bd7
PS
522 // We are relying on the fact that unit tests are always run by admin, to
523 // ensure the user_allows_editing call returns true.
524
525 // Setup fixture.
37ba2528 526 global $USER;
0d123bd7
PS
527
528 $this->testpage->set_context(context_system::instance());
529 $this->setAdminUser();
530
37ba2528 531 $USER->editing = false;
0d123bd7 532 // Validated.
37ba2528
PS
533 $this->assertFalse($this->testpage->user_is_editing());
534 }
535
536 public function test_default_editing_capabilities() {
0d123bd7
PS
537 $this->testpage->set_context(context_system::instance());
538 $this->setAdminUser();
539
540 // Validated.
37ba2528
PS
541 $this->assertEquals(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
542 }
543
544 public function test_other_block_editing_cap() {
0d123bd7
PS
545 $this->testpage->set_context(context_system::instance());
546 $this->setAdminUser();
547
548 // Exercise SUT.
37ba2528 549 $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
0d123bd7 550 // Validated.
37ba2528
PS
551 $this->assertEquals(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
552 }
553
554 public function test_other_editing_cap() {
0d123bd7
PS
555 $this->testpage->set_context(context_system::instance());
556 $this->setAdminUser();
557
558 // Exercise SUT.
37ba2528 559 $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
0d123bd7 560 // Validated.
37ba2528
PS
561 $actualcaps = $this->testpage->all_editing_caps();
562 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
563 $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
564 }
565
566 public function test_other_editing_caps() {
0d123bd7
PS
567 $this->testpage->set_context(context_system::instance());
568 $this->setAdminUser();
569
570 // Exercise SUT.
37ba2528 571 $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
0d123bd7 572 // Validated.
37ba2528
PS
573 $actualcaps = $this->testpage->all_editing_caps();
574 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
575 $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
576 }
37ba2528 577
166ac0a3
SH
578 /**
579 * Test getting a renderer.
580 */
581 public function test_get_renderer() {
582 global $OUTPUT, $PAGE;
583 $oldoutput = $OUTPUT;
584 $oldpage = $PAGE;
585 $PAGE = $this->testpage;
586
587 $this->testpage->set_pagelayout('standard');
588 $this->assertEquals('standard', $this->testpage->pagelayout);
589 // Initialise theme and output for the next tests.
590 $this->testpage->initialise_theme_and_output();
591 // Check the generated $OUTPUT object is a core renderer.
592 $this->assertInstanceOf('core_renderer', $OUTPUT);
593 // Check we can get a core renderer if we explicitly request one (no component).
594 $this->assertInstanceOf('core_renderer', $this->testpage->get_renderer('core'));
595 // Check we get a CLI renderer if we request a maintenance renderer. The CLI target should take precedence.
596 $this->assertInstanceOf('core_renderer_cli',
597 $this->testpage->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE));
598
599 // Check we can get a coures renderer if we explicitly request one (valid component).
600 $this->assertInstanceOf('core_course_renderer', $this->testpage->get_renderer('core', 'course'));
601
602 // Check a properly invalid component.
603 try {
604 $this->testpage->get_renderer('core', 'monkeys');
605 $this->fail('Request for renderer with invalid component didn\'t throw expected exception.');
606 } catch (coding_exception $exception) {
607 $this->assertEquals('monkeys', $exception->debuginfo);
608 }
609
610 $PAGE = $oldpage;
611 $OUTPUT = $oldoutput;
612 }
613
614 /**
615 * Tests getting a renderer with a maintenance layout.
616 *
617 * This layout has special hacks in place in order to deliver a "maintenance" renderer.
618 */
619 public function test_get_renderer_maintenance() {
620 global $OUTPUT, $PAGE;
621 $oldoutput = $OUTPUT;
622 $oldpage = $PAGE;
623 $PAGE = $this->testpage;
624
625 $this->testpage->set_pagelayout('maintenance');
626 $this->assertEquals('maintenance', $this->testpage->pagelayout);
627 // Initialise theme and output for the next tests.
628 $this->testpage->initialise_theme_and_output();
629 // Check the generated $OUTPUT object is a core cli renderer.
630 // It shouldn't be maintenance because there the cli target should take greater precedence.
631 $this->assertInstanceOf('core_renderer_cli', $OUTPUT);
632 // Check we can get a core renderer if we explicitly request one (no component).
633 $this->assertInstanceOf('core_renderer', $this->testpage->get_renderer('core'));
634 // Check we get a CLI renderer if we request a maintenance renderer. The CLI target should take precedence.
635 $this->assertInstanceOf('core_renderer_cli',
636 $this->testpage->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE));
637 // Check we can get a coures renderer if we explicitly request one (valid component).
638 $this->assertInstanceOf('core_course_renderer', $this->testpage->get_renderer('core', 'course'));
639
640 try {
641 $this->testpage->get_renderer('core', 'monkeys');
642 $this->fail('Request for renderer with invalid component didn\'t throw expected exception.');
643 } catch (coding_exception $exception) {
644 $this->assertEquals('monkeys', $exception->debuginfo);
645 }
646
647 $PAGE = $oldpage;
648 $OUTPUT = $oldoutput;
649 }
34f5a2bc
RS
650
651 public function test_render_to_cli() {
652 global $OUTPUT;
653
654 $footer = $OUTPUT->footer();
655 $this->assertEmpty($footer, 'cli output does not have a footer.');
656 }
166ac0a3 657}
0d123bd7
PS
658
659/**
660 * Test-specific subclass to make some protected things public.
661 */
662class testable_moodle_page extends moodle_page {
663 public function initialise_default_pagetype($script = null) {
664 parent::initialise_default_pagetype($script);
665 }
666 public function url_to_class_name($url) {
667 return parent::url_to_class_name($url);
668 }
669 public function all_editing_caps() {
670 return parent::all_editing_caps();
671 }
672}