7
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The Open Group Test Suite License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
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
20
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
21
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
23
App::import('Model', array('Model', 'DataSource', 'DboSource', 'DboMysql', 'App'));
24
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
30
* @subpackage cake.tests.cases.libs.model.datasources
32
class TestModel extends CakeTestModel {
37
* @var string 'TestModel'
40
var $name = 'TestModel';
48
var $useTable = false;
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)
80
* @param mixed $conditions
81
* @param mixed $fields
83
* @param mixed $recursive
87
function find($conditions = null, $fields = null, $order = null, $recursive = null) {
88
return array($conditions, $fields);
94
* @param mixed $conditions
95
* @param mixed $fields
97
* @param mixed $recursive
101
function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
110
* @subpackage cake.tests.cases.libs.model.datasources
112
class TestModel2 extends CakeTestModel {
117
* @var string 'TestModel2'
120
var $name = 'TestModel2';
128
var $useTable = false;
135
* @subpackage cake.tests.cases.libs.model.datasources
137
class TestModel3 extends CakeTestModel {
142
* @var string 'TestModel3'
145
var $name = 'TestModel3';
153
var $useTable = false;
160
* @subpackage cake.tests.cases.libs.model.datasources
162
class TestModel4 extends CakeTestModel {
167
* @var string 'TestModel4'
170
var $name = 'TestModel4';
175
* @var string 'test_model4'
178
var $table = 'test_model4';
186
var $useTable = false;
194
var $belongsTo = array(
195
'TestModel4Parent' => array(
196
'className' => 'TestModel4',
197
'foreignKey' => 'parent_id'
208
'TestModel5' => array(
209
'className' => 'TestModel5',
210
'foreignKey' => 'test_model4_id'
215
* hasAndBelongsToMany property
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'
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)
243
return $this->_schema;
248
* TestModel4TestModel7 class
251
* @subpackage cake.tests.cases.libs.model.datasources
253
class TestModel4TestModel7 extends CakeTestModel {
258
* @var string 'TestModel4TestModel7'
261
var $name = 'TestModel4TestModel7';
266
* @var string 'test_model4_test_model7'
269
var $table = 'test_model4_test_model7';
277
var $useTable = false;
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')
292
return $this->_schema;
300
* @subpackage cake.tests.cases.libs.model.datasources
302
class TestModel5 extends CakeTestModel {
307
* @var string 'TestModel5'
310
var $name = 'TestModel5';
315
* @var string 'test_model5'
318
var $table = 'test_model5';
326
var $useTable = false;
334
var $belongsTo = array('TestModel4' => array(
335
'className' => 'TestModel4',
336
'foreignKey' => 'test_model4_id'
345
var $hasMany = array('TestModel6' => array(
346
'className' => 'TestModel6',
347
'foreignKey' => 'test_model5_id'
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)
366
return $this->_schema;
374
* @subpackage cake.tests.cases.libs.model.datasources
376
class TestModel6 extends CakeTestModel {
381
* @var string 'TestModel6'
384
var $name = 'TestModel6';
389
* @var string 'test_model6'
392
var $table = 'test_model6';
400
var $useTable = false;
408
var $belongsTo = array('TestModel5' => array(
409
'className' => 'TestModel5',
410
'foreignKey' => 'test_model5_id'
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)
429
return $this->_schema;
437
* @subpackage cake.tests.cases.libs.model.datasources
439
class TestModel7 extends CakeTestModel {
444
* @var string 'TestModel7'
447
var $name = 'TestModel7';
452
* @var string 'test_model7'
455
var $table = 'test_model7';
463
var $useTable = false;
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)
480
return $this->_schema;
488
* @subpackage cake.tests.cases.libs.model.datasources
490
class TestModel8 extends CakeTestModel {
495
* @var string 'TestModel8'
498
var $name = 'TestModel8';
503
* @var string 'test_model8'
506
var $table = 'test_model8';
514
var $useTable = false;
523
'TestModel9' => array(
524
'className' => 'TestModel9',
525
'foreignKey' => 'test_model8_id',
526
'conditions' => 'TestModel9.name != \'mariano\''
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)
546
return $this->_schema;
554
* @subpackage cake.tests.cases.libs.model.datasources
556
class TestModel9 extends CakeTestModel {
561
* @var string 'TestModel9'
564
var $name = 'TestModel9';
569
* @var string 'test_model9'
572
var $table = 'test_model9';
580
var $useTable = false;
588
var $belongsTo = array('TestModel8' => array(
589
'className' => 'TestModel8',
590
'foreignKey' => 'test_model8_id',
591
'conditions' => 'TestModel8.name != \'larry\''
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)
610
return $this->_schema;
618
* @subpackage cake.tests.cases.libs.model.datasources
620
class Level extends CakeTestModel {
625
* @var string 'Level'
633
* @var string 'level'
636
var $table = 'level';
644
var $useTable = false;
652
var $hasMany = array(
654
'className' => 'Group'
657
'className' => 'User2'
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'),
674
return $this->_schema;
682
* @subpackage cake.tests.cases.libs.model.datasources
684
class Group extends CakeTestModel {
689
* @var string 'Group'
697
* @var string 'group'
700
var $table = 'group';
708
var $useTable = false;
716
var $belongsTo = array('Level');
724
var $hasMany = array('Category2', 'User2');
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'),
740
return $this->_schema;
749
* @subpackage cake.tests.cases.libs.model.datasources
751
class User2 extends CakeTestModel {
756
* @var string 'User2'
775
var $useTable = false;
783
var $belongsTo = array(
785
'className' => 'Group'
788
'className' => 'Level'
798
var $hasMany = array(
800
'className' => 'Article2'
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'),
819
return $this->_schema;
827
* @subpackage cake.tests.cases.libs.model.datasources
829
class Category2 extends CakeTestModel {
834
* @var string 'Category2'
837
var $name = 'Category2';
842
* @var string 'category'
845
var $table = 'category';
853
var $useTable = false;
861
var $belongsTo = array(
863
'className' => 'Group',
864
'foreignKey' => 'group_id'
866
'ParentCat' => array(
867
'className' => 'Category2',
868
'foreignKey' => 'parent_id'
878
var $hasMany = array(
880
'className' => 'Category2',
881
'foreignKey' => 'parent_id'
884
'className' => 'Article2',
885
'order'=>'Article2.published_date DESC',
886
'foreignKey' => 'category_id',
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),
908
return $this->_schema;
916
* @subpackage cake.tests.cases.libs.model.datasources
918
class Article2 extends CakeTestModel {
923
* @var string 'Article2'
926
var $name = 'Article2';
931
* @var string 'article'
934
var $table = 'article';
942
var $useTable = false;
950
var $belongsTo = array(
951
'Category2' => array('className' => 'Category2'),
952
'User2' => array('className' => 'User2')
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)
985
return $this->_schema;
990
* CategoryFeatured2 class
993
* @subpackage cake.tests.cases.libs.model.datasources
995
class CategoryFeatured2 extends CakeTestModel {
1000
* @var string 'CategoryFeatured2'
1003
var $name = 'CategoryFeatured2';
1008
* @var string 'category_featured'
1011
var $table = 'category_featured';
1019
var $useTable = false;
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)
1037
return $this->_schema;
1045
* @subpackage cake.tests.cases.libs.model.datasources
1047
class Featured2 extends CakeTestModel {
1052
* @var string 'Featured2'
1055
var $name = 'Featured2';
1060
* @var string 'featured2'
1063
var $table = 'featured2';
1071
var $useTable = false;
1074
* belongsTo property
1079
var $belongsTo = array(
1080
'CategoryFeatured2' => array(
1081
'className' => 'CategoryFeatured2'
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')
1100
return $this->_schema;
1108
* @subpackage cake.tests.cases.libs.model.datasources
1110
class Comment2 extends CakeTestModel {
1115
* @var string 'Comment2'
1118
var $name = 'Comment2';
1123
* @var string 'comment'
1126
var $table = 'comment';
1129
* belongsTo property
1134
var $belongsTo = array('ArticleFeatured2', 'User2');
1142
var $useTable = false;
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')
1159
return $this->_schema;
1164
* ArticleFeatured2 class
1167
* @subpackage cake.tests.cases.libs.model.datasources
1169
class ArticleFeatured2 extends CakeTestModel {
1174
* @var string 'ArticleFeatured2'
1177
var $name = 'ArticleFeatured2';
1182
* @var string 'article_featured'
1185
var $table = 'article_featured';
1193
var $useTable = false;
1196
* belongsTo property
1201
var $belongsTo = array(
1202
'CategoryFeatured2' => array('className' => 'CategoryFeatured2'),
1203
'User2' => array('className' => 'User2')
1212
var $hasOne = array(
1213
'Featured2' => array('className' => 'Featured2')
1222
var $hasMany = array(
1223
'Comment2' => array('className'=>'Comment2', 'dependent' => true)
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)
1246
return $this->_schema;
1251
* DboSourceTest class
1254
* @subpackage cake.tests.cases.libs.model.datasources
1256
class DboSourceTest extends CakeTestCase {
1267
* autoFixtures property
1272
var $autoFixtures = false;
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'
1291
function startTest() {
1292
$this->__config = $this->db->config;
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();
1307
function _execute(\$sql) {
1308
\$this->simulated[] = \$sql;
1313
* getLastQuery method
1318
function getLastQuery() {
1319
return \$this->simulated[count(\$this->simulated) - 1];
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();
1339
function endTest() {
1340
unset($this->Model);
1341
Configure::write('debug', $this->debug);
1342
ClassRegistry::flush();
1343
unset($this->debug);
1347
* testFieldDoubleEscaping method
1352
function testFieldDoubleEscaping() {
1353
$config = array_merge($this->__config, array('driver' => 'test'));
1354
$test =& ConnectionManager::create('quoteTest', $config);
1355
$test->simulated = array();
1357
$this->Model =& new Article2(array('alias' => 'Article', 'ds' => 'quoteTest'));
1358
$this->Model->setDataSource('quoteTest');
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`'));
1364
$result = $test->read($this->Model, array(
1365
'fields' => $this->Model->escapeField(),
1366
'conditions' => null,
1369
$this->assertEqual(trim($test->simulated[0]), 'SELECT `Article`.`id` FROM `' . $this->testDb->fullTableName('article', false) . '` AS `Article` WHERE 1 = 1');
1371
$test->startQuote = '[';
1372
$test->endQuote = ']';
1373
$this->assertEqual($this->Model->escapeField(), '[Article].[id]');
1375
$result = $test->fields($this->Model, null, $this->Model->escapeField());
1376
$this->assertEqual($result, array('[Article].[id]'));
1378
$result = $test->read($this->Model, array(
1379
'fields' => $this->Model->escapeField(),
1380
'conditions' => null,
1383
$this->assertEqual(trim($test->simulated[1]), 'SELECT [Article].[id] FROM [' . $this->testDb->fullTableName('article', false) . '] AS [Article] WHERE 1 = 1');
1385
ClassRegistry::removeObject('Article');
1389
* testGenerateAssociationQuerySelfJoin method
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();
1402
$queryData = array();
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']);
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);
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);
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);
1424
$this->Model =& new TestModel4();
1425
$this->Model->schema();
1426
$this->_buildRelatedModels($this->Model);
1428
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
1429
$queryData = array();
1433
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
1441
'`TestModel4`.`id`',
1442
'`TestModel4`.`name`',
1443
'`TestModel4`.`created`',
1444
'`TestModel4`.`updated`',
1445
'`TestModel4Parent`.`id`',
1446
'`TestModel4Parent`.`name`',
1447
'`TestModel4Parent`.`created`',
1448
'`TestModel4Parent`.`updated`'
1452
'table' => '`test_model4`',
1453
'alias' => 'TestModel4Parent',
1455
'conditions' => '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`'
1459
'offset' => array(),
1460
'conditions' => array(),
1464
$this->assertEqual($queryData, $expected);
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);
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');
1480
* testGenerateInnerJoinAssociationQuery method
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');
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);
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);
1504
* testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding method
1509
function testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding() {
1510
$this->Model =& new TestModel8();
1511
$this->Model->schema();
1512
$this->_buildRelatedModels($this->Model);
1514
$binding = array('type' => 'hasOne', 'model' => 'TestModel9');
1515
$queryData = array();
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);
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);
1532
* testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding method
1537
function testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding() {
1538
$this->Model =& new TestModel9();
1539
$this->Model->schema();
1540
$this->_buildRelatedModels($this->Model);
1542
$binding = array('type' => 'belongsTo', 'model' => 'TestModel8');
1543
$queryData = array();
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);
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);
1559
* testGenerateAssociationQuerySelfJoinWithConditions method
1564
function testGenerateAssociationQuerySelfJoinWithConditions() {
1565
$this->Model =& new TestModel4();
1566
$this->Model->schema();
1567
$this->_buildRelatedModels($this->Model);
1569
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
1570
$queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano'));
1574
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1576
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
1577
$this->assertTrue($result);
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);
1585
$this->Featured2 =& new Featured2();
1586
$this->Featured2->schema();
1588
$this->Featured2->bindModel(array(
1589
'belongsTo' => array(
1590
'ArticleFeatured2' => array(
1591
'conditions' => 'ArticleFeatured2.published = \'Y\'',
1592
'fields' => 'id, title, user_id, published'
1597
$this->_buildRelatedModels($this->Featured2);
1599
$binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2');
1600
$queryData = array('conditions' => array());
1604
$params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding);
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);
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*$/',
1621
* testGenerateAssociationQueryHasOne method
1626
function testGenerateAssociationQueryHasOne() {
1627
$this->Model =& new TestModel4();
1628
$this->Model->schema();
1629
$this->_buildRelatedModels($this->Model);
1631
$binding = array('type' => 'hasOne', 'model' => 'TestModel5');
1633
$queryData = array();
1637
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1639
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
1640
$this->assertTrue($result);
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));
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);
1654
* testGenerateAssociationQueryHasOneWithConditions method
1659
function testGenerateAssociationQueryHasOneWithConditions() {
1660
$this->Model =& new TestModel4();
1661
$this->Model->schema();
1662
$this->_buildRelatedModels($this->Model);
1664
$binding = array('type' => 'hasOne', 'model' => 'TestModel5');
1666
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
1670
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1672
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
1673
$this->assertTrue($result);
1675
$result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
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);
1684
* testGenerateAssociationQueryBelongsTo method
1689
function testGenerateAssociationQueryBelongsTo() {
1690
$this->Model =& new TestModel5();
1691
$this->Model->schema();
1692
$this->_buildRelatedModels($this->Model);
1694
$binding = array('type'=>'belongsTo', 'model'=>'TestModel4');
1695
$queryData = array();
1699
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1701
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
1702
$this->assertTrue($result);
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));
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);
1716
* testGenerateAssociationQueryBelongsToWithConditions method
1721
function testGenerateAssociationQueryBelongsToWithConditions() {
1722
$this->Model =& new TestModel5();
1723
$this->Model->schema();
1724
$this->_buildRelatedModels($this->Model);
1726
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
1727
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
1731
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1733
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
1734
$this->assertTrue($result);
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));
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);
1748
* testGenerateAssociationQueryHasMany method
1753
function testGenerateAssociationQueryHasMany() {
1754
$this->Model =& new TestModel5();
1755
$this->Model->schema();
1756
$this->_buildRelatedModels($this->Model);
1758
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1759
$queryData = array();
1763
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1765
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
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);
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);
1778
* testGenerateAssociationQueryHasManyWithLimit method
1783
function testGenerateAssociationQueryHasManyWithLimit() {
1784
$this->Model =& new TestModel5();
1785
$this->Model->schema();
1786
$this->_buildRelatedModels($this->Model);
1788
$this->Model->hasMany['TestModel6']['limit'] = 2;
1790
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1791
$queryData = array();
1795
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1797
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
1798
$this->assertPattern(
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*'.
1807
$result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
1808
$this->assertPattern(
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*(?:\))?'.
1818
* testGenerateAssociationQueryHasManyWithConditions method
1823
function testGenerateAssociationQueryHasManyWithConditions() {
1824
$this->Model =& new TestModel5();
1825
$this->Model->schema();
1826
$this->_buildRelatedModels($this->Model);
1828
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1829
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
1833
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
1847
* testGenerateAssociationQueryHasManyWithOffsetAndLimit method
1852
function testGenerateAssociationQueryHasManyWithOffsetAndLimit() {
1853
$this->Model =& new TestModel5();
1854
$this->Model->schema();
1855
$this->_buildRelatedModels($this->Model);
1857
$__backup = $this->Model->hasMany['TestModel6'];
1859
$this->Model->hasMany['TestModel6']['offset'] = 2;
1860
$this->Model->hasMany['TestModel6']['limit'] = 5;
1862
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1863
$queryData = array();
1867
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
1869
$result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
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);
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);
1881
$this->Model->hasMany['TestModel6'] = $__backup;
1885
* testGenerateAssociationQueryHasManyWithPageAndLimit method
1890
function testGenerateAssociationQueryHasManyWithPageAndLimit() {
1891
$this->Model =& new TestModel5();
1892
$this->Model->schema();
1893
$this->_buildRelatedModels($this->Model);
1895
$__backup = $this->Model->hasMany['TestModel6'];
1897
$this->Model->hasMany['TestModel6']['page'] = 2;
1898
$this->Model->hasMany['TestModel6']['limit'] = 5;
1900
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1901
$queryData = array();
1905
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
1918
$this->Model->hasMany['TestModel6'] = $__backup;
1922
* testGenerateAssociationQueryHasManyWithFields method
1927
function testGenerateAssociationQueryHasManyWithFields() {
1928
$this->Model =& new TestModel5();
1929
$this->Model->schema();
1930
$this->_buildRelatedModels($this->Model);
1932
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1933
$queryData = array('fields' => array('`TestModel5`.`name`'));
1937
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
1949
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1950
$queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`'));
1954
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
1966
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1967
$queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`'));
1971
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
1983
$this->Model->hasMany['TestModel6']['fields'] = array('name');
1985
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
1986
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
1990
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2002
unset($this->Model->hasMany['TestModel6']['fields']);
2004
$this->Model->hasMany['TestModel6']['fields'] = array('id', 'name');
2006
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
2007
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
2011
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2023
unset($this->Model->hasMany['TestModel6']['fields']);
2025
$this->Model->hasMany['TestModel6']['fields'] = array('test_model5_id', 'name');
2027
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
2028
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
2032
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2044
unset($this->Model->hasMany['TestModel6']['fields']);
2048
* test generateAssociationQuery with a hasMany and an aggregate function.
2052
function testGenerateAssociationQueryHasManyAndAggregateFunction() {
2053
$this->Model =& new TestModel5();
2054
$this->Model->schema();
2055
$this->_buildRelatedModels($this->Model);
2057
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
2058
$queryData = array('fields' => array('MIN(TestModel5.test_model4_id)'));
2062
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
2063
$this->Model->recursive = 0;
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);
2070
* testGenerateAssociationQueryHasAndBelongsToMany method
2075
function testGenerateAssociationQueryHasAndBelongsToMany() {
2076
$this->Model =& new TestModel4();
2077
$this->Model->schema();
2078
$this->_buildRelatedModels($this->Model);
2080
$binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
2081
$queryData = array();
2085
$params =& $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2101
* testGenerateAssociationQueryHasAndBelongsToManyWithConditions method
2106
function testGenerateAssociationQueryHasAndBelongsToManyWithConditions() {
2107
$this->Model =& new TestModel4();
2108
$this->Model->schema();
2109
$this->_buildRelatedModels($this->Model);
2111
$binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
2112
$queryData = array('conditions' => array('TestModel4.name !=' => 'mariano'));
2116
$params =& $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2130
* testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit method
2135
function testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit() {
2136
$this->Model =& new TestModel4();
2137
$this->Model->schema();
2138
$this->_buildRelatedModels($this->Model);
2140
$__backup = $this->Model->hasAndBelongsToMany['TestModel7'];
2142
$this->Model->hasAndBelongsToMany['TestModel7']['offset'] = 2;
2143
$this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5;
2145
$binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
2146
$queryData = array();
2150
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2163
$this->Model->hasAndBelongsToMany['TestModel7'] = $__backup;
2167
* testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit method
2172
function testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit() {
2173
$this->Model =& new TestModel4();
2174
$this->Model->schema();
2175
$this->_buildRelatedModels($this->Model);
2177
$__backup = $this->Model->hasAndBelongsToMany['TestModel7'];
2179
$this->Model->hasAndBelongsToMany['TestModel7']['page'] = 2;
2180
$this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5;
2182
$binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
2183
$queryData = array();
2187
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
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);
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);
2200
$this->Model->hasAndBelongsToMany['TestModel7'] = $__backup;
2204
* buildRelatedModels method
2206
* @param mixed $model
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'];
2218
$model->$className =& new $className();
2219
$model->$className->schema();
2225
* &_prepareAssociationQuery method
2227
* @param mixed $model
2228
* @param mixed $queryData
2229
* @param mixed $binding
2233
function &_prepareAssociationQuery(&$model, &$queryData, $binding) {
2234
$type = $binding['type'];
2235
$assoc = $binding['model'];
2236
$assocData = $model->{$type}[$assoc];
2237
$className = $assocData['className'];
2239
$linkModel =& $model->{$className};
2240
$external = isset($assocData['external']);
2241
$queryData = $this->testDb->__scrubQueryData($queryData);
2243
$result = array_merge(array('linkModel' => &$linkModel), compact('type', 'assoc', 'assocData', 'external'));
2248
* testSelectDistict method
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);
2260
* test that booleans and null make logical condition strings.
2264
function testBooleanNullConditionsParsing() {
2265
$result = $this->testDb->conditions(true);
2266
$this->assertEqual($result, ' WHERE 1 = 1', 'true conditions failed %s');
2268
$result = $this->testDb->conditions(false);
2269
$this->assertEqual($result, ' WHERE 0 = 1', 'false conditions failed %s');
2271
$result = $this->testDb->conditions(null);
2272
$this->assertEqual($result, ' WHERE 1 = 1', 'null conditions failed %s');
2274
$result = $this->testDb->conditions(array());
2275
$this->assertEqual($result, ' WHERE 1 = 1', 'array() conditions failed %s');
2277
$result = $this->testDb->conditions('');
2278
$this->assertEqual($result, ' WHERE 1 = 1', '"" conditions failed %s');
2280
$result = $this->testDb->conditions(' ', '" " conditions failed %s');
2281
$this->assertEqual($result, ' WHERE 1 = 1');
2284
* testStringConditionsParsing method
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);
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);
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);
2302
$result = $this->testDb->conditions("Post.title = '1.1'");
2303
$expected = " WHERE `Post`.`title` = '1.1'";
2304
$this->assertEqual($result, $expected);
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);
2310
$result = $this->testDb->conditions("SUM(Post.comments_count) > 500");
2311
$expected = " WHERE SUM(`Post`.`comments_count`) > 500";
2312
$this->assertEqual($result, $expected);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
2359
$result = $this->testDb->conditions("Comment.id = 'a'");
2360
$expected = " WHERE `Comment`.`id` = 'a'";
2361
$this->assertEqual($result, $expected);
2363
$result = $this->testDb->conditions("lower(Article.title) LIKE 'a%'");
2364
$expected = " WHERE lower(`Article`.`title`) LIKE 'a%'";
2365
$this->assertEqual($result, $expected);
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);
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);
2375
$result = $this->testDb->conditions('file = "index.html"');
2376
$expected = ' WHERE file = "index.html"';
2377
$this->assertEqual($result, $expected);
2379
$result = $this->testDb->conditions("file = 'index.html'");
2380
$expected = " WHERE file = 'index.html'";
2381
$this->assertEqual($result, $expected);
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);
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);
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);
2401
* testQuotesInStringConditions method
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);
2411
$result = $this->testDb->conditions('Member.email = "mariano@cricava.com"');
2412
$expected = ' WHERE `Member`.`email` = "mariano@cricava.com"';
2413
$this->assertEqual($result, $expected);
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);
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);
2426
* testParenthesisInStringConditions method
2431
function testParenthesisInStringConditions() {
2432
$result = $this->testDb->conditions('Member.name = \'(lu\'');
2433
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
2435
$result = $this->testDb->conditions('Member.name = \')lu\'');
2436
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
2438
$result = $this->testDb->conditions('Member.name = \'va(lu\'');
2439
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
2441
$result = $this->testDb->conditions('Member.name = \'va)lu\'');
2442
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
2444
$result = $this->testDb->conditions('Member.name = \'va(lu)\'');
2445
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
2447
$result = $this->testDb->conditions('Member.name = \'va(lu)e\'');
2448
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
2450
$result = $this->testDb->conditions('Member.name = \'(mariano)\'');
2451
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
2453
$result = $this->testDb->conditions('Member.name = \'(mariano)iglesias\'');
2454
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
2456
$result = $this->testDb->conditions('Member.name = \'(mariano) iglesias\'');
2457
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
2459
$result = $this->testDb->conditions('Member.name = \'(mariano word) iglesias\'');
2460
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
2462
$result = $this->testDb->conditions('Member.name = \'(mariano.iglesias)\'');
2463
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
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);
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);
2471
$result = $this->testDb->conditions('Member.name = \'(mariano.iglesias) CakePHP\'');
2472
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
2476
* testParenthesisInArrayConditions method
2481
function testParenthesisInArrayConditions() {
2482
$result = $this->testDb->conditions(array('Member.name' => '(lu'));
2483
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
2485
$result = $this->testDb->conditions(array('Member.name' => ')lu'));
2486
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
2488
$result = $this->testDb->conditions(array('Member.name' => 'va(lu'));
2489
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
2491
$result = $this->testDb->conditions(array('Member.name' => 'va)lu'));
2492
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
2494
$result = $this->testDb->conditions(array('Member.name' => 'va(lu)'));
2495
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
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);
2500
$result = $this->testDb->conditions(array('Member.name' => '(mariano)'));
2501
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
2503
$result = $this->testDb->conditions(array('Member.name' => '(mariano)iglesias'));
2504
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
2506
$result = $this->testDb->conditions(array('Member.name' => '(mariano) iglesias'));
2507
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
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);
2512
$result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias)'));
2513
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
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);
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);
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);
2526
* testArrayConditionsParsing method
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);
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);
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);
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);
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);
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);
2554
$result = $this->testDb->conditions(array('Post.title' => 1.1));
2555
$expected = " WHERE `Post`.`title` = 1.100000";
2556
$this->assertEqual($result, $expected);
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);
2562
$result = $this->testDb->conditions(array('SUM(Post.comments_count) >' => '500'));
2563
$expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
2564
$this->assertEqual($result, $expected);
2566
$result = $this->testDb->conditions(array('MAX(Post.rating) >' => '50'));
2567
$expected = " WHERE MAX(`Post`.`rating`) > '50'";
2568
$this->assertEqual($result, $expected);
2570
$result = $this->testDb->conditions(array('title LIKE' => '%hello'));
2571
$expected = " WHERE `title` LIKE '%hello'";
2572
$this->assertEqual($result, $expected);
2574
$result = $this->testDb->conditions(array('Post.name' => 'mad(g)ik'));
2575
$expected = " WHERE `Post`.`name` = 'mad(g)ik'";
2576
$this->assertEqual($result, $expected);
2578
$result = $this->testDb->conditions(array('score' => array(1, 2, 10)));
2579
$expected = " WHERE score IN (1, 2, 10)";
2580
$this->assertEqual($result, $expected);
2582
$result = $this->testDb->conditions(array('score' => array()));
2583
$expected = " WHERE `score` IS NULL";
2584
$this->assertEqual($result, $expected);
2586
$result = $this->testDb->conditions(array('score !=' => array()));
2587
$expected = " WHERE `score` IS NOT NULL";
2588
$this->assertEqual($result, $expected);
2590
$result = $this->testDb->conditions(array('score !=' => '20'));
2591
$expected = " WHERE `score` != '20'";
2592
$this->assertEqual($result, $expected);
2594
$result = $this->testDb->conditions(array('score >' => '20'));
2595
$expected = " WHERE `score` > '20'";
2596
$this->assertEqual($result, $expected);
2598
$result = $this->testDb->conditions(array('client_id >' => '20'), true, true, new TestModel());
2599
$expected = " WHERE `client_id` > 20";
2600
$this->assertEqual($result, $expected);
2602
$result = $this->testDb->conditions(array('OR' => array(
2603
array('User.user' => 'mariano'),
2604
array('User.user' => 'nate')
2607
$expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))";
2608
$this->assertEqual($result, $expected);
2610
$result = $this->testDb->conditions(array('or' => array(
2611
'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20'
2613
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))";
2614
$this->assertEqual($result, $expected);
2616
$result = $this->testDb->conditions(array('or' => array(
2617
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
2619
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))";
2620
$this->assertEqual($result, $expected);
2622
$result = $this->testDb->conditions(array('and' => array(
2623
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
2625
$expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))";
2626
$this->assertEqual($result, $expected);
2628
$result = $this->testDb->conditions(array(
2629
'published' => 1, 'or' => array('score >' => '2', array('score >' => '20'))
2631
$expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))";
2632
$this->assertEqual($result, $expected);
2634
$result = $this->testDb->conditions(array(array('Project.removed' => false)));
2635
$expected = " WHERE `Project`.`removed` = 0";
2636
$this->assertEqual($result, $expected);
2638
$result = $this->testDb->conditions(array(array('Project.removed' => true)));
2639
$expected = " WHERE `Project`.`removed` = 1";
2640
$this->assertEqual($result, $expected);
2642
$result = $this->testDb->conditions(array(array('Project.removed' => null)));
2643
$expected = " WHERE `Project`.`removed` IS NULL";
2644
$this->assertEqual($result, $expected);
2646
$result = $this->testDb->conditions(array(array('Project.removed !=' => null)));
2647
$expected = " WHERE `Project`.`removed` IS NOT NULL";
2648
$this->assertEqual($result, $expected);
2650
$result = $this->testDb->conditions(array('(Usergroup.permissions) & 4' => 4));
2651
$expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4";
2652
$this->assertEqual($result, $expected);
2654
$result = $this->testDb->conditions(array('((Usergroup.permissions) & 4)' => 4));
2655
$expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4";
2656
$this->assertEqual($result, $expected);
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);
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);
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')
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);
2672
$result = $this->testDb->conditions(array('id <>' => '8'));
2673
$this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
2675
$result = $this->testDb->conditions(array('TestModel.field =' => 'gribe$@()lu'));
2676
$expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
2677
$this->assertEqual($result, $expected);
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%"
2684
$conditions[1]['OR'] = array(
2685
"Listing.title LIKE" => "%term_2%",
2686
"Listing.description LIKE" => "%term_2%"
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);
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);
2698
$result = $this->testDb->conditions(array(
2699
'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah')
2701
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')";
2702
$this->assertEqual($result, $expected);
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);
2709
$conditions = array('title' => 'user(s)');
2710
$result = $this->testDb->conditions($conditions);
2711
$expected = " WHERE `title` = 'user(s)'";
2712
$this->assertEqual($result, $expected);
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);
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);
2724
$result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM)'));
2725
$expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
2726
$this->assertEqual($result, $expected);
2728
$result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM) '));
2729
$expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
2730
$this->assertEqual($result, $expected);
2732
$result = $this->testDb->conditions(array("Book.id" => 0));
2733
$expected = " WHERE `Book`.`id` = 0";
2734
$this->assertEqual($result, $expected);
2736
$result = $this->testDb->conditions(array("Book.id" => NULL));
2737
$expected = " WHERE `Book`.`id` IS NULL";
2738
$this->assertEqual($result, $expected);
2740
$result = $this->testDb->conditions(array('Listing.beds >=' => 0));
2741
$expected = " WHERE `Listing`.`beds` >= 0";
2742
$this->assertEqual($result, $expected);
2744
$result = $this->testDb->conditions(array(
2745
'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90)
2747
$expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90';
2748
$this->assertEqual($result, $expected);
2750
$result = $this->testDb->conditions(array('or' => array(
2751
'? BETWEEN Model.field1 AND Model.field2' => '2009-03-04'
2753
$expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2";
2754
$this->assertEqual($result, $expected);
2758
* testArrayConditionsParsingComplexKeys method
2763
function testArrayConditionsParsingComplexKeys() {
2764
$result = $this->testDb->conditions(array(
2765
'CAST(Book.created AS DATE)' => '2008-08-02'
2767
$expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'";
2768
$this->assertEqual($result, $expected);
2770
$result = $this->testDb->conditions(array(
2771
'CAST(Book.created AS DATE) <=' => '2008-08-02'
2773
$expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'";
2774
$this->assertEqual($result, $expected);
2776
$result = $this->testDb->conditions(array(
2777
'(Stats.clicks * 100) / Stats.views >' => 50
2779
$expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50";
2780
$this->assertEqual($result, $expected);
2784
* testMixedConditionsParsing method
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);
2796
$conditions = array(
2797
'Thread.project_id' => 5,
2798
'Thread.buyer_id' => 14,
2799
'1=1 GROUP BY Thread.project_id'
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);
2806
* testConditionsOptionalArguments method
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);
2815
$result = $this->testDb->conditions( array(), true, false);
2816
$this->assertPattern('/^\s*1\s*=\s*1\s*$/', $result);
2820
* testConditionsWithModel
2825
function testConditionsWithModel() {
2826
$this->Model =& new Article2();
2828
$result = $this->testDb->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model);
2829
$expected = " WHERE `Article2`.`viewed` >= 0";
2830
$this->assertEqual($result, $expected);
2832
$result = $this->testDb->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model);
2833
$expected = " WHERE `Article2`.`viewed` >= 0";
2834
$this->assertEqual($result, $expected);
2836
$result = $this->testDb->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model);
2837
$expected = " WHERE `Article2`.`viewed` >= 1";
2838
$this->assertEqual($result, $expected);
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);
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);
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);
2854
* testFieldParsing method
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);
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);
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);
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'));
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'));
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'));
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);
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");
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'
2890
$this->assertEqual($result, $expected);
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);
2896
$result = $this->testDb->fields($this->Model, 'Post');
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`'
2904
$this->assertEqual($result, $expected);
2906
$result = $this->testDb->fields($this->Model, 'Other');
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`'
2914
$this->assertEqual($result, $expected);
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);
2920
$result = $this->testDb->fields($this->Model, null, 'COUNT(*)');
2921
$expected = array('COUNT(*)');
2922
$this->assertEqual($result, $expected);
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);
2928
$result = $this->testDb->fields($this->Model, null, 'name, count(*)');
2929
$expected = array('`TestModel`.`name`', 'count(*)');
2930
$this->assertEqual($result, $expected);
2932
$result = $this->testDb->fields($this->Model, null, 'count(*), name');
2933
$expected = array('count(*)', '`TestModel`.`name`');
2934
$this->assertEqual($result, $expected);
2936
$result = $this->testDb->fields(
2937
$this->Model, null, 'field1, field2, field3, count(*), name'
2940
'`TestModel`.`field1`', '`TestModel`.`field2`',
2941
'`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'
2943
$this->assertEqual($result, $expected);
2945
$result = $this->testDb->fields($this->Model, null, array('dayofyear(now())'));
2946
$expected = array('dayofyear(now())');
2947
$this->assertEqual($result, $expected);
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);
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);
2957
$result = $this->testDb->fields($this->Model, null, array('field AS AnotherName'));
2958
$expected = array('`field` AS `AnotherName`');
2959
$this->assertEqual($result, $expected);
2961
$result = $this->testDb->fields($this->Model, null, array(
2962
'TestModel.field AS AnotherName'
2964
$expected = array('`TestModel`.`field` AS `AnotherName`');
2965
$this->assertEqual($result, $expected);
2967
$result = $this->testDb->fields($this->Model, 'Foo', array(
2968
'id', 'title', '(user_count + discussion_count + post_count) AS score'
2973
'(user_count + discussion_count + post_count) AS score'
2975
$this->assertEqual($result, $expected);
2979
* test that fields() will accept objects made from DboSource::expression
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));
2988
"CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"
2990
$this->assertEqual($result, $expected);
2994
* test that order() will accept objects made from DboSource::expression
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);
3006
* testMergeAssociations method
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'
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'
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'
3028
'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
3029
'updated' => '2007-03-17 01:18:31'
3032
$this->testDb->__mergeAssociation($data, $merge, 'Topic', 'hasOne');
3033
$this->assertEqual($data, $expected);
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'
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'
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'
3052
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3055
$this->testDb->__mergeAssociation($data, $merge, 'User2', 'belongsTo');
3056
$this->assertEqual($data, $expected);
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'
3063
$merge = array(array('Comment' => false));
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'
3068
'Comment' => array()
3070
$this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
3071
$this->assertEqual($data, $expected);
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'
3081
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3086
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
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'
3096
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3099
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3103
$this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
3104
$this->assertEqual($data, $expected);
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'
3114
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3117
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3122
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3125
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
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'
3135
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
3137
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3141
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
3143
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3148
$this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
3149
$this->assertEqual($data, $expected);
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'
3159
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3162
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3165
array('id' => 1, 'tag' => 'Tag 1'),
3166
array('id' => 2, 'tag' => 'Tag 2')
3171
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3174
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
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'
3185
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
3187
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3190
array('id' => 1, 'tag' => 'Tag 1'),
3191
array('id' => 2, 'tag' => 'Tag 2')
3195
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
3197
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3203
$this->testDb->__mergeAssociation($data, $merge, 'Comment', 'hasMany');
3204
$this->assertEqual($data, $expected);
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'
3214
'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3219
'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3224
'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
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'
3234
'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3237
'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3240
'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3244
$this->testDb->__mergeAssociation($data, $merge, 'Tag', 'hasAndBelongsToMany');
3245
$this->assertEqual($data, $expected);
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'
3255
'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3260
'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
3265
'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
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'
3273
'Tag' => array('id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
3275
$this->testDb->__mergeAssociation($data, $merge, 'Tag', 'hasOne');
3276
$this->assertEqual($data, $expected);
3280
* testRenderStatement method
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' => ''
3290
$this->assertPattern('/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result);
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);
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);
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);
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);
3306
* testStatements method
3311
function testStatements() {
3312
$Article =& ClassRegistry::init('Article');
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);
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);
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);
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);
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);
3339
$result = $this->testDb->hasAny($Article, '1=2');
3340
$this->assertFalse($result);
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);
3354
function testSchema() {
3355
$Schema =& new CakeSchema();
3356
$Schema->tables = array('table' => array(), 'anotherTable' => array());
3358
$this->expectError();
3359
$result = $this->testDb->dropSchema(null);
3360
$this->assertTrue($result === null);
3362
$result = $this->testDb->dropSchema($Schema, 'non_existing');
3363
$this->assertTrue(empty($result));
3365
$result = $this->testDb->dropSchema($Schema, 'table');
3366
$this->assertPattern('/^\s*DROP TABLE IF EXISTS\s+' . $this->testDb->fullTableName('table') . ';\s*$/s', $result);
3370
* testMagicMethodQuerying method
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
3381
$this->assertEqual($result, $expected);
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
3388
$this->assertEqual($result, $expected);
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
3396
$this->assertEqual($result, $expected);
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
3404
$this->assertEqual($result, $expected);
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
3411
$this->assertEqual($result, $expected);
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
3418
$this->assertEqual($result, $expected);
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
3425
$this->assertEqual($result, $expected);
3427
$result = $this->testDb->query('findByFieldName', array(), $this->Model);
3429
$this->assertEqual($result, $expected);
3431
$result = $this->testDb->query('directCall', array(), $this->Model);
3432
$this->assertFalse($result);
3434
$result = $this->testDb->query('directCall', true, $this->Model);
3435
$this->assertFalse($result);
3437
$result = $this->testDb->query('directCall', false, $this->Model);
3438
$this->assertFalse($result);
3442
* testOrderParsing method
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);
3452
$result = $this->testDb->order("title, id");
3453
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
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);
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);
3461
$result = $this->testDb->order(array("title", "id"));
3462
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
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);
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);
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);
3473
$result = $this->testDb->order(array("title"));
3474
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
3476
$result = $this->testDb->order(array(array("title")));
3477
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
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);
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);
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);
3489
$result = $this->testDb->order("(Post.views)");
3490
$this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\s+ASC\s*$/", $result);
3492
$result = $this->testDb->order("(Post.views)*Post.views");
3493
$this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\*`Post`\.`views`\s+ASC\s*$/", $result);
3495
$result = $this->testDb->order("(Post.views) * Post.views");
3496
$this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\) \* `Post`\.`views`\s+ASC\s*$/", $result);
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);
3501
$result = $this->testDb->order("Model.name+0 ASC");
3502
$this->assertPattern("/^\s*ORDER BY\s+`Model`\.`name`\+0\s+ASC\s*$/", $result);
3504
$result = $this->testDb->order("Anuncio.destaque & 2 DESC");
3505
$expected = ' ORDER BY `Anuncio`.`destaque` & 2 DESC';
3506
$this->assertEqual($result, $expected);
3508
$result = $this->testDb->order("3963.191 * id");
3509
$expected = ' ORDER BY 3963.191 * id ASC';
3510
$this->assertEqual($result, $expected);
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);
3518
* testComplexSortExpression method
3523
function testComplexSortExpression() {
3524
$result = $this->testDb->order(array('(Model.field > 100) DESC', 'Model.field ASC'));
3525
$this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field`\s+>\s+100\)\s+DESC,\s+`Model`\.`field`\s+ASC\s*$/", $result);
3529
* testCalculations method
3534
function testCalculations() {
3535
$result = $this->testDb->calculate($this->Model, 'count');
3536
$this->assertEqual($result, 'COUNT(*) AS `count`');
3538
$result = $this->testDb->calculate($this->Model, 'count', array('id'));
3539
$this->assertEqual($result, 'COUNT(`id`) AS `count`');
3541
$result = $this->testDb->calculate(
3544
array($this->testDb->expression('DISTINCT id'))
3546
$this->assertEqual($result, 'COUNT(DISTINCT id) AS `count`');
3548
$result = $this->testDb->calculate($this->Model, 'count', array('id', 'id_count'));
3549
$this->assertEqual($result, 'COUNT(`id`) AS `id_count`');
3551
$result = $this->testDb->calculate($this->Model, 'count', array('Model.id', 'id_count'));
3552
$this->assertEqual($result, 'COUNT(`Model`.`id`) AS `id_count`');
3554
$result = $this->testDb->calculate($this->Model, 'max', array('id'));
3555
$this->assertEqual($result, 'MAX(`id`) AS `id`');
3557
$result = $this->testDb->calculate($this->Model, 'max', array('Model.id', 'id'));
3558
$this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`');
3560
$result = $this->testDb->calculate($this->Model, 'max', array('`Model`.`id`', 'id'));
3561
$this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`');
3563
$result = $this->testDb->calculate($this->Model, 'min', array('`Model`.`id`', 'id'));
3564
$this->assertEqual($result, 'MIN(`Model`.`id`) AS `id`');
3566
$result = $this->testDb->calculate($this->Model, 'min', 'left');
3567
$this->assertEqual($result, 'MIN(`left`) AS `left`');
3576
function testLength() {
3577
$result = $this->testDb->length('varchar(255)');
3579
$this->assertIdentical($result, $expected);
3581
$result = $this->testDb->length('int(11)');
3583
$this->assertIdentical($result, $expected);
3585
$result = $this->testDb->length('float(5,3)');
3587
$this->assertIdentical($result, $expected);
3589
$result = $this->testDb->length('decimal(5,2)');
3591
$this->assertIdentical($result, $expected);
3593
$result = $this->testDb->length("enum('test','me','now')");
3595
$this->assertIdentical($result, $expected);
3597
$result = $this->testDb->length("set('a','b','cd')");
3599
$this->assertIdentical($result, $expected);
3601
$this->expectError();
3602
$result = $this->testDb->length(false);
3603
$this->assertTrue($result === null);
3605
$result = $this->testDb->length('datetime');
3607
$this->assertIdentical($result, $expected);
3609
$result = $this->testDb->length('text');
3611
$this->assertIdentical($result, $expected);
3615
* testBuildIndex method
3620
function testBuildIndex() {
3622
'PRIMARY' => array('column' => 'id')
3624
$result = $this->testDb->buildIndex($data);
3625
$expected = array('PRIMARY KEY (`id`)');
3626
$this->assertIdentical($result, $expected);
3629
'MyIndex' => array('column' => 'id', 'unique' => true)
3631
$result = $this->testDb->buildIndex($data);
3632
$expected = array('UNIQUE KEY `MyIndex` (`id`)');
3633
$this->assertEqual($result, $expected);
3636
'MyIndex' => array('column' => array('id', 'name'), 'unique' => true)
3638
$result = $this->testDb->buildIndex($data);
3639
$expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)');
3640
$this->assertEqual($result, $expected);
3644
* testBuildColumn method
3649
function testBuildColumn() {
3650
$this->expectError();
3652
'name' => 'testName',
3653
'type' => 'varchar(255)',
3658
$this->testDb->buildColumn($data);
3661
'name' => 'testName',
3668
$result = $this->testDb->buildColumn($data);
3669
$expected = '`testName` varchar(255) DEFAULT NULL';
3670
$this->assertEqual($result, $expected);
3673
'name' => 'int_field',
3674
'type' => 'integer',
3678
$restore = $this->testDb->columns;
3680
$this->testDb->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), );
3681
$result = $this->testDb->buildColumn($data);
3682
$expected = '`int_field` int(11) NOT NULL';
3683
$this->assertEqual($result, $expected);
3685
$this->testDb->fieldParameters['param'] = array(
3686
'value' => 'COLLATE',
3689
'column' => 'Collate',
3690
'position' => 'beforeDefault',
3691
'options' => array('GOOD', 'OK')
3694
'name' => 'int_field',
3695
'type' => 'integer',
3700
$result = $this->testDb->buildColumn($data);
3701
$expected = '`int_field` int(11) NOT NULL';
3702
$this->assertEqual($result, $expected);
3705
'name' => 'int_field',
3706
'type' => 'integer',
3711
$result = $this->testDb->buildColumn($data);
3712
$expected = '`int_field` int(11) COLLATE GOOD NOT NULL';
3713
$this->assertEqual($result, $expected);
3715
$this->testDb->columns = $restore;
3718
'name' => 'created',
3719
'type' => 'timestamp',
3720
'default' => 'current_timestamp',
3723
$result = $this->db->buildColumn($data);
3724
$expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL';
3725
$this->assertEqual($result, $expected);
3728
'name' => 'created',
3729
'type' => 'timestamp',
3730
'default' => 'CURRENT_TIMESTAMP',
3733
$result = $this->db->buildColumn($data);
3734
$expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP';
3735
$this->assertEqual($result, $expected);
3738
'name' => 'modified',
3739
'type' => 'timestamp',
3742
$result = $this->db->buildColumn($data);
3743
$expected = '`modified` timestamp NULL';
3744
$this->assertEqual($result, $expected);
3747
'name' => 'modified',
3748
'type' => 'timestamp',
3752
$result = $this->db->buildColumn($data);
3753
$expected = '`modified` timestamp NULL';
3754
$this->assertEqual($result, $expected);
3762
function testHasAny() {
3763
$this->testDb->hasAny($this->Model, array());
3764
$expected = 'SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1';
3765
$this->assertEqual(end($this->testDb->simulated), $expected);
3767
$this->testDb->hasAny($this->Model, array('TestModel.name' => 'harry'));
3768
$expected = "SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = 'harry'";
3769
$this->assertEqual(end($this->testDb->simulated), $expected);
3773
* testIntrospectType method
3778
function testIntrospectType() {
3779
$this->assertEqual($this->testDb->introspectType(0), 'integer');
3780
$this->assertEqual($this->testDb->introspectType(2), 'integer');
3781
$this->assertEqual($this->testDb->introspectType('2'), 'string');
3782
$this->assertEqual($this->testDb->introspectType('2.2'), 'string');
3783
$this->assertEqual($this->testDb->introspectType(2.2), 'float');
3784
$this->assertEqual($this->testDb->introspectType('stringme'), 'string');
3785
$this->assertEqual($this->testDb->introspectType('0stringme'), 'string');
3788
$this->assertEqual($this->testDb->introspectType($data), 'float');
3790
$data = array('2.2');
3791
$this->assertEqual($this->testDb->introspectType($data), 'float');
3794
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3797
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3799
$data = array('string');
3800
$this->assertEqual($this->testDb->introspectType($data), 'string');
3802
$data = array(2.2, '2.2');
3803
$this->assertEqual($this->testDb->introspectType($data), 'float');
3805
$data = array(2, '2');
3806
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3808
$data = array('string one', 'string two');
3809
$this->assertEqual($this->testDb->introspectType($data), 'string');
3811
$data = array('2.2', 3);
3812
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3814
$data = array('2.2', '0stringme');
3815
$this->assertEqual($this->testDb->introspectType($data), 'string');
3817
$data = array(2.2, 3);
3818
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3820
$data = array(2.2, '0stringme');
3821
$this->assertEqual($this->testDb->introspectType($data), 'string');
3823
$data = array(2, 'stringme');
3824
$this->assertEqual($this->testDb->introspectType($data), 'string');
3826
$data = array(2, '2.2', 'stringgme');
3827
$this->assertEqual($this->testDb->introspectType($data), 'string');
3829
$data = array(2, '2.2');
3830
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3832
$data = array(2, 2.2);
3833
$this->assertEqual($this->testDb->introspectType($data), 'integer');
3837
$result = $this->testDb->value(null, 'boolean');
3838
$this->assertEqual($result, 'NULL');
3841
$result = $this->testDb->value('', 'boolean');
3842
$this->assertEqual($result, "NULL");
3846
$result = $this->testDb->value('true', 'boolean');
3847
$this->assertEqual($result, 1);
3849
$result = $this->testDb->value('false', 'boolean');
3850
$this->assertEqual($result, 1);
3852
$result = $this->testDb->value(true, 'boolean');
3853
$this->assertEqual($result, 1);
3855
$result = $this->testDb->value(false, 'boolean');
3856
$this->assertEqual($result, 0);
3858
$result = $this->testDb->value(1, 'boolean');
3859
$this->assertEqual($result, 1);
3861
$result = $this->testDb->value(0, 'boolean');
3862
$this->assertEqual($result, 0);
3864
$result = $this->testDb->value('abc', 'boolean');
3865
$this->assertEqual($result, 1);
3867
$result = $this->testDb->value(1.234, 'boolean');
3868
$this->assertEqual($result, 1);
3870
$result = $this->testDb->value('1.234e05', 'boolean');
3871
$this->assertEqual($result, 1);
3874
$result = $this->testDb->value(123, 'integer');
3875
$this->assertEqual($result, 123);
3877
$result = $this->testDb->value('123', 'integer');
3878
$this->assertEqual($result, '123');
3880
$result = $this->testDb->value('0123', 'integer');
3881
$this->assertEqual($result, "'0123'");
3883
$result = $this->testDb->value('0x123ABC', 'integer');
3884
$this->assertEqual($result, "'0x123ABC'");
3886
$result = $this->testDb->value('0x123', 'integer');
3887
$this->assertEqual($result, "'0x123'");
3889
$result = $this->testDb->value(1.234, 'float');
3890
$this->assertEqual($result, 1.234);
3892
$result = $this->testDb->value('1.234', 'float');
3893
$this->assertEqual($result, '1.234');
3895
$result = $this->testDb->value(' 1.234 ', 'float');
3896
$this->assertEqual($result, "' 1.234 '");
3898
$result = $this->testDb->value('1.234e05', 'float');
3899
$this->assertEqual($result, "'1.234e05'");
3901
$result = $this->testDb->value('1.234e+5', 'float');
3902
$this->assertEqual($result, "'1.234e+5'");
3904
$result = $this->testDb->value('1,234', 'float');
3905
$this->assertEqual($result, "'1,234'");
3907
$result = $this->testDb->value('FFF', 'integer');
3908
$this->assertEqual($result, "'FFF'");
3910
$result = $this->testDb->value('abc', 'integer');
3911
$this->assertEqual($result, "'abc'");
3914
$result = $this->testDb->value('123', 'string');
3915
$this->assertEqual($result, "'123'");
3917
$result = $this->testDb->value(123, 'string');
3918
$this->assertEqual($result, "'123'");
3920
$result = $this->testDb->value(1.234, 'string');
3921
$this->assertEqual($result, "'1.234'");
3923
$result = $this->testDb->value('abc', 'string');
3924
$this->assertEqual($result, "'abc'");
3926
$result = $this->testDb->value(' abc ', 'string');
3927
$this->assertEqual($result, "' abc '");
3929
$result = $this->testDb->value('a bc', 'string');
3930
$this->assertEqual($result, "'a bc'");
3939
function testValue() {
3940
$result = $this->testDb->value('{$__cakeForeignKey__$}');
3941
$this->assertEqual($result, '{$__cakeForeignKey__$}');
3943
$result = $this->testDb->value(array('first', 2, 'third'));
3944
$expected = array('\'first\'', 2, '\'third\'');
3945
$this->assertEqual($result, $expected);
3949
* testReconnect method
3954
function testReconnect() {
3955
$this->testDb->reconnect(array('prefix' => 'foo'));
3956
$this->assertTrue($this->testDb->connected);
3957
$this->assertEqual($this->testDb->config['prefix'], 'foo');
3961
* testRealQueries method
3966
function testRealQueries() {
3967
$this->loadFixtures('Apple', 'Article', 'User', 'Comment', 'Tag');
3969
$Apple =& ClassRegistry::init('Apple');
3970
$Article =& ClassRegistry::init('Article');
3972
$result = $this->db->rawQuery('SELECT color, name FROM ' . $this->db->fullTableName('apples'));
3973
$this->assertTrue(!empty($result));
3975
$result = $this->db->fetchRow($result);
3976
$expected = array($this->db->fullTableName('apples', false) => array(
3978
'name' => 'Red Apple 1'
3980
$this->assertEqual($result, $expected);
3982
$result = $this->db->fetchAll('SELECT name FROM ' . $this->testDb->fullTableName('apples') . ' ORDER BY id');
3984
array($this->db->fullTableName('apples', false) => array('name' => 'Red Apple 1')),
3985
array($this->db->fullTableName('apples', false) => array('name' => 'Bright Red Apple')),
3986
array($this->db->fullTableName('apples', false) => array('name' => 'green blue')),
3987
array($this->db->fullTableName('apples', false) => array('name' => 'Test Name')),
3988
array($this->db->fullTableName('apples', false) => array('name' => 'Blue Green')),
3989
array($this->db->fullTableName('apples', false) => array('name' => 'My new apple')),
3990
array($this->db->fullTableName('apples', false) => array('name' => 'Some odd color'))
3992
$this->assertEqual($result, $expected);
3994
$result = $this->db->field($this->testDb->fullTableName('apples', false), 'SELECT color, name FROM ' . $this->testDb->fullTableName('apples') . ' ORDER BY id');
3997
'name' => 'Red Apple 1'
3999
$this->assertEqual($result, $expected);
4001
$Apple->unbindModel(array(), false);
4002
$result = $this->db->read($Apple, array(
4003
'fields' => array($Apple->escapeField('name')),
4004
'conditions' => null,
4008
array('Apple' => array('name' => 'Red Apple 1')),
4009
array('Apple' => array('name' => 'Bright Red Apple')),
4010
array('Apple' => array('name' => 'green blue')),
4011
array('Apple' => array('name' => 'Test Name')),
4012
array('Apple' => array('name' => 'Blue Green')),
4013
array('Apple' => array('name' => 'My new apple')),
4014
array('Apple' => array('name' => 'Some odd color'))
4016
$this->assertEqual($result, $expected);
4018
$result = $this->db->read($Article, array(
4019
'fields' => array('id', 'user_id', 'title'),
4020
'conditions' => null,
4024
$this->assertTrue(Set::matches('/Article[id=1]', $result));
4025
$this->assertTrue(Set::matches('/Comment[id=1]', $result));
4026
$this->assertTrue(Set::matches('/Comment[id=2]', $result));
4027
$this->assertFalse(Set::matches('/Comment[id=10]', $result));
4036
function testName() {
4037
$result = $this->testDb->name('name');
4038
$expected = '`name`';
4039
$this->assertEqual($result, $expected);
4041
$result = $this->testDb->name(array('name', 'Model.*'));
4042
$expected = array('`name`', '`Model`.*');
4043
$this->assertEqual($result, $expected);
4045
$result = $this->testDb->name('MTD()');
4046
$expected = 'MTD()';
4047
$this->assertEqual($result, $expected);
4049
$result = $this->testDb->name('(sm)');
4051
$this->assertEqual($result, $expected);
4053
$result = $this->testDb->name('name AS x');
4054
$expected = '`name` AS `x`';
4055
$this->assertEqual($result, $expected);
4057
$result = $this->testDb->name('Model.name AS x');
4058
$expected = '`Model`.`name` AS `x`';
4059
$this->assertEqual($result, $expected);
4061
$result = $this->testDb->name('Function(Something.foo)');
4062
$expected = 'Function(`Something`.`foo`)';
4063
$this->assertEqual($result, $expected);
4065
$result = $this->testDb->name('Function(SubFunction(Something.foo))');
4066
$expected = 'Function(SubFunction(`Something`.`foo`))';
4067
$this->assertEqual($result, $expected);
4069
$result = $this->testDb->name('Function(Something.foo) AS x');
4070
$expected = 'Function(`Something`.`foo`) AS `x`';
4071
$this->assertEqual($result, $expected);
4073
$result = $this->testDb->name('name-with-minus');
4074
$expected = '`name-with-minus`';
4075
$this->assertEqual($result, $expected);
4077
$result = $this->testDb->name(array('my-name', 'Foo-Model.*'));
4078
$expected = array('`my-name`', '`Foo-Model`.*');
4079
$this->assertEqual($result, $expected);
4083
* test that cacheMethod works as exepected
4087
function testCacheMethod() {
4088
$this->testDb->cacheMethods = true;
4089
$result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
4090
$this->assertEqual($result, 'stuff');
4092
$result = $this->testDb->cacheMethod('name', 'some-key');
4093
$this->assertEqual($result, 'stuff');
4095
$result = $this->testDb->cacheMethod('conditions', 'some-key');
4096
$this->assertNull($result);
4098
$result = $this->testDb->cacheMethod('name', 'other-key');
4099
$this->assertNull($result);
4101
$this->testDb->cacheMethods = false;
4102
$result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
4103
$this->assertEqual($result, 'stuff');
4105
$result = $this->testDb->cacheMethod('name', 'some-key');
4106
$this->assertNull($result);
4115
function testLog() {
4116
$this->testDb->logQuery('Query 1');
4117
$this->testDb->logQuery('Query 2');
4119
$log = $this->testDb->getLog(false, false);
4120
$result = Set::extract($log['log'], '/query');
4121
$expected = array('Query 1', 'Query 2');
4122
$this->assertEqual($result, $expected);
4124
$oldError = $this->testDb->error;
4125
$this->testDb->error = true;
4126
$result = $this->testDb->logQuery('Error 1');
4127
$this->assertFalse($result);
4128
$this->testDb->error = $oldError;
4130
$log = $this->testDb->getLog(false, false);
4131
$result = Set::combine($log['log'], '/query', '/error');
4132
$expected = array('Query 1' => false, 'Query 2' => false, 'Error 1' => true);
4133
$this->assertEqual($result, $expected);
4135
Configure::write('debug', 2);
4137
$this->testDb->showLog();
4138
$contents = ob_get_clean();
4140
$this->assertPattern('/Query 1/s', $contents);
4141
$this->assertPattern('/Query 2/s', $contents);
4142
$this->assertPattern('/Error 1/s', $contents);
4145
$this->testDb->showLog(true);
4146
$contents = ob_get_clean();
4148
$this->assertPattern('/Query 1/s', $contents);
4149
$this->assertPattern('/Query 2/s', $contents);
4150
$this->assertPattern('/Error 1/s', $contents);
4152
$oldError = $this->testDb->error;
4153
$oldDebug = Configure::read('debug');
4154
Configure::write('debug', 2);
4156
$this->testDb->error = true;
4157
$this->expectError();
4159
$this->testDb->showQuery('Error 2');
4160
$contents = ob_get_clean();
4162
$this->assertPattern('/Error 2/s', $contents);
4164
$this->testDb->error = $oldError;
4165
Configure::write('debug', $oldDebug);
4169
* test getting the query log as an array.
4173
function testGetLog() {
4174
$this->testDb->logQuery('Query 1');
4175
$this->testDb->logQuery('Query 2');
4177
$oldError = $this->testDb->error;
4178
$this->testDb->error = true;
4179
$result = $this->testDb->logQuery('Error 1');
4180
$this->assertFalse($result);
4181
$this->testDb->error = $oldError;
4183
$log = $this->testDb->getLog();
4184
$expected = array('query' => 'Query 1', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
4185
$this->assertEqual($log['log'][0], $expected);
4186
$expected = array('query' => 'Query 2', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
4187
$this->assertEqual($log['log'][1], $expected);
4188
$expected = array('query' => 'Error 1', 'error' => true, 'affected' => '', 'numRows' => '', 'took' => '');
4189
$this->assertEqual($log['log'][2], $expected);
4193
* test that execute runs queries.
4197
function testExecute() {
4198
$query = 'SELECT * FROM ' . $this->testDb->fullTableName('articles') . ' WHERE 1 = 1';
4200
$this->db->_result = null;
4201
$this->db->took = null;
4202
$this->db->affected = null;
4203
$result = $this->db->execute($query, array('stats' => false));
4204
$this->assertNotNull($result, 'No query performed! %s');
4205
$this->assertNull($this->db->took, 'Stats were set %s');
4206
$this->assertNull($this->db->affected, 'Stats were set %s');
4208
$result = $this->db->execute($query);
4209
$this->assertNotNull($result, 'No query performed! %s');
4210
$this->assertNotNull($this->db->took, 'Stats were not set %s');
4211
$this->assertNotNull($this->db->affected, 'Stats were not set %s');
4215
* test that query() returns boolean values from operations like CREATE TABLE
4219
function testFetchAllBooleanReturns() {
4220
$name = $this->db->fullTableName('test_query');
4221
$query = "CREATE TABLE {$name} (name varchar(10));";
4222
$result = $this->db->query($query);
4223
$this->assertTrue($result, 'Query did not return a boolean. %s');
4225
$query = "DROP TABLE {$name};";
4226
$result = $this->db->fetchAll($query);
4227
$this->assertTrue($result, 'Query did not return a boolean. %s');
4231
* test ShowQuery generation of regular and error messages
4235
function testShowQuery() {
4236
$this->testDb->error = false;
4238
$this->testDb->showQuery('Some Query');
4239
$contents = ob_get_clean();
4240
$this->assertPattern('/Some Query/s', $contents);
4241
$this->assertPattern('/Aff:/s', $contents);
4242
$this->assertPattern('/Num:/s', $contents);
4243
$this->assertPattern('/Took:/s', $contents);
4245
$this->expectError();
4246
$this->testDb->error = true;
4248
$this->testDb->showQuery('Another Query');
4249
$contents = ob_get_clean();
4250
$this->assertPattern('/Another Query/s', $contents);
4251
$this->assertNoPattern('/Aff:/s', $contents);
4252
$this->assertNoPattern('/Num:/s', $contents);
4253
$this->assertNoPattern('/Took:/s', $contents);
4257
* test fields generating usable virtual fields to use in query
4261
function testVirtualFields() {
4262
$this->loadFixtures('Article');
4264
$Article =& ClassRegistry::init('Article');
4265
$Article->virtualFields = array(
4266
'this_moment' => 'NOW()',
4268
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
4269
' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id'
4271
$result = $this->db->fields($Article);
4274
'`Article`.`user_id`',
4275
'`Article`.`title`',
4277
'`Article`.`published`',
4278
'`Article`.`created`',
4279
'`Article`.`updated`',
4280
'(NOW()) AS `Article__this_moment`',
4281
'(1 + 1) AS `Article__two`',
4282
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
4284
$this->assertEqual($expected, $result);
4286
$result = $this->db->fields($Article, null, array('this_moment', 'title'));
4288
'`Article`.`title`',
4289
'(NOW()) AS `Article__this_moment`',
4291
$this->assertEqual($expected, $result);
4293
$result = $this->db->fields($Article, null, array('Article.title', 'Article.this_moment'));
4295
'`Article`.`title`',
4296
'(NOW()) AS `Article__this_moment`',
4298
$this->assertEqual($expected, $result);
4300
$result = $this->db->fields($Article, null, array('Article.this_moment', 'Article.title'));
4302
'`Article`.`title`',
4303
'(NOW()) AS `Article__this_moment`',
4305
$this->assertEqual($expected, $result);
4307
$result = $this->db->fields($Article, null, array('Article.*'));
4310
'(NOW()) AS `Article__this_moment`',
4311
'(1 + 1) AS `Article__two`',
4312
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
4314
$this->assertEqual($expected, $result);
4316
$result = $this->db->fields($Article, null, array('*'));
4319
'(NOW()) AS `Article__this_moment`',
4320
'(1 + 1) AS `Article__two`',
4321
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
4323
$this->assertEqual($expected, $result);
4327
* test conditions to generate query conditions for virtual fields
4331
function testVirtualFieldsInConditions() {
4332
$Article =& ClassRegistry::init('Article');
4333
$Article->virtualFields = array(
4334
'this_moment' => 'NOW()',
4336
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
4337
' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id'
4339
$conditions = array('two' => 2);
4340
$result = $this->db->conditions($conditions, true, false, $Article);
4341
$expected = '(1 + 1) = 2';
4342
$this->assertEqual($expected, $result);
4344
$conditions = array('this_moment BETWEEN ? AND ?' => array(1,2));
4345
$expected = 'NOW() BETWEEN 1 AND 2';
4346
$result = $this->db->conditions($conditions, true, false, $Article);
4347
$this->assertEqual($expected, $result);
4349
$conditions = array('comment_count >' => 5);
4350
$expected = '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) > 5';
4351
$result = $this->db->conditions($conditions, true, false, $Article);
4352
$this->assertEqual($expected, $result);
4354
$conditions = array('NOT' => array('two' => 2));
4355
$result = $this->db->conditions($conditions, true, false, $Article);
4356
$expected = 'NOT ((1 + 1) = 2)';
4357
$this->assertEqual($expected, $result);
4361
* test that virtualFields with complex functions and aliases work.
4365
function testConditionsWithComplexVirtualFields() {
4366
$Article =& ClassRegistry::init('Article');
4367
$Article->virtualFields = array(
4368
'distance' => 'ACOS(SIN(20 * PI() / 180)
4369
* SIN(Article.latitude * PI() / 180)
4370
+ COS(20 * PI() / 180)
4371
* COS(Article.latitude * PI() / 180)
4372
* COS((50 - Article.longitude) * PI() / 180)
4373
) * 180 / PI() * 60 * 1.1515 * 1.609344'
4375
$conditions = array('distance >=' => 20);
4376
$result = $this->db->conditions($conditions, true, true, $Article);
4378
$this->assertPattern('/\) >= 20/', $result);
4379
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result);
4380
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result);
4384
* test order to generate query order clause for virtual fields
4388
function testVirtualFieldsInOrder() {
4389
$Article =& ClassRegistry::init('Article');
4390
$Article->virtualFields = array(
4391
'this_moment' => 'NOW()',
4394
$order = array('two', 'this_moment');
4395
$result = $this->db->order($order, 'ASC', $Article);
4396
$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
4397
$this->assertEqual($expected, $result);
4399
$order = array('Article.two', 'Article.this_moment');
4400
$result = $this->db->order($order, 'ASC', $Article);
4401
$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
4402
$this->assertEqual($expected, $result);
4406
* test calculate to generate claculate statements on virtual fields
4410
function testVirtualFieldsInCalculate() {
4411
$Article =& ClassRegistry::init('Article');
4412
$Article->virtualFields = array(
4413
'this_moment' => 'NOW()',
4415
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
4416
' WHERE Article.id = ' . $this->db->fullTableName('comments'). '.article_id'
4419
$result = $this->db->calculate($Article, 'count', array('this_moment'));
4420
$expected = 'COUNT(NOW()) AS `count`';
4421
$this->assertEqual($expected, $result);
4423
$result = $this->db->calculate($Article, 'max', array('comment_count'));
4424
$expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`';
4425
$this->assertEqual($expected, $result);
4429
* test a full example of using virtual fields
4433
function testVirtualFieldsFetch() {
4434
$this->loadFixtures('Article', 'Comment');
4436
$Article =& ClassRegistry::init('Article');
4437
$Article->virtualFields = array(
4438
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
4439
' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id'
4442
$conditions = array('comment_count >' => 2);
4443
$query = 'SELECT ' . join(',',$this->db->fields($Article, null, array('id', 'comment_count'))) .
4444
' FROM ' . $this->db->fullTableName($Article) . ' Article ' . $this->db->conditions($conditions, true, true, $Article);
4445
$result = $this->db->fetchAll($query);
4446
$expected = array(array(
4447
'Article' => array('id' => 1, 'comment_count' => 4)
4449
$this->assertEqual($expected, $result);
4453
* test reading complex virtualFields with subqueries.
4457
function testVirtualFieldsComplexRead() {
4458
$this->loadFixtures('DataTest', 'Article', 'Comment');
4460
$Article =& ClassRegistry::init('Article');
4461
$commentTable = $this->db->fullTableName('comments');
4462
$Article =& ClassRegistry::init('Article');
4463
$Article->virtualFields = array(
4464
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
4465
' AS Comment WHERE Article.id = Comment.article_id'
4467
$result = $Article->find('all');
4468
$this->assertTrue(count($result) > 0);
4469
$this->assertTrue($result[0]['Article']['comment_count'] > 0);
4471
$DataTest =& ClassRegistry::init('DataTest');
4472
$DataTest->virtualFields = array(
4473
'complicated' => 'ACOS(SIN(20 * PI() / 180)
4474
* SIN(DataTest.float * PI() / 180)
4475
+ COS(20 * PI() / 180)
4476
* COS(DataTest.count * PI() / 180)
4477
* COS((50 - DataTest.float) * PI() / 180)
4478
) * 180 / PI() * 60 * 1.1515 * 1.609344'
4480
$result = $DataTest->find('all');
4481
$this->assertTrue(count($result) > 0);
4482
$this->assertTrue($result[0]['DataTest']['complicated'] > 0);
4486
* test that virtualFields with complex functions and aliases work.
4490
function testFieldsWithComplexVirtualFields() {
4491
$Article =& new Article();
4492
$Article->virtualFields = array(
4493
'distance' => 'ACOS(SIN(20 * PI() / 180)
4494
* SIN(Article.latitude * PI() / 180)
4495
+ COS(20 * PI() / 180)
4496
* COS(Article.latitude * PI() / 180)
4497
* COS((50 - Article.longitude) * PI() / 180)
4498
) * 180 / PI() * 60 * 1.1515 * 1.609344'
4501
$fields = array('id', 'distance');
4502
$result = $this->db->fields($Article, null, $fields);
4503
$qs = $this->db->startQuote;
4504
$qe = $this->db->endQuote;
4506
$this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}");
4507
$this->assertPattern('/Article__distance/', $result[1]);
4508
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]);
4509
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]);
4513
* test reading virtual fields containing newlines when recursive > 0
4517
function testReadVirtualFieldsWithNewLines() {
4518
$Article =& new Article();
4519
$Article->recursive = 1;
4520
$Article->virtualFields = array(
4525
$result = $this->db->fields($Article, null, array());
4526
$result = $this->db->fields($Article, $Article->alias, $result);
4527
$this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]);
4531
* test group to generate GROUP BY statements on virtual fields
4535
function testVirtualFieldsInGroup() {
4536
$Article =& ClassRegistry::init('Article');
4537
$Article->virtualFields = array(
4538
'this_year' => 'YEAR(Article.created)'
4541
$result = $this->db->group('this_year',$Article);
4542
$expected = " GROUP BY (YEAR(`Article`.`created`))";
4543
$this->assertEqual($expected, $result);
4547
* test the permutations of fullTableName()
4551
function testFullTablePermutations() {
4552
$Article =& ClassRegistry::init('Article');
4553
$result = $this->testDb->fullTableName($Article, false);
4554
$this->assertEqual($result, 'articles');
4556
$Article->tablePrefix = 'tbl_';
4557
$result = $this->testDb->fullTableName($Article, false);
4558
$this->assertEqual($result, 'tbl_articles');
4560
$Article->useTable = $Article->table = 'with spaces';
4561
$Article->tablePrefix = '';
4562
$result = $this->testDb->fullTableName($Article);
4563
$this->assertEqual($result, '`with spaces`');
4567
* test that read() only calls queryAssociation on db objects when the method is defined.
4571
function testReadOnlyCallingQueryAssociationWhenDefined() {
4572
ConnectionManager::create('test_no_queryAssociation', array(
4573
'datasource' => 'data'
4575
$Article =& ClassRegistry::init('Article');
4576
$Article->Comment->useDbConfig = 'test_no_queryAssociation';
4577
$result = $Article->find('all');
4578
$this->assertTrue(is_array($result));
4582
* test that fields() is using methodCache()
4586
function testFieldsUsingMethodCache() {
4587
$this->testDb->cacheMethods = false;
4588
$this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty');
4590
$Article =& ClassRegistry::init('Article');
4591
$this->testDb->fields($Article, null, array('title', 'body', 'published'));
4592
$this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty');