moodle_page: MDL-12212 ->category and ->categories
[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
2afe21ee 147 public function test_set_state_normal_path() {
148 $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
149
150 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
151 $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
152
153 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
154 $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state);
155
156 $this->testpage->set_state(moodle_page::STATE_PRINTING_FOOTER);
157 $this->assertEqual(moodle_page::STATE_PRINTING_FOOTER, $this->testpage->state);
158
159 $this->testpage->set_state(moodle_page::STATE_DONE);
160 $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state);
161 }
162
163 public function test_set_state_cannot_skip_one() {
164 // Set expectation.
165 $this->expectException();
166 // Exercise SUT
167 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
168 }
169
170 public function test_header_printed_false_initially() {
171 // Validate
172 $this->assertFalse($this->testpage->headerprinted);
173 }
174
175 public function test_header_printed_becomes_true() {
176 // Exercise SUT
177 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
178 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
179 // Validate
180 $this->assertTrue($this->testpage->headerprinted);
181 }
182
183 public function test_cant_get_context_before_set() {
184 // Set expectation.
185 $this->expectException();
186 // Exercise SUT
187 $this->testpage->context;
188 }
189
190 public function test_set_context() {
191 // Setup fixture
192 $context = $this->create_a_context();
193 // Exercise SUT
194 $this->testpage->set_context($context);
195 // Validate
196 $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context);
197 }
f230ce19 198
e88462a0 199 public function test_pagetype_defaults_to_script() {
200 // Exercise SUT and validate
201 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
f230ce19 202 }
203
204 public function test_set_pagetype() {
205 // Exercise SUT
206 $this->testpage->set_pagetype('a-page-type');
207 // Validate
208 $this->assertEqual('a-page-type', $this->testpage->pagetype);
209 }
e88462a0 210
211 public function test_initialise_default_pagetype() {
212 // Exercise SUT
213 $this->testpage->initialise_default_pagetype('admin/report/unittest/index.php');
214 // Validate
215 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
216 }
217
218 public function test_initialise_default_pagetype_fp() {
219 // Exercise SUT
220 $this->testpage->initialise_default_pagetype('index.php');
221 // Validate
222 $this->assertEqual('site-index', $this->testpage->pagetype);
223 }
753debd2 224
225 public function test_get_body_classes_empty() {
226 // Validate
227 $this->assertEqual('', $this->testpage->bodyclasses);
228 }
229
230 public function test_get_body_classes_single() {
231 // Exercise SUT
232 $this->testpage->add_body_class('aclassname');
233 // Validate
234 $this->assertEqual('aclassname', $this->testpage->bodyclasses);
235 }
236
89fbdca3 237 public function test_get_body_classes() {
753debd2 238 // Exercise SUT
89fbdca3 239 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
753debd2 240 // Validate
241 $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses);
242 }
89fbdca3 243
244 public function test_url_to_class_name() {
245 $this->assertEqual('example-com', $this->testpage->url_to_class_name('http://example.com'));
246 $this->assertEqual('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
247 $this->assertEqual('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
248 $this->assertEqual('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
249 }
2afe21ee 250}
251
252/**
948203a5 253 * Test functions that rely on the context table.
2afe21ee 254 */
948203a5 255class moodle_page_with_context_table_test extends UnitTestCaseUsingDatabase {
2afe21ee 256 protected $testpage;
257 protected $originalcourse;
258
259 public function setUp() {
260 global $COURSE;
948203a5 261 parent::setUp();
2afe21ee 262 $this->originalcourse = $COURSE;
263 $this->testpage = new moodle_page();
264 $this->create_test_table('context', 'lib');
265 $this->switch_to_test_db();
266 }
267
268 public function tearDown() {
269 global $COURSE;
270 $this->testpage = NULL;
271 $COURSE = $this->originalcourse;
948203a5 272 parent::tearDown();
2afe21ee 273 }
274
275 /** Creates an object with all the fields you would expect a $course object to have. */
276 protected function create_a_course_with_context() {
277 $course = new stdClass;
278 $course->id = -1;
279 $course->category = 2;
280 $course->fullname = 'Anonymous test course';
281 $course->shortname = 'ANON';
282 $course->summary = '';
283
284 $context = new stdClass;
285 $context->contextlevel = CONTEXT_COURSE;
286 $context->instanceid = $course->id;
287 $context->path = 'not initialised';
288 $context->depth = '-1';
289 $this->testdb->insert_record('context', $context);
290
291 return $course;
292 }
293
294 public function test_setting_course_sets_context() {
295 // Setup fixture
296 $course = $this->create_a_course_with_context();
297 // Exercise SUT
298 $this->testpage->set_course($course);
299 // Validate
300 $expectedcontext = new stdClass;
301 $expectedcontext->contextlevel = CONTEXT_COURSE;
302 $expectedcontext->instanceid = $course->id;
303 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
304 }
305}
948203a5 306
307/**
308 * Test functions that rely on the context table.
309 */
310class moodle_page_categories_test extends UnitTestCaseUsingDatabase {
311 protected $testpage;
312 protected $originalcourse;
313
314 public function setUp() {
315 global $COURSE, $SITE;
316 parent::setUp();
317 $this->originalcourse = $COURSE;
318 $this->testpage = new moodle_page();
319 $this->create_test_tables(array('course_categories', 'context'), 'lib');
320 $this->switch_to_test_db();
321
322 $context = new stdClass;
323 $context->contextlevel = CONTEXT_COURSE;
324 $context->instanceid = $SITE->id;
325 $context->path = 'not initialised';
326 $context->depth = '-1';
327 $this->testdb->insert_record('context', $context);
328 }
329
330 public function tearDown() {
331 global $COURSE;
332 $this->testpage = NULL;
333 $COURSE = $this->originalcourse;
334 parent::tearDown();
335 }
336
337 /** Creates an object with all the fields you would expect a $course object to have. */
338 protected function create_a_category_with_context($parentid = 0) {
339 if ($parentid) {
340 $parent = $this->testdb->get_record('course_categories', array('id' => $parentid));
341 } else {
342 $parent = new stdClass;
343 $parent->depth = 0;
344 $parent->path = '';
345 }
346 $cat = new stdClass;
347 $cat->name = 'Anonymous test category';
348 $cat->description = '';
349 $cat->parent = $parentid;
350 $cat->depth = $parent->depth + 1;
351 $cat->id = $this->testdb->insert_record('course_categories', $cat);
352 $cat->path = $parent->path . '/' . $cat->id;
353 $this->testdb->set_field('course_categories', 'path', $cat->path, array('id' => $cat->id));
354
355 $context = new stdClass;
356 $context->contextlevel = CONTEXT_COURSECAT;
357 $context->instanceid = $cat->id;
358 $context->path = 'not initialised';
359 $context->depth = '-1';
360 $this->testdb->insert_record('context', $context);
361
362 return $cat;
363 }
364
365 public function test_set_category_top_level() {
366 // Setup fixture
367 $cat = $this->create_a_category_with_context();
368 // Exercise SUT
369 $this->testpage->set_category_by_id($cat->id);
370 // Validate
371 $this->assert(new CheckSpecifiedFieldsExpectation($cat), $this->testpage->category);
372 $expectedcontext = new stdClass; // Test it sets the context.
373 $expectedcontext->contextlevel = CONTEXT_COURSECAT;
374 $expectedcontext->instanceid = $cat->id;
375 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
376 }
377
378 public function test_set_nested_categories() {
379 // Setup fixture
380 $topcat = $this->create_a_category_with_context();
381 $subcat = $this->create_a_category_with_context($topcat->id);
382 // Exercise SUT
383 $this->testpage->set_category_by_id($subcat->id);
384 // Validate
385 $categories = $this->testpage->categories;
386 $this->assertEqual(2, count($categories));
387 $this->assert(new CheckSpecifiedFieldsExpectation($topcat), array_pop($categories));
388 $this->assert(new CheckSpecifiedFieldsExpectation($subcat), array_pop($categories));
389 }
390}
391
2afe21ee 392?>