MDL-14958 Basic tests done
[moodle.git] / lib / dml / simpletest / testdmllib.php
CommitLineData
73f7ad71 1<?php
2/**
3 * Unit tests for dmllib
4 * @package dmllib
5 */
6
7if (!defined('MOODLE_INTERNAL')) {
8 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
9}
10
73f7ad71 11class dmllib_test extends UnitTestCase {
12 private $tables = array();
73f7ad71 13 private $db;
14
15 function setUp() {
b936956e 16 global $CFG, $DB, $FUNCT_TEST_DB;
e6b4f00e 17
b936956e 18 if (isset($FUNCT_TEST_DB)) {
19 $this->db = $FUNCT_TEST_DB;
e6b4f00e 20 } else {
21 $this->db = $DB;
22 }
73f7ad71 23
e6b4f00e 24 $dbmanager = $this->db->get_manager();
73f7ad71 25
26 $table = new xmldb_table("testtable");
e6b4f00e 27 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
28 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
29 $table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
73f7ad71 30 array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
e6b4f00e 31 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
32 $table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
33 $table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
34 $table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
35 $table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
36 $table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
37 $table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
38 $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
39 $table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
40 $table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
41 $table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
42 $table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
43 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
44 $table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
45 $table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
46 $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
47 $table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
48 $table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
49 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
50 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
73f7ad71 51 $table->setComment("This is a test'n drop table. You can drop it safely");
52
e6b4f00e 53 if ($dbmanager->table_exists($table)) {
54 $dbmanager->drop_table($table, true, false);
55 }
e6b4f00e 56 $dbmanager->create_table($table);
809df0e2 57 $this->tables[$table->getName()] = $table;
73f7ad71 58
73f7ad71 59 }
60
61 function tearDown() {
809df0e2 62 $dbmanager = $this->db->get_manager();
63
64 foreach ($this->tables as $table) {
65 if ($dbmanager->table_exists($table)) {
66 $dbmanager->drop_table($table, true, false);
73f7ad71 67 }
68 }
e6b4f00e 69 $this->tables = array();
73f7ad71 70 }
71
72 function test_fix_sql_params() {
e6b4f00e 73 $DB = $this->db; // do not use global $DB!
74
73f7ad71 75 // Malformed table placeholder
76 $sql = "SELECT * FROM [testtable]";
e6b4f00e 77 $sqlarray = $DB->fix_sql_params($sql);
73f7ad71 78 $this->assertEqual($sql, $sqlarray[0]);
79
80 // Correct table placeholder substitution
81 $sql = "SELECT * FROM {testtable}";
e6b4f00e 82 $sqlarray = $DB->fix_sql_params($sql);
83 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable", $sqlarray[0]);
73f7ad71 84
85 // Malformed param placeholders
86 $sql = "SELECT * FROM {testtable} WHERE name = ?param1";
87 $params = array('param1' => 'first record');
e6b4f00e 88 $sqlarray = $DB->fix_sql_params($sql, $params);
89 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
73f7ad71 90
91 // Mixed param types (colon and dollar)
92 $sql = "SELECT * FROM {testtable} WHERE name = :param1, rsstype = \$1";
93 $params = array('param1' => 'first record', 'param2' => 1);
94 try {
e6b4f00e 95 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 96 } catch (Exception $e) {
97 $this->assertEqual('error() call: ERROR: Mixed types of sql query parameters!!', $e->getMessage());
98 }
99
100 // Mixed param types (question and dollar)
101 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = \$1";
102 $params = array('param1' => 'first record', 'param2' => 1);
103 $exception_caught = false;
104 try {
e6b4f00e 105 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 106 } catch (Exception $e) {
107 $exception_caught = true;
108 }
109 $this->assertTrue($exception_caught);
110
111 // Too many params in sql
112 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?, course = ?";
113 $params = array('first record', 1);
114 $exception_caught = false;
115 try {
e6b4f00e 116 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 117 } catch (Exception $e) {
118 $exception_caught = true;
119 }
120 $this->assertTrue($exception_caught);
121
122 // Too many params in array: no error
123 $params[] = 1;
124 $params[] = time();
125 $exception_caught = false;
126 $sqlarray = null;
127
128 try {
e6b4f00e 129 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 130 } catch (Exception $e) {
131 $exception_caught = true;
132 }
133 $this->assertFalse($exception_caught);
134 $this->assertTrue($sqlarray[0]);
135
136 // Named params missing from array
137 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
138 $params = array('wrongname' => 'first record', 'rsstype' => 1);
139 $exception_caught = false;
140 try {
e6b4f00e 141 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 142 } catch (Exception $e) {
143 $exception_caught = true;
144 }
145 $this->assertTrue($exception_caught);
146
147 // Duplicate named param in query
148 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :name";
149 $params = array('name' => 'first record', 'rsstype' => 1);
150 $exception_caught = false;
151 try {
e6b4f00e 152 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 153 } catch (Exception $e) {
154 $exception_caught = true;
155 }
156 $this->assertTrue($exception_caught);
157
158 // Unsupported Bound params
159 $sql = "SELECT * FROM {testtable} WHERE name = $1, rsstype = $2";
160 $params = array('first record', 1);
161 $exception_caught = false;
162 try {
e6b4f00e 163 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 164 } catch (Exception $e) {
165 $exception_caught = true;
166 }
167 $this->assertTrue($exception_caught);
168
169 // Correct named param placeholders
170 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
171 $params = array('name' => 'first record', 'rsstype' => 1);
e6b4f00e 172 $sqlarray = $DB->fix_sql_params($sql, $params);
173 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
73f7ad71 174 $this->assertEqual(2, count($sqlarray[1]));
175
176 // Correct ? params
177 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?";
178 $params = array('first record', 1);
e6b4f00e 179 $sqlarray = $DB->fix_sql_params($sql, $params);
180 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
73f7ad71 181 $this->assertEqual(2, count($sqlarray[1]));
182
183 }
184
bb78c788 185 public function testGetTables() {
186 global $DB;
187 // Need to test with multiple DBs
188 $this->assertTrue($DB->get_tables() > 2);
189 }
190
191 public function testGetIndexes() {
192 global $DB;
193 // Need to test with multiple DBs
194 $this->assertTrue($indices = $DB->get_indexes('testtable'));
195 $this->assertTrue(count($indices) == 1);
196
197 $xmldb_indexes = $this->tables['testtable']->getIndexes();
198 $this->assertEqual(count($indices), count($xmldb_indexes));
199
200 for ($i = 0; $i < count($indices); $i++) {
201 if ($i == 0) {
202 $next_index = reset($indices);
203 $next_xmldb_index = reset($xmldb_indexes);
204 } else {
205 $next_index = next($indices);
206 $next_xmldb_index = next($xmldb_indexes);
207 }
208
209 $this->assertEqual($next_index['columns'][0], $next_xmldb_index->name);
210 }
211 }
212
213 public function testGetColumns() {
214 global $DB;
215
216 $this->assertTrue($columns = $DB->get_columns('testtable'));
217 $fields = $this->tables['testtable']->getFields();
218 $this->assertEqual(count($columns), count($fields));
219
220 for ($i = 0; $i < count($columns); $i++) {
221 if ($i == 0) {
222 $next_column = reset($columns);
223 $next_field = reset($fields);
224 } else {
225 $next_column = next($columns);
226 $next_field = next($fields);
227 }
228
229 $this->assertEqual($next_column->name, $next_field->name);
230 }
231 }
232
233 public function testExecute() {
234 global $DB;
235 $sql = "SELECT * FROM {testtable}";
236 $this->assertTrue($DB->execute($sql));
73f7ad71 237
bb78c788 238 $sql = "INSERT INTO {testtable}
239 SET course = :course,
240 type = :type,
241 name = :name,
242 intro = :intro,
243 assessed = :assessed,
244 assesstimestart = :assesstimestart,
245 assesstimefinish = :assesstimefinish,
246 scale = :scale,
247 maxbytes = :maxbytes,
248 forcesubscribe = :forcesubscribe,
249 trackingtype = :trackingtype,
250 rsstype = :rsstype,
251 rssarticles = :rssarticles,
252 timemodified = :timemodified,
253 warnafter = :warnafter,
254 blockafter = :blockafter,
255 blockperiod = :blockperiod";
256 $values = array('course' => 1,
257 'type' => 'news',
258 'name' => 'test',
259 'intro' => 'Simple news forum',
260 'assessed' => time(),
261 'assesstimestart' => time(),
262 'assesstimefinish' => time() + 579343,
263 'scale' => 1,
264 'maxbytes' => 512,
265 'forcesubscribe' => 1,
266 'trackingtype' => 1,
267 'rssarticles' => 1,
268 'rsstype' => 1,
269 'timemodified' => time(),
270 'warnafter' => time() + 579343,
271 'blockafter' => time() + 600000,
272 'blockperiod' => 5533);
273 $this->assertTrue($DB->execute($sql, $values));
274
275 $record = $DB->get_record('testtable', array('blockperiod' => 5533));
276
277 foreach ($values as $field => $value) {
278 $this->assertEqual($value, $record->$field, "Field $field in DB ({$record->$field}) is not equal to field $field in sql ($value)");
279 }
280 }
281
282}