9f36c35c9d8ac29901169bd09441d53c149ff941
[moodle.git] / lib / simpletest / testsimpletestlib.php
1 <?php
2 /**
3  * Unit tests for (some of) ../simpletestlib.php.
4  *
5  * @author T.J.Hunt@open.ac.uk
6  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7  * @package SimpleTestEx
8  */
10 if (!defined('MOODLE_INTERNAL')) {
11     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
12 }
14 class simpletestlib_test extends UnitTestCaseUsingDatabase {
16     function test_table_creation_and_data() {
17         global $DB;
19         $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
20         $dbman = $DB->get_manager();
22         // Create table and test
23         $this->create_test_table('context', 'lib');
24         $this->assertTrue($dbman->table_exists('context'));
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));
31         foreach($sampledata as $key => $record) {
32             $sampledata[$key]['id'] = $DB->insert_record('context', $record);
33         }
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     }
43     function test_tables_are_dropped() {
44         global $DB;
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'));
50     }
51 }
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  */
60 class 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     }
66     function test_other_attrs() {
67         $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
68         $this->assertTrue($expectation->test('<span     oneattr="thingy"   class  =  "error"  otherattr="thingy">message</span>'));
69     }
71     function test_fails() {
72         $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
73         $this->assertFalse($expectation->test('<span class="mismatch">message</span>'));
74     }
76     function test_link() {
77         $html = '<a href="http://www.test.com">Click Here</a>';
78         $expectation = new ContainsTagWithAttribute('a', 'href', 'http://www.test.com');
79         $this->assertTrue($expectation->test($html));
80         $this->assert(new ContainsTagWithContents('a', 'Click Here'), $html);
81     }
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>'));
87     }
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);
92     }
94     function test_real_regression1() {
95         $expectation = new ContainsTagWithAttribute('label', 'for', 'html_select4ac387224bf9d');
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     }
100     function test_zero_attr() {
101         $expectation = new ContainsTagWithAttribute('span', 'class', 0);
102         $this->assertTrue($expectation->test('<span class="0">message</span>'));
103     }
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     }
110     function test_blank_attr() {
111         $expectation = new ContainsTagWithAttribute('span', 'class', '');
112         $this->assertTrue($expectation->test('<span class="">message</span>'));
113     }
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     }
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  */
128 class 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" />
132 END;
133         $expectation = new ContainsTagWithAttributes('input',
134                 array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
135         $this->assert($expectation, $content);
136     }
138     function test_zero_attr() {
139         $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
140         $this->assertTrue($expectation->test('<span class="0">message</span>'));
141     }
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     }
148     function test_blank_attr() {
149         $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
150         $this->assertTrue($expectation->test('<span class="">message</span>'));
151     }
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     }
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  */
166 class 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     }
172     function test_no_end() {
173         $expectation = new ContainsTagWithContents('span', 'message');
174         $this->assertFalse($expectation->test('<span class="error">message'));
175     }
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  */
185 class 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     }
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     }
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     }
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     }
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     }
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     }
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     }
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     }
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     }
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     }
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     }
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  */
294 class 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" />
298 END;
299         $expectation = new DoesNotContainTagWithAttributes('input',
300                 array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
301         $this->assertFalse($expectation->test($content));
302     }
304     function test_zero_attr() {
305         $expectation = new DoesNotContainTagWithAttributes('span', array('class' => 0));
306         $this->assertFalse($expectation->test('<span class="0">message</span>'));
307     }
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     }
314     function test_blank_attr() {
315         $expectation = new DoesNotContainTagWithAttributes('span', array('class' => ''));
316         $this->assertFalse($expectation->test('<span class="">message</span>'));
317     }
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     }