MDL-24060 text_to_html tests. Make sure alt text for LaTeX images doesn't break.
[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