2
/* SVN FILE: $Id: model.test.php 8225 2009-07-08 03:25:30Z mark_story $ */
5
* ModelIntegrationTest file
9
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
10
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The Open Group Test Suite License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
18
* @subpackage cake.tests.cases.libs.model
19
* @since CakePHP(tm) v 1.2.0.4206
20
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
22
require_once dirname(__FILE__) . DS . 'model.test.php';
23
App::import('Core', 'DboSource');
27
* A Dbo Source driver to mock a connection and a identity name() method
29
class DboMock extends DboSource {
32
* Returns the $field without modifications
34
function name($field) {
38
* Returns true to fake a database connection
46
* ModelIntegrationTest
49
* @subpackage cake.tests.cases.libs.model.operations
51
class ModelIntegrationTest extends BaseModelTest {
54
* testPkInHAbtmLinkModelArticleB
59
function testPkInHabtmLinkModelArticleB() {
60
$this->loadFixtures('Article', 'Tag');
61
$TestModel2 =& new ArticleB();
62
$this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id');
66
* Tests that $cacheSources can only be disabled in the db using model settings, not enabled
71
function testCacheSourcesDisabling() {
72
$this->db->cacheSources = true;
73
$TestModel = new JoinA();
74
$TestModel->cacheSources = false;
75
$TestModel->setSource('join_as');
76
$this->assertFalse($this->db->cacheSources);
78
$this->db->cacheSources = false;
79
$TestModel = new JoinA();
80
$TestModel->cacheSources = true;
81
$TestModel->setSource('join_as');
82
$this->assertFalse($this->db->cacheSources);
86
* testPkInHabtmLinkModel method
91
function testPkInHabtmLinkModel() {
92
//Test Nonconformant Models
93
$this->loadFixtures('Content', 'ContentAccount', 'Account');
94
$TestModel =& new Content();
95
$this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId');
97
//test conformant models with no PK in the join table
98
$this->loadFixtures('Article', 'Tag');
99
$TestModel2 =& new Article();
100
$this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id');
102
//test conformant models with PK in join table
103
$this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio');
104
$TestModel3 =& new Portfolio();
105
$this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id');
107
//test conformant models with PK in join table - join table contains extra field
108
$this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
109
$TestModel4 =& new JoinA();
110
$this->assertEqual($TestModel4->JoinAsJoinB->primaryKey, 'id');
115
* testDynamicBehaviorAttachment method
120
function testDynamicBehaviorAttachment() {
121
$this->loadFixtures('Apple');
122
$TestModel =& new Apple();
123
$this->assertEqual($TestModel->Behaviors->attached(), array());
125
$TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field'));
126
$this->assertTrue(is_object($TestModel->Behaviors->Tree));
127
$this->assertEqual($TestModel->Behaviors->attached(), array('Tree'));
130
'parent' => 'parent_id',
131
'left' => 'left_field',
132
'right' => 'right_field',
135
'__parentChange' => false,
139
$this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected);
141
$expected['enabled'] = false;
142
$TestModel->Behaviors->attach('Tree', array('enabled' => false));
143
$this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected);
144
$this->assertEqual($TestModel->Behaviors->attached(), array('Tree'));
146
$TestModel->Behaviors->detach('Tree');
147
$this->assertEqual($TestModel->Behaviors->attached(), array());
148
$this->assertFalse(isset($TestModel->Behaviors->Tree));
152
* Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
153
* NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
154
* or one connection will step on the other.
156
function testCrossDatabaseJoins() {
157
$config = new DATABASE_CONFIG();
159
$skip = $this->skipIf(
160
!isset($config->test) || !isset($config->test2),
161
'%s Primary and secondary test databases not configured, skipping cross-database '
163
.' To run these tests, you must define $test and $test2 in your database configuration.'
170
$this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment');
171
$TestModel =& new Article();
178
'title' => 'First Article',
179
'body' => 'First Article Body',
181
'created' => '2007-03-18 10:39:23',
182
'updated' => '2007-03-18 10:41:31'
187
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
188
'created' => '2007-03-17 01:16:23',
189
'updated' => '2007-03-17 01:18:31'
196
'comment' => 'First Comment for First Article',
198
'created' => '2007-03-18 10:45:23',
199
'updated' => '2007-03-18 10:47:31'
205
'comment' => 'Second Comment for First Article',
207
'created' => '2007-03-18 10:47:23',
208
'updated' => '2007-03-18 10:49:31'
214
'comment' => 'Third Comment for First Article',
216
'created' => '2007-03-18 10:49:23',
217
'updated' => '2007-03-18 10:51:31'
223
'comment' => 'Fourth Comment for First Article',
225
'created' => '2007-03-18 10:51:23',
226
'updated' => '2007-03-18 10:53:31'
232
'created' => '2007-03-18 12:22:23',
233
'updated' => '2007-03-18 12:24:31'
238
'created' => '2007-03-18 12:24:23',
239
'updated' => '2007-03-18 12:26:31'
245
'title' => 'Second Article',
246
'body' => 'Second Article Body',
248
'created' => '2007-03-18 10:41:23',
249
'updated' => '2007-03-18 10:43:31'
254
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
255
'created' => '2007-03-17 01:20:23',
256
'updated' => '2007-03-17 01:22:31'
263
'comment' => 'First Comment for Second Article',
265
'created' => '2007-03-18 10:53:23',
266
'updated' => '2007-03-18 10:55:31'
272
'comment' => 'Second Comment for Second Article',
274
'created' => '2007-03-18 10:55:23',
275
'updated' => '2007-03-18 10:57:31'
281
'created' => '2007-03-18 12:22:23',
282
'updated' => '2007-03-18 12:24:31'
287
'created' => '2007-03-18 12:26:23',
288
'updated' => '2007-03-18 12:28:31'
294
'title' => 'Third Article',
295
'body' => 'Third Article Body',
297
'created' => '2007-03-18 10:43:23',
298
'updated' => '2007-03-18 10:45:31'
303
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
304
'created' => '2007-03-17 01:16:23',
305
'updated' => '2007-03-17 01:18:31'
307
'Comment' => array(),
310
$this->assertEqual($TestModel->find('all'), $expected);
312
$db2 =& ConnectionManager::getDataSource('test2');
314
foreach (array('User', 'Comment') as $class) {
315
$this->_fixtures[$this->_fixtureClassMap[$class]]->create($db2);
316
$this->_fixtures[$this->_fixtureClassMap[$class]]->insert($db2);
317
$this->db->truncate(Inflector::pluralize(Inflector::underscore($class)));
320
$this->assertEqual($TestModel->User->find('all'), array());
321
$this->assertEqual($TestModel->Comment->find('all'), array());
322
$this->assertEqual($TestModel->find('count'), 3);
324
$TestModel->User->setDataSource('test2');
325
$TestModel->Comment->setDataSource('test2');
327
foreach ($expected as $key => $value) {
328
unset($value['Comment'], $value['Tag']);
329
$expected[$key] = $value;
332
$TestModel->recursive = 0;
333
$result = $TestModel->find('all');
334
$this->assertEqual($result, $expected);
336
foreach ($expected as $key => $value) {
337
unset($value['Comment'], $value['Tag']);
338
$expected[$key] = $value;
341
$TestModel->recursive = 0;
342
$result = $TestModel->find('all');
343
$this->assertEqual($result, $expected);
345
$result = Set::extract($TestModel->User->find('all'), '{n}.User.id');
346
$this->assertEqual($result, array('1', '2', '3', '4'));
347
$this->assertEqual($TestModel->find('all'), $expected);
349
$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')));
356
'comment' => 'First Comment for First Article',
358
'created' => '2007-03-18 10:45:23',
359
'updated' => '2007-03-18 10:47:31'
364
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
365
'created' => '2007-03-17 01:18:23',
366
'updated' => '2007-03-17 01:20:31'
371
'title' => 'First Article',
372
'body' => 'First Article Body',
374
'created' => '2007-03-18 10:39:23',
375
'updated' => '2007-03-18 10:41:31'
382
'comment' => 'Second Comment for First Article',
384
'created' => '2007-03-18 10:47:23',
385
'updated' => '2007-03-18 10:49:31'
390
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
391
'created' => '2007-03-17 01:22:23',
392
'updated' => '2007-03-17 01:24:31'
397
'title' => 'First Article',
398
'body' => 'First Article Body',
400
'created' => '2007-03-18 10:39:23',
401
'updated' => '2007-03-18 10:41:31'
408
'comment' => 'Third Comment for First Article',
410
'created' => '2007-03-18 10:49:23',
411
'updated' => '2007-03-18 10:51:31'
416
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
417
'created' => '2007-03-17 01:16:23',
418
'updated' => '2007-03-17 01:18:31'
423
'title' => 'First Article',
424
'body' => 'First Article Body',
426
'created' => '2007-03-18 10:39:23',
427
'updated' => '2007-03-18 10:41:31'
434
'comment' => 'Fourth Comment for First Article',
436
'created' => '2007-03-18 10:51:23',
437
'updated' => '2007-03-18 10:53:31'
442
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
443
'created' => '2007-03-17 01:16:23',
444
'updated' => '2007-03-17 01:18:31'
449
'title' => 'First Article',
450
'body' => 'First Article Body',
452
'created' => '2007-03-18 10:39:23',
453
'updated' => '2007-03-18 10:41:31'
460
'comment' => 'First Comment for Second Article',
462
'created' => '2007-03-18 10:53:23',
463
'updated' => '2007-03-18 10:55:31'
468
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
469
'created' => '2007-03-17 01:16:23',
470
'updated' => '2007-03-17 01:18:31'
475
'title' => 'Second Article',
476
'body' => 'Second Article Body',
478
'created' => '2007-03-18 10:41:23',
479
'updated' => '2007-03-18 10:43:31'
486
'comment' => 'Second Comment for Second Article',
488
'created' => '2007-03-18 10:55:23',
489
'updated' => '2007-03-18 10:57:31'
494
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
495
'created' => '2007-03-17 01:18:23',
496
'updated' => '2007-03-17 01:20:31'
501
'title' => 'Second Article',
502
'body' => 'Second Article Body',
504
'created' => '2007-03-18 10:41:23',
505
'updated' => '2007-03-18 10:43:31'
507
$this->assertEqual($TestModel->Comment->find('all'), $expected);
509
foreach (array('User', 'Comment') as $class) {
510
$this->_fixtures[$this->_fixtureClassMap[$class]]->drop($db2);
515
* testDisplayField method
520
function testDisplayField() {
521
$this->loadFixtures('Post', 'Comment', 'Person');
523
$Comment = new Comment();
524
$Person = new Person();
526
$this->assertEqual($Post->displayField, 'title');
527
$this->assertEqual($Person->displayField, 'name');
528
$this->assertEqual($Comment->displayField, 'id');
537
function testSchema() {
540
$result = $Post->schema();
541
$columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
542
$this->assertEqual(array_keys($result), $columns);
544
$types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
545
$this->assertEqual(Set::extract(array_values($result), '{n}.type'), $types);
547
$result = $Post->schema('body');
548
$this->assertEqual($result['type'], 'text');
549
$this->assertNull($Post->schema('foo'));
551
$this->assertEqual($Post->getColumnTypes(), array_combine($columns, $types));
555
* test deconstruct() with time fields.
559
function testDeconstructFieldsTime() {
560
$this->loadFixtures('Apple');
561
$TestModel =& new Apple();
564
$data['Apple']['mytime']['hour'] = '';
565
$data['Apple']['mytime']['min'] = '';
566
$data['Apple']['mytime']['sec'] = '';
568
$TestModel->data = null;
569
$TestModel->set($data);
570
$expected = array('Apple'=> array('mytime'=> ''));
571
$this->assertEqual($TestModel->data, $expected);
574
$data['Apple']['mytime']['hour'] = '';
575
$data['Apple']['mytime']['min'] = '';
576
$data['Apple']['mytime']['meridan'] = '';
578
$TestModel->data = null;
579
$TestModel->set($data);
580
$expected = array('Apple'=> array('mytime'=> ''));
581
$this->assertEqual($TestModel->data, $expected, 'Empty values are not returning properly. %s');
584
$data['Apple']['mytime']['hour'] = '12';
585
$data['Apple']['mytime']['min'] = '0';
586
$data['Apple']['mytime']['meridian'] = 'am';
588
$TestModel->data = null;
589
$TestModel->set($data);
590
$expected = array('Apple'=> array('mytime'=> '00:00:00'));
591
$this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
594
$data['Apple']['mytime']['hour'] = '00';
595
$data['Apple']['mytime']['min'] = '00';
597
$TestModel->data = null;
598
$TestModel->set($data);
599
$expected = array('Apple'=> array('mytime'=> '00:00:00'));
600
$this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
603
$data['Apple']['mytime']['hour'] = '03';
604
$data['Apple']['mytime']['min'] = '04';
605
$data['Apple']['mytime']['sec'] = '04';
607
$TestModel->data = null;
608
$TestModel->set($data);
609
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
610
$this->assertEqual($TestModel->data, $expected);
613
$data['Apple']['mytime']['hour'] = '3';
614
$data['Apple']['mytime']['min'] = '4';
615
$data['Apple']['mytime']['sec'] = '4';
617
$TestModel->data = null;
618
$TestModel->set($data);
619
$expected = array('Apple' => array('mytime'=> '03:04:04'));
620
$this->assertEqual($TestModel->data, $expected);
623
$data['Apple']['mytime']['hour'] = '03';
624
$data['Apple']['mytime']['min'] = '4';
625
$data['Apple']['mytime']['sec'] = '4';
627
$TestModel->data = null;
628
$TestModel->set($data);
629
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
630
$this->assertEqual($TestModel->data, $expected);
632
$db = ConnectionManager::getDataSource('test_suite');
634
$data['Apple']['mytime'] = $db->expression('NOW()');
635
$TestModel->data = null;
636
$TestModel->set($data);
637
$this->assertEqual($TestModel->data, $data);
641
* testDeconstructFields with datetime, timestamp, and date fields
646
function testDeconstructFieldsDateTime() {
647
$this->loadFixtures('Apple');
648
$TestModel =& new Apple();
650
//test null/empty values first
651
$data['Apple']['created']['year'] = '';
652
$data['Apple']['created']['month'] = '';
653
$data['Apple']['created']['day'] = '';
654
$data['Apple']['created']['hour'] = '';
655
$data['Apple']['created']['min'] = '';
656
$data['Apple']['created']['sec'] = '';
658
$TestModel->data = null;
659
$TestModel->set($data);
660
$expected = array('Apple'=> array('created'=> ''));
661
$this->assertEqual($TestModel->data, $expected);
664
$data['Apple']['date']['year'] = '';
665
$data['Apple']['date']['month'] = '';
666
$data['Apple']['date']['day'] = '';
668
$TestModel->data = null;
669
$TestModel->set($data);
670
$expected = array('Apple'=> array('date'=> ''));
671
$this->assertEqual($TestModel->data, $expected);
674
$data['Apple']['created']['year'] = '2007';
675
$data['Apple']['created']['month'] = '08';
676
$data['Apple']['created']['day'] = '20';
677
$data['Apple']['created']['hour'] = '';
678
$data['Apple']['created']['min'] = '';
679
$data['Apple']['created']['sec'] = '';
681
$TestModel->data = null;
682
$TestModel->set($data);
683
$expected = array('Apple'=> array('created'=> '2007-08-20 00:00:00'));
684
$this->assertEqual($TestModel->data, $expected);
687
$data['Apple']['created']['year'] = '2007';
688
$data['Apple']['created']['month'] = '08';
689
$data['Apple']['created']['day'] = '20';
690
$data['Apple']['created']['hour'] = '10';
691
$data['Apple']['created']['min'] = '12';
692
$data['Apple']['created']['sec'] = '';
694
$TestModel->data = null;
695
$TestModel->set($data);
696
$expected = array('Apple'=> array('created'=> '2007-08-20 10:12:00'));
697
$this->assertEqual($TestModel->data, $expected);
700
$data['Apple']['created']['year'] = '2007';
701
$data['Apple']['created']['month'] = '';
702
$data['Apple']['created']['day'] = '12';
703
$data['Apple']['created']['hour'] = '20';
704
$data['Apple']['created']['min'] = '';
705
$data['Apple']['created']['sec'] = '';
707
$TestModel->data = null;
708
$TestModel->set($data);
709
$expected = array('Apple'=> array('created'=> ''));
710
$this->assertEqual($TestModel->data, $expected);
713
$data['Apple']['created']['hour'] = '20';
714
$data['Apple']['created']['min'] = '33';
716
$TestModel->data = null;
717
$TestModel->set($data);
718
$expected = array('Apple'=> array('created'=> ''));
719
$this->assertEqual($TestModel->data, $expected);
722
$data['Apple']['created']['hour'] = '20';
723
$data['Apple']['created']['min'] = '33';
724
$data['Apple']['created']['sec'] = '33';
726
$TestModel->data = null;
727
$TestModel->set($data);
728
$expected = array('Apple'=> array('created'=> ''));
729
$this->assertEqual($TestModel->data, $expected);
732
$data['Apple']['created']['hour'] = '13';
733
$data['Apple']['created']['min'] = '00';
734
$data['Apple']['date']['year'] = '2006';
735
$data['Apple']['date']['month'] = '12';
736
$data['Apple']['date']['day'] = '25';
738
$TestModel->data = null;
739
$TestModel->set($data);
743
'date'=> '2006-12-25'
745
$this->assertEqual($TestModel->data, $expected);
748
$data['Apple']['created']['year'] = '2007';
749
$data['Apple']['created']['month'] = '08';
750
$data['Apple']['created']['day'] = '20';
751
$data['Apple']['created']['hour'] = '10';
752
$data['Apple']['created']['min'] = '12';
753
$data['Apple']['created']['sec'] = '09';
754
$data['Apple']['date']['year'] = '2006';
755
$data['Apple']['date']['month'] = '12';
756
$data['Apple']['date']['day'] = '25';
758
$TestModel->data = null;
759
$TestModel->set($data);
762
'created'=> '2007-08-20 10:12:09',
763
'date'=> '2006-12-25'
765
$this->assertEqual($TestModel->data, $expected);
768
$data['Apple']['created']['year'] = '--';
769
$data['Apple']['created']['month'] = '--';
770
$data['Apple']['created']['day'] = '--';
771
$data['Apple']['created']['hour'] = '--';
772
$data['Apple']['created']['min'] = '--';
773
$data['Apple']['created']['sec'] = '--';
774
$data['Apple']['date']['year'] = '--';
775
$data['Apple']['date']['month'] = '--';
776
$data['Apple']['date']['day'] = '--';
778
$TestModel->data = null;
779
$TestModel->set($data);
780
$expected = array('Apple'=> array('created'=> '', 'date'=> ''));
781
$this->assertEqual($TestModel->data, $expected);
784
$data['Apple']['created']['year'] = '2007';
785
$data['Apple']['created']['month'] = '--';
786
$data['Apple']['created']['day'] = '20';
787
$data['Apple']['created']['hour'] = '10';
788
$data['Apple']['created']['min'] = '12';
789
$data['Apple']['created']['sec'] = '09';
790
$data['Apple']['date']['year'] = '2006';
791
$data['Apple']['date']['month'] = '12';
792
$data['Apple']['date']['day'] = '25';
794
$TestModel->data = null;
795
$TestModel->set($data);
796
$expected = array('Apple'=> array('created'=> '', 'date'=> '2006-12-25'));
797
$this->assertEqual($TestModel->data, $expected);
800
$data['Apple']['date']['year'] = '2006';
801
$data['Apple']['date']['month'] = '12';
802
$data['Apple']['date']['day'] = '25';
804
$TestModel->data = null;
805
$TestModel->set($data);
806
$expected = array('Apple'=> array('date'=> '2006-12-25'));
807
$this->assertEqual($TestModel->data, $expected);
809
$db = ConnectionManager::getDataSource('test_suite');
811
$data['Apple']['modified'] = $db->expression('NOW()');
812
$TestModel->data = null;
813
$TestModel->set($data);
814
$this->assertEqual($TestModel->data, $data);
818
* testTablePrefixSwitching method
823
function testTablePrefixSwitching() {
824
ConnectionManager::create('database1',
825
array_merge($this->db->config, array('prefix' => 'aaa_')
827
ConnectionManager::create('database2',
828
array_merge($this->db->config, array('prefix' => 'bbb_')
831
$db1 = ConnectionManager::getDataSource('database1');
832
$db2 = ConnectionManager::getDataSource('database2');
834
$TestModel = new Apple();
835
$TestModel->setDataSource('database1');
836
$this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
837
$this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
838
$this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
840
$TestModel->setDataSource('database2');
841
$this->assertEqual($this->db->fullTableName($TestModel, false), 'bbb_apples');
842
$this->assertEqual($db1->fullTableName($TestModel, false), 'bbb_apples');
843
$this->assertEqual($db2->fullTableName($TestModel, false), 'bbb_apples');
845
$TestModel = new Apple();
846
$TestModel->tablePrefix = 'custom_';
847
$this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
848
$TestModel->setDataSource('database1');
849
$this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
850
$this->assertEqual($db1->fullTableName($TestModel, false), 'custom_apples');
852
$TestModel = new Apple();
853
$TestModel->setDataSource('database1');
854
$this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
855
$TestModel->tablePrefix = '';
856
$TestModel->setDataSource('database2');
857
$this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
858
$this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
860
$TestModel->tablePrefix = null;
861
$TestModel->setDataSource('database1');
862
$this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
863
$this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
865
$TestModel->tablePrefix = false;
866
$TestModel->setDataSource('database2');
867
$this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
868
$this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
872
* Tests validation parameter order in custom validation methods
877
function testInvalidAssociation() {
878
$TestModel =& new ValidationTest1();
879
$this->assertNull($TestModel->getAssociated('Foo'));
883
* testLoadModelSecondIteration method
888
function testLoadModelSecondIteration() {
889
$model = new ModelA();
890
$this->assertIsA($model,'ModelA');
892
$this->assertIsA($model->ModelB, 'ModelB');
893
$this->assertIsA($model->ModelB->ModelD, 'ModelD');
895
$this->assertIsA($model->ModelC, 'ModelC');
896
$this->assertIsA($model->ModelC->ModelD, 'ModelD');
900
* ensure that exists() does not persist between method calls reset on create
904
function testResetOfExistsOnCreate() {
905
$this->loadFixtures('Article');
906
$Article =& new Article();
908
$Article->saveField('title', 'Reset me');
911
$this->assertFalse($Article->exists());
914
$this->assertFalse($Article->exists());
916
$Article->saveField('title', 'Staying alive');
917
$result = $Article->read(null, 2);
918
$this->assertEqual($result['Article']['title'], 'Staying alive');
922
* testUseTableFalseExistsCheck method
926
function testUseTableFalseExistsCheck() {
927
$this->loadFixtures('Article');
928
$Article =& new Article();
930
$result = $Article->exists();
931
$this->assertFalse($result);
933
$Article->useTable = false;
935
$result = $Article->exists();
936
$this->assertFalse($result);
938
// An article with primary key of '1' has been loaded by the fixtures.
939
$Article->useTable = false;
941
$result = $Article->exists();
942
$this->assertTrue($result);
946
* testPluginAssociations method
951
function testPluginAssociations() {
952
$this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
953
$TestModel =& new TestPluginArticle();
955
$result = $TestModel->find('all');
958
'TestPluginArticle' => array(
961
'title' => 'First Plugin Article',
962
'body' => 'First Plugin Article Body',
964
'created' => '2008-09-24 10:39:23',
965
'updated' => '2008-09-24 10:41:31'
970
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
971
'created' => '2007-03-17 01:16:23',
972
'updated' => '2007-03-17 01:18:31'
974
'TestPluginComment' => array(
979
'comment' => 'First Comment for First Plugin Article',
981
'created' => '2008-09-24 10:45:23',
982
'updated' => '2008-09-24 10:47:31'
988
'comment' => 'Second Comment for First Plugin Article',
990
'created' => '2008-09-24 10:47:23',
991
'updated' => '2008-09-24 10:49:31'
997
'comment' => 'Third Comment for First Plugin Article',
999
'created' => '2008-09-24 10:49:23',
1000
'updated' => '2008-09-24 10:51:31'
1006
'comment' => 'Fourth Comment for First Plugin Article',
1008
'created' => '2008-09-24 10:51:23',
1009
'updated' => '2008-09-24 10:53:31'
1012
'TestPluginArticle' => array(
1015
'title' => 'Second Plugin Article',
1016
'body' => 'Second Plugin Article Body',
1018
'created' => '2008-09-24 10:41:23',
1019
'updated' => '2008-09-24 10:43:31'
1024
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1025
'created' => '2007-03-17 01:20:23',
1026
'updated' => '2007-03-17 01:22:31'
1028
'TestPluginComment' => array(
1033
'comment' => 'First Comment for Second Plugin Article',
1035
'created' => '2008-09-24 10:53:23',
1036
'updated' => '2008-09-24 10:55:31'
1042
'comment' => 'Second Comment for Second Plugin Article',
1044
'created' => '2008-09-24 10:55:23',
1045
'updated' => '2008-09-24 10:57:31'
1048
'TestPluginArticle' => array(
1051
'title' => 'Third Plugin Article',
1052
'body' => 'Third Plugin Article Body',
1054
'created' => '2008-09-24 10:43:23',
1055
'updated' => '2008-09-24 10:45:31'
1059
'user' => 'mariano',
1060
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1061
'created' => '2007-03-17 01:16:23',
1062
'updated' => '2007-03-17 01:18:31'
1064
'TestPluginComment' => array()
1067
$this->assertEqual($result, $expected);
1071
* Tests getAssociated method
1076
function testGetAssociated() {
1077
$this->loadFixtures('Article');
1078
$Article = ClassRegistry::init('Article');
1080
$assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
1081
foreach ($assocTypes as $type) {
1082
$this->assertEqual($Article->getAssociated($type), array_keys($Article->{$type}));
1085
$Article->bindModel(array('hasMany' => array('Category')));
1086
$this->assertEqual($Article->getAssociated('hasMany'), array('Comment', 'Category'));
1088
$results = $Article->getAssociated();
1089
$this->assertEqual(sort(array_keys($results)), array('Category', 'Comment', 'Tag'));
1091
$Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
1092
$this->assertEqual($Article->getAssociated('hasAndBelongsToMany'), array());
1094
$result = $Article->getAssociated('Category');
1096
'className' => 'Category',
1097
'foreignKey' => 'article_id',
1105
'finderQuery' => '',
1106
'counterQuery' => '',
1107
'association' => 'hasMany',
1109
$this->assertEqual($result, $expected);
1113
* testAutoConstructAssociations method
1118
function testAutoConstructAssociations() {
1119
$this->loadFixtures('User', 'ArticleFeatured');
1120
$TestModel =& new AssociationTest1();
1122
$result = $TestModel->hasAndBelongsToMany;
1123
$expected = array('AssociationTest2' => array(
1125
'joinTable' => 'join_as_join_bs',
1126
'foreignKey' => false,
1127
'className' => 'AssociationTest2',
1128
'with' => 'JoinAsJoinB',
1129
'associationForeignKey' => 'join_b_id',
1130
'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
1131
'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
1133
$this->assertEqual($result, $expected);
1135
// Tests related to ticket https://trac.cakephp.org/ticket/5594
1136
$TestModel =& new ArticleFeatured();
1137
$TestFakeModel =& new ArticleFeatured(array('table' => false));
1141
'className' => 'User', 'foreignKey' => 'user_id',
1142
'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
1144
'Category' => array(
1145
'className' => 'Category', 'foreignKey' => 'category_id',
1146
'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
1149
$this->assertIdentical($TestModel->belongsTo, $expected);
1150
$this->assertIdentical($TestFakeModel->belongsTo, $expected);
1152
$this->assertEqual($TestModel->User->name, 'User');
1153
$this->assertEqual($TestFakeModel->User->name, 'User');
1154
$this->assertEqual($TestModel->Category->name, 'Category');
1155
$this->assertEqual($TestFakeModel->Category->name, 'Category');
1158
'Featured' => array(
1159
'className' => 'Featured',
1160
'foreignKey' => 'article_featured_id',
1167
$this->assertIdentical($TestModel->hasOne, $expected);
1168
$this->assertIdentical($TestFakeModel->hasOne, $expected);
1170
$this->assertEqual($TestModel->Featured->name, 'Featured');
1171
$this->assertEqual($TestFakeModel->Featured->name, 'Featured');
1175
'className' => 'Comment',
1176
'dependent' => true,
1177
'foreignKey' => 'article_featured_id',
1184
'finderQuery' => '',
1185
'counterQuery' => ''
1188
$this->assertIdentical($TestModel->hasMany, $expected);
1189
$this->assertIdentical($TestFakeModel->hasMany, $expected);
1191
$this->assertEqual($TestModel->Comment->name, 'Comment');
1192
$this->assertEqual($TestFakeModel->Comment->name, 'Comment');
1196
'className' => 'Tag',
1197
'joinTable' => 'article_featureds_tags',
1198
'with' => 'ArticleFeaturedsTag',
1199
'foreignKey' => 'article_featured_id',
1200
'associationForeignKey' => 'tag_id',
1207
'finderQuery' => '',
1208
'deleteQuery' => '',
1212
$this->assertIdentical($TestModel->hasAndBelongsToMany, $expected);
1213
$this->assertIdentical($TestFakeModel->hasAndBelongsToMany, $expected);
1215
$this->assertEqual($TestModel->Tag->name, 'Tag');
1216
$this->assertEqual($TestFakeModel->Tag->name, 'Tag');
1220
* test Model::__construct
1222
* ensure that $actsAS and $_findMethods are merged.
1226
function testConstruct() {
1227
$this->loadFixtures('Post', 'Comment');
1229
$TestModel =& ClassRegistry::init('MergeVarPluginPost');
1230
$this->assertEqual($TestModel->actsAs, array('Containable', 'Tree'));
1231
$this->assertTrue(isset($TestModel->Behaviors->Containable));
1232
$this->assertTrue(isset($TestModel->Behaviors->Tree));
1234
$TestModel =& ClassRegistry::init('MergeVarPluginComment');
1235
$expected = array('Containable', 'Containable' => array('some_settings'));
1236
$this->assertEqual($TestModel->actsAs, $expected);
1237
$this->assertTrue(isset($TestModel->Behaviors->Containable));
1241
* test Model::__construct
1243
* ensure that $actsAS and $_findMethods are merged.
1247
function testConstructWithAlternateDataSource() {
1248
$TestModel =& ClassRegistry::init(array(
1249
'class' => 'DoesntMatter', 'ds' => 'test_suite', 'table' => false
1251
$this->assertEqual('test_suite', $TestModel->useDbConfig);
1253
//deprecated but test it anyway
1254
$NewVoid =& new TheVoid(null, false, 'other');
1255
$this->assertEqual('other', $NewVoid->useDbConfig);
1259
* testColumnTypeFetching method
1264
function testColumnTypeFetching() {
1265
$model =& new Test();
1266
$this->assertEqual($model->getColumnType('id'), 'integer');
1267
$this->assertEqual($model->getColumnType('notes'), 'text');
1268
$this->assertEqual($model->getColumnType('updated'), 'datetime');
1269
$this->assertEqual($model->getColumnType('unknown'), null);
1271
$model =& new Article();
1272
$this->assertEqual($model->getColumnType('User.created'), 'datetime');
1273
$this->assertEqual($model->getColumnType('Tag.id'), 'integer');
1274
$this->assertEqual($model->getColumnType('Article.id'), 'integer');
1278
* testHabtmUniqueKey method
1283
function testHabtmUniqueKey() {
1284
$model =& new Item();
1285
$this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
1289
* testIdentity method
1294
function testIdentity() {
1295
$TestModel =& new Test();
1296
$result = $TestModel->alias;
1298
$this->assertEqual($result, $expected);
1300
$TestModel =& new TestAlias();
1301
$result = $TestModel->alias;
1302
$expected = 'TestAlias';
1303
$this->assertEqual($result, $expected);
1305
$TestModel =& new Test(array('alias' => 'AnotherTest'));
1306
$result = $TestModel->alias;
1307
$expected = 'AnotherTest';
1308
$this->assertEqual($result, $expected);
1312
* testWithAssociation method
1317
function testWithAssociation() {
1318
$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
1319
$TestModel =& new Something();
1320
$result = $TestModel->SomethingElse->find('all');
1324
'SomethingElse' => array(
1326
'title' => 'First Post',
1327
'body' => 'First Post Body',
1329
'created' => '2007-03-18 10:39:23',
1330
'updated' => '2007-03-18 10:41:31'
1332
'Something' => array(
1335
'title' => 'Third Post',
1336
'body' => 'Third Post Body',
1338
'created' => '2007-03-18 10:43:23',
1339
'updated' => '2007-03-18 10:45:31',
1340
'JoinThing' => array(
1342
'something_id' => '3',
1343
'something_else_id' => '1',
1345
'created' => '2007-03-18 10:43:23',
1346
'updated' => '2007-03-18 10:45:31'
1349
'SomethingElse' => array(
1351
'title' => 'Second Post',
1352
'body' => 'Second Post Body',
1354
'created' => '2007-03-18 10:41:23',
1355
'updated' => '2007-03-18 10:43:31'
1357
'Something' => array(
1360
'title' => 'First Post',
1361
'body' => 'First Post Body',
1363
'created' => '2007-03-18 10:39:23',
1364
'updated' => '2007-03-18 10:41:31',
1365
'JoinThing' => array(
1367
'something_id' => '1',
1368
'something_else_id' => '2',
1370
'created' => '2007-03-18 10:39:23',
1371
'updated' => '2007-03-18 10:41:31'
1374
'SomethingElse' => array(
1376
'title' => 'Third Post',
1377
'body' => 'Third Post Body',
1379
'created' => '2007-03-18 10:43:23',
1380
'updated' => '2007-03-18 10:45:31'
1382
'Something' => array(
1385
'title' => 'Second Post',
1386
'body' => 'Second Post Body',
1388
'created' => '2007-03-18 10:41:23',
1389
'updated' => '2007-03-18 10:43:31',
1390
'JoinThing' => array(
1392
'something_id' => '2',
1393
'something_else_id' => '3',
1395
'created' => '2007-03-18 10:41:23',
1396
'updated' => '2007-03-18 10:43:31'
1398
$this->assertEqual($result, $expected);
1400
$result = $TestModel->find('all');
1403
'Something' => array(
1405
'title' => 'First Post',
1406
'body' => 'First Post Body',
1408
'created' => '2007-03-18 10:39:23',
1409
'updated' => '2007-03-18 10:41:31'
1411
'SomethingElse' => array(
1414
'title' => 'Second Post',
1415
'body' => 'Second Post Body',
1417
'created' => '2007-03-18 10:41:23',
1418
'updated' => '2007-03-18 10:43:31',
1419
'JoinThing' => array(
1421
'something_id' => '1',
1422
'something_else_id' => '2'
1425
'Something' => array(
1427
'title' => 'Second Post',
1428
'body' => 'Second Post Body',
1430
'created' => '2007-03-18 10:41:23',
1431
'updated' => '2007-03-18 10:43:31'
1433
'SomethingElse' => array(
1436
'title' => 'Third Post',
1437
'body' => 'Third Post Body',
1439
'created' => '2007-03-18 10:43:23',
1440
'updated' => '2007-03-18 10:45:31',
1441
'JoinThing' => array(
1443
'something_id' => '2',
1444
'something_else_id' => '3'
1447
'Something' => array(
1449
'title' => 'Third Post',
1450
'body' => 'Third Post Body',
1452
'created' => '2007-03-18 10:43:23',
1453
'updated' => '2007-03-18 10:45:31'
1455
'SomethingElse' => array(
1458
'title' => 'First Post',
1459
'body' => 'First Post Body',
1461
'created' => '2007-03-18 10:39:23',
1462
'updated' => '2007-03-18 10:41:31',
1463
'JoinThing' => array(
1465
'something_id' => '3',
1466
'something_else_id' => '1'
1468
$this->assertEqual($result, $expected);
1470
$result = $TestModel->findById(1);
1472
'Something' => array(
1474
'title' => 'First Post',
1475
'body' => 'First Post Body',
1477
'created' => '2007-03-18 10:39:23',
1478
'updated' => '2007-03-18 10:41:31'
1480
'SomethingElse' => array(
1483
'title' => 'Second Post',
1484
'body' => 'Second Post Body',
1486
'created' => '2007-03-18 10:41:23',
1487
'updated' => '2007-03-18 10:43:31',
1488
'JoinThing' => array(
1490
'something_id' => '1',
1491
'something_else_id' => '2'
1493
$this->assertEqual($result, $expected);
1495
$expected = $TestModel->findById(1);
1496
$TestModel->set($expected);
1498
$result = $TestModel->findById(1);
1499
$this->assertEqual($result, $expected);
1501
$TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
1502
$TestModel->create(array(
1503
'Something' => array('id' => 1),
1504
'SomethingElse' => array(3, array(
1505
'something_else_id' => 1,
1509
$ts = date('Y-m-d H:i:s');
1512
$TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
1513
$result = $TestModel->findById(1);
1515
'Something' => array(
1517
'title' => 'First Post',
1518
'body' => 'First Post Body',
1520
'created' => '2007-03-18 10:39:23',
1522
'SomethingElse' => array(
1525
'title' => 'First Post',
1526
'body' => 'First Post Body',
1528
'created' => '2007-03-18 10:39:23',
1529
'updated' => '2007-03-18 10:41:31',
1530
'JoinThing' => array(
1532
'something_id' => '1',
1533
'something_else_id' => '1'
1537
'title' => 'Second Post',
1538
'body' => 'Second Post Body',
1540
'created' => '2007-03-18 10:41:23',
1541
'updated' => '2007-03-18 10:43:31',
1542
'JoinThing' => array(
1544
'something_id' => '1',
1545
'something_else_id' => '2'
1549
'title' => 'Third Post',
1550
'body' => 'Third Post Body',
1552
'created' => '2007-03-18 10:43:23',
1553
'updated' => '2007-03-18 10:45:31',
1554
'JoinThing' => array(
1556
'something_id' => '1',
1557
'something_else_id' => '3'
1560
$this->assertEqual($result, $expected);
1564
* testFindSelfAssociations method
1569
function testFindSelfAssociations() {
1570
$this->loadFixtures('Person');
1572
$TestModel =& new Person();
1573
$TestModel->recursive = 2;
1574
$result = $TestModel->read(null, 1);
1589
'name' => 'mother - grand mother',
1595
'name' => 'mother - grand father',
1606
'name' => 'father - grand father',
1612
'name' => 'father - grand mother',
1617
$this->assertEqual($result, $expected);
1619
$TestModel->recursive = 3;
1620
$result = $TestModel->read(null, 1);
1635
'name' => 'mother - grand mother',
1638
'Mother' => array(),
1639
'Father' => array()),
1642
'name' => 'mother - grand father',
1645
'Father' => array(),
1655
'name' => 'father - grand father',
1658
'Father' => array(),
1663
'name' => 'father - grand mother',
1666
'Mother' => array(),
1670
$this->assertEqual($result, $expected);
1674
* testDynamicAssociations method
1679
function testDynamicAssociations() {
1680
$this->loadFixtures('Article', 'Comment');
1681
$TestModel =& new Article();
1683
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
1684
$TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
1685
'foreignKey' => false,
1686
'conditions' => array('Comment.user_id =' => '2')
1688
$result = $TestModel->find('all');
1694
'title' => 'First Article',
1695
'body' => 'First Article Body',
1697
'created' => '2007-03-18 10:39:23',
1698
'updated' => '2007-03-18 10:41:31'
1703
'article_id' => '1',
1705
'comment' => 'First Comment for First Article',
1707
'created' => '2007-03-18 10:45:23',
1708
'updated' => '2007-03-18 10:47:31'
1712
'article_id' => '2',
1714
'comment' => 'Second Comment for Second Article',
1716
'created' => '2007-03-18 10:55:23',
1717
'updated' => '2007-03-18 10:57:31'
1723
'title' => 'Second Article',
1724
'body' => 'Second Article Body',
1726
'created' => '2007-03-18 10:41:23',
1727
'updated' => '2007-03-18 10:43:31'
1732
'article_id' => '1',
1734
'comment' => 'First Comment for First Article',
1736
'created' => '2007-03-18 10:45:23',
1737
'updated' => '2007-03-18 10:47:31'
1741
'article_id' => '2',
1743
'comment' => 'Second Comment for Second Article',
1745
'created' => '2007-03-18 10:55:23',
1746
'updated' => '2007-03-18 10:57:31'
1752
'title' => 'Third Article',
1753
'body' => 'Third Article Body',
1755
'created' => '2007-03-18 10:43:23',
1756
'updated' => '2007-03-18 10:45:31'
1761
'article_id' => '1',
1763
'comment' => 'First Comment for First Article',
1765
'created' => '2007-03-18 10:45:23',
1766
'updated' => '2007-03-18 10:47:31'
1770
'article_id' => '2',
1772
'comment' => 'Second Comment for Second Article',
1774
'created' => '2007-03-18 10:55:23',
1775
'updated' => '2007-03-18 10:57:31'
1778
$this->assertEqual($result, $expected);
1782
* testCreation method
1787
function testCreation() {
1788
$this->loadFixtures('Article');
1789
$TestModel =& new Test();
1790
$result = $TestModel->create();
1791
$expected = array('Test' => array('notes' => 'write some notes here'));
1792
$this->assertEqual($result, $expected);
1793
$TestModel =& new User();
1794
$result = $TestModel->schema();
1796
if (isset($this->db->columns['primary_key']['length'])) {
1797
$intLength = $this->db->columns['primary_key']['length'];
1798
} elseif (isset($this->db->columns['integer']['length'])) {
1799
$intLength = $this->db->columns['integer']['length'];
1803
foreach (array('collate', 'charset') as $type) {
1804
unset($result['user'][$type]);
1805
unset($result['password'][$type]);
1810
'type' => 'integer',
1813
'length' => $intLength,
1822
'password' => array(
1829
'type' => 'datetime',
1835
'type' => 'datetime',
1841
$this->assertEqual($result, $expected);
1843
$TestModel =& new Article();
1844
$result = $TestModel->create();
1845
$expected = array('Article' => array('published' => 'N'));
1846
$this->assertEqual($result, $expected);
1848
$FeaturedModel =& new Featured();
1850
'article_featured_id' => 1,
1852
'published_date' => array(
1857
'end_date' => array(
1864
'Featured' => array(
1865
'article_featured_id' => 1,
1867
'published_date' => '2008-6-11 00:00:00',
1868
'end_date' => '2008-6-20 00:00:00'
1871
$this->assertEqual($FeaturedModel->create($data), $expected);
1874
'published_date' => array(
1879
'end_date' => array(
1884
'article_featured_id' => 1,
1889
'Featured' => array(
1890
'published_date' => '2008-6-11 00:00:00',
1891
'end_date' => '2008-6-20 00:00:00',
1892
'article_featured_id' => 1,
1896
$this->assertEqual($FeaturedModel->create($data), $expected);
1900
* testEscapeField to prove it escapes the field well even when it has part of the alias on it
1901
* @see ttp://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/473-escapefield-doesnt-consistently-prepend-modelname
1906
function testEscapeField() {
1907
$TestModel =& new Test();
1908
$db =& $TestModel->getDataSource();
1910
$result = $TestModel->escapeField('test_field');
1911
$expected = $db->name('Test.test_field');
1912
$this->assertEqual($result, $expected);
1914
$result = $TestModel->escapeField('TestField');
1915
$expected = $db->name('Test.TestField');
1916
$this->assertEqual($result, $expected);
1918
$result = $TestModel->escapeField('DomainHandle', 'Domain');
1919
$expected = $db->name('Domain.DomainHandle');
1920
$this->assertEqual($result, $expected);
1922
ConnectionManager::create('mock', array('driver' => 'mock'));
1923
$TestModel->setDataSource('mock');
1924
$db =& $TestModel->getDataSource();
1926
$result = $TestModel->escapeField('DomainHandle', 'Domain');
1927
$expected = $db->name('Domain.DomainHandle');
1928
$this->assertEqual($result, $expected);