MDL-23979 fixed old style url init
[moodle.git] / lib / simpletest / testpagelib_moodlepage.php
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Tests for the moodle_page class in ../pagelib.php.
28  *
29  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30  * @package moodlecore
31  */
33 if (!defined('MOODLE_INTERNAL')) {
34     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
35 }
37 require_once($CFG->libdir . '/pagelib.php');
38 require_once($CFG->libdir . '/blocklib.php');
40 /** Test-specific subclass to make some protected things public. */
41 class testable_moodle_page extends moodle_page {
42     public function initialise_default_pagetype($script = null) {
43         parent::initialise_default_pagetype($script);
44     }
45     public function url_to_class_name($url) {
46         return parent::url_to_class_name($url);
47     }
48     public function all_editing_caps() {
49         return parent::all_editing_caps();
50     }
51 }
53 /**
54  * Test functions that don't need to touch the database.
55  */
56 class moodle_page_test extends UnitTestCase {
57     protected $testpage;
58     protected $originalcourse;
59     protected $originalpage;
60     public static $includecoverage = array('lib/pagelib.php', 'lib/blocklib.php');
62     public function setUp() {
63         global $COURSE, $PAGE;
64         $this->originalcourse = $COURSE;
65         $this->originalpage = $PAGE;
66         $this->testpage = new testable_moodle_page();
67     }
69     public function tearDown() {
70         global $COURSE;
71         $this->testpage = NULL;
72         $COURSE = $this->originalcourse;
73         $PAGE = $this->originalpage;
74     }
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;
79         $course->id = 13; // Why 13, good question. MDL-21007
80         $course->category = 2;
81         $course->fullname = 'Anonymous test course';
82         $course->shortname = 'ANON';
83         $course->summary = '';
84         return $course;
85     }
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     }
98     public function test_course_returns_site_before_set() {
99         global $SITE;
100         // Validate
101         $this->assertIdentical($SITE, $this->testpage->course);
102     }
104     public function test_setting_course_works() {
105         // Setup fixture
106         $course = $this->create_a_course();
107         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
108         // Exercise SUT
109         $this->testpage->set_course($course);
110         // Validate
111         $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course);
112     }
114     public function test_global_course_and_page_course_are_same_with_global_page() {
115         global $COURSE, $PAGE;
116         // Setup fixture
117         $course = $this->create_a_course();
118         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
119         $PAGE = $this->testpage;
120         // Exercise SUT
121         $this->testpage->set_course($course);
122         // Validate
123         $this->assertIdentical($this->testpage->course, $COURSE);
124     }
126     public function test_global_course_not_changed_with_non_global_page() {
127         global $COURSE;
128         $originalcourse = $COURSE;
129         // Setup fixture
130         $course = $this->create_a_course();
131         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
132         // Exercise SUT
133         $this->testpage->set_course($course);
134         // Validate
135         $this->assertIdentical($originalcourse, $COURSE);
136     }
138     public function test_cannot_set_course_once_theme_set() {
139         // Setup fixture
140         $this->testpage->force_theme(theme_config::DEFAULT_THEME);
141         $course = $this->create_a_course();
142         // Set expectation.
143         $this->expectException();
144         // Exercise SUT
145         $this->testpage->set_course($course);
146     }
148     public function test_cannot_set_category_once_theme_set() {
149         // Setup fixture
150         $this->testpage->force_theme(theme_config::DEFAULT_THEME);
151         // Set expectation.
152         $this->expectException();
153         // Exercise SUT
154         $this->testpage->set_category_by_id(123);
155     }
157     public function test_cannot_set_category_once_course_set() {
158         // Setup fixture
159         $course = $this->create_a_course();
160         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
161         $this->testpage->set_course($course);
162         // Set expectation.
163         $this->expectException();
164         // Exercise SUT
165         $this->testpage->set_category_by_id(123);
166     }
168     public function test_categories_array_empty_for_front_page() {
169         // Setup fixture
170         $course = $this->create_a_course();
171         $course->category = 0;
172         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context.
173         $this->testpage->set_course($course);
174         // Exercise SUT and validate.
175         $this->assertEqual(array(), $this->testpage->categories);
176     }
178     public function test_set_state_normal_path() {
179         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
180         $this->testpage->set_course($this->create_a_course());
182         $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
184         $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
185         $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
187         $this->testpage->set_state(moodle_page::STATE_IN_BODY);
188         $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state);
190         $this->testpage->set_state(moodle_page::STATE_DONE);
191         $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state);
192     }
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     }
201     public function test_header_printed_false_initially() {
202         // Validate
203         $this->assertFalse($this->testpage->headerprinted);
204     }
206     public function test_header_printed_becomes_true() {
207         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
208         $this->testpage->set_course($this->create_a_course());
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     }
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     }
226     public function test_pagetype_defaults_to_script() {
227         // Exercise SUT and validate
228         $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
229     }
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     }
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     }
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     }
252     public function test_get_body_classes_empty() {
253         // Validate
254         $this->assertEqual('', $this->testpage->bodyclasses);
255     }
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     }
264     public function test_get_body_classes() {
265         // Exercise SUT
266         $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
267         // Validate
268         $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses);
269     }
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     }
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     }
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     }
292     public function test_set_url_root() {
293         global $CFG;
294         // Exercise SUT
295         $this->testpage->set_url('/');
296         // Validate
297         $this->assertEqual($CFG->wwwroot . '/', $this->testpage->url->out());
298     }
300     public function test_set_url_one_param() {
301         global $CFG;
302         // Exercise SUT
303         $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
304         // Validate
305         $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
306     }
308     public function test_set_url_two_params() {
309         global $CFG;
310         // Exercise SUT
311         $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
312         // Validate
313         $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
314     }
316     public function test_set_url_using_moodle_url() {
317         global $CFG;
318         // Fixture setup
319         $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
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     }
326     public function test_set_url_sets_page_type() {
327         // Exercise SUT
328         $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
329         // Validate
330         $this->assertEqual('mod-quiz-attempt', $this->testpage->pagetype);
331     }
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
337         $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
338         // Validate
339         $this->assertEqual('a-page-type', $this->testpage->pagetype);
340     }
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     }
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     }
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     }
363     public function test_default_pagelayout() {
364         // Exercise SUT and Validate
365         $this->assertEqual('base', $this->testpage->pagelayout);
366     }
368     public function test_set_pagelayout() {
369         // Exercise SUT
370         $this->testpage->set_pagelayout('type');
371         // Validate
372         $this->assertEqual('type', $this->testpage->pagelayout);
373     }
376 /**
377  * Test functions that rely on the context table.
378  */
379 class moodle_page_with_context_table_test extends UnitTestCaseUsingDatabase {
380     protected $testpage;
381     protected $originalcourse;
383     public function setUp() {
384         global $COURSE;
385         parent::setUp();
386         $this->originalcourse = $COURSE;
387         $this->testpage = new moodle_page();
388         $this->create_test_table('context', 'lib');
389         $this->switch_to_test_db();
390     }
392     public function tearDown() {
393         global $COURSE;
394         $this->testpage = NULL;
395         $COURSE = $this->originalcourse;
396         parent::tearDown();
397     }
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;
402         $course->id = 13;
403         $course->category = 2;
404         $course->fullname = 'Anonymous test course';
405         $course->shortname = 'ANON';
406         $course->summary = '';
408         $context = new stdClass;
409         $context->contextlevel = CONTEXT_COURSE;
410         $context->instanceid = $course->id;
411         $context->path = 'not initialised';
412         $context->depth = '13';
413         $this->testdb->insert_record('context', $context);
415         return $course;
416     }
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     }
431 /**
432  * Test functions that rely on the context table.
433  */
434 class moodle_page_categories_test extends UnitTestCaseUsingDatabase {
435     protected $testpage;
436     protected $originalcourse;
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();
446         $context = new stdClass;
447         $context->contextlevel = CONTEXT_COURSE;
448         $context->instanceid = $SITE->id;
449         $context->path = 'not initialised';
450         $context->depth = '13';
451         $this->testdb->insert_record('context', $context);
452     }
454     public function tearDown() {
455         global $COURSE;
456         $this->testpage = NULL;
457         $COURSE = $this->originalcourse;
458         parent::tearDown();
459     }
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));
479         $context = new stdClass;
480         $context->contextlevel = CONTEXT_COURSECAT;
481         $context->instanceid = $cat->id;
482         $context->path = 'not initialised';
483         $context->depth = '13';
484         $this->testdb->insert_record('context', $context);
486         return $cat;
487     }
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     }
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     }
516 /**
517  * Test functions that rely on the context table.
518  */
519 class moodle_page_cm_test extends UnitTestCaseUsingDatabase {
520     protected $testpage;
521     protected $originalcourse;
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();
532         $context = new stdClass;
533         $context->contextlevel = CONTEXT_COURSE;
534         $context->instanceid = $SITE->id;
535         $context->path = 'not initialised';
536         $context->depth = '13';
537         $this->testdb->insert_record('context', $context);
538     }
540     public function tearDown() {
541         global $COURSE;
542         $this->testpage = NULL;
543         $COURSE = $this->originalcourse;
544         parent::tearDown();
545     }
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);
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);
562         $cm = new stdClass;
563         $cm->id = 13;
564         $cm->course = $course->id;
565         $cm->instance = $forum->id;
566         $cm->modname = 'forum';
567         $cm->name = $forum->name;
569         $context = new stdClass;
570         $context->contextlevel = CONTEXT_MODULE;
571         $context->instanceid = $cm->id;
572         $context->path = 'not initialised';
573         $context->depth = '13';
574         $this->testdb->insert_record('context', $context);
576         return array($cm, $course, $forum);
577     }
579     public function test_cm_null_initially() {
580         // Validate
581         $this->assertNull($this->testpage->cm);
582     }
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     }
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     }
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     }
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     }
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     }
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     }
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     }
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
660         $forum->course = 13;
661         $this->testpage->set_activity_record($forum);
662     }
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
671         $forum->id = 13;
672         $this->testpage->set_activity_record($forum);
673     }
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     }
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     }
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
703         $cm->course = 13;
704         $this->testpage->set_cm($cm, $course);
705     }
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     }
717 /**
718  * Test functions that affect filter_active table with contextid = $syscontextid.
719  */
720 class moodle_page_editing_test extends UnitTestCase {
721     protected $testpage;
722     protected $originaluserediting;
724     public function setUp() {
725         global $USER;
726         $this->originaluserediting = !empty($USER->editing);
727         $this->testpage = new testable_moodle_page();
728         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
729     }
731     public function tearDown() {
732         global $USER;
733         $this->testpage = NULL;
734         $USER->editing = $this->originaluserediting;
735     }
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.
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     }
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.
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     }
757     public function test_default_editing_capabilities() {
758         // Validate
759         $this->assertEqual(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
760     }
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     }
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     }
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     }