Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / xmldb / actions / test / test.class.php
blob6369e15de8cdc057c7013577ebd55d435dca8970
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will perform one full test of all the available DDL
28 /// functions under your DB
30 class test extends XMLDBAction {
32 /**
33 * Init method, every subclass will have its own
35 function init() {
36 parent::init();
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'back' => 'xmldb'
43 ));
46 /**
47 * Invoke method, every class will have its own
48 * returns true/false on completion, setting both
49 * errormsg and output as necessary
51 function invoke() {
52 parent::invoke();
54 $result = true;
56 /// Set own core attributes
57 //$this->does_generate = ACTION_NONE;
58 $this->does_generate = ACTION_GENERATE_HTML;
60 /// These are always here
61 global $CFG, $XMLDB, $db;
63 /// ADD YOUR CODE HERE
64 require_once ($CFG->libdir . '/ddllib.php');
66 /// Where all the tests will be stored
67 $tests = array();
69 /// The back to edit table button
70 $b = ' <p class="centerpara buttons">';
71 $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
72 $b .= '</p>';
73 $o = $b;
75 /// Silenty drop any previous test tables
76 $table = new XMLDBTable('testtable');
77 if (table_exists($table)) {
78 $status = drop_table($table, true, false);
80 $table = new XMLDBTable ('anothertest');
81 if (table_exists($table)) {
82 $status = drop_table($table, true, false);
84 $table = new XMLDBTable ('newnameforthetable');
85 if (table_exists($table)) {
86 $status = drop_table($table, true, false);
89 /// 1st test. Complete table creation.
90 $table = new XMLDBTable('testtable');
91 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
92 $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
93 $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
94 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, '');
95 $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, '');
96 $table->addFieldInfo('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
97 $table->addFieldInfo('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
98 $table->addFieldInfo('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
99 $table->addFieldInfo('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
100 $table->addFieldInfo('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
101 $table->addFieldInfo('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
102 $table->addFieldInfo('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
103 $table->addFieldInfo('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
104 $table->addFieldInfo('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
105 $table->addFieldInfo('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
106 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
107 $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
108 $table->addFieldInfo('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
109 $table->addFieldInfo('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
110 $table->addFieldInfo('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
111 $table->addFieldInfo('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
112 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
113 $table->addKeyInfo('type-name', XMLDB_KEY_UNIQUE, array('type', 'name'));
114 $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
115 $table->addIndexInfo('rsstype', XMLDB_INDEX_UNIQUE, array('rsstype'));
117 $table->setComment("This is a test'n drop table. You can drop it safely");
119 /// Get SQL code and execute it
120 $test = new stdClass;
121 $test->sql = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, true);
122 $test->status = create_table($table, false, false);
123 if (!$test->status) {
124 $test->error = $db->ErrorMsg();
126 $tests['create table'] = $test;
128 /// 2nd test. drop table
129 if ($test->status) {
130 /// Get SQL code and execute it
131 $test = new stdClass;
132 $test->sql = $table->getDropTableSQL($CFG->dbtype, $CFG->prefix, true);
133 $test->status = drop_table($table, false, false);
134 if (!$test->status) {
135 $test->error = $db->ErrorMsg();
137 $tests['drop table'] = $test;
140 /// 3rd test. creating another, smaller table
141 if ($test->status) {
142 $table = new XMLDBTable ('anothertest');
143 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
144 $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
145 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
146 $table->addFieldInfo('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
147 $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
148 $table->addFieldInfo('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null, null, null);
149 $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null, null, null);
150 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
151 /// Get SQL code and execute it
152 $test = new stdClass;
153 $test->sql = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, true);
154 $test->status = create_table($table, false, false);
155 if (!$test->status) {
156 $test->error = $db->ErrorMsg();
158 $tests['create table - 2'] = $test;
161 /// Insert two records to do the work with real data
162 $rec->course = 1;
163 $rec->name = 'Martin';
164 $rec->secondname = 'Dougiamas';
165 $rec->intro = 'The creator of Moodle';
166 $rec->grade = 10.0001;
167 insert_record('anothertest', $rec);
168 $rec->course = 2;
169 $rec->name = 'Eloy';
170 $rec->secondname = 'Lafuente';
171 $rec->intro = 'One poor developer';
172 $rec->grade = 9.99;
173 insert_record('anothertest', $rec);
175 /// 4th test. Adding one complex enum field
176 if ($test->status) {
177 /// Create a new field with complex specs (enums are good candidates)
178 $field = new XMLDBField('type');
179 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
180 /// Get SQL code and execute it
181 $test = new stdClass;
182 $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
183 $test->status = add_field($table, $field, false, false);
184 if (!$test->status) {
185 $test->error = $db->ErrorMsg();
187 $tests['add enum field'] = $test;
190 /// 5th test. Dropping one complex enum field
191 if ($test->status) {
192 /// Create a new field with complex specs (enums are good candidates)
193 $test = new stdClass;
194 $test->sql = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
195 $test->status = drop_field($table, $field, false, false);
196 if (!$test->status) {
197 $test->error = $db->ErrorMsg();
199 $tests['drop enum field'] = $test;
202 /// 6th test. Adding one complex enum field
203 if ($test->status) {
204 /// Create a new field with complex specs (enums are good candidates)
205 $field = new XMLDBField('type');
206 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
207 /// Get SQL code and execute it
208 $test = new stdClass;
209 $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
210 $test->status = add_field($table, $field, false, false);
211 if (!$test->status) {
212 $test->error = $db->ErrorMsg();
214 $tests['add enum field again'] = $test;
217 /// 7th test. Adding one numeric field
218 if ($test->status) {
219 /// Create a new field (numeric)
220 $field = new XMLDBField('onenumber');
221 $field->setAttributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'type');
222 /// Get SQL code and execute it
223 $test = new stdClass;
224 $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
225 $test->status = add_field($table, $field, false, false);
226 if (!$test->status) {
227 $test->error = $db->ErrorMsg();
229 $tests['add numeric field'] = $test;
232 /// 8th test. Dropping one complex enum field
233 if ($test->status) {
234 /// Create a new field with complex specs (enums are good candidates)
235 $field = new XMLDBField('type');
236 $test = new stdClass;
237 $test->sql = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
238 $test->status = drop_field($table, $field, false, false);
239 if (!$test->status) {
240 $test->error = $db->ErrorMsg();
242 $tests['drop enum field again'] = $test;
245 /// 9th test. Change the type of one column from integer to varchar
246 if ($test->status) {
247 /// Get SQL code and execute it
248 $test = new stdClass;
249 $field = new XMLDBField('course');
250 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, '0');
252 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
253 $test->status = change_field_type($table, $field, false, false);
254 if (!$test->status) {
255 $test->error = $db->ErrorMsg();
257 $tests['change field type (int2char)'] = $test;
260 /// 10th test. Change the type of one column from varchar to integer
261 if ($test->status) {
262 /// Get SQL code and execute it
263 $test = new stdClass;
264 $field = new XMLDBField('course');
265 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
267 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
268 $test->status = change_field_type($table, $field, false, false);
269 if (!$test->status) {
270 $test->error = $db->ErrorMsg();
272 $tests['change field type (char2int)'] = $test;
275 /// 11th test. Change the type of one column from number to varchar
276 if ($test->status) {
277 /// Get SQL code and execute it
278 $test = new stdClass;
279 $field = new XMLDBField('grade');
280 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, "test'n drop");
282 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
283 $test->status = change_field_type($table, $field, false, false);
284 if (!$test->status) {
285 $test->error = $db->ErrorMsg();
287 $tests['change field type (number2char)'] = $test;
290 /// 12th test. Change the type of one column from varchar to float
291 if ($test->status) {
292 /// Get SQL code and execute it
293 $test = new stdClass;
294 $field = new XMLDBField('grade');
295 $field->setAttributes(XMLDB_TYPE_FLOAT, '20,10', XMLDB_UNSIGNED, null, null, null, null, null);
297 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
298 $test->status = change_field_type($table, $field, false, false);
299 if (!$test->status) {
300 $test->error = $db->ErrorMsg();
302 $tests['change field type (char2float)'] = $test;
305 /// 13th test. Change the type of one column from float to char
306 if ($test->status) {
307 /// Get SQL code and execute it
308 $test = new stdClass;
309 $field = new XMLDBField('grade');
310 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'test');
312 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
313 $test->status = change_field_type($table, $field, false, false);
314 if (!$test->status) {
315 $test->error = $db->ErrorMsg();
317 $tests['change field type (float2char)'] = $test;
320 /// 14th test. Change the type of one column from char to number
321 if ($test->status) {
322 /// Get SQL code and execute it
323 $test = new stdClass;
324 $field = new XMLDBField('grade');
325 $field->setAttributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null, null, null);
327 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
328 $test->status = change_field_type($table, $field, false, false);
329 if (!$test->status) {
330 $test->error = $db->ErrorMsg();
332 $tests['change field type (char2number)'] = $test;
336 /// 15th test. Change the precision of one text field
337 if ($test->status) {
338 /// Get SQL code and execute it
339 $test = new stdClass;
340 $field = new XMLDBField('intro');
341 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
343 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
344 $test->status = change_field_precision($table, $field, false, false);
345 if (!$test->status) {
346 $test->error = $db->ErrorMsg();
348 $tests['change field precision (text)'] = $test;
351 /// 16th test. Change the precision of one char field
352 if ($test->status) {
353 /// Get SQL code and execute it
354 $test = new stdClass;
355 $field = new XMLDBField('secondname');
356 $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null);
358 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
359 $test->status = change_field_precision($table, $field, false, false);
360 if (!$test->status) {
361 $test->error = $db->ErrorMsg();
363 $tests['change field precision (char)'] = $test;
366 /// 17th test. Change the precision of one numeric field
367 if ($test->status) {
368 /// Get SQL code and execute it
369 $test = new stdClass;
370 $field = new XMLDBField('grade');
371 $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null);
373 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
374 $test->status = change_field_precision($table, $field, false, false);
375 if (!$test->status) {
376 $test->error = $db->ErrorMsg();
378 $tests['change field precision (number)'] = $test;
381 /// 18th test. Change the sign of one numeric field to unsigned
382 if ($test->status) {
383 /// Get SQL code and execute it
384 $test = new stdClass;
385 $field = new XMLDBField('grade');
386 $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', XMLDB_UNSIGNED, null, null, null, null, null);
388 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
389 $test->status = change_field_unsigned($table, $field, false, false);
390 if (!$test->status) {
391 $test->error = $db->ErrorMsg();
393 $tests['change field sign (unsigned)'] = $test;
396 /// 19th test. Change the sign of one numeric field to signed
397 if ($test->status) {
398 /// Get SQL code and execute it
399 $test = new stdClass;
400 $field = new XMLDBField('grade');
401 $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null);
403 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
404 $test->status = change_field_unsigned($table, $field, false, false);
405 if (!$test->status) {
406 $test->error = $db->ErrorMsg();
408 $tests['change field sign (signed)'] = $test;
411 /// 20th test. Change the nullability of one char field to not null
412 if ($test->status) {
413 /// Get SQL code and execute it
414 $test = new stdClass;
415 $field = new XMLDBField('name');
416 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'Moodle');
418 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
419 $test->status = change_field_notnull($table, $field, false, false);
420 if (!$test->status) {
421 $test->error = $db->ErrorMsg();
423 $tests['change field nullability (not null)'] = $test;
426 /// 21th test. Change the nullability of one char field to null
427 if ($test->status) {
428 /// Get SQL code and execute it
429 $test = new stdClass;
430 $field = new XMLDBField('name');
431 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
433 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
434 $test->status = change_field_notnull($table, $field, false, false);
435 if (!$test->status) {
436 $test->error = $db->ErrorMsg();
438 $tests['change field nullability (null)'] = $test;
441 /// 22th test. Dropping the default of one field
442 if ($test->status) {
443 /// Get SQL code and execute it
444 $test = new stdClass;
445 $field = new XMLDBField('name');
446 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
448 $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
449 $test->status = change_field_default($table, $field, false, false);
450 if (!$test->status) {
451 $test->error = $db->ErrorMsg();
453 $tests['drop field default of NULL field'] = $test;
456 /// 23th test. Creating the default for one field
457 if ($test->status) {
458 /// Get SQL code and execute it
459 $test = new stdClass;
460 $field = new XMLDBField('name');
461 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
463 $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
464 $test->status = change_field_default($table, $field, false, false);
465 if (!$test->status) {
466 $test->error = $db->ErrorMsg();
468 $tests['add field default of NULL field'] = $test;
471 /// 24th test. Creating the default for one field
472 if ($test->status) {
473 /// Get SQL code and execute it
474 $test = new stdClass;
475 $field = new XMLDBField('secondname');
476 $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'Moodle2');
478 $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
479 $test->status = change_field_default($table, $field, false, false);
480 if (!$test->status) {
481 $test->error = $db->ErrorMsg();
483 $tests['add field default of NOT NULL field'] = $test;
487 /// 25th test. Dropping the default of one NOT NULL field
488 if ($test->status) {
489 /// Get SQL code and execute it
490 $test = new stdClass;
491 $field = new XMLDBField('secondname');
492 $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null);
494 $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
495 $test->status = change_field_default($table, $field, false, false);
496 if (!$test->status) {
497 $test->error = $db->ErrorMsg();
499 $tests['drop field default of NOT NULL field'] = $test;
502 /// 26th test. Adding one unique index to the table
503 if ($test->status) {
504 /// Get SQL code and execute it
505 $test = new stdClass;
506 $index = new XMLDBIndex('secondname');
507 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'secondname', 'grade'));
509 $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
510 $test->status = add_index($table, $index, false, false);
511 if (!$test->status) {
512 $test->error = $db->ErrorMsg();
514 $tests['add unique index'] = $test;
517 /// 27th test. Adding one not unique index to the table
518 if ($test->status) {
519 /// Get SQL code and execute it
520 $test = new stdClass;
521 $index = new XMLDBIndex('secondname');
522 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
524 $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
525 $test->status = add_index($table, $index, false, false);
526 if (!$test->status) {
527 $test->error = $db->ErrorMsg();
529 $tests['add not unique index'] = $test;
532 /// 28th test. Re-add the same index than previous test. Check find_index_name() works.
533 if ($test->status) {
534 /// Get SQL code and execute it
535 $test = new stdClass;
536 $index = new XMLDBIndex('secondname');
537 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name', 'course'));
539 if ($indexfound = find_index_name($table, $index)) {
540 $test->status = true;
541 $test->sql = array();
542 } else {
543 $test->status = true;
544 $test->error = 'Index not found!';
545 $test->sql = array();
548 $tests['check find_index_name()'] = $test;
551 /// 29th test. Dropping one index from the table
552 if ($test->status) {
553 /// Get SQL code and execute it
554 $test = new stdClass;
555 $index = new XMLDBIndex('name');
556 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'grade', 'secondname'));
558 $test->sql = $table->getDropIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
559 $test->status = drop_index($table, $index, false, false);
560 if (!$test->status) {
561 $test->error = $db->ErrorMsg();
563 $tests['drop index'] = $test;
566 /// 30th test. Adding one unique key to the table
567 if ($test->status) {
568 /// Get SQL code and execute it
569 $test = new stdClass;
570 $key = new XMLDBKey('id-course-grade');
571 $key->setAttributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
573 $test->sql = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
574 $test->status = add_key($table, $key, false, false);
575 if (!$test->status) {
576 $test->error = $db->ErrorMsg();
578 $tests['add unique key'] = $test;
581 /// 31th test. Adding one foreign+unique key to the table
582 if ($test->status) {
583 /// Get SQL code and execute it
584 $test = new stdClass;
585 $key = new XMLDBKey('course');
586 $key->setAttributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'anothertest', array('id'));
588 $test->sql = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
589 $test->status = add_key($table, $key, false, false);
590 if (!$test->status) {
591 $test->error = $db->ErrorMsg();
593 $tests['add foreign+unique key'] = $test;
596 /// 32th test. Drop one key
597 if ($test->status) {
598 /// Get SQL code and execute it
599 $test = new stdClass;
600 $key = new XMLDBKey('course');
601 $key->setAttributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'anothertest', array('id'));
603 $test->sql = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
604 $test->status = drop_key($table, $key, false, false);
605 if (!$test->status) {
606 $test->error = $db->ErrorMsg();
608 $tests['drop foreign+unique key'] = $test;
611 /// 33th test. Adding one foreign key to the table
612 if ($test->status) {
613 /// Get SQL code and execute it
614 $test = new stdClass;
615 $key = new XMLDBKey('course');
616 $key->setAttributes(XMLDB_KEY_FOREIGN, array('course'), 'anothertest', array('id'));
618 $test->sql = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
619 $test->status = add_key($table, $key, false, false);
620 if (!$test->status) {
621 $test->error = $db->ErrorMsg();
623 $tests['add foreign key'] = $test;
626 /// 34th test. Drop one foreign key
627 if ($test->status) {
628 /// Get SQL code and execute it
629 $test = new stdClass;
630 $key = new XMLDBKey('course');
631 $key->setAttributes(XMLDB_KEY_FOREIGN, array('course'), 'anothertest', array('id'));
633 $test->sql = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
634 $test->status = drop_key($table, $key, false, false);
635 if (!$test->status) {
636 $test->error = $db->ErrorMsg();
638 $tests['drop foreign key'] = $test;
641 /// 35th test. Adding one complex enum field
642 if ($test->status) {
643 /// Create a new field with complex specs (enums are good candidates)
644 $field = new XMLDBField('type');
645 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
646 /// Get SQL code and execute it
647 $test = new stdClass;
648 $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
649 $test->status = add_field($table, $field, false, false);
650 if (!$test->status) {
651 $test->error = $db->ErrorMsg();
653 $tests['add field with enum'] = $test;
656 /// 36th test. Dropping the enum of one field
657 if ($test->status) {
658 /// Get SQL code and execute it
659 $test = new stdClass;
660 $field = new XMLDBField('type');
661 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'general', 'course');
663 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
664 $test->status = change_field_enum($table, $field, false, false);
665 if (!$test->status) {
666 $test->error = $db->ErrorMsg();
668 $tests['delete enumlist from one field'] = $test;
671 /// 37th test. Creating the enum for one field
672 if ($test->status) {
673 /// Get SQL code and execute it
674 $test = new stdClass;
675 $field = new XMLDBField('type');
676 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
677 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
678 $test->status = change_field_enum($table, $field, false, false);
679 if (!$test->status) {
680 $test->error = $db->ErrorMsg();
682 $tests['add enumlist to one field'] = $test;
685 /// 38th test. Renaming one index
686 if ($test->status) {
687 /// Get SQL code and execute it
688 $test = new stdClass;
689 $index = new XMLDBIndex('anyname');
690 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'course'));
692 $test->sql = $table->getRenameIndexSQL($CFG->dbtype, $CFG->prefix, $index, 'newnamefortheindex', true);
693 $test->status = rename_index($table, $index, 'newnamefortheindex', false, false);
694 if (!$test->status) {
695 $test->error = $db->ErrorMsg();
697 $tests['rename index (experimental. DO NOT USE IT)'] = $test;
700 /// 39th test. Renaming one key
701 if ($test->status) {
702 /// Get SQL code and execute it
703 $test = new stdClass;
704 $key = new XMLDBKey('anyname');
705 $key->setAttributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
707 $test->sql = $table->getRenameKeySQL($CFG->dbtype, $CFG->prefix, $key, 'newnameforthekey', true);
708 $test->status = rename_key($table, $key, 'newnameforthekey', false, false);
709 if (!$test->status) {
710 $test->error = $db->ErrorMsg();
712 $tests['rename key (experimental. DO NOT USE IT)'] = $test;
715 /// 40th test. Renaming one field
716 if ($test->status) {
717 /// Get SQL code and execute it
718 $test = new stdClass;
719 $field = new XMLDBField('type');
720 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
722 $test->sql = $table->getRenameFieldSQL($CFG->dbtype, $CFG->prefix, $field, 'newnameforthefield', true);
723 $test->status = rename_field($table, $field, 'newnameforthefield', false, false);
724 if (!$test->status) {
725 $test->error = $db->ErrorMsg();
727 $tests['rename field'] = $test;
730 /// 41th test. Renaming one table
731 if ($test->status) {
732 /// Get SQL code and execute it
733 $test = new stdClass;
735 $test->sql = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, 'newnameforthetable', true);
736 $test->status = rename_table($table, 'newnameforthetable', false, false);
737 if (!$test->status) {
738 $test->error = $db->ErrorMsg();
740 $tests['rename table'] = $test;
743 /// 42th test. Getting the PK sequence name for one table
744 if ($test->status) {
745 $test = new stdClass;
746 $table->setName('newnameforthetable');
747 $test->sql = array(find_sequence_name($table));
748 $test->status = find_sequence_name($table);
749 if (!$test->status) {
750 if (!$test->error = $db->ErrorMsg()) { //If no db errors, result is ok. Just the driver doesn't support this
751 $test->sql = array('not needed for this DB');
752 $test->status = true;
755 $tests['find sequence name'] = $test;
758 /// 43th test. Inserting TEXT contents
759 $textlib = textlib_get_instance();
760 if ($test->status) {
761 $test = new stdClass;
762 $test->status = false;
763 $test->sql = array();
764 $basetext = '日本語 • Русский • Deutsch • English • Español • Français • Italiano • Nederlands • Polski • Português • Svenska • العربية • فارسی 한국어 • עברית • ไทย中文 Ελληνικά • Български • Српски • Українська • Bosanski • Català • Česky • Dansk • Eesti • Simple English • Esperanto • Euskara • Galego • Hrvatski • Ido • Bahasa Indonesia • Íslenska • Lëtzebuergesch • Lietuvių • Magyar • Bahasa Melayu اردو • ئۇيغۇرچه • हिन्दी • नेपाल भाषा मराठी • தமிழ் Հայերեն • Беларуская • Чăваш • Ирон æвзаг • Македонски • Сибирской говор • Afrikaans • Aragonés • Arpitan • Asturianu • Kreyòl Ayisyen • Azərbaycan • Bân-lâm-gú • Basa Banyumasan • Brezhoneg • Corsu • Cymraeg • Deitsch • Føroyskt • Frysk • Furlan • Gaeilge • Gàidhlig • Ilokano • Interlingua • Basa Jawa • Kapampangan • Kernewek • Kurdî كوردی • Ladino לאדינו • Latina • Latviešu • Limburgs • Lumbaart • Nedersaksisch • Nouormand • Occitan • O‘zbek • Piemontèis • Plattdüütsch • Ripoarisch • Sámegiella • Scots • Shqip • Sicilianu • Sinugboanon • Srpskohrvatski / Српскохрватски • Basa Sunda • Kiswahili • Tagalog • Tatarça • Walon • Winaray Авар • Башҡорт • Кыргызча Монгол • Қазақша • Тоҷикӣ • Удмурт • Armãneashce • Bamanankan • Eald Englisc • Gaelg • Interlingue • Kaszëbsczi • Kongo • Ligure • Lingála • lojban • Malagasy • Malti • Māori • Nāhuatl • Ekakairũ Naoero • Novial • Pangasinán • Tok Pisin • Romani / रोमानी • Rumantsch • Runa Simi • Sardu • Tetun • Türkmen / تركمن / Туркмен • Vèneto • Volapük • Võro • West-Vlaoms • Wollof • Zazaki • Žemaitėška';
765 /// Create one big text (1.500.000 chars)
766 $fulltext = '';
767 for ($i=0; $i<1000; $i++) { //1500 * 1000 chars
768 $fulltext .= $basetext;
771 /// Build the record to insert
772 $rec->intro = addslashes($fulltext);
773 $rec->name = 'texttest';
774 /// Calculate its length
775 $textlen = $textlib->strlen($fulltext);
776 if ($rec->id = insert_record('newnameforthetable', $rec)) {
777 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
778 delete_records('newnameforthetable', 'id', $new->id);
779 $newtextlen = $textlib->strlen($new->intro);
780 if ($fulltext === $new->intro) {
781 $test->sql = array($newtextlen . ' cc. (text) sent and received ok');
782 $test->status = true;
783 } else {
784 $test->error = $db->ErrorMsg();
785 $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
786 $test->status = false;
788 } else {
789 $test->error = $db->ErrorMsg();
791 } else {
792 $test->error = $db->ErrorMsg();
794 $tests['insert record '. $textlen . ' cc. (text)'] = $test;
797 /// 44th test. Inserting BINARY contents
798 if ($test->status) {
799 $test = new stdClass;
800 $test->status = false;
801 /// Build the record to insert
802 $rec->avatar = addslashes($fulltext);
803 $rec->name = 'binarytest';
804 /// Calculate its length
805 $textlen = strlen($fulltext);
806 if ($rec->id = insert_record('newnameforthetable', $rec)) {
807 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
808 $newtextlen = strlen($new->avatar);
809 if ($fulltext === $new->avatar) {
810 $test->sql = array($newtextlen . ' bytes (binary) sent and received ok');
811 $test->status = true;
812 } else {
813 $test->error = $db->ErrorMsg();
814 $test->sql = array($newtextlen . ' bytes (binary) transfer failed. Data changed!');
815 $test->status = false;
817 } else {
818 $test->error = $db->ErrorMsg();
820 } else {
821 $test->error = $db->ErrorMsg();
823 $tests['insert record '. $textlen . ' bytes (binary)'] = $test;
826 /// 45th test. update_record with TEXT and BINARY contents
827 if ($test->status) {
828 $test = new stdClass;
829 $test->status = false;
830 $test->sql = array();
831 /// Build the record to insert
832 $rec->intro = addslashes($basetext);
833 $rec->avatar = addslashes($basetext);
834 $rec->name = 'updatelobs';
835 /// Calculate its length
836 $textlen = $textlib->strlen($basetext);
837 $imglen = strlen($basetext);
838 if (update_record('newnameforthetable', $rec)) {
839 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
840 $newtextlen = $textlib->strlen($new->intro);
841 $newimglen = strlen($new->avatar);
842 if ($basetext === $new->avatar && $basetext === $new->intro) {
843 $test->sql = array($newtextlen . ' cc. (text) sent and received ok',
844 $newimglen . ' bytes (binary) sent and received ok');
845 $test->status = true;
846 } else {
847 if ($rec->avatar !== $new->avatar) {
848 $test->error = $db->ErrorMsg();
849 $test->sql = array($newimglen . ' bytes (binary) transfer failed. Data changed!');
850 $test->status = false;
851 } else {
852 $test->error = $db->ErrorMsg();
853 $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
854 $test->status = false;
857 } else {
858 $test->error = $db->ErrorMsg();
860 } else {
861 $test->error = $db->ErrorMsg();
863 $tests['update record '. $textlen . ' cc. (text) and ' . $imglen . ' bytes (binary)'] = $test;
866 /// 46th test. set_field with TEXT contents
867 if ($test->status) {
868 $test = new stdClass;
869 $test->status = false;
870 $test->sql = array();
871 /// Build the record to insert
872 $rec->intro = addslashes($fulltext);
873 $rec->name = 'updatelobs';
874 /// Calculate its length
875 $textlen = $textlib->strlen($fulltext);
876 if (set_field('newnameforthetable', 'intro', $rec->intro, 'name', $rec->name)) {
877 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
878 $newtextlen = $textlib->strlen($new->intro);
879 if ($fulltext === $new->intro) {
880 $test->sql = array($newtextlen . ' cc. (text) sent and received ok');
881 $test->status = true;
882 } else {
883 $test->error = $db->ErrorMsg();
884 $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
885 $test->status = false;
887 } else {
888 $test->error = $db->ErrorMsg();
890 } else {
891 $test->error = $db->ErrorMsg();
893 $tests['set field '. $textlen . ' cc. (text)'] = $test;
896 /// 47th test. set_field with BINARY contents
897 if ($test->status) {
898 $test = new stdClass;
899 $test->status = false;
900 $test->sql = array();
901 /// Build the record to insert
902 $rec->avatar = addslashes($fulltext);
903 $rec->name = 'updatelobs';
904 /// Calculate its length
905 $textlen = strlen($fulltext);
906 if (set_field('newnameforthetable', 'avatar', $rec->avatar, 'name', $rec->name)) {
907 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
908 $newtextlen = strlen($new->avatar);
909 if ($fulltext === $new->avatar) {
910 $test->sql = array($newtextlen . ' bytes (binary) sent and received ok');
911 $test->status = true;
912 } else {
913 $test->error = $db->ErrorMsg();
914 $test->sql = array($newtextlen . ' bytes (binary) transfer failed. Data changed!');
915 $test->status = false;
917 } else {
918 $test->error = $db->ErrorMsg();
920 } else {
921 $test->error = $db->ErrorMsg();
923 $tests['set field '. $textlen . ' bytes (binary)'] = $test;
926 /// TODO: Check here values of the inserted records to see that everything ha the correct value
929 /// Iterate over tests, showing information as needed
930 $o .= '<ol>';
931 foreach ($tests as $key => $test) {
932 $o .= '<li>' . $key . ($test->status ? '<font color="green"> Ok</font>' : ' <font color="red">Error</font>');
933 if (!$test->status) {
934 $o .= '<br/><font color="red">' . $test->error . '</font>';
936 $o .= '<pre>' . implode('<br/>', $test->sql) . '</pre>';
937 $o .= '</li>';
939 $o .= '</ol>';
941 $this->output = $o;
943 /// Launch postaction if exists (leave this here!)
944 if ($this->getPostAction() && $result) {
945 return $this->launch($this->getPostAction());
947 /// Return ok if arrived here
948 return $result;