moodle_page: MDL-14305 (and MDL-12212) class on body based on ->wwwroot
[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
127 public function test_set_state_normal_path() {
128 $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
129
130 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
131 $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
132
133 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
134 $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state);
135
136 $this->testpage->set_state(moodle_page::STATE_PRINTING_FOOTER);
137 $this->assertEqual(moodle_page::STATE_PRINTING_FOOTER, $this->testpage->state);
138
139 $this->testpage->set_state(moodle_page::STATE_DONE);
140 $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state);
141 }
142
143 public function test_set_state_cannot_skip_one() {
144 // Set expectation.
145 $this->expectException();
146 // Exercise SUT
147 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
148 }
149
150 public function test_header_printed_false_initially() {
151 // Validate
152 $this->assertFalse($this->testpage->headerprinted);
153 }
154
155 public function test_header_printed_becomes_true() {
156 // Exercise SUT
157 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
158 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
159 // Validate
160 $this->assertTrue($this->testpage->headerprinted);
161 }
162
163 public function test_cant_get_context_before_set() {
164 // Set expectation.
165 $this->expectException();
166 // Exercise SUT
167 $this->testpage->context;
168 }
169
170 public function test_set_context() {
171 // Setup fixture
172 $context = $this->create_a_context();
173 // Exercise SUT
174 $this->testpage->set_context($context);
175 // Validate
176 $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context);
177 }
f230ce19 178
e88462a0 179 public function test_pagetype_defaults_to_script() {
180 // Exercise SUT and validate
181 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
f230ce19 182 }
183
184 public function test_set_pagetype() {
185 // Exercise SUT
186 $this->testpage->set_pagetype('a-page-type');
187 // Validate
188 $this->assertEqual('a-page-type', $this->testpage->pagetype);
189 }
e88462a0 190
191 public function test_initialise_default_pagetype() {
192 // Exercise SUT
193 $this->testpage->initialise_default_pagetype('admin/report/unittest/index.php');
194 // Validate
195 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
196 }
197
198 public function test_initialise_default_pagetype_fp() {
199 // Exercise SUT
200 $this->testpage->initialise_default_pagetype('index.php');
201 // Validate
202 $this->assertEqual('site-index', $this->testpage->pagetype);
203 }
753debd2 204
205 public function test_get_body_classes_empty() {
206 // Validate
207 $this->assertEqual('', $this->testpage->bodyclasses);
208 }
209
210 public function test_get_body_classes_single() {
211 // Exercise SUT
212 $this->testpage->add_body_class('aclassname');
213 // Validate
214 $this->assertEqual('aclassname', $this->testpage->bodyclasses);
215 }
216
89fbdca3 217 public function test_get_body_classes() {
753debd2 218 // Exercise SUT
89fbdca3 219 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
753debd2 220 // Validate
221 $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses);
222 }
89fbdca3 223
224 public function test_url_to_class_name() {
225 $this->assertEqual('example-com', $this->testpage->url_to_class_name('http://example.com'));
226 $this->assertEqual('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
227 $this->assertEqual('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
228 $this->assertEqual('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
229 }
2afe21ee 230}
231
232/**
233 * Test functions that affect filter_active table with contextid = $syscontextid.
234 */
235class moodle_page_with_db_test extends UnitTestCaseUsingDatabase {
236 protected $testpage;
237 protected $originalcourse;
238
239 public function setUp() {
240 global $COURSE;
241 $this->originalcourse = $COURSE;
242 $this->testpage = new moodle_page();
243 $this->create_test_table('context', 'lib');
244 $this->switch_to_test_db();
245 }
246
247 public function tearDown() {
248 global $COURSE;
249 $this->testpage = NULL;
250 $COURSE = $this->originalcourse;
251 }
252
253 /** Creates an object with all the fields you would expect a $course object to have. */
254 protected function create_a_course_with_context() {
255 $course = new stdClass;
256 $course->id = -1;
257 $course->category = 2;
258 $course->fullname = 'Anonymous test course';
259 $course->shortname = 'ANON';
260 $course->summary = '';
261
262 $context = new stdClass;
263 $context->contextlevel = CONTEXT_COURSE;
264 $context->instanceid = $course->id;
265 $context->path = 'not initialised';
266 $context->depth = '-1';
267 $this->testdb->insert_record('context', $context);
268
269 return $course;
270 }
271
272 public function test_setting_course_sets_context() {
273 // Setup fixture
274 $course = $this->create_a_course_with_context();
275 // Exercise SUT
276 $this->testpage->set_course($course);
277 // Validate
278 $expectedcontext = new stdClass;
279 $expectedcontext->contextlevel = CONTEXT_COURSE;
280 $expectedcontext->instanceid = $course->id;
281 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
282 }
283}
284?>