Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ModelIntegrationTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Model
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
20
 
21
App::uses('DboSource', 'Model/Datasource');
22
App::uses('DboMock', 'Model/Datasource');
23
 
24
/**
25
 * DboMock class
26
 * A Dbo Source driver to mock a connection and a identity name() method
27
 */
28
class DboMock extends DboSource {
29
 
30
/**
31
 * Returns the $field without modifications
32
 */
33
	public function name($field) {
34
		return $field;
35
	}
36
 
37
/**
38
 * Returns true to fake a database connection
39
 */
40
	public function connect() {
41
		return true;
42
	}
43
 
44
}
45
 
46
/**
47
 * ModelIntegrationTest
48
 *
49
 * @package       Cake.Test.Case.Model
50
 */
51
class ModelIntegrationTest extends BaseModelTest {
52
 
53
/**
54
 * testAssociationLazyLoading
55
 *
56
 * @group lazyloading
57
 * @return void
58
 */
59
	public function testAssociationLazyLoading() {
60
		$this->loadFixtures('ArticleFeaturedsTags');
61
		$Article = new ArticleFeatured();
62
		$this->assertTrue(isset($Article->belongsTo['User']));
63
		$this->assertFalse(property_exists($Article, 'User'));
64
		$this->assertInstanceOf('User', $Article->User);
65
 
66
		$this->assertTrue(isset($Article->belongsTo['Category']));
67
		$this->assertFalse(property_exists($Article, 'Category'));
68
		$this->assertTrue(isset($Article->Category));
69
		$this->assertInstanceOf('Category', $Article->Category);
70
 
71
		$this->assertTrue(isset($Article->hasMany['Comment']));
72
		$this->assertFalse(property_exists($Article, 'Comment'));
73
		$this->assertTrue(isset($Article->Comment));
74
		$this->assertInstanceOf('Comment', $Article->Comment);
75
 
76
		$this->assertTrue(isset($Article->hasAndBelongsToMany['Tag']));
77
		//There was not enough information to setup the association (joinTable and associationForeignKey)
78
		//so the model was not lazy loaded
79
		$this->assertTrue(property_exists($Article, 'Tag'));
80
		$this->assertTrue(isset($Article->Tag));
81
		$this->assertInstanceOf('Tag', $Article->Tag);
82
 
83
		$this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag'));
84
		$this->assertInstanceOf('AppModel', $Article->ArticleFeaturedsTag);
85
		$this->assertEquals('article_featureds_tags', $Article->hasAndBelongsToMany['Tag']['joinTable']);
86
		$this->assertEquals('tag_id', $Article->hasAndBelongsToMany['Tag']['associationForeignKey']);
87
	}
88
 
89
/**
90
 * testAssociationLazyLoadWithHABTM
91
 *
92
 * @group lazyloading
93
 * @return void
94
 */
95
	public function testAssociationLazyLoadWithHABTM() {
96
		$this->loadFixtures('FruitsUuidTag', 'ArticlesTag');
97
		$this->db->cacheSources = false;
98
		$Article = new ArticleB();
99
		$this->assertTrue(isset($Article->hasAndBelongsToMany['TagB']));
100
		$this->assertFalse(property_exists($Article, 'TagB'));
101
		$this->assertInstanceOf('TagB', $Article->TagB);
102
 
103
		$this->assertFalse(property_exists($Article, 'ArticlesTag'));
104
		$this->assertInstanceOf('AppModel', $Article->ArticlesTag);
105
 
106
		$UuidTag = new UuidTag();
107
		$this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit']));
108
		$this->assertFalse(property_exists($UuidTag, 'Fruit'));
109
		$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
110
		$this->assertTrue(isset($UuidTag->Fruit));
111
 
112
		$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
113
		$this->assertTrue(isset($UuidTag->FruitsUuidTag));
114
		$this->assertInstanceOf('FruitsUuidTag', $UuidTag->FruitsUuidTag);
115
	}
116
 
117
/**
118
 * testAssociationLazyLoadWithBindModel
119
 *
120
 * @group lazyloading
121
 * @return void
122
 */
123
	public function testAssociationLazyLoadWithBindModel() {
124
		$this->loadFixtures('Article', 'User');
125
		$Article = new ArticleB();
126
 
127
		$this->assertFalse(isset($Article->belongsTo['User']));
128
		$this->assertFalse(property_exists($Article, 'User'));
129
 
130
		$Article->bindModel(array('belongsTo' => array('User')));
131
		$this->assertTrue(isset($Article->belongsTo['User']));
132
		$this->assertFalse(property_exists($Article, 'User'));
133
		$this->assertInstanceOf('User', $Article->User);
134
	}
135
 
136
/**
137
 * Tests that creating a model with no existent database table associated will throw an exception
138
 *
139
 * @expectedException MissingTableException
140
 * @return void
141
 */
142
	public function testMissingTable() {
143
		$Article = new ArticleB(false, uniqid());
144
		$Article->schema();
145
	}
146
 
147
/**
148
 * testPkInHAbtmLinkModelArticleB
149
 *
150
 * @return void
151
 */
152
	public function testPkInHabtmLinkModelArticleB() {
153
		$this->loadFixtures('Article', 'Tag', 'ArticlesTag');
154
		$TestModel = new ArticleB();
155
		$this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey);
156
	}
157
 
158
/**
159
 * Tests that $cacheSources is restored despite the settings on the model.
160
 *
161
 * @return void
162
 */
163
	public function testCacheSourcesRestored() {
164
		$this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
165
		$this->db->cacheSources = true;
166
		$TestModel = new JoinA();
167
		$TestModel->cacheSources = false;
168
		$TestModel->setSource('join_as');
169
		$this->assertTrue($this->db->cacheSources);
170
 
171
		$this->db->cacheSources = false;
172
		$TestModel = new JoinA();
173
		$TestModel->cacheSources = true;
174
		$TestModel->setSource('join_as');
175
		$this->assertFalse($this->db->cacheSources);
176
	}
177
 
178
/**
179
 * testPkInHabtmLinkModel method
180
 *
181
 * @return void
182
 */
183
	public function testPkInHabtmLinkModel() {
184
		//Test Nonconformant Models
185
		$this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC', 'ItemsPortfolio');
186
		$TestModel = new Content();
187
		$this->assertEquals('iContentAccountsId', $TestModel->ContentAccount->primaryKey);
188
 
189
		//test conformant models with no PK in the join table
190
		$this->loadFixtures('Article', 'Tag');
191
		$TestModel = new Article();
192
		$this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey);
193
 
194
		//test conformant models with PK in join table
195
		$TestModel = new Portfolio();
196
		$this->assertEquals('id', $TestModel->ItemsPortfolio->primaryKey);
197
 
198
		//test conformant models with PK in join table - join table contains extra field
199
		$this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
200
		$TestModel = new JoinA();
201
		$this->assertEquals('id', $TestModel->JoinAsJoinB->primaryKey);
202
	}
203
 
204
/**
205
 * testDynamicBehaviorAttachment method
206
 *
207
 * @return void
208
 */
209
	public function testDynamicBehaviorAttachment() {
210
		$this->loadFixtures('Apple', 'Sample', 'Author');
211
		$TestModel = new Apple();
212
		$this->assertEquals(array(), $TestModel->Behaviors->loaded());
213
 
214
		$TestModel->Behaviors->load('Tree', array('left' => 'left_field', 'right' => 'right_field'));
215
		$this->assertTrue(is_object($TestModel->Behaviors->Tree));
216
		$this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
217
 
218
		$expected = array(
219
			'parent' => 'parent_id',
220
			'left' => 'left_field',
221
			'right' => 'right_field',
222
			'scope' => '1 = 1',
223
			'type' => 'nested',
224
			'__parentChange' => false,
225
			'recursive' => -1
226
		);
227
		$this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
228
 
229
		$TestModel->Behaviors->load('Tree', array('enabled' => false));
230
		$this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
231
		$this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
232
 
233
		$TestModel->Behaviors->unload('Tree');
234
		$this->assertEquals(array(), $TestModel->Behaviors->loaded());
235
		$this->assertFalse(isset($TestModel->Behaviors->Tree));
236
	}
237
 
238
/**
239
 * testFindWithJoinsOption method
240
 *
241
 * @return void
242
 */
243
	public function testFindWithJoinsOption() {
244
		$this->loadFixtures('Article', 'User');
245
		$TestUser = new User();
246
 
247
		$options = array(
248
			'fields' => array(
249
				'user',
250
				'Article.published',
251
			),
252
			'joins' => array(
253
				array(
254
					'table' => 'articles',
255
					'alias' => 'Article',
256
					'type' => 'LEFT',
257
					'conditions' => array(
258
						'User.id = Article.user_id',
259
					),
260
				),
261
			),
262
			'group' => array('User.user', 'Article.published'),
263
			'recursive' => -1,
264
			'order' => array('User.user')
265
		);
266
		$result = $TestUser->find('all', $options);
267
		$expected = array(
268
			array('User' => array('user' => 'garrett'), 'Article' => array('published' => '')),
269
			array('User' => array('user' => 'larry'), 'Article' => array('published' => 'Y')),
270
			array('User' => array('user' => 'mariano'), 'Article' => array('published' => 'Y')),
271
			array('User' => array('user' => 'nate'), 'Article' => array('published' => ''))
272
		);
273
		$this->assertEquals($expected, $result);
274
	}
275
 
276
/**
277
 * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
278
 * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
279
 * or one connection will step on the other.
280
 */
281
	public function testCrossDatabaseJoins() {
282
		$config = ConnectionManager::enumConnectionObjects();
283
 
284
		$skip = (!isset($config['test']) || !isset($config['test2']));
285
		if ($skip) {
286
			$this->markTestSkipped('Primary and secondary test databases not configured, skipping cross-database
287
				join tests. To run theses tests defined $test and $test2 in your database configuration.'
288
			);
289
		}
290
 
291
		$this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment');
292
		$TestModel = new Article();
293
 
294
		$expected = array(
295
			array(
296
				'Article' => array(
297
					'id' => '1',
298
					'user_id' => '1',
299
					'title' => 'First Article',
300
					'body' => 'First Article Body',
301
					'published' => 'Y',
302
					'created' => '2007-03-18 10:39:23',
303
					'updated' => '2007-03-18 10:41:31'
304
				),
305
				'User' => array(
306
					'id' => '1',
307
					'user' => 'mariano',
308
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
309
					'created' => '2007-03-17 01:16:23',
310
					'updated' => '2007-03-17 01:18:31'
311
				),
312
				'Comment' => array(
313
					array(
314
						'id' => '1',
315
						'article_id' => '1',
316
						'user_id' => '2',
317
						'comment' => 'First Comment for First Article',
318
						'published' => 'Y',
319
						'created' => '2007-03-18 10:45:23',
320
						'updated' => '2007-03-18 10:47:31'
321
					),
322
					array(
323
						'id' => '2',
324
						'article_id' => '1',
325
						'user_id' => '4',
326
						'comment' => 'Second Comment for First Article',
327
						'published' => 'Y',
328
						'created' => '2007-03-18 10:47:23',
329
						'updated' => '2007-03-18 10:49:31'
330
					),
331
					array(
332
						'id' => '3',
333
						'article_id' => '1',
334
						'user_id' => '1',
335
						'comment' => 'Third Comment for First Article',
336
						'published' => 'Y',
337
						'created' => '2007-03-18 10:49:23',
338
						'updated' => '2007-03-18 10:51:31'
339
					),
340
					array(
341
						'id' => '4',
342
						'article_id' => '1',
343
						'user_id' => '1',
344
						'comment' => 'Fourth Comment for First Article',
345
						'published' => 'N',
346
						'created' => '2007-03-18 10:51:23',
347
						'updated' => '2007-03-18 10:53:31'
348
				)),
349
				'Tag' => array(
350
					array(
351
						'id' => '1',
352
						'tag' => 'tag1',
353
						'created' => '2007-03-18 12:22:23',
354
						'updated' => '2007-03-18 12:24:31'
355
					),
356
					array(
357
						'id' => '2',
358
						'tag' => 'tag2',
359
						'created' => '2007-03-18 12:24:23',
360
						'updated' => '2007-03-18 12:26:31'
361
			))),
362
			array(
363
				'Article' => array(
364
					'id' => '2',
365
					'user_id' => '3',
366
					'title' => 'Second Article',
367
					'body' => 'Second Article Body',
368
					'published' => 'Y',
369
					'created' => '2007-03-18 10:41:23',
370
					'updated' => '2007-03-18 10:43:31'
371
				),
372
				'User' => array(
373
					'id' => '3',
374
					'user' => 'larry',
375
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
376
					'created' => '2007-03-17 01:20:23',
377
					'updated' => '2007-03-17 01:22:31'
378
				),
379
				'Comment' => array(
380
					array(
381
						'id' => '5',
382
						'article_id' => '2',
383
						'user_id' => '1',
384
						'comment' => 'First Comment for Second Article',
385
						'published' => 'Y',
386
						'created' => '2007-03-18 10:53:23',
387
						'updated' => '2007-03-18 10:55:31'
388
					),
389
					array(
390
						'id' => '6',
391
						'article_id' => '2',
392
						'user_id' => '2',
393
						'comment' => 'Second Comment for Second Article',
394
						'published' => 'Y',
395
						'created' => '2007-03-18 10:55:23',
396
						'updated' => '2007-03-18 10:57:31'
397
				)),
398
				'Tag' => array(
399
					array(
400
						'id' => '1',
401
						'tag' => 'tag1',
402
						'created' => '2007-03-18 12:22:23',
403
						'updated' => '2007-03-18 12:24:31'
404
					),
405
					array(
406
						'id' => '3',
407
						'tag' => 'tag3',
408
						'created' => '2007-03-18 12:26:23',
409
						'updated' => '2007-03-18 12:28:31'
410
			))),
411
			array(
412
				'Article' => array(
413
					'id' => '3',
414
					'user_id' => '1',
415
					'title' => 'Third Article',
416
					'body' => 'Third Article Body',
417
					'published' => 'Y',
418
					'created' => '2007-03-18 10:43:23',
419
					'updated' => '2007-03-18 10:45:31'
420
				),
421
				'User' => array(
422
					'id' => '1',
423
					'user' => 'mariano',
424
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
425
					'created' => '2007-03-17 01:16:23',
426
					'updated' => '2007-03-17 01:18:31'
427
				),
428
				'Comment' => array(),
429
				'Tag' => array()
430
		));
431
		$this->assertEquals($expected, $TestModel->find('all'));
432
 
433
		$db2 = ConnectionManager::getDataSource('test2');
434
		$this->fixtureManager->loadSingle('User', $db2);
435
		$this->fixtureManager->loadSingle('Comment', $db2);
436
		$this->assertEquals(3, $TestModel->find('count'));
437
 
438
		$TestModel->User->setDataSource('test2');
439
		$TestModel->Comment->setDataSource('test2');
440
 
441
		foreach ($expected as $key => $value) {
442
			unset($value['Comment'], $value['Tag']);
443
			$expected[$key] = $value;
444
		}
445
 
446
		$TestModel->recursive = 0;
447
		$result = $TestModel->find('all');
448
		$this->assertEquals($expected, $result);
449
 
450
		foreach ($expected as $key => $value) {
451
			unset($value['Comment'], $value['Tag']);
452
			$expected[$key] = $value;
453
		}
454
 
455
		$TestModel->recursive = 0;
456
		$result = $TestModel->find('all');
457
		$this->assertEquals($expected, $result);
458
 
459
		$result = Hash::extract($TestModel->User->find('all'), '{n}.User.id');
460
		$this->assertEquals(array('1', '2', '3', '4'), $result);
461
		$this->assertEquals($expected, $TestModel->find('all'));
462
 
463
		$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')));
464
		$expected = array(
465
			array(
466
				'Comment' => array(
467
					'id' => '1',
468
					'article_id' => '1',
469
					'user_id' => '2',
470
					'comment' => 'First Comment for First Article',
471
					'published' => 'Y',
472
					'created' => '2007-03-18 10:45:23',
473
					'updated' => '2007-03-18 10:47:31'
474
				),
475
				'User' => array(
476
					'id' => '2',
477
					'user' => 'nate',
478
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
479
					'created' => '2007-03-17 01:18:23',
480
					'updated' => '2007-03-17 01:20:31'
481
				),
482
				'Article' => array(
483
					'id' => '1',
484
					'user_id' => '1',
485
					'title' => 'First Article',
486
					'body' => 'First Article Body',
487
					'published' => 'Y',
488
					'created' => '2007-03-18 10:39:23',
489
					'updated' => '2007-03-18 10:41:31'
490
			)),
491
			array(
492
				'Comment' => array(
493
					'id' => '2',
494
					'article_id' => '1',
495
					'user_id' => '4',
496
					'comment' => 'Second Comment for First Article',
497
					'published' => 'Y',
498
					'created' => '2007-03-18 10:47:23',
499
					'updated' => '2007-03-18 10:49:31'
500
				),
501
				'User' => array(
502
					'id' => '4',
503
					'user' => 'garrett',
504
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
505
					'created' => '2007-03-17 01:22:23',
506
					'updated' => '2007-03-17 01:24:31'
507
				),
508
				'Article' => array(
509
					'id' => '1',
510
					'user_id' => '1',
511
					'title' => 'First Article',
512
					'body' => 'First Article Body',
513
					'published' => 'Y',
514
					'created' => '2007-03-18 10:39:23',
515
					'updated' => '2007-03-18 10:41:31'
516
			)),
517
			array(
518
				'Comment' => array(
519
					'id' => '3',
520
					'article_id' => '1',
521
					'user_id' => '1',
522
					'comment' => 'Third Comment for First Article',
523
					'published' => 'Y',
524
					'created' => '2007-03-18 10:49:23',
525
					'updated' => '2007-03-18 10:51:31'
526
				),
527
				'User' => array(
528
					'id' => '1',
529
					'user' => 'mariano',
530
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
531
					'created' => '2007-03-17 01:16:23',
532
					'updated' => '2007-03-17 01:18:31'
533
				),
534
				'Article' => array(
535
					'id' => '1',
536
					'user_id' => '1',
537
					'title' => 'First Article',
538
					'body' => 'First Article Body',
539
					'published' => 'Y',
540
					'created' => '2007-03-18 10:39:23',
541
					'updated' => '2007-03-18 10:41:31'
542
			)),
543
			array(
544
				'Comment' => array(
545
					'id' => '4',
546
					'article_id' => '1',
547
					'user_id' => '1',
548
					'comment' => 'Fourth Comment for First Article',
549
					'published' => 'N',
550
					'created' => '2007-03-18 10:51:23',
551
					'updated' => '2007-03-18 10:53:31'
552
				),
553
				'User' => array(
554
					'id' => '1',
555
					'user' => 'mariano',
556
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
557
					'created' => '2007-03-17 01:16:23',
558
					'updated' => '2007-03-17 01:18:31'
559
				),
560
				'Article' => array(
561
					'id' => '1',
562
					'user_id' => '1',
563
					'title' => 'First Article',
564
					'body' => 'First Article Body',
565
					'published' => 'Y',
566
					'created' => '2007-03-18 10:39:23',
567
					'updated' => '2007-03-18 10:41:31'
568
			)),
569
			array(
570
				'Comment' => array(
571
					'id' => '5',
572
					'article_id' => '2',
573
					'user_id' => '1',
574
					'comment' => 'First Comment for Second Article',
575
					'published' => 'Y',
576
					'created' => '2007-03-18 10:53:23',
577
					'updated' => '2007-03-18 10:55:31'
578
				),
579
				'User' => array(
580
					'id' => '1',
581
					'user' => 'mariano',
582
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
583
					'created' => '2007-03-17 01:16:23',
584
					'updated' => '2007-03-17 01:18:31'
585
				),
586
				'Article' => array(
587
					'id' => '2',
588
					'user_id' => '3',
589
					'title' => 'Second Article',
590
					'body' => 'Second Article Body',
591
					'published' => 'Y',
592
					'created' => '2007-03-18 10:41:23',
593
					'updated' => '2007-03-18 10:43:31'
594
			)),
595
			array(
596
				'Comment' => array(
597
					'id' => '6',
598
					'article_id' => '2',
599
					'user_id' => '2',
600
					'comment' => 'Second Comment for Second Article',
601
					'published' => 'Y',
602
					'created' => '2007-03-18 10:55:23',
603
					'updated' => '2007-03-18 10:57:31'
604
				),
605
				'User' => array(
606
					'id' => '2',
607
					'user' => 'nate',
608
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
609
					'created' => '2007-03-17 01:18:23',
610
					'updated' => '2007-03-17 01:20:31'
611
				),
612
				'Article' => array(
613
					'id' => '2',
614
					'user_id' => '3',
615
					'title' => 'Second Article',
616
					'body' => 'Second Article Body',
617
					'published' => 'Y',
618
					'created' => '2007-03-18 10:41:23',
619
					'updated' => '2007-03-18 10:43:31'
620
		)));
621
		$this->assertEquals($expected, $TestModel->Comment->find('all'));
622
	}
623
 
624
/**
625
 * test HABM operations without clobbering existing records #275
626
 *
627
 * @return void
628
 */
629
	public function testHABTMKeepExisting() {
630
		$this->loadFixtures('Site', 'Domain', 'DomainsSite');
631
 
632
		$Site = new Site();
633
		$results = $Site->find('count');
634
		$expected = 3;
635
		$this->assertEquals($expected, $results);
636
 
637
		$data = $Site->findById(1);
638
 
639
		// include api.cakephp.org
640
		$data['Domain'] = array('Domain' => array(1, 2, 3));
641
		$Site->save($data);
642
 
643
		$Site->id = 1;
644
		$results = $Site->read();
645
		$expected = 3; // 3 domains belonging to cakephp
646
		$this->assertEquals($expected, count($results['Domain']));
647
 
648
		$Site->id = 2;
649
		$results = $Site->read();
650
		$expected = 2; // 2 domains belonging to markstory
651
		$this->assertEquals($expected, count($results['Domain']));
652
 
653
		$Site->id = 3;
654
		$results = $Site->read();
655
		$expected = 2;
656
		$this->assertEquals($expected, count($results['Domain']));
657
		$results['Domain'] = array('Domain' => array(7));
658
		$Site->save($results); // remove association from domain 6
659
		$results = $Site->read();
660
		$expected = 1; // only 1 domain left belonging to rchavik
661
		$this->assertEquals($expected, count($results['Domain']));
662
 
663
		// add deleted domain back
664
		$results['Domain'] = array('Domain' => array(6, 7));
665
		$Site->save($results);
666
		$results = $Site->read();
667
		$expected = 2; // 2 domains belonging to rchavik
668
		$this->assertEquals($expected, count($results['Domain']));
669
 
670
		$Site->DomainsSite->id = $results['Domain'][0]['DomainsSite']['id'];
671
		$Site->DomainsSite->saveField('active', true);
672
 
673
		$results = $Site->Domain->DomainsSite->find('count', array(
674
			'conditions' => array(
675
				'DomainsSite.active' => true,
676
			),
677
		));
678
		$expected = 5;
679
		$this->assertEquals($expected, $results);
680
 
681
		// activate api.cakephp.org
682
		$activated = $Site->DomainsSite->findByDomainId(3);
683
		$activated['DomainsSite']['active'] = true;
684
		$Site->DomainsSite->save($activated);
685
 
686
		$results = $Site->DomainsSite->find('count', array(
687
			'conditions' => array(
688
				'DomainsSite.active' => true,
689
			),
690
		));
691
		$expected = 6;
692
		$this->assertEquals($expected, $results);
693
 
694
		// remove 2 previously active domains, and leave $activated alone
695
		$data = array(
696
			'Site' => array('id' => 1, 'name' => 'cakephp (modified)'),
697
			'Domain' => array(
698
				'Domain' => array(3),
699
			)
700
		);
701
		$Site->create($data);
702
		$Site->save($data);
703
 
704
		// tests that record is still identical prior to removal
705
		$Site->id = 1;
706
		$results = $Site->read();
707
		unset($results['Domain'][0]['DomainsSite']['updated']);
708
		unset($activated['DomainsSite']['updated']);
709
		$this->assertEquals($activated['DomainsSite'], $results['Domain'][0]['DomainsSite']);
710
	}
711
 
712
/**
713
 * testHABTMKeepExistingAlternateDataFormat
714
 *
715
 * @return void
716
 */
717
	public function testHABTMKeepExistingAlternateDataFormat() {
718
		$this->loadFixtures('Site', 'Domain', 'DomainsSite');
719
 
720
		$Site = new Site();
721
 
722
		$expected = array(
723
			array(
724
				'DomainsSite' => array(
725
					'id' => 1,
726
					'site_id' => 1,
727
					'domain_id' => 1,
728
					'active' => true,
729
					'created' => '2007-03-17 01:16:23'
730
				)
731
			),
732
			array(
733
				'DomainsSite' => array(
734
					'id' => 2,
735
					'site_id' => 1,
736
					'domain_id' => 2,
737
					'active' => true,
738
					'created' => '2007-03-17 01:16:23'
739
				)
740
			)
741
		);
742
		$result = $Site->DomainsSite->find('all', array(
743
			'conditions' => array('DomainsSite.site_id' => 1),
744
			'fields' => array(
745
				'DomainsSite.id',
746
				'DomainsSite.site_id',
747
				'DomainsSite.domain_id',
748
				'DomainsSite.active',
749
				'DomainsSite.created'
750
			),
751
			'order' => 'DomainsSite.id'
752
		));
753
		$this->assertEquals($expected, $result);
754
 
755
		$time = date('Y-m-d H:i:s');
756
		$data = array(
757
			'Site' => array(
758
				'id' => 1
759
			),
760
			'Domain' => array(
761
				array(
762
					'site_id' => 1,
763
					'domain_id'	=> 3,
764
					'created' => $time,
765
				),
766
				array(
767
					'id' => 2,
768
					'site_id' => 1,
769
					'domain_id'	=> 2
770
				),
771
			)
772
		);
773
		$Site->save($data);
774
		$expected = array(
775
			array(
776
				'DomainsSite' => array(
777
					'id' => 2,
778
					'site_id' => 1,
779
					'domain_id' => 2,
780
					'active' => true,
781
					'created' => '2007-03-17 01:16:23'
782
				)
783
			),
784
			array(
785
				'DomainsSite' => array(
786
					'id' => 7,
787
					'site_id' => 1,
788
					'domain_id' => 3,
789
					'active' => false,
790
					'created' => $time
791
				)
792
			)
793
		);
794
		$result = $Site->DomainsSite->find('all', array(
795
			'conditions' => array('DomainsSite.site_id' => 1),
796
			'fields' => array(
797
				'DomainsSite.id',
798
				'DomainsSite.site_id',
799
				'DomainsSite.domain_id',
800
				'DomainsSite.active',
801
				'DomainsSite.created'
802
			),
803
			'order' => 'DomainsSite.id'
804
		));
805
		$this->assertEquals($expected, $result);
806
	}
807
 
808
/**
809
 * test HABM operations without clobbering existing records #275
810
 *
811
 * @return void
812
 */
813
	public function testHABTMKeepExistingWithThreeDbs() {
814
		$config = ConnectionManager::enumConnectionObjects();
815
		$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
816
		$this->skipIf(
817
			!isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
818
			'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.'
819
		);
820
 
821
		$this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
822
		$Player = ClassRegistry::init('Player');
823
		$Player->bindModel(array(
824
			'hasAndBelongsToMany' => array(
825
				'Armor' => array(
826
					'with' => 'ArmorsPlayer',
827
					'unique' => 'keepExisting',
828
				),
829
			),
830
		), false);
831
		$this->assertEquals('test', $Player->useDbConfig);
832
		$this->assertEquals('test', $Player->Guild->useDbConfig);
833
		$this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
834
		$this->assertEquals('test2', $Player->Armor->useDbConfig);
835
		$this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig);
836
 
837
		$players = $Player->find('all');
838
		$this->assertEquals(4, count($players));
839
		$playersGuilds = Hash::extract($players, '{n}.Guild.{n}.GuildsPlayer');
840
		$this->assertEquals(3, count($playersGuilds));
841
		$playersArmors = Hash::extract($players, '{n}.Armor.{n}.ArmorsPlayer');
842
		$this->assertEquals(3, count($playersArmors));
843
		unset($players);
844
 
845
		$larry = $Player->findByName('larry');
846
		$larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
847
		$this->assertEquals(1, count($larrysArmor));
848
 
849
		$larry['Guild']['Guild'] = array(1, 3); // larry joins another guild
850
		$larry['Armor']['Armor'] = array(2, 3); // purchases chainmail
851
		$Player->save($larry);
852
		unset($larry);
853
 
854
		$larry = $Player->findByName('larry');
855
		$larrysGuild = Hash::extract($larry, 'Guild.{n}.GuildsPlayer');
856
		$this->assertEquals(2, count($larrysGuild));
857
		$larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
858
		$this->assertEquals(2, count($larrysArmor));
859
 
860
		$Player->ArmorsPlayer->id = 3;
861
		$Player->ArmorsPlayer->saveField('broken', true); // larry's cloak broke
862
 
863
		$larry = $Player->findByName('larry');
864
		$larrysCloak = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer[armor_id=3]', $larry);
865
		$this->assertNotEmpty($larrysCloak);
866
		$this->assertTrue($larrysCloak[0]['broken']); // still broken
867
	}
868
 
869
/**
870
 * testDisplayField method
871
 *
872
 * @return void
873
 */
874
	public function testDisplayField() {
875
		$this->loadFixtures('Post', 'Comment', 'Person', 'User');
876
		$Post = new Post();
877
		$Comment = new Comment();
878
		$Person = new Person();
879
 
880
		$this->assertEquals('title', $Post->displayField);
881
		$this->assertEquals('name', $Person->displayField);
882
		$this->assertEquals('id', $Comment->displayField);
883
	}
884
 
885
/**
886
 * testSchema method
887
 *
888
 * @return void
889
 */
890
	public function testSchema() {
891
		$Post = new Post();
892
 
893
		$result = $Post->schema();
894
		$columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
895
		$this->assertEquals($columns, array_keys($result));
896
 
897
		$types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
898
		$this->assertEquals(Hash::extract(array_values($result), '{n}.type'), $types);
899
 
900
		$result = $Post->schema('body');
901
		$this->assertEquals('text', $result['type']);
902
		$this->assertNull($Post->schema('foo'));
903
 
904
		$this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types));
905
	}
906
 
907
/**
908
 * Check schema() on a model with useTable = false;
909
 *
910
 * @return void
911
 */
912
	public function testSchemaUseTableFalse() {
913
		$model = new TheVoid();
914
		$result = $model->schema();
915
		$this->assertNull($result);
916
 
917
		$result = $model->create();
918
		$this->assertEmpty($result);
919
	}
920
 
921
/**
922
 * data provider for time tests.
923
 *
924
 * @return array
925
 */
926
	public static function timeProvider() {
927
		$db = ConnectionManager::getDataSource('test');
928
		$now = $db->expression('NOW()');
929
		return array(
930
			// blank
931
			array(
932
				array('hour' => '', 'min' => '', 'meridian' => ''),
933
				''
934
			),
935
			// missing hour
936
			array(
937
				array('hour' => '', 'min' => '00', 'meridian' => 'pm'),
938
				''
939
			),
940
			// all blank
941
			array(
942
				array('hour' => '', 'min' => '', 'sec' => ''),
943
				''
944
			),
945
			// set and empty merdian
946
			array(
947
				array('hour' => '1', 'min' => '00', 'meridian' => ''),
948
				''
949
			),
950
			// midnight
951
			array(
952
				array('hour' => '12', 'min' => '0', 'meridian' => 'am'),
953
				'00:00:00'
954
			),
955
			array(
956
				array('hour' => '00', 'min' => '00'),
957
				'00:00:00'
958
			),
959
			// 3am
960
			array(
961
				array('hour' => '03', 'min' => '04', 'sec' => '04'),
962
				'03:04:04'
963
			),
964
			array(
965
				array('hour' => '3', 'min' => '4', 'sec' => '4'),
966
				'03:04:04'
967
			),
968
			array(
969
				array('hour' => '03', 'min' => '4', 'sec' => '4'),
970
				'03:04:04'
971
			),
972
			array(
973
				$now,
974
				$now
975
			)
976
		);
977
	}
978
 
979
/**
980
 * test deconstruct with time fields.
981
 *
982
 * @dataProvider timeProvider
983
 * @return void
984
 */
985
	public function testDeconstructFieldsTime($input, $result) {
986
		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
987
 
988
		$this->loadFixtures('Apple');
989
		$TestModel = new Apple();
990
 
991
		$data = array(
992
			'Apple' => array(
993
				'mytime' => $input
994
			)
995
		);
996
 
997
		$TestModel->data = null;
998
		$TestModel->set($data);
999
		$expected = array('Apple' => array('mytime' => $result));
1000
		$this->assertEquals($expected, $TestModel->data);
1001
	}
1002
 
1003
/**
1004
 * testDeconstructFields with datetime, timestamp, and date fields
1005
 *
1006
 * @return void
1007
 */
1008
	public function testDeconstructFieldsDateTime() {
1009
		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
1010
 
1011
		$this->loadFixtures('Apple');
1012
		$TestModel = new Apple();
1013
 
1014
		//test null/empty values first
1015
		$data['Apple']['created']['year'] = '';
1016
		$data['Apple']['created']['month'] = '';
1017
		$data['Apple']['created']['day'] = '';
1018
		$data['Apple']['created']['hour'] = '';
1019
		$data['Apple']['created']['min'] = '';
1020
		$data['Apple']['created']['sec'] = '';
1021
 
1022
		$TestModel->data = null;
1023
		$TestModel->set($data);
1024
		$expected = array('Apple' => array('created' => ''));
1025
		$this->assertEquals($expected, $TestModel->data);
1026
 
1027
		$data = array();
1028
		$data['Apple']['date']['year'] = '';
1029
		$data['Apple']['date']['month'] = '';
1030
		$data['Apple']['date']['day'] = '';
1031
 
1032
		$TestModel->data = null;
1033
		$TestModel->set($data);
1034
		$expected = array('Apple' => array('date' => ''));
1035
		$this->assertEquals($expected, $TestModel->data);
1036
 
1037
		$data = array();
1038
		$data['Apple']['created']['year'] = '2007';
1039
		$data['Apple']['created']['month'] = '08';
1040
		$data['Apple']['created']['day'] = '20';
1041
		$data['Apple']['created']['hour'] = '';
1042
		$data['Apple']['created']['min'] = '';
1043
		$data['Apple']['created']['sec'] = '';
1044
 
1045
		$TestModel->data = null;
1046
		$TestModel->set($data);
1047
		$expected = array('Apple' => array('created' => '2007-08-20 00:00:00'));
1048
		$this->assertEquals($expected, $TestModel->data);
1049
 
1050
		$data = array();
1051
		$data['Apple']['created']['year'] = '2007';
1052
		$data['Apple']['created']['month'] = '08';
1053
		$data['Apple']['created']['day'] = '20';
1054
		$data['Apple']['created']['hour'] = '10';
1055
		$data['Apple']['created']['min'] = '12';
1056
		$data['Apple']['created']['sec'] = '';
1057
 
1058
		$TestModel->data = null;
1059
		$TestModel->set($data);
1060
		$expected = array('Apple' => array('created' => '2007-08-20 10:12:00'));
1061
		$this->assertEquals($expected, $TestModel->data);
1062
 
1063
		$data = array();
1064
		$data['Apple']['created']['year'] = '2007';
1065
		$data['Apple']['created']['month'] = '';
1066
		$data['Apple']['created']['day'] = '12';
1067
		$data['Apple']['created']['hour'] = '20';
1068
		$data['Apple']['created']['min'] = '';
1069
		$data['Apple']['created']['sec'] = '';
1070
 
1071
		$TestModel->data = null;
1072
		$TestModel->set($data);
1073
		$expected = array('Apple' => array('created' => ''));
1074
		$this->assertEquals($expected, $TestModel->data);
1075
 
1076
		$data = array();
1077
		$data['Apple']['created']['hour'] = '20';
1078
		$data['Apple']['created']['min'] = '33';
1079
 
1080
		$TestModel->data = null;
1081
		$TestModel->set($data);
1082
		$expected = array('Apple' => array('created' => ''));
1083
		$this->assertEquals($expected, $TestModel->data);
1084
 
1085
		$data = array();
1086
		$data['Apple']['created']['hour'] = '20';
1087
		$data['Apple']['created']['min'] = '33';
1088
		$data['Apple']['created']['sec'] = '33';
1089
 
1090
		$TestModel->data = null;
1091
		$TestModel->set($data);
1092
		$expected = array('Apple' => array('created' => ''));
1093
		$this->assertEquals($expected, $TestModel->data);
1094
 
1095
		$data = array();
1096
		$data['Apple']['created']['hour'] = '13';
1097
		$data['Apple']['created']['min'] = '00';
1098
		$data['Apple']['date']['year'] = '2006';
1099
		$data['Apple']['date']['month'] = '12';
1100
		$data['Apple']['date']['day'] = '25';
1101
 
1102
		$TestModel->data = null;
1103
		$TestModel->set($data);
1104
		$expected = array(
1105
			'Apple' => array(
1106
			'created' => '',
1107
			'date' => '2006-12-25'
1108
		));
1109
		$this->assertEquals($expected, $TestModel->data);
1110
 
1111
		$data = array();
1112
		$data['Apple']['created']['year'] = '2007';
1113
		$data['Apple']['created']['month'] = '08';
1114
		$data['Apple']['created']['day'] = '20';
1115
		$data['Apple']['created']['hour'] = '10';
1116
		$data['Apple']['created']['min'] = '12';
1117
		$data['Apple']['created']['sec'] = '09';
1118
		$data['Apple']['date']['year'] = '2006';
1119
		$data['Apple']['date']['month'] = '12';
1120
		$data['Apple']['date']['day'] = '25';
1121
 
1122
		$TestModel->data = null;
1123
		$TestModel->set($data);
1124
		$expected = array(
1125
			'Apple' => array(
1126
				'created' => '2007-08-20 10:12:09',
1127
				'date' => '2006-12-25'
1128
		));
1129
		$this->assertEquals($expected, $TestModel->data);
1130
 
1131
		$data = array();
1132
		$data['Apple']['created']['year'] = '--';
1133
		$data['Apple']['created']['month'] = '--';
1134
		$data['Apple']['created']['day'] = '--';
1135
		$data['Apple']['created']['hour'] = '--';
1136
		$data['Apple']['created']['min'] = '--';
1137
		$data['Apple']['created']['sec'] = '--';
1138
		$data['Apple']['date']['year'] = '--';
1139
		$data['Apple']['date']['month'] = '--';
1140
		$data['Apple']['date']['day'] = '--';
1141
 
1142
		$TestModel->data = null;
1143
		$TestModel->set($data);
1144
		$expected = array('Apple' => array('created' => '', 'date' => ''));
1145
		$this->assertEquals($expected, $TestModel->data);
1146
 
1147
		$data = array();
1148
		$data['Apple']['created']['year'] = '2007';
1149
		$data['Apple']['created']['month'] = '--';
1150
		$data['Apple']['created']['day'] = '20';
1151
		$data['Apple']['created']['hour'] = '10';
1152
		$data['Apple']['created']['min'] = '12';
1153
		$data['Apple']['created']['sec'] = '09';
1154
		$data['Apple']['date']['year'] = '2006';
1155
		$data['Apple']['date']['month'] = '12';
1156
		$data['Apple']['date']['day'] = '25';
1157
 
1158
		$TestModel->data = null;
1159
		$TestModel->set($data);
1160
		$expected = array('Apple' => array('created' => '', 'date' => '2006-12-25'));
1161
		$this->assertEquals($expected, $TestModel->data);
1162
 
1163
		$data = array();
1164
		$data['Apple']['date']['year'] = '2006';
1165
		$data['Apple']['date']['month'] = '12';
1166
		$data['Apple']['date']['day'] = '25';
1167
 
1168
		$TestModel->data = null;
1169
		$TestModel->set($data);
1170
		$expected = array('Apple' => array('date' => '2006-12-25'));
1171
		$this->assertEquals($expected, $TestModel->data);
1172
 
1173
		$db = ConnectionManager::getDataSource('test');
1174
		$data = array();
1175
		$data['Apple']['modified'] = $db->expression('NOW()');
1176
		$TestModel->data = null;
1177
		$TestModel->set($data);
1178
		$this->assertEquals($TestModel->data, $data);
1179
	}
1180
 
1181
/**
1182
 * testTablePrefixSwitching method
1183
 *
1184
 * @return void
1185
 */
1186
	public function testTablePrefixSwitching() {
1187
		ConnectionManager::create('database1',
1188
				array_merge($this->db->config, array('prefix' => 'aaa_')
1189
		));
1190
		ConnectionManager::create('database2',
1191
			array_merge($this->db->config, array('prefix' => 'bbb_')
1192
		));
1193
 
1194
		$db1 = ConnectionManager::getDataSource('database1');
1195
		$db2 = ConnectionManager::getDataSource('database2');
1196
 
1197
		$TestModel = new Apple();
1198
		$TestModel->setDataSource('database1');
1199
		$this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
1200
		$this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
1201
		$this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
1202
 
1203
		$TestModel->setDataSource('database2');
1204
		$this->assertContains('bbb_apples', $this->db->fullTableName($TestModel));
1205
		$this->assertContains('bbb_apples', $db1->fullTableName($TestModel));
1206
		$this->assertContains('bbb_apples', $db2->fullTableName($TestModel));
1207
 
1208
		$TestModel = new Apple();
1209
		$TestModel->tablePrefix = 'custom_';
1210
		$this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
1211
		$TestModel->setDataSource('database1');
1212
		$this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
1213
		$this->assertContains('custom_apples', $db1->fullTableName($TestModel));
1214
 
1215
		$TestModel = new Apple();
1216
		$TestModel->setDataSource('database1');
1217
		$this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
1218
		$TestModel->tablePrefix = '';
1219
		$TestModel->setDataSource('database2');
1220
		$this->assertContains('apples', $db2->fullTableName($TestModel));
1221
		$this->assertContains('apples', $db1->fullTableName($TestModel));
1222
 
1223
		$TestModel->tablePrefix = null;
1224
		$TestModel->setDataSource('database1');
1225
		$this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
1226
		$this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
1227
 
1228
		$TestModel->tablePrefix = false;
1229
		$TestModel->setDataSource('database2');
1230
		$this->assertContains('apples', $db2->fullTableName($TestModel));
1231
		$this->assertContains('apples', $db1->fullTableName($TestModel));
1232
	}
1233
 
1234
/**
1235
 * Tests validation parameter order in custom validation methods
1236
 *
1237
 * @return void
1238
 */
1239
	public function testInvalidAssociation() {
1240
		$TestModel = new ValidationTest1();
1241
		$this->assertNull($TestModel->getAssociated('Foo'));
1242
	}
1243
 
1244
/**
1245
 * testLoadModelSecondIteration method
1246
 *
1247
 * @return void
1248
 */
1249
	public function testLoadModelSecondIteration() {
1250
		$this->loadFixtures('Apple', 'Message', 'Thread', 'Bid');
1251
		$model = new ModelA();
1252
		$this->assertInstanceOf('ModelA', $model);
1253
 
1254
		$this->assertInstanceOf('ModelB', $model->ModelB);
1255
		$this->assertInstanceOf('ModelD', $model->ModelB->ModelD);
1256
 
1257
		$this->assertInstanceOf('ModelC', $model->ModelC);
1258
		$this->assertInstanceOf('ModelD', $model->ModelC->ModelD);
1259
	}
1260
 
1261
/**
1262
 * ensure that exists() does not persist between method calls reset on create
1263
 *
1264
 * @return void
1265
 */
1266
	public function testResetOfExistsOnCreate() {
1267
		$this->loadFixtures('Article');
1268
		$Article = new Article();
1269
		$Article->id = 1;
1270
		$Article->saveField('title', 'Reset me');
1271
		$Article->delete();
1272
		$Article->id = 1;
1273
		$this->assertFalse($Article->exists());
1274
 
1275
		$Article->create();
1276
		$this->assertFalse($Article->exists());
1277
		$Article->id = 2;
1278
		$Article->saveField('title', 'Staying alive');
1279
		$result = $Article->read(null, 2);
1280
		$this->assertEquals('Staying alive', $result['Article']['title']);
1281
	}
1282
 
1283
/**
1284
 * testUseTableFalseExistsCheck method
1285
 *
1286
 * @return void
1287
 */
1288
	public function testUseTableFalseExistsCheck() {
1289
		$this->loadFixtures('Article');
1290
		$Article = new Article();
1291
		$Article->id = 1337;
1292
		$result = $Article->exists();
1293
		$this->assertFalse($result);
1294
 
1295
		$Article->useTable = false;
1296
		$Article->id = null;
1297
		$result = $Article->exists();
1298
		$this->assertFalse($result);
1299
 
1300
		// An article with primary key of '1' has been loaded by the fixtures.
1301
		$Article->useTable = false;
1302
		$Article->id = 1;
1303
		$result = $Article->exists();
1304
		$this->assertTrue($result);
1305
	}
1306
 
1307
/**
1308
 * testPluginAssociations method
1309
 *
1310
 * @return void
1311
 */
1312
	public function testPluginAssociations() {
1313
		$this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
1314
		$TestModel = new TestPluginArticle();
1315
 
1316
		$result = $TestModel->find('all');
1317
		$expected = array(
1318
			array(
1319
				'TestPluginArticle' => array(
1320
					'id' => 1,
1321
					'user_id' => 1,
1322
					'title' => 'First Plugin Article',
1323
					'body' => 'First Plugin Article Body',
1324
					'published' => 'Y',
1325
					'created' => '2008-09-24 10:39:23',
1326
					'updated' => '2008-09-24 10:41:31'
1327
				),
1328
				'User' => array(
1329
					'id' => 1,
1330
					'user' => 'mariano',
1331
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1332
					'created' => '2007-03-17 01:16:23',
1333
					'updated' => '2007-03-17 01:18:31'
1334
				),
1335
				'TestPluginComment' => array(
1336
					array(
1337
						'id' => 1,
1338
						'article_id' => 1,
1339
						'user_id' => 2,
1340
						'comment' => 'First Comment for First Plugin Article',
1341
						'published' => 'Y',
1342
						'created' => '2008-09-24 10:45:23',
1343
						'updated' => '2008-09-24 10:47:31'
1344
					),
1345
					array(
1346
						'id' => 2,
1347
						'article_id' => 1,
1348
						'user_id' => 4,
1349
						'comment' => 'Second Comment for First Plugin Article',
1350
						'published' => 'Y',
1351
						'created' => '2008-09-24 10:47:23',
1352
						'updated' => '2008-09-24 10:49:31'
1353
					),
1354
					array(
1355
						'id' => 3,
1356
						'article_id' => 1,
1357
						'user_id' => 1,
1358
						'comment' => 'Third Comment for First Plugin Article',
1359
						'published' => 'Y',
1360
						'created' => '2008-09-24 10:49:23',
1361
						'updated' => '2008-09-24 10:51:31'
1362
					),
1363
					array(
1364
						'id' => 4,
1365
						'article_id' => 1,
1366
						'user_id' => 1,
1367
						'comment' => 'Fourth Comment for First Plugin Article',
1368
						'published' => 'N',
1369
						'created' => '2008-09-24 10:51:23',
1370
						'updated' => '2008-09-24 10:53:31'
1371
			))),
1372
			array(
1373
				'TestPluginArticle' => array(
1374
					'id' => 2,
1375
					'user_id' => 3,
1376
					'title' => 'Second Plugin Article',
1377
					'body' => 'Second Plugin Article Body',
1378
					'published' => 'Y',
1379
					'created' => '2008-09-24 10:41:23',
1380
					'updated' => '2008-09-24 10:43:31'
1381
				),
1382
				'User' => array(
1383
					'id' => 3,
1384
					'user' => 'larry',
1385
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1386
					'created' => '2007-03-17 01:20:23',
1387
					'updated' => '2007-03-17 01:22:31'
1388
				),
1389
				'TestPluginComment' => array(
1390
					array(
1391
						'id' => 5,
1392
						'article_id' => 2,
1393
						'user_id' => 1,
1394
						'comment' => 'First Comment for Second Plugin Article',
1395
						'published' => 'Y',
1396
						'created' => '2008-09-24 10:53:23',
1397
						'updated' => '2008-09-24 10:55:31'
1398
					),
1399
					array(
1400
						'id' => 6,
1401
						'article_id' => 2,
1402
						'user_id' => 2,
1403
						'comment' => 'Second Comment for Second Plugin Article',
1404
						'published' => 'Y',
1405
						'created' => '2008-09-24 10:55:23',
1406
						'updated' => '2008-09-24 10:57:31'
1407
			))),
1408
			array(
1409
				'TestPluginArticle' => array(
1410
					'id' => 3,
1411
					'user_id' => 1,
1412
					'title' => 'Third Plugin Article',
1413
					'body' => 'Third Plugin Article Body',
1414
					'published' => 'Y',
1415
					'created' => '2008-09-24 10:43:23',
1416
					'updated' => '2008-09-24 10:45:31'
1417
				),
1418
				'User' => array(
1419
					'id' => 1,
1420
					'user' => 'mariano',
1421
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1422
					'created' => '2007-03-17 01:16:23',
1423
					'updated' => '2007-03-17 01:18:31'
1424
				),
1425
				'TestPluginComment' => array()
1426
		));
1427
 
1428
		$this->assertEquals($expected, $result);
1429
	}
1430
 
1431
/**
1432
 * Tests getAssociated method
1433
 *
1434
 * @return void
1435
 */
1436
	public function testGetAssociated() {
1437
		$this->loadFixtures('Article', 'Tag');
1438
		$Article = ClassRegistry::init('Article');
1439
 
1440
		$assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
1441
		foreach ($assocTypes as $type) {
1442
			$this->assertEquals($Article->getAssociated($type), array_keys($Article->{$type}));
1443
		}
1444
 
1445
		$Article->bindModel(array('hasMany' => array('Category')));
1446
		$this->assertEquals(array('Comment', 'Category'), $Article->getAssociated('hasMany'));
1447
 
1448
		$results = $Article->getAssociated();
1449
		$results = array_keys($results);
1450
		sort($results);
1451
		$this->assertEquals(array('Category', 'Comment', 'Tag', 'User'), $results);
1452
 
1453
		$Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
1454
		$this->assertEquals(array(), $Article->getAssociated('hasAndBelongsToMany'));
1455
 
1456
		$result = $Article->getAssociated('Category');
1457
		$expected = array(
1458
			'className' => 'Category',
1459
			'foreignKey' => 'article_id',
1460
			'conditions' => '',
1461
			'fields' => '',
1462
			'order' => '',
1463
			'limit' => '',
1464
			'offset' => '',
1465
			'dependent' => '',
1466
			'exclusive' => '',
1467
			'finderQuery' => '',
1468
			'counterQuery' => '',
1469
			'association' => 'hasMany',
1470
		);
1471
		$this->assertEquals($expected, $result);
1472
	}
1473
 
1474
/**
1475
 * testAutoConstructAssociations method
1476
 *
1477
 * @return void
1478
 */
1479
	public function testAutoConstructAssociations() {
1480
		$this->loadFixtures('User', 'ArticleFeatured', 'Featured', 'ArticleFeaturedsTags');
1481
		$TestModel = new AssociationTest1();
1482
 
1483
		$result = $TestModel->hasAndBelongsToMany;
1484
		$expected = array('AssociationTest2' => array(
1485
				'unique' => false,
1486
				'joinTable' => 'join_as_join_bs',
1487
				'foreignKey' => false,
1488
				'className' => 'AssociationTest2',
1489
				'with' => 'JoinAsJoinB',
1490
				'dynamicWith' => true,
1491
				'associationForeignKey' => 'join_b_id',
1492
				'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
1493
				'finderQuery' => ''
1494
		));
1495
		$this->assertEquals($expected, $result);
1496
 
1497
		$TestModel = new ArticleFeatured();
1498
		$TestFakeModel = new ArticleFeatured(array('table' => false));
1499
 
1500
		$expected = array(
1501
			'User' => array(
1502
				'className' => 'User', 'foreignKey' => 'user_id',
1503
				'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
1504
			),
1505
			'Category' => array(
1506
				'className' => 'Category', 'foreignKey' => 'category_id',
1507
				'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
1508
			)
1509
		);
1510
		$this->assertSame($TestModel->belongsTo, $expected);
1511
		$this->assertSame($TestFakeModel->belongsTo, $expected);
1512
 
1513
		$this->assertEquals('User', $TestModel->User->name);
1514
		$this->assertEquals('User', $TestFakeModel->User->name);
1515
		$this->assertEquals('Category', $TestModel->Category->name);
1516
		$this->assertEquals('Category', $TestFakeModel->Category->name);
1517
 
1518
		$expected = array(
1519
			'Featured' => array(
1520
				'className' => 'Featured',
1521
				'foreignKey' => 'article_featured_id',
1522
				'conditions' => '',
1523
				'fields' => '',
1524
				'order' => '',
1525
				'dependent' => ''
1526
		));
1527
 
1528
		$this->assertSame($TestModel->hasOne, $expected);
1529
		$this->assertSame($TestFakeModel->hasOne, $expected);
1530
 
1531
		$this->assertEquals('Featured', $TestModel->Featured->name);
1532
		$this->assertEquals('Featured', $TestFakeModel->Featured->name);
1533
 
1534
		$expected = array(
1535
			'Comment' => array(
1536
				'className' => 'Comment',
1537
				'dependent' => true,
1538
				'foreignKey' => 'article_featured_id',
1539
				'conditions' => '',
1540
				'fields' => '',
1541
				'order' => '',
1542
				'limit' => '',
1543
				'offset' => '',
1544
				'exclusive' => '',
1545
				'finderQuery' => '',
1546
				'counterQuery' => ''
1547
		));
1548
 
1549
		$this->assertSame($TestModel->hasMany, $expected);
1550
		$this->assertSame($TestFakeModel->hasMany, $expected);
1551
 
1552
		$this->assertEquals('Comment', $TestModel->Comment->name);
1553
		$this->assertEquals('Comment', $TestFakeModel->Comment->name);
1554
 
1555
		$expected = array(
1556
			'Tag' => array(
1557
				'className' => 'Tag',
1558
				'joinTable' => 'article_featureds_tags',
1559
				'with' => 'ArticleFeaturedsTag',
1560
				'dynamicWith' => true,
1561
				'foreignKey' => 'article_featured_id',
1562
				'associationForeignKey' => 'tag_id',
1563
				'conditions' => '',
1564
				'fields' => '',
1565
				'order' => '',
1566
				'limit' => '',
1567
				'offset' => '',
1568
				'unique' => true,
1569
				'finderQuery' => '',
1570
		));
1571
 
1572
		$this->assertSame($TestModel->hasAndBelongsToMany, $expected);
1573
		$this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected);
1574
 
1575
		$this->assertEquals('Tag', $TestModel->Tag->name);
1576
		$this->assertEquals('Tag', $TestFakeModel->Tag->name);
1577
	}
1578
 
1579
/**
1580
 * test creating associations with plugins. Ensure a double alias isn't created
1581
 *
1582
 * @return void
1583
 */
1584
	public function testAutoConstructPluginAssociations() {
1585
		$Comment = ClassRegistry::init('TestPluginComment');
1586
 
1587
		$this->assertEquals(2, count($Comment->belongsTo), 'Too many associations');
1588
		$this->assertFalse(isset($Comment->belongsTo['TestPlugin.User']));
1589
		$this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association');
1590
		$this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association');
1591
	}
1592
 
1593
/**
1594
 * test Model::__construct
1595
 *
1596
 * ensure that $actsAS and $findMethods are merged.
1597
 *
1598
 * @return void
1599
 */
1600
	public function testConstruct() {
1601
		$this->loadFixtures('Post');
1602
 
1603
		$TestModel = ClassRegistry::init('MergeVarPluginPost');
1604
		$this->assertEquals(array('Containable' => null, 'Tree' => null), $TestModel->actsAs);
1605
		$this->assertTrue(isset($TestModel->Behaviors->Containable));
1606
		$this->assertTrue(isset($TestModel->Behaviors->Tree));
1607
 
1608
		$TestModel = ClassRegistry::init('MergeVarPluginComment');
1609
		$expected = array('Containable' => array('some_settings'));
1610
		$this->assertEquals($expected, $TestModel->actsAs);
1611
		$this->assertTrue(isset($TestModel->Behaviors->Containable));
1612
	}
1613
 
1614
/**
1615
 * test Model::__construct
1616
 *
1617
 * ensure that $actsAS and $findMethods are merged.
1618
 *
1619
 * @return void
1620
 */
1621
	public function testConstructWithAlternateDataSource() {
1622
		$TestModel = ClassRegistry::init(array(
1623
			'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false
1624
		));
1625
		$this->assertEquals('test', $TestModel->useDbConfig);
1626
 
1627
		//deprecated but test it anyway
1628
		$NewVoid = new TheVoid(null, false, 'other');
1629
		$this->assertEquals('other', $NewVoid->useDbConfig);
1630
	}
1631
 
1632
/**
1633
 * testColumnTypeFetching method
1634
 *
1635
 * @return void
1636
 */
1637
	public function testColumnTypeFetching() {
1638
		$model = new Test();
1639
		$this->assertEquals('integer', $model->getColumnType('id'));
1640
		$this->assertEquals('text', $model->getColumnType('notes'));
1641
		$this->assertEquals('datetime', $model->getColumnType('updated'));
1642
		$this->assertEquals(null, $model->getColumnType('unknown'));
1643
 
1644
		$model = new Article();
1645
		$this->assertEquals('datetime', $model->getColumnType('User.created'));
1646
		$this->assertEquals('integer', $model->getColumnType('Tag.id'));
1647
		$this->assertEquals('integer', $model->getColumnType('Article.id'));
1648
	}
1649
 
1650
/**
1651
 * testHabtmUniqueKey method
1652
 *
1653
 * @return void
1654
 */
1655
	public function testHabtmUniqueKey() {
1656
		$model = new Item();
1657
		$this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
1658
	}
1659
 
1660
/**
1661
 * testIdentity method
1662
 *
1663
 * @return void
1664
 */
1665
	public function testIdentity() {
1666
		$TestModel = new Test();
1667
		$result = $TestModel->alias;
1668
		$expected = 'Test';
1669
		$this->assertEquals($expected, $result);
1670
 
1671
		$TestModel = new TestAlias();
1672
		$result = $TestModel->alias;
1673
		$expected = 'TestAlias';
1674
		$this->assertEquals($expected, $result);
1675
 
1676
		$TestModel = new Test(array('alias' => 'AnotherTest'));
1677
		$result = $TestModel->alias;
1678
		$expected = 'AnotherTest';
1679
		$this->assertEquals($expected, $result);
1680
 
1681
		$TestModel = ClassRegistry::init('Test');
1682
		$expected = null;
1683
		$this->assertEquals($expected, $TestModel->plugin);
1684
 
1685
		$TestModel = ClassRegistry::init('TestPlugin.TestPluginComment');
1686
		$expected = 'TestPlugin';
1687
		$this->assertEquals($expected, $TestModel->plugin);
1688
	}
1689
 
1690
/**
1691
 * testWithAssociation method
1692
 *
1693
 * @return void
1694
 */
1695
	public function testWithAssociation() {
1696
		$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
1697
		$TestModel = new Something();
1698
		$result = $TestModel->SomethingElse->find('all');
1699
 
1700
		$expected = array(
1701
			array(
1702
				'SomethingElse' => array(
1703
					'id' => '1',
1704
					'title' => 'First Post',
1705
					'body' => 'First Post Body',
1706
					'published' => 'Y',
1707
					'created' => '2007-03-18 10:39:23',
1708
					'updated' => '2007-03-18 10:41:31'
1709
				),
1710
				'Something' => array(
1711
					array(
1712
						'id' => '3',
1713
						'title' => 'Third Post',
1714
						'body' => 'Third Post Body',
1715
						'published' => 'Y',
1716
						'created' => '2007-03-18 10:43:23',
1717
						'updated' => '2007-03-18 10:45:31',
1718
						'JoinThing' => array(
1719
							'id' => '3',
1720
							'something_id' => '3',
1721
							'something_else_id' => '1',
1722
							'doomed' => true,
1723
							'created' => '2007-03-18 10:43:23',
1724
							'updated' => '2007-03-18 10:45:31'
1725
			)))),
1726
			array(
1727
				'SomethingElse' => array(
1728
					'id' => '2',
1729
					'title' => 'Second Post',
1730
					'body' => 'Second Post Body',
1731
					'published' => 'Y',
1732
					'created' => '2007-03-18 10:41:23',
1733
					'updated' => '2007-03-18 10:43:31'
1734
				),
1735
				'Something' => array(
1736
					array(
1737
						'id' => '1',
1738
						'title' => 'First Post',
1739
						'body' => 'First Post Body',
1740
						'published' => 'Y',
1741
						'created' => '2007-03-18 10:39:23',
1742
						'updated' => '2007-03-18 10:41:31',
1743
						'JoinThing' => array(
1744
							'id' => '1',
1745
							'something_id' => '1',
1746
							'something_else_id' => '2',
1747
							'doomed' => true,
1748
							'created' => '2007-03-18 10:39:23',
1749
							'updated' => '2007-03-18 10:41:31'
1750
			)))),
1751
			array(
1752
				'SomethingElse' => array(
1753
					'id' => '3',
1754
					'title' => 'Third Post',
1755
					'body' => 'Third Post Body',
1756
					'published' => 'Y',
1757
					'created' => '2007-03-18 10:43:23',
1758
					'updated' => '2007-03-18 10:45:31'
1759
				),
1760
				'Something' => array(
1761
					array(
1762
						'id' => '2',
1763
						'title' => 'Second Post',
1764
						'body' => 'Second Post Body',
1765
						'published' => 'Y',
1766
						'created' => '2007-03-18 10:41:23',
1767
						'updated' => '2007-03-18 10:43:31',
1768
						'JoinThing' => array(
1769
							'id' => '2',
1770
							'something_id' => '2',
1771
							'something_else_id' => '3',
1772
							'doomed' => false,
1773
							'created' => '2007-03-18 10:41:23',
1774
							'updated' => '2007-03-18 10:43:31'
1775
		)))));
1776
		$this->assertEquals($expected, $result);
1777
 
1778
		$result = $TestModel->find('all');
1779
		$expected = array(
1780
			array(
1781
				'Something' => array(
1782
					'id' => '1',
1783
					'title' => 'First Post',
1784
					'body' => 'First Post Body',
1785
					'published' => 'Y',
1786
					'created' => '2007-03-18 10:39:23',
1787
					'updated' => '2007-03-18 10:41:31'
1788
				),
1789
				'SomethingElse' => array(
1790
					array(
1791
						'id' => '2',
1792
						'title' => 'Second Post',
1793
						'body' => 'Second Post Body',
1794
						'published' => 'Y',
1795
						'created' => '2007-03-18 10:41:23',
1796
						'updated' => '2007-03-18 10:43:31',
1797
						'JoinThing' => array(
1798
							'doomed' => true,
1799
							'something_id' => '1',
1800
							'something_else_id' => '2'
1801
			)))),
1802
			array(
1803
				'Something' => array(
1804
					'id' => '2',
1805
					'title' => 'Second Post',
1806
					'body' => 'Second Post Body',
1807
					'published' => 'Y',
1808
					'created' => '2007-03-18 10:41:23',
1809
					'updated' => '2007-03-18 10:43:31'
1810
				),
1811
				'SomethingElse' => array(
1812
					array(
1813
						'id' => '3',
1814
						'title' => 'Third Post',
1815
						'body' => 'Third Post Body',
1816
						'published' => 'Y',
1817
						'created' => '2007-03-18 10:43:23',
1818
						'updated' => '2007-03-18 10:45:31',
1819
						'JoinThing' => array(
1820
							'doomed' => false,
1821
							'something_id' => '2',
1822
							'something_else_id' => '3'
1823
			)))),
1824
			array(
1825
				'Something' => array(
1826
					'id' => '3',
1827
					'title' => 'Third Post',
1828
					'body' => 'Third Post Body',
1829
					'published' => 'Y',
1830
					'created' => '2007-03-18 10:43:23',
1831
					'updated' => '2007-03-18 10:45:31'
1832
				),
1833
				'SomethingElse' => array(
1834
					array(
1835
						'id' => '1',
1836
						'title' => 'First Post',
1837
						'body' => 'First Post Body',
1838
						'published' => 'Y',
1839
						'created' => '2007-03-18 10:39:23',
1840
						'updated' => '2007-03-18 10:41:31',
1841
						'JoinThing' => array(
1842
							'doomed' => true,
1843
							'something_id' => '3',
1844
							'something_else_id' => '1'
1845
		)))));
1846
		$this->assertEquals($expected, $result);
1847
 
1848
		$result = $TestModel->findById(1);
1849
		$expected = array(
1850
			'Something' => array(
1851
				'id' => '1',
1852
				'title' => 'First Post',
1853
				'body' => 'First Post Body',
1854
				'published' => 'Y',
1855
				'created' => '2007-03-18 10:39:23',
1856
				'updated' => '2007-03-18 10:41:31'
1857
			),
1858
			'SomethingElse' => array(
1859
				array(
1860
					'id' => '2',
1861
					'title' => 'Second Post',
1862
					'body' => 'Second Post Body',
1863
					'published' => 'Y',
1864
					'created' => '2007-03-18 10:41:23',
1865
					'updated' => '2007-03-18 10:43:31',
1866
					'JoinThing' => array(
1867
						'doomed' => true,
1868
						'something_id' => '1',
1869
						'something_else_id' => '2'
1870
		))));
1871
		$this->assertEquals($expected, $result);
1872
 
1873
		$expected = $TestModel->findById(1);
1874
		$TestModel->set($expected);
1875
		$TestModel->save();
1876
		$result = $TestModel->findById(1);
1877
		$this->assertEquals($expected, $result);
1878
 
1879
		$TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
1880
		$TestModel->create(array(
1881
			'Something' => array('id' => 1),
1882
			'SomethingElse' => array(3, array(
1883
				'something_else_id' => 1,
1884
				'doomed' => true
1885
		))));
1886
 
1887
		$TestModel->save();
1888
 
1889
		$TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
1890
		$result = $TestModel->findById(1);
1891
		$expected = array(
1892
			'Something' => array(
1893
				'id' => '1',
1894
				'title' => 'First Post',
1895
				'body' => 'First Post Body',
1896
				'published' => 'Y',
1897
				'created' => '2007-03-18 10:39:23'
1898
			),
1899
			'SomethingElse' => array(
1900
				array(
1901
					'id' => '1',
1902
					'title' => 'First Post',
1903
					'body' => 'First Post Body',
1904
					'published' => 'Y',
1905
					'created' => '2007-03-18 10:39:23',
1906
					'updated' => '2007-03-18 10:41:31',
1907
					'JoinThing' => array(
1908
						'doomed' => true,
1909
						'something_id' => '1',
1910
						'something_else_id' => '1'
1911
				)
1912
			),
1913
				array(
1914
					'id' => '2',
1915
					'title' => 'Second Post',
1916
					'body' => 'Second Post Body',
1917
					'published' => 'Y',
1918
					'created' => '2007-03-18 10:41:23',
1919
					'updated' => '2007-03-18 10:43:31',
1920
					'JoinThing' => array(
1921
						'doomed' => true,
1922
						'something_id' => '1',
1923
						'something_else_id' => '2'
1924
				)
1925
			),
1926
				array(
1927
					'id' => '3',
1928
					'title' => 'Third Post',
1929
					'body' => 'Third Post Body',
1930
					'published' => 'Y',
1931
					'created' => '2007-03-18 10:43:23',
1932
					'updated' => '2007-03-18 10:45:31',
1933
					'JoinThing' => array(
1934
						'doomed' => false,
1935
						'something_id' => '1',
1936
						'something_else_id' => '3')
1937
					)
1938
				)
1939
			);
1940
		$this->assertEquals(self::date(), $result['Something']['updated']);
1941
		unset($result['Something']['updated']);
1942
		$this->assertEquals($expected, $result);
1943
	}
1944
 
1945
/**
1946
 * testFindSelfAssociations method
1947
 *
1948
 * @return void
1949
 */
1950
	public function testFindSelfAssociations() {
1951
		$this->loadFixtures('Person');
1952
 
1953
		$TestModel = new Person();
1954
		$TestModel->recursive = 2;
1955
		$result = $TestModel->read(null, 1);
1956
		$expected = array(
1957
			'Person' => array(
1958
				'id' => 1,
1959
				'name' => 'person',
1960
				'mother_id' => 2,
1961
				'father_id' => 3
1962
			),
1963
			'Mother' => array(
1964
				'id' => 2,
1965
				'name' => 'mother',
1966
				'mother_id' => 4,
1967
				'father_id' => 5,
1968
				'Mother' => array(
1969
					'id' => 4,
1970
					'name' => 'mother - grand mother',
1971
					'mother_id' => 0,
1972
					'father_id' => 0
1973
				),
1974
				'Father' => array(
1975
					'id' => 5,
1976
					'name' => 'mother - grand father',
1977
					'mother_id' => 0,
1978
					'father_id' => 0
1979
			)),
1980
			'Father' => array(
1981
				'id' => 3,
1982
				'name' => 'father',
1983
				'mother_id' => 6,
1984
				'father_id' => 7,
1985
				'Father' => array(
1986
					'id' => 7,
1987
					'name' => 'father - grand father',
1988
					'mother_id' => 0,
1989
					'father_id' => 0
1990
				),
1991
				'Mother' => array(
1992
					'id' => 6,
1993
					'name' => 'father - grand mother',
1994
					'mother_id' => 0,
1995
					'father_id' => 0
1996
		)));
1997
 
1998
		$this->assertEquals($expected, $result);
1999
 
2000
		$TestModel->recursive = 3;
2001
		$result = $TestModel->read(null, 1);
2002
		$expected = array(
2003
			'Person' => array(
2004
				'id' => 1,
2005
				'name' => 'person',
2006
				'mother_id' => 2,
2007
				'father_id' => 3
2008
			),
2009
			'Mother' => array(
2010
				'id' => 2,
2011
				'name' => 'mother',
2012
				'mother_id' => 4,
2013
				'father_id' => 5,
2014
				'Mother' => array(
2015
					'id' => 4,
2016
					'name' => 'mother - grand mother',
2017
					'mother_id' => 0,
2018
					'father_id' => 0,
2019
					'Mother' => array(),
2020
					'Father' => array()),
2021
				'Father' => array(
2022
					'id' => 5,
2023
					'name' => 'mother - grand father',
2024
					'mother_id' => 0,
2025
					'father_id' => 0,
2026
					'Father' => array(),
2027
					'Mother' => array()
2028
			)),
2029
			'Father' => array(
2030
				'id' => 3,
2031
				'name' => 'father',
2032
				'mother_id' => 6,
2033
				'father_id' => 7,
2034
				'Father' => array(
2035
					'id' => 7,
2036
					'name' => 'father - grand father',
2037
					'mother_id' => 0,
2038
					'father_id' => 0,
2039
					'Father' => array(),
2040
					'Mother' => array()
2041
				),
2042
				'Mother' => array(
2043
					'id' => 6,
2044
					'name' => 'father - grand mother',
2045
					'mother_id' => 0,
2046
					'father_id' => 0,
2047
					'Mother' => array(),
2048
					'Father' => array()
2049
		)));
2050
 
2051
		$this->assertEquals($expected, $result);
2052
	}
2053
 
2054
/**
2055
 * testDynamicAssociations method
2056
 *
2057
 * @return void
2058
 */
2059
	public function testDynamicAssociations() {
2060
		$this->loadFixtures('Article', 'Comment');
2061
		$TestModel = new Article();
2062
 
2063
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
2064
		$TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
2065
			'foreignKey' => false,
2066
			'conditions' => array('Comment.user_id =' => '2')
2067
		));
2068
		$result = $TestModel->find('all');
2069
		$expected = array(
2070
			array(
2071
				'Article' => array(
2072
					'id' => '1',
2073
					'user_id' => '1',
2074
					'title' => 'First Article',
2075
					'body' => 'First Article Body',
2076
					'published' => 'Y',
2077
					'created' => '2007-03-18 10:39:23',
2078
					'updated' => '2007-03-18 10:41:31'
2079
				),
2080
				'Comment' => array(
2081
					array(
2082
						'id' => '1',
2083
						'article_id' => '1',
2084
						'user_id' => '2',
2085
						'comment' => 'First Comment for First Article',
2086
						'published' => 'Y',
2087
						'created' => '2007-03-18 10:45:23',
2088
						'updated' => '2007-03-18 10:47:31'
2089
					),
2090
					array(
2091
						'id' => '6',
2092
						'article_id' => '2',
2093
						'user_id' => '2',
2094
						'comment' => 'Second Comment for Second Article',
2095
						'published' => 'Y',
2096
						'created' => '2007-03-18 10:55:23',
2097
						'updated' => '2007-03-18 10:57:31'
2098
			))),
2099
			array(
2100
				'Article' => array(
2101
					'id' => '2',
2102
					'user_id' => '3',
2103
					'title' => 'Second Article',
2104
					'body' => 'Second Article Body',
2105
					'published' => 'Y',
2106
					'created' => '2007-03-18 10:41:23',
2107
					'updated' => '2007-03-18 10:43:31'
2108
				),
2109
				'Comment' => array(
2110
					array(
2111
						'id' => '1',
2112
						'article_id' => '1',
2113
						'user_id' => '2',
2114
						'comment' => 'First Comment for First Article',
2115
						'published' => 'Y',
2116
						'created' => '2007-03-18 10:45:23',
2117
						'updated' => '2007-03-18 10:47:31'
2118
					),
2119
					array(
2120
						'id' => '6',
2121
						'article_id' => '2',
2122
						'user_id' => '2',
2123
						'comment' => 'Second Comment for Second Article',
2124
						'published' => 'Y',
2125
						'created' => '2007-03-18 10:55:23',
2126
						'updated' => '2007-03-18 10:57:31'
2127
			))),
2128
			array(
2129
				'Article' => array(
2130
					'id' => '3',
2131
					'user_id' => '1',
2132
					'title' => 'Third Article',
2133
					'body' => 'Third Article Body',
2134
					'published' => 'Y',
2135
					'created' => '2007-03-18 10:43:23',
2136
					'updated' => '2007-03-18 10:45:31'
2137
				),
2138
				'Comment' => array(
2139
					array(
2140
						'id' => '1',
2141
						'article_id' => '1',
2142
						'user_id' => '2',
2143
						'comment' => 'First Comment for First Article',
2144
						'published' => 'Y',
2145
						'created' => '2007-03-18 10:45:23',
2146
						'updated' => '2007-03-18 10:47:31'
2147
					),
2148
					array(
2149
						'id' => '6',
2150
						'article_id' => '2',
2151
						'user_id' => '2',
2152
						'comment' => 'Second Comment for Second Article',
2153
						'published' => 'Y',
2154
						'created' => '2007-03-18 10:55:23',
2155
						'updated' => '2007-03-18 10:57:31'
2156
		))));
2157
 
2158
		$this->assertEquals($expected, $result);
2159
	}
2160
 
2161
/**
2162
 * testCreation method
2163
 *
2164
 * @return void
2165
 */
2166
	public function testCreation() {
2167
		$this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured');
2168
		$TestModel = new Test();
2169
		$result = $TestModel->create();
2170
		$expected = array('Test' => array('notes' => 'write some notes here'));
2171
		$this->assertEquals($expected, $result);
2172
		$TestModel = new User();
2173
		$result = $TestModel->schema();
2174
 
2175
		if (isset($this->db->columns['primary_key']['length'])) {
2176
			$intLength = $this->db->columns['primary_key']['length'];
2177
		} elseif (isset($this->db->columns['integer']['length'])) {
2178
			$intLength = $this->db->columns['integer']['length'];
2179
		} else {
2180
			$intLength = 11;
2181
		}
2182
		foreach (array('collate', 'charset', 'comment') as $type) {
2183
			foreach ($result as $i => $r) {
2184
				unset($result[$i][$type]);
2185
			}
2186
		}
2187
 
2188
		$expected = array(
2189
			'id' => array(
2190
				'type' => 'integer',
2191
				'null' => false,
2192
				'default' => null,
2193
				'length' => $intLength,
2194
				'key' => 'primary'
2195
			),
2196
			'user' => array(
2197
				'type' => 'string',
2198
				'null' => true,
2199
				'default' => '',
2200
				'length' => 255
2201
			),
2202
			'password' => array(
2203
				'type' => 'string',
2204
				'null' => true,
2205
				'default' => '',
2206
				'length' => 255
2207
			),
2208
			'created' => array(
2209
				'type' => 'datetime',
2210
				'null' => true,
2211
				'default' => null,
2212
				'length' => null
2213
			),
2214
			'updated' => array(
2215
				'type' => 'datetime',
2216
				'null' => true,
2217
				'default' => null,
2218
				'length' => null
2219
		));
2220
 
2221
		$this->assertEquals($expected, $result);
2222
 
2223
		$TestModel = new Article();
2224
		$result = $TestModel->create();
2225
		$expected = array('Article' => array('published' => 'N'));
2226
		$this->assertEquals($expected, $result);
2227
 
2228
		$FeaturedModel = new Featured();
2229
		$data = array(
2230
			'article_featured_id' => 1,
2231
			'category_id' => 1,
2232
			'published_date' => array(
2233
				'year' => 2008,
2234
				'month' => 06,
2235
				'day' => 11
2236
			),
2237
			'end_date' => array(
2238
				'year' => 2008,
2239
				'month' => 06,
2240
				'day' => 20
2241
		));
2242
 
2243
		$expected = array(
2244
			'Featured' => array(
2245
				'article_featured_id' => 1,
2246
				'category_id' => 1,
2247
				'published_date' => '2008-06-11 00:00:00',
2248
				'end_date' => '2008-06-20 00:00:00'
2249
		));
2250
 
2251
		$this->assertEquals($expected, $FeaturedModel->create($data));
2252
 
2253
		$data = array(
2254
			'published_date' => array(
2255
				'year' => 2008,
2256
				'month' => 06,
2257
				'day' => 11
2258
			),
2259
			'end_date' => array(
2260
				'year' => 2008,
2261
				'month' => 06,
2262
				'day' => 20
2263
			),
2264
			'article_featured_id' => 1,
2265
			'category_id' => 1
2266
		);
2267
 
2268
		$expected = array(
2269
			'Featured' => array(
2270
				'published_date' => '2008-06-11 00:00:00',
2271
				'end_date' => '2008-06-20 00:00:00',
2272
				'article_featured_id' => 1,
2273
				'category_id' => 1
2274
		));
2275
 
2276
		$this->assertEquals($expected, $FeaturedModel->create($data));
2277
	}
2278
 
2279
/**
2280
 * testEscapeField to prove it escapes the field well even when it has part of the alias on it
2281
 *
2282
 * @return void
2283
 */
2284
	public function testEscapeField() {
2285
		$TestModel = new Test();
2286
		$db = $TestModel->getDataSource();
2287
 
2288
		$result = $TestModel->escapeField('test_field');
2289
		$expected = $db->name('Test.test_field');
2290
		$this->assertEquals($expected, $result);
2291
 
2292
		$result = $TestModel->escapeField('TestField');
2293
		$expected = $db->name('Test.TestField');
2294
		$this->assertEquals($expected, $result);
2295
 
2296
		$result = $TestModel->escapeField('DomainHandle', 'Domain');
2297
		$expected = $db->name('Domain.DomainHandle');
2298
		$this->assertEquals($expected, $result);
2299
 
2300
		ConnectionManager::create('mock', array('datasource' => 'DboMock'));
2301
		$TestModel->setDataSource('mock');
2302
		$db = $TestModel->getDataSource();
2303
 
2304
		$result = $TestModel->escapeField('DomainHandle', 'Domain');
2305
		$expected = $db->name('Domain.DomainHandle');
2306
		$this->assertEquals($expected, $result);
2307
		ConnectionManager::drop('mock');
2308
	}
2309
 
2310
/**
2311
 * testGetID
2312
 *
2313
 * @return void
2314
 */
2315
	public function testGetID() {
2316
		$TestModel = new Test();
2317
 
2318
		$result = $TestModel->getID();
2319
		$this->assertFalse($result);
2320
 
2321
		$TestModel->id = 9;
2322
		$result = $TestModel->getID();
2323
		$this->assertEquals(9, $result);
2324
 
2325
		$TestModel->id = array(10, 9, 8, 7);
2326
		$result = $TestModel->getID(2);
2327
		$this->assertEquals(8, $result);
2328
 
2329
		$TestModel->id = array(array(), 1, 2, 3);
2330
		$result = $TestModel->getID();
2331
		$this->assertFalse($result);
2332
	}
2333
 
2334
/**
2335
 * test that model->hasMethod checks self and behaviors.
2336
 *
2337
 * @return void
2338
 */
2339
	public function testHasMethod() {
2340
		$Article = new Article();
2341
		$Article->Behaviors = $this->getMock('BehaviorCollection');
2342
 
2343
		$Article->Behaviors->expects($this->at(0))
2344
			->method('hasMethod')
2345
			->will($this->returnValue(true));
2346
 
2347
		$Article->Behaviors->expects($this->at(1))
2348
			->method('hasMethod')
2349
			->will($this->returnValue(false));
2350
 
2351
		$this->assertTrue($Article->hasMethod('find'));
2352
 
2353
		$this->assertTrue($Article->hasMethod('pass'));
2354
		$this->assertFalse($Article->hasMethod('fail'));
2355
	}
2356
 
2357
/**
2358
 * testMultischemaFixture
2359
 *
2360
 * @return void
2361
 */
2362
	public function testMultischemaFixture() {
2363
		$config = ConnectionManager::enumConnectionObjects();
2364
		$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
2365
		$this->skipIf(!isset($config['test']) || !isset($config['test2']),
2366
			'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.'
2367
			);
2368
 
2369
		$this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
2370
 
2371
		$Player = ClassRegistry::init('Player');
2372
		$this->assertEquals('test', $Player->useDbConfig);
2373
		$this->assertEquals('test', $Player->Guild->useDbConfig);
2374
		$this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
2375
		$this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
2376
 
2377
		$players = $Player->find('all', array('recursive' => -1));
2378
		$guilds = $Player->Guild->find('all', array('recursive' => -1));
2379
		$guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
2380
 
2381
		$this->assertEquals(true, count($players) > 1);
2382
		$this->assertEquals(true, count($guilds) > 1);
2383
		$this->assertEquals(true, count($guildsPlayers) > 1);
2384
	}
2385
 
2386
/**
2387
 * testMultischemaFixtureWithThreeDatabases, three databases
2388
 *
2389
 * @return void
2390
 */
2391
	public function testMultischemaFixtureWithThreeDatabases() {
2392
		$config = ConnectionManager::enumConnectionObjects();
2393
		$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.');
2394
		$this->skipIf(
2395
			!isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']),
2396
			'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.'
2397
			);
2398
 
2399
		$this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
2400
 
2401
		$Player = ClassRegistry::init('Player');
2402
		$Player->bindModel(array(
2403
			'hasAndBelongsToMany' => array(
2404
				'Armor' => array(
2405
					'with' => 'ArmorsPlayer',
2406
					),
2407
				),
2408
			), false);
2409
		$this->assertEquals('test', $Player->useDbConfig);
2410
		$this->assertEquals('test', $Player->Guild->useDbConfig);
2411
		$this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig);
2412
		$this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig);
2413
		$this->assertEquals('test2', $Player->Armor->useDbConfig);
2414
		$this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig);
2415
		$this->assertEquals('test', $Player->getDataSource()->configKeyName);
2416
		$this->assertEquals('test', $Player->Guild->getDataSource()->configKeyName);
2417
		$this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName);
2418
		$this->assertEquals('test2', $Player->Armor->getDataSource()->configKeyName);
2419
		$this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName);
2420
 
2421
		$players = $Player->find('all', array('recursive' => -1));
2422
		$guilds = $Player->Guild->find('all', array('recursive' => -1));
2423
		$guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1));
2424
		$armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1));
2425
 
2426
		$this->assertEquals(true, count($players) > 1);
2427
		$this->assertEquals(true, count($guilds) > 1);
2428
		$this->assertEquals(true, count($guildsPlayers) > 1);
2429
		$this->assertEquals(true, count($armorsPlayers) > 1);
2430
	}
2431
 
2432
/**
2433
 * Tests that calling schema() on a model that is not supposed to use a table
2434
 * does not trigger any calls on any datasource
2435
 *
2436
 * @return void
2437
 */
2438
	public function testSchemaNoDB() {
2439
		$model = $this->getMock('Article', array('getDataSource'));
2440
		$model->useTable = false;
2441
		$model->expects($this->never())->method('getDataSource');
2442
		$this->assertEmpty($model->schema());
2443
	}
2444
}