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