moodle_page: MDL-12212 eliminate the two different interpretations of
[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 }
44}
45
2afe21ee 46/**
47 * Test functions that affect filter_active table with contextid = $syscontextid.
48 */
49class moodle_page_test extends UnitTestCase {
50 protected $testpage;
51 protected $originalcourse;
52
53 public function setUp() {
54 global $COURSE;
55 $this->originalcourse = $COURSE;
e88462a0 56 $this->testpage = new testable_moodle_page();
2afe21ee 57 }
58
59 public function tearDown() {
60 global $COURSE;
61 $this->testpage = NULL;
62 $COURSE = $this->originalcourse;
63 }
64
65 /** Creates an object with all the fields you would expect a $course object to have. */
66 protected function create_a_course() {
67 $course = new stdClass;
68 $course->id = -1;
69 $course->category = 2;
70 $course->fullname = 'Anonymous test course';
71 $course->shortname = 'ANON';
72 $course->summary = '';
73 return $course;
74 }
75
76 /** Creates an object with all the fields you would expect a $course object to have. */
77 protected function create_a_context() {
78 $context = new stdClass;
79 $context->id = 2;
80 $context->contextlevel = CONTEXT_COURSECAT;
81 $context->instanceid = 1;
82 $context->path = '/1/2';
83 $context->depth = '2';
84 return $context;
85 }
86
87 public function test_course_returns_site_before_set() {
88 global $SITE;
89 // Validate
90 $this->assertIdentical($SITE, $this->testpage->course);
91 }
92
93 public function test_setting_course_works() {
94 // Setup fixture
95 $course = $this->create_a_course();
96 $this->testpage->set_context(new stdClass); // Avoid trying to set the context.
97 // Exercise SUT
98 $this->testpage->set_course($course);
99 // Validate
100 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course);
101 }
102
103 public function test_global_course_and_page_course_are_same() {
104 global $COURSE;
105 // Setup fixture
106 $course = $this->create_a_course();
107 $this->testpage->set_context(new stdClass); // Avoid trying to set the context.
108 // Exercise SUT
109 $this->testpage->set_course($course);
110 // Validate
111 $this->assertIdentical($this->testpage->course, $COURSE);
112 }
113
114 public function test_cannot_set_course_once_output_started() {
115 // Setup fixture
116 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
117 $course = $this->create_a_course();
118 // Set expectation.
119 $this->expectException();
120 // Exercise SUT
121 $this->testpage->set_course($course);
122 }
123
124 public function test_set_state_normal_path() {
125 $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
126
127 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
128 $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
129
130 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
131 $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state);
132
133 $this->testpage->set_state(moodle_page::STATE_PRINTING_FOOTER);
134 $this->assertEqual(moodle_page::STATE_PRINTING_FOOTER, $this->testpage->state);
135
136 $this->testpage->set_state(moodle_page::STATE_DONE);
137 $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state);
138 }
139
140 public function test_set_state_cannot_skip_one() {
141 // Set expectation.
142 $this->expectException();
143 // Exercise SUT
144 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
145 }
146
147 public function test_header_printed_false_initially() {
148 // Validate
149 $this->assertFalse($this->testpage->headerprinted);
150 }
151
152 public function test_header_printed_becomes_true() {
153 // Exercise SUT
154 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
155 $this->testpage->set_state(moodle_page::STATE_IN_BODY);
156 // Validate
157 $this->assertTrue($this->testpage->headerprinted);
158 }
159
160 public function test_cant_get_context_before_set() {
161 // Set expectation.
162 $this->expectException();
163 // Exercise SUT
164 $this->testpage->context;
165 }
166
167 public function test_set_context() {
168 // Setup fixture
169 $context = $this->create_a_context();
170 // Exercise SUT
171 $this->testpage->set_context($context);
172 // Validate
173 $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context);
174 }
f230ce19 175
e88462a0 176 public function test_pagetype_defaults_to_script() {
177 // Exercise SUT and validate
178 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
f230ce19 179 }
180
181 public function test_set_pagetype() {
182 // Exercise SUT
183 $this->testpage->set_pagetype('a-page-type');
184 // Validate
185 $this->assertEqual('a-page-type', $this->testpage->pagetype);
186 }
e88462a0 187
188 public function test_initialise_default_pagetype() {
189 // Exercise SUT
190 $this->testpage->initialise_default_pagetype('admin/report/unittest/index.php');
191 // Validate
192 $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype);
193 }
194
195 public function test_initialise_default_pagetype_fp() {
196 // Exercise SUT
197 $this->testpage->initialise_default_pagetype('index.php');
198 // Validate
199 $this->assertEqual('site-index', $this->testpage->pagetype);
200 }
2afe21ee 201}
202
203/**
204 * Test functions that affect filter_active table with contextid = $syscontextid.
205 */
206class moodle_page_with_db_test extends UnitTestCaseUsingDatabase {
207 protected $testpage;
208 protected $originalcourse;
209
210 public function setUp() {
211 global $COURSE;
212 $this->originalcourse = $COURSE;
213 $this->testpage = new moodle_page();
214 $this->create_test_table('context', 'lib');
215 $this->switch_to_test_db();
216 }
217
218 public function tearDown() {
219 global $COURSE;
220 $this->testpage = NULL;
221 $COURSE = $this->originalcourse;
222 }
223
224 /** Creates an object with all the fields you would expect a $course object to have. */
225 protected function create_a_course_with_context() {
226 $course = new stdClass;
227 $course->id = -1;
228 $course->category = 2;
229 $course->fullname = 'Anonymous test course';
230 $course->shortname = 'ANON';
231 $course->summary = '';
232
233 $context = new stdClass;
234 $context->contextlevel = CONTEXT_COURSE;
235 $context->instanceid = $course->id;
236 $context->path = 'not initialised';
237 $context->depth = '-1';
238 $this->testdb->insert_record('context', $context);
239
240 return $course;
241 }
242
243 public function test_setting_course_sets_context() {
244 // Setup fixture
245 $course = $this->create_a_course_with_context();
246 // Exercise SUT
247 $this->testpage->set_course($course);
248 // Validate
249 $expectedcontext = new stdClass;
250 $expectedcontext->contextlevel = CONTEXT_COURSE;
251 $expectedcontext->instanceid = $course->id;
252 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
253 }
254}
255?>