PageRenderTime 127ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/cake/tests/cases/libs/model/datasources/dbo_source.test.php

https://github.com/msadouni/cakephp2x
PHP | 4087 lines | 2386 code | 612 blank | 1089 comment | 32 complexity | 629f8ded0e3acf81082e5c954b201760 MD5 | raw file
  1. <?php
  2. /**
  3. * DboSourceTest file
  4. *
  5. * Long description for file
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The Open Group Test Suite License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  17. * @package cake
  18. * @subpackage cake.tests.cases.libs.model.datasources
  19. * @since CakePHP(tm) v 1.2.0.4206
  20. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  21. */
  22. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  23. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  24. }
  25. App::import('Model', array('Model', 'DataSource', 'DboSource', 'DboMysql', 'App'));
  26. require_once dirname(dirname(__FILE__)) . DS . 'models.php';
  27. /**
  28. * TestModel class
  29. *
  30. * @package cake
  31. * @subpackage cake.tests.cases.libs.model.datasources
  32. */
  33. class TestModel extends CakeTestModel {
  34. /**
  35. * name property
  36. *
  37. * @var string 'TestModel'
  38. * @access public
  39. */
  40. var $name = 'TestModel';
  41. /**
  42. * useTable property
  43. *
  44. * @var bool false
  45. * @access public
  46. */
  47. var $useTable = false;
  48. /**
  49. * schema property
  50. *
  51. * @var array
  52. * @access protected
  53. */
  54. var $_schema = array(
  55. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  56. 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '11'),
  57. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  58. 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  59. 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  60. 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  61. 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
  62. 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  63. 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  64. 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  65. 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  66. 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  67. 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  68. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  69. 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => '155'),
  70. 'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  71. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  72. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  73. );
  74. /**
  75. * find method
  76. *
  77. * @param mixed $conditions
  78. * @param mixed $fields
  79. * @param mixed $order
  80. * @param mixed $recursive
  81. * @access public
  82. * @return void
  83. */
  84. function find($conditions = null, $fields = null, $order = null, $recursive = null) {
  85. return array($conditions, $fields);
  86. }
  87. /**
  88. * findAll method
  89. *
  90. * @param mixed $conditions
  91. * @param mixed $fields
  92. * @param mixed $order
  93. * @param mixed $recursive
  94. * @access public
  95. * @return void
  96. */
  97. function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
  98. return $conditions;
  99. }
  100. }
  101. /**
  102. * TestModel2 class
  103. *
  104. * @package cake
  105. * @subpackage cake.tests.cases.libs.model.datasources
  106. */
  107. class TestModel2 extends CakeTestModel {
  108. /**
  109. * name property
  110. *
  111. * @var string 'TestModel2'
  112. * @access public
  113. */
  114. var $name = 'TestModel2';
  115. /**
  116. * useTable property
  117. *
  118. * @var bool false
  119. * @access public
  120. */
  121. var $useTable = false;
  122. }
  123. /**
  124. * TestModel4 class
  125. *
  126. * @package cake
  127. * @subpackage cake.tests.cases.libs.model.datasources
  128. */
  129. class TestModel3 extends CakeTestModel {
  130. /**
  131. * name property
  132. *
  133. * @var string 'TestModel3'
  134. * @access public
  135. */
  136. var $name = 'TestModel3';
  137. /**
  138. * useTable property
  139. *
  140. * @var bool false
  141. * @access public
  142. */
  143. var $useTable = false;
  144. }
  145. /**
  146. * TestModel4 class
  147. *
  148. * @package cake
  149. * @subpackage cake.tests.cases.libs.model.datasources
  150. */
  151. class TestModel4 extends CakeTestModel {
  152. /**
  153. * name property
  154. *
  155. * @var string 'TestModel4'
  156. * @access public
  157. */
  158. var $name = 'TestModel4';
  159. /**
  160. * table property
  161. *
  162. * @var string 'test_model4'
  163. * @access public
  164. */
  165. var $table = 'test_model4';
  166. /**
  167. * useTable property
  168. *
  169. * @var bool false
  170. * @access public
  171. */
  172. var $useTable = false;
  173. /**
  174. * belongsTo property
  175. *
  176. * @var array
  177. * @access public
  178. */
  179. var $belongsTo = array(
  180. 'TestModel4Parent' => array(
  181. 'className' => 'TestModel4',
  182. 'foreignKey' => 'parent_id'
  183. )
  184. );
  185. /**
  186. * hasOne property
  187. *
  188. * @var array
  189. * @access public
  190. */
  191. var $hasOne = array(
  192. 'TestModel5' => array(
  193. 'className' => 'TestModel5',
  194. 'foreignKey' => 'test_model4_id'
  195. )
  196. );
  197. /**
  198. * hasAndBelongsToMany property
  199. *
  200. * @var array
  201. * @access public
  202. */
  203. var $hasAndBelongsToMany = array('TestModel7' => array(
  204. 'className' => 'TestModel7',
  205. 'joinTable' => 'test_model4_test_model7',
  206. 'foreignKey' => 'test_model4_id',
  207. 'associationForeignKey' => 'test_model7_id',
  208. 'with' => 'TestModel4TestModel7'
  209. ));
  210. /**
  211. * schema method
  212. *
  213. * @access public
  214. * @return void
  215. */
  216. function schema() {
  217. if (!isset($this->_schema)) {
  218. $this->_schema = array(
  219. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  220. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  221. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  222. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  223. );
  224. }
  225. return $this->_schema;
  226. }
  227. }
  228. /**
  229. * TestModel4TestModel7 class
  230. *
  231. * @package cake
  232. * @subpackage cake.tests.cases.libs.model.datasources
  233. */
  234. class TestModel4TestModel7 extends CakeTestModel {
  235. /**
  236. * name property
  237. *
  238. * @var string 'TestModel4TestModel7'
  239. * @access public
  240. */
  241. var $name = 'TestModel4TestModel7';
  242. /**
  243. * table property
  244. *
  245. * @var string 'test_model4_test_model7'
  246. * @access public
  247. */
  248. var $table = 'test_model4_test_model7';
  249. /**
  250. * useTable property
  251. *
  252. * @var bool false
  253. * @access public
  254. */
  255. var $useTable = false;
  256. /**
  257. * schema method
  258. *
  259. * @access public
  260. * @return void
  261. */
  262. function schema() {
  263. if (!isset($this->_schema)) {
  264. $this->_schema = array(
  265. 'test_model4_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  266. 'test_model7_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8')
  267. );
  268. }
  269. return $this->_schema;
  270. }
  271. }
  272. /**
  273. * TestModel5 class
  274. *
  275. * @package cake
  276. * @subpackage cake.tests.cases.libs.model.datasources
  277. */
  278. class TestModel5 extends CakeTestModel {
  279. /**
  280. * name property
  281. *
  282. * @var string 'TestModel5'
  283. * @access public
  284. */
  285. var $name = 'TestModel5';
  286. /**
  287. * table property
  288. *
  289. * @var string 'test_model5'
  290. * @access public
  291. */
  292. var $table = 'test_model5';
  293. /**
  294. * useTable property
  295. *
  296. * @var bool false
  297. * @access public
  298. */
  299. var $useTable = false;
  300. /**
  301. * belongsTo property
  302. *
  303. * @var array
  304. * @access public
  305. */
  306. var $belongsTo = array('TestModel4' => array(
  307. 'className' => 'TestModel4',
  308. 'foreignKey' => 'test_model4_id'
  309. ));
  310. /**
  311. * hasMany property
  312. *
  313. * @var array
  314. * @access public
  315. */
  316. var $hasMany = array('TestModel6' => array(
  317. 'className' => 'TestModel6',
  318. 'foreignKey' => 'test_model5_id'
  319. ));
  320. /**
  321. * schema method
  322. *
  323. * @access public
  324. * @return void
  325. */
  326. function schema() {
  327. if (!isset($this->_schema)) {
  328. $this->_schema = array(
  329. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'test_model4_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  331. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  332. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  333. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  334. );
  335. }
  336. return $this->_schema;
  337. }
  338. }
  339. /**
  340. * TestModel6 class
  341. *
  342. * @package cake
  343. * @subpackage cake.tests.cases.libs.model.datasources
  344. */
  345. class TestModel6 extends CakeTestModel {
  346. /**
  347. * name property
  348. *
  349. * @var string 'TestModel6'
  350. * @access public
  351. */
  352. var $name = 'TestModel6';
  353. /**
  354. * table property
  355. *
  356. * @var string 'test_model6'
  357. * @access public
  358. */
  359. var $table = 'test_model6';
  360. /**
  361. * useTable property
  362. *
  363. * @var bool false
  364. * @access public
  365. */
  366. var $useTable = false;
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. * @access public
  372. */
  373. var $belongsTo = array('TestModel5' => array(
  374. 'className' => 'TestModel5',
  375. 'foreignKey' => 'test_model5_id'
  376. ));
  377. /**
  378. * schema method
  379. *
  380. * @access public
  381. * @return void
  382. */
  383. function schema() {
  384. if (!isset($this->_schema)) {
  385. $this->_schema = array(
  386. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  387. 'test_model5_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  388. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  389. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  390. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  391. );
  392. }
  393. return $this->_schema;
  394. }
  395. }
  396. /**
  397. * TestModel7 class
  398. *
  399. * @package cake
  400. * @subpackage cake.tests.cases.libs.model.datasources
  401. */
  402. class TestModel7 extends CakeTestModel {
  403. /**
  404. * name property
  405. *
  406. * @var string 'TestModel7'
  407. * @access public
  408. */
  409. var $name = 'TestModel7';
  410. /**
  411. * table property
  412. *
  413. * @var string 'test_model7'
  414. * @access public
  415. */
  416. var $table = 'test_model7';
  417. /**
  418. * useTable property
  419. *
  420. * @var bool false
  421. * @access public
  422. */
  423. var $useTable = false;
  424. /**
  425. * schema method
  426. *
  427. * @access public
  428. * @return void
  429. */
  430. function schema() {
  431. if (!isset($this->_schema)) {
  432. $this->_schema = array(
  433. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  434. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  435. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  436. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  437. );
  438. }
  439. return $this->_schema;
  440. }
  441. }
  442. /**
  443. * TestModel8 class
  444. *
  445. * @package cake
  446. * @subpackage cake.tests.cases.libs.model.datasources
  447. */
  448. class TestModel8 extends CakeTestModel {
  449. /**
  450. * name property
  451. *
  452. * @var string 'TestModel8'
  453. * @access public
  454. */
  455. var $name = 'TestModel8';
  456. /**
  457. * table property
  458. *
  459. * @var string 'test_model8'
  460. * @access public
  461. */
  462. var $table = 'test_model8';
  463. /**
  464. * useTable property
  465. *
  466. * @var bool false
  467. * @access public
  468. */
  469. var $useTable = false;
  470. /**
  471. * hasOne property
  472. *
  473. * @var array
  474. * @access public
  475. */
  476. var $hasOne = array(
  477. 'TestModel9' => array(
  478. 'className' => 'TestModel9',
  479. 'foreignKey' => 'test_model8_id',
  480. 'conditions' => 'TestModel9.name != \'mariano\''
  481. )
  482. );
  483. /**
  484. * schema method
  485. *
  486. * @access public
  487. * @return void
  488. */
  489. function schema() {
  490. if (!isset($this->_schema)) {
  491. $this->_schema = array(
  492. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  493. 'test_model9_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  495. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  496. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  497. );
  498. }
  499. return $this->_schema;
  500. }
  501. }
  502. /**
  503. * TestModel9 class
  504. *
  505. * @package cake
  506. * @subpackage cake.tests.cases.libs.model.datasources
  507. */
  508. class TestModel9 extends CakeTestModel {
  509. /**
  510. * name property
  511. *
  512. * @var string 'TestModel9'
  513. * @access public
  514. */
  515. var $name = 'TestModel9';
  516. /**
  517. * table property
  518. *
  519. * @var string 'test_model9'
  520. * @access public
  521. */
  522. var $table = 'test_model9';
  523. /**
  524. * useTable property
  525. *
  526. * @var bool false
  527. * @access public
  528. */
  529. var $useTable = false;
  530. /**
  531. * belongsTo property
  532. *
  533. * @var array
  534. * @access public
  535. */
  536. var $belongsTo = array('TestModel8' => array(
  537. 'className' => 'TestModel8',
  538. 'foreignKey' => 'test_model8_id',
  539. 'conditions' => 'TestModel8.name != \'larry\''
  540. ));
  541. /**
  542. * schema method
  543. *
  544. * @access public
  545. * @return void
  546. */
  547. function schema() {
  548. if (!isset($this->_schema)) {
  549. $this->_schema = array(
  550. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  551. 'test_model8_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '11'),
  552. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  553. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  554. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  555. );
  556. }
  557. return $this->_schema;
  558. }
  559. }
  560. /**
  561. * Level class
  562. *
  563. * @package cake
  564. * @subpackage cake.tests.cases.libs.model.datasources
  565. */
  566. class Level extends CakeTestModel {
  567. /**
  568. * name property
  569. *
  570. * @var string 'Level'
  571. * @access public
  572. */
  573. var $name = 'Level';
  574. /**
  575. * table property
  576. *
  577. * @var string 'level'
  578. * @access public
  579. */
  580. var $table = 'level';
  581. /**
  582. * useTable property
  583. *
  584. * @var bool false
  585. * @access public
  586. */
  587. var $useTable = false;
  588. /**
  589. * hasMany property
  590. *
  591. * @var array
  592. * @access public
  593. */
  594. var $hasMany = array(
  595. 'Group'=> array(
  596. 'className' => 'Group'
  597. ),
  598. 'User2' => array(
  599. 'className' => 'User2'
  600. )
  601. );
  602. /**
  603. * schema method
  604. *
  605. * @access public
  606. * @return void
  607. */
  608. function schema() {
  609. if (!isset($this->_schema)) {
  610. $this->_schema = array(
  611. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  612. 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'),
  613. );
  614. }
  615. return $this->_schema;
  616. }
  617. }
  618. /**
  619. * Group class
  620. *
  621. * @package cake
  622. * @subpackage cake.tests.cases.libs.model.datasources
  623. */
  624. class Group extends CakeTestModel {
  625. /**
  626. * name property
  627. *
  628. * @var string 'Group'
  629. * @access public
  630. */
  631. var $name = 'Group';
  632. /**
  633. * table property
  634. *
  635. * @var string 'group'
  636. * @access public
  637. */
  638. var $table = 'group';
  639. /**
  640. * useTable property
  641. *
  642. * @var bool false
  643. * @access public
  644. */
  645. var $useTable = false;
  646. /**
  647. * belongsTo property
  648. *
  649. * @var array
  650. * @access public
  651. */
  652. var $belongsTo = array('Level');
  653. /**
  654. * hasMany property
  655. *
  656. * @var array
  657. * @access public
  658. */
  659. var $hasMany = array('Category2', 'User2');
  660. /**
  661. * schema method
  662. *
  663. * @access public
  664. * @return void
  665. */
  666. function schema() {
  667. if (!isset($this->_schema)) {
  668. $this->_schema = array(
  669. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  670. 'level_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  671. 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'),
  672. );
  673. }
  674. return $this->_schema;
  675. }
  676. }
  677. /**
  678. * User2 class
  679. *
  680. * @package cake
  681. * @subpackage cake.tests.cases.libs.model.datasources
  682. */
  683. class User2 extends CakeTestModel {
  684. /**
  685. * name property
  686. *
  687. * @var string 'User2'
  688. * @access public
  689. */
  690. var $name = 'User2';
  691. /**
  692. * table property
  693. *
  694. * @var string 'user'
  695. * @access public
  696. */
  697. var $table = 'user';
  698. /**
  699. * useTable property
  700. *
  701. * @var bool false
  702. * @access public
  703. */
  704. var $useTable = false;
  705. /**
  706. * belongsTo property
  707. *
  708. * @var array
  709. * @access public
  710. */
  711. var $belongsTo = array(
  712. 'Group' => array(
  713. 'className' => 'Group'
  714. ),
  715. 'Level' => array(
  716. 'className' => 'Level'
  717. )
  718. );
  719. /**
  720. * hasMany property
  721. *
  722. * @var array
  723. * @access public
  724. */
  725. var $hasMany = array(
  726. 'Article2' => array(
  727. 'className' => 'Article2'
  728. ),
  729. );
  730. /**
  731. * schema method
  732. *
  733. * @access public
  734. * @return void
  735. */
  736. function schema() {
  737. if (!isset($this->_schema)) {
  738. $this->_schema = array(
  739. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  740. 'group_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  741. 'level_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  742. 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'),
  743. );
  744. }
  745. return $this->_schema;
  746. }
  747. }
  748. /**
  749. * Category2 class
  750. *
  751. * @package cake
  752. * @subpackage cake.tests.cases.libs.model.datasources
  753. */
  754. class Category2 extends CakeTestModel {
  755. /**
  756. * name property
  757. *
  758. * @var string 'Category2'
  759. * @access public
  760. */
  761. var $name = 'Category2';
  762. /**
  763. * table property
  764. *
  765. * @var string 'category'
  766. * @access public
  767. */
  768. var $table = 'category';
  769. /**
  770. * useTable property
  771. *
  772. * @var bool false
  773. * @access public
  774. */
  775. var $useTable = false;
  776. /**
  777. * belongsTo property
  778. *
  779. * @var array
  780. * @access public
  781. */
  782. var $belongsTo = array(
  783. 'Group' => array(
  784. 'className' => 'Group',
  785. 'foreignKey' => 'group_id'
  786. ),
  787. 'ParentCat' => array(
  788. 'className' => 'Category2',
  789. 'foreignKey' => 'parent_id'
  790. )
  791. );
  792. /**
  793. * hasMany property
  794. *
  795. * @var array
  796. * @access public
  797. */
  798. var $hasMany = array(
  799. 'ChildCat' => array(
  800. 'className' => 'Category2',
  801. 'foreignKey' => 'parent_id'
  802. ),
  803. 'Article2' => array(
  804. 'className' => 'Article2',
  805. 'order'=>'Article2.published_date DESC',
  806. 'foreignKey' => 'category_id',
  807. 'limit'=>'3')
  808. );
  809. /**
  810. * schema method
  811. *
  812. * @access public
  813. * @return void
  814. */
  815. function schema() {
  816. if (!isset($this->_schema)) {
  817. $this->_schema = array(
  818. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'),
  819. 'group_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'),
  820. 'parent_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'),
  821. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  822. 'icon' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  823. 'description' => array('type' => 'text', 'null' => false, 'default' => '', 'length' => null),
  824. );
  825. }
  826. return $this->_schema;
  827. }
  828. }
  829. /**
  830. * Article2 class
  831. *
  832. * @package cake
  833. * @subpackage cake.tests.cases.libs.model.datasources
  834. */
  835. class Article2 extends CakeTestModel {
  836. /**
  837. * name property
  838. *
  839. * @var string 'Article2'
  840. * @access public
  841. */
  842. var $name = 'Article2';
  843. /**
  844. * table property
  845. *
  846. * @var string 'article'
  847. * @access public
  848. */
  849. var $table = 'article';
  850. /**
  851. * useTable property
  852. *
  853. * @var bool false
  854. * @access public
  855. */
  856. var $useTable = false;
  857. /**
  858. * belongsTo property
  859. *
  860. * @var array
  861. * @access public
  862. */
  863. var $belongsTo = array(
  864. 'Category2' => array('className' => 'Category2'),
  865. 'User2' => array('className' => 'User2')
  866. );
  867. /**
  868. * schema method
  869. *
  870. * @access public
  871. * @return void
  872. */
  873. function schema() {
  874. if (!isset($this->_schema)) {
  875. $this->_schema = array(
  876. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'),
  877. 'category_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  878. 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  879. 'rate_count' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  880. 'rate_sum' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  881. 'viewed' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  882. 'version' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => '45'),
  883. 'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '200'),
  884. 'intro' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null),
  885. 'comments' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '4'),
  886. 'body' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null),
  887. 'isdraft' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'),
  888. 'allow_comments' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'length' => '1'),
  889. 'moderate_comments' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'length' => '1'),
  890. 'published' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'),
  891. 'multipage' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'),
  892. 'published_date' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null),
  893. 'created' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null),
  894. 'modified' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null)
  895. );
  896. }
  897. return $this->_schema;
  898. }
  899. }
  900. /**
  901. * CategoryFeatured2 class
  902. *
  903. * @package cake
  904. * @subpackage cake.tests.cases.libs.model.datasources
  905. */
  906. class CategoryFeatured2 extends CakeTestModel {
  907. /**
  908. * name property
  909. *
  910. * @var string 'CategoryFeatured2'
  911. * @access public
  912. */
  913. var $name = 'CategoryFeatured2';
  914. /**
  915. * table property
  916. *
  917. * @var string 'category_featured'
  918. * @access public
  919. */
  920. var $table = 'category_featured';
  921. /**
  922. * useTable property
  923. *
  924. * @var bool false
  925. * @access public
  926. */
  927. var $useTable = false;
  928. /**
  929. * schema method
  930. *
  931. * @access public
  932. * @return void
  933. */
  934. function schema() {
  935. if (!isset($this->_schema)) {
  936. $this->_schema = array(
  937. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'),
  938. 'parent_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'),
  939. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  940. 'icon' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  941. 'description' => array('text' => 'string', 'null' => false, 'default' => '', 'length' => null)
  942. );
  943. }
  944. return $this->_schema;
  945. }
  946. }
  947. /**
  948. * Featured2 class
  949. *
  950. * @package cake
  951. * @subpackage cake.tests.cases.libs.model.datasources
  952. */
  953. class Featured2 extends CakeTestModel {
  954. /**
  955. * name property
  956. *
  957. * @var string 'Featured2'
  958. * @access public
  959. */
  960. var $name = 'Featured2';
  961. /**
  962. * table property
  963. *
  964. * @var string 'featured2'
  965. * @access public
  966. */
  967. var $table = 'featured2';
  968. /**
  969. * useTable property
  970. *
  971. * @var bool false
  972. * @access public
  973. */
  974. var $useTable = false;
  975. /**
  976. * belongsTo property
  977. *
  978. * @var array
  979. * @access public
  980. */
  981. var $belongsTo = array(
  982. 'CategoryFeatured2' => array(
  983. 'className' => 'CategoryFeatured2'
  984. )
  985. );
  986. /**
  987. * schema method
  988. *
  989. * @access public
  990. * @return void
  991. */
  992. function schema() {
  993. if (!isset($this->_schema)) {
  994. $this->_schema = array(
  995. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  996. 'article_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  997. 'category_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  998. 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20')
  999. );
  1000. }
  1001. return $this->_schema;
  1002. }
  1003. }
  1004. /**
  1005. * Comment2 class
  1006. *
  1007. * @package cake
  1008. * @subpackage cake.tests.cases.libs.model.datasources
  1009. */
  1010. class Comment2 extends CakeTestModel {
  1011. /**
  1012. * name property
  1013. *
  1014. * @var string 'Comment2'
  1015. * @access public
  1016. */
  1017. var $name = 'Comment2';
  1018. /**
  1019. * table property
  1020. *
  1021. * @var string 'comment'
  1022. * @access public
  1023. */
  1024. var $table = 'comment';
  1025. /**
  1026. * belongsTo property
  1027. *
  1028. * @var array
  1029. * @access public
  1030. */
  1031. var $belongsTo = array('ArticleFeatured2', 'User2');
  1032. /**
  1033. * useTable property
  1034. *
  1035. * @var bool false
  1036. * @access public
  1037. */
  1038. var $useTable = false;
  1039. /**
  1040. * schema method
  1041. *
  1042. * @access public
  1043. * @return void
  1044. */
  1045. function schema() {
  1046. if (!isset($this->_schema)) {
  1047. $this->_schema = array(
  1048. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  1049. 'article_featured_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  1050. 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  1051. 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20')
  1052. );
  1053. }
  1054. return $this->_schema;
  1055. }
  1056. }
  1057. /**
  1058. * ArticleFeatured2 class
  1059. *
  1060. * @package cake
  1061. * @subpackage cake.tests.cases.libs.model.datasources
  1062. */
  1063. class ArticleFeatured2 extends CakeTestModel {
  1064. /**
  1065. * name property
  1066. *
  1067. * @var string 'ArticleFeatured2'
  1068. * @access public
  1069. */
  1070. var $name = 'ArticleFeatured2';
  1071. /**
  1072. * table property
  1073. *
  1074. * @var string 'article_featured'
  1075. * @access public
  1076. */
  1077. var $table = 'article_featured';
  1078. /**
  1079. * useTable property
  1080. *
  1081. * @var bool false
  1082. * @access public
  1083. */
  1084. var $useTable = false;
  1085. /**
  1086. * belongsTo property
  1087. *
  1088. * @var array
  1089. * @access public
  1090. */
  1091. var $belongsTo = array(
  1092. 'CategoryFeatured2' => array('className' => 'CategoryFeatured2'),
  1093. 'User2' => array('className' => 'User2')
  1094. );
  1095. /**
  1096. * hasOne property
  1097. *
  1098. * @var array
  1099. * @access public
  1100. */
  1101. var $hasOne = array(
  1102. 'Featured2' => array('className' => 'Featured2')
  1103. );
  1104. /**
  1105. * hasMany property
  1106. *
  1107. * @var array
  1108. * @access public
  1109. */
  1110. var $hasMany = array(
  1111. 'Comment2' => array('className'=>'Comment2', 'dependent' => true)
  1112. );
  1113. /**
  1114. * schema method
  1115. *
  1116. * @access public
  1117. * @return void
  1118. */
  1119. function schema() {
  1120. if (!isset($this->_schema)) {
  1121. $this->_schema = array(
  1122. 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'),
  1123. 'category_featured_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  1124. 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'),
  1125. 'title' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'),
  1126. 'body' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null),
  1127. 'published' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'),
  1128. 'published_date' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null),
  1129. 'created' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null),
  1130. 'modified' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null)
  1131. );
  1132. }
  1133. return $this->_schema;
  1134. }
  1135. }
  1136. /**
  1137. * DboSourceTest class
  1138. *
  1139. * @package cake
  1140. * @subpackage cake.tests.cases.libs.model.datasources
  1141. */
  1142. class DboSourceTest extends CakeTestCase {
  1143. /**
  1144. * debug property
  1145. *
  1146. * @var mixed null
  1147. * @access public
  1148. */
  1149. var $debug = null;
  1150. /**
  1151. * autoFixtures property
  1152. *
  1153. * @var bool false
  1154. * @access public
  1155. */
  1156. var $autoFixtures = false;
  1157. /**
  1158. * fixtures property
  1159. *
  1160. * @var array
  1161. * @access public
  1162. */
  1163. var $fixtures = array(
  1164. 'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
  1165. 'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author'
  1166. );
  1167. /**
  1168. * startTest method
  1169. *
  1170. * @access public
  1171. * @return void
  1172. */
  1173. function startTest() {
  1174. $this->__config = $this->db->config;
  1175. if (!class_exists('DboTest')) {
  1176. $db = ConnectionManager::getDataSource('test_suite');
  1177. $class = get_class($db);
  1178. eval("class DboTest extends $class {
  1179. var \$simulated = array();
  1180. /**
  1181. * execute method
  1182. *
  1183. * @param \$sql
  1184. * @access protected
  1185. * @return void
  1186. */
  1187. function _execute(\$sql) {
  1188. \$this->simulated[] = \$sql;
  1189. return null;
  1190. }
  1191. /**
  1192. * getLastQuery method
  1193. *
  1194. * @access public
  1195. * @return void
  1196. */
  1197. function getLastQuery() {
  1198. return \$this->simulated[count(\$this->simulated) - 1];
  1199. }
  1200. }");
  1201. }
  1202. $this->testDb = new DboTest($this->__config);
  1203. $this->testDb->cacheSources = false;
  1204. $this->testDb->startQuote = '`';
  1205. $this->testDb->endQuote = '`';
  1206. Configure::write('debug', 1);
  1207. $this->debug = Configure::read('debug');
  1208. $this->Model = new TestModel();
  1209. }
  1210. /**
  1211. * endTest method
  1212. *
  1213. * @access public
  1214. * @return void
  1215. */
  1216. function endTest() {
  1217. unset($this->Model);
  1218. Configure::write('debug', $this->debug);
  1219. unset($this->debug);
  1220. }
  1221. /**
  1222. * testFieldDoubleEscaping method
  1223. *
  1224. * @access public
  1225. * @return void
  1226. */
  1227. function testFieldDoubleEscaping() {
  1228. $config = array_merge($this->__config, array('driver' => 'test'));
  1229. $test = ConnectionManager::create('quoteTest', $config);
  1230. $test->simulated = array();
  1231. $this->Model = new Article2(array('alias' => 'Article', 'ds' => 'quoteTest'));
  1232. $this->Model->setDataSource('quoteTest');
  1233. $this->assertEqual($this->Model->escapeField(), '`Article`.`id`');
  1234. $result = $test->fields($this->Model, null, $this->Model->escapeField());
  1235. $this->assertEqual($result, array('`Article`.`id`'));
  1236. $result = $test->read($this->Model, array(
  1237. 'fields' => $this->Model->escapeField(),
  1238. 'conditions' => null,
  1239. 'recursive' => -1
  1240. ));
  1241. $this->assertEqual(trim($test->simulated[0]), 'SELECT `Article`.`id` FROM `' . $this->testDb->fullTableName('article', false) . '` AS `Article` WHERE 1 = 1');
  1242. $test->startQuote = '[';
  1243. $test->endQuote = ']';
  1244. $this->assertEqual($this->Model->escapeField(), '[Article].[id]');
  1245. $result = $test->fields($this->Model, null, $this->Model->escapeField());
  1246. $this->assertEqual($result, array('[Article].[id]'));
  1247. $result = $test->read($this->Model, array(
  1248. 'fields' => $this->Model->escapeField(),
  1249. 'conditions' => null,
  1250. 'recursive' => -1
  1251. ));
  1252. $this->assertEqual(trim($test->simulated[1]), 'SELECT [Article].[id] FROM [' . $this->testDb->fullTableName('article', false) . '] AS [Article] WHERE 1 = 1');
  1253. ClassRegistry::removeObject('Article');
  1254. }
  1255. /**
  1256. * testGenerateAssociationQuerySelfJoin method
  1257. *
  1258. * @access public
  1259. * @return void
  1260. */
  1261. function testGenerateAssociationQuerySelfJoin() {
  1262. $this->startTime = microtime(true);
  1263. $this->Model = new Article2();
  1264. $this->_buildRelatedModels($this->Model);
  1265. $this->_buildRelatedModels($this->Model->Category2);
  1266. $this->Model->Category2->ChildCat = new Category2();
  1267. $this->Model->Category2->ParentCat = new Category2();
  1268. $queryData = array();
  1269. foreach ($this->Model->Category2->__associations as $type) {
  1270. foreach ($this->Model->Category2->{$type} as $assoc => $assocData) {
  1271. $linkModel = $this->Model->Category2->{$assoc};
  1272. $external = isset($assocData['external']);
  1273. if ($this->Model->Category2->alias == $linkModel->alias && $type != 'hasAndBelongsToMany' && $type != 'hasMany') {
  1274. $result = $this->testDb->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
  1275. $this->assertTrue($result);
  1276. } else {
  1277. if ($this->Model->Category2->useDbConfig == $linkModel->useDbConfig) {
  1278. $result = $this->testDb->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
  1279. $this->assertTrue($result);
  1280. }
  1281. }
  1282. }
  1283. }
  1284. $query = $this->testDb->generateAssociationQuery($this->Model->Category2, $null, null, null, null, $queryData, false, $null);
  1285. $this->assertPattern('/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/', $query);
  1286. $this->Model = new TestModel4();
  1287. $this->Model->schema();
  1288. $this->_buildRelatedModels($this->Model);
  1289. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
  1290. $queryData = array();
  1291. $resultSet = null;
  1292. $null = null;
  1293. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1294. $_queryData = $queryData;
  1295. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1296. $this->assertTrue($result);
  1297. $expected = array(
  1298. 'fields' => array(
  1299. '`TestModel4`.`id`',
  1300. '`TestModel4`.`name`',
  1301. '`TestModel4`.`created`',
  1302. '`TestModel4`.`updated`',
  1303. '`TestModel4Parent`.`id`',
  1304. '`TestModel4Parent`.`name`',
  1305. '`TestModel4Parent`.`created`',
  1306. '`TestModel4Parent`.`updated`'
  1307. ),
  1308. 'joins' => array(
  1309. array(
  1310. 'table' => '`test_model4`',
  1311. 'alias' => 'TestModel4Parent',
  1312. 'type' => 'LEFT',
  1313. 'conditions' => '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`'
  1314. )
  1315. ),
  1316. 'limit' => array(),
  1317. 'offset' => array(),
  1318. 'conditions' => array(),
  1319. 'order' => array(),
  1320. 'group' => null
  1321. );
  1322. $this->assertEqual($queryData, $expected);
  1323. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1324. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result);
  1325. $this->assertPattern('/FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model4` AS `TestModel4Parent`/', $result);
  1326. $this->assertPattern('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result);
  1327. $this->assertPattern('/\s+WHERE\s+1 = 1\s+$/', $result);
  1328. $params['assocData']['type'] = 'INNER';
  1329. $this->Model->belongsTo['TestModel4Parent']['type'] = 'INNER';
  1330. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $_queryData, $params['external'], $resultSet);
  1331. $this->assertTrue($result);
  1332. $this->assertEqual($_queryData['joins'][0]['type'], 'INNER');
  1333. }
  1334. /**
  1335. * testGenerateInnerJoinAssociationQuery method
  1336. *
  1337. * @access public
  1338. * @return void
  1339. */
  1340. function testGenerateInnerJoinAssociationQuery() {
  1341. $this->Model = new TestModel9();
  1342. $test = ConnectionManager::create('test2', $this->__config);
  1343. $this->Model->setDataSource('test2');
  1344. $this->Model->TestModel8 = new TestModel8();
  1345. $this->Model->TestModel8->setDataSource('test2');
  1346. $this->testDb->read($this->Model, array('recursive' => 1));
  1347. $result = $this->testDb->getLastQuery();
  1348. $this->assertPattern('/`TestModel9` LEFT JOIN `' . $this->testDb->fullTableName('test_model8', false) . '`/', $result);
  1349. $this->Model->belongsTo['TestModel8']['type'] = 'INNER';
  1350. $this->testDb->read($this->Model, array('recursive' => 1));
  1351. $result = $this->testDb->getLastQuery();
  1352. $this->assertPattern('/`TestModel9` INNER JOIN `' . $this->testDb->fullTableName('test_model8', false) . '`/', $result);
  1353. }
  1354. /**
  1355. * testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding method
  1356. *
  1357. * @access public
  1358. * @return void
  1359. */
  1360. function testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding() {
  1361. $this->Model = new TestModel8();
  1362. $this->Model->schema();
  1363. $this->_buildRelatedModels($this->Model);
  1364. $binding = array('type' => 'hasOne', 'model' => 'TestModel9');
  1365. $queryData = array();
  1366. $resultSet = null;
  1367. $null = null;
  1368. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1369. $_queryData = $queryData;
  1370. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1371. $this->assertTrue($result);
  1372. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1373. $this->assertPattern('/^SELECT\s+`TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`, `TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`\s+/', $result);
  1374. $this->assertPattern('/FROM\s+`test_model8` AS `TestModel8`\s+LEFT JOIN\s+`test_model9` AS `TestModel9`/', $result);
  1375. $this->assertPattern('/\s+ON\s+\(`TestModel9`\.`name` != \'mariano\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result);
  1376. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1377. }
  1378. /**
  1379. * testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding method
  1380. *
  1381. * @access public
  1382. * @return void
  1383. */
  1384. function testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding() {
  1385. $this->Model = new TestModel9();
  1386. $this->Model->schema();
  1387. $this->_buildRelatedModels($this->Model);
  1388. $binding = array('type' => 'belongsTo', 'model' => 'TestModel8');
  1389. $queryData = array();
  1390. $resultSet = null;
  1391. $null = null;
  1392. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1393. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1394. $this->assertTrue($result);
  1395. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1396. $this->assertPattern('/^SELECT\s+`TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`, `TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`\s+/', $result);
  1397. $this->assertPattern('/FROM\s+`test_model9` AS `TestModel9`\s+LEFT JOIN\s+`test_model8` AS `TestModel8`/', $result);
  1398. $this->assertPattern('/\s+ON\s+\(`TestModel8`\.`name` != \'larry\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result);
  1399. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1400. }
  1401. /**
  1402. * testGenerateAssociationQuerySelfJoinWithConditions method
  1403. *
  1404. * @access public
  1405. * @return void
  1406. */
  1407. function testGenerateAssociationQuerySelfJoinWithConditions() {
  1408. $this->Model = new TestModel4();
  1409. $this->Model->schema();
  1410. $this->_buildRelatedModels($this->Model);
  1411. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
  1412. $queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano'));
  1413. $resultSet = null;
  1414. $null = null;
  1415. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1416. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1417. $this->assertTrue($result);
  1418. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1419. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result);
  1420. $this->assertPattern('/FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model4` AS `TestModel4Parent`/', $result);
  1421. $this->assertPattern('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result);
  1422. $this->assertPattern('/\s+WHERE\s+(?:\()?`TestModel4Parent`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
  1423. $this->Featured2 = new Featured2();
  1424. $this->Featured2->schema();
  1425. $this->Featured2->bindModel(array(
  1426. 'belongsTo' => array(
  1427. 'ArticleFeatured2' => array(
  1428. 'conditions' => 'ArticleFeatured2.published = \'Y\'',
  1429. 'fields' => 'id, title, user_id, published'
  1430. )
  1431. )
  1432. ));
  1433. $this->_buildRelatedModels($this->Featured2);
  1434. $binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2');
  1435. $queryData = array('conditions' => array());
  1436. $resultSet = null;
  1437. $null = null;
  1438. $params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding);
  1439. $result = $this->testDb->generateAssociationQuery($this->Featured2, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1440. $this->assertTrue($result);
  1441. $result = $this->testDb->generateAssociationQuery($this->Featured2, $null, null, null, null, $queryData, false, $null);
  1442. $this->assertPattern(
  1443. '/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+'.
  1444. '`ArticleFeatured2`\.`id`, `ArticleFeatured2`\.`title`, `ArticleFeatured2`\.`user_id`, `ArticleFeatured2`\.`published`\s+' .
  1445. 'FROM\s+`featured2` AS `Featured2`\s+LEFT JOIN\s+`article_featured` AS `ArticleFeatured2`' .
  1446. '\s+ON\s+\(`ArticleFeatured2`.`published` = \'Y\'\s+AND\s+`Featured2`\.`article_featured2_id` = `ArticleFeatured2`\.`id`\)' .
  1447. '\s+WHERE\s+1\s+=\s+1\s*$/',
  1448. $result
  1449. );
  1450. }
  1451. /**
  1452. * testGenerateAssociationQueryHasOne method
  1453. *
  1454. * @access public
  1455. * @return void
  1456. */
  1457. function testGenerateAssociationQueryHasOne() {
  1458. $this->Model = new TestModel4();
  1459. $this->Model->schema();
  1460. $this->_buildRelatedModels($this->Model);
  1461. $binding = array('type' => 'hasOne', 'model' => 'TestModel5');
  1462. $queryData = array();
  1463. $resultSet = null;
  1464. $null = null;
  1465. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1466. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1467. $this->assertTrue($result);
  1468. $result = $this->testDb->buildJoinStatement($queryData['joins'][0]);
  1469. $expected = ' LEFT JOIN `test_model5` AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
  1470. $this->assertEqual(trim($result), trim($expected));
  1471. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1472. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1473. $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+/', $result);
  1474. $this->assertPattern('/`test_model5` AS `TestModel5`\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE/', $result);
  1475. $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
  1476. }
  1477. /**
  1478. * testGenerateAssociationQueryHasOneWithConditions method
  1479. *
  1480. * @access public
  1481. * @return void
  1482. */
  1483. function testGenerateAssociationQueryHasOneWithConditions() {
  1484. $this->Model = new TestModel4();
  1485. $this->Model->schema();
  1486. $this->_buildRelatedModels($this->Model);
  1487. $binding = array('type' => 'hasOne', 'model' => 'TestModel5');
  1488. $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
  1489. $resultSet = null;
  1490. $null = null;
  1491. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1492. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1493. $this->assertTrue($result);
  1494. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1495. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1496. $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model5` AS `TestModel5`/', $result);
  1497. $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id`\s+=\s+`TestModel4`.`id`\)\s+WHERE/', $result);
  1498. $this->assertPattern('/\s+WHERE\s+(?:\()?\s*`TestModel5`.`name`\s+!=\s+\'mariano\'\s*(?:\))?\s*$/', $result);
  1499. }
  1500. /**
  1501. * testGenerateAssociationQueryBelongsTo method
  1502. *
  1503. * @access public
  1504. * @return void
  1505. */
  1506. function testGenerateAssociationQueryBelongsTo() {
  1507. $this->Model = new TestModel5();
  1508. $this->Model->schema();
  1509. $this->_buildRelatedModels($this->Model);
  1510. $binding = array('type'=>'belongsTo', 'model'=>'TestModel4');
  1511. $queryData = array();
  1512. $resultSet = null;
  1513. $null = null;
  1514. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1515. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1516. $this->assertTrue($result);
  1517. $result = $this->testDb->buildJoinStatement($queryData['joins'][0]);
  1518. $expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
  1519. $this->assertEqual(trim($result), trim($expected));
  1520. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1521. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1522. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result);
  1523. $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
  1524. $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
  1525. }
  1526. /**
  1527. * testGenerateAssociationQueryBelongsToWithConditions method
  1528. *
  1529. * @access public
  1530. * @return void
  1531. */
  1532. function testGenerateAssociationQueryBelongsToWithConditions() {
  1533. $this->Model = new TestModel5();
  1534. $this->Model->schema();
  1535. $this->_buildRelatedModels($this->Model);
  1536. $binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
  1537. $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
  1538. $resultSet = null;
  1539. $null = null;
  1540. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1541. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1542. $this->assertTrue($result);
  1543. $result = $this->testDb->buildJoinStatement($queryData['joins'][0]);
  1544. $expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
  1545. $this->assertEqual(trim($result), trim($expected));
  1546. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1547. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1548. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result);
  1549. $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
  1550. $this->assertPattern('/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/', $result);
  1551. }
  1552. /**
  1553. * testGenerateAssociationQueryHasMany method
  1554. *
  1555. * @access public
  1556. * @return void
  1557. */
  1558. function testGenerateAssociationQueryHasMany() {
  1559. $this->Model = new TestModel5();
  1560. $this->Model->schema();
  1561. $this->_buildRelatedModels($this->Model);
  1562. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1563. $queryData = array();
  1564. $resultSet = null;
  1565. $null = null;
  1566. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1567. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1568. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1569. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE/', $result);
  1570. $this->assertPattern('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/', $result);
  1571. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1572. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1573. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1574. $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
  1575. }
  1576. /**
  1577. * testGenerateAssociationQueryHasManyWithLimit method
  1578. *
  1579. * @access public
  1580. * @return void
  1581. */
  1582. function testGenerateAssociationQueryHasManyWithLimit() {
  1583. $this->Model = new TestModel5();
  1584. $this->Model->schema();
  1585. $this->_buildRelatedModels($this->Model);
  1586. $this->Model->hasMany['TestModel6']['limit'] = 2;
  1587. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1588. $queryData = array();
  1589. $resultSet = null;
  1590. $null = null;
  1591. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1592. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1593. $this->assertPattern(
  1594. '/^SELECT\s+' .
  1595. '`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+'.
  1596. 'FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+' .
  1597. '`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)\s*'.
  1598. 'LIMIT \d*'.
  1599. '\s*$/', $result
  1600. );
  1601. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1602. $this->assertPattern(
  1603. '/^SELECT\s+'.
  1604. '`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+'.
  1605. 'FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+'.
  1606. '(?:\()?\s*1 = 1\s*(?:\))?'.
  1607. '\s*$/', $result
  1608. );
  1609. }
  1610. /**
  1611. * testGenerateAssociationQueryHasManyWithConditions method
  1612. *
  1613. * @access public
  1614. * @return void
  1615. */
  1616. function testGenerateAssociationQueryHasManyWithConditions() {
  1617. $this->Model = new TestModel5();
  1618. $this->Model->schema();
  1619. $this->_buildRelatedModels($this->Model);
  1620. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1621. $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
  1622. $resultSet = null;
  1623. $null = null;
  1624. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1625. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1626. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1627. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1628. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1629. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1630. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1631. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1632. $this->assertPattern('/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
  1633. }
  1634. /**
  1635. * testGenerateAssociationQueryHasManyWithOffsetAndLimit method
  1636. *
  1637. * @access public
  1638. * @return void
  1639. */
  1640. function testGenerateAssociationQueryHasManyWithOffsetAndLimit() {
  1641. $this->Model = new TestModel5();
  1642. $this->Model->schema();
  1643. $this->_buildRelatedModels($this->Model);
  1644. $__backup = $this->Model->hasMany['TestModel6'];
  1645. $this->Model->hasMany['TestModel6']['offset'] = 2;
  1646. $this->Model->hasMany['TestModel6']['limit'] = 5;
  1647. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1648. $queryData = array();
  1649. $resultSet = null;
  1650. $null = null;
  1651. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1652. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1653. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1654. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1655. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1656. $this->assertPattern('/\s+LIMIT 2,\s*5\s*$/', $result);
  1657. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1658. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1659. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1660. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1661. $this->Model->hasMany['TestModel6'] = $__backup;
  1662. }
  1663. /**
  1664. * testGenerateAssociationQueryHasManyWithPageAndLimit method
  1665. *
  1666. * @access public
  1667. * @return void
  1668. */
  1669. function testGenerateAssociationQueryHasManyWithPageAndLimit() {
  1670. $this->Model = new TestModel5();
  1671. $this->Model->schema();
  1672. $this->_buildRelatedModels($this->Model);
  1673. $__backup = $this->Model->hasMany['TestModel6'];
  1674. $this->Model->hasMany['TestModel6']['page'] = 2;
  1675. $this->Model->hasMany['TestModel6']['limit'] = 5;
  1676. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1677. $queryData = array();
  1678. $resultSet = null;
  1679. $null = null;
  1680. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1681. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1682. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1683. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1684. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1685. $this->assertPattern('/\s+LIMIT 5,\s*5\s*$/', $result);
  1686. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1687. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
  1688. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1689. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1690. $this->Model->hasMany['TestModel6'] = $__backup;
  1691. }
  1692. /**
  1693. * testGenerateAssociationQueryHasManyWithFields method
  1694. *
  1695. * @access public
  1696. * @return void
  1697. */
  1698. function testGenerateAssociationQueryHasManyWithFields() {
  1699. $this->Model = new TestModel5();
  1700. $this->Model->schema();
  1701. $this->_buildRelatedModels($this->Model);
  1702. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1703. $queryData = array('fields' => array('`TestModel5`.`name`'));
  1704. $resultSet = null;
  1705. $null = null;
  1706. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1707. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1708. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1709. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1710. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1711. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1712. $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/', $result);
  1713. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1714. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1715. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1716. $queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`'));
  1717. $resultSet = null;
  1718. $null = null;
  1719. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1720. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1721. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1722. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1723. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1724. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1725. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1726. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1727. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1728. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1729. $queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`'));
  1730. $resultSet = null;
  1731. $null = null;
  1732. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1733. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1734. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
  1735. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1736. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1737. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1738. $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/', $result);
  1739. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1740. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1741. $this->Model->hasMany['TestModel6']['fields'] = array('name');
  1742. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1743. $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
  1744. $resultSet = null;
  1745. $null = null;
  1746. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1747. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1748. $this->assertPattern('/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
  1749. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1750. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1751. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1752. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1753. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1754. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1755. unset($this->Model->hasMany['TestModel6']['fields']);
  1756. $this->Model->hasMany['TestModel6']['fields'] = array('id', 'name');
  1757. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1758. $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
  1759. $resultSet = null;
  1760. $null = null;
  1761. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1762. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1763. $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
  1764. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1765. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1766. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1767. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1768. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1769. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1770. unset($this->Model->hasMany['TestModel6']['fields']);
  1771. $this->Model->hasMany['TestModel6']['fields'] = array('test_model5_id', 'name');
  1772. $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
  1773. $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
  1774. $resultSet = null;
  1775. $null = null;
  1776. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1777. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1778. $this->assertPattern('/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result);
  1779. $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
  1780. $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
  1781. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1782. $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
  1783. $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
  1784. $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1785. unset($this->Model->hasMany['TestModel6']['fields']);
  1786. }
  1787. /**
  1788. * testGenerateAssociationQueryHasAndBelongsToMany method
  1789. *
  1790. * @access public
  1791. * @return void
  1792. */
  1793. function testGenerateAssociationQueryHasAndBelongsToMany() {
  1794. $this->Model = new TestModel4();
  1795. $this->Model->schema();
  1796. $this->_buildRelatedModels($this->Model);
  1797. $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
  1798. $queryData = array();
  1799. $resultSet = null;
  1800. $null = null;
  1801. $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1802. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1803. $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1804. $this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`/', $result);
  1805. $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+AND/', $result);
  1806. $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/', $result);
  1807. $this->assertPattern('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
  1808. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1809. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1810. $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE/', $result);
  1811. $this->assertPattern('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
  1812. }
  1813. /**
  1814. * testGenerateAssociationQueryHasAndBelongsToManyWithConditions method
  1815. *
  1816. * @access public
  1817. * @return void
  1818. */
  1819. function testGenerateAssociationQueryHasAndBelongsToManyWithConditions() {
  1820. $this->Model = new TestModel4();
  1821. $this->Model->schema();
  1822. $this->_buildRelatedModels($this->Model);
  1823. $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
  1824. $queryData = array('conditions' => array('TestModel4.name !=' => 'mariano'));
  1825. $resultSet = null;
  1826. $null = null;
  1827. $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1828. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1829. $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1830. $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
  1831. $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result);
  1832. $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
  1833. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1834. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1835. $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?`TestModel4`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
  1836. }
  1837. /**
  1838. * testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit method
  1839. *
  1840. * @access public
  1841. * @return void
  1842. */
  1843. function testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit() {
  1844. $this->Model = new TestModel4();
  1845. $this->Model->schema();
  1846. $this->_buildRelatedModels($this->Model);
  1847. $__backup = $this->Model->hasAndBelongsToMany['TestModel7'];
  1848. $this->Model->hasAndBelongsToMany['TestModel7']['offset'] = 2;
  1849. $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5;
  1850. $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
  1851. $queryData = array();
  1852. $resultSet = null;
  1853. $null = null;
  1854. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1855. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1856. $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1857. $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
  1858. $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+/', $result);
  1859. $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
  1860. $this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/', $result);
  1861. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1862. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1863. $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1864. $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup;
  1865. }
  1866. /**
  1867. * testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit method
  1868. *
  1869. * @access public
  1870. * @return void
  1871. */
  1872. function testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit() {
  1873. $this->Model = new TestModel4();
  1874. $this->Model->schema();
  1875. $this->_buildRelatedModels($this->Model);
  1876. $__backup = $this->Model->hasAndBelongsToMany['TestModel7'];
  1877. $this->Model->hasAndBelongsToMany['TestModel7']['page'] = 2;
  1878. $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5;
  1879. $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
  1880. $queryData = array();
  1881. $resultSet = null;
  1882. $null = null;
  1883. $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
  1884. $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
  1885. $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
  1886. $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
  1887. $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result);
  1888. $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
  1889. $this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/', $result);
  1890. $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
  1891. $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
  1892. $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
  1893. $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup;
  1894. }
  1895. /**
  1896. * buildRelatedModels method
  1897. *
  1898. * @param mixed $model
  1899. * @access protected
  1900. * @return void
  1901. */
  1902. function _buildRelatedModels(&$model) {
  1903. foreach ($model->__associations as $type) {
  1904. foreach ($model->{$type} as $assoc => $assocData) {
  1905. if (is_string($assocData)) {
  1906. $className = $assocData;
  1907. } elseif (isset($assocData['className'])) {
  1908. $className = $assocData['className'];
  1909. }
  1910. $model->$className = new $className();
  1911. $model->$className->schema();
  1912. }
  1913. }
  1914. }
  1915. /**
  1916. * &_prepareAssociationQuery method
  1917. *
  1918. * @param mixed $model
  1919. * @param mixed $queryData
  1920. * @param mixed $binding
  1921. * @access public
  1922. * @return void
  1923. */
  1924. function &_prepareAssociationQuery(&$model, &$queryData, $binding) {
  1925. $type = $binding['type'];
  1926. $assoc = $binding['model'];
  1927. $assocData = $model->{$type}[$assoc];
  1928. $className = $assocData['className'];
  1929. $linkModel = $model->{$className};
  1930. $external = isset($assocData['external']);
  1931. $queryData = $this->testDb->__scrubQueryData($queryData);
  1932. $result = array_merge(array('linkModel' => &$linkModel), compact('type', 'assoc', 'assocData', 'external'));
  1933. return $result;
  1934. }
  1935. /**
  1936. * testSelectDistict method
  1937. *
  1938. * @access public
  1939. * @return void
  1940. */
  1941. function testSelectDistict() {
  1942. $result = $this->testDb->fields($this->Model, 'Vendor', "DISTINCT Vendor.id, Vendor.name");
  1943. $expected = array('DISTINCT `Vendor`.`id`', '`Vendor`.`name`');
  1944. $this->assertEqual($result, $expected);
  1945. }
  1946. /**
  1947. * testStringConditionsParsing method
  1948. *
  1949. * @access public
  1950. * @return void
  1951. */
  1952. function testStringConditionsParsing() {
  1953. $result = $this->testDb->conditions("ProjectBid.project_id = Project.id");
  1954. $expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id`";
  1955. $this->assertEqual($result, $expected);
  1956. $result = $this->testDb->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'");
  1957. $expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'";
  1958. $this->assertEqual($result, $expected);
  1959. $result = $this->testDb->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'");
  1960. $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
  1961. $this->assertEqual($result, $expected);
  1962. $result = $this->testDb->conditions("Post.title = '1.1'");
  1963. $expected = " WHERE `Post`.`title` = '1.1'";
  1964. $this->assertEqual($result, $expected);
  1965. $result = $this->testDb->conditions("User.id != 0 AND User.user LIKE '%arr%'");
  1966. $expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'";
  1967. $this->assertEqual($result, $expected);
  1968. $result = $this->testDb->conditions("SUM(Post.comments_count) > 500");
  1969. $expected = " WHERE SUM(`Post`.`comments_count`) > 500";
  1970. $this->assertEqual($result, $expected);
  1971. $result = $this->testDb->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
  1972. $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
  1973. $this->assertEqual($result, $expected);
  1974. $result = $this->testDb->conditions("score BETWEEN 90.1 AND 95.7");
  1975. $expected = " WHERE score BETWEEN 90.1 AND 95.7";
  1976. $this->assertEqual($result, $expected);
  1977. $result = $this->testDb->conditions(array('score' => array(2=>1, 2, 10)));
  1978. $expected = " WHERE score IN (1, 2, 10)";
  1979. $this->assertEqual($result, $expected);
  1980. $result = $this->testDb->conditions("Aro.rght = Aro.lft + 1.1");
  1981. $expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
  1982. $this->assertEqual($result, $expected);
  1983. $result = $this->testDb->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
  1984. $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
  1985. $this->assertEqual($result, $expected);
  1986. $result = $this->testDb->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');
  1987. $expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2';
  1988. $this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result);
  1989. $this->assertEqual($result, $expected);
  1990. $result = $this->testDb->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"');
  1991. $expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"';
  1992. $this->assertEqual($result, $expected);
  1993. $result = $this->testDb->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL');
  1994. $expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL';
  1995. $this->assertEqual($result, $expected);
  1996. $result = $this->testDb->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500');
  1997. $expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500';
  1998. $this->assertEqual($result, $expected);
  1999. $result = $this->testDb->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500');
  2000. $expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500';
  2001. $this->assertEqual($result, $expected);
  2002. $result = $this->testDb->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title');
  2003. $expected = ' WHERE NOT `Post`.`title_extended` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title_extended` != `Post`.`title`';
  2004. $this->assertEqual($result, $expected);
  2005. $result = $this->testDb->conditions("Comment.id = 'a'");
  2006. $expected = " WHERE `Comment`.`id` = 'a'";
  2007. $this->assertEqual($result, $expected);
  2008. $result = $this->testDb->conditions("lower(Article.title) LIKE 'a%'");
  2009. $expected = " WHERE lower(`Article`.`title`) LIKE 'a%'";
  2010. $this->assertEqual($result, $expected);
  2011. $result = $this->testDb->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))');
  2012. $expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))';
  2013. $this->assertEqual($result, $expected);
  2014. $result = $this->testDb->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1');
  2015. $expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1";
  2016. $this->assertEqual($result, $expected);
  2017. $result = $this->testDb->conditions('file = "index.html"');
  2018. $expected = ' WHERE file = "index.html"';
  2019. $this->assertEqual($result, $expected);
  2020. $result = $this->testDb->conditions("file = 'index.html'");
  2021. $expected = " WHERE file = 'index.html'";
  2022. $this->assertEqual($result, $expected);
  2023. $letter = $letter = 'd.a';
  2024. $conditions = array('Company.name like ' => $letter . '%');
  2025. $result = $this->testDb->conditions($conditions);
  2026. $expected = " WHERE `Company`.`name` like 'd.a%'";
  2027. $this->assertEqual($result, $expected);
  2028. $conditions = array('Artist.name' => 'JUDY and MARY');
  2029. $result = $this->testDb->conditions($conditions);
  2030. $expected = " WHERE `Artist`.`name` = 'JUDY and MARY'";
  2031. $this->assertEqual($result, $expected);
  2032. $conditions = array('Artist.name' => 'JUDY AND MARY');
  2033. $result = $this->testDb->conditions($conditions);
  2034. $expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'";
  2035. $this->assertEqual($result, $expected);
  2036. }
  2037. /**
  2038. * testQuotesInStringConditions method
  2039. *
  2040. * @access public
  2041. * @return void
  2042. */
  2043. function testQuotesInStringConditions() {
  2044. $result = $this->testDb->conditions('Member.email = \'mariano@cricava.com\'');
  2045. $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\'';
  2046. $this->assertEqual($result, $expected);
  2047. $result = $this->testDb->conditions('Member.email = "mariano@cricava.com"');
  2048. $expected = ' WHERE `Member`.`email` = "mariano@cricava.com"';
  2049. $this->assertEqual($result, $expected);
  2050. $result = $this->testDb->conditions('Member.email = \'mariano@cricava.com\' AND Member.user LIKE \'mariano.iglesias%\'');
  2051. $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\' AND `Member`.`user` LIKE \'mariano.iglesias%\'';
  2052. $this->assertEqual($result, $expected);
  2053. $result = $this->testDb->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"');
  2054. $expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"';
  2055. $this->assertEqual($result, $expected);
  2056. }
  2057. /**
  2058. * testParenthesisInStringConditions method
  2059. *
  2060. * @access public
  2061. * @return void
  2062. */
  2063. function testParenthesisInStringConditions() {
  2064. $result = $this->testDb->conditions('Member.name = \'(lu\'');
  2065. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
  2066. $result = $this->testDb->conditions('Member.name = \')lu\'');
  2067. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
  2068. $result = $this->testDb->conditions('Member.name = \'va(lu\'');
  2069. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
  2070. $result = $this->testDb->conditions('Member.name = \'va)lu\'');
  2071. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
  2072. $result = $this->testDb->conditions('Member.name = \'va(lu)\'');
  2073. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
  2074. $result = $this->testDb->conditions('Member.name = \'va(lu)e\'');
  2075. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
  2076. $result = $this->testDb->conditions('Member.name = \'(mariano)\'');
  2077. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
  2078. $result = $this->testDb->conditions('Member.name = \'(mariano)iglesias\'');
  2079. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
  2080. $result = $this->testDb->conditions('Member.name = \'(mariano) iglesias\'');
  2081. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
  2082. $result = $this->testDb->conditions('Member.name = \'(mariano word) iglesias\'');
  2083. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
  2084. $result = $this->testDb->conditions('Member.name = \'(mariano.iglesias)\'');
  2085. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
  2086. $result = $this->testDb->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias)\'');
  2087. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
  2088. $result = $this->testDb->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\'');
  2089. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
  2090. $result = $this->testDb->conditions('Member.name = \'(mariano.iglesias) CakePHP\'');
  2091. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
  2092. }
  2093. /**
  2094. * testParenthesisInArrayConditions method
  2095. *
  2096. * @access public
  2097. * @return void
  2098. */
  2099. function testParenthesisInArrayConditions() {
  2100. $result = $this->testDb->conditions(array('Member.name' => '(lu'));
  2101. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
  2102. $result = $this->testDb->conditions(array('Member.name' => ')lu'));
  2103. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
  2104. $result = $this->testDb->conditions(array('Member.name' => 'va(lu'));
  2105. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
  2106. $result = $this->testDb->conditions(array('Member.name' => 'va)lu'));
  2107. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
  2108. $result = $this->testDb->conditions(array('Member.name' => 'va(lu)'));
  2109. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
  2110. $result = $this->testDb->conditions(array('Member.name' => 'va(lu)e'));
  2111. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
  2112. $result = $this->testDb->conditions(array('Member.name' => '(mariano)'));
  2113. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
  2114. $result = $this->testDb->conditions(array('Member.name' => '(mariano)iglesias'));
  2115. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
  2116. $result = $this->testDb->conditions(array('Member.name' => '(mariano) iglesias'));
  2117. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
  2118. $result = $this->testDb->conditions(array('Member.name' => '(mariano word) iglesias'));
  2119. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
  2120. $result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias)'));
  2121. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
  2122. $result = $this->testDb->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias)'));
  2123. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
  2124. $result = $this->testDb->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP'));
  2125. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
  2126. $result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias) CakePHP'));
  2127. $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
  2128. }
  2129. /**
  2130. * testArrayConditionsParsing method
  2131. *
  2132. * @access public
  2133. * @return void
  2134. */
  2135. function testArrayConditionsParsing() {
  2136. $result = $this->testDb->conditions(array('Stereo.type' => 'in dash speakers'));
  2137. $this->assertPattern("/^\s+WHERE\s+`Stereo`.`type`\s+=\s+'in dash speakers'/", $result);
  2138. $result = $this->testDb->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c'));
  2139. $this->assertPattern("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result);
  2140. $result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c'));
  2141. $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
  2142. $this->assertEqual($result, $expected);
  2143. $result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%'));
  2144. $expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'";
  2145. $this->assertEqual($result, $expected);
  2146. $result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%'));
  2147. $expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'";
  2148. $this->assertEqual($result, $expected);
  2149. $result = $this->testDb->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7)));
  2150. $expected = " WHERE `score` BETWEEN 90.1 AND 95.7";
  2151. $this->assertEqual($result, $expected);
  2152. $result = $this->testDb->conditions(array('Post.title' => 1.1));
  2153. $expected = " WHERE `Post`.`title` = 1.1";
  2154. $this->assertEqual($result, $expected);
  2155. $result = $this->testDb->conditions(array('Post.title' => 1.1), true, true, new Post());
  2156. $expected = " WHERE `Post`.`title` = '1.1'";
  2157. $this->assertEqual($result, $expected);
  2158. $result = $this->testDb->conditions(array('SUM(Post.comments_count) >' => '500'));
  2159. $expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
  2160. $this->assertEqual($result, $expected);
  2161. $result = $this->testDb->conditions(array('MAX(Post.rating) >' => '50'));
  2162. $expected = " WHERE MAX(`Post`.`rating`) > '50'";
  2163. $this->assertEqual($result, $expected);
  2164. $result = $this->testDb->conditions(array('title LIKE' => '%hello'));
  2165. $expected = " WHERE `title` LIKE '%hello'";
  2166. $this->assertEqual($result, $expected);
  2167. $result = $this->testDb->conditions(array('Post.name' => 'mad(g)ik'));
  2168. $expected = " WHERE `Post`.`name` = 'mad(g)ik'";
  2169. $this->assertEqual($result, $expected);
  2170. $result = $this->testDb->conditions(array('score' => array(1, 2, 10)));
  2171. $expected = " WHERE score IN (1, 2, 10)";
  2172. $this->assertEqual($result, $expected);
  2173. $result = $this->testDb->conditions(array('score' => array()));
  2174. $expected = " WHERE `score` IS NULL";
  2175. $this->assertEqual($result, $expected);
  2176. $result = $this->testDb->conditions(array('score !=' => array()));
  2177. $expected = " WHERE `score` IS NOT NULL";
  2178. $this->assertEqual($result, $expected);
  2179. $result = $this->testDb->conditions(array('score !=' => '20'));
  2180. $expected = " WHERE `score` != '20'";
  2181. $this->assertEqual($result, $expected);
  2182. $result = $this->testDb->conditions(array('score >' => '20'));
  2183. $expected = " WHERE `score` > '20'";
  2184. $this->assertEqual($result, $expected);
  2185. $result = $this->testDb->conditions(array('client_id >' => '20'), true, true, new TestModel());
  2186. $expected = " WHERE `client_id` > 20";
  2187. $this->assertEqual($result, $expected);
  2188. $result = $this->testDb->conditions(array('OR' => array(
  2189. array('User.user' => 'mariano'),
  2190. array('User.user' => 'nate')
  2191. )));
  2192. $expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))";
  2193. $this->assertEqual($result, $expected);
  2194. $result = $this->testDb->conditions(array('or' => array(
  2195. 'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20'
  2196. )));
  2197. $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))";
  2198. $this->assertEqual($result, $expected);
  2199. $result = $this->testDb->conditions(array('or' => array(
  2200. 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
  2201. )));
  2202. $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))";
  2203. $this->assertEqual($result, $expected);
  2204. $result = $this->testDb->conditions(array('and' => array(
  2205. 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
  2206. )));
  2207. $expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))";
  2208. $this->assertEqual($result, $expected);
  2209. $result = $this->testDb->conditions(array(
  2210. 'published' => 1, 'or' => array('score >' => '2', array('score >' => '20'))
  2211. ));
  2212. $expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))";
  2213. $this->assertEqual($result, $expected);
  2214. $result = $this->testDb->conditions(array(array('Project.removed' => false)));
  2215. $expected = " WHERE `Project`.`removed` = 0";
  2216. $this->assertEqual($result, $expected);
  2217. $result = $this->testDb->conditions(array(array('Project.removed' => true)));
  2218. $expected = " WHERE `Project`.`removed` = 1";
  2219. $this->assertEqual($result, $expected);
  2220. $result = $this->testDb->conditions(array(array('Project.removed' => null)));
  2221. $expected = " WHERE `Project`.`removed` IS NULL";
  2222. $this->assertEqual($result, $expected);
  2223. $result = $this->testDb->conditions(array(array('Project.removed !=' => null)));
  2224. $expected = " WHERE `Project`.`removed` IS NOT NULL";
  2225. $this->assertEqual($result, $expected);
  2226. $result = $this->testDb->conditions(array('(Usergroup.permissions) & 4' => 4));
  2227. $expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4";
  2228. $this->assertEqual($result, $expected);
  2229. $result = $this->testDb->conditions(array('((Usergroup.permissions) & 4)' => 4));
  2230. $expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4";
  2231. $this->assertEqual($result, $expected);
  2232. $result = $this->testDb->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)'));
  2233. $expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'";
  2234. $this->assertEqual($result, $expected);
  2235. $result = $this->testDb->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
  2236. $expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
  2237. $this->assertEqual($result, $expected);
  2238. $result = $this->testDb->conditions(array(
  2239. 'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
  2240. 'Enrollment.yearcompleted >' => '0')
  2241. );
  2242. $this->assertPattern('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
  2243. $result = $this->testDb->conditions(array('id <>' => '8'));
  2244. $this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
  2245. $result = $this->testDb->conditions(array('TestModel.field =' => 'gribe$@()lu'));
  2246. $expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
  2247. $this->assertEqual($result, $expected);
  2248. $conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100"));
  2249. $conditions[0]['OR'] = array(
  2250. "Listing.title LIKE" => "%term%",
  2251. "Listing.description LIKE" => "%term%"
  2252. );
  2253. $conditions[1]['OR'] = array(
  2254. "Listing.title LIKE" => "%term_2%",
  2255. "Listing.description LIKE" => "%term_2%"
  2256. );
  2257. $result = $this->testDb->conditions($conditions);
  2258. $expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND" .
  2259. " ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND" .
  2260. " ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
  2261. $this->assertEqual($result, $expected);
  2262. $result = $this->testDb->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah'));
  2263. $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'";
  2264. $this->assertEqual($result, $expected);
  2265. $result = $this->testDb->conditions(array(
  2266. 'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah')
  2267. ));
  2268. $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')";
  2269. $this->assertEqual($result, $expected);
  2270. $conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76));
  2271. $result = $this->testDb->conditions($conditions);
  2272. $expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
  2273. $this->assertEqual($result, $expected);
  2274. $conditions = array('title' => 'user(s)');
  2275. $result = $this->testDb->conditions($conditions);
  2276. $expected = " WHERE `title` = 'user(s)'";
  2277. $this->assertEqual($result, $expected);
  2278. $conditions = array('title' => 'user(s) data');
  2279. $result = $this->testDb->conditions($conditions);
  2280. $expected = " WHERE `title` = 'user(s) data'";
  2281. $this->assertEqual($result, $expected);
  2282. $conditions = array('title' => 'user(s,arg) data');
  2283. $result = $this->testDb->conditions($conditions);
  2284. $expected = " WHERE `title` = 'user(s,arg) data'";
  2285. $this->assertEqual($result, $expected);
  2286. $result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM)'));
  2287. $expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
  2288. $this->assertEqual($result, $expected);
  2289. $result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM) '));
  2290. $expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
  2291. $this->assertEqual($result, $expected);
  2292. $result = $this->testDb->conditions(array("Book.id" => 0));
  2293. $expected = " WHERE `Book`.`id` = 0";
  2294. $this->assertEqual($result, $expected);
  2295. $result = $this->testDb->conditions(array("Book.id" => NULL));
  2296. $expected = " WHERE `Book`.`id` IS NULL";
  2297. $this->assertEqual($result, $expected);
  2298. $result = $this->testDb->conditions(array('Listing.beds >=' => 0));
  2299. $expected = " WHERE `Listing`.`beds` >= 0";
  2300. $this->assertEqual($result, $expected);
  2301. $result = $this->testDb->conditions(array(
  2302. 'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90)
  2303. ));
  2304. $expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90';
  2305. $this->assertEqual($result, $expected);
  2306. $result = $this->testDb->conditions(array('or' => array(
  2307. '? BETWEEN Model.field1 AND Model.field2' => '2009-03-04'
  2308. )));
  2309. $expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2";
  2310. $this->assertEqual($result, $expected);
  2311. }
  2312. /**
  2313. * testArrayConditionsParsingComplexKeys method
  2314. *
  2315. * @access public
  2316. * @return void
  2317. */
  2318. function testArrayConditionsParsingComplexKeys() {
  2319. $result = $this->testDb->conditions(array(
  2320. 'CAST(Book.created AS DATE)' => '2008-08-02'
  2321. ));
  2322. $expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'";
  2323. $this->assertEqual($result, $expected);
  2324. $result = $this->testDb->conditions(array(
  2325. 'CAST(Book.created AS DATE) <=' => '2008-08-02'
  2326. ));
  2327. $expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'";
  2328. $this->assertEqual($result, $expected);
  2329. $result = $this->testDb->conditions(array(
  2330. '(Stats.clicks * 100) / Stats.views >' => 50
  2331. ));
  2332. $expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50";
  2333. $this->assertEqual($result, $expected);
  2334. }
  2335. /**
  2336. * testMixedConditionsParsing method
  2337. *
  2338. * @access public
  2339. * @return void
  2340. */
  2341. function testMixedConditionsParsing() {
  2342. $conditions[] = 'User.first_name = \'Firstname\'';
  2343. $conditions[] = array('User.last_name' => 'Lastname');
  2344. $result = $this->testDb->conditions($conditions);
  2345. $expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'";
  2346. $this->assertEqual($result, $expected);
  2347. $conditions = array(
  2348. 'Thread.project_id' => 5,
  2349. 'Thread.buyer_id' => 14,
  2350. '1=1 GROUP BY Thread.project_id'
  2351. );
  2352. $result = $this->testDb->conditions($conditions);
  2353. $this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result);
  2354. }
  2355. /**
  2356. * testConditionsOptionalArguments method
  2357. *
  2358. * @access public
  2359. * @return void
  2360. */
  2361. function testConditionsOptionalArguments() {
  2362. $result = $this->testDb->conditions( array('Member.name' => 'Mariano'), true, false);
  2363. $this->assertPattern('/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/', $result);
  2364. $result = $this->testDb->conditions( array(), true, false);
  2365. $this->assertPattern('/^\s*1\s*=\s*1\s*$/', $result);
  2366. }
  2367. /**
  2368. * testConditionsWithModel
  2369. *
  2370. * @access public
  2371. * @return void
  2372. */
  2373. function testConditionsWithModel() {
  2374. $this->Model = new Article2();
  2375. $result = $this->testDb->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model);
  2376. $expected = " WHERE `Article2`.`viewed` >= 0";
  2377. $this->assertEqual($result, $expected);
  2378. $result = $this->testDb->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model);
  2379. $expected = " WHERE `Article2`.`viewed` >= 0";
  2380. $this->assertEqual($result, $expected);
  2381. $result = $this->testDb->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model);
  2382. $expected = " WHERE `Article2`.`viewed` >= 1";
  2383. $this->assertEqual($result, $expected);
  2384. $result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array(0, 10)), true, true, $this->Model);
  2385. $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
  2386. $this->assertEqual($result, $expected);
  2387. $result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('0', '10')), true, true, $this->Model);
  2388. $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
  2389. $this->assertEqual($result, $expected);
  2390. $result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('1', '10')), true, true, $this->Model);
  2391. $expected = " WHERE `Article2`.`rate_sum` BETWEEN 1 AND 10";
  2392. $this->assertEqual($result, $expected);
  2393. }
  2394. /**
  2395. * testFieldParsing method
  2396. *
  2397. * @access public
  2398. * @return void
  2399. */
  2400. function testFieldParsing() {
  2401. $result = $this->testDb->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`");
  2402. $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
  2403. $this->assertEqual($result, $expected);
  2404. $result = $this->testDb->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`");
  2405. $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
  2406. $this->assertEqual($result, $expected);
  2407. $result = $this->testDb->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created");
  2408. $expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`");
  2409. $this->assertEqual($result, $expected);
  2410. $result = $this->testDb->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test');
  2411. $this->assertEqual($result, array('round( (3.55441 * fooField), 3 ) AS test'));
  2412. $result = $this->testDb->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating');
  2413. $this->assertEqual($result, array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'));
  2414. $result = $this->testDb->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating');
  2415. $this->assertEqual($result, array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'));
  2416. $result = $this->testDb->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name");
  2417. $expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name");
  2418. $this->assertEqual($result, $expected);
  2419. $result = $this->testDb->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1");
  2420. $expected = array(
  2421. '2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`',
  2422. "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1'
  2423. );
  2424. $this->assertEqual($result, $expected);
  2425. $result = $this->testDb->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
  2426. $expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
  2427. $this->assertEqual($result, $expected);
  2428. $result = $this->testDb->fields($this->Model, 'Post');
  2429. $expected = array(
  2430. '`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`',
  2431. '`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`',
  2432. '`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`',
  2433. '`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`',
  2434. '`Post`.`created`', '`Post`.`updated`'
  2435. );
  2436. $this->assertEqual($result, $expected);
  2437. $result = $this->testDb->fields($this->Model, 'Other');
  2438. $expected = array(
  2439. '`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`',
  2440. '`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`',
  2441. '`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`',
  2442. '`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`',
  2443. '`Other`.`created`', '`Other`.`updated`'
  2444. );
  2445. $this->assertEqual($result, $expected);
  2446. $result = $this->testDb->fields($this->Model, null, array(), false);
  2447. $expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated');
  2448. $this->assertEqual($result, $expected);
  2449. $result = $this->testDb->fields($this->Model, null, 'COUNT(*)');
  2450. $expected = array('COUNT(*)');
  2451. $this->assertEqual($result, $expected);
  2452. $result = $this->testDb->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->testDb->name('sum_unread_buyer'));
  2453. $expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`');
  2454. $this->assertEqual($result, $expected);
  2455. $result = $this->testDb->fields($this->Model, null, 'name, count(*)');
  2456. $expected = array('`TestModel`.`name`', 'count(*)');
  2457. $this->assertEqual($result, $expected);
  2458. $result = $this->testDb->fields($this->Model, null, 'count(*), name');
  2459. $expected = array('count(*)', '`TestModel`.`name`');
  2460. $this->assertEqual($result, $expected);
  2461. $result = $this->testDb->fields(
  2462. $this->Model, null, 'field1, field2, field3, count(*), name'
  2463. );
  2464. $expected = array(
  2465. '`TestModel`.`field1`', '`TestModel`.`field2`',
  2466. '`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'
  2467. );
  2468. $this->assertEqual($result, $expected);
  2469. $result = $this->testDb->fields($this->Model, null, array('dayofyear(now())'));
  2470. $expected = array('dayofyear(now())');
  2471. $this->assertEqual($result, $expected);
  2472. $result = $this->testDb->fields($this->Model, null, array('MAX(Model.field) As Max'));
  2473. $expected = array('MAX(`Model`.`field`) As Max');
  2474. $this->assertEqual($result, $expected);
  2475. $result = $this->testDb->fields($this->Model, null, array('Model.field AS AnotherName'));
  2476. $expected = array('`Model`.`field` AS `AnotherName`');
  2477. $this->assertEqual($result, $expected);
  2478. $result = $this->testDb->fields($this->Model, null, array('field AS AnotherName'));
  2479. $expected = array('`field` AS `AnotherName`');
  2480. $this->assertEqual($result, $expected);
  2481. $result = $this->testDb->fields($this->Model, null, array(
  2482. 'TestModel.field AS AnotherName'
  2483. ));
  2484. $expected = array('`TestModel`.`field` AS `AnotherName`');
  2485. $this->assertEqual($result, $expected);
  2486. $result = $this->testDb->fields($this->Model, 'Foo', array(
  2487. 'id', 'title', '(user_count + discussion_count + post_count) AS score'
  2488. ));
  2489. $expected = array(
  2490. '`Foo`.`id`',
  2491. '`Foo`.`title`',
  2492. '(user_count + discussion_count + post_count) AS score'
  2493. );
  2494. $this->assertEqual($result, $expected);
  2495. }
  2496. /**
  2497. * testMergeAssociations method
  2498. *
  2499. * @access public
  2500. * @return void
  2501. */
  2502. function testMergeAssociations() {
  2503. $data = array('Article2' => array(
  2504. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  2505. 'body' => 'First Article Body', 'published' => 'Y',
  2506. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2507. ));
  2508. $merge = array('Topic' => array(array(
  2509. 'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
  2510. 'updated' => '2007-03-17 01:18:31'
  2511. )));
  2512. $expected = array(
  2513. 'Article2' => array(
  2514. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  2515. 'body' => 'First Article Body', 'published' => 'Y',
  2516. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2517. ),
  2518. 'Topic' => array(
  2519. 'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
  2520. 'updated' => '2007-03-17 01:18:31'
  2521. )
  2522. );
  2523. $this->testDb->__mergeAssociation($data, $merge, 'Topic', 'hasOne');
  2524. $this->assertEqual($data, $expected);
  2525. $data = array('Article2' => array(
  2526. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  2527. 'body' => 'First Article Body', 'published' => 'Y',
  2528. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2529. ));
  2530. $merge = array('User2' => array(array(
  2531. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  2532. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2533. )));
  2534. $expected = array(
  2535. 'Article2' => array(
  2536. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  2537. 'body' => 'First Article Body', 'published' => 'Y',
  2538. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2539. ),
  2540. 'User2' => array(
  2541. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2542. )
  2543. );
  2544. $this->testDb->__mergeAssociation($data, $merge, 'User2', 'belongsTo');
  2545. $this->assertEqual($data, $expected);
  2546. $data = array(
  2547. 'Article2' => array(
  2548. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2549. )
  2550. );
  2551. $merge = array(array('Comment' => false));
  2552. $expected = array(
  2553. 'Article2' => array(
  2554. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2555. ),
  2556. 'Comment' => array()
  2557. );
  2558. $this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
  2559. $this->assertEqual($data, $expected);
  2560. $data = array(
  2561. 'Article' => array(
  2562. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2563. )
  2564. );
  2565. $merge = array(
  2566. array(
  2567. 'Comment' => array(
  2568. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2569. )
  2570. ),
  2571. array(
  2572. 'Comment' => array(
  2573. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2574. )
  2575. )
  2576. );
  2577. $expected = array(
  2578. 'Article' => array(
  2579. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2580. ),
  2581. 'Comment' => array(
  2582. array(
  2583. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2584. ),
  2585. array(
  2586. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2587. )
  2588. )
  2589. );
  2590. $this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
  2591. $this->assertEqual($data, $expected);
  2592. $data = array(
  2593. 'Article' => array(
  2594. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2595. )
  2596. );
  2597. $merge = array(
  2598. array(
  2599. 'Comment' => array(
  2600. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2601. ),
  2602. 'User2' => array(
  2603. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2604. )
  2605. ),
  2606. array(
  2607. 'Comment' => array(
  2608. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2609. ),
  2610. 'User2' => array(
  2611. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2612. )
  2613. )
  2614. );
  2615. $expected = array(
  2616. 'Article' => array(
  2617. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2618. ),
  2619. 'Comment' => array(
  2620. array(
  2621. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  2622. 'User2' => array(
  2623. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2624. )
  2625. ),
  2626. array(
  2627. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  2628. 'User2' => array(
  2629. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2630. )
  2631. )
  2632. )
  2633. );
  2634. $this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
  2635. $this->assertEqual($data, $expected);
  2636. $data = array(
  2637. 'Article' => array(
  2638. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2639. )
  2640. );
  2641. $merge = array(
  2642. array(
  2643. 'Comment' => array(
  2644. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2645. ),
  2646. 'User2' => array(
  2647. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2648. ),
  2649. 'Tag' => array(
  2650. array('id' => 1, 'tag' => 'Tag 1'),
  2651. array('id' => 2, 'tag' => 'Tag 2')
  2652. )
  2653. ),
  2654. array(
  2655. 'Comment' => array(
  2656. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2657. ),
  2658. 'User2' => array(
  2659. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2660. ),
  2661. 'Tag' => array()
  2662. )
  2663. );
  2664. $expected = array(
  2665. 'Article' => array(
  2666. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2667. ),
  2668. 'Comment' => array(
  2669. array(
  2670. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  2671. 'User2' => array(
  2672. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2673. ),
  2674. 'Tag' => array(
  2675. array('id' => 1, 'tag' => 'Tag 1'),
  2676. array('id' => 2, 'tag' => 'Tag 2')
  2677. )
  2678. ),
  2679. array(
  2680. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  2681. 'User2' => array(
  2682. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2683. ),
  2684. 'Tag' => array()
  2685. )
  2686. )
  2687. );
  2688. $this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
  2689. $this->assertEqual($data, $expected);
  2690. $data = array(
  2691. 'Article' => array(
  2692. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2693. )
  2694. );
  2695. $merge = array(
  2696. array(
  2697. 'Tag' => array(
  2698. 'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2699. )
  2700. ),
  2701. array(
  2702. 'Tag' => array(
  2703. 'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2704. )
  2705. ),
  2706. array(
  2707. 'Tag' => array(
  2708. 'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2709. )
  2710. )
  2711. );
  2712. $expected = array(
  2713. 'Article' => array(
  2714. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2715. ),
  2716. 'Tag' => array(
  2717. array(
  2718. 'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2719. ),
  2720. array(
  2721. 'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2722. ),
  2723. array(
  2724. 'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2725. )
  2726. )
  2727. );
  2728. $this->testDb->__mergeAssociation($data, $merge, 'Tag', 'hasAndBelongsToMany');
  2729. $this->assertEqual($data, $expected);
  2730. $data = array(
  2731. 'Article' => array(
  2732. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2733. )
  2734. );
  2735. $merge = array(
  2736. array(
  2737. 'Tag' => array(
  2738. 'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2739. )
  2740. ),
  2741. array(
  2742. 'Tag' => array(
  2743. 'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2744. )
  2745. ),
  2746. array(
  2747. 'Tag' => array(
  2748. 'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  2749. )
  2750. )
  2751. );
  2752. $expected = array(
  2753. 'Article' => array(
  2754. 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  2755. ),
  2756. 'Tag' => array('id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
  2757. );
  2758. $this->testDb->__mergeAssociation($data, $merge, 'Tag', 'hasOne');
  2759. $this->assertEqual($data, $expected);
  2760. }
  2761. /**
  2762. * testRenderStatement method
  2763. *
  2764. * @access public
  2765. * @return void
  2766. */
  2767. function testRenderStatement() {
  2768. $result = $this->testDb->renderStatement('select', array(
  2769. 'fields' => 'id', 'table' => 'table', 'conditions' => 'WHERE 1=1',
  2770. 'alias' => '', 'joins' => '', 'order' => '', 'limit' => '', 'group' => ''
  2771. ));
  2772. $this->assertPattern('/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result);
  2773. $result = $this->testDb->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => ''));
  2774. $this->assertPattern('/^\s*UPDATE\s+table\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result);
  2775. $result = $this->testDb->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => ''));
  2776. $this->assertPattern('/^\s*UPDATE\s+table\s+AS\s+alias\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result);
  2777. $result = $this->testDb->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => ''));
  2778. $this->assertPattern('/^\s*DELETE\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result);
  2779. $result = $this->testDb->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => ''));
  2780. $this->assertPattern('/^\s*DELETE\s+alias\s+FROM\s+table\s+AS\s+alias\s+WHERE\s+1=1\s*$/', $result);
  2781. }
  2782. /**
  2783. * testStatements method
  2784. *
  2785. * @access public
  2786. * @return void
  2787. */
  2788. function testStatements() {
  2789. $Article = ClassRegistry::init('Article');
  2790. $result = $this->testDb->update($Article, array('field1'), array('value1'));
  2791. $this->assertFalse($result);
  2792. $result = $this->testDb->getLastQuery();
  2793. $this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . '\s+SET\s+`field1`\s*=\s*\'value1\'\s+WHERE\s+1 = 1\s*$/', $result);
  2794. $result = $this->testDb->update($Article, array('field1'), array('2'), '2=2');
  2795. $this->assertFalse($result);
  2796. $result = $this->testDb->getLastQuery();
  2797. $this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . ' AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+SET\s+`Article`\.`field1`\s*=\s*2\s+WHERE\s+2\s*=\s*2\s*$/', $result);
  2798. $result = $this->testDb->delete($Article);
  2799. $this->assertTrue($result);
  2800. $result = $this->testDb->getLastQuery();
  2801. $this->assertPattern('/^\s*DELETE\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+WHERE\s+1 = 1\s*$/', $result);
  2802. $result = $this->testDb->delete($Article, true);
  2803. $this->assertTrue($result);
  2804. $result = $this->testDb->getLastQuery();
  2805. $this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+1\s*=\s*1\s*$/', $result);
  2806. $result = $this->testDb->delete($Article, '2=2');
  2807. $this->assertTrue($result);
  2808. $result = $this->testDb->getLastQuery();
  2809. $this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+2\s*=\s*2\s*$/', $result);
  2810. $result = $this->testDb->hasAny($Article, '1=2');
  2811. $this->assertFalse($result);
  2812. $result = $this->testDb->insertMulti('articles', array('field'), array('(1)', '(2)'));
  2813. $this->assertFalse($result);
  2814. $result = $this->testDb->getLastQuery();
  2815. $this->assertPattern('/^\s*INSERT INTO\s+' . $this->testDb->fullTableName('articles') . '\s+\(`field`\)\s+VALUES\s+\(1\),\s*\(2\)\s*$/', $result);
  2816. }
  2817. /**
  2818. * testSchema method
  2819. *
  2820. * @access public
  2821. * @return void
  2822. */
  2823. function testSchema() {
  2824. $Schema = new CakeSchema();
  2825. $Schema->tables = array('table' => array(), 'anotherTable' => array());
  2826. $this->expectError();
  2827. $result = $this->testDb->dropSchema(null);
  2828. $this->assertTrue($result === null);
  2829. $result = $this->testDb->dropSchema($Schema, 'non_existing');
  2830. $this->assertTrue(empty($result));
  2831. $result = $this->testDb->dropSchema($Schema, 'table');
  2832. $this->assertPattern('/^\s*DROP TABLE IF EXISTS\s+' . $this->testDb->fullTableName('table') . ';\s*$/s', $result);
  2833. }
  2834. /**
  2835. * testMagicMethodQuerying method
  2836. *
  2837. * @access public
  2838. * @return void
  2839. */
  2840. function testMagicMethodQuerying() {
  2841. $result = $this->testDb->query('findByFieldName', array('value'), $this->Model);
  2842. $expected = array('first', array(
  2843. 'conditions' => array('TestModel.field_name' => 'value'),
  2844. 'fields' => null, 'order' => null, 'recursive' => null
  2845. ));
  2846. $this->assertEqual($result, $expected);
  2847. $result = $this->testDb->query('findByFindBy', array('value'), $this->Model);
  2848. $expected = array('first', array(
  2849. 'conditions' => array('TestModel.find_by' => 'value'),
  2850. 'fields' => null, 'order' => null, 'recursive' => null
  2851. ));
  2852. $this->assertEqual($result, $expected);
  2853. $result = $this->testDb->query('findAllByFieldName', array('value'), $this->Model);
  2854. $expected = array('all', array(
  2855. 'conditions' => array('TestModel.field_name' => 'value'),
  2856. 'fields' => null, 'order' => null, 'limit' => null,
  2857. 'page' => null, 'recursive' => null
  2858. ));
  2859. $this->assertEqual($result, $expected);
  2860. $result = $this->testDb->query('findAllById', array('a'), $this->Model);
  2861. $expected = array('all', array(
  2862. 'conditions' => array('TestModel.id' => 'a'),
  2863. 'fields' => null, 'order' => null, 'limit' => null,
  2864. 'page' => null, 'recursive' => null
  2865. ));
  2866. $this->assertEqual($result, $expected);
  2867. $result = $this->testDb->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
  2868. $expected = array('first', array(
  2869. 'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')),
  2870. 'fields' => null, 'order' => null, 'recursive' => null
  2871. ));
  2872. $this->assertEqual($result, $expected);
  2873. $result = $this->testDb->query('findByFieldName', array(null), $this->Model);
  2874. $expected = array('first', array(
  2875. 'conditions' => array('TestModel.field_name' => null),
  2876. 'fields' => null, 'order' => null, 'recursive' => null
  2877. ));
  2878. $this->assertEqual($result, $expected);
  2879. $result = $this->testDb->query('findByFieldName', array('= a'), $this->Model);
  2880. $expected = array('first', array(
  2881. 'conditions' => array('TestModel.field_name' => '= a'),
  2882. 'fields' => null, 'order' => null, 'recursive' => null
  2883. ));
  2884. $this->assertEqual($result, $expected);
  2885. $result = $this->testDb->query('findByFieldName', array(), $this->Model);
  2886. $expected = false;
  2887. $this->assertEqual($result, $expected);
  2888. $result = $this->testDb->query('directCall', array(), $this->Model);
  2889. $this->assertFalse($result);
  2890. $result = $this->testDb->query('directCall', true, $this->Model);
  2891. $this->assertFalse($result);
  2892. $result = $this->testDb->query('directCall', false, $this->Model);
  2893. $this->assertFalse($result);
  2894. }
  2895. /**
  2896. * testOrderParsing method
  2897. *
  2898. * @access public
  2899. * @return void
  2900. */
  2901. function testOrderParsing() {
  2902. $result = $this->testDb->order("ADDTIME(Event.time_begin, '-06:00:00') ASC");
  2903. $expected = " ORDER BY ADDTIME(`Event`.`time_begin`, '-06:00:00') ASC";
  2904. $this->assertEqual($result, $expected);
  2905. $result = $this->testDb->order("title, id");
  2906. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
  2907. $result = $this->testDb->order("title desc, id desc");
  2908. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result);
  2909. $result = $this->testDb->order(array("title desc, id desc"));
  2910. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result);
  2911. $result = $this->testDb->order(array("title", "id"));
  2912. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
  2913. $result = $this->testDb->order(array(array('title'), array('id')));
  2914. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
  2915. $result = $this->testDb->order(array("Post.title" => 'asc', "Post.id" => 'desc'));
  2916. $this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result);
  2917. $result = $this->testDb->order(array(array("Post.title" => 'asc', "Post.id" => 'desc')));
  2918. $this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result);
  2919. $result = $this->testDb->order(array("title"));
  2920. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
  2921. $result = $this->testDb->order(array(array("title")));
  2922. $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
  2923. $result = $this->testDb->order("Dealer.id = 7 desc, Dealer.id = 3 desc, Dealer.title asc");
  2924. $expected = " ORDER BY `Dealer`.`id` = 7 desc, `Dealer`.`id` = 3 desc, `Dealer`.`title` asc";
  2925. $this->assertEqual($result, $expected);
  2926. $result = $this->testDb->order(array("Page.name" => "='test' DESC"));
  2927. $this->assertPattern("/^\s*ORDER BY\s+`Page`\.`name`\s*='test'\s+DESC\s*$/", $result);
  2928. $result = $this->testDb->order("Page.name = 'view' DESC");
  2929. $this->assertPattern("/^\s*ORDER BY\s+`Page`\.`name`\s*=\s*'view'\s+DESC\s*$/", $result);
  2930. $result = $this->testDb->order("(Post.views)");
  2931. $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\s+ASC\s*$/", $result);
  2932. $result = $this->testDb->order("(Post.views)*Post.views");
  2933. $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\*`Post`\.`views`\s+ASC\s*$/", $result);
  2934. $result = $this->testDb->order("(Post.views) * Post.views");
  2935. $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\) \* `Post`\.`views`\s+ASC\s*$/", $result);
  2936. $result = $this->testDb->order("(Model.field1 + Model.field2) * Model.field3");
  2937. $this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field1` \+ `Model`\.`field2`\) \* `Model`\.`field3`\s+ASC\s*$/", $result);
  2938. $result = $this->testDb->order("Model.name+0 ASC");
  2939. $this->assertPattern("/^\s*ORDER BY\s+`Model`\.`name`\+0\s+ASC\s*$/", $result);
  2940. $result = $this->testDb->order("Anuncio.destaque & 2 DESC");
  2941. $expected = ' ORDER BY `Anuncio`.`destaque` & 2 DESC';
  2942. $this->assertEqual($result, $expected);
  2943. $result = $this->testDb->order("3963.191 * id");
  2944. $expected = ' ORDER BY 3963.191 * id ASC';
  2945. $this->assertEqual($result, $expected);
  2946. $result = $this->testDb->order(array('Property.sale_price IS NULL'));
  2947. $expected = ' ORDER BY `Property`.`sale_price` IS NULL ASC';
  2948. $this->assertEqual($result, $expected);
  2949. }
  2950. /**
  2951. * testComplexSortExpression method
  2952. *
  2953. * @return void
  2954. * @access public
  2955. */
  2956. function testComplexSortExpression() {
  2957. $result = $this->testDb->order(array('(Model.field > 100) DESC', 'Model.field ASC'));
  2958. $this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field`\s+>\s+100\)\s+DESC,\s+`Model`\.`field`\s+ASC\s*$/", $result);
  2959. }
  2960. /**
  2961. * testCalculations method
  2962. *
  2963. * @access public
  2964. * @return void
  2965. */
  2966. function testCalculations() {
  2967. $result = $this->testDb->calculate($this->Model, 'count');
  2968. $this->assertEqual($result, 'COUNT(*) AS `count`');
  2969. $result = $this->testDb->calculate($this->Model, 'count', array('id'));
  2970. $this->assertEqual($result, 'COUNT(`id`) AS `count`');
  2971. $result = $this->testDb->calculate(
  2972. $this->Model,
  2973. 'count',
  2974. array($this->testDb->expression('DISTINCT id'))
  2975. );
  2976. $this->assertEqual($result, 'COUNT(DISTINCT id) AS `count`');
  2977. $result = $this->testDb->calculate($this->Model, 'count', array('id', 'id_count'));
  2978. $this->assertEqual($result, 'COUNT(`id`) AS `id_count`');
  2979. $result = $this->testDb->calculate($this->Model, 'count', array('Model.id', 'id_count'));
  2980. $this->assertEqual($result, 'COUNT(`Model`.`id`) AS `id_count`');
  2981. $result = $this->testDb->calculate($this->Model, 'max', array('id'));
  2982. $this->assertEqual($result, 'MAX(`id`) AS `id`');
  2983. $result = $this->testDb->calculate($this->Model, 'max', array('Model.id', 'id'));
  2984. $this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`');
  2985. $result = $this->testDb->calculate($this->Model, 'max', array('`Model`.`id`', 'id'));
  2986. $this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`');
  2987. $result = $this->testDb->calculate($this->Model, 'min', array('`Model`.`id`', 'id'));
  2988. $this->assertEqual($result, 'MIN(`Model`.`id`) AS `id`');
  2989. $result = $this->testDb->calculate($this->Model, 'min', 'left');
  2990. $this->assertEqual($result, 'MIN(`left`) AS `left`');
  2991. }
  2992. /**
  2993. * testLength method
  2994. *
  2995. * @access public
  2996. * @return void
  2997. */
  2998. function testLength() {
  2999. $result = $this->testDb->length('varchar(255)');
  3000. $expected = 255;
  3001. $this->assertIdentical($result, $expected);
  3002. $result = $this->testDb->length('int(11)');
  3003. $expected = 11;
  3004. $this->assertIdentical($result, $expected);
  3005. $result = $this->testDb->length('float(5,3)');
  3006. $expected = '5,3';
  3007. $this->assertIdentical($result, $expected);
  3008. $result = $this->testDb->length('decimal(5,2)');
  3009. $expected = '5,2';
  3010. $this->assertIdentical($result, $expected);
  3011. $result = $this->testDb->length("enum('test','me','now')");
  3012. $expected = 4;
  3013. $this->assertIdentical($result, $expected);
  3014. $result = $this->testDb->length("set('a','b','cd')");
  3015. $expected = 2;
  3016. $this->assertIdentical($result, $expected);
  3017. $this->expectError();
  3018. $result = $this->testDb->length(false);
  3019. $this->assertTrue($result === null);
  3020. $result = $this->testDb->length('datetime');
  3021. $expected = null;
  3022. $this->assertIdentical($result, $expected);
  3023. $result = $this->testDb->length('text');
  3024. $expected = null;
  3025. $this->assertIdentical($result, $expected);
  3026. }
  3027. /**
  3028. * testBuildIndex method
  3029. *
  3030. * @access public
  3031. * @return void
  3032. */
  3033. function testBuildIndex() {
  3034. $data = array(
  3035. 'PRIMARY' => array('column' => 'id')
  3036. );
  3037. $result = $this->testDb->buildIndex($data);
  3038. $expected = array('PRIMARY KEY (`id`)');
  3039. $this->assertIdentical($result, $expected);
  3040. $data = array(
  3041. 'MyIndex' => array('column' => 'id', 'unique' => true)
  3042. );
  3043. $result = $this->testDb->buildIndex($data);
  3044. $expected = array('UNIQUE KEY `MyIndex` (`id`)');
  3045. $this->assertEqual($result, $expected);
  3046. $data = array(
  3047. 'MyIndex' => array('column' => array('id', 'name'), 'unique' => true)
  3048. );
  3049. $result = $this->testDb->buildIndex($data);
  3050. $expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)');
  3051. $this->assertEqual($result, $expected);
  3052. }
  3053. /**
  3054. * testBuildColumn method
  3055. *
  3056. * @access public
  3057. * @return void
  3058. */
  3059. function testBuildColumn() {
  3060. $this->expectError();
  3061. $data = array(
  3062. 'name' => 'testName',
  3063. 'type' => 'varchar(255)',
  3064. 'default',
  3065. 'null' => true,
  3066. 'key'
  3067. );
  3068. $this->testDb->buildColumn($data);
  3069. $data = array(
  3070. 'name' => 'testName',
  3071. 'type' => 'string',
  3072. 'length' => 255,
  3073. 'default',
  3074. 'null' => true,
  3075. 'key'
  3076. );
  3077. $result = $this->testDb->buildColumn($data);
  3078. $expected = '`testName` varchar(255) DEFAULT NULL';
  3079. $this->assertEqual($result, $expected);
  3080. $data = array(
  3081. 'name' => 'int_field',
  3082. 'type' => 'integer',
  3083. 'default' => '',
  3084. 'null' => false,
  3085. );
  3086. $this->testDb->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), );
  3087. $result = $this->testDb->buildColumn($data);
  3088. $expected = '`int_field` int(11) NOT NULL';
  3089. $this->assertEqual($result, $expected);
  3090. $this->testDb->fieldParameters['param'] = array(
  3091. 'value' => 'COLLATE',
  3092. 'quote' => false,
  3093. 'join' => ' ',
  3094. 'column' => 'Collate',
  3095. 'position' => 'beforeDefault',
  3096. 'options' => array('GOOD', 'OK')
  3097. );
  3098. $data = array(
  3099. 'name' => 'int_field',
  3100. 'type' => 'integer',
  3101. 'default' => '',
  3102. 'null' => false,
  3103. 'param' => 'BAD'
  3104. );
  3105. $result = $this->testDb->buildColumn($data);
  3106. $expected = '`int_field` int(11) NOT NULL';
  3107. $this->assertEqual($result, $expected);
  3108. $data = array(
  3109. 'name' => 'int_field',
  3110. 'type' => 'integer',
  3111. 'default' => '',
  3112. 'null' => false,
  3113. 'param' => 'GOOD'
  3114. );
  3115. $result = $this->testDb->buildColumn($data);
  3116. $expected = '`int_field` int(11) COLLATE GOOD NOT NULL';
  3117. $this->assertEqual($result, $expected);
  3118. }
  3119. /**
  3120. * test hasAny()
  3121. *
  3122. * @return void
  3123. */
  3124. function testHasAny() {
  3125. $this->testDb->hasAny($this->Model, array());
  3126. $expected = 'SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1';
  3127. $this->assertEqual(end($this->testDb->simulated), $expected);
  3128. $this->testDb->hasAny($this->Model, array('TestModel.name' => 'harry'));
  3129. $expected = "SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = 'harry'";
  3130. $this->assertEqual(end($this->testDb->simulated), $expected);
  3131. }
  3132. /**
  3133. * testIntrospectType method
  3134. *
  3135. * @access public
  3136. * @return void
  3137. */
  3138. function testIntrospectType() {
  3139. $this->assertEqual($this->testDb->introspectType(0), 'integer');
  3140. $this->assertEqual($this->testDb->introspectType(2), 'integer');
  3141. $this->assertEqual($this->testDb->introspectType('2'), 'string');
  3142. $this->assertEqual($this->testDb->introspectType('2.2'), 'string');
  3143. $this->assertEqual($this->testDb->introspectType(2.2), 'float');
  3144. $this->assertEqual($this->testDb->introspectType('stringme'), 'string');
  3145. $this->assertEqual($this->testDb->introspectType('0stringme'), 'string');
  3146. $data = array(2.2);
  3147. $this->assertEqual($this->testDb->introspectType($data), 'float');
  3148. $data = array('2.2');
  3149. $this->assertEqual($this->testDb->introspectType($data), 'float');
  3150. $data = array(2);
  3151. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3152. $data = array('2');
  3153. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3154. $data = array('string');
  3155. $this->assertEqual($this->testDb->introspectType($data), 'string');
  3156. $data = array(2.2, '2.2');
  3157. $this->assertEqual($this->testDb->introspectType($data), 'float');
  3158. $data = array(2, '2');
  3159. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3160. $data = array('string one', 'string two');
  3161. $this->assertEqual($this->testDb->introspectType($data), 'string');
  3162. $data = array('2.2', 3);
  3163. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3164. $data = array('2.2', '0stringme');
  3165. $this->assertEqual($this->testDb->introspectType($data), 'string');
  3166. $data = array(2.2, 3);
  3167. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3168. $data = array(2.2, '0stringme');
  3169. $this->assertEqual($this->testDb->introspectType($data), 'string');
  3170. $data = array(2, 'stringme');
  3171. $this->assertEqual($this->testDb->introspectType($data), 'string');
  3172. $data = array(2, '2.2', 'stringgme');
  3173. $this->assertEqual($this->testDb->introspectType($data), 'string');
  3174. $data = array(2, '2.2');
  3175. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3176. $data = array(2, 2.2);
  3177. $this->assertEqual($this->testDb->introspectType($data), 'integer');
  3178. // NULL
  3179. $result = $this->testDb->value(null, 'boolean');
  3180. $this->assertEqual($result, 'NULL');
  3181. // EMPTY STRING
  3182. $result = $this->testDb->value('', 'boolean');
  3183. $this->assertEqual($result, "NULL");
  3184. // BOOLEAN
  3185. $result = $this->testDb->value('true', 'boolean');
  3186. $this->assertEqual($result, 1);
  3187. $result = $this->testDb->value('false', 'boolean');
  3188. $this->assertEqual($result, 1);
  3189. $result = $this->testDb->value(true, 'boolean');
  3190. $this->assertEqual($result, 1);
  3191. $result = $this->testDb->value(false, 'boolean');
  3192. $this->assertEqual($result, 0);
  3193. $result = $this->testDb->value(1, 'boolean');
  3194. $this->assertEqual($result, 1);
  3195. $result = $this->testDb->value(0, 'boolean');
  3196. $this->assertEqual($result, 0);
  3197. $result = $this->testDb->value('abc', 'boolean');
  3198. $this->assertEqual($result, 1);
  3199. $result = $this->testDb->value(1.234, 'boolean');
  3200. $this->assertEqual($result, 1);
  3201. $result = $this->testDb->value('1.234e05', 'boolean');
  3202. $this->assertEqual($result, 1);
  3203. // NUMBERS
  3204. $result = $this->testDb->value(123, 'integer');
  3205. $this->assertEqual($result, 123);
  3206. $result = $this->testDb->value('123', 'integer');
  3207. $this->assertEqual($result, '123');
  3208. $result = $this->testDb->value('0123', 'integer');
  3209. $this->assertEqual($result, "'0123'");
  3210. $result = $this->testDb->value('0x123ABC', 'integer');
  3211. $this->assertEqual($result, "'0x123ABC'");
  3212. $result = $this->testDb->value('0x123', 'integer');
  3213. $this->assertEqual($result, "'0x123'");
  3214. $result = $this->testDb->value(1.234, 'float');
  3215. $this->assertEqual($result, 1.234);
  3216. $result = $this->testDb->value('1.234', 'float');
  3217. $this->assertEqual($result, '1.234');
  3218. $result = $this->testDb->value(' 1.234 ', 'float');
  3219. $this->assertEqual($result, "' 1.234 '");
  3220. $result = $this->testDb->value('1.234e05', 'float');
  3221. $this->assertEqual($result, "'1.234e05'");
  3222. $result = $this->testDb->value('1.234e+5', 'float');
  3223. $this->assertEqual($result, "'1.234e+5'");
  3224. $result = $this->testDb->value('1,234', 'float');
  3225. $this->assertEqual($result, "'1,234'");
  3226. $result = $this->testDb->value('FFF', 'integer');
  3227. $this->assertEqual($result, "'FFF'");
  3228. $result = $this->testDb->value('abc', 'integer');
  3229. $this->assertEqual($result, "'abc'");
  3230. // STRINGS
  3231. $result = $this->testDb->value('123', 'string');
  3232. $this->assertEqual($result, "'123'");
  3233. $result = $this->testDb->value(123, 'string');
  3234. $this->assertEqual($result, "'123'");
  3235. $result = $this->testDb->value(1.234, 'string');
  3236. $this->assertEqual($result, "'1.234'");
  3237. $result = $this->testDb->value('abc', 'string');
  3238. $this->assertEqual($result, "'abc'");
  3239. $result = $this->testDb->value(' abc ', 'string');
  3240. $this->assertEqual($result, "' abc '");
  3241. $result = $this->testDb->value('a bc', 'string');
  3242. $this->assertEqual($result, "'a bc'");
  3243. }
  3244. /**
  3245. * testValue method
  3246. *
  3247. * @access public
  3248. * @return void
  3249. */
  3250. function testValue() {
  3251. $result = $this->testDb->value('{$__cakeForeignKey__$}');
  3252. $this->assertEqual($result, '{$__cakeForeignKey__$}');
  3253. $result = $this->testDb->value(array('first', 2, 'third'));
  3254. $expected = array('\'first\'', 2, '\'third\'');
  3255. $this->assertEqual($result, $expected);
  3256. }
  3257. /**
  3258. * testReconnect method
  3259. *
  3260. * @access public
  3261. * @return void
  3262. */
  3263. function testReconnect() {
  3264. $this->testDb->reconnect(array('prefix' => 'foo'));
  3265. $this->assertTrue($this->testDb->connected);
  3266. $this->assertEqual($this->testDb->config['prefix'], 'foo');
  3267. }
  3268. /**
  3269. * testRealQueries method
  3270. *
  3271. * @access public
  3272. * @return void
  3273. */
  3274. function testRealQueries() {
  3275. $this->loadFixtures('Apple', 'Article', 'User', 'Comment', 'Tag');
  3276. $Apple = ClassRegistry::init('Apple');
  3277. $Article = ClassRegistry::init('Article');
  3278. $result = $this->db->rawQuery('SELECT color, name FROM ' . $this->db->fullTableName('apples'));
  3279. $this->assertTrue(!empty($result));
  3280. $result = $this->db->fetchRow($result);
  3281. $expected = array($this->db->fullTableName('apples', false) => array(
  3282. 'color' => 'Red 1',
  3283. 'name' => 'Red Apple 1'
  3284. ));
  3285. $this->assertEqual($result, $expected);
  3286. $result = $this->db->fetchAll('SELECT name FROM ' . $this->testDb->fullTableName('apples') . ' ORDER BY id');
  3287. $expected = array(
  3288. array($this->db->fullTableName('apples', false) => array('name' => 'Red Apple 1')),
  3289. array($this->db->fullTableName('apples', false) => array('name' => 'Bright Red Apple')),
  3290. array($this->db->fullTableName('apples', false) => array('name' => 'green blue')),
  3291. array($this->db->fullTableName('apples', false) => array('name' => 'Test Name')),
  3292. array($this->db->fullTableName('apples', false) => array('name' => 'Blue Green')),
  3293. array($this->db->fullTableName('apples', false) => array('name' => 'My new apple')),
  3294. array($this->db->fullTableName('apples', false) => array('name' => 'Some odd color'))
  3295. );
  3296. $this->assertEqual($result, $expected);
  3297. $result = $this->db->field($this->testDb->fullTableName('apples', false), 'SELECT color, name FROM ' . $this->testDb->fullTableName('apples') . ' ORDER BY id');
  3298. $expected = array(
  3299. 'color' => 'Red 1',
  3300. 'name' => 'Red Apple 1'
  3301. );
  3302. $this->assertEqual($result, $expected);
  3303. $Apple->unbindModel(array(), false);
  3304. $result = $this->db->read($Apple, array(
  3305. 'fields' => array($Apple->escapeField('name')),
  3306. 'conditions' => null,
  3307. 'recursive' => -1
  3308. ));
  3309. $expected = array(
  3310. array('Apple' => array('name' => 'Red Apple 1')),
  3311. array('Apple' => array('name' => 'Bright Red Apple')),
  3312. array('Apple' => array('name' => 'green blue')),
  3313. array('Apple' => array('name' => 'Test Name')),
  3314. array('Apple' => array('name' => 'Blue Green')),
  3315. array('Apple' => array('name' => 'My new apple')),
  3316. array('Apple' => array('name' => 'Some odd color'))
  3317. );
  3318. $this->assertEqual($result, $expected);
  3319. $result = $this->db->read($Article, array(
  3320. 'fields' => array('id', 'user_id', 'title'),
  3321. 'conditions' => null,
  3322. 'recursive' => 1
  3323. ));
  3324. $this->assertTrue(Set::matches('/Article[id=1]', $result));
  3325. $this->assertTrue(Set::matches('/Comment[id=1]', $result));
  3326. $this->assertTrue(Set::matches('/Comment[id=2]', $result));
  3327. $this->assertFalse(Set::matches('/Comment[id=10]', $result));
  3328. }
  3329. /**
  3330. * testName method
  3331. *
  3332. * @access public
  3333. * @return void
  3334. */
  3335. function testName() {
  3336. $result = $this->testDb->name('name');
  3337. $expected = '`name`';
  3338. $this->assertEqual($result, $expected);
  3339. $result = $this->testDb->name(array('name', 'Model.*'));
  3340. $expected = array('`name`', '`Model`.*');
  3341. $this->assertEqual($result, $expected);
  3342. $result = $this->testDb->name('MTD()');
  3343. $expected = 'MTD()';
  3344. $this->assertEqual($result, $expected);
  3345. $result = $this->testDb->name('(sm)');
  3346. $expected = '(sm)';
  3347. $this->assertEqual($result, $expected);
  3348. $result = $this->testDb->name('Function(Something.foo)');
  3349. $expected = 'Function(`Something`.`foo`)';
  3350. $this->assertEqual($result, $expected);
  3351. }
  3352. /**
  3353. * testLog method
  3354. *
  3355. * @access public
  3356. * @return void
  3357. */
  3358. function testLog() {
  3359. $this->testDb->logQuery('Query 1');
  3360. $this->testDb->logQuery('Query 2');
  3361. $result = Set::extract($this->testDb->_queriesLog, '/query');
  3362. $expected = array('Query 1', 'Query 2');
  3363. $this->assertEqual($result, $expected);
  3364. $oldError = $this->testDb->error;
  3365. $this->testDb->error = true;
  3366. $result = $this->testDb->logQuery('Error 1');
  3367. $this->assertFalse($result);
  3368. $this->testDb->error = $oldError;
  3369. $result = Set::combine($this->testDb->_queriesLog, '/query', '/error');
  3370. $expected = array('Query 1' => false, 'Query 2' => false, 'Error 1' => true);
  3371. $this->assertEqual($result, $expected);
  3372. ob_start();
  3373. $this->testDb->showLog();
  3374. $contents = ob_get_clean();
  3375. $this->assertPattern('/Query 1/s', $contents);
  3376. $this->assertPattern('/Query 2/s', $contents);
  3377. $this->assertPattern('/Error 1/s', $contents);
  3378. ob_start();
  3379. $this->testDb->showLog(true);
  3380. $contents = ob_get_clean();
  3381. $this->assertPattern('/Query 1/s', $contents);
  3382. $this->assertPattern('/Query 2/s', $contents);
  3383. $this->assertPattern('/Error 1/s', $contents);
  3384. $oldError = $this->testDb->error;
  3385. $oldDebug = Configure::read('debug');
  3386. Configure::write('debug', 2);
  3387. $this->testDb->error = true;
  3388. $this->expectError();
  3389. ob_start();
  3390. $this->testDb->showQuery('Error 2');
  3391. $contents = ob_get_clean();
  3392. $this->assertPattern('/Error 2/s', $contents);
  3393. $this->testDb->error = $oldError;
  3394. Configure::write('debug', $oldDebug);
  3395. }
  3396. /**
  3397. * test getting the query log as an array.
  3398. *
  3399. * @return void
  3400. */
  3401. function testGetLog() {
  3402. $this->testDb->logQuery('Query 1');
  3403. $this->testDb->logQuery('Query 2');
  3404. $oldError = $this->testDb->error;
  3405. $this->testDb->error = true;
  3406. $result = $this->testDb->logQuery('Error 1');
  3407. $this->assertFalse($result);
  3408. $this->testDb->error = $oldError;
  3409. $log = $this->testDb->getLog();
  3410. $expected = array('query' => 'Query 1', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
  3411. $this->assertEqual($log[0], $expected);
  3412. $expected = array('query' => 'Query 2', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
  3413. $this->assertEqual($log[1], $expected);
  3414. $expected = array('query' => 'Error 1', 'error' => true, 'affected' => '', 'numRows' => '', 'took' => '');
  3415. $this->assertEqual($log[2], $expected);
  3416. }
  3417. /**
  3418. * test that execute runs queries.
  3419. *
  3420. * @return void
  3421. */
  3422. function testExecute() {
  3423. $query = 'SELECT * FROM ' . $this->testDb->fullTableName('articles') . ' WHERE 1 = 1';
  3424. $this->db->_result = null;
  3425. $this->db->took = null;
  3426. $this->db->affected = null;
  3427. $result = $this->db->execute($query, array('stats' => false));
  3428. $this->assertNotNull($result, 'No query performed! %s');
  3429. $this->assertNull($this->db->took, 'Stats were set %s');
  3430. $this->assertNull($this->db->affected, 'Stats were set %s');
  3431. $result = $this->db->execute($query);
  3432. $this->assertNotNull($result, 'No query performed! %s');
  3433. $this->assertNotNull($this->db->took, 'Stats were not set %s');
  3434. $this->assertNotNull($this->db->affected, 'Stats were not set %s');
  3435. }
  3436. /**
  3437. * test that query() returns boolean values from operations like CREATE TABLE
  3438. *
  3439. * @return void
  3440. */
  3441. function testFetchAllBooleanReturns() {
  3442. $name = $this->db->fullTableName('test_query');
  3443. $query = "CREATE TABLE {$name} (name varchar(10));";
  3444. $result = $this->db->query($query);
  3445. $this->assertTrue($result, 'Query did not return a boolean. %s');
  3446. $query = "DROP TABLE {$name};";
  3447. $result = $this->db->fetchAll($query);
  3448. $this->assertTrue($result, 'Query did not return a boolean. %s');
  3449. }
  3450. /**
  3451. * test ShowQuery generation of regular and error messages
  3452. *
  3453. * @return void
  3454. */
  3455. function testShowQuery() {
  3456. $this->testDb->error = false;
  3457. ob_start();
  3458. $this->testDb->showQuery('Some Query');
  3459. $contents = ob_get_clean();
  3460. $this->assertPattern('/Some Query/s', $contents);
  3461. $this->assertPattern('/Aff:/s', $contents);
  3462. $this->assertPattern('/Num:/s', $contents);
  3463. $this->assertPattern('/Took:/s', $contents);
  3464. $this->expectError();
  3465. $this->testDb->error = true;
  3466. ob_start();
  3467. $this->testDb->showQuery('Another Query');
  3468. $contents = ob_get_clean();
  3469. $this->assertPattern('/Another Query/s', $contents);
  3470. $this->assertNoPattern('/Aff:/s', $contents);
  3471. $this->assertNoPattern('/Num:/s', $contents);
  3472. $this->assertNoPattern('/Took:/s', $contents);
  3473. }
  3474. }
  3475. ?>