Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / test / test.class.php
blobab210992ba2f4dbebeaf0e27dff819ea23b798e3
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) 1999 onwards 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, 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 precision of one integer field to a smaller one
382 if ($test->status) {
383 /// Get SQL code and execute it
384 $test = new stdClass;
385 $field = new XMLDBField('course');
386 $field->setAttributes(XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
388 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
389 $test->status = change_field_precision($table, $field, false, false);
390 if (!$test->status) {
391 $test->error = $db->ErrorMsg();
393 $tests['change field precision (integer) to smaller one'] = $test;
396 /// 19th test. Change the sign of one numeric field to unsigned
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', XMLDB_UNSIGNED, 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 (unsigned)'] = $test;
411 /// 20th test. Change the sign of one numeric field to signed
412 if ($test->status) {
413 /// Get SQL code and execute it
414 $test = new stdClass;
415 $field = new XMLDBField('grade');
416 $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null);
418 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
419 $test->status = change_field_unsigned($table, $field, false, false);
420 if (!$test->status) {
421 $test->error = $db->ErrorMsg();
423 $tests['change field sign (signed)'] = $test;
426 /// 21th test. Change the nullability of one char field to not 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, XMLDB_NOTNULL, 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 (not null)'] = $test;
441 /// 22th test. Change the nullability of one char field to null
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, 'Moodle');
448 $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
449 $test->status = change_field_notnull($table, $field, false, false);
450 if (!$test->status) {
451 $test->error = $db->ErrorMsg();
453 $tests['change field nullability (null)'] = $test;
456 /// 23th test. Dropping the default of 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, null);
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['drop 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('name');
476 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
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 NULL field'] = $test;
486 /// 25th test. Creating the default for one field
487 if ($test->status) {
488 /// Get SQL code and execute it
489 $test = new stdClass;
490 $field = new XMLDBField('secondname');
491 $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'Moodle2');
493 $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
494 $test->status = change_field_default($table, $field, false, false);
495 if (!$test->status) {
496 $test->error = $db->ErrorMsg();
498 $tests['add field default of NOT NULL field'] = $test;
502 /// 26th test. Dropping the default of one NOT NULL field
503 if ($test->status) {
504 /// Get SQL code and execute it
505 $test = new stdClass;
506 $field = new XMLDBField('secondname');
507 $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null);
509 $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
510 $test->status = change_field_default($table, $field, false, false);
511 if (!$test->status) {
512 $test->error = $db->ErrorMsg();
514 $tests['drop field default of NOT NULL field'] = $test;
517 /// 27th test. Adding one 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_UNIQUE, array('name', 'secondname', 'grade'));
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 unique index'] = $test;
532 /// 28th test. Adding one not unique index to the table
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('course', 'name'));
539 $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
540 $test->status = add_index($table, $index, false, false);
541 if (!$test->status) {
542 $test->error = $db->ErrorMsg();
544 $tests['add not unique index'] = $test;
547 /// 29th test. Re-add the same index than previous test. Check find_index_name() works.
548 if ($test->status) {
549 /// Get SQL code and execute it
550 $test = new stdClass;
551 $index = new XMLDBIndex('secondname');
552 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name', 'course'));
554 if ($indexfound = find_index_name($table, $index)) {
555 $test->status = true;
556 $test->sql = array();
557 } else {
558 $test->status = true;
559 $test->error = 'Index not found!';
560 $test->sql = array();
563 $tests['check find_index_name()'] = $test;
566 /// 30th test. Dropping one index from the table
567 if ($test->status) {
568 /// Get SQL code and execute it
569 $test = new stdClass;
570 $index = new XMLDBIndex('name');
571 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'grade', 'secondname'));
573 $test->sql = $table->getDropIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
574 $test->status = drop_index($table, $index, false, false);
575 if (!$test->status) {
576 $test->error = $db->ErrorMsg();
578 $tests['drop index'] = $test;
581 /// 31th test. Adding one unique key to the table
582 if ($test->status) {
583 /// Get SQL code and execute it
584 $test = new stdClass;
585 $key = new XMLDBKey('id-course-grade');
586 $key->setAttributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
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 unique key'] = $test;
596 /// 32th test. Adding one foreign+unique key to the table
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->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
604 $test->status = add_key($table, $key, false, false);
605 if (!$test->status) {
606 $test->error = $db->ErrorMsg();
608 $tests['add foreign+unique key'] = $test;
611 /// 33th test. Drop one key
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_UNIQUE, array('course'), 'anothertest', array('id'));
618 $test->sql = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
619 $test->status = drop_key($table, $key, false, false);
620 if (!$test->status) {
621 $test->error = $db->ErrorMsg();
623 $tests['drop foreign+unique key'] = $test;
626 /// 34th test. Adding one foreign key to the table
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->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
634 $test->status = add_key($table, $key, false, false);
635 if (!$test->status) {
636 $test->error = $db->ErrorMsg();
638 $tests['add foreign key'] = $test;
641 /// 35th test. Drop one foreign key
642 if ($test->status) {
643 /// Get SQL code and execute it
644 $test = new stdClass;
645 $key = new XMLDBKey('course');
646 $key->setAttributes(XMLDB_KEY_FOREIGN, array('course'), 'anothertest', array('id'));
648 $test->sql = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
649 $test->status = drop_key($table, $key, false, false);
650 if (!$test->status) {
651 $test->error = $db->ErrorMsg();
653 $tests['drop foreign key'] = $test;
656 /// 36th test. Adding one complex enum field
657 if ($test->status) {
658 /// Create a new field with complex specs (enums are good candidates)
659 $field = new XMLDBField('type');
660 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
661 /// Get SQL code and execute it
662 $test = new stdClass;
663 $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
664 $test->status = add_field($table, $field, false, false);
665 if (!$test->status) {
666 $test->error = $db->ErrorMsg();
668 $tests['add field with enum'] = $test;
671 /// 37th test. Dropping the enum of 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, null, null, 'general', 'course');
678 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
679 $test->status = change_field_enum($table, $field, false, false);
680 if (!$test->status) {
681 $test->error = $db->ErrorMsg();
683 $tests['delete enumlist from one field'] = $test;
686 /// 38th test. Creating the enum for one field
687 if ($test->status) {
688 /// Get SQL code and execute it
689 $test = new stdClass;
690 $field = new XMLDBField('type');
691 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
692 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
693 $test->status = change_field_enum($table, $field, false, false);
694 if (!$test->status) {
695 $test->error = $db->ErrorMsg();
697 $tests['add enumlist to one field'] = $test;
700 /// 39th test. Renaming one index
701 if ($test->status) {
702 /// Get SQL code and execute it
703 $test = new stdClass;
704 $index = new XMLDBIndex('anyname');
705 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'course'));
707 $test->sql = $table->getRenameIndexSQL($CFG->dbtype, $CFG->prefix, $index, 'newnamefortheindex', true);
708 $test->status = rename_index($table, $index, 'newnamefortheindex', false, false);
709 if (!$test->status) {
710 $test->error = $db->ErrorMsg();
712 $tests['rename index (experimental. DO NOT USE IT)'] = $test;
715 /// 40th test. Renaming one key
716 if ($test->status) {
717 /// Get SQL code and execute it
718 $test = new stdClass;
719 $key = new XMLDBKey('anyname');
720 $key->setAttributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
722 $test->sql = $table->getRenameKeySQL($CFG->dbtype, $CFG->prefix, $key, 'newnameforthekey', true);
723 $test->status = rename_key($table, $key, 'newnameforthekey', false, false);
724 if (!$test->status) {
725 $test->error = $db->ErrorMsg();
727 $tests['rename key (experimental. DO NOT USE IT)'] = $test;
730 /// 41th test. Renaming one field
731 if ($test->status) {
732 /// Get SQL code and execute it
733 $test = new stdClass;
734 $field = new XMLDBField('type');
735 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
737 $test->sql = $table->getRenameFieldSQL($CFG->dbtype, $CFG->prefix, $field, 'newnameforthefield', true);
738 $test->status = rename_field($table, $field, 'newnameforthefield', false, false);
739 if (!$test->status) {
740 $test->error = $db->ErrorMsg();
742 $tests['rename field'] = $test;
745 /// 42th test. Renaming one table
746 if ($test->status) {
747 /// Get SQL code and execute it
748 $test = new stdClass;
750 $test->sql = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, 'newnameforthetable', true);
751 $test->status = rename_table($table, 'newnameforthetable', false, false);
752 if (!$test->status) {
753 $test->error = $db->ErrorMsg();
755 $tests['rename table'] = $test;
758 /// 43th test. Add enum to field containing enum
759 if ($test->status) {
760 /// Add enum to field containing enum
761 $table->setName('newnameforthetable');
762 $field = new XMLDBField('newnameforthefield');
763 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
764 /// Get SQL code and execute it
765 $test = new stdClass;
766 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
767 $test->status = change_field_enum($table, $field, false, false);
768 /// Let's see if the constraint exists to alter results
769 if (check_constraint_exists($table, $field)) {
770 $test->sql = array('Nothing executed. Enum already exists. Correct.');
771 } else {
772 $test->status = false;
774 if (!$test->status) {
775 $test->error = $db->ErrorMsg();
777 $tests['add enum to field containing enum'] = $test;
780 /// 44th test. Drop enum from field containing enum
781 if ($test->status) {
782 /// Drop enum from field containing enum
783 $table->setName('newnameforthetable');
784 $field = new XMLDBField('newnameforthefield');
785 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'general', 'course');
786 /// Get SQL code and execute it
787 $test = new stdClass;
788 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
789 $test->status = change_field_enum($table, $field, false, false);
790 if (!$test->status) {
791 $test->error = $db->ErrorMsg();
793 $tests['drop enum from field containing enum'] = $test;
796 /// 45th test. Drop enum from field not containing enum
797 if ($test->status) {
798 /// Drop enum from field not containing enum
799 $table->setName('newnameforthetable');
800 $field = new XMLDBField('newnameforthefield');
801 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'general', 'course');
802 /// Get SQL code and execute it
803 $test = new stdClass;
804 $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
805 $test->status = change_field_enum($table, $field, false, false);
806 /// Let's see if the constraint exists to alter results
807 if (!check_constraint_exists($table, $field)) {
808 $test->sql = array('Nothing executed. Enum does not exists. Correct.');
809 } else {
810 $test->status = false;
812 if (!$test->status) {
813 $test->error = $db->ErrorMsg();
815 $tests['drop enum from field not containing enum'] = $test;
818 /// 46th test. Getting the PK sequence name for one table
819 if ($test->status) {
820 $test = new stdClass;
821 $test->sql = array(find_sequence_name($table));
822 $test->status = find_sequence_name($table);
823 if (!$test->status) {
824 if (!$test->error = $db->ErrorMsg()) { //If no db errors, result is ok. Just the driver doesn't support this
825 $test->sql = array('Not needed for this DB. Correct.');
826 $test->status = true;
829 $tests['find sequence name'] = $test;
832 /// 47th test. Inserting TEXT contents
833 $textlib = textlib_get_instance();
834 if ($test->status) {
835 $test = new stdClass;
836 $test->status = false;
837 $test->sql = array();
838 $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";
839 /// Create one big text (1.500.000 chars)
840 $fulltext = '';
841 for ($i=0; $i<1000; $i++) { //1500 * 1000 chars
842 $fulltext .= $basetext;
845 /// Build the record to insert
846 $rec->intro = addslashes($fulltext);
847 $rec->name = 'texttest';
848 /// Calculate its length
849 $textlen = $textlib->strlen($fulltext);
850 if ($rec->id = insert_record('newnameforthetable', $rec)) {
851 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
852 delete_records('newnameforthetable', 'id', $new->id);
853 $newtextlen = $textlib->strlen($new->intro);
854 if ($fulltext === $new->intro) {
855 $test->sql = array($newtextlen . ' cc. (text) sent and received ok');
856 $test->status = true;
857 } else {
858 $test->error = $db->ErrorMsg();
859 $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
860 $test->status = false;
862 } else {
863 $test->error = $db->ErrorMsg();
865 } else {
866 $test->error = $db->ErrorMsg();
868 $tests['insert record '. $textlen . ' cc. (text)'] = $test;
871 /// 48th test. Inserting BINARY contents
872 if ($test->status) {
873 $test = new stdClass;
874 $test->status = false;
875 /// Build the record to insert
876 $rec->avatar = addslashes($fulltext);
877 $rec->name = 'binarytest';
878 /// Calculate its length
879 $textlen = strlen($fulltext);
880 if ($rec->id = insert_record('newnameforthetable', $rec)) {
881 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
882 $newtextlen = strlen($new->avatar);
883 if ($fulltext === $new->avatar) {
884 $test->sql = array($newtextlen . ' bytes (binary) sent and received ok');
885 $test->status = true;
886 } else {
887 $test->error = $db->ErrorMsg();
888 $test->sql = array($newtextlen . ' bytes (binary) transfer failed. Data changed!');
889 $test->status = false;
891 } else {
892 $test->error = $db->ErrorMsg();
894 } else {
895 $test->error = $db->ErrorMsg();
897 $tests['insert record '. $textlen . ' bytes (binary)'] = $test;
900 /// 49th test. update_record with TEXT and BINARY contents
901 if ($test->status) {
902 $test = new stdClass;
903 $test->status = false;
904 $test->sql = array();
905 /// Build the record to insert
906 $rec->intro = addslashes($basetext);
907 $rec->avatar = addslashes($basetext);
908 $rec->name = 'updatelobs';
909 /// Calculate its length
910 $textlen = $textlib->strlen($basetext);
911 $imglen = strlen($basetext);
912 if (update_record('newnameforthetable', $rec)) {
913 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
914 $newtextlen = $textlib->strlen($new->intro);
915 $newimglen = strlen($new->avatar);
916 if ($basetext === $new->avatar && $basetext === $new->intro) {
917 $test->sql = array($newtextlen . ' cc. (text) sent and received ok',
918 $newimglen . ' bytes (binary) sent and received ok');
919 $test->status = true;
920 } else {
921 if ($rec->avatar !== $new->avatar) {
922 $test->error = $db->ErrorMsg();
923 $test->sql = array($newimglen . ' bytes (binary) transfer failed. Data changed!');
924 $test->status = false;
925 } else {
926 $test->error = $db->ErrorMsg();
927 $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
928 $test->status = false;
931 } else {
932 $test->error = $db->ErrorMsg();
934 } else {
935 $test->error = $db->ErrorMsg();
937 $tests['update record '. $textlen . ' cc. (text) and ' . $imglen . ' bytes (binary)'] = $test;
940 /// 50th test. set_field with TEXT contents
941 if ($test->status) {
942 $test = new stdClass;
943 $test->status = false;
944 $test->sql = array();
945 /// Build the record to insert
946 $rec->intro = addslashes($fulltext);
947 $rec->name = 'updatelobs';
948 /// Calculate its length
949 $textlen = $textlib->strlen($fulltext);
950 if (set_field('newnameforthetable', 'intro', $rec->intro, 'name', $rec->name)) {
951 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
952 $newtextlen = $textlib->strlen($new->intro);
953 if ($fulltext === $new->intro) {
954 $test->sql = array($newtextlen . ' cc. (text) sent and received ok');
955 $test->status = true;
956 } else {
957 $test->error = $db->ErrorMsg();
958 $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
959 $test->status = false;
961 } else {
962 $test->error = $db->ErrorMsg();
964 } else {
965 $test->error = $db->ErrorMsg();
967 $tests['set field '. $textlen . ' cc. (text)'] = $test;
970 /// 51th test. set_field with BINARY contents
971 if ($test->status) {
972 $test = new stdClass;
973 $test->status = false;
974 $test->sql = array();
975 /// Build the record to insert
976 $rec->avatar = addslashes($fulltext);
977 $rec->name = 'updatelobs';
978 /// Calculate its length
979 $textlen = strlen($fulltext);
980 if (set_field('newnameforthetable', 'avatar', $rec->avatar, 'name', $rec->name)) {
981 if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
982 $newtextlen = strlen($new->avatar);
983 if ($fulltext === $new->avatar) {
984 $test->sql = array($newtextlen . ' bytes (binary) sent and received ok');
985 $test->status = true;
986 } else {
987 $test->error = $db->ErrorMsg();
988 $test->sql = array($newtextlen . ' bytes (binary) transfer failed. Data changed!');
989 $test->status = false;
991 } else {
992 $test->error = $db->ErrorMsg();
994 } else {
995 $test->error = $db->ErrorMsg();
997 $tests['set field '. $textlen . ' bytes (binary)'] = $test;
1000 /// TODO: Check here values of the inserted records to see that everything ha the correct value
1003 /// Iterate over tests, showing information as needed
1004 $o .= '<ol>';
1005 foreach ($tests as $key => $test) {
1006 $o .= '<li>' . $key . ($test->status ? '<font color="green"> Ok</font>' : ' <font color="red">Error</font>');
1007 if (!$test->status) {
1008 $o .= '<br/><font color="red">' . $test->error . '</font>';
1010 $o .= '<pre>' . implode('<br/>', $test->sql) . '</pre>';
1011 $o .= '</li>';
1013 $o .= '</ol>';
1015 $this->output = $o;
1017 /// Launch postaction if exists (leave this here!)
1018 if ($this->getPostAction() && $result) {
1019 return $this->launch($this->getPostAction());
1021 /// Return ok if arrived here
1022 return $result;