PageRenderTime 41ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/model/datasources/dbo_source.test.php

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