~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to lib/simpletest/testdmllib.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 * @package moodlecore
9
9
 */
10
10
 
11
 
/** */
12
 
require_once(dirname(__FILE__) . '/../../config.php');
 
11
if (!defined('MOODLE_INTERNAL')) {
 
12
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 
13
}
13
14
 
14
 
global $CFG;
15
 
require_once($CFG->libdir . '/simpletestlib.php');
16
15
require_once($CFG->libdir . '/simpletestlib/web_tester.php');
17
16
require_once($CFG->libdir . '/dmllib.php');
18
17
 
24
23
            array(  2,    'toad',     102),
25
24
            array(  3, 'tadpole',     103),
26
25
            array(  4, 'tadpole',     104),
 
26
            array(  5, 'nothing',     NULL),
27
27
        );
28
28
    var $objects = array();
29
29
 
30
30
    function setUp() {
31
31
        global $CFG, $db;
32
32
        parent::setUp();
33
 
        wipe_tables($CFG->prefix, $db);
34
33
        load_test_table($CFG->prefix . $this->table, $this->data, $db);
35
34
        $keys = reset($this->data);
36
 
        foreach ($this->data as $datum) {
37
 
            if ($datum != $keys) {
38
 
               $this->objects[$datum[0]] = (object) array_combine($keys, $datum);
 
35
        foreach ($this->data as $row=>$datum) {
 
36
            if ($row == 0) {
 
37
                continue;
39
38
            }
 
39
            $this->objects[$datum[0]] = (object) array_combine($keys, $datum);
40
40
        }
41
41
    }
42
42
 
50
50
        $this->assertEqual(where_clause('f1', 'v1'), "WHERE f1 = 'v1'");
51
51
        $this->assertEqual(where_clause('f1', 'v1', 'f2', 2), "WHERE f1 = 'v1' AND f2 = '2'");
52
52
        $this->assertEqual(where_clause('f1', 'v1', 'f2', 1.75, 'f3', 'v3'), "WHERE f1 = 'v1' AND f2 = '1.75' AND f3 = 'v3'");
 
53
        $this->assertEqual(where_clause('f1', NULL), "WHERE f1 IS NULL");
53
54
    }
54
 
    
 
55
 
55
56
    function test_record_exists() {
56
57
        $this->assertTrue(record_exists($this->table, 'numberfield', 101, 'id', 1));
57
58
        $this->assertFalse(record_exists($this->table, 'numberfield', 102, 'id', 1));
 
59
        $this->assertTrue(record_exists($this->table, 'numberfield', NULL));
58
60
    }
59
61
 
60
62
    function test_record_exists_select() {
61
63
        $this->assertTrue(record_exists_select($this->table, 'numberfield = 101 AND id = 1'));
62
64
        $this->assertFalse(record_exists_select($this->table, 'numberfield = 102 AND id = 1'));
 
65
        $this->assertTrue(record_exists_select($this->table, 'numberfield IS NULL'));
63
66
    }
64
67
 
65
68
    function test_record_exists_sql() {
66
69
        global $CFG;
67
70
        $this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 101 AND id = 1"));
68
71
        $this->assertFalse(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 102 AND id = 1"));
 
72
        $this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield IS NULL"));
69
73
    }
70
74
 
71
75
 
72
76
    function test_get_record() {
73
77
        // Get particular records.
74
 
        $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[1]), get_record($this->table, 'id', 1), 'id = 1');
75
 
        $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[3]), get_record($this->table, 'textfield', 'tadpole', 'numberfield', 103), 'textfield = tadpole AND numberfield = 103');
 
78
        $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[1]), get_record($this->table, 'id', 1));
 
79
        $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[3]), get_record($this->table, 'textfield', 'tadpole', 'numberfield', 103));
 
80
        $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[5]), get_record($this->table, 'numberfield', null));
76
81
 
77
82
        // Abiguous get attempt, should return one, and print a warning in debug mode.
78
83
        global $CFG;
85
90
        ob_end_clean();
86
91
        $this->assertEqual('', $result, '%s (No error ouside debug mode).');
87
92
 
88
 
        $CFG->debug = E_ALL;
 
93
        $CFG->debug = DEBUG_DEVELOPER;
89
94
        ob_start();
90
95
        $record = get_record($this->table, 'textfield', 'tadpole');
91
96
        $result = ob_get_contents();
127
132
        ob_end_clean();
128
133
        $this->assertEqual('', $result, '%s (No error ouside debug mode).');
129
134
 
130
 
        $CFG->debug = E_ALL;
 
135
        $CFG->debug = DEBUG_DEVELOPER;
131
136
        ob_start();
132
137
        $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE textfield = 'tadpole'");
133
138
        $result = ob_get_contents();
161
166
        ob_end_clean();
162
167
        $this->assertEqual('', $result, '%s (No error ouside debug mode).');
163
168
 
164
 
        $CFG->debug = E_ALL;
 
169
        $CFG->debug = DEBUG_DEVELOPER;
165
170
        ob_start();
166
171
        $record = get_record_select($this->table, "textfield = 'tadpole'");
167
172
        $result = ob_get_contents();
187
192
        $this->assertEqual(get_field($this->table, 'textfield', 'numberfield', 102), 'toad');
188
193
        $this->assertEqual(get_field($this->table, 'numberfield', 'textfield', 'tadpole', 'id', 4), 104);
189
194
        $this->assertEqual(get_field($this->table, 'numberfield + id', 'textfield', 'tadpole', 'id', 4), 108);
 
195
        $this->assertNull(get_field($this->table, 'numberfield', 'id', 5));
190
196
    }
191
197
 
192
198
    function test_get_field_select() {
207
213
 
208
214
        set_field($this->table, 'numberfield', -1, 'textfield', 'tadpole', 'id', 4);
209
215
        $this->assertEqual(get_field($this->table, 'numberfield', 'textfield', 'tadpole', 'id', 4), -1);
 
216
 
 
217
        set_field($this->table, 'textfield', null, 'id', 5);
 
218
        $this->assertNull(get_field($this->table, 'textfield', 'id', 5));
210
219
    }
211
220
 
212
221
    function test_delete_records() {
213
222
        delete_records($this->table, 'id', 666);
214
 
        $this->assertEqual(count_records($this->table), 4);
 
223
        $this->assertEqual(count_records($this->table), 5);
215
224
        delete_records($this->table, 'id', 1);
216
 
        $this->assertEqual(count_records($this->table), 3);
 
225
        $this->assertEqual(count_records($this->table), 4);
217
226
        delete_records($this->table, 'textfield', 'tadpole');
 
227
        $this->assertEqual(count_records($this->table), 2);
 
228
        delete_records($this->table, 'numberfield', NULL);
218
229
        $this->assertEqual(count_records($this->table), 1);
219
230
    }
220
231
 
221
232
    function test_delete_records2() {
222
233
        delete_records($this->table, 'textfield', 'tadpole', 'id', 4);
223
 
        $this->assertEqual(count_records($this->table), 3);
 
234
        $this->assertEqual(count_records($this->table), 4);
224
235
        delete_records($this->table);
225
236
        $this->assertEqual(count_records($this->table), 0);
226
237
    }
227
238
 
228
239
    function test_delete_records_select() {
229
240
        delete_records_select($this->table, "textfield LIKE 't%'");
230
 
        $this->assertEqual(count_records($this->table), 1);
 
241
        $this->assertEqual(count_records($this->table), 2);
231
242
        delete_records_select($this->table, "'1' = '1'");
232
243
        $this->assertEqual(count_records($this->table), 0);
233
244
    }
234
245
 
 
246
    function test_update_record() {
 
247
        global $CFG;
 
248
 
 
249
        // Simple update
 
250
        $obj = new stdClass;
 
251
        $obj->id = 1;
 
252
        $obj->textfield = 'changed entry';
 
253
        $obj->numberfield = 123;
 
254
        $this->assertTrue(update_record($this->table, $obj));
 
255
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple update (%s)'), get_record($this->table, 'id', $obj->id));
 
256
 
 
257
        // Simple incomplete update
 
258
        $obj = new stdClass;
 
259
        $obj->id = 2;
 
260
        $obj->numberfield = 123;
 
261
        $this->assertTrue(update_record($this->table, $obj));
 
262
        $obj->textfield = 'toad';
 
263
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple update (%s)'), get_record($this->table, 'id', $obj->id));
 
264
 
 
265
        // Simple incomplete update
 
266
        $obj = new stdClass;
 
267
        $obj->id = 3;
 
268
        $obj->numberfield = 123;
 
269
        $obj->textfield = null;
 
270
        $this->assertTrue(update_record($this->table, $obj));
 
271
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple update (%s)'), get_record($this->table, 'id', $obj->id));
 
272
 
 
273
    }
 
274
 
235
275
//function insert_record($table, $dataobject, $returnid=true, $primarykey='id', $feedback=true) {
236
276
    function test_insert_record() {
 
277
        global $CFG;
 
278
 
237
279
        // Simple insert with $returnid
238
280
        $obj = new stdClass;
239
281
        $obj->textfield = 'new entry';
240
282
        $obj->numberfield = 123;
241
 
        $this->assertEqual(insert_record($this->table, $obj), 5);
242
 
        $obj->id = 5;
243
 
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table, 'id', 5));
244
 
        
 
283
        $this->assertEqual(insert_record($this->table, $obj), 6);
 
284
        $obj->id = 6;
 
285
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table, 'id', $obj->id));
 
286
 
245
287
        // Simple insert without $returnid
246
288
        $obj = new stdClass;
247
289
        $obj->textfield = 'newer entry';
248
290
        $obj->numberfield = 321;
249
291
        $this->assertEqual(insert_record($this->table, $obj, false), true);
250
 
        $obj->id = 6;
251
 
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table, 'id', 6));
252
 
        
 
292
        $obj->id = 7;
 
293
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table, 'id', $obj->id));
 
294
 
253
295
        // Insert with missing columns - should get defaults.
254
296
        $obj = new stdClass;
255
297
        $obj->textfield = 'partial entry';
256
 
        $this->assertEqual(insert_record($this->table, $obj), 7);
257
 
        $obj->id = 7;
 
298
        $this->assertEqual(insert_record($this->table, $obj), 8);
 
299
        $obj->id = 8;
258
300
        $obj->numberfield = 0xDefa;
259
 
        $got = get_record($this->table, 'id', 7);
260
 
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table, 'id', 7));
261
 
        
 
301
        $got = get_record($this->table, 'id', 8);
 
302
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table, 'id', $obj->id));
 
303
 
262
304
        // Insert with extra columns - should be ingnored.
263
305
        $obj = new stdClass;
264
306
        $obj->textfield = 'entry with extra';
265
307
        $obj->numberfield = 747;
266
308
        $obj->unused = 666;
267
 
        $this->assertEqual(insert_record($this->table, $obj), 8);
268
 
        $obj->id = 8;
 
309
        $this->assertEqual(insert_record($this->table, $obj), 9);
 
310
        $obj->id = 9;
269
311
        unset($obj->unused);
270
 
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with extra columns - should be ingnored (%s)'), get_record($this->table, 'id', 8));
271
 
        
 
312
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with extra columns - should be ingnored (%s)'), get_record($this->table, 'id', $obj->id));
 
313
 
 
314
        // Simple insert with $returnid and NULL values
 
315
        $obj = new stdClass;
 
316
        $obj->textfield = null;
 
317
        $obj->numberfield = null;
 
318
        $this->assertEqual(insert_record($this->table, $obj), 10);
 
319
        $obj->id = 10;
 
320
        $new = get_record($this->table, 'id', $obj->id);
 
321
        $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), $new);
 
322
        $this->assertNull($new->textfield);
 
323
        $this->assertNull($new->numberfield);
 
324
 
272
325
        // Insert into nonexistant table - should fail.
273
326
        $obj = new stdClass;
274
327
        $obj->textfield = 'new entry';
275
328
        $obj->numberfield = 123;
276
329
        $this->assertFalse(insert_record('nonexistant_table', $obj), 'Insert into nonexistant table');
277
 
        
278
 
        // Insert bad data - error should be printed.
279
 
        $obj = new stdClass;
280
 
        $obj->textfield = 'new entry';
281
 
        $obj->numberfield = 'not a number';
282
 
        ob_start();
283
 
        $this->assertFalse(insert_record($this->table, $obj), 'Insert bad data - should fail.');
284
 
        $result = ob_get_contents();
285
 
        ob_end_clean();
286
 
        $this->assert(new TextExpectation('ERROR:'), $result, 'Insert bad data - error should have been printed. This is known not to work on MySQL.');
 
330
 
287
331
    }
288
332
}
289
333