3 * Unit tests for (some of) ../datalib.php.
5 * @copyright © 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 if (!defined('MOODLE_INTERNAL')) {
12 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
15 require_once($CFG->libdir
. '/simpletestlib/web_tester.php');
16 require_once($CFG->libdir
. '/dmllib.php');
18 class datalib_test
extends prefix_changing_test_case
{
21 array('id', 'textfield', 'numberfield'),
22 array( 1, 'frog', 101),
23 array( 2, 'toad', 102),
24 array( 3, 'tadpole', 103),
25 array( 4, 'tadpole', 104),
27 var $objects = array();
32 wipe_tables($CFG->prefix
, $db);
33 load_test_table($CFG->prefix
. $this->table
, $this->data
, $db);
34 $keys = reset($this->data
);
35 foreach ($this->data
as $datum) {
36 if ($datum != $keys) {
37 $this->objects
[$datum[0]] = (object) array_combine($keys, $datum);
44 remove_test_table($CFG->prefix
. $this->table
, $db);
48 function test_where_clause() {
49 $this->assertEqual(where_clause('f1', 'v1'), "WHERE f1 = 'v1'");
50 $this->assertEqual(where_clause('f1', 'v1', 'f2', 2), "WHERE f1 = 'v1' AND f2 = '2'");
51 $this->assertEqual(where_clause('f1', 'v1', 'f2', 1.75, 'f3', 'v3'), "WHERE f1 = 'v1' AND f2 = '1.75' AND f3 = 'v3'");
54 function test_record_exists() {
55 $this->assertTrue(record_exists($this->table
, 'numberfield', 101, 'id', 1));
56 $this->assertFalse(record_exists($this->table
, 'numberfield', 102, 'id', 1));
59 function test_record_exists_select() {
60 $this->assertTrue(record_exists_select($this->table
, 'numberfield = 101 AND id = 1'));
61 $this->assertFalse(record_exists_select($this->table
, 'numberfield = 102 AND id = 1'));
64 function test_record_exists_sql() {
66 $this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 101 AND id = 1"));
67 $this->assertFalse(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 102 AND id = 1"));
71 function test_get_record() {
72 // Get particular records.
73 $this->assert(new CheckSpecifiedFieldsExpectation($this->objects
[1]), get_record($this->table
, 'id', 1), 'id = 1');
74 $this->assert(new CheckSpecifiedFieldsExpectation($this->objects
[3]), get_record($this->table
, 'textfield', 'tadpole', 'numberfield', 103), 'textfield = tadpole AND numberfield = 103');
76 // Abiguous get attempt, should return one, and print a warning in debug mode.
78 $old_debug = $CFG->debug
;
82 $record = get_record($this->table
, 'textfield', 'tadpole');
83 $result = ob_get_contents();
85 $this->assertEqual('', $result, '%s (No error ouside debug mode).');
89 $record = get_record($this->table
, 'textfield', 'tadpole');
90 $result = ob_get_contents();
92 $this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.');
94 $CFG->debug
= $old_debug;
96 // Return only specified fields
97 $expected = new stdClass
;
99 $expected->textfield
= 'tadpole';
100 $result = get_record($this->table
, 'id', '3', '', '', '', '', 'id,textfield');
101 $this->assert(new CheckSpecifiedFieldsExpectation($expected), $result);
102 $this->assertFalse(isset($result->numberfield
));
103 $expected = new stdClass
;
104 $expected->textfield
= 'tadpole';
105 $expected->numberfield
= 103;
106 $result = get_record($this->table
, 'id', '3', '', '', '', '', 'textfield,numberfield');
107 $this->assert(new CheckSpecifiedFieldsExpectation($expected), $result);
108 $this->assertFalse(isset($result->id
));
110 // Attempting to get a non-existant records should return false.
111 $this->assertFalse(get_record($this->table
, 'textfield', 'not there'), 'attempt to get non-existant record');
114 function test_get_record_sql() {
116 // Get particular records.
117 $this->assert(new CheckSpecifiedFieldsExpectation($this->objects
[1]), get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table
. " WHERE id = '1'", 'id = 1'));
119 // Abiguous get attempt, should return one, and print a warning in debug mode, unless $expectmultiple is used.
120 $old_debug = $CFG->debug
;
124 $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table
. " WHERE textfield = 'tadpole'");
125 $result = ob_get_contents();
127 $this->assertEqual('', $result, '%s (No error ouside debug mode).');
131 $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table
. " WHERE textfield = 'tadpole'");
132 $result = ob_get_contents();
134 $this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.');
137 $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table
. " WHERE textfield = 'tadpole'", true);
138 $result = ob_get_contents();
140 $this->assertEqual('', $result, '%s (No error ouside debug mode).');
142 $CFG->debug
= $old_debug;
144 // Attempting to get a non-existant records should return false.
145 $this->assertFalse(get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table
. " WHERE textfield = 'not there'"), 'attempt to get non-existant record');
148 function test_get_record_select() {
149 // Get particular records.
150 $this->assert(new CheckSpecifiedFieldsExpectation($this->objects
[2]), get_record_select($this->table
, 'id > 1 AND id < 3'), 'id > 1 AND id < 3');
152 // Abiguous get attempt, should return one, and print a warning in debug mode.
154 $old_debug = $CFG->debug
;
158 $record = get_record_select($this->table
, "textfield = 'tadpole'");
159 $result = ob_get_contents();
161 $this->assertEqual('', $result, '%s (No error ouside debug mode).');
165 $record = get_record_select($this->table
, "textfield = 'tadpole'");
166 $result = ob_get_contents();
168 $this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.');
170 $CFG->debug
= $old_debug;
172 // Return only specified fields
173 $expected = new stdClass
;
175 $expected->textfield
= 'frog';
176 $result = get_record_select($this->table
, "textfield = 'frog'", 'id,textfield');
177 $this->assert(new CheckSpecifiedFieldsExpectation($expected), $result);
178 $this->assertFalse(isset($result->numberfield
));
180 // Attempting to get a non-existant records should return false.
181 $this->assertFalse(get_record_select($this->table
, 'id > 666'), 'attempt to get non-existant record');
184 function test_get_field() {
185 $this->assertEqual(get_field($this->table
, 'numberfield', 'id', 1), 101);
186 $this->assertEqual(get_field($this->table
, 'textfield', 'numberfield', 102), 'toad');
187 $this->assertEqual(get_field($this->table
, 'numberfield', 'textfield', 'tadpole', 'id', 4), 104);
188 $this->assertEqual(get_field($this->table
, 'numberfield + id', 'textfield', 'tadpole', 'id', 4), 108);
191 function test_get_field_select() {
192 $this->assertEqual(get_field_select($this->table
, 'numberfield', 'id = 1'), 101);
195 function test_get_field_sql() {
197 $this->assertEqual(get_field_sql("SELECT numberfield FROM {$CFG->prefix}$this->table WHERE id = 1"), 101);
200 function test_set_field() {
201 set_field($this->table
, 'numberfield', 12345, 'id', 1);
202 $this->assertEqual(get_field($this->table
, 'numberfield', 'id', 1), 12345);
204 set_field($this->table
, 'textfield', 'newvalue', 'numberfield', 102);
205 $this->assertEqual(get_field($this->table
, 'textfield', 'numberfield', 102), 'newvalue');
207 set_field($this->table
, 'numberfield', -1, 'textfield', 'tadpole', 'id', 4);
208 $this->assertEqual(get_field($this->table
, 'numberfield', 'textfield', 'tadpole', 'id', 4), -1);
211 function test_delete_records() {
212 delete_records($this->table
, 'id', 666);
213 $this->assertEqual(count_records($this->table
), 4);
214 delete_records($this->table
, 'id', 1);
215 $this->assertEqual(count_records($this->table
), 3);
216 delete_records($this->table
, 'textfield', 'tadpole');
217 $this->assertEqual(count_records($this->table
), 1);
220 function test_delete_records2() {
221 delete_records($this->table
, 'textfield', 'tadpole', 'id', 4);
222 $this->assertEqual(count_records($this->table
), 3);
223 delete_records($this->table
);
224 $this->assertEqual(count_records($this->table
), 0);
227 function test_delete_records_select() {
228 delete_records_select($this->table
, "textfield LIKE 't%'");
229 $this->assertEqual(count_records($this->table
), 1);
230 delete_records_select($this->table
, "'1' = '1'");
231 $this->assertEqual(count_records($this->table
), 0);
234 //function insert_record($table, $dataobject, $returnid=true, $primarykey='id', $feedback=true) {
235 function test_insert_record() {
238 // Simple insert with $returnid
240 $obj->textfield
= 'new entry';
241 $obj->numberfield
= 123;
242 $this->assertEqual(insert_record($this->table
, $obj), 5);
244 $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table
, 'id', 5));
246 // Simple insert without $returnid
248 $obj->textfield
= 'newer entry';
249 $obj->numberfield
= 321;
250 $this->assertEqual(insert_record($this->table
, $obj, false), true);
252 $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table
, 'id', 6));
254 // Insert with missing columns - should get defaults.
256 $obj->textfield
= 'partial entry';
257 $this->assertEqual(insert_record($this->table
, $obj), 7);
259 $obj->numberfield
= 0xDefa;
260 $got = get_record($this->table
, 'id', 7);
261 $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table
, 'id', 7));
263 // Insert with extra columns - should be ingnored.
265 $obj->textfield
= 'entry with extra';
266 $obj->numberfield
= 747;
268 $this->assertEqual(insert_record($this->table
, $obj), 8);
271 $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with extra columns - should be ingnored (%s)'), get_record($this->table
, 'id', 8));
273 // Insert into nonexistant table - should fail.
275 $obj->textfield
= 'new entry';
276 $obj->numberfield
= 123;
277 $this->assertFalse(insert_record('nonexistant_table', $obj), 'Insert into nonexistant table');
279 // Insert bad data - error should be printed - mysql not tested
280 if ($CFG->dbfamily
!= 'mysql') {
282 $obj->textfield
= 'new entry';
283 $obj->numberfield
= 'not a number';
285 $this->assertFalse(insert_record($this->table
, $obj), 'Insert bad data - should fail.');
286 $result = ob_get_contents();
288 $this->assert(new TextExpectation('ERROR:'), $result, 'Insert bad data - error should have been printed. This is known not to work on MySQL.');