Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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