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