MDL-25715 simpletestlib new expectations for testing renderer output.
[moodle.git] / lib / simpletest / testsimpletestlib.php
CommitLineData
821e4ecf 1<?php
2/**
b1bc1263 3 * Unit tests for (some of) ../simpletestlib.php.
821e4ecf 4 *
821e4ecf 5 * @author T.J.Hunt@open.ac.uk
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
b1bc1263 7 * @package SimpleTestEx
821e4ecf 8 */
9
10if (!defined('MOODLE_INTERNAL')) {
11 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
12}
13
8eb32141 14class simpletestlib_test extends UnitTestCaseUsingDatabase {
821e4ecf 15
8eb32141 16 function test_table_creation_and_data() {
821e4ecf 17 global $DB;
8eb32141
EL
18
19 $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
20 $dbman = $DB->get_manager();
21
22 // Create table and test
23 $this->create_test_table('context', 'lib');
24 $this->assertTrue($dbman->table_exists('context'));
25
26 $sampledata = array(
27 array('contextlevel' => 10, 'instanceid' => 666, 'path' => '', 'depth' => 1),
28 array('contextlevel' => 40, 'instanceid' => 666, 'path' => '', 'depth' => 2),
29 array('contextlevel' => 50, 'instanceid' => 666, 'path' => '', 'depth' => 3));
30
31 foreach($sampledata as $key => $record) {
32 $sampledata[$key]['id'] = $DB->insert_record('context', $record);
33 }
34
35 // Just test added data and delete later
36 $this->assertEqual($DB->count_records('context'), 3);
37 $this->assertTrue($DB->record_exists('context', array('id' => $sampledata[0]['id'])));
38 $this->assertTrue($DB->get_field('context', 'contextlevel', array('id' => $sampledata[2]['id'])), $sampledata[2]['contextlevel']);
39 $DB->delete_records('context');
40 $this->assertFalse($DB->record_exists('context', array('id' => $sampledata[1]['id'])));
41 }
42
43 function test_tables_are_dropped() {
44 global $DB;
45
46 $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
47 $dbman = $DB->get_manager();
48 // Previous method tearDown *must* delete all created tables, so here 'context' must not exist anymore
49 $this->assertFalse($dbman->table_exists('context'));
821e4ecf 50 }
51}
b1bc1263 52
8954245a 53
54/**
55 * Unit tests for the ContainsTagWithAttribute_test class.
56 *
57 * @copyright 2009 Tim Hunt
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59 */
60class ContainsTagWithAttribute_test extends UnitTestCase {
61 function test_simple() {
62 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
63 $this->assertTrue($expectation->test('<span class="error">message</span>'));
64 }
65
66 function test_other_attrs() {
67 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
6a923128 68 $this->assertTrue($expectation->test('<span oneattr="thingy" class = "error" otherattr="thingy">message</span>'));
8954245a 69 }
70
71 function test_fails() {
72 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
73 $this->assertFalse($expectation->test('<span class="mismatch">message</span>'));
74 }
5daf5d35 75
76 function test_link() {
6a923128 77 $html = '<a href="http://www.test.com">Click Here</a>';
5daf5d35 78 $expectation = new ContainsTagWithAttribute('a', 'href', 'http://www.test.com');
6a923128 79 $this->assertTrue($expectation->test($html));
80 $this->assert(new ContainsTagWithContents('a', 'Click Here'), $html);
81 }
82
83 function test_garbage() {
84 $expectation = new ContainsTagWithAttribute('a', 'href', '!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~');
85 $this->assertTrue($expectation->test('<a href="!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~">Click Here</a>'));
86
87 }
88
89 function test_inline_js() {
90 $html = '<a title="Popup window" href="http://otheraddress.com" class="link" onclick="this.target=\'my_popup\';">Click here</a>';
91 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
5daf5d35 92 }
91b652ac 93
94 function test_real_regression1() {
4454447d 95 $expectation = new ContainsTagWithAttribute('label', 'for', 'html_select4ac387224bf9d');
91b652ac 96 $html = '<label for="html_select4ac387224bf9d">Cool menu</label><select name="mymenu" id="html_select4ac387224bf9d" class="menumymenu select"> <option value="0">Choose...</option><option value="10">ten</option><option value="c2">two</option></select>';
97 $this->assert($expectation, $html);
98 }
d5bbc449
TH
99
100 function test_zero_attr() {
101 $expectation = new ContainsTagWithAttribute('span', 'class', 0);
102 $this->assertTrue($expectation->test('<span class="0">message</span>'));
103 }
104
105 function test_zero_attr_does_not_match_blank() {
106 $expectation = new ContainsTagWithAttribute('span', 'class', 0);
107 $this->assertFalse($expectation->test('<span class="">message</span>'));
108 }
109
110 function test_blank_attr() {
111 $expectation = new ContainsTagWithAttribute('span', 'class', '');
112 $this->assertTrue($expectation->test('<span class="">message</span>'));
113 }
114
115 function test_blank_attr_does_not_match_zero() {
116 $expectation = new ContainsTagWithAttribute('span', 'class', '');
117 $this->assertFalse($expectation->test('<span class="0">message</span>'));
118 }
8954245a 119}
120
d5bbc449
TH
121
122/**
123 * Unit tests for the ContainsTagWithAttribute class.
124 *
125 * @copyright 2009 Tim Hunt
126 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
127 */
128class ContainsTagWithAttributes_test extends UnitTestCase {
129 function test_simple() {
130 $content = <<<END
131<input id="qIhr6wWLTt3,1_omact_gen_14" name="qIhr6wWLTt3,1_omact_gen_14" onclick="if(this.hasSubmitted) { return false; } this.hasSubmitted=true; preSubmit(this.form); return true;" type="submit" value="Check" />
132END;
133 $expectation = new ContainsTagWithAttributes('input',
134 array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
135 $this->assert($expectation, $content);
136 }
137
138 function test_zero_attr() {
139 $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
140 $this->assertTrue($expectation->test('<span class="0">message</span>'));
141 }
142
143 function test_zero_attr_does_not_match_blank() {
144 $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
145 $this->assertFalse($expectation->test('<span class="">message</span>'));
146 }
147
148 function test_blank_attr() {
149 $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
150 $this->assertTrue($expectation->test('<span class="">message</span>'));
151 }
152
153 function test_blank_attr_does_not_match_zero() {
154 $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
155 $this->assertFalse($expectation->test('<span class="0">message</span>'));
156 }
157}
158
159
8954245a 160/**
161 * Unit tests for the ContainsTagWithAttribute_test class.
162 *
163 * @copyright 2009 Tim Hunt
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165 */
166class ContainsTagWithContents_test extends UnitTestCase {
167 function test_simple() {
168 $expectation = new ContainsTagWithContents('span', 'message');
169 $this->assertTrue($expectation->test('<span class="error">message</span>'));
170 }
171
172 function test_no_end() {
173 $expectation = new ContainsTagWithContents('span', 'message');
174 $this->assertFalse($expectation->test('<span class="error">message'));
175 }
176}
177
8926f844 178
ccdd1596
TH
179/**
180 * Unit tests for the {@link ContainsSelectExpectation} class.
181 *
182 * @copyright 2010 The Open University.
183 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
184 */
185class ContainsSelectExpectation_test extends UnitTestCase {
186 function test_matching_select_passes() {
187 $expectation = new ContainsSelectExpectation('selectname', array('Choice1', 'Choice2'));
188 $this->assertTrue($expectation->test('
189 <select name="selectname">
190 <option value="0">Choice1</option>
191 <option value="1">Choice2</option>
192 </select>'));
193 }
194
195 function test_fails_if_no_select() {
196 $expectation = new ContainsSelectExpectation('selectname', array('Choice1', 'Choice2'));
197 $this->assertFalse($expectation->test('<span>should not match</span>'));
198 }
199
200 function test_select_with_missing_choices_fails() {
201 $expectation = new ContainsSelectExpectation('selectname', array('Choice1', 'Choice2'));
202 $this->assertFalse($expectation->test('
203 <select name="selectname">
204 <option value="0">Choice1</option>
205 </select>'));
206 }
207
208 function test_select_with_extra_choices_fails() {
209 $expectation = new ContainsSelectExpectation('selectname', array('Choice1'));
210 $this->assertFalse($expectation->test('
211 <select name="selectname">
212 <option value="0">Choice1</option>
213 <option value="1">Choice2</option>
214 </select>'));
215 }
216
217 function test_select_with_wrong_order_choices_fails() {
218 $expectation = new ContainsSelectExpectation('selectname', array('Choice1'));
219 $this->assertFalse($expectation->test('
220 <select name="selectname">
221 <option value="1">Choice2</option>
222 <option value="0">Choice1</option>
223 </select>'));
224 }
225
226 function test_select_check_selected_pass() {
227 $expectation = new ContainsSelectExpectation('selectname',
228 array('key1' => 'Choice1', 'key2' => 'Choice2'), 'key2');
229 $this->assertTrue($expectation->test('
230 <select name="selectname">
231 <option value="key1">Choice1</option>
232 <option value="key2" selected="selected">Choice2</option>
233 </select>'));
234 }
235
236 function test_select_check_wrong_one_selected_fail() {
237 $expectation = new ContainsSelectExpectation('selectname',
238 array('key1' => 'Choice1', 'key2' => 'Choice2'), 'key2');
239 $this->assertFalse($expectation->test('
240 <select name="selectname">
241 <option value="key1" selected="selected">Choice1</option>
242 <option value="key2">Choice2</option>
243 </select>'));
244 }
245
246 function test_select_check_nothing_selected_fail() {
247 $expectation = new ContainsSelectExpectation('selectname',
248 array('key1' => 'Choice1', 'key2' => 'Choice2'), 'key2');
249 $this->assertFalse($expectation->test('
250 <select name="selectname">
251 <option value="key1">Choice1</option>
252 <option value="key2">Choice2</option>
253 </select>'));
254 }
255
256 function test_select_disabled_pass() {
257 $expectation = new ContainsSelectExpectation('selectname',
258 array('key1' => 'Choice1', 'key2' => 'Choice2'), null, false);
259 $this->assertTrue($expectation->test('
260 <select name="selectname" disabled="disabled">
261 <option value="key1">Choice1</option>
262 <option value="key2">Choice2</option>
263 </select>'));
264 }
265
266 function test_select_disabled_fail1() {
267 $expectation = new ContainsSelectExpectation('selectname',
268 array('key1' => 'Choice1', 'key2' => 'Choice2'), null, true);
269 $this->assertFalse($expectation->test('
270 <select name="selectname" disabled="disabled">
271 <option value="key1">Choice1</option>
272 <option value="key2">Choice2</option>
273 </select>'));
274 }
275
276 function test_select_disabled_fail2() {
277 $expectation = new ContainsSelectExpectation('selectname',
278 array('key1' => 'Choice1', 'key2' => 'Choice2'), null, false);
279 $this->assertFalse($expectation->test('
280 <select name="selectname">
281 <option value="key1">Choice1</option>
282 <option value="key2">Choice2</option>
283 </select>'));
284 }
285}
286
287
288/**
289 * Unit tests for the {@link DoesNotContainTagWithAttributes} class.
290 *
291 * @copyright 2010 The Open University
292 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
293 */
294class DoesNotContainTagWithAttributes_test extends UnitTestCase {
295 function test_simple() {
296 $content = <<<END
297<input id="qIhr6wWLTt3,1_omact_gen_14" name="qIhr6wWLTt3,1_omact_gen_14" onclick="if(this.hasSubmitted) { return false; } this.hasSubmitted=true; preSubmit(this.form); return true;" type="submit" value="Check" />
298END;
299 $expectation = new DoesNotContainTagWithAttributes('input',
300 array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
301 $this->assertFalse($expectation->test($content));
302 }
303
304 function test_zero_attr() {
305 $expectation = new DoesNotContainTagWithAttributes('span', array('class' => 0));
306 $this->assertFalse($expectation->test('<span class="0">message</span>'));
307 }
308
309 function test_zero_different_attr_ok() {
310 $expectation = new DoesNotContainTagWithAttributes('span', array('class' => 'shrub'));
311 $this->assertTrue($expectation->test('<span class="tree">message</span>'));
312 }
313
314 function test_blank_attr() {
315 $expectation = new DoesNotContainTagWithAttributes('span', array('class' => ''));
316 $this->assertFalse($expectation->test('<span class="">message</span>'));
317 }
318
319 function test_blank_attr_does_not_match_zero() {
320 $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
321 $this->assertFalse($expectation->test('<span class="0">message</span>'));
322 }
323}