MDL-14741: use the value of the main killswitch, and add back missing functions
[moodle.git] / lib / dml / simpletest / testdml.php
CommitLineData
73f7ad71 1<?php
2/**
3dce78e1 3 * Unit tests for dml
4 * @package dml
73f7ad71 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
3dce78e1 11class dml_test extends UnitTestCase {
73f7ad71 12 private $tables = array();
251387d0 13 private $tdb;
b579f0db 14 private $data;
73f7ad71 15
16 function setUp() {
a230012c 17 global $CFG, $DB, $UNITTEST;
e6b4f00e 18
a230012c 19 if (isset($UNITTEST->func_test_db)) {
251387d0 20 $this->tdb = $UNITTEST->func_test_db;
e6b4f00e 21 } else {
251387d0 22 $this->tdb = $DB;
e6b4f00e 23 }
73f7ad71 24
73f7ad71 25 }
26
27 function tearDown() {
251387d0 28 $dbman = $this->tdb->get_manager();
809df0e2 29
30 foreach ($this->tables as $table) {
251387d0 31 if ($dbman->table_exists($table)) {
32 $dbman->drop_table($table, true, false);
73f7ad71 33 }
34 }
e6b4f00e 35 $this->tables = array();
73f7ad71 36 }
37
38 function test_fix_sql_params() {
7f9f11b5 39 $DB = $this->tdb;
b579f0db 40
73f7ad71 41 // Malformed table placeholder
42 $sql = "SELECT * FROM [testtable]";
e6b4f00e 43 $sqlarray = $DB->fix_sql_params($sql);
73f7ad71 44 $this->assertEqual($sql, $sqlarray[0]);
45
46 // Correct table placeholder substitution
47 $sql = "SELECT * FROM {testtable}";
e6b4f00e 48 $sqlarray = $DB->fix_sql_params($sql);
49 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable", $sqlarray[0]);
73f7ad71 50
51 // Malformed param placeholders
52 $sql = "SELECT * FROM {testtable} WHERE name = ?param1";
b579f0db 53 $params = array('param1' => 'record2');
e6b4f00e 54 $sqlarray = $DB->fix_sql_params($sql, $params);
55 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
73f7ad71 56
57 // Mixed param types (colon and dollar)
b579f0db 58 $sql = "SELECT * FROM {testtable} WHERE name = :param1, course = \$1";
59 $params = array('param1' => 'record1', 'param2' => 3);
73f7ad71 60 try {
e6b4f00e 61 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 62 $this->fail("Expecting an exception, none occurred");
73f7ad71 63 } catch (Exception $e) {
251387d0 64 $this->assertTrue($e instanceof moodle_exception);
73f7ad71 65 }
66
67 // Mixed param types (question and dollar)
b579f0db 68 $sql = "SELECT * FROM {testtable} WHERE name = ?, course = \$1";
69 $params = array('param1' => 'record2', 'param2' => 5);
73f7ad71 70 try {
e6b4f00e 71 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 72 $this->fail("Expecting an exception, none occurred");
73f7ad71 73 } catch (Exception $e) {
251387d0 74 $this->assertTrue($e instanceof moodle_exception);
73f7ad71 75 }
73f7ad71 76
77 // Too many params in sql
b579f0db 78 $sql = "SELECT * FROM {testtable} WHERE name = ?, course = ?, id = ?";
79 $params = array('record2', 3);
73f7ad71 80 try {
e6b4f00e 81 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 82 $this->fail("Expecting an exception, none occurred");
73f7ad71 83 } catch (Exception $e) {
251387d0 84 $this->assertTrue($e instanceof moodle_exception);
73f7ad71 85 }
73f7ad71 86
87 // Too many params in array: no error
88 $params[] = 1;
89 $params[] = time();
73f7ad71 90 $sqlarray = null;
91
92 try {
e6b4f00e 93 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 94 $this->pass();
73f7ad71 95 } catch (Exception $e) {
b579f0db 96 $this->fail("Unexpected " . get_class($e) . " exception");
73f7ad71 97 }
73f7ad71 98 $this->assertTrue($sqlarray[0]);
99
100 // Named params missing from array
b579f0db 101 $sql = "SELECT * FROM {testtable} WHERE name = :name, course = :course";
102 $params = array('wrongname' => 'record1', 'course' => 1);
73f7ad71 103 try {
e6b4f00e 104 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 105 $this->fail("Expecting an exception, none occurred");
73f7ad71 106 } catch (Exception $e) {
251387d0 107 $this->assertTrue($e instanceof moodle_exception);
73f7ad71 108 }
73f7ad71 109
110 // Duplicate named param in query
b579f0db 111 $sql = "SELECT * FROM {testtable} WHERE name = :name, course = :name";
112 $params = array('name' => 'record2', 'course' => 3);
73f7ad71 113 try {
e6b4f00e 114 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 115 $this->fail("Expecting an exception, none occurred");
73f7ad71 116 } catch (Exception $e) {
251387d0 117 $this->assertTrue($e instanceof moodle_exception);
73f7ad71 118 }
73f7ad71 119
120 // Unsupported Bound params
b579f0db 121 $sql = "SELECT * FROM {testtable} WHERE name = $1, course = $2";
73f7ad71 122 $params = array('first record', 1);
73f7ad71 123 try {
e6b4f00e 124 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 125 $this->fail("Expecting an exception, none occurred");
73f7ad71 126 } catch (Exception $e) {
251387d0 127 $this->assertTrue($e instanceof moodle_exception);
73f7ad71 128 }
73f7ad71 129
130 // Correct named param placeholders
b579f0db 131 $sql = "SELECT * FROM {testtable} WHERE name = :name, course = :course";
132 $params = array('name' => 'first record', 'course' => 1);
e6b4f00e 133 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 134 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, course = ?", $sqlarray[0]);
73f7ad71 135 $this->assertEqual(2, count($sqlarray[1]));
136
137 // Correct ? params
b579f0db 138 $sql = "SELECT * FROM {testtable} WHERE name = ?, course = ?";
73f7ad71 139 $params = array('first record', 1);
e6b4f00e 140 $sqlarray = $DB->fix_sql_params($sql, $params);
b579f0db 141 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, course = ?", $sqlarray[0]);
73f7ad71 142 $this->assertEqual(2, count($sqlarray[1]));
143
144 }
145
bb78c788 146 public function testGetTables() {
7f9f11b5 147 $DB = $this->tdb;
251387d0 148 $dbman = $this->tdb->get_manager();
a230012c 149
b579f0db 150 $original_count = count($DB->get_tables());
151
bb78c788 152 // Need to test with multiple DBs
b579f0db 153 $table = new xmldb_table("testtable");
154 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
b579f0db 155 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
156 $dbman->create_table($table);
157 $this->tables[$table->getName()] = $table;
158 $this->assertTrue(count($DB->get_tables()) == $original_count + 1);
bb78c788 159 }
160
161 public function testGetIndexes() {
7f9f11b5 162 $DB = $this->tdb;
251387d0 163 $dbman = $this->tdb->get_manager();
a230012c 164
b579f0db 165 $table = new xmldb_table("testtable");
166 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
167 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
b579f0db 168 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
169 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
170 $dbman->create_table($table);
171 $this->tables[$table->getName()] = $table;
172
bb78c788 173 $this->assertTrue($indices = $DB->get_indexes('testtable'));
174 $this->assertTrue(count($indices) == 1);
175
0088bd31 176 $xmldb_indexes = $table->getIndexes();
bb78c788 177 $this->assertEqual(count($indices), count($xmldb_indexes));
178
179 for ($i = 0; $i < count($indices); $i++) {
180 if ($i == 0) {
181 $next_index = reset($indices);
182 $next_xmldb_index = reset($xmldb_indexes);
183 } else {
184 $next_index = next($indices);
185 $next_xmldb_index = next($xmldb_indexes);
186 }
187
188 $this->assertEqual($next_index['columns'][0], $next_xmldb_index->name);
189 }
190 }
191
192 public function testGetColumns() {
7f9f11b5 193 $DB = $this->tdb;
251387d0 194 $dbman = $this->tdb->get_manager();
bb78c788 195
b579f0db 196 $table = new xmldb_table("testtable");
197 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
198 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
199 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '0');
b579f0db 200 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
201 $dbman->create_table($table);
202 $this->tables[$table->getName()] = $table;
203
bb78c788 204 $this->assertTrue($columns = $DB->get_columns('testtable'));
205 $fields = $this->tables['testtable']->getFields();
206 $this->assertEqual(count($columns), count($fields));
207
208 for ($i = 0; $i < count($columns); $i++) {
209 if ($i == 0) {
210 $next_column = reset($columns);
211 $next_field = reset($fields);
212 } else {
213 $next_column = next($columns);
214 $next_field = next($fields);
215 }
216
217 $this->assertEqual($next_column->name, $next_field->name);
218 }
219 }
220
221 public function testExecute() {
7f9f11b5 222 $DB = $this->tdb;
251387d0 223 $dbman = $this->tdb->get_manager();
a230012c 224
b579f0db 225 $table = new xmldb_table("testtable");
226 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
227 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
228 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '0');
229 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
230 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
231 $dbman->create_table($table);
232 $this->tables[$table->getName()] = $table;
233
bb78c788 234 $sql = "SELECT * FROM {testtable}";
b579f0db 235
bb78c788 236 $this->assertTrue($DB->execute($sql));
73f7ad71 237
b579f0db 238 $params = array('course' => 1, 'name' => 'test');
6807d2b3 239
240 $sql = "INSERT INTO {testtable} (".implode(',', array_keys($params)).")
241 VALUES (".implode(',', array_fill(0, count($params), '?')).")";
242
243
244 $this->assertTrue($DB->execute($sql, $params));
bb78c788 245
b579f0db 246 $record = $DB->get_record('testtable', array('id' => 1));
bb78c788 247
6807d2b3 248 foreach ($params as $field => $value) {
bb78c788 249 $this->assertEqual($value, $record->$field, "Field $field in DB ({$record->$field}) is not equal to field $field in sql ($value)");
250 }
251 }
252
7f9f11b5 253 public function test_get_in_or_equal() {
254 $DB = $this->tdb;
255
256 // SQL_PARAMS_QM - IN or =
257
258 // Correct usage of multiple values
259 $in_values = array('value1', 'value2', 'value3', 'value4');
260 list($usql, $params) = $DB->get_in_or_equal($in_values);
261 $this->assertEqual("IN (?,?,?,?)", $usql);
262 $this->assertEqual(4, count($params));
263 foreach ($params as $key => $value) {
264 $this->assertEqual($in_values[$key], $value);
265 }
266
267 // Correct usage of single value (in an array)
268 $in_values = array('value1');
269 list($usql, $params) = $DB->get_in_or_equal($in_values);
270 $this->assertEqual("= ?", $usql);
271 $this->assertEqual(1, count($params));
272 $this->assertEqual($in_values[0], $params[0]);
273
274 // Correct usage of single value
275 $in_value = 'value1';
276 list($usql, $params) = $DB->get_in_or_equal($in_values);
277 $this->assertEqual("= ?", $usql);
278 $this->assertEqual(1, count($params));
279 $this->assertEqual($in_value, $params[0]);
280
281 // SQL_PARAMS_QM - NOT IN or <>
282
283 // Correct usage of multiple values
284 $in_values = array('value1', 'value2', 'value3', 'value4');
285 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false);
286 $this->assertEqual("NOT IN (?,?,?,?)", $usql);
287 $this->assertEqual(4, count($params));
288 foreach ($params as $key => $value) {
289 $this->assertEqual($in_values[$key], $value);
290 }
291
292 // Correct usage of single value (in array()
293 $in_values = array('value1');
294 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false);
295 $this->assertEqual("<> ?", $usql);
296 $this->assertEqual(1, count($params));
297 $this->assertEqual($in_values[0], $params[0]);
298
299 // Correct usage of single value
300 $in_value = 'value1';
301 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false);
302 $this->assertEqual("<> ?", $usql);
303 $this->assertEqual(1, count($params));
304 $this->assertEqual($in_value, $params[0]);
305
306 // SQL_PARAMS_NAMED - IN or =
307
308 // Correct usage of multiple values
309 $in_values = array('value1', 'value2', 'value3', 'value4');
310 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', true);
311 $this->assertEqual("IN (:param01,:param02,:param03,:param04)", $usql);
312 $this->assertEqual(4, count($params));
313 reset($in_values);
314 foreach ($params as $key => $value) {
315 $this->assertEqual(current($in_values), $value);
316 next($in_values);
317 }
318
319 // Correct usage of single values (in array)
320 $in_values = array('value1');
321 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', true);
322 $this->assertEqual("= :param01", $usql);
323 $this->assertEqual(1, count($params));
324 $this->assertEqual($in_values[0], $params['param01']);
325
326 // Correct usage of single value
327 $in_value = 'value1';
328 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', true);
329 $this->assertEqual("= :param01", $usql);
330 $this->assertEqual(1, count($params));
331 $this->assertEqual($in_value, $params['param01']);
332
333 // SQL_PARAMS_NAMED - NOT IN or <>
334
335 // Correct usage of multiple values
336 $in_values = array('value1', 'value2', 'value3', 'value4');
337 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
338 $this->assertEqual("NOT IN (:param01,:param02,:param03,:param04)", $usql);
339 $this->assertEqual(4, count($params));
340 reset($in_values);
341 foreach ($params as $key => $value) {
342 $this->assertEqual(current($in_values), $value);
343 next($in_values);
344 }
345
346 // Correct usage of single values (in array)
347 $in_values = array('value1');
348 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
349 $this->assertEqual("<> :param01", $usql);
350 $this->assertEqual(1, count($params));
351 $this->assertEqual($in_values[0], $params['param01']);
352
353 // Correct usage of single value
354 $in_value = 'value1';
355 list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
356 $this->assertEqual("<> :param01", $usql);
357 $this->assertEqual(1, count($params));
358 $this->assertEqual($in_value, $params['param01']);
359
360 }
361
362 public function test_fix_table_names() {
363 $DB = new moodle_database_for_testing();
364 $prefix = $DB->get_prefix();
365
366 // Simple placeholder
367 $placeholder = "{user}";
368 $this->assertEqual($prefix . "user", $DB->public_fix_table_names($placeholder));
369
370 // Full SQL
371 $sql = "SELECT * FROM {user}, {funny_table_name}, {mdl_stupid_table} WHERE {user}.id = {funny_table_name}.userid";
372 $expected = "SELECT * FROM {$prefix}user, {$prefix}funny_table_name, {$prefix}mdl_stupid_table WHERE {$prefix}user.id = {$prefix}funny_table_name.userid";
373 $this->assertEqual($expected, $DB->public_fix_table_names($sql));
374
b579f0db 375
376 }
377
378 public function test_get_recordset() {
379 $DB = $this->tdb;
380 $dbman = $DB->get_manager();
381
382 $table = new xmldb_table("testtable");
383 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
384 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
385 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '0');
386 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
387 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
388 $dbman->create_table($table);
389 $this->tables[$table->getName()] = $table;
390
391 $data = array(array('id' => 1, 'course' => 3, 'name' => 'record1'),
392 array('id' => 2, 'course' => 3, 'name' => 'record2'),
393 array('id' => 3, 'course' => 5, 'name' => 'record3'));
394 foreach ($data as $record) {
395 $DB->insert_record('testtable', $record);
396 }
397
398 $rs = $DB->get_recordset('testtable');
399 $this->assertTrue($rs);
400
401 reset($data);
402 foreach($rs as $record) {
403 $data_record = current($data);
404 foreach ($record as $k => $v) {
405 $this->assertEqual($data_record[$k], $v);
406 }
407 next($data);
408 }
409 $rs->close();
7f9f11b5 410 }
0088bd31 411
412 public function test_get_recordset_list() {
413 $DB = $this->tdb;
414 $dbman = $DB->get_manager();
415
416 $table = new xmldb_table("testtable");
417 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
418 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
419 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
420 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
421 $dbman->create_table($table);
422 $this->tables[$table->getName()] = $table;
423
424 $DB->insert_record('testtable', array('course' => 3));
425 $DB->insert_record('testtable', array('course' => 3));
426 $DB->insert_record('testtable', array('course' => 5));
427 $DB->insert_record('testtable', array('course' => 2));
428
429 $rs = $DB->get_recordset_list('testtable', 'course', array(3, 2));
430
431 $this->assertTrue($rs);
432
433 $counter = 0;
434 foreach ($rs as $record) {
435 $counter++;
436 }
437 $this->assertEqual(3, $counter);
438 }
439
440 public function test_get_recordset_select() {
441 $DB = $this->tdb;
442 $dbman = $DB->get_manager();
443
444 $table = new xmldb_table("testtable");
445 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
446 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
447 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
448 $dbman->create_table($table);
449 $this->tables[$table->getName()] = $table;
450
451 $DB->insert_record('testtable', array('course' => 3));
452 $DB->insert_record('testtable', array('course' => 3));
453 $DB->insert_record('testtable', array('course' => 5));
454 $DB->insert_record('testtable', array('course' => 2));
455
456 $this->assertTrue($rs = $DB->get_recordset_select('testtable', ''));
457 $counter = 0;
458 foreach ($rs as $record) {
459 $counter++;
460 }
461 $this->assertEqual(4, $counter);
462
463 $this->assertTrue($rs = $DB->get_recordset_select('testtable', 'course = 3'));
464 $counter = 0;
465 foreach ($rs as $record) {
466 $counter++;
467 }
468 $this->assertEqual(2, $counter);
469 }
470
471 public function test_get_recordset_sql() {
472 $DB = $this->tdb;
473 $dbman = $DB->get_manager();
474
475 $table = new xmldb_table("testtable");
476 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
477 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
478 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
479 $dbman->create_table($table);
480 $this->tables[$table->getName()] = $table;
481
482 $DB->insert_record('testtable', array('course' => 3));
483 $DB->insert_record('testtable', array('course' => 3));
484 $DB->insert_record('testtable', array('course' => 5));
485 $DB->insert_record('testtable', array('course' => 2));
486
487 $this->assertTrue($rs = $DB->get_recordset_sql('SELECT * FROM {testtable} WHERE course = ?', array(3)));
488 $counter = 0;
489 foreach ($rs as $record) {
490 $counter++;
491 }
492 $this->assertEqual(2, $counter);
493 }
494
495 public function test_get_records() {
496 $DB = $this->tdb;
497 $dbman = $DB->get_manager();
498
499 $table = new xmldb_table("testtable");
500 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
501 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
502 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
503 $dbman->create_table($table);
504 $this->tables[$table->getName()] = $table;
505
506 $DB->insert_record('testtable', array('course' => 3));
507 $DB->insert_record('testtable', array('course' => 3));
508 $DB->insert_record('testtable', array('course' => 5));
509 $DB->insert_record('testtable', array('course' => 2));
510
511 // All records
512 $records = $DB->get_records('testtable');
513 $this->assertEqual(4, count($records));
514 $this->assertEqual(3, $records[1]->course);
515 $this->assertEqual(3, $records[2]->course);
516 $this->assertEqual(5, $records[3]->course);
517 $this->assertEqual(2, $records[4]->course);
518
519 // Records matching certain conditions
520 $records = $DB->get_records('testtable', array('course' => 3));
521 $this->assertEqual(2, count($records));
522 $this->assertEqual(3, $records[1]->course);
523 $this->assertEqual(3, $records[2]->course);
524
525 // All records sorted by course
526 $records = $DB->get_records('testtable', null, 'course');
527 $this->assertEqual(4, count($records));
528 $current_record = reset($records);
529 $this->assertEqual(4, $current_record->id);
530 $current_record = next($records);
531 $this->assertEqual(1, $current_record->id);
532 $current_record = next($records);
533 $this->assertEqual(2, $current_record->id);
534 $current_record = next($records);
535 $this->assertEqual(3, $current_record->id);
536
537 // All records, but get only one field
538 $records = $DB->get_records('testtable', null, '', 'id');
539 $this->assertTrue(empty($records[1]->course));
540 $this->assertFalse(empty($records[1]->id));
541 $this->assertEqual(4, count($records));
542 }
543
544 public function test_get_records_list() {
545
546 }
547
548 public function test_get_records_sql() {
549
550 }
551
552 public function test_get_records_menu() {
553
554 }
555
556 public function test_get_records_select_menu() {
557
558 }
559
560 public function test_get_records_sql_menu() {
561
562 }
563
564 public function test_get_record() {
565
566 }
567
568 public function test_get_record_select() {
569
570 }
571
572 public function test_get_record_sql() {
573
574 }
575
576 public function test_get_field() {
577
578 }
579
580 public function test_get_field_select() {
581
582 }
583
584 public function test_get_field_sql() {
585
586 }
587
588 public function test_get_field_select() {
589
590 }
591
592 public function test_get_fieldset_sql() {
593
594 }
595
596 public function test_insert_record_raw() {
597
598 }
599
600 public function test_insert_record() {
601
602 }
603
604 public function test_update_record_raw() {
605
606 }
607
608 public function test_update_record() {
609
610 }
611
612 public function test_set_field() {
613
614 }
615
616 public function test_set_field_select() {
617
618 }
619
620 public function test_count_records() {
621 $DB = $this->tdb;
622
623 $dbman = $DB->get_manager();
624
625 $table = new xmldb_table("testtable");
626 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
627 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
628 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
629 $dbman->create_table($table);
630 $this->tables[$table->getName()] = $table;
631
632 $this->assertEqual(0, $DB->count_records('testtable'));
633
634 $DB->insert_record('testtable', array('course' => 3));
635 $DB->insert_record('testtable', array('course' => 4));
636 $DB->insert_record('testtable', array('course' => 5));
637
638 $this->assertEqual(3, $DB->count_records('testtable'));
639 }
640
641 public function test_count_records_select() {
642
643 }
644
645 public function test_count_records_sql() {
646
647 }
648
649 public function test_record_exists() {
650
651 }
652
653 public function test_record_exists_select() {
654
655 }
656
657 public function test_record_exists_sql() {
658
659 }
660
661 public function test_delete_records() {
662
663 }
664
665 public function test_delete_records_select() {
666
667 }
668
669 public function test_where_clause() {
670
671 }
672
7f9f11b5 673}
674
675/**
676 * This class is not a proper subclass of moodle_database. It is
677 * intended to be used only in unit tests, in order to gain access to the
678 * protected methods of moodle_database, and unit test them.
679 */
680class moodle_database_for_testing extends moodle_database {
681 protected $prefix = 'mdl_';
682
683 public function public_fix_table_names($sql) {
684 return $this->fix_table_names($sql);
685 }
686
687 public function driver_installed(){}
688 public function get_dbfamily(){}
689 protected function get_dbtype(){}
690 public function get_name(){}
691 public function get_configuration_hints(){}
692 public function export_dbconfig(){}
693 public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null){}
694 public function get_server_info(){}
695 protected function allowed_param_types(){}
696 public function get_last_error(){}
697 public function get_tables(){}
698 public function get_indexes($table){}
699 public function get_columns($table, $usecache=true){}
700 public function set_debug($state){}
701 public function get_debug(){}
702 public function set_logging($state){}
703 public function change_database_structure($sql){}
704 public function execute($sql, array $params=null){}
705 public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0){}
706 public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0){}
707 public function get_fieldset_sql($sql, array $params=null){}
708 public function insert_record_raw($table, $params, $returnid=true, $bulk=false){}
709 public function insert_record($table, $dataobject, $returnid=true, $bulk=false){}
710 public function update_record_raw($table, $params, $bulk=false){}
711 public function update_record($table, $dataobject, $bulk=false){}
712 public function set_field_select($table, $newfield, $newvalue, $select, array $params=null){}
713 public function delete_records_select($table, $select, array $params=null){}
714 public function sql_concat(){}
715 public function sql_concat_join($separator="' '", $elements=array()){}
716 public function sql_substr(){}
bb78c788 717}