Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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