mirrored from git://git.moodle.org/moodle.git
/
ddl_test.php
1976 lines (1642 loc) · 84.6 KB
/
ddl_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* DDL layer tests.
*
* @package core_ddl
* @category phpunit
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class core_ddl_testcase extends database_driver_testcase {
private $tables = array();
private $records= array();
protected function setUp() {
parent::setUp();
$dbman = $this->tdb->get_manager(); // Loads DDL libs.
$table = new xmldb_table('test_table0');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null);
$table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
$table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, null, null);
$table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
$table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
$table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', null, null, null, null);
$table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, 66.6);
$table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('course', XMLDB_KEY_UNIQUE, array('course'));
$table->add_index('type-name', XMLDB_INDEX_UNIQUE, array('type', 'name'));
$table->add_index('rsstype', XMLDB_INDEX_NOTUNIQUE, array('rsstype'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
// Define 2 initial records for this table.
$this->records[$table->getName()] = array(
(object)array(
'course' => '1',
'type' => 'general',
'name' => 'record',
'intro' => 'first record'),
(object)array(
'course' => '2',
'type' => 'social',
'name' => 'record',
'intro' => 'second record'));
// Second, smaller table.
$table = new xmldb_table ('test_table1');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, null, null, 'Moodle');
$table->add_field('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
$table->add_field('thirdname', XMLDB_TYPE_CHAR, '30', null, null, null, ''); // Nullable column with empty default.
$table->add_field('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
$table->add_field('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null);
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null);
$table->add_field('gradefloat', XMLDB_TYPE_FLOAT, '20,0', null, null, null, null);
$table->add_field('percentfloat', XMLDB_TYPE_FLOAT, '5,2', null, null, null, 99.9);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('course', XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'test_table0', array('course'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
// Define 2 initial records for this table.
$this->records[$table->getName()] = array(
(object)array(
'course' => '1',
'secondname' => 'first record', // Less than 10 cc, please don't modify. Some tests below depend of this.
'intro' => 'first record'),
(object)array(
'course' => '2',
'secondname' => 'second record', // More than 10 cc, please don't modify. Some tests below depend of this.
'intro' => 'second record'));
}
private function create_deftable($tablename) {
$dbman = $this->tdb->get_manager();
if (!isset($this->tables[$tablename])) {
return null;
}
$table = $this->tables[$tablename];
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$dbman->create_table($table);
return $table;
}
/**
* Fill the given test table with some records, as far as
* DDL behaviour must be tested both with real data and
* with empty tables
* @param string $tablename
* @return int count of records
*/
private function fill_deftable($tablename) {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
if (!isset($this->records[$tablename])) {
return null;
}
if ($dbman->table_exists($tablename)) {
foreach ($this->records[$tablename] as $row) {
$DB->insert_record($tablename, $row);
}
} else {
return null;
}
return count($this->records[$tablename]);
}
/**
* Test behaviour of table_exists()
*/
public function test_table_exists() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
// First make sure it returns false if table does not exist.
$table = $this->tables['test_table0'];
try {
$result = $DB->get_records('test_table0');
} catch (dml_exception $e) {
$result = false;
}
$this->resetDebugging();
$this->assertFalse($result);
$this->assertFalse($dbman->table_exists('test_table0')); // By name..
$this->assertFalse($dbman->table_exists($table)); // By xmldb_table..
// Create table and test again.
$dbman->create_table($table);
$this->assertSame(array(), $DB->get_records('test_table0'));
$this->assertTrue($dbman->table_exists('test_table0')); // By name.
$this->assertTrue($dbman->table_exists($table)); // By xmldb_table.
// Drop table and test again.
$dbman->drop_table($table);
try {
$result = $DB->get_records('test_table0');
} catch (dml_exception $e) {
$result = false;
}
$this->resetDebugging();
$this->assertFalse($result);
$this->assertFalse($dbman->table_exists('test_table0')); // By name.
$this->assertFalse($dbman->table_exists($table)); // By xmldb_table.
}
/**
* Test behaviour of create_table()
*/
public function test_create_table() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
// Create table.
$table = $this->tables['test_table1'];
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
// Basic get_tables() test.
$tables = $DB->get_tables();
$this->assertArrayHasKey('test_table1', $tables);
// Basic get_columns() tests.
$columns = $DB->get_columns('test_table1');
$this->assertSame('R', $columns['id']->meta_type);
$this->assertSame('I', $columns['course']->meta_type);
$this->assertSame('C', $columns['name']->meta_type);
$this->assertSame('C', $columns['secondname']->meta_type);
$this->assertSame('C', $columns['thirdname']->meta_type);
$this->assertSame('X', $columns['intro']->meta_type);
$this->assertSame('B', $columns['avatar']->meta_type);
$this->assertSame('N', $columns['grade']->meta_type);
$this->assertSame('N', $columns['percentfloat']->meta_type);
$this->assertSame('I', $columns['userid']->meta_type);
// Some defaults.
$this->assertTrue($columns['course']->has_default);
$this->assertEquals(0, $columns['course']->default_value);
$this->assertTrue($columns['name']->has_default);
$this->assertSame('Moodle', $columns['name']->default_value);
$this->assertTrue($columns['secondname']->has_default);
$this->assertSame('', $columns['secondname']->default_value);
$this->assertTrue($columns['thirdname']->has_default);
$this->assertSame('', $columns['thirdname']->default_value);
$this->assertTrue($columns['percentfloat']->has_default);
$this->assertEquals(99.9, $columns['percentfloat']->default_value);
$this->assertTrue($columns['userid']->has_default);
$this->assertEquals(0, $columns['userid']->default_value);
// Basic get_indexes() test.
$indexes = $DB->get_indexes('test_table1');
$courseindex = reset($indexes);
$this->assertEquals(1, $courseindex['unique']);
$this->assertSame('course', $courseindex['columns'][0]);
// Check sequence returns 1 for first insert.
$rec = (object)array(
'course' => 10,
'secondname' => 'not important',
'intro' => 'not important');
$this->assertSame(1, $DB->insert_record('test_table1', $rec));
// Check defined defaults are working ok.
$dbrec = $DB->get_record('test_table1', array('id' => 1));
$this->assertSame('Moodle', $dbrec->name);
$this->assertSame('', $dbrec->thirdname);
// Check exceptions if multiple R columns.
$table = new xmldb_table ('test_table2');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('rid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('primaryx', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
// Check exceptions missing primary key on R column.
$table = new xmldb_table ('test_table2');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
// Long table name names - the largest allowed.
$table = new xmldb_table('test_table0123456789_____xyz');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
$dbman->drop_table($table);
// Table name is too long.
$table = new xmldb_table('test_table0123456789_____xyz9');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid table name.
$table = new xmldb_table('test_tableCD');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Weird column names - the largest allowed.
$table = new xmldb_table('test_table3');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('abcdef____0123456789_______xyz', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
$dbman->drop_table($table);
// Too long field name - max 30.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('abcdeabcdeabcdeabcdeabcdeabcdez', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid field name.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('abCD', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid integer length.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '21', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid integer default.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 'x');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal length.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '21,10', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal decimals.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '10,11', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal default.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '10,5', null, XMLDB_NOTNULL, null, 'x');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid float length.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_FLOAT, '21,10', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid float decimals.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_FLOAT, '10,11', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid float default.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_FLOAT, '10,5', null, XMLDB_NOTNULL, null, 'x');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
/**
* Test behaviour of drop_table()
*/
public function test_drop_table() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
// Initially table doesn't exist.
$this->assertFalse($dbman->table_exists('test_table0'));
// Create table with contents.
$table = $this->create_deftable('test_table0');
$this->assertTrue($dbman->table_exists('test_table0'));
// Fill the table with some records before dropping it.
$this->fill_deftable('test_table0');
// Drop by xmldb_table object.
$dbman->drop_table($table);
$this->assertFalse($dbman->table_exists('test_table0'));
// Basic get_tables() test.
$tables = $DB->get_tables();
$this->assertArrayNotHasKey('test_table0', $tables);
// Columns cache must be empty.
$columns = $DB->get_columns('test_table0');
$this->assertEmpty($columns);
$indexes = $DB->get_indexes('test_table0');
$this->assertEmpty($indexes);
}
/**
* Test behaviour of rename_table()
*/
public function test_rename_table() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table1');
// Fill the table with some records before renaming it.
$insertedrows = $this->fill_deftable('test_table1');
$this->assertFalse($dbman->table_exists('test_table_cust1'));
$dbman->rename_table($table, 'test_table_cust1');
$this->assertTrue($dbman->table_exists('test_table_cust1'));
// Check sequence returns $insertedrows + 1 for this insert (after rename).
$rec = (object)array(
'course' => 20,
'secondname' => 'not important',
'intro' => 'not important');
$this->assertSame($insertedrows+1, $DB->insert_record('test_table_cust1', $rec));
}
/**
* Test behaviour of field_exists()
*/
public function test_field_exists() {
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table0');
// String params.
// Give a nonexistent table as first param (throw exception).
try {
$dbman->field_exists('nonexistenttable', 'id');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
// Give a nonexistent field as second param (return false).
$this->assertFalse($dbman->field_exists('test_table0', 'nonexistentfield'));
// Correct string params.
$this->assertTrue($dbman->field_exists('test_table0', 'id'));
// Object params.
$realfield = $table->getField('id');
// Give a nonexistent table as first param (throw exception).
$nonexistenttable = new xmldb_table('nonexistenttable');
try {
$dbman->field_exists($nonexistenttable, $realfield);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
// Give a nonexistent field as second param (return false).
$nonexistentfield = new xmldb_field('nonexistentfield');
$this->assertFalse($dbman->field_exists($table, $nonexistentfield));
// Correct object params.
$this->assertTrue($dbman->field_exists($table, $realfield));
// Mix string and object params.
// Correct ones.
$this->assertTrue($dbman->field_exists($table, 'id'));
$this->assertTrue($dbman->field_exists('test_table0', $realfield));
// Non existing tables (throw exception).
try {
$this->assertFalse($dbman->field_exists($nonexistenttable, 'id'));
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
try {
$this->assertFalse($dbman->field_exists('nonexistenttable', $realfield));
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
// Non existing fields (return false).
$this->assertFalse($dbman->field_exists($table, 'nonexistentfield'));
$this->assertFalse($dbman->field_exists('test_table0', $nonexistentfield));
}
/**
* Test behaviour of add_field()
*/
public function test_add_field() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table1');
// Fill the table with some records before adding fields.
$this->fill_deftable('test_table1');
// Add one not null field without specifying default value (throws ddl_exception).
$field = new xmldb_field('onefield');
$field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, null);
try {
$dbman->add_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
// Add one existing field (throws ddl_exception).
$field = new xmldb_field('course');
$field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, 2);
try {
$dbman->add_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
// TODO: add one field with invalid type, must throw exception.
// TODO: add one text field with default, must throw exception.
// TODO: add one binary field with default, must throw exception.
// Add one integer field and check it.
$field = new xmldb_field('oneinteger');
$field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, 2);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'oneinteger'));
$columns = $DB->get_columns('test_table1');
$this->assertEquals('oneinteger', $columns['oneinteger']->name);
$this->assertTrue($columns['oneinteger']->not_null);
// Max_length and scale cannot be checked under all DBs at all for integer fields.
$this->assertFalse($columns['oneinteger']->primary_key);
$this->assertFalse($columns['oneinteger']->binary);
$this->assertTrue($columns['oneinteger']->has_default);
$this->assertEquals(2, $columns['oneinteger']->default_value);
$this->assertSame('I', $columns['oneinteger']->meta_type);
$this->assertEquals(2, $DB->get_field('test_table1', 'oneinteger', array(), IGNORE_MULTIPLE)); // Check default has been applied.
// Add one numeric field and check it.
$field = new xmldb_field('onenumber');
$field->set_attributes(XMLDB_TYPE_NUMBER, '6,3', null, XMLDB_NOTNULL, null, 2.55);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onenumber'));
$columns = $DB->get_columns('test_table1');
$this->assertSame('onenumber', $columns['onenumber']->name);
$this->assertEquals(6, $columns['onenumber']->max_length);
$this->assertEquals(3, $columns['onenumber']->scale);
$this->assertTrue($columns['onenumber']->not_null);
$this->assertFalse($columns['onenumber']->primary_key);
$this->assertFalse($columns['onenumber']->binary);
$this->assertTrue($columns['onenumber']->has_default);
$this->assertEquals(2.550, $columns['onenumber']->default_value);
$this->assertSame('N', $columns['onenumber']->meta_type);
$this->assertEquals(2.550, $DB->get_field('test_table1', 'onenumber', array(), IGNORE_MULTIPLE)); // Check default has been applied.
// Add one numeric field with scale of 0 and check it.
$field = new xmldb_field('onenumberwith0scale');
$field->set_attributes(XMLDB_TYPE_NUMBER, '6,0', null, XMLDB_NOTNULL, null, 2);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onenumberwith0scale'));
$columns = $DB->get_columns('test_table1');
$this->assertEquals(6, $columns['onenumberwith0scale']->max_length);
// We can not use assertEquals as that accepts null/false as a valid value.
$this->assertSame('0', strval($columns['onenumberwith0scale']->scale));
// Add one float field and check it (not official type - must work as number).
$field = new xmldb_field('onefloat');
$field->set_attributes(XMLDB_TYPE_FLOAT, '6,3', null, XMLDB_NOTNULL, null, 3.550);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onefloat'));
$columns = $DB->get_columns('test_table1');
$this->assertSame('onefloat', $columns['onefloat']->name);
$this->assertTrue($columns['onefloat']->not_null);
// Max_length and scale cannot be checked under all DBs at all for float fields.
$this->assertFalse($columns['onefloat']->primary_key);
$this->assertFalse($columns['onefloat']->binary);
$this->assertTrue($columns['onefloat']->has_default);
$this->assertEquals(3.550, $columns['onefloat']->default_value);
$this->assertSame('N', $columns['onefloat']->meta_type);
// Just rounding DB information to 7 decimal digits. Fair enough to test 3.550 and avoids one nasty bug
// in MSSQL core returning wrong floats (http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/5e08de63-16bb-4f24-b645-0cf8fc669de3)
// In any case, floats aren't officially supported by Moodle, with number/decimal type being the correct ones, so
// this isn't a real problem at all.
$this->assertEquals(3.550, round($DB->get_field('test_table1', 'onefloat', array(), IGNORE_MULTIPLE), 7)); // Check default has been applied.
// Add one char field and check it.
$field = new xmldb_field('onechar');
$field->set_attributes(XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, 'Nice dflt!');
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onechar'));
$columns = $DB->get_columns('test_table1');
$this->assertSame('onechar', $columns['onechar']->name);
$this->assertEquals(25, $columns['onechar']->max_length);
$this->assertNull($columns['onechar']->scale);
$this->assertTrue($columns['onechar']->not_null);
$this->assertFalse($columns['onechar']->primary_key);
$this->assertFalse($columns['onechar']->binary);
$this->assertTrue($columns['onechar']->has_default);
$this->assertSame('Nice dflt!', $columns['onechar']->default_value);
$this->assertSame('C', $columns['onechar']->meta_type);
$this->assertEquals('Nice dflt!', $DB->get_field('test_table1', 'onechar', array(), IGNORE_MULTIPLE)); // Check default has been applied.
// Add one big text field and check it.
$field = new xmldb_field('onetext');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big');
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onetext'));
$columns = $DB->get_columns('test_table1');
$this->assertSame('onetext', $columns['onetext']->name);
$this->assertEquals(-1, $columns['onetext']->max_length); // -1 means unknown or big.
$this->assertNull($columns['onetext']->scale);
$this->assertFalse($columns['onetext']->not_null);
$this->assertFalse($columns['onetext']->primary_key);
$this->assertFalse($columns['onetext']->binary);
$this->assertFalse($columns['onetext']->has_default);
$this->assertNull($columns['onetext']->default_value);
$this->assertSame('X', $columns['onetext']->meta_type);
// Add one medium text field and check it.
$field = new xmldb_field('mediumtext');
$field->set_attributes(XMLDB_TYPE_TEXT, 'medium');
$dbman->add_field($table, $field);
$columns = $DB->get_columns('test_table1');
$this->assertTrue(($columns['mediumtext']->max_length == -1) or ($columns['mediumtext']->max_length >= 16777215)); // -1 means unknown or big.
// Add one small text field and check it.
$field = new xmldb_field('smalltext');
$field->set_attributes(XMLDB_TYPE_TEXT, 'small');
$dbman->add_field($table, $field);
$columns = $DB->get_columns('test_table1');
$this->assertTrue(($columns['smalltext']->max_length == -1) or ($columns['smalltext']->max_length >= 65535)); // -1 means unknown or big.
// Add one binary field and check it.
$field = new xmldb_field('onebinary');
$field->set_attributes(XMLDB_TYPE_BINARY);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onebinary'));
$columns = $DB->get_columns('test_table1');
$this->assertSame('onebinary', $columns['onebinary']->name);
$this->assertEquals(-1, $columns['onebinary']->max_length);
$this->assertNull($columns['onebinary']->scale);
$this->assertFalse($columns['onebinary']->not_null);
$this->assertFalse($columns['onebinary']->primary_key);
$this->assertTrue($columns['onebinary']->binary);
$this->assertFalse($columns['onebinary']->has_default);
$this->assertNull($columns['onebinary']->default_value);
$this->assertSame('B', $columns['onebinary']->meta_type);
// TODO: check datetime type. Although unused should be fully supported.
}
/**
* Test behaviour of drop_field()
*/
public function test_drop_field() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table0');
// Fill the table with some records before dropping fields.
$this->fill_deftable('test_table0');
// Drop field with simple xmldb_field having indexes, must return exception.
$field = new xmldb_field('type'); // Field has indexes and default clause.
$this->assertTrue($dbman->field_exists($table, 'type'));
try {
$dbman->drop_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
$this->assertTrue($dbman->field_exists($table, 'type')); // Continues existing, drop aborted.
// Drop field with complete xmldb_field object and related indexes, must return exception.
$field = $table->getField('course'); // Field has indexes and default clause.
$this->assertTrue($dbman->field_exists($table, $field));
try {
$dbman->drop_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
$this->assertTrue($dbman->field_exists($table, $field)); // Continues existing, drop aborted.
// Drop one non-existing field, must return exception.
$field = new xmldb_field('nonexistingfield');
$this->assertFalse($dbman->field_exists($table, $field));
try {
$dbman->drop_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_field_missing_exception', $e);
}
// Drop field with simple xmldb_field, not having related indexes.
$field = new xmldb_field('forcesubscribe'); // Field has default clause.
$this->assertTrue($dbman->field_exists($table, 'forcesubscribe'));
$dbman->drop_field($table, $field);
$this->assertFalse($dbman->field_exists($table, 'forcesubscribe'));
// Drop field with complete xmldb_field object, not having related indexes.
$field = new xmldb_field('trackingtype'); // Field has default clause.
$this->assertTrue($dbman->field_exists($table, $field));
$dbman->drop_field($table, $field);
$this->assertFalse($dbman->field_exists($table, $field));
}
/**
* Test behaviour of change_field_type()
*/
public function test_change_field_type() {
$DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
// Create table with indexed field and not indexed field to
// perform tests in both fields, both having defaults.
$table = new xmldb_table('test_table_cust0');
$table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_field('anothernumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '4');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));
$dbman->create_table($table);
$record = new stdClass();
$record->onenumber = 2;
$record->anothernumber = 4;
$recoriginal = $DB->insert_record('test_table_cust0', $record);
// Change column from integer to varchar. Must return exception because of dependent index.
$field = new xmldb_field('onenumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'test');
try {
$dbman->change_field_type($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
// Column continues being integer 10 not null default 2.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('I', $columns['onenumber']->meta_type);
// TODO: check the rest of attributes.
// Change column from integer to varchar. Must work because column has no dependencies.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'test');
$dbman->change_field_type($table, $field);
// Column is char 30 not null default 'test' now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('C', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change column back from char to integer.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_INTEGER, '8', null, XMLDB_NOTNULL, null, '5');
$dbman->change_field_type($table, $field);
// Column is integer 8 not null default 5 now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('I', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change column once more from integer to char.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, "test'n drop");
$dbman->change_field_type($table, $field);
// Column is char 30 not null default "test'n drop" now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('C', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Insert one string value and try to convert to integer. Must throw exception.
$record = new stdClass();
$record->onenumber = 7;
$record->anothernumber = 'string value';
$rectodrop = $DB->insert_record('test_table_cust0', $record);
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '5');
try {
$dbman->change_field_type($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_change_structure_exception', $e);
}
// Column continues being char 30 not null default "test'n drop" now.
$this->assertSame('C', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
$DB->delete_records('test_table_cust0', array('id' => $rectodrop)); // Delete the string record.
// Change the column from varchar to float.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_FLOAT, '20,10', null, null, null, null);
$dbman->change_field_type($table, $field);
// Column is float 20,10 null default null.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('N', $columns['anothernumber']->meta_type); // Floats are seen as number.
// TODO: check the rest of attributes.
// Change the column back from float to varchar.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'test');
$dbman->change_field_type($table, $field);
// Column is char 20 not null default "test" now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('C', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change the column from varchar to number.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', null, null, null, null);
$dbman->change_field_type($table, $field);
// Column is number 20,10 null default null now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('N', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change the column from number to integer.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, null, null, null);
$dbman->change_field_type($table, $field);
// Column is integer 2 null default null now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('I', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change the column from integer to text.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
$dbman->change_field_type($table, $field);
// Column is char text not null default null.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('X', $columns['anothernumber']->meta_type);
// Change the column back from text to number.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', null, null, null, null);
$dbman->change_field_type($table, $field);
// Column is number 20,10 null default null now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('N', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change the column from number to text.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
$dbman->change_field_type($table, $field);
// Column is char text not null default "test" now.
$columns = $DB->get_columns('test_table_cust0');
$this->assertSame('X', $columns['anothernumber']->meta_type);
// TODO: check the rest of attributes.
// Change the column back from text to integer.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 10);
$dbman->change_field_type($table, $field);