moodle_url: MDL-12212 clean up moodle_url coding style
[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 {
41 public function initialise_default_pagetype($script = '') {
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 }
2afe21ee 274}
275
276/**
948203a5 277 * Test functions that rely on the context table.
2afe21ee 278 */
948203a5 279class moodle_page_with_context_table_test extends UnitTestCaseUsingDatabase {
2afe21ee 280 protected $testpage;
281 protected $originalcourse;
282
283 public function setUp() {
284 global $COURSE;
948203a5 285 parent::setUp();
2afe21ee 286 $this->originalcourse = $COURSE;
287 $this->testpage = new moodle_page();
288 $this->create_test_table('context', 'lib');
289 $this->switch_to_test_db();
290 }
291
292 public function tearDown() {
293 global $COURSE;
294 $this->testpage = NULL;
295 $COURSE = $this->originalcourse;
948203a5 296 parent::tearDown();
2afe21ee 297 }
298
299 /** Creates an object with all the fields you would expect a $course object to have. */
300 protected function create_a_course_with_context() {
301 $course = new stdClass;
302 $course->id = -1;
303 $course->category = 2;
304 $course->fullname = 'Anonymous test course';
305 $course->shortname = 'ANON';
306 $course->summary = '';
307
308 $context = new stdClass;
309 $context->contextlevel = CONTEXT_COURSE;
310 $context->instanceid = $course->id;
311 $context->path = 'not initialised';
312 $context->depth = '-1';
313 $this->testdb->insert_record('context', $context);
314
315 return $course;
316 }
317
318 public function test_setting_course_sets_context() {
319 // Setup fixture
320 $course = $this->create_a_course_with_context();
321 // Exercise SUT
322 $this->testpage->set_course($course);
323 // Validate
324 $expectedcontext = new stdClass;
325 $expectedcontext->contextlevel = CONTEXT_COURSE;
326 $expectedcontext->instanceid = $course->id;
327 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
328 }
329}
948203a5 330
331/**
332 * Test functions that rely on the context table.
333 */
334class moodle_page_categories_test extends UnitTestCaseUsingDatabase {
335 protected $testpage;
336 protected $originalcourse;
337
338 public function setUp() {
339 global $COURSE, $SITE;
340 parent::setUp();
341 $this->originalcourse = $COURSE;
342 $this->testpage = new moodle_page();
343 $this->create_test_tables(array('course_categories', 'context'), 'lib');
344 $this->switch_to_test_db();
345
346 $context = new stdClass;
347 $context->contextlevel = CONTEXT_COURSE;
348 $context->instanceid = $SITE->id;
349 $context->path = 'not initialised';
350 $context->depth = '-1';
351 $this->testdb->insert_record('context', $context);
352 }
353
354 public function tearDown() {
355 global $COURSE;
356 $this->testpage = NULL;
357 $COURSE = $this->originalcourse;
358 parent::tearDown();
359 }
360
361 /** Creates an object with all the fields you would expect a $course object to have. */
362 protected function create_a_category_with_context($parentid = 0) {
363 if ($parentid) {
364 $parent = $this->testdb->get_record('course_categories', array('id' => $parentid));
365 } else {
366 $parent = new stdClass;
367 $parent->depth = 0;
368 $parent->path = '';
369 }
370 $cat = new stdClass;
371 $cat->name = 'Anonymous test category';
372 $cat->description = '';
373 $cat->parent = $parentid;
374 $cat->depth = $parent->depth + 1;
375 $cat->id = $this->testdb->insert_record('course_categories', $cat);
376 $cat->path = $parent->path . '/' . $cat->id;
377 $this->testdb->set_field('course_categories', 'path', $cat->path, array('id' => $cat->id));
378
379 $context = new stdClass;
380 $context->contextlevel = CONTEXT_COURSECAT;
381 $context->instanceid = $cat->id;
382 $context->path = 'not initialised';
383 $context->depth = '-1';
384 $this->testdb->insert_record('context', $context);
385
386 return $cat;
387 }
388
389 public function test_set_category_top_level() {
390 // Setup fixture
391 $cat = $this->create_a_category_with_context();
392 // Exercise SUT
393 $this->testpage->set_category_by_id($cat->id);
394 // Validate
395 $this->assert(new CheckSpecifiedFieldsExpectation($cat), $this->testpage->category);
396 $expectedcontext = new stdClass; // Test it sets the context.
397 $expectedcontext->contextlevel = CONTEXT_COURSECAT;
398 $expectedcontext->instanceid = $cat->id;
399 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
400 }
401
402 public function test_set_nested_categories() {
403 // Setup fixture
404 $topcat = $this->create_a_category_with_context();
405 $subcat = $this->create_a_category_with_context($topcat->id);
406 // Exercise SUT
407 $this->testpage->set_category_by_id($subcat->id);
408 // Validate
409 $categories = $this->testpage->categories;
410 $this->assertEqual(2, count($categories));
411 $this->assert(new CheckSpecifiedFieldsExpectation($topcat), array_pop($categories));
412 $this->assert(new CheckSpecifiedFieldsExpectation($subcat), array_pop($categories));
413 }
414}
415
2afe21ee 416?>