Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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