moodle_page: MDL-12212 implement ->context
[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 }
169}
170
171/**
172 * Test functions that affect filter_active table with contextid = $syscontextid.
173 */
174class moodle_page_with_db_test extends UnitTestCaseUsingDatabase {
175 protected $testpage;
176 protected $originalcourse;
177
178 public function setUp() {
179 global $COURSE;
180 $this->originalcourse = $COURSE;
181 $this->testpage = new moodle_page();
182 $this->create_test_table('context', 'lib');
183 $this->switch_to_test_db();
184 }
185
186 public function tearDown() {
187 global $COURSE;
188 $this->testpage = NULL;
189 $COURSE = $this->originalcourse;
190 }
191
192 /** Creates an object with all the fields you would expect a $course object to have. */
193 protected function create_a_course_with_context() {
194 $course = new stdClass;
195 $course->id = -1;
196 $course->category = 2;
197 $course->fullname = 'Anonymous test course';
198 $course->shortname = 'ANON';
199 $course->summary = '';
200
201 $context = new stdClass;
202 $context->contextlevel = CONTEXT_COURSE;
203 $context->instanceid = $course->id;
204 $context->path = 'not initialised';
205 $context->depth = '-1';
206 $this->testdb->insert_record('context', $context);
207
208 return $course;
209 }
210
211 public function test_setting_course_sets_context() {
212 // Setup fixture
213 $course = $this->create_a_course_with_context();
214 // Exercise SUT
215 $this->testpage->set_course($course);
216 // Validate
217 $expectedcontext = new stdClass;
218 $expectedcontext->contextlevel = CONTEXT_COURSE;
219 $expectedcontext->instanceid = $course->id;
220 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context);
221 }
222}
223?>