comparison cake/tests/cases/libs/model/datasources/dbo_source.test.php @ 0:261e66bd5a0c

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