]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/database/DBManagerTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / database / DBManagerTest.php
1 <?php
2 require_once 'include/database/DBManagerFactory.php';
3 require_once 'modules/Contacts/Contact.php';
4
5 class DBManagerTest extends Sugar_PHPUnit_Framework_TestCase
6 {
7     private $_db;
8     
9     protected $backupGlobals = FALSE;
10     
11     public function setUp()
12     {
13         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
14         $this->_db = DBManagerFactory::getInstance();
15                 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
16     }
17     
18     public function tearDown() 
19     {
20         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
21         unset($GLOBALS['current_user']);
22         unset($GLOBALS['app_strings']);
23     }
24     
25     private function _createRecords(
26         $num
27         )
28     {
29         $beanIds = array();
30         for ( $i = 0; $i < $num; $i++ ) {
31             $bean = new Contact();
32             $bean->id = "$i-test" . date("YmdHis");
33             $bean->last_name = "foobar";
34             $this->_db->insert($bean);
35             $beanIds[] = $bean->id;
36         }
37         
38         return $beanIds;
39     }
40     
41     private function _removeRecords(
42         array $ids
43         )
44     {
45         foreach ($ids as $id)
46             $this->_db->query("DELETE From contacts where id = '{$id}'");
47     }
48     
49     public function testGetTableName()
50     {
51         $this->_db->createTableParams('MyTableName',array('foo'=>'foo'),array());
52         
53         $this->assertEquals($this->_db->getTableName(),'MyTableName');
54     }
55     
56     public function testGetDatabase()
57     {
58         if ( $this->_db instanceOf MysqliManager )
59             $this->assertType('Mysqli',$this->_db->getDatabase());
60         else
61             $this->assertTrue(is_resource($this->_db->getDatabase()));
62     }
63     
64     public function testGetHelper()
65     {
66         $this->assertType('DBHelper',$this->_db->getHelper());
67     }
68     
69     public function testCheckError()
70     {
71         $this->assertFalse($this->_db->checkError());
72     }
73     
74     public function testCheckErrorNoConnection()
75     {
76         $this->_db->disconnect();
77         $this->assertTrue($this->_db->checkError());
78         $this->_db = &DBManagerFactory::getInstance();
79     }
80     
81     public function testGetQueryTime()
82     {
83         $this->_db->version();
84         $this->assertTrue($this->_db->getQueryTime() > 0);
85     }
86     
87     public function testCheckConnection()
88     {
89         $this->_db->checkConnection();
90         if ( $this->_db instanceOf MysqliManager )
91             $this->assertType('Mysqli',$this->_db->getDatabase());
92         else
93             $this->assertTrue(is_resource($this->_db->getDatabase()));
94     }
95     
96     public function testInsert()
97     {
98         $bean = new Contact();
99         $bean->last_name = 'foobar' . date("YmdHis");
100         $bean->id   = 'test' . date("YmdHis");
101         $this->_db->insert($bean);
102         
103         $result = $this->_db->query("select id, last_name from contacts where id = '{$bean->id}'");
104         $row = $this->_db->fetchByAssoc($result);
105         $this->assertEquals($row['last_name'],$bean->last_name);
106         $this->assertEquals($row['id'],$bean->id);
107         
108         $this->_db->query("delete from contacts where id = '{$row['id']}'");
109     }
110     
111     public function testUpdate()
112     {
113         $bean = new Contact();
114         $bean->last_name = 'foobar' . date("YmdHis");
115         $bean->id   = 'test' . date("YmdHis");
116         $this->_db->insert($bean);
117         $id = $bean->id;
118         
119         $bean = new Contact();
120         $bean->last_name = 'newfoobar' . date("YmdHis");
121         $this->_db->update($bean,array('id'=>$id));
122         
123         $result = $this->_db->query("select id, last_name from contacts where id = '{$id}'");
124         $row = $this->_db->fetchByAssoc($result);
125         $this->assertEquals($row['last_name'],$bean->last_name);
126         $this->assertEquals($row['id'],$id);
127         
128         $this->_db->query("delete from contacts where id = '{$row['id']}'");
129     }
130     
131     public function testDelete()
132     {
133         $bean = new Contact();
134         $bean->last_name = 'foobar' . date("YmdHis");
135         $bean->id   = 'test' . date("YmdHis");
136         $this->_db->insert($bean);
137         $id = $bean->id;
138         
139         $bean = new Contact();
140         $this->_db->delete($bean,array('id'=>$id));
141         
142         $result = $this->_db->query("select deleted from contacts where id = '{$id}'");
143         $row = $this->_db->fetchByAssoc($result);
144         $this->assertEquals($row['deleted'],'1');
145         
146         $this->_db->query("delete from contacts where id = '{$id}'");
147     }
148     
149     public function testRetrieve()
150     {
151         $bean = new Contact();
152         $bean->last_name = 'foobar' . date("YmdHis");
153         $bean->id   = 'test' . date("YmdHis");
154         $this->_db->insert($bean);
155         $id = $bean->id;
156         
157         $bean = new Contact();
158         $result = $this->_db->retrieve($bean,array('id'=>$id));
159         $row = $this->_db->fetchByAssoc($result);
160         $this->assertEquals($row['id'],$id);
161         
162         $this->_db->query("delete from contacts where id = '{$id}'");
163     }
164     
165     public function testRetrieveView()
166     {
167         // TODO: Write this test
168     }
169     
170     public function testCreateTable()
171     {
172         // TODO: Write this test
173     }
174     
175     public function testCreateTableParams()
176     {
177         $tablename = 'test' . date("YmdHis");
178         $this->_db->createTableParams($tablename,
179             array(
180                 'foo' => array (
181                     'name' => 'foo',
182                     'type' => 'varchar',
183                     'len' => '255',
184                     ),
185                 ),
186             array(
187                 array(
188                     'name'   => 'idx_foo',
189                     'type'   => 'index',
190                     'fields' => array('foo'),
191                     )
192                 )
193             );
194         $this->assertTrue(in_array($tablename,$this->_db->getTablesArray()));
195         
196         $this->_db->dropTableName($tablename);
197     }
198     
199     public function testRepairTable()
200     {
201         // TODO: Write this test
202     }
203     
204     public function testRepairTableParams()
205     {
206         // TODO: Write this test
207     }
208     
209     public function testCompareFieldInTables()
210     {
211         $tablename1 = 'test1_' . date("YmdHis");
212         $this->_db->createTableParams($tablename1,
213             array(
214                 'foo' => array (
215                     'name' => 'foo',
216                     'type' => 'varchar',
217                     'len' => '255',
218                     ),
219                 ),
220             array()
221             );
222         $tablename2 = 'test2_' . date("YmdHis");
223         $this->_db->createTableParams($tablename2,
224             array(
225                 'foo' => array (
226                     'name' => 'foo',
227                     'type' => 'varchar',
228                     'len' => '255',
229                     ),
230                 ),
231             array()
232             );
233         
234         $res = $this->_db->compareFieldInTables(
235             'foo', $tablename1, $tablename2);
236         
237         $this->assertEquals($res['msg'],'match');
238         
239         $this->_db->dropTableName($tablename1);
240         $this->_db->dropTableName($tablename2);
241     }
242     
243     public function testCompareFieldInTablesNotInTable1()
244     {
245         $tablename1 = 'test3_' . date("YmdHis");
246         $this->_db->createTableParams($tablename1,
247             array(
248                 'foobar' => array (
249                     'name' => 'foobar',
250                     'type' => 'varchar',
251                     'len' => '255',
252                     ),
253                 ),
254             array()
255             );
256         $tablename2 = 'test4_' . date("YmdHis");
257         $this->_db->createTableParams($tablename2,
258             array(
259                 'foo' => array (
260                     'name' => 'foo',
261                     'type' => 'varchar',
262                     'len' => '255',
263                     ),
264                 ),
265             array()
266             );
267         
268         $res = $this->_db->compareFieldInTables(
269             'foo', $tablename1, $tablename2);
270         $this->assertEquals($res['msg'],'not_exists_table1');
271         
272         $this->_db->dropTableName($tablename1);
273         $this->_db->dropTableName($tablename2);
274     }
275     
276     public function testCompareFieldInTablesNotInTable2()
277     {
278         $tablename1 = 'test5_' . date("YmdHis");
279         $this->_db->createTableParams($tablename1,
280             array(
281                 'foo' => array (
282                     'name' => 'foo',
283                     'type' => 'varchar',
284                     'len' => '255',
285                     ),
286                 ),
287             array()
288             );
289         $tablename2 = 'test6_' . date("YmdHis");
290         $this->_db->createTableParams($tablename2,
291             array(
292                 'foobar' => array (
293                     'name' => 'foobar',
294                     'type' => 'varchar',
295                     'len' => '255',
296                     ),
297                 ),
298             array()
299             );
300         
301         $res = $this->_db->compareFieldInTables(
302             'foo', $tablename1, $tablename2);
303         
304         $this->assertEquals($res['msg'],'not_exists_table2');
305         
306         $this->_db->dropTableName($tablename1);
307         $this->_db->dropTableName($tablename2);
308     }
309     
310     public function testCompareFieldInTablesFieldsDoNotMatch()
311     {
312         $tablename1 = 'test7_' . date("YmdHis");
313         $this->_db->createTableParams($tablename1,
314             array(
315                 'foo' => array (
316                     'name' => 'foo',
317                     'type' => 'varchar',
318                     'len' => '255',
319                     ),
320                 ),
321             array()
322             );
323         $tablename2 = 'test8_' . date("YmdHis");
324         $this->_db->createTableParams($tablename2,
325             array(
326                 'foo' => array (
327                     'name' => 'foo',
328                     'type' => 'int',
329                     ),
330                 ),
331             array()
332             );
333         
334         $res = $this->_db->compareFieldInTables(
335             'foo', $tablename1, $tablename2);
336         
337         $this->assertEquals($res['msg'],'no_match');
338         
339         $this->_db->dropTableName($tablename1);
340         $this->_db->dropTableName($tablename2);
341     }
342     
343     public function testCompareIndexInTables()
344     {
345         $tablename1 = 'test9_' . date("YmdHis");
346         $this->_db->createTableParams($tablename1,
347             array(
348                 'foo' => array (
349                     'name' => 'foo',
350                     'type' => 'varchar',
351                     'len' => '255',
352                     ),
353                 ),
354             array(
355                 array(
356                     'name'   => 'idx_foo',
357                     'type'   => 'index',
358                     'fields' => array('foo'),
359                     )
360                 )
361             );
362         $tablename2 = 'test10_' . date("YmdHis");
363         $this->_db->createTableParams($tablename2,
364             array(
365                 'foo' => array (
366                     'name' => 'foo',
367                     'type' => 'varchar',
368                     'len' => '255',
369                     ),
370                 ),
371             array(
372                 array(
373                     'name'   => 'idx_foo',
374                     'type'   => 'index',
375                     'fields' => array('foo'),
376                     )
377                 )
378             );
379         
380         $res = $this->_db->compareIndexInTables(
381             'idx_foo', $tablename1, $tablename2);
382         
383         $this->assertEquals($res['msg'],'match');
384         
385         $this->_db->dropTableName($tablename1);
386         $this->_db->dropTableName($tablename2);
387     }
388     
389     public function testCompareIndexInTablesNotInTable1()
390     {
391         $tablename1 = 'test11_' . date("YmdHis");
392         $this->_db->createTableParams($tablename1,
393             array(
394                 'foo' => array (
395                     'name' => 'foo',
396                     'type' => 'varchar',
397                     'len' => '255',
398                     ),
399                 ),
400             array(
401                 array(
402                     'name'   => 'idx_foobar',
403                     'type'   => 'index',
404                     'fields' => array('foo'),
405                     )
406                 )
407             );
408         $tablename2 = 'test12_' . date("YmdHis");
409         $this->_db->createTableParams($tablename2,
410             array(
411                 'foo' => array (
412                     'name' => 'foo',
413                     'type' => 'varchar',
414                     'len' => '255',
415                     ),
416                 ),
417             array(
418                 array(
419                     'name'   => 'idx_foo',
420                     'type'   => 'index',
421                     'fields' => array('foo'),
422                     )
423                 )
424             );
425         
426         $res = $this->_db->compareIndexInTables(
427             'idx_foo', $tablename1, $tablename2);
428         
429         $this->assertEquals($res['msg'],'not_exists_table1');
430         
431         $this->_db->dropTableName($tablename1);
432         $this->_db->dropTableName($tablename2);
433     }
434     
435     public function testCompareIndexInTablesNotInTable2()
436     {
437         $tablename1 = 'test13_' . date("YmdHis");
438         $this->_db->createTableParams($tablename1,
439             array(
440                 'foo' => array (
441                     'name' => 'foo',
442                     'type' => 'varchar',
443                     'len' => '255',
444                     ),
445                 ),
446             array(
447                 array(
448                     'name'   => 'idx_foo',
449                     'type'   => 'index',
450                     'fields' => array('foo'),
451                     )
452                 )
453             );
454         $tablename2 = 'test14_' . date("YmdHis");
455         $this->_db->createTableParams($tablename2,
456             array(
457                 'foo' => array (
458                     'name' => 'foo',
459                     'type' => 'varchar',
460                     'len' => '255',
461                     ),
462                 ),
463             array(
464                 array(
465                     'name'   => 'idx_foobar',
466                     'type'   => 'index',
467                     'fields' => array('foo'),
468                     )
469                 )
470             );
471         
472         $res = $this->_db->compareIndexInTables(
473             'idx_foo', $tablename1, $tablename2);
474         
475         $this->assertEquals($res['msg'],'not_exists_table2');
476         
477         $this->_db->dropTableName($tablename1);
478         $this->_db->dropTableName($tablename2);
479     }
480     
481     public function testCompareIndexInTablesIndexesDoNotMatch()
482     {
483         $tablename1 = 'test15_' . date("YmdHis");
484         $this->_db->createTableParams($tablename1,
485             array(
486                 'foo' => array (
487                     'name' => 'foo',
488                     'type' => 'varchar',
489                     'len' => '255',
490                     ),
491                 ),
492             array(
493                 array(
494                     'name'   => 'idx_foo',
495                     'type'   => 'index',
496                     'fields' => array('foo'),
497                     )
498                 )
499             );
500         $tablename2 = 'test16_' . date("YmdHis");
501         $this->_db->createTableParams($tablename2,
502             array(
503                 'foo' => array (
504                     'name' => 'foobar',
505                     'type' => 'varchar',
506                     'len' => '255',
507                     ),
508                 ),
509             array(
510                 array(
511                     'name'   => 'idx_foo',
512                     'type'   => 'index',
513                     'fields' => array('foobar'),
514                     )
515                 )
516             );
517         
518         $res = $this->_db->compareIndexInTables(
519             'idx_foo', $tablename1, $tablename2);
520         
521         $this->assertEquals($res['msg'],'no_match');
522         
523         $this->_db->dropTableName($tablename1);
524         $this->_db->dropTableName($tablename2);
525     }
526     
527     public function testCreateIndex()
528     {
529         // TODO: Write this test
530     }
531     
532     public function testAddIndexes()
533     {
534         $tablename1 = 'test17_' . date("YmdHis");
535         $this->_db->createTableParams($tablename1,
536             array(
537                 'foo' => array (
538                     'name' => 'foo',
539                     'type' => 'varchar',
540                     'len' => '255',
541                     ),
542                 ),
543             array(
544                 array(
545                     'name'   => 'idx_foo',
546                     'type'   => 'index',
547                     'fields' => array('foo'),
548                     )
549                 )
550             );
551         $tablename2 = 'test18_' . date("YmdHis");
552         $this->_db->createTableParams($tablename2,
553             array(
554                 'foo' => array (
555                     'name' => 'foo',
556                     'type' => 'varchar',
557                     'len' => '255',
558                     ),
559                 ),
560             array()
561             );
562         
563         // first test not executing the statement
564         $this->_db->addIndexes(
565             $tablename2,
566             array(array(
567                 'name'   => 'idx_foo',
568                 'type'   => 'index',
569                 'fields' => array('foo'),
570                 )),
571             false);
572         
573         $res = $this->_db->compareIndexInTables(
574             'idx_foo', $tablename1, $tablename2);
575         
576         $this->assertEquals($res['msg'],'not_exists_table2');
577         
578         // now, execute the statement
579         $this->_db->addIndexes(
580             $tablename2,
581             array(array(
582                 'name'   => 'idx_foo',
583                 'type'   => 'index',
584                 'fields' => array('foo'),
585                 ))
586             );
587         $res = $this->_db->compareIndexInTables(
588             'idx_foo', $tablename1, $tablename2);
589         
590         $this->assertEquals($res['msg'],'match');
591         
592         $this->_db->dropTableName($tablename1);
593         $this->_db->dropTableName($tablename2);
594     }
595     
596     public function testDropIndexes()
597     {
598         $tablename1 = 'test19_' . date("YmdHis");
599         $this->_db->createTableParams($tablename1,
600             array(
601                 'foo' => array (
602                     'name' => 'foo',
603                     'type' => 'varchar',
604                     'len' => '255',
605                     ),
606                 ),
607             array(
608                 array(
609                     'name'   => 'idx_foo',
610                     'type'   => 'index',
611                     'fields' => array('foo'),
612                     )
613                 )
614             );
615         $tablename2 = 'test20_' . date("YmdHis");
616         $this->_db->createTableParams($tablename2,
617             array(
618                 'foo' => array (
619                     'name' => 'foo',
620                     'type' => 'varchar',
621                     'len' => '255',
622                     ),
623                 ),
624             array(
625                 array(
626                     'name'   => 'idx_foo',
627                     'type'   => 'index',
628                     'fields' => array('foo'),
629                     )
630                 )
631             );
632         
633         $res = $this->_db->compareIndexInTables(
634             'idx_foo', $tablename1, $tablename2);
635         
636         $this->assertEquals($res['msg'],'match');
637         
638         // first test not executing the statement
639         $this->_db->dropIndexes(
640             $tablename2,
641             array(array(
642                 'name'   => 'idx_foo',
643                 'type'   => 'index',
644                 'fields' => array('foo'),
645                 )),
646             false);
647         
648         $res = $this->_db->compareIndexInTables(
649             'idx_foo', $tablename1, $tablename2);
650         
651         $this->assertEquals($res['msg'],'match');
652         
653         // now, execute the statement
654         $sql = $this->_db->dropIndexes(
655             $tablename2,
656             array(array(
657                 'name'   => 'idx_foo',
658                 'type'   => 'index',
659                 'fields' => array('foo'),
660                 )),
661             true
662             );
663         
664         $res = $this->_db->compareIndexInTables(
665             'idx_foo', $tablename1, $tablename2);
666         
667         $this->assertEquals($res['msg'],'not_exists_table2');
668         
669         $this->_db->dropTableName($tablename1);
670         $this->_db->dropTableName($tablename2);
671     }
672     
673     public function testModifyIndexes()
674     {
675         $tablename1 = 'test21_' . date("YmdHis");
676         $this->_db->createTableParams($tablename1,
677             array(
678                 'foo' => array (
679                     'name' => 'foo',
680                     'type' => 'varchar',
681                     'len' => '255',
682                     ),
683                 'foobar' => array (
684                     'name' => 'foobar',
685                     'type' => 'varchar',
686                     'len' => '255',
687                     ),
688                 ),
689             array(
690                 array(
691                     'name'   => 'idx_foo',
692                     'type'   => 'index',
693                     'fields' => array('foo'),
694                     )
695                 )
696             );
697         $tablename2 = 'test22_' . date("YmdHis");
698         $this->_db->createTableParams($tablename2,
699             array(
700                 'foo' => array (
701                     'name' => 'foo',
702                     'type' => 'varchar',
703                     'len' => '255',
704                     ),
705                 'foobar' => array (
706                     'name' => 'foobar',
707                     'type' => 'varchar',
708                     'len' => '255',
709                     ),
710                 ),
711             array(
712                 array(
713                     'name'   => 'idx_foo',
714                     'type'   => 'index',
715                     'fields' => array('foobar'),
716                     )
717                 )
718             );
719         
720         $res = $this->_db->compareIndexInTables(
721             'idx_foo', $tablename1, $tablename2);
722         
723         $this->assertEquals($res['msg'],'no_match');
724         
725         $this->_db->modifyIndexes(
726             $tablename2,
727             array(array(
728                 'name'   => 'idx_foo',
729                 'type'   => 'index',
730                 'fields' => array('foo'),
731                 )),
732             false);
733         
734         $res = $this->_db->compareIndexInTables(
735             'idx_foo', $tablename1, $tablename2);
736         
737         $this->assertEquals($res['msg'],'no_match');
738         
739         $this->_db->modifyIndexes(
740             $tablename2,
741             array(array(
742                 'name'   => 'idx_foo',
743                 'type'   => 'index',
744                 'fields' => array('foo'),
745                 ))
746             );
747         
748         $res = $this->_db->compareIndexInTables(
749             'idx_foo', $tablename1, $tablename2);
750         
751         $this->assertEquals($res['msg'],'match');
752         
753         $this->_db->dropTableName($tablename1);
754         $this->_db->dropTableName($tablename2);
755     }
756     
757     public function testAddColumn()
758     {
759         $tablename1 = 'test23_' . date("YmdHis");
760         $this->_db->createTableParams($tablename1,
761             array(
762                 'foo' => array (
763                     'name' => 'foo',
764                     'type' => 'varchar',
765                     'len' => '255',
766                     ),
767                 'foobar' => array (
768                     'name' => 'foobar',
769                     'type' => 'varchar',
770                     'len' => '255',
771                     ),
772                 ),
773             array()
774             );
775         $tablename2 = 'test24_' . date("YmdHis");
776         $this->_db->createTableParams($tablename2,
777             array(
778                 'foo' => array (
779                     'name' => 'foo',
780                     'type' => 'varchar',
781                     'len' => '255',
782                     ),
783                 ),
784             array()
785             );
786         
787         $res = $this->_db->compareFieldInTables(
788             'foobar', $tablename1, $tablename2);
789         
790         $this->assertEquals($res['msg'],'not_exists_table2');
791         
792         $this->_db->addColumn(
793             $tablename2,
794             array(
795                 'foobar' => array (
796                     'name' => 'foobar',
797                     'type' => 'varchar',
798                     'len' => '255',
799                     )
800                 )
801             );
802         
803         $res = $this->_db->compareFieldInTables(
804             'foobar', $tablename1, $tablename2);
805         
806         $this->assertEquals($res['msg'],'match');
807         
808         $this->_db->dropTableName($tablename1);
809         $this->_db->dropTableName($tablename2);
810     }
811     
812     public function testAlterColumn()
813     {
814         $tablename1 = 'test25_' . date("YmdHis");
815         $this->_db->createTableParams($tablename1,
816             array(
817                 'foo' => array (
818                     'name' => 'foo',
819                     'type' => 'varchar',
820                     'len' => '255',
821                     ),
822                 'foobar' => array (
823                     'name' => 'foobar',
824                     'type' => 'varchar',
825                     'len' => '255',
826                     'required' => true,
827                     ),
828                 ),
829             array()
830             );
831         $tablename2 = 'test26_' . date("YmdHis");
832         $this->_db->createTableParams($tablename2,
833             array(
834                 'foo' => array (
835                     'name' => 'foo',
836                     'type' => 'varchar',
837                     'len' => '255',
838                     ),
839                 'foobar' => array (
840                     'name' => 'foobar',
841                     'type' => 'int',
842                     ),
843                 ),
844             array()
845             );
846         
847         $res = $this->_db->compareFieldInTables(
848             'foobar', $tablename1, $tablename2);
849         
850         $this->assertEquals($res['msg'],'no_match');
851         
852         $this->_db->alterColumn(
853             $tablename2,
854             array(
855                 'foobar' => array (
856                     'name' => 'foobar',
857                     'type' => 'varchar',
858                     'len' => '255',
859                     'required' => true,
860                     )
861                 )
862             );
863         
864         $res = $this->_db->compareFieldInTables(
865             'foobar', $tablename1, $tablename2);
866         
867         $this->assertEquals($res['msg'],'match');
868         
869         $this->_db->dropTableName($tablename1);
870         $this->_db->dropTableName($tablename2);
871     }
872     
873     public function testDropTable()
874     {
875         // TODO: Write this test
876     }
877     
878     public function testDropTableName()
879     {
880         $tablename = 'test' . date("YmdHis");
881         $this->_db->createTableParams($tablename,
882             array(
883                 'foo' => array (
884                     'name' => 'foo',
885                     'type' => 'varchar',
886                     'len' => '255',
887                     ),
888                 ),
889             array()
890             );
891         $this->assertTrue(in_array($tablename,$this->_db->getTablesArray()));
892         
893         $this->_db->dropTableName($tablename);
894         
895         $this->assertFalse(in_array($tablename,$this->_db->getTablesArray()));
896     }
897     
898     public function testDeleteColumn()
899     {
900         // TODO: Write this test
901     }
902     
903     public function testDisconnectAll()
904     {
905         $this->_db->disconnectAll();
906         $this->assertTrue($this->_db->checkError());
907         $this->_db = &DBManagerFactory::getInstance();
908     }
909         
910     public function testQuote()
911     {
912         $string = "'dog eat ";
913         
914         if ( $this->_db->dbType == 'mysql')
915             $this->assertEquals($this->_db->quoteForEmail($string),"\'dog eat ");
916         else
917             $this->assertEquals($this->_db->quoteForEmail($string),"''dog eat ");
918     }
919     
920     public function testQuoteForEmail()
921     {
922         $string = "'dog eat ";
923         
924         if ( $this->_db->dbType == 'mysql')
925             $this->assertEquals($this->_db->quoteForEmail($string),"\'dog eat ");
926         else
927             $this->assertEquals($this->_db->quoteForEmail($string),"''dog eat ");
928     }
929     
930     public function testArrayQuote()
931     {
932         $string = array("'dog eat ");
933         $this->_db->arrayQuote($string);
934         if ( $this->_db->dbType == 'mysql')
935             $this->assertEquals($string,array("\'dog eat "));
936         else
937             $this->assertEquals($string,array("''dog eat "));
938     }
939     
940     public function testQuery()
941     {
942         $beanIds = $this->_createRecords(5);
943         
944         $result = $this->_db->query("SELECT id From contacts where last_name = 'foobar'");
945         if ( $this->_db instanceOf MysqliManager )
946             $this->assertType('Mysqli_result',$result);
947         else
948             $this->assertTrue(is_resource($result));
949         
950         while ( $row = $this->_db->fetchByAssoc($result) ) 
951             $this->assertTrue(in_array($row['id'],$beanIds),"Id not found '{$row['id']}'");
952         
953         $this->_removeRecords($beanIds);
954     }
955     
956     public function disabledLimitQuery()
957     {
958         $beanIds = $this->_createRecords(5);
959         $_REQUEST['module'] = 'contacts';
960         $result = $this->_db->limitQuery("SELECT id From contacts where last_name = 'foobar'",1,3);
961         if ( $this->_db instanceOf MysqliManager )
962             $this->assertType('Mysqli_result',$result);
963         else
964             $this->assertTrue(is_resource($result));
965         
966         while ( $row = $this->_db->fetchByAssoc($result) ) {
967             if ( $row['id'][0] > 3 || $row['id'][0] < 0 )
968                 $this->assertFalse(in_array($row['id'],$beanIds),"Found {$row['id']} in error");
969             else
970                 $this->assertTrue(in_array($row['id'],$beanIds),"Didn't find {$row['id']}");
971         }
972         unset($_REQUEST['module']);
973         $this->_removeRecords($beanIds);
974     }
975     
976     public function testGetOne()
977     {
978         $beanIds = $this->_createRecords(1);
979         
980         $id = $this->_db->getOne("SELECT id From contacts where last_name = 'foobar'");
981         $this->assertEquals($id,$beanIds[0]);
982         
983         $this->_removeRecords($beanIds);
984     }
985     
986     public function testGetFieldsArray()
987     {
988         $beanIds = $this->_createRecords(1);
989         
990         $result = $this->_db->query("SELECT id From contacts where id = '{$beanIds[0]}'");
991         $fields = $this->_db->getFieldsArray($result,true);
992         
993         $this->assertEquals(array("id"),$fields);
994         
995         $this->_removeRecords($beanIds);
996     }
997     
998     public function testGetRowCount()
999     {
1000         $beanIds = $this->_createRecords(1);
1001         
1002         $result = $this->_db->query("SELECT id From contacts where id = '{$beanIds[0]}'");
1003         
1004         $this->assertEquals($this->_db->getRowCount($result),1);
1005         
1006         $this->_removeRecords($beanIds);
1007     }
1008     
1009     public function testGetAffectedRowCount()
1010     {
1011         if ( ($this->_db instanceOf MysqliManager) )
1012             $this->markTestSkipped('Skipping on Mysqli; doesn\'t apply to this backend');
1013         
1014         $beanIds = $this->_createRecords(1);
1015         $result = $this->_db->query("DELETE From contacts where id = '{$beanIds[0]}'");
1016         $this->assertEquals($this->_db->getAffectedRowCount(),1);
1017     }
1018     
1019     public function testFetchByAssoc()
1020     {
1021         $beanIds = $this->_createRecords(1);
1022         
1023         $result = $this->_db->query("SELECT id From contacts where id = '{$beanIds[0]}'");
1024         
1025         $row = $this->_db->fetchByAssoc($result);
1026         
1027         $this->assertTrue(is_array($row));
1028         $this->assertEquals($row['id'],$beanIds[0]);
1029         
1030         $this->_removeRecords($beanIds);
1031     }
1032     
1033     public function testConnect()
1034     {
1035         // TODO: Write this test
1036     }
1037     
1038     public function testDisconnect()
1039     {
1040         $this->_db->disconnect();
1041         $this->assertTrue($this->_db->checkError());
1042         $this->_db = &DBManagerFactory::getInstance();
1043     }
1044     
1045     public function testGetTablesArray()
1046     {
1047         $tablename = 'test' . date("YmdHis");
1048         $this->_db->createTableParams($tablename,
1049             array(
1050                 'foo' => array (
1051                     'name' => 'foo',
1052                     'type' => 'varchar',
1053                     'len' => '255',
1054                     ),
1055                 ),
1056             array()
1057             );
1058         
1059         $this->assertTrue($this->_db->tableExists($tablename));
1060         
1061         $this->_db->dropTableName($tablename);
1062     }
1063     
1064     public function testVersion()
1065     {
1066         $ver = $this->_db->version();
1067         
1068         $this->assertTrue(is_string($ver));
1069     }
1070     
1071     public function testTableExists()
1072     {
1073         $tablename = 'test' . date("YmdHis");
1074         $this->_db->createTableParams($tablename,
1075             array(
1076                 'foo' => array (
1077                     'name' => 'foo',
1078                     'type' => 'varchar',
1079                     'len' => '255',
1080                     ),
1081                 ),
1082             array()
1083             );
1084         
1085         $this->assertTrue(in_array($tablename,$this->_db->getTablesArray()));
1086         
1087         $this->_db->dropTableName($tablename);
1088     }
1089     
1090     public function providerCompareVardefs()
1091     {
1092         $returnArray = array(
1093             array(
1094                 array(
1095                     'name' => 'foo',
1096                     'type' => 'varchar',
1097                     'len' => '255',
1098                     ),
1099                 array(
1100                     'name' => 'foo',
1101                     'type' => 'varchar',
1102                     'len' => '255',
1103                     ),
1104                 true),
1105             array(
1106                 array(
1107                     'name' => 'foo',
1108                     'type' => 'char',
1109                     'len' => '255',
1110                     ),
1111                 array(
1112                     'name' => 'foo',
1113                     'type' => 'varchar',
1114                     'len' => '255',
1115                     ),
1116                 false),
1117             array(
1118                 array(
1119                     'name' => 'foo',
1120                     'type' => 'char',
1121                     'len' => '255',
1122                     ),
1123                 array(
1124                     'name' => 'foo',
1125                     'len' => '255',
1126                 ),
1127                 false),
1128             array(
1129                 array(
1130                     'name' => 'foo',
1131                     'len' => '255',
1132                     ),
1133                 array(
1134                     'name' => 'foo',
1135                     'type' => 'varchar',
1136                     'len' => '255',
1137                     ),
1138                 true),
1139             array(
1140                 array(
1141                     'name' => 'foo',
1142                     'type' => 'varchar',
1143                     'len' => '255',
1144                     ),
1145                 array(
1146                     'name' => 'FOO',
1147                     'type' => 'varchar',
1148                     'len' => '255',
1149                     ), 
1150                 true),
1151             );
1152         
1153         return $returnArray;
1154     }
1155     
1156     /**
1157      * @dataProvider providerCompareVarDefs
1158      */
1159     public function testCompareVarDefs($fieldDef1,$fieldDef2,$expectedResult)
1160     {
1161         if ( $expectedResult ) {
1162             $this->assertTrue($this->_db->compareVarDefs($fieldDef1,$fieldDef2));
1163         }
1164         else {
1165             $this->assertFalse($this->_db->compareVarDefs($fieldDef1,$fieldDef2));
1166         }
1167     }
1168     
1169     public function providerConvert()
1170     {
1171         $db = DBManagerFactory::getInstance();
1172         
1173         $returnArray = array(
1174             array(
1175                 array('foo','nothing'),
1176                 'foo'
1177                 )
1178             );
1179         if ( $db instanceOf MysqlManager )
1180             $returnArray += array(
1181                 array(
1182                     array('foo','today'),
1183                     'CURDATE()'
1184                     ),
1185                 array(
1186                     array('foo','left'),
1187                     'LEFT(foo)'
1188                 ),
1189             array(
1190                     array('foo','left',array('1','2','3')),
1191                     'LEFT(foo,1,2,3)'
1192                     ),
1193                 array(
1194                     array('foo','date_format'),
1195                     'DATE_FORMAT(foo)'
1196                         ),
1197                 array(
1198                     array('foo','date_format',array('1','2','3')),
1199                     'DATE_FORMAT(foo,1,2,3)'
1200                     ),
1201                 array(
1202                     array('foo','datetime',array("'%Y-%m'")),
1203                     'DATE_FORMAT(foo, \'%Y-%m-%d %H:%i:%s\')'
1204                         ),
1205                 array(
1206                     array('foo','IFNULL'),
1207                     'IFNULL(foo)'
1208                     ),
1209                 array(
1210                     array('foo','IFNULL',array('1','2','3')),
1211                     'IFNULL(foo,1,2,3)'
1212                     ),
1213                 array(
1214                     array('foo','CONCAT',array('1','2','3')),
1215                     'CONCAT(foo,1,2,3)'
1216                     ),
1217                 array(
1218                     array('foo','text2char'),
1219                     'foo'
1220                 ),
1221             );
1222         if ( $db instanceOf MssqlManager )
1223             $returnArray += array(
1224                 array(
1225                     array('foo','today'),
1226                     'GETDATE()'
1227                     ),
1228                 array(
1229                     array('foo','left'),
1230                     'LEFT(foo)'
1231                     ),
1232                 array(
1233                     array('foo','left',array('1','2','3')),
1234                     'LEFT(foo,1,2,3)'
1235                     ),
1236                 array(
1237                     array('foo','date_format'),
1238                     'CONVERT(varchar(10),foo,120)'
1239                     ),
1240                 array(
1241                     array('foo','date_format',array('1','2','3')),
1242                     'CONVERT(varchar(10),foo,120)'
1243                     ),
1244                 array(
1245                     array('foo','date_format',array("'%Y-%m'")),
1246                     'CONVERT(varchar(7),foo,120)'
1247                     ),
1248                 array(
1249                     array('foo','IFNULL'),
1250                     'ISNULL(foo)'
1251                     ),
1252                 array(
1253                     array('foo','IFNULL',array('1','2','3')),
1254                     'ISNULL(foo,1,2,3)'
1255                     ),
1256                 array(
1257                     array('foo','CONCAT',array('1','2','3')),
1258                     'foo+1+2+3'
1259                     ),
1260                 array(
1261                     array('foo','text2char'),
1262                     'CAST(foo AS varchar(8000))'
1263                     ),
1264                 );
1265         if ( $db instanceOf SqlsrvManager )
1266             $returnArray += array(
1267                 array(
1268                     array('foo','datetime'),
1269                     'CONVERT(varchar(20),foo,120)'
1270                     ),
1271                 );
1272         
1273         return $returnArray;
1274     }
1275     
1276     /**
1277      * @group bug33283
1278      * @dataProvider providerConvert
1279      */
1280     public function testConvert(
1281          array $parameters,
1282          $result
1283         )
1284     {
1285          if ( count($parameters) < 3 )
1286              $this->assertEquals(
1287                  $this->_db->convert($parameters[0],$parameters[1]),
1288                  $result);
1289          elseif ( count($parameters) < 4 )
1290              $this->assertEquals(
1291                  $this->_db->convert($parameters[0],$parameters[1],$parameters[2]),
1292                  $result);
1293         else
1294             $this->assertEquals(
1295                  $this->_db->convert($parameters[0],$parameters[1],$parameters[2],$parameters[3]),
1296                  $result);
1297      }
1298      
1299      /**
1300       * @group bug33283
1301       */
1302      public function testConcat()
1303      {
1304          $ret = $this->_db->concat('foo',array('col1','col2','col3'));
1305          
1306          if ( $this->_db instanceOf MysqlManager )
1307              $this->assertEquals($ret,
1308                  "CONCAT(IFNULL(foo.col1,''),' ',IFNULL(foo.col2,''),' ',IFNULL(foo.col3,''))"
1309                  );
1310          if ( $this->_db instanceOf MssqlManager )
1311              $this->assertEquals($ret,
1312                  "CONCAT(IFNULL(foo.col1,''),' ',IFNULL(foo.col2,''),' ',IFNULL(foo.col3,''))"
1313                  );
1314          if ( $this->_db instanceOf OracleManager )
1315              $this->assertEquals($ret,
1316                  "CONCAT(IFNULL(foo.col1,''),' ',IFNULL(foo.col2,''),' ',IFNULL(foo.col3,''))"
1317                  );
1318      }
1319      
1320      public function providerFromConvert()
1321      {
1322          $returnArray = array(
1323              array(
1324                  array('foo','nothing'),
1325                  'foo'
1326                  )
1327              );
1328          if ( $this->_db instanceOf MssqlManager 
1329                 || $this->_db instanceOf OracleManager )
1330              $returnArray += array(
1331                  array(
1332                      array('2009-01-01 12:00:00','date'),
1333                      '2009-01-01'
1334                      ),
1335                  array(
1336                      array('2009-01-01 12:00:00','time'),
1337                      '12:00:00'
1338                      )
1339                  );
1340          
1341          return $returnArray;
1342      }
1343      
1344      /**
1345       * @group bug33283
1346       * @dataProvider providerFromConvert
1347       */
1348      public function testFromConvert(
1349          array $parameters,
1350          $result
1351          )
1352      {
1353          $this->assertEquals(
1354              $this->_db->fromConvert($parameters[0],$parameters[1]),
1355              $result);
1356     }
1357     
1358     /**
1359      * @group bug34892
1360      */
1361     public function testMssqlNotClearingErrorResults()
1362     {
1363         if ( get_class($this->_db) != 'MssqlManager' )
1364             $this->markTestSkipped('Skipping; only applies with php_mssql driver');
1365         
1366         // execute a bad query
1367         $this->_db->query("select dsdsdsdsdsdsdsdsdsd");
1368         // assert it found an error
1369         $this->assertTrue($this->_db->checkError());
1370         // now, execute a good query
1371         $this->_db->query("select * from config");
1372         // and make no error messages are asserted
1373         $this->assertFalse($this->_db->checkError());
1374     }
1375 }