MDL-23979 fixed old style url init
[moodle.git] / lib / simpletest / testpagelib_moodlepage.php
CommitLineData
8926f844 1<?php
2afe21ee 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
93d4a373 27 * Tests for the moodle_page class in ../pagelib.php.
2afe21ee 28 *
29 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30 * @package moodlecore
31 */
32
33if (!defined('MOODLE_INTERNAL')) {
34 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
35}
36
37require_once($CFG->libdir . '/pagelib.php');
c33db6e6 38require_once($CFG->libdir . '/blocklib.php');
2afe21ee 39
c33db6e6 40/** Test-specific subclass to make some protected things public. */
e88462a0 41class testable_moodle_page extends moodle_page {
ad52c04f 42 public function initialise_default_pagetype($script = null) {
e88462a0 43 parent::initialise_default_pagetype($script);
44 }
89fbdca3 45 public function url_to_class_name($url) {
46 return parent::url_to_class_name($url);
47 }
934524d7 48 public function all_editing_caps() {
49 return parent::all_editing_caps();
50 }
e88462a0 51}
52
2afe21ee 53/**
c33db6e6 54 * Test functions that don't need to touch the database.
2afe21ee 55 */
56class moodle_page_test extends UnitTestCase {
57 protected $testpage;
58 protected $originalcourse;
7d875874 59 protected $originalpage;
081a63a9 60 public static $includecoverage = array('lib/pagelib.php', 'lib/blocklib.php');
2afe21ee 61
62 public function setUp() {
7d875874 63 global $COURSE, $PAGE;
2afe21ee 64 $this->originalcourse = $COURSE;
7d875874 65 $this->originalpage = $PAGE;
e88462a0 66 $this->testpage = new testable_moodle_page();
2afe21ee 67 }
68
69 public function tearDown() {
70 global $COURSE;
71 $this->testpage = NULL;
72 $COURSE = $this->originalcourse;
7d875874 73 $PAGE = $this->originalpage;
2afe21ee 74 }
75
76 /** Creates an object with all the fields you would expect a $course object to have. */
77 protected function create_a_course() {
78 $course = new stdClass;
d0212567 79 $course->id = 13; // Why 13, good question. MDL-21007
2afe21ee 80 $course->category = 2;
81 $course->fullname = 'Anonymous test course';
82 $course->shortname = 'ANON';
83 $course->summary = '';
84 return $course;
85 }
86
87 /** Creates an object with all the fields you would expect a $course object to have. */
88 protected function create_a_context() {
89 $context = new stdClass;
90 $context->id = 2;
91 $context->contextlevel = CONTEXT_COURSECAT;
92 $context->instanceid = 1;
93 $context->path = '/1/2';
94 $context->depth = '2';
95 return $context;
96 }
97
98 public function test_course_returns_site_before_set() {
99 global $SITE;
100 // Validate
101 $this->assertIdentical($SITE, $this->testpage->course);
102 }
103
104 public function test_setting_course_works() {
105 // Setup fixture
106 $course = $this->create_a_course();
f69bdd00 107 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
2afe21ee 108 // Exercise SUT
109 $this->testpage->set_course($course);
110 // Validate
111 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course);
112 }
113
7d875874 114 public function test_global_course_and_page_course_are_same_with_global_page() {
115 global $COURSE, $PAGE;
2afe21ee 116 // Setup fixture
117 $course = $this->create_a_course();
f69bdd00 118 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
7d875874 119 $PAGE = $this->testpage;
2afe21ee 120 // Exercise SUT
121 $this->testpage->set_course($course);
122 // Validate
123 $this->assertIdentical($this->testpage->course, $COURSE);
124 }
125
7d875874 126 public function test_global_course_not_changed_with_non_global_page() {
127 global $COURSE;
128 $originalcourse = $COURSE;
2afe21ee 129 // Setup fixture
7d875874 130 $course = $this->create_a_course();
f69bdd00 131 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
7d875874 132 // Exercise SUT
133 $this->testpage->set_course($course);
134 // Validate
135 $this->assertIdentical($originalcourse, $COURSE);
136 }
137
138 public function test_cannot_set_course_once_theme_set() {
139 // Setup fixture
5f0baa43 140 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
2afe21ee 141 $course = $this->create_a_course();
142 // Set expectation.
143 $this->expectException();
144 // Exercise SUT
145 $this->testpage->set_course($course);
146 }
147
7d875874 148 public function test_cannot_set_category_once_theme_set() {
948203a5 149 // Setup fixture
5f0baa43 150 $this->testpage->force_theme(theme_config::DEFAULT_THEME);
948203a5 151 // Set expectation.
152 $this->expectException();
153 // Exercise SUT
154 $this->testpage->set_category_by_id(123);
155 }
156
157 public function test_cannot_set_category_once_course_set() {
158 // Setup fixture
159 $course = $this->create_a_course();
f69bdd00 160 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
948203a5 161 $this->testpage->set_course($course);
162 // Set expectation.
163 $this->expectException();
164 // Exercise SUT
165 $this->testpage->set_category_by_id(123);
166 }
167
d7ab8879 168 public function test_categories_array_empty_for_front_page() {
169 // Setup fixture
170 $course = $this->create_a_course();
171 $course->category = 0;
f69bdd00 172 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
d7ab8879 173 $this->testpage->set_course($course);
174 // Exercise SUT and validate.
175 $this->assertEqual(array(), $this->testpage->categories);
176 }
177
2afe21ee 178 public function test_set_state_normal_path() {
7d875874 179 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
78946b9b 180 $this->testpage->set_course($this->create_a_course());
7d875874 181
78946b9b 182 $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
2afe21ee 183
184 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
185 $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
186
187 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
188 $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state);
189
2afe21ee 190 $this->testpage->set_state(moodle_page::STATE_DONE);
191 $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state);
192 }
193
194 public function test_set_state_cannot_skip_one() {
195 // Set expectation.
196 $this->expectException();
197 // Exercise SUT
198 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
199 }
200
201 public function test_header_printed_false_initially() {
202 // Validate
203 $this->assertFalse($this->testpage->headerprinted);
204 }
205
206 public function test_header_printed_becomes_true() {
7d875874 207 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
208 $this->testpage->set_course($this->create_a_course());
209
2afe21ee 210 // Exercise SUT
211 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
212 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
213 // Validate
214 $this->assertTrue($this->testpage->headerprinted);
215 }
216
2afe21ee 217 public function test_set_context() {
218 // Setup fixture
219 $context = $this->create_a_context();
220 // Exercise SUT
221 $this->testpage->set_context($context);
222 // Validate
223 $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context);
224 }
f230ce19 225
e88462a0 226 public function test_pagetype_defaults_to_script() {
227 // Exercise SUT and validate
228 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
f230ce19 229 }
230
231 public function test_set_pagetype() {
232 // Exercise SUT
233 $this->testpage->set_pagetype('a-page-type');
234 // Validate
235 $this->assertEqual('a-page-type', $this->testpage->pagetype);
236 }
e88462a0 237
238 public function test_initialise_default_pagetype() {
239 // Exercise SUT
240 $this->testpage->initialise_default_pagetype('admin/report/unittest/index.php');
241 // Validate
242 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
243 }
244
245 public function test_initialise_default_pagetype_fp() {
246 // Exercise SUT
247 $this->testpage->initialise_default_pagetype('index.php');
248 // Validate
249 $this->assertEqual('site-index', $this->testpage->pagetype);
250 }
753debd2 251
252 public function test_get_body_classes_empty() {
253 // Validate
254 $this->assertEqual('', $this->testpage->bodyclasses);
255 }
256
257 public function test_get_body_classes_single() {
258 // Exercise SUT
259 $this->testpage->add_body_class('aclassname');
260 // Validate
261 $this->assertEqual('aclassname', $this->testpage->bodyclasses);
262 }
263
89fbdca3 264 public function test_get_body_classes() {
753debd2 265 // Exercise SUT
89fbdca3 266 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
753debd2 267 // Validate
268 $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses);
269 }
89fbdca3 270
271 public function test_url_to_class_name() {
272 $this->assertEqual('example-com', $this->testpage->url_to_class_name('http://example.com'));
273 $this->assertEqual('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
274 $this->assertEqual('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
275 $this->assertEqual('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
276 }
82611d8d 277
278 public function test_set_docs_path() {
279 // Exercise SUT
280 $this->testpage->set_docs_path('a/file/path');
281 // Validate
282 $this->assertEqual('a/file/path', $this->testpage->docspath);
283 }
284
285 public function test_docs_path_defaults_from_pagetype() {
286 // Exercise SUT
287 $this->testpage->set_pagetype('a-page-type');
288 // Validate
289 $this->assertEqual('a/page/type', $this->testpage->docspath);
290 }
75781f87 291
292 public function test_set_url_root() {
293 global $CFG;
294 // Exercise SUT
61ca8f73 295 $this->testpage->set_url('/');
75781f87 296 // Validate
297 $this->assertEqual($CFG->wwwroot . '/', $this->testpage->url->out());
298 }
299
300 public function test_set_url_one_param() {
301 global $CFG;
302 // Exercise SUT
61ca8f73 303 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
75781f87 304 // Validate
305 $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
306 }
307
308 public function test_set_url_two_params() {
309 global $CFG;
310 // Exercise SUT
61ca8f73 311 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
75781f87 312 // Validate
313 $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
314 }
315
7dc928b5 316 public function test_set_url_using_moodle_url() {
317 global $CFG;
318 // Fixture setup
a6855934 319 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
7dc928b5 320 // Exercise SUT
321 $this->testpage->set_url($url);
322 // Validate
323 $this->assertEqual($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
324 }
325
75781f87 326 public function test_set_url_sets_page_type() {
327 // Exercise SUT
61ca8f73 328 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
75781f87 329 // Validate
330 $this->assertEqual('mod-quiz-attempt', $this->testpage->pagetype);
331 }
332
333 public function test_set_url_does_not_change_explicit_page_type() {
334 // Setup fixture
335 $this->testpage->set_pagetype('a-page-type');
336 // Exercise SUT
61ca8f73 337 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
75781f87 338 // Validate
339 $this->assertEqual('a-page-type', $this->testpage->pagetype);
340 }
5ed70539 341
342 public function test_set_subpage() {
343 // Exercise SUT
344 $this->testpage->set_subpage('somestring');
345 // Validate
346 $this->assertEqual('somestring', $this->testpage->subpage);
347 }
34a2777c 348
349 public function test_set_heading() {
350 // Exercise SUT
351 $this->testpage->set_heading('a heading');
352 // Validate
353 $this->assertEqual('a heading', $this->testpage->heading);
354 }
355
356 public function test_set_title() {
357 // Exercise SUT
358 $this->testpage->set_title('a title');
359 // Validate
360 $this->assertEqual('a title', $this->testpage->title);
361 }
362
78946b9b 363 public function test_default_pagelayout() {
34a2777c 364 // Exercise SUT and Validate
61ca8f73 365 $this->assertEqual('base', $this->testpage->pagelayout);
34a2777c 366 }
367
78946b9b 368 public function test_set_pagelayout() {
34a2777c 369 // Exercise SUT
78946b9b 370 $this->testpage->set_pagelayout('type');
34a2777c 371 // Validate
78946b9b 372 $this->assertEqual('type', $this->testpage->pagelayout);
34a2777c 373 }
2afe21ee 374}
375
376/**
948203a5 377 * Test functions that rely on the context table.
2afe21ee 378 */
948203a5 379class moodle_page_with_context_table_test extends UnitTestCaseUsingDatabase {
2afe21ee 380 protected $testpage;
381 protected $originalcourse;
382
383 public function setUp() {
384 global $COURSE;
948203a5 385 parent::setUp();
2afe21ee 386 $this->originalcourse = $COURSE;
387 $this->testpage = new moodle_page();
388 $this->create_test_table('context', 'lib');
389 $this->switch_to_test_db();
390 }
391
392 public function tearDown() {
393 global $COURSE;
394 $this->testpage = NULL;
395 $COURSE = $this->originalcourse;
948203a5 396 parent::tearDown();
2afe21ee 397 }
398
399 /** Creates an object with all the fields you would expect a $course object to have. */
400 protected function create_a_course_with_context() {
401 $course = new stdClass;
d0212567 402 $course->id = 13;
2afe21ee 403 $course->category = 2;
404 $course->fullname = 'Anonymous test course';
405 $course->shortname = 'ANON';
406 $course->summary = '';
407
408 $context = new stdClass;
409 $context->contextlevel = CONTEXT_COURSE;
410 $context->instanceid = $course->id;
411 $context->path = 'not initialised';
d0212567 412 $context->depth = '13';
2afe21ee 413 $this->testdb->insert_record('context', $context);
414
415 return $course;
416 }
417
418 public function test_setting_course_sets_context() {
419 // Setup fixture
420 $course = $this->create_a_course_with_context();
421 // Exercise SUT
422 $this->testpage->set_course($course);
423 // Validate
424 $expectedcontext = new stdClass;
425 $expectedcontext->contextlevel = CONTEXT_COURSE;
426 $expectedcontext->instanceid = $course->id;
427 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
428 }
429}
948203a5 430
431/**
432 * Test functions that rely on the context table.
433 */
434class moodle_page_categories_test extends UnitTestCaseUsingDatabase {
435 protected $testpage;
436 protected $originalcourse;
437
438 public function setUp() {
439 global $COURSE, $SITE;
440 parent::setUp();
441 $this->originalcourse = $COURSE;
442 $this->testpage = new moodle_page();
443 $this->create_test_tables(array('course_categories', 'context'), 'lib');
444 $this->switch_to_test_db();
445
446 $context = new stdClass;
447 $context->contextlevel = CONTEXT_COURSE;
448 $context->instanceid = $SITE->id;
449 $context->path = 'not initialised';
d0212567 450 $context->depth = '13';
948203a5 451 $this->testdb->insert_record('context', $context);
452 }
453
454 public function tearDown() {
455 global $COURSE;
456 $this->testpage = NULL;
457 $COURSE = $this->originalcourse;
458 parent::tearDown();
459 }
460
461 /** Creates an object with all the fields you would expect a $course object to have. */
462 protected function create_a_category_with_context($parentid = 0) {
463 if ($parentid) {
464 $parent = $this->testdb->get_record('course_categories', array('id' => $parentid));
465 } else {
466 $parent = new stdClass;
467 $parent->depth = 0;
468 $parent->path = '';
469 }
470 $cat = new stdClass;
471 $cat->name = 'Anonymous test category';
472 $cat->description = '';
473 $cat->parent = $parentid;
474 $cat->depth = $parent->depth + 1;
475 $cat->id = $this->testdb->insert_record('course_categories', $cat);
476 $cat->path = $parent->path . '/' . $cat->id;
477 $this->testdb->set_field('course_categories', 'path', $cat->path, array('id' => $cat->id));
478
479 $context = new stdClass;
480 $context->contextlevel = CONTEXT_COURSECAT;
481 $context->instanceid = $cat->id;
482 $context->path = 'not initialised';
d0212567 483 $context->depth = '13';
948203a5 484 $this->testdb->insert_record('context', $context);
485
486 return $cat;
487 }
488
489 public function test_set_category_top_level() {
490 // Setup fixture
491 $cat = $this->create_a_category_with_context();
492 // Exercise SUT
493 $this->testpage->set_category_by_id($cat->id);
494 // Validate
495 $this->assert(new CheckSpecifiedFieldsExpectation($cat), $this->testpage->category);
496 $expectedcontext = new stdClass; // Test it sets the context.
497 $expectedcontext->contextlevel = CONTEXT_COURSECAT;
498 $expectedcontext->instanceid = $cat->id;
499 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
500 }
501
502 public function test_set_nested_categories() {
503 // Setup fixture
504 $topcat = $this->create_a_category_with_context();
505 $subcat = $this->create_a_category_with_context($topcat->id);
506 // Exercise SUT
507 $this->testpage->set_category_by_id($subcat->id);
508 // Validate
509 $categories = $this->testpage->categories;
510 $this->assertEqual(2, count($categories));
511 $this->assert(new CheckSpecifiedFieldsExpectation($topcat), array_pop($categories));
512 $this->assert(new CheckSpecifiedFieldsExpectation($subcat), array_pop($categories));
513 }
514}
515
5ec434a9 516/**
517 * Test functions that rely on the context table.
518 */
519class moodle_page_cm_test extends UnitTestCaseUsingDatabase {
520 protected $testpage;
521 protected $originalcourse;
522
523 public function setUp() {
524 global $COURSE, $SITE;
525 parent::setUp();
526 $this->originalcourse = $COURSE;
527 $this->testpage = new moodle_page();
528 $this->create_test_tables(array('course', 'context'), 'lib');
529 $this->create_test_table('forum', 'mod/forum');
530 $this->switch_to_test_db();
531
532 $context = new stdClass;
533 $context->contextlevel = CONTEXT_COURSE;
534 $context->instanceid = $SITE->id;
535 $context->path = 'not initialised';
d0212567 536 $context->depth = '13';
5ec434a9 537 $this->testdb->insert_record('context', $context);
538 }
539
540 public function tearDown() {
541 global $COURSE;
542 $this->testpage = NULL;
543 $COURSE = $this->originalcourse;
544 parent::tearDown();
545 }
546
547 /** Creates an object with all the fields you would expect a $course object to have. */
548 protected function create_a_forum_with_context() {
549 $course = new stdClass;
550 $course->category = 2;
551 $course->fullname = 'Anonymous test course';
552 $course->shortname = 'ANON';
553 $course->summary = '';
554 $course->id = $this->testdb->insert_record('course', $course);
555
556 $forum = new stdClass;
557 $forum->course = $course->id;
558 $forum->name = 'Anonymouse test forum';
559 $forum->intro = '';
560 $forum->id = $this->testdb->insert_record('forum', $forum);
561
562 $cm = new stdClass;
d0212567 563 $cm->id = 13;
5ec434a9 564 $cm->course = $course->id;
565 $cm->instance = $forum->id;
566 $cm->modname = 'forum';
567 $cm->name = $forum->name;
568
569 $context = new stdClass;
570 $context->contextlevel = CONTEXT_MODULE;
571 $context->instanceid = $cm->id;
572 $context->path = 'not initialised';
d0212567 573 $context->depth = '13';
5ec434a9 574 $this->testdb->insert_record('context', $context);
575
576 return array($cm, $course, $forum);
577 }
578
579 public function test_cm_null_initially() {
580 // Validate
581 $this->assertNull($this->testpage->cm);
582 }
583
584 public function test_set_cm() {
585 // Setup fixture
586 list($cm) = $this->create_a_forum_with_context();
587 // Exercise SUT
588 $this->testpage->set_cm($cm);
589 // Validate
590 $this->assert(new CheckSpecifiedFieldsExpectation($cm), $this->testpage->cm);
591 }
592
593 public function test_cannot_set_cm_without_name() {
594 // Setup fixture
595 list($cm) = $this->create_a_forum_with_context();
596 // Set expectation
597 $this->expectException();
598 // Exercise SUT
599 unset($cm->name);
600 $this->testpage->set_cm($cm);
601 }
602
603 public function test_cannot_set_cm_without_modname() {
604 // Setup fixture
605 list($cm) = $this->create_a_forum_with_context();
606 // Set expectation
607 $this->expectException();
608 // Exercise SUT
609 unset($cm->modname);
610 $this->testpage->set_cm($cm);
611 }
612
613 public function test_cannot_set_activity_record_before_cm() {
614 // Setup fixture
615 list($cm, $course, $forum) = $this->create_a_forum_with_context();
616 // Set expectation
617 $this->expectException();
618 // Exercise SUT
619 $this->testpage->set_activity_record($forum);
620 }
621
622 public function test_setting_cm_sets_context() {
623 // Setup fixture
624 list($cm) = $this->create_a_forum_with_context();
625 // Exercise SUT
626 $this->testpage->set_cm($cm);
627 // Validate
628 $expectedcontext = new stdClass;
629 $expectedcontext->contextlevel = CONTEXT_MODULE;
630 $expectedcontext->instanceid = $cm->id;
631 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
632 }
633
634 public function test_activity_record_loaded_if_not_set() {
635 // Setup fixture
636 list($cm, $course, $forum) = $this->create_a_forum_with_context();
637 // Exercise SUT
638 $this->testpage->set_cm($cm);
639 // Validate
640 $this->assert(new CheckSpecifiedFieldsExpectation($forum), $this->testpage->activityrecord);
641 }
642
643 public function test_set_activity_record() {
644 // Setup fixture
645 list($cm, $course, $forum) = $this->create_a_forum_with_context();
646 $this->testpage->set_cm($cm);
647 // Exercise SUT
648 $this->testpage->set_activity_record($forum);
649 // Validate
650 $this->assert(new CheckSpecifiedFieldsExpectation($forum), $this->testpage->activityrecord);
651 }
652
653 public function test_cannot_set_inconsistent_activity_record_course() {
654 // Setup fixture
655 list($cm, $course, $forum) = $this->create_a_forum_with_context();
656 $this->testpage->set_cm($cm);
657 // Set expectation
658 $this->expectException();
659 // Exercise SUT
d0212567 660 $forum->course = 13;
5ec434a9 661 $this->testpage->set_activity_record($forum);
662 }
663
664 public function test_cannot_set_inconsistent_activity_record_instance() {
665 // Setup fixture
666 list($cm, $course, $forum) = $this->create_a_forum_with_context();
667 $this->testpage->set_cm($cm);
668 // Set expectation
669 $this->expectException();
670 // Exercise SUT
d0212567 671 $forum->id = 13;
5ec434a9 672 $this->testpage->set_activity_record($forum);
673 }
674
675 public function test_settin_cm_sets_course() {
676 // Setup fixture
677 list($cm, $course) = $this->create_a_forum_with_context();
678 // Exercise SUT
679 $this->testpage->set_cm($cm);
680 // Validate
681 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course);
682 }
683
684 public function test_set_cm_with_course_and_activity_no_db() {
685 // Setup fixture
686 list($cm, $course, $forum) = $this->create_a_forum_with_context();
687 $this->drop_test_table('forum');
688 $this->drop_test_table('course');
689 // Exercise SUT
690 $this->testpage->set_cm($cm, $course, $forum);
691 // Validate
692 $this->assert(new CheckSpecifiedFieldsExpectation($cm), $this->testpage->cm);
693 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course);
694 $this->assert(new CheckSpecifiedFieldsExpectation($forum), $this->testpage->activityrecord);
695 }
696
697 public function test_cannot_set_cm_with_inconsistent_course() {
698 // Setup fixture
699 list($cm, $course, $forum) = $this->create_a_forum_with_context();
700 // Set expectation
701 $this->expectException();
702 // Exercise SUT
d0212567 703 $cm->course = 13;
5ec434a9 704 $this->testpage->set_cm($cm, $course);
705 }
706
707 public function test_get_activity_name() {
708 // Setup fixture
709 list($cm, $course, $forum) = $this->create_a_forum_with_context();
710 // Exercise SUT
711 $this->testpage->set_cm($cm, $course, $forum);
712 // Validate
713 $this->assertEqual('forum', $this->testpage->activityname);
714 }
715}
716
830dd6e9 717/**
718 * Test functions that affect filter_active table with contextid = $syscontextid.
719 */
720class moodle_page_editing_test extends UnitTestCase {
721 protected $testpage;
722 protected $originaluserediting;
723
724 public function setUp() {
725 global $USER;
726 $this->originaluserediting = !empty($USER->editing);
727 $this->testpage = new testable_moodle_page();
934524d7 728 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
830dd6e9 729 }
730
731 public function tearDown() {
732 global $USER;
733 $this->testpage = NULL;
734 $USER->editing = $this->originaluserediting;
735 }
736
934524d7 737 // We are relying on the fact that unit tests are alwyas run by admin, to
738 // ensure the user_allows_editing call returns true.
830dd6e9 739 public function test_user_is_editing_on() {
740 // Setup fixture
741 global $USER;
742 $USER->editing = true;
743 // Validate
744 $this->assertTrue($this->testpage->user_is_editing());
745 }
746
934524d7 747 // We are relying on the fact that unit tests are alwyas run by admin, to
748 // ensure the user_allows_editing call returns true.
830dd6e9 749 public function test_user_is_editing_off() {
750 // Setup fixture
751 global $USER;
752 $USER->editing = false;
753 // Validate
754 $this->assertFalse($this->testpage->user_is_editing());
755 }
934524d7 756
757 public function test_default_editing_capabilities() {
758 // Validate
759 $this->assertEqual(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
760 }
761
762 public function test_other_block_editing_cap() {
763 // Exercise SUT
764 $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
765 // Validate
766 $this->assertEqual(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
767 }
768
769 public function test_other_editing_cap() {
770 // Exercise SUT
771 $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
772 // Validate
773 $actualcaps = $this->testpage->all_editing_caps();
774 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
775 $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps);
776 }
777
778 public function test_other_editing_caps() {
779 // Exercise SUT
780 $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
781 // Validate
782 $actualcaps = $this->testpage->all_editing_caps();
783 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
784 $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps);
785 }
830dd6e9 786}
8926f844 787