moodle_page: MDL-12212 eliminate page::edit_always
[moodle.git] / lib / simpletest / testpagelib_moodlepage.php
CommitLineData
2afe21ee 1<?php // $Id$
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/**
27 * Tests for the parts of ../filterlib.php that handle creating filter objects,
28 * and using them to filter strings.
29 *
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package moodlecore
32 */
33
34if (!defined('MOODLE_INTERNAL')) {
35 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
36}
37
38require_once($CFG->libdir . '/pagelib.php');
39
e88462a0 40class testable_moodle_page extends moodle_page {
ad52c04f 41 public function initialise_default_pagetype($script = null) {
e88462a0 42 parent::initialise_default_pagetype($script);
43 }
89fbdca3 44 public function url_to_class_name($url) {
45 return parent::url_to_class_name($url);
46 }
e88462a0 47}
48
2afe21ee 49/**
50 * Test functions that affect filter_active table with contextid = $syscontextid.
51 */
52class moodle_page_test extends UnitTestCase {
53 protected $testpage;
54 protected $originalcourse;
55
56 public function setUp() {
57 global $COURSE;
58 $this->originalcourse = $COURSE;
e88462a0 59 $this->testpage = new testable_moodle_page();
2afe21ee 60 }
61
62 public function tearDown() {
63 global $COURSE;
64 $this->testpage = NULL;
65 $COURSE = $this->originalcourse;
66 }
67
68 /** Creates an object with all the fields you would expect a $course object to have. */
69 protected function create_a_course() {
70 $course = new stdClass;
71 $course->id = -1;
72 $course->category = 2;
73 $course->fullname = 'Anonymous test course';
74 $course->shortname = 'ANON';
75 $course->summary = '';
76 return $course;
77 }
78
79 /** Creates an object with all the fields you would expect a $course object to have. */
80 protected function create_a_context() {
81 $context = new stdClass;
82 $context->id = 2;
83 $context->contextlevel = CONTEXT_COURSECAT;
84 $context->instanceid = 1;
85 $context->path = '/1/2';
86 $context->depth = '2';
87 return $context;
88 }
89
90 public function test_course_returns_site_before_set() {
91 global $SITE;
92 // Validate
93 $this->assertIdentical($SITE, $this->testpage->course);
94 }
95
96 public function test_setting_course_works() {
97 // Setup fixture
98 $course = $this->create_a_course();
99 $this->testpage->set_context(new stdClass); // Avoid trying to set the context.
100 // Exercise SUT
101 $this->testpage->set_course($course);
102 // Validate
103 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course);
104 }
105
106 public function test_global_course_and_page_course_are_same() {
107 global $COURSE;
108 // Setup fixture
109 $course = $this->create_a_course();
110 $this->testpage->set_context(new stdClass); // Avoid trying to set the context.
111 // Exercise SUT
112 $this->testpage->set_course($course);
113 // Validate
114 $this->assertIdentical($this->testpage->course, $COURSE);
115 }
116
117 public function test_cannot_set_course_once_output_started() {
118 // Setup fixture
119 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
120 $course = $this->create_a_course();
121 // Set expectation.
122 $this->expectException();
123 // Exercise SUT
124 $this->testpage->set_course($course);
125 }
126
948203a5 127 public function test_cannot_set_category_once_output_started() {
128 // Setup fixture
129 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
130 // Set expectation.
131 $this->expectException();
132 // Exercise SUT
133 $this->testpage->set_category_by_id(123);
134 }
135
136 public function test_cannot_set_category_once_course_set() {
137 // Setup fixture
138 $course = $this->create_a_course();
139 $this->testpage->set_context(new stdClass); // Avoid trying to set the context.
140 $this->testpage->set_course($course);
141 // Set expectation.
142 $this->expectException();
143 // Exercise SUT
144 $this->testpage->set_category_by_id(123);
145 }
146
d7ab8879 147 public function test_categories_array_empty_for_front_page() {
148 // Setup fixture
149 $course = $this->create_a_course();
150 $course->category = 0;
151 $this->testpage->set_context(new stdClass); // Avoid trying to set the context.
152 $this->testpage->set_course($course);
153 // Exercise SUT and validate.
154 $this->assertEqual(array(), $this->testpage->categories);
155 }
156
2afe21ee 157 public function test_set_state_normal_path() {
158 $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
159
160 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
161 $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
162
163 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
164 $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state);
165
166 $this->testpage->set_state(moodle_page::STATE_PRINTING_FOOTER);
167 $this->assertEqual(moodle_page::STATE_PRINTING_FOOTER, $this->testpage->state);
168
169 $this->testpage->set_state(moodle_page::STATE_DONE);
170 $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state);
171 }
172
173 public function test_set_state_cannot_skip_one() {
174 // Set expectation.
175 $this->expectException();
176 // Exercise SUT
177 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
178 }
179
180 public function test_header_printed_false_initially() {
181 // Validate
182 $this->assertFalse($this->testpage->headerprinted);
183 }
184
185 public function test_header_printed_becomes_true() {
186 // Exercise SUT
187 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
188 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
189 // Validate
190 $this->assertTrue($this->testpage->headerprinted);
191 }
192
193 public function test_cant_get_context_before_set() {
194 // Set expectation.
195 $this->expectException();
196 // Exercise SUT
197 $this->testpage->context;
198 }
199
200 public function test_set_context() {
201 // Setup fixture
202 $context = $this->create_a_context();
203 // Exercise SUT
204 $this->testpage->set_context($context);
205 // Validate
206 $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context);
207 }
f230ce19 208
e88462a0 209 public function test_pagetype_defaults_to_script() {
210 // Exercise SUT and validate
211 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
f230ce19 212 }
213
214 public function test_set_pagetype() {
215 // Exercise SUT
216 $this->testpage->set_pagetype('a-page-type');
217 // Validate
218 $this->assertEqual('a-page-type', $this->testpage->pagetype);
219 }
e88462a0 220
221 public function test_initialise_default_pagetype() {
222 // Exercise SUT
223 $this->testpage->initialise_default_pagetype('admin/report/unittest/index.php');
224 // Validate
225 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
226 }
227
228 public function test_initialise_default_pagetype_fp() {
229 // Exercise SUT
230 $this->testpage->initialise_default_pagetype('index.php');
231 // Validate
232 $this->assertEqual('site-index', $this->testpage->pagetype);
233 }
753debd2 234
235 public function test_get_body_classes_empty() {
236 // Validate
237 $this->assertEqual('', $this->testpage->bodyclasses);
238 }
239
240 public function test_get_body_classes_single() {
241 // Exercise SUT
242 $this->testpage->add_body_class('aclassname');
243 // Validate
244 $this->assertEqual('aclassname', $this->testpage->bodyclasses);
245 }
246
89fbdca3 247 public function test_get_body_classes() {
753debd2 248 // Exercise SUT
89fbdca3 249 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
753debd2 250 // Validate
251 $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses);
252 }
89fbdca3 253
254 public function test_url_to_class_name() {
255 $this->assertEqual('example-com', $this->testpage->url_to_class_name('http://example.com'));
256 $this->assertEqual('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
257 $this->assertEqual('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
258 $this->assertEqual('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
259 }
82611d8d 260
261 public function test_set_docs_path() {
262 // Exercise SUT
263 $this->testpage->set_docs_path('a/file/path');
264 // Validate
265 $this->assertEqual('a/file/path', $this->testpage->docspath);
266 }
267
268 public function test_docs_path_defaults_from_pagetype() {
269 // Exercise SUT
270 $this->testpage->set_pagetype('a-page-type');
271 // Validate
272 $this->assertEqual('a/page/type', $this->testpage->docspath);
273 }
75781f87 274
275 public function test_set_url_root() {
276 global $CFG;
277 // Exercise SUT
278 $this->testpage->set_url('');
279 // Validate
280 $this->assertEqual($CFG->wwwroot . '/', $this->testpage->url->out());
281 }
282
283 public function test_set_url_one_param() {
284 global $CFG;
285 // Exercise SUT
286 $this->testpage->set_url('mod/quiz/attempt.php', array('attempt' => 123));
287 // Validate
288 $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
289 }
290
291 public function test_set_url_two_params() {
292 global $CFG;
293 // Exercise SUT
294 $this->testpage->set_url('mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
295 // Validate
296 $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
297 }
298
299 public function test_set_url_sets_page_type() {
300 // Exercise SUT
301 $this->testpage->set_url('mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
302 // Validate
303 $this->assertEqual('mod-quiz-attempt', $this->testpage->pagetype);
304 }
305
306 public function test_set_url_does_not_change_explicit_page_type() {
307 // Setup fixture
308 $this->testpage->set_pagetype('a-page-type');
309 // Exercise SUT
310 $this->testpage->set_url('mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
311 // Validate
312 $this->assertEqual('a-page-type', $this->testpage->pagetype);
313 }
2afe21ee 314}
315
316/**
948203a5 317 * Test functions that rely on the context table.
2afe21ee 318 */
948203a5 319class moodle_page_with_context_table_test extends UnitTestCaseUsingDatabase {
2afe21ee 320 protected $testpage;
321 protected $originalcourse;
322
323 public function setUp() {
324 global $COURSE;
948203a5 325 parent::setUp();
2afe21ee 326 $this->originalcourse = $COURSE;
327 $this->testpage = new moodle_page();
328 $this->create_test_table('context', 'lib');
329 $this->switch_to_test_db();
330 }
331
332 public function tearDown() {
333 global $COURSE;
334 $this->testpage = NULL;
335 $COURSE = $this->originalcourse;
948203a5 336 parent::tearDown();
2afe21ee 337 }
338
339 /** Creates an object with all the fields you would expect a $course object to have. */
340 protected function create_a_course_with_context() {
341 $course = new stdClass;
342 $course->id = -1;
343 $course->category = 2;
344 $course->fullname = 'Anonymous test course';
345 $course->shortname = 'ANON';
346 $course->summary = '';
347
348 $context = new stdClass;
349 $context->contextlevel = CONTEXT_COURSE;
350 $context->instanceid = $course->id;
351 $context->path = 'not initialised';
352 $context->depth = '-1';
353 $this->testdb->insert_record('context', $context);
354
355 return $course;
356 }
357
358 public function test_setting_course_sets_context() {
359 // Setup fixture
360 $course = $this->create_a_course_with_context();
361 // Exercise SUT
362 $this->testpage->set_course($course);
363 // Validate
364 $expectedcontext = new stdClass;
365 $expectedcontext->contextlevel = CONTEXT_COURSE;
366 $expectedcontext->instanceid = $course->id;
367 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
368 }
369}
948203a5 370
371/**
372 * Test functions that rely on the context table.
373 */
374class moodle_page_categories_test extends UnitTestCaseUsingDatabase {
375 protected $testpage;
376 protected $originalcourse;
377
378 public function setUp() {
379 global $COURSE, $SITE;
380 parent::setUp();
381 $this->originalcourse = $COURSE;
382 $this->testpage = new moodle_page();
383 $this->create_test_tables(array('course_categories', 'context'), 'lib');
384 $this->switch_to_test_db();
385
386 $context = new stdClass;
387 $context->contextlevel = CONTEXT_COURSE;
388 $context->instanceid = $SITE->id;
389 $context->path = 'not initialised';
390 $context->depth = '-1';
391 $this->testdb->insert_record('context', $context);
392 }
393
394 public function tearDown() {
395 global $COURSE;
396 $this->testpage = NULL;
397 $COURSE = $this->originalcourse;
398 parent::tearDown();
399 }
400
401 /** Creates an object with all the fields you would expect a $course object to have. */
402 protected function create_a_category_with_context($parentid = 0) {
403 if ($parentid) {
404 $parent = $this->testdb->get_record('course_categories', array('id' => $parentid));
405 } else {
406 $parent = new stdClass;
407 $parent->depth = 0;
408 $parent->path = '';
409 }
410 $cat = new stdClass;
411 $cat->name = 'Anonymous test category';
412 $cat->description = '';
413 $cat->parent = $parentid;
414 $cat->depth = $parent->depth + 1;
415 $cat->id = $this->testdb->insert_record('course_categories', $cat);
416 $cat->path = $parent->path . '/' . $cat->id;
417 $this->testdb->set_field('course_categories', 'path', $cat->path, array('id' => $cat->id));
418
419 $context = new stdClass;
420 $context->contextlevel = CONTEXT_COURSECAT;
421 $context->instanceid = $cat->id;
422 $context->path = 'not initialised';
423 $context->depth = '-1';
424 $this->testdb->insert_record('context', $context);
425
426 return $cat;
427 }
428
429 public function test_set_category_top_level() {
430 // Setup fixture
431 $cat = $this->create_a_category_with_context();
432 // Exercise SUT
433 $this->testpage->set_category_by_id($cat->id);
434 // Validate
435 $this->assert(new CheckSpecifiedFieldsExpectation($cat), $this->testpage->category);
436 $expectedcontext = new stdClass; // Test it sets the context.
437 $expectedcontext->contextlevel = CONTEXT_COURSECAT;
438 $expectedcontext->instanceid = $cat->id;
439 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
440 }
441
442 public function test_set_nested_categories() {
443 // Setup fixture
444 $topcat = $this->create_a_category_with_context();
445 $subcat = $this->create_a_category_with_context($topcat->id);
446 // Exercise SUT
447 $this->testpage->set_category_by_id($subcat->id);
448 // Validate
449 $categories = $this->testpage->categories;
450 $this->assertEqual(2, count($categories));
451 $this->assert(new CheckSpecifiedFieldsExpectation($topcat), array_pop($categories));
452 $this->assert(new CheckSpecifiedFieldsExpectation($subcat), array_pop($categories));
453 }
454}
455
2afe21ee 456?>