PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/include/database/DBManagerTest.php

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