ModelReadTest::testFindNeighborsNoPrev()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.4285
1
<?php
2
/**
3
 * ModelReadTest 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
/**
22
 * ModelReadTest
23
 *
24
 * @package       Cake.Test.Case.Model
25
 */
26
class ModelReadTest extends BaseModelTest {
27
28
/**
29
 * testExists function
30
 * @return void
31
 */
32
	public function testExists() {
33
		$this->loadFixtures('User');
34
		$TestModel = new User();
35
36
		$this->assertTrue($TestModel->exists(1));
37
38
		$TestModel->id = 2;
39
		$this->assertTrue($TestModel->exists());
40
41
		$TestModel->delete();
42
		$this->assertFalse($TestModel->exists());
43
44
		$this->assertFalse($TestModel->exists(2));
45
	}
46
47
/**
48
 * testFetchingNonUniqueFKJoinTableRecords()
49
 *
50
 * Tests if the results are properly returned in the case there are non-unique FK's
51
 * in the join table but another fields value is different. For example:
52
 * something_id | something_else_id | doomed = 1
53
 * something_id | something_else_id | doomed = 0
54
 * Should return both records and not just one.
55
 *
56
 * @return void
57
 */
58
	public function testFetchingNonUniqueFKJoinTableRecords() {
59
		$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
60
		$Something = new Something();
61
62
		$joinThingData = array(
63
			'JoinThing' => array(
64
				'something_id' => 1,
65
				'something_else_id' => 2,
66
				'doomed' => '0',
67
				'created' => '2007-03-18 10:39:23',
68
				'updated' => '2007-03-18 10:41:31'
69
			)
70
		);
71
72
		$Something->JoinThing->create($joinThingData);
73
		$Something->JoinThing->save();
74
75
		$result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2)));
76
77
		$this->assertEquals(true, $result[0]['JoinThing']['doomed']);
78
		$this->assertEquals(false, $result[1]['JoinThing']['doomed']);
79
80
		$result = $Something->find('first');
81
82
		$this->assertEquals(2, count($result['SomethingElse']));
83
84
		$doomed = Hash::extract($result['SomethingElse'], '{n}.JoinThing.doomed');
85
		$this->assertTrue(in_array(true, $doomed));
86
		$this->assertTrue(in_array(false, $doomed));
87
	}
88
89
/**
90
 * testGroupBy method
91
 *
92
 * These tests will never pass with Postgres or Oracle as all fields in a select must be
93
 * part of an aggregate function or in the GROUP BY statement.
94
 *
95
 * @return void
96
 */
97
	public function testGroupBy() {
98
		$isStrictGroupBy = $this->db instanceof Postgres || $this->db instanceof Sqlite || $this->db instanceof Oracle || $this->db instanceof Sqlserver;
0 ignored issues
show
Bug introduced by
The class Oracle does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
99
		$message = 'Postgres, Oracle, SQLite and SQL Server have strict GROUP BY and are incompatible with this test.';
100
101
		$this->skipIf($isStrictGroupBy, $message);
102
103
		$this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
104
		$Thread = new Thread();
105
		$Product = new Product();
106
107
		$result = $Thread->find('all', array(
108
			'group' => 'Thread.project_id',
109
			'order' => 'Thread.id ASC'
110
		));
111
112
		$expected = array(
113
			array(
114
				'Thread' => array(
115
					'id' => 1,
116
					'project_id' => 1,
117
					'name' => 'Project 1, Thread 1'
118
				),
119
				'Project' => array(
120
					'id' => 1,
121
					'name' => 'Project 1'
122
				),
123
				'Message' => array(
124
					array(
125
						'id' => 1,
126
						'thread_id' => 1,
127
						'name' => 'Thread 1, Message 1'
128
			))),
129
			array(
130
				'Thread' => array(
131
					'id' => 3,
132
					'project_id' => 2,
133
					'name' => 'Project 2, Thread 1'
134
				),
135
				'Project' => array(
136
					'id' => 2,
137
					'name' => 'Project 2'
138
				),
139
				'Message' => array(
140
					array(
141
						'id' => 3,
142
						'thread_id' => 3,
143
						'name' => 'Thread 3, Message 1'
144
		))));
145
		$this->assertEquals($expected, $result);
146
147
		$rows = $Thread->find('all', array(
148
			'group' => 'Thread.project_id',
149
			'fields' => array('Thread.project_id', 'COUNT(*) AS total')
150
		));
151
		$result = array();
152 View Code Duplication
		foreach ($rows as $row) {
0 ignored issues
show
Bug introduced by
The expression $rows of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
153
			$result[$row['Thread']['project_id']] = $row[0]['total'];
154
		}
155
		$expected = array(
156
			1 => 2,
157
			2 => 1
158
		);
159
		$this->assertEquals($expected, $result);
160
161
		$rows = $Thread->find('all', array(
162
			'group' => 'Thread.project_id',
163
			'fields' => array('Thread.project_id', 'COUNT(*) AS total'),
164
			'order' => 'Thread.project_id'
165
		));
166
		$result = array();
167 View Code Duplication
		foreach ($rows as $row) {
0 ignored issues
show
Bug introduced by
The expression $rows of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
168
			$result[$row['Thread']['project_id']] = $row[0]['total'];
169
		}
170
		$expected = array(
171
			1 => 2,
172
			2 => 1
173
		);
174
		$this->assertEquals($expected, $result);
175
176
		$result = $Thread->find('all', array(
177
			'conditions' => array('Thread.project_id' => 1),
178
			'group' => 'Thread.project_id'
179
		));
180
		$expected = array(
181
			array(
182
				'Thread' => array(
183
					'id' => 1,
184
					'project_id' => 1,
185
					'name' => 'Project 1, Thread 1'
186
				),
187
				'Project' => array(
188
					'id' => 1,
189
					'name' => 'Project 1'
190
				),
191
				'Message' => array(
192
					array(
193
						'id' => 1,
194
						'thread_id' => 1,
195
						'name' => 'Thread 1, Message 1'
196
		))));
197
		$this->assertEquals($expected, $result);
198
199
		$result = $Thread->find('all', array(
200
			'conditions' => array('Thread.project_id' => 1),
201
			'group' => 'Thread.project_id, Project.id'
202
		));
203
		$this->assertEquals($expected, $result);
204
205
		$result = $Thread->find('all', array(
206
			'conditions' => array('Thread.project_id' => 1),
207
			'group' => 'project_id'
208
		));
209
		$this->assertEquals($expected, $result);
210
211
		$result = $Thread->find('all', array(
212
			'conditions' => array('Thread.project_id' => 1),
213
			'group' => array('project_id')
214
		));
215
		$this->assertEquals($expected, $result);
216
217
		$result = $Thread->find('all', array(
218
			'conditions' => array('Thread.project_id' => 1),
219
			'group' => array('project_id', 'Project.id')
220
		));
221
		$this->assertEquals($expected, $result);
222
223
		$result = $Thread->find('all', array(
224
			'conditions' => array('Thread.project_id' => 1),
225
			'group' => array('Thread.project_id', 'Project.id')
226
		));
227
		$this->assertEquals($expected, $result);
228
229
		$expected = array(
230
			array('Product' => array('type' => 'Clothing'), array('price' => 32)),
231
			array('Product' => array('type' => 'Food'), array('price' => 9)),
232
			array('Product' => array('type' => 'Music'), array('price' => 4)),
233
			array('Product' => array('type' => 'Toy'), array('price' => 3))
234
		);
235
		$result = $Product->find('all', array(
236
			'fields' => array('Product.type', 'MIN(Product.price) as price'),
237
			'group' => 'Product.type',
238
			'order' => 'Product.type ASC'
239
			));
240
		$this->assertEquals($expected, $result);
241
242
		$result = $Product->find('all', array(
243
			'fields' => array('Product.type', 'MIN(Product.price) as price'),
244
			'group' => array('Product.type'),
245
			'order' => 'Product.type ASC'));
246
		$this->assertEquals($expected, $result);
247
	}
248
249
/**
250
 * testOldQuery method
251
 *
252
 * @return void
253
 */
254
	public function testOldQuery() {
255
		$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment');
256
		$Article = new Article();
257
258
		$query = 'SELECT title FROM ';
259
		$query .= $this->db->fullTableName('articles');
260
		$query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id IN (1,2)';
261
262
		$results = $Article->query($query);
263
		$this->assertTrue(is_array($results));
264
		$this->assertEquals(2, count($results));
265
266
		$query = 'SELECT title, body FROM ';
267
		$query .= $this->db->fullTableName('articles');
268
		$query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id = 1';
269
270
		$results = $Article->query($query, false);
271
		$this->assertFalse($this->db->getQueryCache($query));
272
		$this->assertTrue(is_array($results));
273
274
		$query = 'SELECT title, id FROM ';
275
		$query .= $this->db->fullTableName('articles');
276
		$query .= ' WHERE ' . $this->db->fullTableName('articles');
277
		$query .= '.published = ' . $this->db->value('Y');
278
279
		$results = $Article->query($query, true);
280
		$result = $this->db->getQueryCache($query);
281
		$this->assertFalse(empty($result));
282
		$this->assertTrue(is_array($results));
283
	}
284
285
/**
286
 * testPreparedQuery method
287
 *
288
 * @return void
289
 */
290
	public function testPreparedQuery() {
291
		$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
292
		$Article = new Article();
293
294
		$query = 'SELECT title, published FROM ';
295
		$query .= $this->db->fullTableName('articles');
296
		$query .= ' WHERE ' . $this->db->fullTableName('articles');
297
		$query .= '.id = ? AND ' . $this->db->fullTableName('articles') . '.published = ?';
298
299
		$params = array(1, 'Y');
300
		$result = $Article->query($query, $params);
301
		$expected = array(
302
			'0' => array(
303
				$this->db->fullTableName('articles', false, false) => array(
304
					'title' => 'First Article', 'published' => 'Y')
305
		));
306
307
		if (isset($result[0][0])) {
308
			$expected[0][0] = $expected[0][$this->db->fullTableName('articles', false, false)];
309
			unset($expected[0][$this->db->fullTableName('articles', false, false)]);
310
		}
311
312
		$this->assertEquals($expected, $result);
313
		$result = $this->db->getQueryCache($query, $params);
314
		$this->assertFalse(empty($result));
315
316
		$query = 'SELECT id, created FROM ';
317
		$query .= $this->db->fullTableName('articles');
318
		$query .= '  WHERE ' . $this->db->fullTableName('articles') . '.title = ?';
319
320
		$params = array('First Article');
321
		$result = $Article->query($query, $params, false);
322
		$this->assertTrue(is_array($result));
323
		$this->assertTrue(
324
			isset($result[0][$this->db->fullTableName('articles', false, false)]) ||
325
			isset($result[0][0])
326
		);
327
		$result = $this->db->getQueryCache($query, $params);
328
		$this->assertTrue(empty($result));
329
330
		$query = 'SELECT title FROM ';
331
		$query .= $this->db->fullTableName('articles');
332
		$query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title LIKE ?';
333
334
		$params = array('%First%');
335
		$result = $Article->query($query, $params);
336
		$this->assertTrue(is_array($result));
337
		$this->assertTrue(
338
			isset($result[0][$this->db->fullTableName('articles', false, false)]['title']) ||
339
			isset($result[0][0]['title'])
340
		);
341
342
		//related to ticket #5035
343
		$query = 'SELECT title FROM ';
344
		$query .= $this->db->fullTableName('articles') . ' WHERE title = ? AND published = ?';
345
		$params = array('First? Article', 'Y');
346
		$Article->query($query, $params);
347
348
		$result = $this->db->getQueryCache($query, $params);
349
		$this->assertFalse($result === false);
350
	}
351
352
/**
353
 * testParameterMismatch method
354
 *
355
 * @expectedException PDOException
356
 * @return void
357
 */
358
	public function testParameterMismatch() {
359
		$this->skipIf($this->db instanceof Sqlite, 'Sqlite does not accept real prepared statements, no way to check this');
360
		$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
361
		$Article = new Article();
362
363
		$query = 'SELECT * FROM ' . $this->db->fullTableName('articles');
364
		$query .= ' WHERE ' . $this->db->fullTableName('articles');
365
		$query .= '.published = ? AND ' . $this->db->fullTableName('articles') . '.user_id = ?';
366
		$params = array('Y');
367
368
		$Article->query($query, $params);
369
	}
370
371
/**
372
 * testVeryStrangeUseCase method
373
 *
374
 * @expectedException PDOException
375
 * @return void
376
 */
377
	public function testVeryStrangeUseCase() {
378
		$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
379
		$Article = new Article();
380
381
		$query = 'SELECT * FROM ? WHERE ? = ? AND ? = ?';
382
		$param = array(
383
			$this->db->fullTableName('articles'),
384
			$this->db->fullTableName('articles') . '.user_id', '3',
385
			$this->db->fullTableName('articles') . '.published', 'Y'
386
		);
387
388
		$Article->query($query, $param);
389
	}
390
391
/**
392
 * testRecursiveUnbind method
393
 *
394
 * @return void
395
 */
396
	public function testRecursiveUnbind() {
397
		$this->skipIf($this->db instanceof Sqlserver, 'The test of testRecursiveUnbind test is not compatible with SQL Server, because it check for time columns.');
398
399
		$this->loadFixtures('Apple', 'Sample');
400
		$TestModel = new Apple();
401
		$TestModel->recursive = 2;
402
403
		$result = $TestModel->find('all');
404
		$expected = array(
405
			array(
406
				'Apple' => array(
407
					'id' => 1,
408
					'apple_id' => 2,
409
					'color' => 'Red 1',
410
					'name' => 'Red Apple 1',
411
					'created' => '2006-11-22 10:38:58',
412
					'date' => '1951-01-04',
413
					'modified' => '2006-12-01 13:31:26',
414
					'mytime' => '22:57:17'
415
				),
416
				'Parent' => array(
417
					'id' => 2,
418
					'apple_id' => 1,
419
					'color' => 'Bright Red 1',
420
					'name' => 'Bright Red Apple',
421
					'created' => '2006-11-22 10:43:13',
422
					'date' => '2014-01-01',
423
					'modified' => '2006-11-30 18:38:10',
424
					'mytime' => '22:57:17',
425
					'Parent' => array(
426
						'id' => 1,
427
						'apple_id' => 2,
428
						'color' => 'Red 1',
429
						'name' => 'Red Apple 1',
430
						'created' => '2006-11-22 10:38:58',
431
						'date' => '1951-01-04',
432
						'modified' => '2006-12-01 13:31:26',
433
						'mytime' => '22:57:17'
434
					),
435
					'Sample' => array(
436
						'id' => 2,
437
						'apple_id' => 2,
438
						'name' => 'sample2'
439
					),
440
					'Child' => array(
441
						array(
442
							'id' => 1,
443
							'apple_id' => 2,
444
							'color' => 'Red 1',
445
							'name' => 'Red Apple 1',
446
							'created' => '2006-11-22 10:38:58',
447
							'date' => '1951-01-04',
448
							'modified' => '2006-12-01 13:31:26',
449
							'mytime' => '22:57:17'
450
						),
451
						array(
452
							'id' => 3,
453
							'apple_id' => 2,
454
							'color' => 'blue green',
455
							'name' => 'green blue',
456
							'created' => '2006-12-25 05:13:36',
457
							'date' => '2006-12-25',
458
							'modified' => '2006-12-25 05:23:24',
459
							'mytime' => '22:57:17'
460
						),
461
						array(
462
							'id' => 4,
463
							'apple_id' => 2,
464
							'color' => 'Blue Green',
465
							'name' => 'Test Name',
466
							'created' => '2006-12-25 05:23:36',
467
							'date' => '2006-12-25',
468
							'modified' => '2006-12-25 05:23:36',
469
							'mytime' => '22:57:17'
470
					))),
471
					'Sample' => array(
472
						'id' => '',
473
						'apple_id' => '',
474
						'name' => ''
475
					),
476
					'Child' => array(
477
						array(
478
							'id' => 2,
479
							'apple_id' => 1,
480
							'color' => 'Bright Red 1',
481
							'name' => 'Bright Red Apple',
482
							'created' => '2006-11-22 10:43:13',
483
							'date' => '2014-01-01',
484
							'modified' => '2006-11-30 18:38:10',
485
							'mytime' => '22:57:17',
486
							'Parent' => array(
487
								'id' => 1,
488
								'apple_id' => 2,
489
								'color' => 'Red 1',
490
								'name' => 'Red Apple 1',
491
								'created' => '2006-11-22 10:38:58',
492
								'date' => '1951-01-04',
493
								'modified' => '2006-12-01 13:31:26',
494
								'mytime' => '22:57:17'
495
							),
496
							'Sample' => array(
497
								'id' => 2,
498
								'apple_id' => 2,
499
								'name' => 'sample2'
500
							),
501
							'Child' => array(
502
								array(
503
									'id' => 1,
504
									'apple_id' => 2,
505
									'color' => 'Red 1',
506
									'name' => 'Red Apple 1',
507
									'created' => '2006-11-22 10:38:58',
508
									'date' => '1951-01-04',
509
									'modified' => '2006-12-01 13:31:26',
510
									'mytime' => '22:57:17'
511
								),
512
								array(
513
									'id' => 3,
514
									'apple_id' => 2,
515
									'color' => 'blue green',
516
									'name' => 'green blue',
517
									'created' => '2006-12-25 05:13:36',
518
									'date' => '2006-12-25',
519
									'modified' => '2006-12-25 05:23:24',
520
									'mytime' => '22:57:17'
521
								),
522
								array(
523
									'id' => 4,
524
									'apple_id' => 2,
525
									'color' => 'Blue Green',
526
									'name' => 'Test Name',
527
									'created' => '2006-12-25 05:23:36',
528
									'date' => '2006-12-25',
529
									'modified' => '2006-12-25 05:23:36',
530
									'mytime' => '22:57:17'
531
			))))),
532
			array(
533
				'Apple' => array(
534
					'id' => 2,
535
					'apple_id' => 1,
536
					'color' => 'Bright Red 1',
537
					'name' => 'Bright Red Apple',
538
					'created' => '2006-11-22 10:43:13',
539
					'date' => '2014-01-01',
540
					'modified' => '2006-11-30 18:38:10',
541
					'mytime' => '22:57:17'
542
				),
543
				'Parent' => array(
544
						'id' => 1,
545
						'apple_id' => 2,
546
						'color' => 'Red 1',
547
						'name' => 'Red Apple 1',
548
						'created' => '2006-11-22 10:38:58',
549
						'date' => '1951-01-04',
550
						'modified' => '2006-12-01 13:31:26',
551
						'mytime' => '22:57:17',
552
						'Parent' => array(
553
							'id' => 2,
554
							'apple_id' => 1,
555
							'color' => 'Bright Red 1',
556
							'name' => 'Bright Red Apple',
557
							'created' => '2006-11-22 10:43:13',
558
							'date' => '2014-01-01',
559
							'modified' => '2006-11-30 18:38:10',
560
							'mytime' => '22:57:17'
561
						),
562
						'Sample' => array(),
563
						'Child' => array(
564
							array(
565
								'id' => 2,
566
								'apple_id' => 1,
567
								'color' => 'Bright Red 1',
568
								'name' => 'Bright Red Apple',
569
								'created' => '2006-11-22 10:43:13',
570
								'date' => '2014-01-01',
571
								'modified' => '2006-11-30 18:38:10',
572
								'mytime' => '22:57:17'
573
					))),
574
					'Sample' => array(
575
						'id' => 2,
576
						'apple_id' => 2,
577
						'name' => 'sample2',
578
						'Apple' => array(
579
							'id' => 2,
580
							'apple_id' => 1,
581
							'color' => 'Bright Red 1',
582
							'name' => 'Bright Red Apple',
583
							'created' => '2006-11-22 10:43:13',
584
							'date' => '2014-01-01',
585
							'modified' => '2006-11-30 18:38:10',
586
							'mytime' => '22:57:17'
587
					)),
588
					'Child' => array(
589
						array(
590
							'id' => 1,
591
							'apple_id' => 2,
592
							'color' => 'Red 1',
593
							'name' => 'Red Apple 1',
594
							'created' => '2006-11-22 10:38:58',
595
							'date' => '1951-01-04',
596
							'modified' => '2006-12-01 13:31:26',
597
							'mytime' => '22:57:17',
598
							'Parent' => array(
599
								'id' => 2,
600
								'apple_id' => 1,
601
								'color' => 'Bright Red 1',
602
								'name' => 'Bright Red Apple',
603
								'created' => '2006-11-22 10:43:13',
604
								'date' => '2014-01-01',
605
								'modified' => '2006-11-30 18:38:10',
606
								'mytime' => '22:57:17'
607
							),
608
							'Sample' => array(),
609
							'Child' => array(
610
								array(
611
									'id' => 2,
612
									'apple_id' => 1,
613
									'color' => 'Bright Red 1',
614
									'name' => 'Bright Red Apple',
615
									'created' => '2006-11-22 10:43:13',
616
									'date' => '2014-01-01',
617
									'modified' => '2006-11-30 18:38:10',
618
									'mytime' => '22:57:17'
619
						))),
620
						array(
621
							'id' => 3,
622
							'apple_id' => 2,
623
							'color' => 'blue green',
624
							'name' => 'green blue',
625
							'created' => '2006-12-25 05:13:36',
626
							'date' => '2006-12-25',
627
							'modified' => '2006-12-25 05:23:24',
628
							'mytime' => '22:57:17',
629
							'Parent' => array(
630
								'id' => 2,
631
								'apple_id' => 1,
632
								'color' => 'Bright Red 1',
633
								'name' => 'Bright Red Apple',
634
								'created' => '2006-11-22 10:43:13',
635
								'date' => '2014-01-01',
636
								'modified' => '2006-11-30 18:38:10',
637
								'mytime' => '22:57:17'
638
							),
639
							'Sample' => array(
640
								'id' => 1,
641
								'apple_id' => 3,
642
								'name' => 'sample1'
643
						)),
644
						array(
645
							'id' => 4,
646
							'apple_id' => 2,
647
							'color' => 'Blue Green',
648
							'name' => 'Test Name',
649
							'created' => '2006-12-25 05:23:36',
650
							'date' => '2006-12-25',
651
							'modified' => '2006-12-25 05:23:36',
652
							'mytime' => '22:57:17',
653
							'Parent' => array(
654
								'id' => 2,
655
								'apple_id' => 1,
656
								'color' => 'Bright Red 1',
657
								'name' => 'Bright Red Apple',
658
								'created' => '2006-11-22 10:43:13',
659
								'date' => '2014-01-01',
660
								'modified' => '2006-11-30 18:38:10',
661
								'mytime' => '22:57:17'
662
							),
663
							'Sample' => array(
664
								'id' => 3,
665
								'apple_id' => 4,
666
								'name' => 'sample3'
667
							),
668
							'Child' => array(
669
								array(
670
									'id' => 6,
671
									'apple_id' => 4,
672
									'color' => 'My new appleOrange',
673
									'name' => 'My new apple',
674
									'created' => '2006-12-25 05:29:39',
675
									'date' => '2006-12-25',
676
									'modified' => '2006-12-25 05:29:39',
677
									'mytime' => '22:57:17'
678
			))))),
679
			array(
680
				'Apple' => array(
681
					'id' => 3,
682
					'apple_id' => 2,
683
					'color' => 'blue green',
684
					'name' => 'green blue',
685
					'created' => '2006-12-25 05:13:36',
686
					'date' => '2006-12-25',
687
					'modified' => '2006-12-25 05:23:24',
688
					'mytime' => '22:57:17'
689
				),
690
				'Parent' => array(
691
					'id' => 2,
692
					'apple_id' => 1,
693
					'color' => 'Bright Red 1',
694
					'name' => 'Bright Red Apple',
695
					'created' => '2006-11-22 10:43:13',
696
					'date' => '2014-01-01',
697
					'modified' => '2006-11-30 18:38:10',
698
					'mytime' => '22:57:17',
699
					'Parent' => array(
700
						'id' => 1,
701
						'apple_id' => 2,
702
						'color' => 'Red 1',
703
						'name' => 'Red Apple 1',
704
						'created' => '2006-11-22 10:38:58',
705
						'date' => '1951-01-04',
706
						'modified' => '2006-12-01 13:31:26',
707
						'mytime' => '22:57:17'
708
					),
709
					'Sample' => array(
710
						'id' => 2,
711
						'apple_id' => 2,
712
						'name' => 'sample2'
713
					),
714
					'Child' => array(
715
						array(
716
							'id' => 1,
717
							'apple_id' => 2,
718
							'color' => 'Red 1',
719
							'name' => 'Red Apple 1',
720
							'created' => '2006-11-22 10:38:58',
721
							'date' => '1951-01-04',
722
							'modified' => '2006-12-01 13:31:26',
723
							'mytime' => '22:57:17'
724
						),
725
						array(
726
							'id' => 3,
727
							'apple_id' => 2,
728
							'color' => 'blue green',
729
							'name' => 'green blue',
730
							'created' => '2006-12-25 05:13:36',
731
							'date' => '2006-12-25',
732
							'modified' => '2006-12-25 05:23:24',
733
							'mytime' => '22:57:17'
734
						),
735
						array(
736
							'id' => 4,
737
							'apple_id' => 2,
738
							'color' => 'Blue Green',
739
							'name' => 'Test Name',
740
							'created' => '2006-12-25 05:23:36',
741
							'date' => '2006-12-25',
742
							'modified' => '2006-12-25 05:23:36',
743
							'mytime' => '22:57:17'
744
				))),
745
				'Sample' => array(
746
					'id' => 1,
747
					'apple_id' => 3,
748
					'name' => 'sample1',
749
					'Apple' => array(
750
						'id' => 3,
751
						'apple_id' => 2,
752
						'color' => 'blue green',
753
						'name' => 'green blue',
754
						'created' => '2006-12-25 05:13:36',
755
						'date' => '2006-12-25',
756
						'modified' => '2006-12-25 05:23:24',
757
						'mytime' => '22:57:17'
758
				)),
759
				'Child' => array()
760
			),
761
			array(
762
				'Apple' => array(
763
					'id' => 4,
764
					'apple_id' => 2,
765
					'color' => 'Blue Green',
766
					'name' => 'Test Name',
767
					'created' => '2006-12-25 05:23:36',
768
					'date' => '2006-12-25',
769
					'modified' => '2006-12-25 05:23:36',
770
					'mytime' => '22:57:17'
771
				),
772
				'Parent' => array(
773
					'id' => 2,
774
					'apple_id' => 1,
775
					'color' => 'Bright Red 1',
776
					'name' => 'Bright Red Apple',
777
					'created' => '2006-11-22 10:43:13',
778
					'date' => '2014-01-01',
779
					'modified' => '2006-11-30 18:38:10',
780
					'mytime' => '22:57:17',
781
					'Parent' => array(
782
						'id' => 1,
783
						'apple_id' => 2,
784
						'color' => 'Red 1',
785
						'name' => 'Red Apple 1',
786
						'created' => '2006-11-22 10:38:58',
787
						'date' => '1951-01-04',
788
						'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
789
						'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
790
						'Child' => array(
791
							array(
792
								'id' => 1,
793
								'apple_id' => 2,
794
								'color' => 'Red 1',
795
								'name' => 'Red Apple 1',
796
								'created' => '2006-11-22 10:38:58',
797
								'date' => '1951-01-04',
798
								'modified' => '2006-12-01 13:31:26',
799
								'mytime' => '22:57:17'
800
							),
801
							array(
802
								'id' => 3,
803
								'apple_id' => 2,
804
								'color' => 'blue green',
805
								'name' => 'green blue',
806
								'created' => '2006-12-25 05:13:36',
807
								'date' => '2006-12-25',
808
								'modified' => '2006-12-25 05:23:24',
809
								'mytime' => '22:57:17'
810
							),
811
							array(
812
								'id' => 4,
813
								'apple_id' => 2,
814
								'color' => 'Blue Green',
815
								'name' => 'Test Name',
816
								'created' => '2006-12-25 05:23:36',
817
								'date' => '2006-12-25',
818
								'modified' => '2006-12-25 05:23:36',
819
								'mytime' => '22:57:17'
820
				))),
821
				'Sample' => array(
822
					'id' => 3,
823
					'apple_id' => 4,
824
					'name' => 'sample3',
825
					'Apple' => array(
826
						'id' => 4,
827
						'apple_id' => 2,
828
						'color' => 'Blue Green',
829
						'name' => 'Test Name',
830
						'created' => '2006-12-25 05:23:36',
831
						'date' => '2006-12-25',
832
						'modified' => '2006-12-25 05:23:36',
833
						'mytime' => '22:57:17'
834
				)),
835
				'Child' => array(
836
					array(
837
						'id' => 6,
838
						'apple_id' => 4,
839
						'color' => 'My new appleOrange',
840
						'name' => 'My new apple',
841
						'created' => '2006-12-25 05:29:39',
842
						'date' => '2006-12-25',
843
						'modified' => '2006-12-25 05:29:39',
844
						'mytime' => '22:57:17',
845
						'Parent' => array(
846
							'id' => 4,
847
							'apple_id' => 2,
848
							'color' => 'Blue Green',
849
							'name' => 'Test Name',
850
							'created' => '2006-12-25 05:23:36',
851
							'date' => '2006-12-25',
852
							'modified' => '2006-12-25 05:23:36',
853
							'mytime' => '22:57:17'
854
						),
855
						'Sample' => array(),
856
						'Child' => array(
857
							array(
858
								'id' => 7,
859
								'apple_id' => 6,
860
								'color' => 'Some wierd color',
861
								'name' => 'Some odd color',
862
								'created' => '2006-12-25 05:34:21',
863
								'date' => '2006-12-25',
864
								'modified' => '2006-12-25 05:34:21',
865
								'mytime' => '22:57:17'
866
			))))),
867
			array(
868
				'Apple' => array(
869
					'id' => 5,
870
					'apple_id' => 5,
871
					'color' => 'Green',
872
					'name' => 'Blue Green',
873
					'created' => '2006-12-25 05:24:06',
874
					'date' => '2006-12-25',
875
					'modified' => '2006-12-25 05:29:16',
876
					'mytime' => '22:57:17'
877
				),
878
				'Parent' => array(
879
					'id' => 5,
880
					'apple_id' => 5,
881
					'color' => 'Green',
882
					'name' => 'Blue Green',
883
					'created' => '2006-12-25 05:24:06',
884
					'date' => '2006-12-25',
885
					'modified' => '2006-12-25 05:29:16',
886
					'mytime' => '22:57:17',
887
					'Parent' => array(
888
						'id' => 5,
889
						'apple_id' => 5,
890
						'color' => 'Green',
891
						'name' => 'Blue Green',
892
						'created' => '2006-12-25 05:24:06',
893
						'date' => '2006-12-25',
894
						'modified' => '2006-12-25 05:29:16',
895
						'mytime' => '22:57:17'
896
					),
897
					'Sample' => array(
898
						'id' => 4,
899
						'apple_id' => 5,
900
						'name' => 'sample4'
901
					),
902
					'Child' => array(
903
						array(
904
							'id' => 5,
905
							'apple_id' => 5,
906
							'color' => 'Green',
907
							'name' => 'Blue Green',
908
							'created' => '2006-12-25 05:24:06',
909
							'date' => '2006-12-25',
910
							'modified' => '2006-12-25 05:29:16',
911
							'mytime' => '22:57:17'
912
				))),
913
				'Sample' => array(
914
					'id' => 4,
915
					'apple_id' => 5,
916
					'name' => 'sample4',
917
					'Apple' => array(
918
						'id' => 5,
919
						'apple_id' => 5,
920
						'color' => 'Green',
921
						'name' => 'Blue Green',
922
						'created' => '2006-12-25 05:24:06',
923
						'date' => '2006-12-25',
924
						'modified' => '2006-12-25 05:29:16',
925
						'mytime' => '22:57:17'
926
					)),
927
					'Child' => array(
928
						array(
929
							'id' => 5,
930
							'apple_id' => 5,
931
							'color' => 'Green',
932
							'name' => 'Blue Green',
933
							'created' => '2006-12-25 05:24:06',
934
							'date' => '2006-12-25',
935
							'modified' => '2006-12-25 05:29:16',
936
							'mytime' => '22:57:17',
937
							'Parent' => array(
938
								'id' => 5,
939
								'apple_id' => 5,
940
								'color' => 'Green',
941
								'name' => 'Blue Green',
942
								'created' => '2006-12-25 05:24:06',
943
								'date' => '2006-12-25',
944
								'modified' => '2006-12-25 05:29:16',
945
								'mytime' => '22:57:17'
946
							),
947
							'Sample' => array(
948
								'id' => 4,
949
								'apple_id' => 5,
950
								'name' => 'sample4'
951
							),
952
							'Child' => array(
953
								array(
954
									'id' => 5,
955
									'apple_id' => 5,
956
									'color' => 'Green',
957
									'name' => 'Blue Green',
958
									'created' => '2006-12-25 05:24:06',
959
									'date' => '2006-12-25',
960
									'modified' => '2006-12-25 05:29:16',
961
									'mytime' => '22:57:17'
962
			))))),
963
			array(
964
				'Apple' => array(
965
					'id' => 6,
966
					'apple_id' => 4,
967
					'color' => 'My new appleOrange',
968
					'name' => 'My new apple',
969
					'created' => '2006-12-25 05:29:39',
970
					'date' => '2006-12-25',
971
					'modified' => '2006-12-25 05:29:39',
972
					'mytime' => '22:57:17'
973
				),
974
				'Parent' => array(
975
					'id' => 4,
976
					'apple_id' => 2,
977
					'color' => 'Blue Green',
978
					'name' => 'Test Name',
979
					'created' => '2006-12-25 05:23:36',
980
					'date' => '2006-12-25',
981
					'modified' => '2006-12-25 05:23:36',
982
					'mytime' => '22:57:17',
983
					'Parent' => array(
984
						'id' => 2,
985
						'apple_id' => 1,
986
						'color' => 'Bright Red 1',
987
						'name' => 'Bright Red Apple',
988
						'created' => '2006-11-22 10:43:13',
989
						'date' => '2014-01-01',
990
						'modified' => '2006-11-30 18:38:10',
991
						'mytime' => '22:57:17'
992
					),
993
					'Sample' => array(
994
						'id' => 3,
995
						'apple_id' => 4,
996
						'name' => 'sample3'
997
					),
998
					'Child' => array(
999
						array(
1000
							'id' => 6,
1001
							'apple_id' => 4,
1002
							'color' => 'My new appleOrange',
1003
							'name' => 'My new apple',
1004
							'created' => '2006-12-25 05:29:39',
1005
							'date' => '2006-12-25',
1006
							'modified' => '2006-12-25 05:29:39',
1007
							'mytime' => '22:57:17'
1008
				))),
1009
				'Sample' => array(
1010
					'id' => '',
1011
					'apple_id' => '',
1012
					'name' => ''
1013
				),
1014
				'Child' => array(
1015
					array(
1016
						'id' => 7,
1017
						'apple_id' => 6,
1018
						'color' => 'Some wierd color',
1019
						'name' => 'Some odd color',
1020
						'created' => '2006-12-25 05:34:21',
1021
						'date' => '2006-12-25',
1022
						'modified' => '2006-12-25 05:34:21',
1023
						'mytime' => '22:57:17',
1024
						'Parent' => array(
1025
							'id' => 6,
1026
							'apple_id' => 4,
1027
							'color' => 'My new appleOrange',
1028
							'name' => 'My new apple',
1029
							'created' => '2006-12-25 05:29:39',
1030
							'date' => '2006-12-25',
1031
							'modified' => '2006-12-25 05:29:39',
1032
							'mytime' => '22:57:17'
1033
						),
1034
						'Sample' => array()
1035
			))),
1036
			array(
1037
				'Apple' => array(
1038
					'id' => 7,
1039
					'apple_id' => 6,
1040
					'color' =>
1041
					'Some wierd color',
1042
					'name' => 'Some odd color',
1043
					'created' => '2006-12-25 05:34:21',
1044
					'date' => '2006-12-25',
1045
					'modified' => '2006-12-25 05:34:21',
1046
					'mytime' => '22:57:17'
1047
				),
1048
				'Parent' => array(
1049
					'id' => 6,
1050
					'apple_id' => 4,
1051
					'color' => 'My new appleOrange',
1052
					'name' => 'My new apple',
1053
					'created' => '2006-12-25 05:29:39',
1054
					'date' => '2006-12-25',
1055
					'modified' => '2006-12-25 05:29:39',
1056
					'mytime' => '22:57:17',
1057
					'Parent' => array(
1058
						'id' => 4,
1059
						'apple_id' => 2,
1060
						'color' => 'Blue Green',
1061
						'name' => 'Test Name',
1062
						'created' => '2006-12-25 05:23:36',
1063
						'date' => '2006-12-25',
1064
						'modified' => '2006-12-25 05:23:36',
1065
						'mytime' => '22:57:17'
1066
					),
1067
					'Sample' => array(),
1068
					'Child' => array(
1069
						array(
1070
							'id' => 7,
1071
							'apple_id' => 6,
1072
							'color' => 'Some wierd color',
1073
							'name' => 'Some odd color',
1074
							'created' => '2006-12-25 05:34:21',
1075
							'date' => '2006-12-25',
1076
							'modified' => '2006-12-25 05:34:21',
1077
							'mytime' => '22:57:17'
1078
				))),
1079
				'Sample' => array(
1080
					'id' => '',
1081
					'apple_id' => '',
1082
					'name' => ''
1083
				),
1084
				'Child' => array()));
1085
		$this->assertEquals($expected, $result);
1086
1087
		$result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample')));
1088
		$this->assertTrue($result);
1089
1090
		$result = $TestModel->find('all');
1091
		$expected = array(
1092
			array(
1093
				'Apple' => array(
1094
					'id' => 1,
1095
					'apple_id' => 2,
1096
					'color' => 'Red 1',
1097
					'name' => 'Red Apple 1',
1098
					'created' => '2006-11-22 10:38:58',
1099
					'date' => '1951-01-04',
1100
					'modified' => '2006-12-01 13:31:26',
1101
					'mytime' => '22:57:17'),
1102
					'Parent' => array(
1103
						'id' => 2,
1104
						'apple_id' => 1,
1105
						'color' => 'Bright Red 1',
1106
						'name' => 'Bright Red Apple',
1107
						'created' => '2006-11-22 10:43:13',
1108
						'date' => '2014-01-01',
1109
						'modified' => '2006-11-30 18:38:10',
1110
						'mytime' => '22:57:17',
1111
						'Parent' => array(
1112
							'id' => 1,
1113
							'apple_id' => 2,
1114
							'color' => 'Red 1',
1115
							'name' => 'Red Apple 1',
1116
							'created' => '2006-11-22 10:38:58',
1117
							'date' => '1951-01-04',
1118
							'modified' => '2006-12-01 13:31:26',
1119
							'mytime' => '22:57:17'
1120
						),
1121
						'Child' => array(
1122
							array(
1123
								'id' => 1,
1124
								'apple_id' => 2,
1125
								'color' => 'Red 1',
1126
								'name' => 'Red Apple 1',
1127
								'created' => '2006-11-22 10:38:58',
1128
								'date' => '1951-01-04',
1129
								'modified' => '2006-12-01 13:31:26',
1130
								'mytime' => '22:57:17'
1131
							),
1132
							array(
1133
								'id' => 3,
1134
								'apple_id' => 2,
1135
								'color' => 'blue green',
1136
								'name' => 'green blue',
1137
								'created' => '2006-12-25 05:13:36',
1138
								'date' => '2006-12-25',
1139
								'modified' => '2006-12-25 05:23:24',
1140
								'mytime' => '22:57:17'
1141
							),
1142
							array(
1143
								'id' => 4,
1144
								'apple_id' => 2,
1145
								'color' => 'Blue Green',
1146
								'name' => 'Test Name',
1147
								'created' => '2006-12-25 05:23:36',
1148
								'date' => '2006-12-25',
1149
								'modified' => '2006-12-25 05:23:36',
1150
								'mytime' => '22:57:17'
1151
					))),
1152
					'Sample' => array(
1153
						'id' => '',
1154
						'apple_id' => '',
1155
						'name' => ''
1156
					),
1157
					'Child' => array(
1158
						array(
1159
							'id' => 2,
1160
							'apple_id' => 1,
1161
							'color' => 'Bright Red 1',
1162
							'name' => 'Bright Red Apple',
1163
							'created' => '2006-11-22 10:43:13',
1164
							'date' => '2014-01-01',
1165
							'modified' => '2006-11-30 18:38:10',
1166
							'mytime' => '22:57:17',
1167
							'Parent' => array(
1168
								'id' => 1,
1169
								'apple_id' => 2,
1170
								'color' => 'Red 1',
1171
								'name' => 'Red Apple 1',
1172
								'created' => '2006-11-22 10:38:58',
1173
								'date' => '1951-01-04',
1174
								'modified' => '2006-12-01 13:31:26',
1175
								'mytime' => '22:57:17'
1176
							),
1177
							'Sample' => array(
1178
								'id' => 2,
1179
								'apple_id' => 2,
1180
								'name' => 'sample2'
1181
							),
1182
							'Child' => array(
1183
								array(
1184
									'id' => 1,
1185
									'apple_id' => 2,
1186
									'color' => 'Red 1',
1187
									'name' => 'Red Apple 1',
1188
									'created' => '2006-11-22 10:38:58',
1189
									'date' => '1951-01-04',
1190
									'modified' => '2006-12-01 13:31:26',
1191
									'mytime' => '22:57:17'
1192
								),
1193
								array(
1194
									'id' => 3,
1195
									'apple_id' => 2,
1196
									'color' => 'blue green',
1197
									'name' => 'green blue',
1198
									'created' => '2006-12-25 05:13:36',
1199
									'date' => '2006-12-25',
1200
									'modified' => '2006-12-25 05:23:24',
1201
									'mytime' => '22:57:17'
1202
								),
1203
								array(
1204
									'id' => 4,
1205
									'apple_id' => 2,
1206
									'color' => 'Blue Green',
1207
									'name' => 'Test Name',
1208
									'created' => '2006-12-25 05:23:36',
1209
									'date' => '2006-12-25',
1210
									'modified' => '2006-12-25 05:23:36',
1211
									'mytime' => '22:57:17'
1212
			))))),
1213
			array(
1214
				'Apple' => array(
1215
					'id' => 2,
1216
					'apple_id' => 1,
1217
					'color' => 'Bright Red 1',
1218
					'name' => 'Bright Red Apple',
1219
					'created' => '2006-11-22 10:43:13',
1220
					'date' => '2014-01-01',
1221
					'modified' => '2006-11-30 18:38:10',
1222
					'mytime' => '22:57:17'
1223
				),
1224
				'Parent' => array(
1225
					'id' => 1,
1226
					'apple_id' => 2,
1227
					'color' => 'Red 1',
1228
					'name' => 'Red Apple 1',
1229
					'created' => '2006-11-22 10:38:58',
1230
					'date' => '1951-01-04',
1231
					'modified' => '2006-12-01 13:31:26',
1232
					'mytime' => '22:57:17',
1233
					'Parent' => array(
1234
						'id' => 2,
1235
						'apple_id' => 1,
1236
						'color' => 'Bright Red 1',
1237
						'name' => 'Bright Red Apple',
1238
						'created' => '2006-11-22 10:43:13',
1239
						'date' => '2014-01-01',
1240
						'modified' => '2006-11-30 18:38:10',
1241
						'mytime' => '22:57:17'
1242
					),
1243
					'Child' => array(
1244
						array(
1245
							'id' => 2,
1246
							'apple_id' => 1,
1247
							'color' => 'Bright Red 1',
1248
							'name' => 'Bright Red Apple',
1249
							'created' => '2006-11-22 10:43:13',
1250
							'date' => '2014-01-01',
1251
							'modified' => '2006-11-30 18:38:10',
1252
							'mytime' => '22:57:17'
1253
				))),
1254
				'Sample' => array(
1255
					'id' => 2,
1256
					'apple_id' => 2,
1257
					'name' => 'sample2',
1258
					'Apple' => array(
1259
						'id' => 2,
1260
						'apple_id' => 1,
1261
						'color' => 'Bright Red 1',
1262
						'name' => 'Bright Red Apple',
1263
						'created' => '2006-11-22 10:43:13',
1264
						'date' => '2014-01-01',
1265
						'modified' => '2006-11-30 18:38:10',
1266
						'mytime' => '22:57:17'
1267
				)),
1268
				'Child' => array(
1269
					array(
1270
						'id' => 1,
1271
						'apple_id' => 2,
1272
						'color' => 'Red 1',
1273
						'name' => 'Red Apple 1',
1274
						'created' => '2006-11-22 10:38:58',
1275
						'date' => '1951-01-04',
1276
						'modified' => '2006-12-01 13:31:26',
1277
						'mytime' => '22:57:17',
1278
						'Parent' => array(
1279
							'id' => 2,
1280
							'apple_id' => 1,
1281
							'color' => 'Bright Red 1',
1282
							'name' => 'Bright Red Apple',
1283
							'created' => '2006-11-22 10:43:13',
1284
							'date' => '2014-01-01',
1285
							'modified' => '2006-11-30 18:38:10',
1286
							'mytime' => '22:57:17'
1287
						),
1288
						'Sample' => array(),
1289
						'Child' => array(
1290
							array(
1291
								'id' => 2,
1292
								'apple_id' => 1,
1293
								'color' => 'Bright Red 1',
1294
								'name' => 'Bright Red Apple',
1295
								'created' => '2006-11-22 10:43:13',
1296
								'date' => '2014-01-01', 'modified' =>
1297
								'2006-11-30 18:38:10',
1298
								'mytime' => '22:57:17'
1299
					))),
1300
					array(
1301
						'id' => 3,
1302
						'apple_id' => 2,
1303
						'color' => 'blue green',
1304
						'name' => 'green blue',
1305
						'created' => '2006-12-25 05:13:36',
1306
						'date' => '2006-12-25',
1307
						'modified' => '2006-12-25 05:23:24',
1308
						'mytime' => '22:57:17',
1309
						'Parent' => array(
1310
							'id' => 2,
1311
							'apple_id' => 1,
1312
							'color' => 'Bright Red 1',
1313
							'name' => 'Bright Red Apple',
1314
							'created' => '2006-11-22 10:43:13',
1315
							'date' => '2014-01-01',
1316
							'modified' => '2006-11-30 18:38:10',
1317
							'mytime' => '22:57:17'
1318
						),
1319
						'Sample' => array(
1320
							'id' => 1,
1321
							'apple_id' => 3,
1322
							'name' => 'sample1'
1323
					)),
1324
					array(
1325
						'id' => 4,
1326
						'apple_id' => 2,
1327
						'color' => 'Blue Green',
1328
						'name' => 'Test Name',
1329
						'created' => '2006-12-25 05:23:36',
1330
						'date' => '2006-12-25',
1331
						'modified' => '2006-12-25 05:23:36',
1332
						'mytime' => '22:57:17',
1333
						'Parent' => array(
1334
							'id' => 2,
1335
							'apple_id' => 1,
1336
							'color' => 'Bright Red 1',
1337
							'name' => 'Bright Red Apple',
1338
							'created' => '2006-11-22 10:43:13',
1339
							'date' => '2014-01-01',
1340
							'modified' => '2006-11-30 18:38:10',
1341
							'mytime' => '22:57:17'
1342
						),
1343
						'Sample' => array(
1344
							'id' => 3,
1345
							'apple_id' => 4,
1346
							'name' => 'sample3'
1347
						),
1348
						'Child' => array(
1349
							array(
1350
								'id' => 6,
1351
								'apple_id' => 4,
1352
								'color' => 'My new appleOrange',
1353
								'name' => 'My new apple',
1354
								'created' => '2006-12-25 05:29:39',
1355
								'date' => '2006-12-25',
1356
								'modified' => '2006-12-25 05:29:39',
1357
								'mytime' => '22:57:17'
1358
			))))),
1359
			array(
1360
				'Apple' => array(
1361
					'id' => 3,
1362
					'apple_id' => 2,
1363
					'color' => 'blue green',
1364
					'name' => 'green blue',
1365
					'created' => '2006-12-25 05:13:36',
1366
					'date' => '2006-12-25',
1367
					'modified' => '2006-12-25 05:23:24',
1368
					'mytime' => '22:57:17'
1369
				),
1370
				'Parent' => array(
1371
					'id' => 2,
1372
					'apple_id' => 1,
1373
					'color' => 'Bright Red 1',
1374
					'name' => 'Bright Red Apple',
1375
					'created' => '2006-11-22 10:43:13',
1376
					'date' => '2014-01-01',
1377
					'modified' => '2006-11-30 18:38:10',
1378
					'mytime' => '22:57:17',
1379
					'Parent' => array(
1380
						'id' => 1,
1381
						'apple_id' => 2,
1382
						'color' => 'Red 1',
1383
						'name' => 'Red Apple 1',
1384
						'created' => '2006-11-22 10:38:58',
1385
						'date' => '1951-01-04',
1386
						'modified' => '2006-12-01 13:31:26',
1387
						'mytime' => '22:57:17'
1388
					),
1389
					'Child' => array(
1390
						array(
1391
							'id' => 1,
1392
							'apple_id' => 2,
1393
							'color' => 'Red 1',
1394
							'name' => 'Red Apple 1',
1395
							'created' => '2006-11-22 10:38:58',
1396
							'date' => '1951-01-04',
1397
							'modified' => '2006-12-01 13:31:26',
1398
							'mytime' => '22:57:17'
1399
						),
1400
						array(
1401
							'id' => 3,
1402
							'apple_id' => 2,
1403
							'color' => 'blue green',
1404
							'name' => 'green blue',
1405
							'created' => '2006-12-25 05:13:36',
1406
							'date' => '2006-12-25',
1407
							'modified' => '2006-12-25 05:23:24',
1408
							'mytime' => '22:57:17'
1409
						),
1410
						array(
1411
							'id' => 4,
1412
							'apple_id' => 2,
1413
							'color' => 'Blue Green',
1414
							'name' => 'Test Name',
1415
							'created' => '2006-12-25 05:23:36',
1416
							'date' => '2006-12-25',
1417
							'modified' => '2006-12-25 05:23:36',
1418
							'mytime' => '22:57:17'
1419
				))),
1420
				'Sample' => array(
1421
					'id' => 1,
1422
					'apple_id' => 3,
1423
					'name' => 'sample1',
1424
					'Apple' => array(
1425
						'id' => 3,
1426
						'apple_id' => 2,
1427
						'color' => 'blue green',
1428
						'name' => 'green blue',
1429
						'created' => '2006-12-25 05:13:36',
1430
						'date' => '2006-12-25',
1431
						'modified' => '2006-12-25 05:23:24',
1432
						'mytime' => '22:57:17'
1433
				)),
1434
				'Child' => array()
1435
			),
1436
			array(
1437
				'Apple' => array(
1438
					'id' => 4,
1439
					'apple_id' => 2,
1440
					'color' => 'Blue Green',
1441
					'name' => 'Test Name',
1442
					'created' => '2006-12-25 05:23:36',
1443
					'date' => '2006-12-25',
1444
					'modified' => '2006-12-25 05:23:36',
1445
					'mytime' => '22:57:17'
1446
				),
1447
				'Parent' => array(
1448
					'id' => 2,
1449
					'apple_id' => 1,
1450
					'color' => 'Bright Red 1',
1451
					'name' => 'Bright Red Apple',
1452
					'created' => '2006-11-22 10:43:13',
1453
					'date' => '2014-01-01',
1454
					'modified' => '2006-11-30 18:38:10',
1455
					'mytime' => '22:57:17',
1456
					'Parent' => array(
1457
						'id' => 1,
1458
						'apple_id' => 2,
1459
						'color' => 'Red 1',
1460
						'name' => 'Red Apple 1',
1461
						'created' => '2006-11-22 10:38:58',
1462
						'date' => '1951-01-04',
1463
						'modified' => '2006-12-01 13:31:26',
1464
						'mytime' => '22:57:17'
1465
					),
1466
					'Child' => array(
1467
						array(
1468
							'id' => 1,
1469
							'apple_id' => 2,
1470
							'color' => 'Red 1',
1471
							'name' => 'Red Apple 1',
1472
							'created' => '2006-11-22 10:38:58',
1473
							'date' => '1951-01-04',
1474
							'modified' => '2006-12-01 13:31:26',
1475
							'mytime' => '22:57:17'
1476
						),
1477
						array(
1478
							'id' => 3,
1479
							'apple_id' => 2,
1480
							'color' => 'blue green',
1481
							'name' => 'green blue',
1482
							'created' => '2006-12-25 05:13:36',
1483
							'date' => '2006-12-25',
1484
							'modified' => '2006-12-25 05:23:24',
1485
							'mytime' => '22:57:17'
1486
						),
1487
						array(
1488
							'id' => 4,
1489
							'apple_id' => 2,
1490
							'color' => 'Blue Green',
1491
							'name' => 'Test Name',
1492
							'created' => '2006-12-25 05:23:36',
1493
							'date' => '2006-12-25',
1494
							'modified' => '2006-12-25 05:23:36',
1495
							'mytime' => '22:57:17'
1496
				))),
1497
				'Sample' => array(
1498
					'id' => 3,
1499
					'apple_id' => 4,
1500
					'name' => 'sample3',
1501
					'Apple' => array(
1502
						'id' => 4,
1503
						'apple_id' => 2,
1504
						'color' => 'Blue Green',
1505
						'name' => 'Test Name',
1506
						'created' => '2006-12-25 05:23:36',
1507
						'date' => '2006-12-25',
1508
						'modified' => '2006-12-25 05:23:36',
1509
						'mytime' => '22:57:17'
1510
				)),
1511
				'Child' => array(
1512
					array(
1513
						'id' => 6,
1514
						'apple_id' => 4,
1515
						'color' => 'My new appleOrange',
1516
						'name' => 'My new apple',
1517
						'created' => '2006-12-25 05:29:39',
1518
						'date' => '2006-12-25',
1519
						'modified' => '2006-12-25 05:29:39',
1520
						'mytime' => '22:57:17',
1521
						'Parent' => array(
1522
							'id' => 4,
1523
							'apple_id' => 2,
1524
							'color' => 'Blue Green',
1525
							'name' => 'Test Name',
1526
							'created' => '2006-12-25 05:23:36',
1527
							'date' => '2006-12-25',
1528
							'modified' => '2006-12-25 05:23:36',
1529
							'mytime' => '22:57:17'
1530
						),
1531
						'Sample' => array(),
1532
							'Child' => array(
1533
								array(
1534
									'id' => 7,
1535
									'apple_id' => 6,
1536
									'color' => 'Some wierd color',
1537
									'name' => 'Some odd color',
1538
									'created' => '2006-12-25 05:34:21',
1539
									'date' => '2006-12-25',
1540
									'modified' => '2006-12-25 05:34:21',
1541
									'mytime' => '22:57:17'
1542
			))))),
1543
			array(
1544
				'Apple' => array(
1545
					'id' => 5,
1546
					'apple_id' => 5,
1547
					'color' => 'Green',
1548
					'name' => 'Blue Green',
1549
					'created' => '2006-12-25 05:24:06',
1550
					'date' => '2006-12-25',
1551
					'modified' => '2006-12-25 05:29:16',
1552
					'mytime' => '22:57:17'
1553
				),
1554
				'Parent' => array(
1555
					'id' => 5,
1556
					'apple_id' => 5,
1557
					'color' => 'Green',
1558
					'name' => 'Blue Green',
1559
					'created' => '2006-12-25 05:24:06',
1560
					'date' => '2006-12-25',
1561
					'modified' => '2006-12-25 05:29:16',
1562
					'mytime' => '22:57:17',
1563
					'Parent' => array(
1564
						'id' => 5,
1565
						'apple_id' => 5,
1566
						'color' => 'Green',
1567
						'name' => 'Blue Green',
1568
						'created' => '2006-12-25 05:24:06',
1569
						'date' => '2006-12-25',
1570
						'modified' => '2006-12-25 05:29:16',
1571
						'mytime' => '22:57:17'
1572
					),
1573
					'Child' => array(
1574
						array(
1575
							'id' => 5,
1576
							'apple_id' => 5,
1577
							'color' => 'Green',
1578
							'name' => 'Blue Green',
1579
							'created' => '2006-12-25 05:24:06',
1580
							'date' => '2006-12-25',
1581
							'modified' => '2006-12-25 05:29:16',
1582
							'mytime' => '22:57:17'
1583
				))),
1584
				'Sample' => array(
1585
					'id' => 4,
1586
					'apple_id' => 5,
1587
					'name' => 'sample4',
1588
					'Apple' => array(
1589
						'id' => 5,
1590
						'apple_id' => 5,
1591
						'color' => 'Green',
1592
						'name' => 'Blue Green',
1593
						'created' => '2006-12-25 05:24:06',
1594
						'date' => '2006-12-25',
1595
						'modified' => '2006-12-25 05:29:16',
1596
						'mytime' => '22:57:17'
1597
				)),
1598
				'Child' => array(
1599
					array(
1600
						'id' => 5,
1601
						'apple_id' => 5,
1602
						'color' => 'Green',
1603
						'name' => 'Blue Green',
1604
						'created' => '2006-12-25 05:24:06',
1605
						'date' => '2006-12-25',
1606
						'modified' => '2006-12-25 05:29:16',
1607
						'mytime' => '22:57:17',
1608
						'Parent' => array(
1609
							'id' => 5,
1610
							'apple_id' => 5,
1611
							'color' => 'Green',
1612
							'name' => 'Blue Green',
1613
							'created' => '2006-12-25 05:24:06',
1614
							'date' => '2006-12-25',
1615
							'modified' => '2006-12-25 05:29:16',
1616
							'mytime' => '22:57:17'
1617
						),
1618
						'Sample' => array(
1619
							'id' => 4,
1620
							'apple_id' => 5,
1621
							'name' => 'sample4'
1622
						),
1623
						'Child' => array(
1624
							array(
1625
								'id' => 5,
1626
								'apple_id' => 5,
1627
								'color' => 'Green',
1628
								'name' => 'Blue Green',
1629
								'created' => '2006-12-25 05:24:06',
1630
								'date' => '2006-12-25',
1631
								'modified' => '2006-12-25 05:29:16',
1632
								'mytime' => '22:57:17'
1633
			))))),
1634
			array(
1635
				'Apple' => array(
1636
					'id' => 6,
1637
					'apple_id' => 4,
1638
					'color' => 'My new appleOrange',
1639
					'name' => 'My new apple',
1640
					'created' => '2006-12-25 05:29:39',
1641
					'date' => '2006-12-25',
1642
					'modified' => '2006-12-25 05:29:39',
1643
					'mytime' => '22:57:17'
1644
				),
1645
				'Parent' => array(
1646
					'id' => 4,
1647
					'apple_id' => 2,
1648
					'color' => 'Blue Green',
1649
					'name' => 'Test Name',
1650
					'created' => '2006-12-25 05:23:36',
1651
					'date' => '2006-12-25',
1652
					'modified' => '2006-12-25 05:23:36',
1653
					'mytime' => '22:57:17',
1654
					'Parent' => array(
1655
						'id' => 2,
1656
						'apple_id' => 1,
1657
						'color' => 'Bright Red 1',
1658
						'name' => 'Bright Red Apple',
1659
						'created' => '2006-11-22 10:43:13',
1660
						'date' => '2014-01-01',
1661
						'modified' => '2006-11-30 18:38:10',
1662
						'mytime' => '22:57:17'
1663
					),
1664
					'Child' => array(
1665
						array(
1666
							'id' => 6,
1667
							'apple_id' => 4,
1668
							'color' => 'My new appleOrange',
1669
							'name' => 'My new apple',
1670
							'created' => '2006-12-25 05:29:39',
1671
							'date' => '2006-12-25',
1672
							'modified' => '2006-12-25 05:29:39',
1673
							'mytime' => '22:57:17'
1674
				))),
1675
				'Sample' => array(
1676
					'id' => '',
1677
					'apple_id' => '',
1678
					'name' => ''
1679
				),
1680
				'Child' => array(
1681
					array(
1682
						'id' => 7,
1683
						'apple_id' => 6,
1684
						'color' => 'Some wierd color',
1685
						'name' => 'Some odd color',
1686
						'created' => '2006-12-25 05:34:21',
1687
						'date' => '2006-12-25',
1688
						'modified' => '2006-12-25 05:34:21',
1689
						'mytime' => '22:57:17',
1690
						'Parent' => array(
1691
							'id' => 6,
1692
							'apple_id' => 4,
1693
							'color' => 'My new appleOrange',
1694
							'name' => 'My new apple',
1695
							'created' => '2006-12-25 05:29:39',
1696
							'date' => '2006-12-25',
1697
							'modified' => '2006-12-25 05:29:39',
1698
							'mytime' => '22:57:17'
1699
						),
1700
						'Sample' => array()
1701
			))),
1702
			array(
1703
				'Apple' => array(
1704
					'id' => 7,
1705
					'apple_id' => 6,
1706
					'color' => 'Some wierd color',
1707
					'name' => 'Some odd color',
1708
					'created' => '2006-12-25 05:34:21',
1709
					'date' => '2006-12-25',
1710
					'modified' => '2006-12-25 05:34:21',
1711
					'mytime' => '22:57:17'
1712
				),
1713
				'Parent' => array(
1714
					'id' => 6,
1715
					'apple_id' => 4,
1716
					'color' => 'My new appleOrange',
1717
					'name' => 'My new apple',
1718
					'created' => '2006-12-25 05:29:39',
1719
					'date' => '2006-12-25',
1720
					'modified' => '2006-12-25 05:29:39',
1721
					'mytime' => '22:57:17',
1722
					'Parent' => array(
1723
						'id' => 4,
1724
						'apple_id' => 2,
1725
						'color' => 'Blue Green',
1726
						'name' => 'Test Name',
1727
						'created' => '2006-12-25 05:23:36',
1728
						'date' => '2006-12-25',
1729
						'modified' => '2006-12-25 05:23:36',
1730
						'mytime' => '22:57:17'
1731
					),
1732
					'Child' => array(
1733
						array(
1734
							'id' => 7,
1735
							'apple_id' => 6,
1736
							'color' => 'Some wierd color',
1737
							'name' => 'Some odd color',
1738
							'created' => '2006-12-25 05:34:21',
1739
							'date' => '2006-12-25',
1740
							'modified' => '2006-12-25 05:34:21',
1741
							'mytime' => '22:57:17'
1742
				))),
1743
				'Sample' => array(
1744
					'id' => '',
1745
					'apple_id' => '',
1746
					'name' => ''
1747
				),
1748
				'Child' => array()
1749
		));
1750
1751
		$this->assertEquals($expected, $result);
1752
1753
		$result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample')));
1754
		$this->assertTrue($result);
1755
1756
		$result = $TestModel->unbindModel(array('hasMany' => array('Child')));
1757
		$this->assertTrue($result);
1758
1759
		$result = $TestModel->find('all');
1760
		$expected = array(
1761
			array(
1762
				'Apple' => array(
1763
					'id' => 1,
1764
					'apple_id' => 2,
1765
					'color' => 'Red 1',
1766
					'name' => 'Red Apple 1',
1767
					'created' => '2006-11-22 10:38:58',
1768
					'date' => '1951-01-04',
1769
					'modified' => '2006-12-01 13:31:26',
1770
					'mytime' => '22:57:17'
1771
				),
1772
				'Parent' => array(
1773
					'id' => 2,
1774
					'apple_id' => 1,
1775
					'color' => 'Bright Red 1',
1776
					'name' => 'Bright Red Apple',
1777
					'created' => '2006-11-22 10:43:13',
1778
					'date' => '2014-01-01',
1779
					'modified' => '2006-11-30 18:38:10',
1780
					'mytime' => '22:57:17',
1781
					'Parent' => array(
1782
						'id' => 1,
1783
						'apple_id' => 2,
1784
						'color' => 'Red 1',
1785
						'name' => 'Red Apple 1',
1786
						'created' => '2006-11-22 10:38:58',
1787
						'date' => '1951-01-04',
1788
						'modified' => '2006-12-01 13:31:26',
1789
						'mytime' => '22:57:17'
1790
					),
1791
					'Child' => array(
1792
						array(
1793
							'id' => 1,
1794
							'apple_id' => 2,
1795
							'color' => 'Red 1',
1796
							'name' => 'Red Apple 1',
1797
							'created' => '2006-11-22 10:38:58',
1798
							'date' => '1951-01-04',
1799
							'modified' => '2006-12-01 13:31:26',
1800
							'mytime' => '22:57:17'
1801
						),
1802
						array(
1803
							'id' => 3,
1804
							'apple_id' => 2,
1805
							'color' => 'blue green',
1806
							'name' => 'green blue',
1807
							'created' => '2006-12-25 05:13:36',
1808
							'date' => '2006-12-25',
1809
							'modified' => '2006-12-25 05:23:24',
1810
							'mytime' => '22:57:17'
1811
						),
1812
						array(
1813
							'id' => 4,
1814
							'apple_id' => 2,
1815
							'color' => 'Blue Green',
1816
							'name' => 'Test Name',
1817
							'created' => '2006-12-25 05:23:36',
1818
							'date' => '2006-12-25',
1819
							'modified' => '2006-12-25 05:23:36',
1820
							'mytime' => '22:57:17'
1821
				))),
1822
				'Sample' => array(
1823
					'id' => '',
1824
					'apple_id' => '',
1825
					'name' => ''
1826
			)),
1827
			array(
1828
				'Apple' => array(
1829
					'id' => 2,
1830
					'apple_id' => 1,
1831
					'color' => 'Bright Red 1',
1832
					'name' => 'Bright Red Apple',
1833
					'created' => '2006-11-22 10:43:13',
1834
					'date' => '2014-01-01',
1835
					'modified' => '2006-11-30 18:38:10',
1836
					'mytime' => '22:57:17'
1837
				),
1838
				'Parent' => array(
1839
					'id' => 1,
1840
					'apple_id' => 2,
1841
					'color' => 'Red 1',
1842
					'name' => 'Red Apple 1',
1843
					'created' => '2006-11-22 10:38:58',
1844
					'date' => '1951-01-04',
1845
					'modified' => '2006-12-01 13:31:26',
1846
					'mytime' => '22:57:17',
1847
					'Parent' => array(
1848
						'id' => 2,
1849
						'apple_id' => 1,
1850
						'color' => 'Bright Red 1',
1851
						'name' => 'Bright Red Apple',
1852
						'created' => '2006-11-22 10:43:13',
1853
						'date' => '2014-01-01',
1854
						'modified' => '2006-11-30 18:38:10',
1855
						'mytime' => '22:57:17'
1856
					),
1857
					'Child' => array(
1858
						array(
1859
							'id' => 2,
1860
							'apple_id' => 1,
1861
							'color' => 'Bright Red 1',
1862
							'name' => 'Bright Red Apple',
1863
							'created' => '2006-11-22 10:43:13',
1864
							'date' => '2014-01-01',
1865
							'modified' => '2006-11-30 18:38:10',
1866
							'mytime' => '22:57:17'
1867
				))),
1868
				'Sample' => array(
1869
					'id' => 2,
1870
					'apple_id' => 2,
1871
					'name' => 'sample2',
1872
					'Apple' => array(
1873
						'id' => 2,
1874
						'apple_id' => 1,
1875
						'color' => 'Bright Red 1',
1876
						'name' => 'Bright Red Apple',
1877
						'created' => '2006-11-22 10:43:13',
1878
						'date' => '2014-01-01',
1879
						'modified' => '2006-11-30 18:38:10',
1880
						'mytime' => '22:57:17'
1881
			))),
1882
			array(
1883
				'Apple' => array(
1884
				'id' => 3,
1885
				'apple_id' => 2,
1886
				'color' => 'blue green',
1887
				'name' => 'green blue',
1888
				'created' => '2006-12-25 05:13:36',
1889
				'date' => '2006-12-25',
1890
				'modified' => '2006-12-25 05:23:24',
1891
				'mytime' => '22:57:17'
1892
			),
1893
			'Parent' => array(
1894
				'id' => 2,
1895
				'apple_id' => 1,
1896
				'color' => 'Bright Red 1',
1897
				'name' => 'Bright Red Apple',
1898
				'created' => '2006-11-22 10:43:13',
1899
				'date' => '2014-01-01',
1900
				'modified' => '2006-11-30 18:38:10',
1901
				'mytime' => '22:57:17',
1902
				'Parent' => array(
1903
					'id' => 1,
1904
					'apple_id' => 2,
1905
					'color' => 'Red 1',
1906
					'name' => 'Red Apple 1',
1907
					'created' => '2006-11-22 10:38:58',
1908
					'date' => '1951-01-04',
1909
					'modified' => '2006-12-01 13:31:26',
1910
					'mytime' => '22:57:17'
1911
				),
1912
				'Child' => array(
1913
					array(
1914
						'id' => 1,
1915
						'apple_id' => 2,
1916
						'color' => 'Red 1',
1917
						'name' => 'Red Apple 1',
1918
						'created' => '2006-11-22 10:38:58',
1919
						'date' => '1951-01-04',
1920
						'modified' => '2006-12-01 13:31:26',
1921
						'mytime' => '22:57:17'
1922
					),
1923
					array(
1924
						'id' => 3,
1925
						'apple_id' => 2,
1926
						'color' => 'blue green',
1927
						'name' => 'green blue',
1928
						'created' => '2006-12-25 05:13:36',
1929
						'date' => '2006-12-25',
1930
						'modified' => '2006-12-25 05:23:24',
1931
						'mytime' => '22:57:17'
1932
					),
1933
					array(
1934
						'id' => 4,
1935
						'apple_id' => 2,
1936
						'color' => 'Blue Green',
1937
						'name' => 'Test Name',
1938
						'created' => '2006-12-25 05:23:36',
1939
						'date' => '2006-12-25',
1940
						'modified' => '2006-12-25 05:23:36',
1941
						'mytime' => '22:57:17'
1942
			))),
1943
			'Sample' => array(
1944
				'id' => 1,
1945
				'apple_id' => 3,
1946
				'name' => 'sample1',
1947
				'Apple' => array(
1948
					'id' => 3,
1949
					'apple_id' => 2,
1950
					'color' => 'blue green',
1951
					'name' => 'green blue',
1952
					'created' => '2006-12-25 05:13:36',
1953
					'date' => '2006-12-25',
1954
					'modified' => '2006-12-25 05:23:24',
1955
					'mytime' => '22:57:17'
1956
		))),
1957
		array(
1958
			'Apple' => array(
1959
				'id' => 4,
1960
				'apple_id' => 2,
1961
				'color' => 'Blue Green',
1962
				'name' => 'Test Name',
1963
				'created' => '2006-12-25 05:23:36',
1964
				'date' => '2006-12-25',
1965
				'modified' => '2006-12-25 05:23:36',
1966
				'mytime' => '22:57:17'
1967
			),
1968
			'Parent' => array(
1969
				'id' => 2,
1970
				'apple_id' => 1,
1971
				'color' => 'Bright Red 1',
1972
				'name' => 'Bright Red Apple',
1973
				'created' => '2006-11-22 10:43:13',
1974
				'date' => '2014-01-01',
1975
				'modified' => '2006-11-30 18:38:10',
1976
				'mytime' => '22:57:17',
1977
				'Parent' => array(
1978
					'id' => 1,
1979
					'apple_id' => 2,
1980
					'color' => 'Red 1',
1981
					'name' => 'Red Apple 1',
1982
					'created' => '2006-11-22 10:38:58',
1983
					'date' => '1951-01-04',
1984
					'modified' => '2006-12-01 13:31:26',
1985
					'mytime' => '22:57:17'
1986
				),
1987
				'Child' => array(
1988
					array(
1989
						'id' => 1,
1990
						'apple_id' => 2,
1991
						'color' => 'Red 1',
1992
						'name' => 'Red Apple 1',
1993
						'created' => '2006-11-22 10:38:58',
1994
						'date' => '1951-01-04',
1995
						'modified' => '2006-12-01 13:31:26',
1996
						'mytime' => '22:57:17'
1997
					),
1998
					array(
1999
						'id' => 3,
2000
						'apple_id' => 2,
2001
						'color' => 'blue green',
2002
						'name' => 'green blue',
2003
						'created' => '2006-12-25 05:13:36',
2004
						'date' => '2006-12-25',
2005
						'modified' => '2006-12-25 05:23:24',
2006
						'mytime' => '22:57:17'
2007
					),
2008
					array(
2009
						'id' => 4,
2010
						'apple_id' => 2,
2011
						'color' => 'Blue Green',
2012
						'name' => 'Test Name',
2013
						'created' => '2006-12-25 05:23:36',
2014
						'date' => '2006-12-25',
2015
						'modified' => '2006-12-25 05:23:36',
2016
						'mytime' => '22:57:17'
2017
			))),
2018
			'Sample' => array(
2019
				'id' => 3,
2020
				'apple_id' => 4,
2021
				'name' => 'sample3',
2022
				'Apple' => array(
2023
					'id' => 4,
2024
					'apple_id' => 2,
2025
					'color' => 'Blue Green',
2026
					'name' => 'Test Name',
2027
					'created' => '2006-12-25 05:23:36',
2028
					'date' => '2006-12-25',
2029
					'modified' => '2006-12-25 05:23:36',
2030
					'mytime' => '22:57:17'
2031
		))),
2032
		array(
2033
			'Apple' => array(
2034
				'id' => 5,
2035
				'apple_id' => 5,
2036
				'color' => 'Green',
2037
				'name' => 'Blue Green',
2038
				'created' => '2006-12-25 05:24:06',
2039
				'date' => '2006-12-25',
2040
				'modified' => '2006-12-25 05:29:16',
2041
				'mytime' => '22:57:17'
2042
			),
2043
			'Parent' => array(
2044
				'id' => 5,
2045
				'apple_id' => 5,
2046
				'color' => 'Green',
2047
				'name' => 'Blue Green',
2048
				'created' => '2006-12-25 05:24:06',
2049
				'date' => '2006-12-25',
2050
				'modified' => '2006-12-25 05:29:16',
2051
				'mytime' => '22:57:17',
2052
				'Parent' => array(
2053
					'id' => 5,
2054
					'apple_id' => 5,
2055
					'color' => 'Green',
2056
					'name' => 'Blue Green',
2057
					'created' => '2006-12-25 05:24:06',
2058
					'date' => '2006-12-25',
2059
					'modified' => '2006-12-25 05:29:16',
2060
					'mytime' => '22:57:17'
2061
				),
2062
				'Child' => array(
2063
					array(
2064
						'id' => 5,
2065
						'apple_id' => 5,
2066
						'color' => 'Green',
2067
						'name' => 'Blue Green',
2068
						'created' => '2006-12-25 05:24:06',
2069
						'date' => '2006-12-25',
2070
						'modified' => '2006-12-25 05:29:16',
2071
						'mytime' => '22:57:17'
2072
			))),
2073
			'Sample' => array(
2074
				'id' => 4,
2075
				'apple_id' => 5,
2076
				'name' => 'sample4',
2077
				'Apple' => array(
2078
					'id' => 5,
2079
					'apple_id' => 5,
2080
					'color' => 'Green',
2081
					'name' => 'Blue Green',
2082
					'created' => '2006-12-25 05:24:06',
2083
					'date' => '2006-12-25',
2084
					'modified' => '2006-12-25 05:29:16',
2085
					'mytime' => '22:57:17'
2086
		))),
2087
		array(
2088
			'Apple' => array(
2089
				'id' => 6,
2090
				'apple_id' => 4,
2091
				'color' => 'My new appleOrange',
2092
				'name' => 'My new apple',
2093
				'created' => '2006-12-25 05:29:39',
2094
				'date' => '2006-12-25',
2095
				'modified' => '2006-12-25 05:29:39',
2096
				'mytime' => '22:57:17'
2097
			),
2098
			'Parent' => array(
2099
				'id' => 4,
2100
				'apple_id' => 2,
2101
				'color' => 'Blue Green',
2102
				'name' => 'Test Name',
2103
				'created' => '2006-12-25 05:23:36',
2104
				'date' => '2006-12-25',
2105
				'modified' => '2006-12-25 05:23:36',
2106
				'mytime' => '22:57:17',
2107
				'Parent' => array(
2108
					'id' => 2,
2109
					'apple_id' => 1,
2110
					'color' => 'Bright Red 1',
2111
					'name' => 'Bright Red Apple',
2112
					'created' => '2006-11-22 10:43:13',
2113
					'date' => '2014-01-01',
2114
					'modified' => '2006-11-30 18:38:10',
2115
					'mytime' => '22:57:17'
2116
				),
2117
				'Child' => array(
2118
					array(
2119
						'id' => 6,
2120
						'apple_id' => 4,
2121
						'color' => 'My new appleOrange',
2122
						'name' => 'My new apple',
2123
						'created' => '2006-12-25 05:29:39',
2124
						'date' => '2006-12-25',
2125
						'modified' => '2006-12-25 05:29:39',
2126
						'mytime' => '22:57:17'
2127
			))),
2128
			'Sample' => array(
2129
				'id' => '',
2130
				'apple_id' => '',
2131
				'name' => ''
2132
		)),
2133
		array(
2134
			'Apple' => array(
2135
				'id' => 7,
2136
				'apple_id' => 6,
2137
				'color' => 'Some wierd color',
2138
				'name' => 'Some odd color',
2139
				'created' => '2006-12-25 05:34:21',
2140
				'date' => '2006-12-25',
2141
				'modified' => '2006-12-25 05:34:21',
2142
				'mytime' => '22:57:17'
2143
			),
2144
			'Parent' => array(
2145
				'id' => 6,
2146
				'apple_id' => 4,
2147
				'color' => 'My new appleOrange',
2148
				'name' => 'My new apple',
2149
				'created' => '2006-12-25 05:29:39',
2150
				'date' => '2006-12-25',
2151
				'modified' => '2006-12-25 05:29:39',
2152
				'mytime' => '22:57:17',
2153
				'Parent' => array(
2154
					'id' => 4,
2155
					'apple_id' => 2,
2156
					'color' => 'Blue Green',
2157
					'name' => 'Test Name',
2158
					'created' => '2006-12-25 05:23:36',
2159
					'date' => '2006-12-25',
2160
					'modified' => '2006-12-25 05:23:36',
2161
					'mytime' => '22:57:17'
2162
				),
2163
				'Child' => array(
2164
					array(
2165
						'id' => 7,
2166
						'apple_id' => 6,
2167
						'color' => 'Some wierd color',
2168
						'name' => 'Some odd color',
2169
						'created' => '2006-12-25 05:34:21',
2170
						'date' => '2006-12-25',
2171
						'modified' => '2006-12-25 05:34:21',
2172
						'mytime' => '22:57:17'
2173
			))),
2174
			'Sample' => array(
2175
				'id' => '',
2176
				'apple_id' => '',
2177
				'name' => ''
2178
		)));
2179
2180
		$this->assertEquals($expected, $result);
2181
2182
		$result = $TestModel->unbindModel(array('hasMany' => array('Child')));
2183
		$this->assertTrue($result);
2184
2185
		$result = $TestModel->Sample->unbindModel(array('belongsTo' => array('Apple')));
2186
		$this->assertTrue($result);
2187
2188
		$result = $TestModel->find('all');
2189
		$expected = array(
2190
			array(
2191
				'Apple' => array(
2192
					'id' => 1,
2193
					'apple_id' => 2,
2194
					'color' => 'Red 1',
2195
					'name' => 'Red Apple 1',
2196
					'created' => '2006-11-22 10:38:58',
2197
					'date' => '1951-01-04',
2198
					'modified' => '2006-12-01 13:31:26',
2199
					'mytime' => '22:57:17'
2200
				),
2201
				'Parent' => array(
2202
					'id' => 2,
2203
					'apple_id' => 1,
2204
					'color' => 'Bright Red 1',
2205
					'name' => 'Bright Red Apple',
2206
					'created' => '2006-11-22 10:43:13',
2207
					'date' => '2014-01-01',
2208
					'modified' => '2006-11-30 18:38:10',
2209
					'mytime' => '22:57:17',
2210
					'Parent' => array(
2211
						'id' => 1,
2212
						'apple_id' => 2,
2213
						'color' => 'Red 1',
2214
						'name' => 'Red Apple 1',
2215
						'created' => '2006-11-22 10:38:58',
2216
						'date' => '1951-01-04',
2217
						'modified' => '2006-12-01 13:31:26',
2218
						'mytime' => '22:57:17'
2219
					),
2220
					'Sample' => array(
2221
						'id' => 2,
2222
						'apple_id' => 2,
2223
						'name' => 'sample2'
2224
					),
2225
					'Child' => array(
2226
						array(
2227
							'id' => 1,
2228
							'apple_id' => 2,
2229
							'color' => 'Red 1',
2230
							'name' => 'Red Apple 1',
2231
							'created' => '2006-11-22 10:38:58',
2232
							'date' => '1951-01-04',
2233
							'modified' => '2006-12-01 13:31:26',
2234
							'mytime' => '22:57:17'
2235
						),
2236
						array(
2237
							'id' => 3,
2238
							'apple_id' => 2,
2239
							'color' => 'blue green',
2240
							'name' => 'green blue',
2241
							'created' => '2006-12-25 05:13:36',
2242
							'date' => '2006-12-25',
2243
							'modified' => '2006-12-25 05:23:24',
2244
							'mytime' => '22:57:17'
2245
						),
2246
						array(
2247
							'id' => 4,
2248
							'apple_id' => 2,
2249
							'color' => 'Blue Green',
2250
							'name' => 'Test Name',
2251
							'created' => '2006-12-25 05:23:36',
2252
							'date' => '2006-12-25',
2253
							'modified' => '2006-12-25 05:23:36',
2254
							'mytime' => '22:57:17'
2255
				))),
2256
				'Sample' => array(
2257
					'id' => '',
2258
					'apple_id' => '',
2259
					'name' => ''
2260
			)),
2261
			array(
2262
				'Apple' => array(
2263
					'id' => 2,
2264
					'apple_id' => 1,
2265
					'color' => 'Bright Red 1',
2266
					'name' => 'Bright Red Apple',
2267
					'created' => '2006-11-22 10:43:13',
2268
					'date' => '2014-01-01',
2269
					'modified' => '2006-11-30 18:38:10',
2270
					'mytime' => '22:57:17'
2271
				),
2272
				'Parent' => array(
2273
					'id' => 1,
2274
					'apple_id' => 2,
2275
					'color' => 'Red 1',
2276
					'name' => 'Red Apple 1',
2277
					'created' => '2006-11-22 10:38:58',
2278
					'date' => '1951-01-04',
2279
					'modified' => '2006-12-01 13:31:26',
2280
					'mytime' => '22:57:17',
2281
					'Parent' => array(
2282
						'id' => 2,
2283
						'apple_id' => 1,
2284
						'color' => 'Bright Red 1',
2285
						'name' => 'Bright Red Apple',
2286
						'created' => '2006-11-22 10:43:13',
2287
						'date' => '2014-01-01',
2288
						'modified' => '2006-11-30 18:38:10',
2289
						'mytime' => '22:57:17'
2290
					),
2291
					'Sample' => array(),
2292
					'Child' => array(
2293
						array(
2294
							'id' => 2,
2295
							'apple_id' => 1,
2296
							'color' => 'Bright Red 1',
2297
							'name' => 'Bright Red Apple',
2298
							'created' => '2006-11-22 10:43:13',
2299
							'date' => '2014-01-01',
2300
							'modified' => '2006-11-30 18:38:10',
2301
							'mytime' => '22:57:17'
2302
				))),
2303
				'Sample' => array(
2304
					'id' => 2,
2305
					'apple_id' => 2,
2306
					'name' => 'sample2'
2307
			)),
2308
			array(
2309
				'Apple' => array(
2310
					'id' => 3,
2311
					'apple_id' => 2,
2312
					'color' => 'blue green',
2313
					'name' => 'green blue',
2314
					'created' => '2006-12-25 05:13:36',
2315
					'date' => '2006-12-25',
2316
					'modified' => '2006-12-25 05:23:24',
2317
					'mytime' => '22:57:17'
2318
				),
2319
				'Parent' => array(
2320
					'id' => 2,
2321
					'apple_id' => 1,
2322
					'color' => 'Bright Red 1',
2323
					'name' => 'Bright Red Apple',
2324
					'created' => '2006-11-22 10:43:13',
2325
					'date' => '2014-01-01',
2326
					'modified' => '2006-11-30 18:38:10',
2327
					'mytime' => '22:57:17',
2328
					'Parent' => array(
2329
						'id' => 1,
2330
						'apple_id' => 2,
2331
						'color' => 'Red 1',
2332
						'name' => 'Red Apple 1',
2333
						'created' => '2006-11-22 10:38:58',
2334
						'date' => '1951-01-04',
2335
						'modified' => '2006-12-01 13:31:26',
2336
						'mytime' => '22:57:17'
2337
					),
2338
					'Sample' => array(
2339
						'id' => 2,
2340
						'apple_id' => 2,
2341
						'name' => 'sample2'
2342
					),
2343
					'Child' => array(
2344
						array(
2345
							'id' => 1,
2346
							'apple_id' => 2,
2347
							'color' => 'Red 1',
2348
							'name' => 'Red Apple 1',
2349
							'created' => '2006-11-22 10:38:58',
2350
							'date' => '1951-01-04',
2351
							'modified' => '2006-12-01 13:31:26',
2352
							'mytime' => '22:57:17'
2353
						),
2354
						array(
2355
							'id' => 3,
2356
							'apple_id' => 2,
2357
							'color' => 'blue green',
2358
							'name' => 'green blue',
2359
							'created' => '2006-12-25 05:13:36',
2360
							'date' => '2006-12-25',
2361
							'modified' => '2006-12-25 05:23:24',
2362
							'mytime' => '22:57:17'
2363
						),
2364
						array(
2365
							'id' => 4,
2366
							'apple_id' => 2,
2367
							'color' => 'Blue Green',
2368
							'name' => 'Test Name',
2369
							'created' => '2006-12-25 05:23:36',
2370
							'date' => '2006-12-25',
2371
							'modified' => '2006-12-25 05:23:36',
2372
							'mytime' => '22:57:17'
2373
				))),
2374
				'Sample' => array(
2375
					'id' => 1,
2376
					'apple_id' => 3,
2377
					'name' => 'sample1'
2378
			)),
2379
			array(
2380
				'Apple' => array(
2381
					'id' => 4,
2382
					'apple_id' => 2,
2383
					'color' => 'Blue Green',
2384
					'name' => 'Test Name',
2385
					'created' => '2006-12-25 05:23:36',
2386
					'date' => '2006-12-25',
2387
					'modified' => '2006-12-25 05:23:36',
2388
					'mytime' => '22:57:17'
2389
				),
2390
				'Parent' => array(
2391
					'id' => 2,
2392
					'apple_id' => 1,
2393
					'color' => 'Bright Red 1',
2394
					'name' => 'Bright Red Apple',
2395
					'created' => '2006-11-22 10:43:13',
2396
					'date' => '2014-01-01',
2397
					'modified' => '2006-11-30 18:38:10',
2398
					'mytime' => '22:57:17',
2399
					'Parent' => array(
2400
						'id' => 1,
2401
						'apple_id' => 2,
2402
						'color' => 'Red 1',
2403
						'name' => 'Red Apple 1',
2404
						'created' => '2006-11-22 10:38:58',
2405
						'date' => '1951-01-04',
2406
						'modified' => '2006-12-01 13:31:26',
2407
						'mytime' => '22:57:17'
2408
					),
2409
					'Sample' => array(
2410
						'id' => 2,
2411
						'apple_id' => 2,
2412
						'name' => 'sample2'
2413
					),
2414
					'Child' => array(
2415
						array(
2416
							'id' => 1,
2417
							'apple_id' => 2,
2418
							'color' => 'Red 1',
2419
							'name' => 'Red Apple 1',
2420
							'created' => '2006-11-22 10:38:58',
2421
							'date' => '1951-01-04',
2422
							'modified' => '2006-12-01 13:31:26',
2423
							'mytime' => '22:57:17'
2424
						),
2425
						array(
2426
							'id' => 3,
2427
							'apple_id' => 2,
2428
							'color' => 'blue green',
2429
							'name' => 'green blue',
2430
							'created' => '2006-12-25 05:13:36',
2431
							'date' => '2006-12-25',
2432
							'modified' => '2006-12-25 05:23:24',
2433
							'mytime' => '22:57:17'
2434
						),
2435
						array(
2436
							'id' => 4,
2437
							'apple_id' => 2,
2438
							'color' => 'Blue Green',
2439
							'name' => 'Test Name',
2440
							'created' => '2006-12-25 05:23:36',
2441
							'date' => '2006-12-25',
2442
							'modified' => '2006-12-25 05:23:36',
2443
							'mytime' => '22:57:17'
2444
				))),
2445
				'Sample' => array(
2446
					'id' => 3,
2447
					'apple_id' => 4,
2448
					'name' => 'sample3'
2449
			)),
2450
			array(
2451
				'Apple' => array(
2452
					'id' => 5,
2453
					'apple_id' => 5,
2454
					'color' => 'Green',
2455
					'name' => 'Blue Green',
2456
					'created' => '2006-12-25 05:24:06',
2457
					'date' => '2006-12-25',
2458
					'modified' => '2006-12-25 05:29:16',
2459
					'mytime' => '22:57:17'
2460
				),
2461
				'Parent' => array(
2462
					'id' => 5,
2463
					'apple_id' => 5,
2464
					'color' => 'Green',
2465
					'name' => 'Blue Green',
2466
					'created' => '2006-12-25 05:24:06',
2467
					'date' => '2006-12-25',
2468
					'modified' => '2006-12-25 05:29:16',
2469
					'mytime' => '22:57:17',
2470
					'Parent' => array(
2471
						'id' => 5,
2472
						'apple_id' => 5,
2473
						'color' => 'Green',
2474
						'name' => 'Blue Green',
2475
						'created' => '2006-12-25 05:24:06',
2476
						'date' => '2006-12-25',
2477
						'modified' => '2006-12-25 05:29:16',
2478
						'mytime' => '22:57:17'
2479
					),
2480
					'Sample' => array(
2481
						'id' => 4,
2482
						'apple_id' => 5,
2483
						'name' => 'sample4'
2484
					),
2485
					'Child' => array(
2486
						array(
2487
							'id' => 5,
2488
							'apple_id' => 5,
2489
							'color' => 'Green',
2490
							'name' => 'Blue Green',
2491
							'created' => '2006-12-25 05:24:06',
2492
							'date' => '2006-12-25',
2493
							'modified' => '2006-12-25 05:29:16',
2494
							'mytime' => '22:57:17'
2495
				))),
2496
				'Sample' => array(
2497
					'id' => 4,
2498
					'apple_id' => 5,
2499
					'name' => 'sample4'
2500
			)),
2501
			array(
2502
				'Apple' => array(
2503
					'id' => 6,
2504
					'apple_id' => 4,
2505
					'color' => 'My new appleOrange',
2506
					'name' => 'My new apple',
2507
					'created' => '2006-12-25 05:29:39',
2508
					'date' => '2006-12-25',
2509
					'modified' => '2006-12-25 05:29:39',
2510
					'mytime' => '22:57:17'
2511
				),
2512
				'Parent' => array(
2513
					'id' => 4,
2514
					'apple_id' => 2,
2515
					'color' => 'Blue Green',
2516
					'name' => 'Test Name',
2517
					'created' => '2006-12-25 05:23:36',
2518
					'date' => '2006-12-25',
2519
					'modified' => '2006-12-25 05:23:36',
2520
					'mytime' => '22:57:17',
2521
					'Parent' => array(
2522
						'id' => 2,
2523
						'apple_id' => 1,
2524
						'color' => 'Bright Red 1',
2525
						'name' => 'Bright Red Apple',
2526
						'created' => '2006-11-22 10:43:13',
2527
						'date' => '2014-01-01',
2528
						'modified' => '2006-11-30 18:38:10',
2529
						'mytime' => '22:57:17'
2530
					),
2531
					'Sample' => array(
2532
						'id' => 3,
2533
						'apple_id' => 4,
2534
						'name' => 'sample3'
2535
					),
2536
					'Child' => array(
2537
						array(
2538
							'id' => 6,
2539
							'apple_id' => 4,
2540
							'color' => 'My new appleOrange',
2541
							'name' => 'My new apple',
2542
							'created' => '2006-12-25 05:29:39',
2543
							'date' => '2006-12-25',
2544
							'modified' => '2006-12-25 05:29:39',
2545
							'mytime' => '22:57:17'
2546
				))),
2547
				'Sample' => array(
2548
					'id' => '',
2549
					'apple_id' => '',
2550
					'name' => ''
2551
			)),
2552
			array(
2553
				'Apple' => array(
2554
					'id' => 7,
2555
					'apple_id' => 6,
2556
					'color' => 'Some wierd color',
2557
					'name' => 'Some odd color',
2558
					'created' => '2006-12-25 05:34:21',
2559
					'date' => '2006-12-25',
2560
					'modified' => '2006-12-25 05:34:21',
2561
					'mytime' => '22:57:17'
2562
				),
2563
				'Parent' => array(
2564
					'id' => 6,
2565
					'apple_id' => 4,
2566
					'color' => 'My new appleOrange',
2567
					'name' => 'My new apple',
2568
					'created' => '2006-12-25 05:29:39',
2569
					'date' => '2006-12-25',
2570
					'modified' => '2006-12-25 05:29:39',
2571
					'mytime' => '22:57:17',
2572
					'Parent' => array(
2573
						'id' => 4,
2574
						'apple_id' => 2,
2575
						'color' => 'Blue Green',
2576
						'name' => 'Test Name',
2577
						'created' => '2006-12-25 05:23:36',
2578
						'date' => '2006-12-25',
2579
						'modified' => '2006-12-25 05:23:36',
2580
						'mytime' => '22:57:17'
2581
					),
2582
					'Sample' => array(),
2583
					'Child' => array(
2584
						array(
2585
							'id' => 7,
2586
							'apple_id' => 6,
2587
							'color' => 'Some wierd color',
2588
							'name' => 'Some odd color',
2589
							'created' => '2006-12-25 05:34:21',
2590
							'date' => '2006-12-25',
2591
							'modified' => '2006-12-25 05:34:21',
2592
							'mytime' => '22:57:17'
2593
				))),
2594
				'Sample' => array(
2595
					'id' => '',
2596
					'apple_id' => '',
2597
					'name' => ''
2598
		)));
2599
		$this->assertEquals($expected, $result);
2600
2601
		$result = $TestModel->Parent->unbindModel(array('belongsTo' => array('Parent')));
2602
		$this->assertTrue($result);
2603
2604
		$result = $TestModel->unbindModel(array('hasMany' => array('Child')));
2605
		$this->assertTrue($result);
2606
2607
		$result = $TestModel->find('all');
2608
		$expected = array(
2609
			array(
2610
				'Apple' => array(
2611
					'id' => 1,
2612
					'apple_id' => 2,
2613
					'color' => 'Red 1',
2614
					'name' => 'Red Apple 1',
2615
					'created' => '2006-11-22 10:38:58',
2616
					'date' => '1951-01-04',
2617
					'modified' => '2006-12-01 13:31:26',
2618
					'mytime' => '22:57:17'
2619
				),
2620
				'Parent' => array(
2621
					'id' => 2,
2622
					'apple_id' => 1,
2623
					'color' => 'Bright Red 1',
2624
					'name' => 'Bright Red Apple',
2625
					'created' => '2006-11-22 10:43:13',
2626
					'date' => '2014-01-01',
2627
					'modified' => '2006-11-30 18:38:10',
2628
					'mytime' => '22:57:17',
2629
					'Sample' => array(
2630
						'id' => 2,
2631
						'apple_id' => 2,
2632
						'name' => 'sample2'
2633
					),
2634
					'Child' => array(
2635
						array(
2636
							'id' => 1,
2637
							'apple_id' => 2,
2638
							'color' => 'Red 1',
2639
							'name' => 'Red Apple 1',
2640
							'created' => '2006-11-22 10:38:58',
2641
							'date' => '1951-01-04',
2642
							'modified' => '2006-12-01 13:31:26',
2643
							'mytime' => '22:57:17'
2644
						),
2645
						array(
2646
							'id' => 3,
2647
							'apple_id' => 2,
2648
							'color' => 'blue green',
2649
							'name' => 'green blue',
2650
							'created' => '2006-12-25 05:13:36',
2651
							'date' => '2006-12-25',
2652
							'modified' => '2006-12-25 05:23:24',
2653
							'mytime' => '22:57:17'
2654
						),
2655
						array(
2656
							'id' => 4,
2657
							'apple_id' => 2,
2658
							'color' => 'Blue Green',
2659
							'name' => 'Test Name',
2660
							'created' => '2006-12-25 05:23:36',
2661
							'date' => '2006-12-25',
2662
							'modified' => '2006-12-25 05:23:36',
2663
							'mytime' => '22:57:17'
2664
				))),
2665
				'Sample' => array(
2666
					'id' => '',
2667
					'apple_id' => '',
2668
					'name' => ''
2669
			)),
2670
			array(
2671
				'Apple' => array(
2672
					'id' => 2,
2673
					'apple_id' => 1,
2674
					'color' => 'Bright Red 1',
2675
					'name' => 'Bright Red Apple',
2676
					'created' => '2006-11-22 10:43:13',
2677
					'date' => '2014-01-01',
2678
					'modified' => '2006-11-30 18:38:10',
2679
					'mytime' => '22:57:17'
2680
				),
2681
				'Parent' => array(
2682
					'id' => 1,
2683
					'apple_id' => 2,
2684
					'color' => 'Red 1',
2685
					'name' => 'Red Apple 1',
2686
					'created' => '2006-11-22 10:38:58',
2687
					'date' => '1951-01-04',
2688
					'modified' => '2006-12-01 13:31:26',
2689
					'mytime' => '22:57:17',
2690
					'Sample' => array(),
2691
						'Child' => array(
2692
							array(
2693
								'id' => 2,
2694
								'apple_id' => 1,
2695
								'color' => 'Bright Red 1',
2696
								'name' => 'Bright Red Apple',
2697
								'created' => '2006-11-22 10:43:13',
2698
								'date' => '2014-01-01',
2699
								'modified' => '2006-11-30 18:38:10',
2700
								'mytime' => '22:57:17'
2701
				))),
2702
				'Sample' => array(
2703
					'id' => 2,
2704
					'apple_id' => 2,
2705
					'name' => 'sample2',
2706
					'Apple' => array(
2707
						'id' => 2,
2708
						'apple_id' => 1,
2709
						'color' => 'Bright Red 1',
2710
						'name' => 'Bright Red Apple',
2711
						'created' => '2006-11-22 10:43:13',
2712
						'date' => '2014-01-01',
2713
						'modified' => '2006-11-30 18:38:10',
2714
						'mytime' => '22:57:17'
2715
			))),
2716
			array(
2717
				'Apple' => array(
2718
					'id' => 3,
2719
					'apple_id' => 2,
2720
					'color' => 'blue green',
2721
					'name' => 'green blue',
2722
					'created' => '2006-12-25 05:13:36',
2723
					'date' => '2006-12-25',
2724
					'modified' => '2006-12-25 05:23:24',
2725
					'mytime' => '22:57:17'
2726
				),
2727
				'Parent' => array(
2728
					'id' => 2,
2729
					'apple_id' => 1,
2730
					'color' => 'Bright Red 1',
2731
					'name' => 'Bright Red Apple',
2732
					'created' => '2006-11-22 10:43:13',
2733
					'date' => '2014-01-01',
2734
					'modified' => '2006-11-30 18:38:10',
2735
					'mytime' => '22:57:17',
2736
					'Sample' => array(
2737
						'id' => 2,
2738
						'apple_id' => 2,
2739
						'name' => 'sample2'
2740
					),
2741
					'Child' => array(
2742
						array(
2743
							'id' => 1,
2744
							'apple_id' => 2,
2745
							'color' => 'Red 1',
2746
							'name' => 'Red Apple 1',
2747
							'created' => '2006-11-22 10:38:58',
2748
							'date' => '1951-01-04',
2749
							'modified' => '2006-12-01 13:31:26',
2750
							'mytime' => '22:57:17'
2751
						),
2752
						array(
2753
							'id' => 3,
2754
							'apple_id' => 2,
2755
							'color' => 'blue green',
2756
							'name' => 'green blue',
2757
							'created' => '2006-12-25 05:13:36',
2758
							'date' => '2006-12-25',
2759
							'modified' => '2006-12-25 05:23:24',
2760
							'mytime' => '22:57:17'
2761
						),
2762
						array(
2763
							'id' => 4,
2764
							'apple_id' => 2,
2765
							'color' => 'Blue Green',
2766
							'name' => 'Test Name',
2767
							'created' => '2006-12-25 05:23:36',
2768
							'date' => '2006-12-25',
2769
							'modified' => '2006-12-25 05:23:36',
2770
							'mytime' => '22:57:17'
2771
				))),
2772
				'Sample' => array(
2773
					'id' => 1,
2774
					'apple_id' => 3,
2775
					'name' => 'sample1',
2776
					'Apple' => array(
2777
						'id' => 3,
2778
						'apple_id' => 2,
2779
						'color' => 'blue green',
2780
						'name' => 'green blue',
2781
						'created' => '2006-12-25 05:13:36',
2782
						'date' => '2006-12-25',
2783
						'modified' => '2006-12-25 05:23:24',
2784
						'mytime' => '22:57:17'
2785
			))),
2786
			array(
2787
				'Apple' => array(
2788
					'id' => 4,
2789
					'apple_id' => 2,
2790
					'color' => 'Blue Green',
2791
					'name' => 'Test Name',
2792
					'created' => '2006-12-25 05:23:36',
2793
					'date' => '2006-12-25',
2794
					'modified' => '2006-12-25 05:23:36',
2795
					'mytime' => '22:57:17'
2796
				),
2797
				'Parent' => array(
2798
					'id' => 2,
2799
					'apple_id' => 1,
2800
					'color' => 'Bright Red 1',
2801
					'name' => 'Bright Red Apple',
2802
					'created' => '2006-11-22 10:43:13',
2803
					'date' => '2014-01-01',
2804
					'modified' => '2006-11-30 18:38:10',
2805
					'mytime' => '22:57:17',
2806
					'Sample' => array(
2807
						'id' => 2,
2808
						'apple_id' => 2,
2809
						'name' => 'sample2'
2810
					),
2811
					'Child' => array(
2812
						array(
2813
							'id' => 1,
2814
							'apple_id' => 2,
2815
							'color' => 'Red 1',
2816
							'name' => 'Red Apple 1',
2817
							'created' => '2006-11-22 10:38:58',
2818
							'date' => '1951-01-04',
2819
							'modified' => '2006-12-01 13:31:26',
2820
							'mytime' => '22:57:17'
2821
						),
2822
						array(
2823
							'id' => 3,
2824
							'apple_id' => 2,
2825
							'color' => 'blue green',
2826
							'name' => 'green blue',
2827
							'created' => '2006-12-25 05:13:36',
2828
							'date' => '2006-12-25',
2829
							'modified' => '2006-12-25 05:23:24',
2830
							'mytime' => '22:57:17'
2831
						),
2832
						array(
2833
							'id' => 4,
2834
							'apple_id' => 2,
2835
							'color' => 'Blue Green',
2836
							'name' => 'Test Name',
2837
							'created' => '2006-12-25 05:23:36',
2838
							'date' => '2006-12-25',
2839
							'modified' => '2006-12-25 05:23:36',
2840
							'mytime' => '22:57:17'
2841
				))),
2842
				'Sample' => array(
2843
					'id' => 3,
2844
					'apple_id' => 4,
2845
					'name' => 'sample3',
2846
					'Apple' => array(
2847
						'id' => 4,
2848
						'apple_id' => 2,
2849
						'color' => 'Blue Green',
2850
						'name' => 'Test Name',
2851
						'created' => '2006-12-25 05:23:36',
2852
						'date' => '2006-12-25',
2853
						'modified' => '2006-12-25 05:23:36',
2854
						'mytime' => '22:57:17'
2855
			))),
2856
			array(
2857
				'Apple' => array(
2858
					'id' => 5,
2859
					'apple_id' => 5,
2860
					'color' => 'Green',
2861
					'name' => 'Blue Green',
2862
					'created' => '2006-12-25 05:24:06',
2863
					'date' => '2006-12-25',
2864
					'modified' =>
2865
					'2006-12-25 05:29:16',
2866
					'mytime' => '22:57:17'
2867
				),
2868
				'Parent' => array(
2869
					'id' => 5,
2870
					'apple_id' => 5,
2871
					'color' => 'Green',
2872
					'name' => 'Blue Green',
2873
					'created' => '2006-12-25 05:24:06',
2874
					'date' => '2006-12-25',
2875
					'modified' => '2006-12-25 05:29:16',
2876
					'mytime' => '22:57:17',
2877
					'Sample' => array(
2878
						'id' => 4,
2879
						'apple_id' => 5,
2880
						'name' => 'sample4'
2881
					),
2882
					'Child' => array(
2883
						array(
2884
							'id' => 5,
2885
							'apple_id' => 5,
2886
							'color' => 'Green',
2887
							'name' => 'Blue Green',
2888
							'created' => '2006-12-25 05:24:06',
2889
							'date' => '2006-12-25',
2890
							'modified' => '2006-12-25 05:29:16',
2891
							'mytime' => '22:57:17'
2892
				))),
2893
				'Sample' => array(
2894
					'id' => 4,
2895
					'apple_id' => 5,
2896
					'name' => 'sample4',
2897
					'Apple' => array(
2898
						'id' => 5,
2899
						'apple_id' => 5,
2900
						'color' => 'Green',
2901
						'name' => 'Blue Green',
2902
						'created' => '2006-12-25 05:24:06',
2903
						'date' => '2006-12-25',
2904
						'modified' => '2006-12-25 05:29:16',
2905
						'mytime' => '22:57:17'
2906
			))),
2907
			array(
2908
				'Apple' => array(
2909
					'id' => 6,
2910
					'apple_id' => 4,
2911
					'color' => 'My new appleOrange',
2912
					'name' => 'My new apple',
2913
					'created' => '2006-12-25 05:29:39',
2914
					'date' => '2006-12-25',
2915
					'modified' => '2006-12-25 05:29:39',
2916
					'mytime' => '22:57:17'),
2917
					'Parent' => array(
2918
						'id' => 4,
2919
						'apple_id' => 2,
2920
						'color' => 'Blue Green',
2921
						'name' => 'Test Name',
2922
						'created' => '2006-12-25 05:23:36',
2923
						'date' => '2006-12-25',
2924
						'modified' => '2006-12-25 05:23:36',
2925
						'mytime' => '22:57:17',
2926
						'Sample' => array(
2927
							'id' => 3,
2928
							'apple_id' => 4,
2929
							'name' => 'sample3'
2930
						),
2931
						'Child' => array(
2932
							array(
2933
								'id' => 6,
2934
								'apple_id' => 4,
2935
								'color' => 'My new appleOrange',
2936
								'name' => 'My new apple',
2937
								'created' => '2006-12-25 05:29:39',
2938
								'date' => '2006-12-25',
2939
								'modified' => '2006-12-25 05:29:39',
2940
								'mytime' => '22:57:17'
2941
					))),
2942
					'Sample' => array(
2943
						'id' => '',
2944
						'apple_id' => '',
2945
						'name' => ''
2946
			)),
2947
			array(
2948
				'Apple' => array(
2949
					'id' => 7,
2950
					'apple_id' => 6,
2951
					'color' => 'Some wierd color',
2952
					'name' => 'Some odd color',
2953
					'created' => '2006-12-25 05:34:21',
2954
					'date' => '2006-12-25',
2955
					'modified' => '2006-12-25 05:34:21',
2956
					'mytime' => '22:57:17'
2957
				),
2958
				'Parent' => array(
2959
					'id' => 6,
2960
					'apple_id' => 4,
2961
					'color' => 'My new appleOrange',
2962
					'name' => 'My new apple',
2963
					'created' => '2006-12-25 05:29:39',
2964
					'date' => '2006-12-25',
2965
					'modified' => '2006-12-25 05:29:39',
2966
					'mytime' => '22:57:17',
2967
					'Sample' => array(),
2968
					'Child' => array(
2969
						array(
2970
							'id' => 7,
2971
							'apple_id' => 6,
2972
							'color' => 'Some wierd color',
2973
							'name' => 'Some odd color',
2974
							'created' => '2006-12-25 05:34:21',
2975
							'date' => '2006-12-25', 'modified' =>
2976
							'2006-12-25 05:34:21',
2977
							'mytime' => '22:57:17'
2978
				))),
2979
				'Sample' => array(
2980
					'id' => '',
2981
					'apple_id' => '',
2982
					'name' => ''
2983
		)));
2984
		$this->assertEquals($expected, $result);
2985
	}
2986
2987
/**
2988
 * testSelfAssociationAfterFind method
2989
 *
2990
 * @return void
2991
 */
2992
	public function testSelfAssociationAfterFind() {
2993
		$this->loadFixtures('Apple', 'Sample');
2994
		$afterFindModel = new NodeAfterFind();
2995
		$afterFindModel->recursive = 3;
2996
		$afterFindData = $afterFindModel->find('all');
2997
2998
		$duplicateModel = new NodeAfterFind();
2999
		$duplicateModel->recursive = 3;
3000
3001
		$noAfterFindModel = new NodeNoAfterFind();
3002
		$noAfterFindModel->recursive = 3;
3003
		$noAfterFindData = $noAfterFindModel->find('all');
3004
3005
		$this->assertFalse($afterFindModel == $noAfterFindModel);
3006
		$this->assertEquals($afterFindData, $noAfterFindData);
3007
	}
3008
3009
/**
3010
 * Test that afterFind can completely unset data.
3011
 *
3012
 * @return void
3013
 */
3014
	public function testAfterFindUnset() {
3015
		$this->loadFixtures('Article', 'Comment', 'User');
3016
		$model = new CustomArticle();
3017
		$model->bindModel(array(
3018
			'hasMany' => array(
3019
				'ModifiedComment' => array(
3020
					'className' => 'ModifiedComment',
3021
					'foreignKey' => 'article_id',
3022
				)
3023
			)
3024
		));
3025
		$model->ModifiedComment->remove = true;
3026
		$result = $model->find('all');
3027
		$this->assertTrue(
3028
			empty($result[0]['ModifiedComment']),
3029
			'Zeroith row should be removed by afterFind'
3030
		);
3031
	}
3032
3033
/**
3034
 * testFindThreadedNoParent method
3035
 *
3036
 * @return void
3037
 */
3038
	public function testFindThreadedNoParent() {
3039
		$this->loadFixtures('Apple', 'Sample');
3040
		$Apple = new Apple();
3041
		$result = $Apple->find('threaded');
3042
		$result = Hash::extract($result, '{n}.children');
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, Hash::extract() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
3043
		$expected = array(array(), array(), array(), array(), array(), array(), array());
3044
		$this->assertEquals($expected, $result);
3045
	}
3046
3047
/**
3048
 * testFindThreaded method
3049
 *
3050
 * @return void
3051
 */
3052
	public function testFindThreaded() {
3053
		$this->loadFixtures('Person');
3054
		$Model = new Person();
3055
		$Model->recursive = -1;
3056
		$result = $Model->find('threaded');
3057
		$result = Hash::extract($result, '{n}.children');
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, Hash::extract() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
3058
		$expected = array(array(), array(), array(), array(), array(), array(), array());
3059
		$this->assertEquals($expected, $result);
3060
3061
		$result = $Model->find('threaded', array('parent' => 'mother_id'));
3062
		$expected = array(
3063
			array(
3064
				'Person' => array(
3065
					'id' => '4',
3066
					'name' => 'mother - grand mother',
3067
					'mother_id' => '0',
3068
					'father_id' => '0'
3069
				),
3070
				'children' => array(
3071
					array(
3072
						'Person' => array(
3073
							'id' => '2',
3074
							'name' => 'mother',
3075
							'mother_id' => '4',
3076
							'father_id' => '5'
3077
						),
3078
						'children' => array(
3079
							array(
3080
								'Person' => array(
3081
									'id' => '1',
3082
									'name' => 'person',
3083
									'mother_id' => '2',
3084
									'father_id' => '3'
3085
								),
3086
								'children' => array()
3087
							)
3088
						)
3089
					)
3090
				)
3091
			),
3092
			array(
3093
				'Person' => array(
3094
					'id' => '5',
3095
					'name' => 'mother - grand father',
3096
					'mother_id' => '0',
3097
					'father_id' => '0'
3098
				),
3099
				'children' => array()
3100
			),
3101
			array(
3102
				'Person' => array(
3103
					'id' => '6',
3104
					'name' => 'father - grand mother',
3105
					'mother_id' => '0',
3106
					'father_id' => '0'
3107
				),
3108
				'children' => array(
3109
					array(
3110
						'Person' => array(
3111
							'id' => '3',
3112
							'name' => 'father',
3113
							'mother_id' => '6',
3114
							'father_id' => '7'
3115
						),
3116
						'children' => array()
3117
					)
3118
				)
3119
			),
3120
			array(
3121
				'Person' => array(
3122
					'id' => '7',
3123
					'name' => 'father - grand father',
3124
					'mother_id' => '0',
3125
					'father_id' => '0'
3126
				),
3127
				'children' => array()
3128
			)
3129
		);
3130
		$this->assertEquals($expected, $result);
3131
	}
3132
3133
/**
3134
 * testFindAllThreaded method
3135
 *
3136
 * @return void
3137
 */
3138
	public function testFindAllThreaded() {
3139
		$this->loadFixtures('Category');
3140
		$TestModel = new Category();
3141
3142
		$result = $TestModel->find('threaded');
3143
		$expected = array(
3144
			array(
3145
				'Category' => array(
3146
					'id' => '1',
3147
					'parent_id' => '0',
3148
					'name' => 'Category 1',
3149
					'created' => '2007-03-18 15:30:23',
3150
					'updated' => '2007-03-18 15:32:31'
3151
				),
3152
				'children' => array(
3153
					array(
3154
						'Category' => array(
3155
							'id' => '2',
3156
							'parent_id' => '1',
3157
							'name' => 'Category 1.1',
3158
							'created' => '2007-03-18 15:30:23',
3159
							'updated' => '2007-03-18 15:32:31'
3160
						),
3161
						'children' => array(
3162
							array('Category' => array(
3163
								'id' => '7',
3164
								'parent_id' => '2',
3165
								'name' => 'Category 1.1.1',
3166
								'created' => '2007-03-18 15:30:23',
3167
								'updated' => '2007-03-18 15:32:31'),
3168
								'children' => array()),
3169
							array('Category' => array(
3170
								'id' => '8',
3171
								'parent_id' => '2',
3172
								'name' => 'Category 1.1.2',
3173
								'created' => '2007-03-18 15:30:23',
3174
								'updated' => '2007-03-18 15:32:31'),
3175
								'children' => array()))
3176
					),
3177
					array(
3178
						'Category' => array(
3179
							'id' => '3',
3180
							'parent_id' => '1',
3181
							'name' => 'Category 1.2',
3182
							'created' => '2007-03-18 15:30:23',
3183
							'updated' => '2007-03-18 15:32:31'
3184
						),
3185
						'children' => array()
3186
					)
3187
				)
3188
			),
3189
			array(
3190
				'Category' => array(
3191
					'id' => '4',
3192
					'parent_id' => '0',
3193
					'name' => 'Category 2',
3194
					'created' => '2007-03-18 15:30:23',
3195
					'updated' => '2007-03-18 15:32:31'
3196
				),
3197
				'children' => array()
3198
			),
3199
			array(
3200
				'Category' => array(
3201
					'id' => '5',
3202
					'parent_id' => '0',
3203
					'name' => 'Category 3',
3204
					'created' => '2007-03-18 15:30:23',
3205
					'updated' => '2007-03-18 15:32:31'
3206
				),
3207
				'children' => array(
3208
					array(
3209
						'Category' => array(
3210
							'id' => '6',
3211
							'parent_id' => '5',
3212
							'name' => 'Category 3.1',
3213
							'created' => '2007-03-18 15:30:23',
3214
							'updated' => '2007-03-18 15:32:31'
3215
						),
3216
						'children' => array()
3217
					)
3218
				)
3219
			)
3220
		);
3221
		$this->assertEquals($expected, $result);
3222
3223
		$result = $TestModel->find('threaded', array(
3224
			'conditions' => array('Category.name LIKE' => 'Category 1%')
3225
		));
3226
3227
		$expected = array(
3228
			array(
3229
				'Category' => array(
3230
					'id' => '1',
3231
					'parent_id' => '0',
3232
					'name' => 'Category 1',
3233
					'created' => '2007-03-18 15:30:23',
3234
					'updated' => '2007-03-18 15:32:31'
3235
				),
3236
				'children' => array(
3237
					array(
3238
						'Category' => array(
3239
							'id' => '2',
3240
							'parent_id' => '1',
3241
							'name' => 'Category 1.1',
3242
							'created' => '2007-03-18 15:30:23',
3243
							'updated' => '2007-03-18 15:32:31'
3244
						),
3245
						'children' => array(
3246
							array('Category' => array(
3247
								'id' => '7',
3248
								'parent_id' => '2',
3249
								'name' => 'Category 1.1.1',
3250
								'created' => '2007-03-18 15:30:23',
3251
								'updated' => '2007-03-18 15:32:31'),
3252
								'children' => array()),
3253
							array('Category' => array(
3254
								'id' => '8',
3255
								'parent_id' => '2',
3256
								'name' => 'Category 1.1.2',
3257
								'created' => '2007-03-18 15:30:23',
3258
								'updated' => '2007-03-18 15:32:31'),
3259
								'children' => array()))
3260
					),
3261
					array(
3262
						'Category' => array(
3263
							'id' => '3',
3264
							'parent_id' => '1',
3265
							'name' => 'Category 1.2',
3266
							'created' => '2007-03-18 15:30:23',
3267
							'updated' => '2007-03-18 15:32:31'
3268
						),
3269
						'children' => array()
3270
					)
3271
				)
3272
			)
3273
		);
3274
		$this->assertEquals($expected, $result);
3275
3276
		$result = $TestModel->find('threaded', array(
3277
			'fields' => 'id, parent_id, name'
3278
		));
3279
3280
		$expected = array(
3281
			array(
3282
				'Category' => array(
3283
					'id' => '1',
3284
					'parent_id' => '0',
3285
					'name' => 'Category 1'
3286
				),
3287
				'children' => array(
3288
					array(
3289
						'Category' => array(
3290
							'id' => '2',
3291
							'parent_id' => '1',
3292
							'name' => 'Category 1.1'
3293
						),
3294
						'children' => array(
3295
							array('Category' => array(
3296
								'id' => '7',
3297
								'parent_id' => '2',
3298
								'name' => 'Category 1.1.1'),
3299
								'children' => array()),
3300
							array('Category' => array(
3301
								'id' => '8',
3302
								'parent_id' => '2',
3303
								'name' => 'Category 1.1.2'),
3304
								'children' => array()))
3305
					),
3306
					array(
3307
						'Category' => array(
3308
							'id' => '3',
3309
							'parent_id' => '1',
3310
							'name' => 'Category 1.2'
3311
						),
3312
						'children' => array()
3313
					)
3314
				)
3315
			),
3316
			array(
3317
				'Category' => array(
3318
					'id' => '4',
3319
					'parent_id' => '0',
3320
					'name' => 'Category 2'
3321
				),
3322
				'children' => array()
3323
			),
3324
			array(
3325
				'Category' => array(
3326
					'id' => '5',
3327
					'parent_id' => '0',
3328
					'name' => 'Category 3'
3329
				),
3330
				'children' => array(
3331
					array(
3332
						'Category' => array(
3333
							'id' => '6',
3334
							'parent_id' => '5',
3335
							'name' => 'Category 3.1'
3336
						),
3337
						'children' => array()
3338
					)
3339
				)
3340
			)
3341
		);
3342
		$this->assertEquals($expected, $result);
3343
3344
		$result = $TestModel->find('threaded', array('order' => 'id DESC'));
3345
3346
		$expected = array(
3347
			array(
3348
				'Category' => array(
3349
					'id' => 5,
3350
					'parent_id' => 0,
3351
					'name' => 'Category 3',
3352
					'created' => '2007-03-18 15:30:23',
3353
					'updated' => '2007-03-18 15:32:31'
3354
				),
3355
				'children' => array(
3356
					array(
3357
						'Category' => array(
3358
							'id' => 6,
3359
							'parent_id' => 5,
3360
							'name' => 'Category 3.1',
3361
							'created' => '2007-03-18 15:30:23',
3362
							'updated' => '2007-03-18 15:32:31'
3363
						),
3364
						'children' => array()
3365
					)
3366
				)
3367
			),
3368
			array(
3369
				'Category' => array(
3370
					'id' => 4,
3371
					'parent_id' => 0,
3372
					'name' => 'Category 2',
3373
					'created' => '2007-03-18 15:30:23',
3374
					'updated' => '2007-03-18 15:32:31'
3375
				),
3376
				'children' => array()
3377
			),
3378
			array(
3379
				'Category' => array(
3380
					'id' => 1,
3381
					'parent_id' => 0,
3382
					'name' => 'Category 1',
3383
					'created' => '2007-03-18 15:30:23',
3384
					'updated' => '2007-03-18 15:32:31'
3385
				),
3386
				'children' => array(
3387
					array(
3388
						'Category' => array(
3389
							'id' => 3,
3390
							'parent_id' => 1,
3391
							'name' => 'Category 1.2',
3392
							'created' => '2007-03-18 15:30:23',
3393
							'updated' => '2007-03-18 15:32:31'
3394
						),
3395
						'children' => array()
3396
					),
3397
					array(
3398
						'Category' => array(
3399
							'id' => 2,
3400
							'parent_id' => 1,
3401
							'name' => 'Category 1.1',
3402
							'created' => '2007-03-18 15:30:23',
3403
							'updated' => '2007-03-18 15:32:31'
3404
						),
3405
						'children' => array(
3406
							array('Category' => array(
3407
								'id' => '8',
3408
								'parent_id' => '2',
3409
								'name' => 'Category 1.1.2',
3410
								'created' => '2007-03-18 15:30:23',
3411
								'updated' => '2007-03-18 15:32:31'),
3412
								'children' => array()),
3413
							array('Category' => array(
3414
								'id' => '7',
3415
								'parent_id' => '2',
3416
								'name' => 'Category 1.1.1',
3417
								'created' => '2007-03-18 15:30:23',
3418
								'updated' => '2007-03-18 15:32:31'),
3419
								'children' => array()))
3420
					)
3421
				)
3422
			)
3423
		);
3424
		$this->assertEquals($expected, $result);
3425
3426
		$result = $TestModel->find('threaded', array(
3427
			'conditions' => array('Category.name LIKE' => 'Category 3%')
3428
		));
3429
		$expected = array(
3430
			array(
3431
				'Category' => array(
3432
					'id' => '5',
3433
					'parent_id' => '0',
3434
					'name' => 'Category 3',
3435
					'created' => '2007-03-18 15:30:23',
3436
					'updated' => '2007-03-18 15:32:31'
3437
				),
3438
				'children' => array(
3439
					array(
3440
						'Category' => array(
3441
							'id' => '6',
3442
							'parent_id' => '5',
3443
							'name' => 'Category 3.1',
3444
							'created' => '2007-03-18 15:30:23',
3445
							'updated' => '2007-03-18 15:32:31'
3446
						),
3447
						'children' => array()
3448
					)
3449
				)
3450
			)
3451
		);
3452
		$this->assertEquals($expected, $result);
3453
3454
		$result = $TestModel->find('threaded', array(
3455
			'conditions' => array('Category.name LIKE' => 'Category 1.1%')
3456
		));
3457
		$expected = array(
3458
				array('Category' =>
3459
					array(
3460
						'id' => '2',
3461
						'parent_id' => '1',
3462
						'name' => 'Category 1.1',
3463
						'created' => '2007-03-18 15:30:23',
3464
						'updated' => '2007-03-18 15:32:31'),
3465
						'children' => array(
3466
							array('Category' => array(
3467
								'id' => '7',
3468
								'parent_id' => '2',
3469
								'name' => 'Category 1.1.1',
3470
								'created' => '2007-03-18 15:30:23',
3471
								'updated' => '2007-03-18 15:32:31'),
3472
								'children' => array()),
3473
							array('Category' => array(
3474
								'id' => '8',
3475
								'parent_id' => '2',
3476
								'name' => 'Category 1.1.2',
3477
								'created' => '2007-03-18 15:30:23',
3478
								'updated' => '2007-03-18 15:32:31'),
3479
								'children' => array()))));
3480
		$this->assertEquals($expected, $result);
3481
3482
		$result = $TestModel->find('threaded', array(
3483
			'fields' => 'id, parent_id, name',
3484
			'conditions' => array('Category.id !=' => 2)
3485
		));
3486
		$expected = array(
3487
			array(
3488
				'Category' => array(
3489
					'id' => '1',
3490
					'parent_id' => '0',
3491
					'name' => 'Category 1'
3492
				),
3493
				'children' => array(
3494
					array(
3495
						'Category' => array(
3496
							'id' => '3',
3497
							'parent_id' => '1',
3498
							'name' => 'Category 1.2'
3499
						),
3500
						'children' => array()
3501
					)
3502
				)
3503
			),
3504
			array(
3505
				'Category' => array(
3506
					'id' => '4',
3507
					'parent_id' => '0',
3508
					'name' => 'Category 2'
3509
				),
3510
				'children' => array()
3511
			),
3512
			array(
3513
				'Category' => array(
3514
					'id' => '5',
3515
					'parent_id' => '0',
3516
					'name' => 'Category 3'
3517
				),
3518
				'children' => array(
3519
					array(
3520
						'Category' => array(
3521
							'id' => '6',
3522
							'parent_id' => '5',
3523
							'name' => 'Category 3.1'
3524
						),
3525
						'children' => array()
3526
					)
3527
				)
3528
			)
3529
		);
3530
		$this->assertEquals($expected, $result);
3531
3532
		$result = $TestModel->find('all', array(
3533
			'fields' => 'id, name, parent_id',
3534
			'conditions' => array('Category.id !=' => 1)
3535
		));
3536
		$expected = array(
3537
			array('Category' => array(
3538
				'id' => '2',
3539
				'name' => 'Category 1.1',
3540
				'parent_id' => '1'
3541
			)),
3542
			array('Category' => array(
3543
				'id' => '3',
3544
				'name' => 'Category 1.2',
3545
				'parent_id' => '1'
3546
			)),
3547
			array('Category' => array(
3548
				'id' => '4',
3549
				'name' => 'Category 2',
3550
				'parent_id' => '0'
3551
			)),
3552
			array('Category' => array(
3553
				'id' => '5',
3554
				'name' => 'Category 3',
3555
				'parent_id' => '0'
3556
			)),
3557
			array('Category' => array(
3558
				'id' => '6',
3559
				'name' => 'Category 3.1',
3560
				'parent_id' => '5'
3561
			)),
3562
			array('Category' => array(
3563
				'id' => '7',
3564
				'name' => 'Category 1.1.1',
3565
				'parent_id' => '2'
3566
			)),
3567
			array('Category' => array(
3568
				'id' => '8',
3569
				'name' => 'Category 1.1.2',
3570
				'parent_id' => '2'
3571
		)));
3572
		$this->assertEquals($expected, $result);
3573
3574
		$result = $TestModel->find('threaded', array(
3575
			'fields' => 'id, parent_id, name',
3576
			'conditions' => array('Category.id !=' => 1)
3577
		));
3578
		$expected = array(
3579
			array(
3580
				'Category' => array(
3581
					'id' => '2',
3582
					'parent_id' => '1',
3583
					'name' => 'Category 1.1'
3584
				),
3585
				'children' => array(
3586
					array('Category' => array(
3587
						'id' => '7',
3588
						'parent_id' => '2',
3589
						'name' => 'Category 1.1.1'),
3590
						'children' => array()),
3591
					array('Category' => array(
3592
						'id' => '8',
3593
						'parent_id' => '2',
3594
						'name' => 'Category 1.1.2'),
3595
						'children' => array()))
3596
			),
3597
			array(
3598
				'Category' => array(
3599
					'id' => '3',
3600
					'parent_id' => '1',
3601
					'name' => 'Category 1.2'
3602
				),
3603
				'children' => array()
3604
			)
3605
		);
3606
		$this->assertEquals($expected, $result);
3607
	}
3608
3609
/**
3610
 * test find('neighbors')
3611
 *
3612
 * @return void
3613
 */
3614
	public function testFindNeighbors() {
3615
		$this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment');
3616
		$TestModel = new Article();
3617
3618
		$TestModel->id = 1;
3619
		$result = $TestModel->find('neighbors', array('fields' => array('id')));
3620
3621
		$this->assertNull($result['prev']);
3622
		$this->assertEquals(array('id' => 2), $result['next']['Article']);
3623
		$this->assertEquals(2, count($result['next']['Comment']));
3624
		$this->assertEquals(2, count($result['next']['Tag']));
3625
3626
		$TestModel->id = 2;
3627
		$TestModel->recursive = 0;
3628
		$result = $TestModel->find('neighbors', array(
3629
			'fields' => array('id')
3630
		));
3631
3632
		$expected = array(
3633
			'prev' => array(
3634
				'Article' => array(
3635
					'id' => 1
3636
			)),
3637
			'next' => array(
3638
				'Article' => array(
3639
					'id' => 3
3640
		)));
3641
		$this->assertEquals($expected, $result);
3642
3643
		$TestModel->id = 3;
3644
		$TestModel->recursive = 1;
3645
		$result = $TestModel->find('neighbors', array('fields' => array('id')));
3646
3647
		$this->assertNull($result['next']);
3648
		$this->assertEquals(array('id' => 2), $result['prev']['Article']);
3649
		$this->assertEquals(2, count($result['prev']['Comment']));
3650
		$this->assertEquals(2, count($result['prev']['Tag']));
3651
3652
		$TestModel->id = 1;
3653
		$result = $TestModel->find('neighbors', array('recursive' => -1));
3654
		$expected = array(
3655
			'prev' => null,
3656
			'next' => array(
3657
				'Article' => array(
3658
					'id' => 2,
3659
					'user_id' => 3,
3660
					'title' => 'Second Article',
3661
					'body' => 'Second Article Body',
3662
					'published' => 'Y',
3663
					'created' => '2007-03-18 10:41:23',
3664
					'updated' => '2007-03-18 10:43:31'
3665
				)
3666
			)
3667
		);
3668
		$this->assertEquals($expected, $result);
3669
3670
		$TestModel->id = 2;
3671
		$result = $TestModel->find('neighbors', array('recursive' => -1));
3672
		$expected = array(
3673
			'prev' => array(
3674
				'Article' => array(
3675
					'id' => 1,
3676
					'user_id' => 1,
3677
					'title' => 'First Article',
3678
					'body' => 'First Article Body',
3679
					'published' => 'Y',
3680
					'created' => '2007-03-18 10:39:23',
3681
					'updated' => '2007-03-18 10:41:31'
3682
				)
3683
			),
3684
			'next' => array(
3685
				'Article' => array(
3686
					'id' => 3,
3687
					'user_id' => 1,
3688
					'title' => 'Third Article',
3689
					'body' => 'Third Article Body',
3690
					'published' => 'Y',
3691
					'created' => '2007-03-18 10:43:23',
3692
					'updated' => '2007-03-18 10:45:31'
3693
				)
3694
			)
3695
		);
3696
		$this->assertEquals($expected, $result);
3697
3698
		$TestModel->id = 3;
3699
		$result = $TestModel->find('neighbors', array('recursive' => -1));
3700
		$expected = array(
3701
			'prev' => array(
3702
				'Article' => array(
3703
					'id' => 2,
3704
					'user_id' => 3,
3705
					'title' => 'Second Article',
3706
					'body' => 'Second Article Body',
3707
					'published' => 'Y',
3708
					'created' => '2007-03-18 10:41:23',
3709
					'updated' => '2007-03-18 10:43:31'
3710
				)
3711
			),
3712
			'next' => null
3713
		);
3714
		$this->assertEquals($expected, $result);
3715
3716
		$TestModel->recursive = 0;
3717
		$TestModel->id = 1;
3718
		$one = $TestModel->read();
3719
		$TestModel->id = 2;
3720
		$two = $TestModel->read();
3721
		$TestModel->id = 3;
3722
		$three = $TestModel->read();
3723
3724
		$TestModel->id = 1;
3725
		$result = $TestModel->find('neighbors');
3726
		$expected = array('prev' => null, 'next' => $two);
3727
		$this->assertEquals($expected, $result);
3728
3729
		$TestModel->id = 2;
3730
		$result = $TestModel->find('neighbors');
3731
		$expected = array('prev' => $one, 'next' => $three);
3732
		$this->assertEquals($expected, $result);
3733
3734
		$TestModel->id = 3;
3735
		$result = $TestModel->find('neighbors');
3736
		$expected = array('prev' => $two, 'next' => null);
3737
		$this->assertEquals($expected, $result);
3738
3739
		$TestModel->recursive = 2;
3740
		$TestModel->id = 1;
3741
		$one = $TestModel->read();
3742
		$TestModel->id = 2;
3743
		$two = $TestModel->read();
3744
		$TestModel->id = 3;
3745
		$three = $TestModel->read();
3746
3747
		$TestModel->id = 1;
3748
		$result = $TestModel->find('neighbors', array('recursive' => 2));
3749
		$expected = array('prev' => null, 'next' => $two);
3750
		$this->assertEquals($expected, $result);
3751
3752
		$TestModel->id = 2;
3753
		$result = $TestModel->find('neighbors', array('recursive' => 2));
3754
		$expected = array('prev' => $one, 'next' => $three);
3755
		$this->assertEquals($expected, $result);
3756
3757
		$TestModel->id = 3;
3758
		$result = $TestModel->find('neighbors', array('recursive' => 2));
3759
		$expected = array('prev' => $two, 'next' => null);
3760
		$this->assertEquals($expected, $result);
3761
	}
3762
3763
/**
3764
 * Test find(neighbors) with missing fields so no neighbors are found.
3765
 *
3766
 * @return void
3767
 */
3768
	public function testFindNeighborsNoPrev() {
3769
		$this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment');
3770
		$Article = new Article();
3771
3772
		$result = $Article->find('neighbors', array(
3773
			'field' => 'Article.title',
3774
			'value' => 'Second Article',
3775
			'fields' => array('id'),
3776
			'conditions' => array(
3777
				'Article.title LIKE' => '%Article%'
3778
			),
3779
			'recursive' => 0,
3780
		));
3781
		$expected = array(
3782
			'prev' => null,
3783
			'next' => null
3784
		);
3785
		$this->assertEquals($expected, $result);
3786
	}
3787
3788
/**
3789
 * testFindCombinedRelations method
3790
 *
3791
 * @return void
3792
 */
3793
	public function testFindCombinedRelations() {
3794
		$this->skipIf($this->db instanceof Sqlserver, 'The test of testRecursiveUnbind test is not compatible with SQL Server, because it check for time columns.');
3795
3796
		$this->loadFixtures('Apple', 'Sample');
3797
		$TestModel = new Apple();
3798
3799
		$result = $TestModel->find('all');
3800
3801
		$expected = array(
3802
			array(
3803
				'Apple' => array(
3804
					'id' => '1',
3805
					'apple_id' => '2',
3806
					'color' => 'Red 1',
3807
					'name' => 'Red Apple 1',
3808
					'created' => '2006-11-22 10:38:58',
3809
					'date' => '1951-01-04',
3810
					'modified' => '2006-12-01 13:31:26',
3811
					'mytime' => '22:57:17'
3812
				),
3813
				'Parent' => array(
3814
					'id' => '2',
3815
					'apple_id' => '1',
3816
					'color' => 'Bright Red 1',
3817
					'name' => 'Bright Red Apple',
3818
					'created' => '2006-11-22 10:43:13',
3819
					'date' => '2014-01-01',
3820
					'modified' => '2006-11-30 18:38:10',
3821
					'mytime' => '22:57:17'
3822
				),
3823
				'Sample' => array(
3824
					'id' => null,
3825
					'apple_id' => null,
3826
					'name' => null
3827
				),
3828
				'Child' => array(
3829
					array(
3830
						'id' => '2',
3831
						'apple_id' => '1',
3832
						'color' => 'Bright Red 1',
3833
						'name' => 'Bright Red Apple',
3834
						'created' => '2006-11-22 10:43:13',
3835
						'date' => '2014-01-01',
3836
						'modified' => '2006-11-30 18:38:10',
3837
						'mytime' => '22:57:17'
3838
			))),
3839
			array(
3840
				'Apple' => array(
3841
					'id' => '2',
3842
					'apple_id' => '1',
3843
					'color' => 'Bright Red 1',
3844
					'name' => 'Bright Red Apple',
3845
					'created' => '2006-11-22 10:43:13',
3846
					'date' => '2014-01-01',
3847
					'modified' => '2006-11-30 18:38:10',
3848
					'mytime' => '22:57:17'
3849
				),
3850
				'Parent' => array(
3851
					'id' => '1',
3852
					'apple_id' => '2',
3853
					'color' => 'Red 1',
3854
					'name' => 'Red Apple 1',
3855
					'created' => '2006-11-22 10:38:58',
3856
					'date' => '1951-01-04',
3857
					'modified' => '2006-12-01 13:31:26',
3858
					'mytime' => '22:57:17'
3859
				),
3860
				'Sample' => array(
3861
					'id' => '2',
3862
					'apple_id' => '2',
3863
					'name' => 'sample2'
3864
				),
3865
				'Child' => array(
3866
					array(
3867
						'id' => '1',
3868
						'apple_id' => '2',
3869
						'color' => 'Red 1',
3870
						'name' => 'Red Apple 1',
3871
						'created' => '2006-11-22 10:38:58',
3872
						'date' => '1951-01-04',
3873
						'modified' => '2006-12-01 13:31:26',
3874
						'mytime' => '22:57:17'
3875
					),
3876
					array(
3877
						'id' => '3',
3878
						'apple_id' => '2',
3879
						'color' => 'blue green',
3880
						'name' => 'green blue',
3881
						'created' => '2006-12-25 05:13:36',
3882
						'date' => '2006-12-25',
3883
						'modified' => '2006-12-25 05:23:24',
3884
						'mytime' => '22:57:17'
3885
					),
3886
					array(
3887
						'id' => '4',
3888
						'apple_id' => '2',
3889
						'color' => 'Blue Green',
3890
						'name' => 'Test Name',
3891
						'created' => '2006-12-25 05:23:36',
3892
						'date' => '2006-12-25',
3893
						'modified' => '2006-12-25 05:23:36',
3894
						'mytime' => '22:57:17'
3895
			))),
3896
			array(
3897
				'Apple' => array(
3898
					'id' => '3',
3899
					'apple_id' => '2',
3900
					'color' => 'blue green',
3901
					'name' => 'green blue',
3902
					'created' => '2006-12-25 05:13:36',
3903
					'date' => '2006-12-25',
3904
					'modified' => '2006-12-25 05:23:24',
3905
					'mytime' => '22:57:17'
3906
				),
3907
				'Parent' => array(
3908
					'id' => '2',
3909
					'apple_id' => '1',
3910
					'color' => 'Bright Red 1',
3911
					'name' => 'Bright Red Apple',
3912
					'created' => '2006-11-22 10:43:13',
3913
					'date' => '2014-01-01',
3914
					'modified' => '2006-11-30 18:38:10',
3915
					'mytime' => '22:57:17'
3916
				),
3917
				'Sample' => array(
3918
					'id' => '1',
3919
					'apple_id' => '3',
3920
					'name' => 'sample1'
3921
				),
3922
				'Child' => array()
3923
			),
3924
			array(
3925
				'Apple' => array(
3926
					'id' => '4',
3927
					'apple_id' => '2',
3928
					'color' => 'Blue Green',
3929
					'name' => 'Test Name',
3930
					'created' => '2006-12-25 05:23:36',
3931
					'date' => '2006-12-25',
3932
					'modified' => '2006-12-25 05:23:36',
3933
					'mytime' => '22:57:17'
3934
				),
3935
				'Parent' => array(
3936
					'id' => '2',
3937
					'apple_id' => '1',
3938
					'color' => 'Bright Red 1',
3939
					'name' => 'Bright Red Apple',
3940
					'created' => '2006-11-22 10:43:13',
3941
					'date' => '2014-01-01',
3942
					'modified' => '2006-11-30 18:38:10',
3943
					'mytime' => '22:57:17'
3944
				),
3945
				'Sample' => array(
3946
					'id' => '3',
3947
					'apple_id' => '4',
3948
					'name' => 'sample3'
3949
				),
3950
				'Child' => array(
3951
					array(
3952
						'id' => '6',
3953
						'apple_id' => '4',
3954
						'color' => 'My new appleOrange',
3955
						'name' => 'My new apple',
3956
						'created' => '2006-12-25 05:29:39',
3957
						'date' => '2006-12-25',
3958
						'modified' => '2006-12-25 05:29:39',
3959
						'mytime' => '22:57:17'
3960
			))),
3961
			array(
3962
				'Apple' => array(
3963
					'id' => '5',
3964
					'apple_id' => '5',
3965
					'color' => 'Green',
3966
					'name' => 'Blue Green',
3967
					'created' => '2006-12-25 05:24:06',
3968
					'date' => '2006-12-25',
3969
					'modified' => '2006-12-25 05:29:16',
3970
					'mytime' => '22:57:17'
3971
				),
3972
				'Parent' => array(
3973
					'id' => '5',
3974
					'apple_id' => '5',
3975
					'color' => 'Green',
3976
					'name' => 'Blue Green',
3977
					'created' => '2006-12-25 05:24:06',
3978
					'date' => '2006-12-25',
3979
					'modified' => '2006-12-25 05:29:16',
3980
					'mytime' => '22:57:17'
3981
				),
3982
				'Sample' => array(
3983
					'id' => '4',
3984
					'apple_id' => '5',
3985
					'name' => 'sample4'
3986
				),
3987
				'Child' => array(
3988
					array(
3989
						'id' => '5',
3990
						'apple_id' => '5',
3991
						'color' => 'Green',
3992
						'name' => 'Blue Green',
3993
						'created' => '2006-12-25 05:24:06',
3994
						'date' => '2006-12-25',
3995
						'modified' => '2006-12-25 05:29:16',
3996
						'mytime' => '22:57:17'
3997
			))),
3998
			array(
3999
				'Apple' => array(
4000
					'id' => '6',
4001
					'apple_id' => '4',
4002
					'color' => 'My new appleOrange',
4003
					'name' => 'My new apple',
4004
					'created' => '2006-12-25 05:29:39',
4005
					'date' => '2006-12-25',
4006
					'modified' => '2006-12-25 05:29:39',
4007
					'mytime' => '22:57:17'
4008
				),
4009
				'Parent' => array(
4010
					'id' => '4',
4011
					'apple_id' => '2',
4012
					'color' => 'Blue Green',
4013
					'name' => 'Test Name',
4014
					'created' => '2006-12-25 05:23:36',
4015
					'date' => '2006-12-25',
4016
					'modified' => '2006-12-25 05:23:36',
4017
					'mytime' => '22:57:17'
4018
				),
4019
				'Sample' => array(
4020
					'id' => null,
4021
					'apple_id' => null,
4022
					'name' => null
4023
				),
4024
				'Child' => array(
4025
					array(
4026
						'id' => '7',
4027
						'apple_id' => '6',
4028
						'color' => 'Some wierd color',
4029
						'name' => 'Some odd color',
4030
						'created' => '2006-12-25 05:34:21',
4031
						'date' => '2006-12-25',
4032
						'modified' => '2006-12-25 05:34:21',
4033
						'mytime' => '22:57:17'
4034
			))),
4035
			array(
4036
				'Apple' => array(
4037
					'id' => '7',
4038
					'apple_id' => '6',
4039
					'color' => 'Some wierd color',
4040
					'name' => 'Some odd color',
4041
					'created' => '2006-12-25 05:34:21',
4042
					'date' => '2006-12-25',
4043
					'modified' => '2006-12-25 05:34:21',
4044
					'mytime' => '22:57:17'
4045
				),
4046
				'Parent' => array(
4047
					'id' => '6',
4048
					'apple_id' => '4',
4049
					'color' => 'My new appleOrange',
4050
					'name' => 'My new apple',
4051
					'created' => '2006-12-25 05:29:39',
4052
					'date' => '2006-12-25',
4053
					'modified' => '2006-12-25 05:29:39',
4054
					'mytime' => '22:57:17'
4055
				),
4056
				'Sample' => array(
4057
					'id' => null,
4058
					'apple_id' => null,
4059
					'name' => null
4060
				),
4061
				'Child' => array()
4062
		));
4063
		$this->assertEquals($expected, $result);
4064
	}
4065
4066
/**
4067
 * testSaveEmpty method
4068
 *
4069
 * @return void
4070
 */
4071
	public function testSaveEmpty() {
4072
		$this->loadFixtures('Thread');
4073
		$TestModel = new Thread();
4074
		$data = array();
4075
		$expected = $TestModel->save($data);
4076
		$this->assertFalse($expected);
4077
	}
4078
4079
/**
4080
 * testFindAllWithConditionInChildQuery
4081
 *
4082
 * @return void
4083
 */
4084
	public function testFindAllWithConditionInChildQuery() {
4085
		$this->loadFixtures('Basket', 'FilmFile');
4086
4087
		$TestModel = new Basket();
4088
		$recursive = 3;
4089
		$result = $TestModel->find('all', compact('recursive'));
4090
4091
		$expected = array(
4092
			array(
4093
				'Basket' => array(
4094
					'id' => 1,
4095
					'type' => 'nonfile',
4096
					'name' => 'basket1',
4097
					'object_id' => 1,
4098
					'user_id' => 1,
4099
				),
4100
				'FilmFile' => array(
4101
					'id' => '',
4102
					'name' => '',
4103
				)
4104
			),
4105
			array(
4106
				'Basket' => array(
4107
					'id' => 2,
4108
					'type' => 'file',
4109
					'name' => 'basket2',
4110
					'object_id' => 2,
4111
					'user_id' => 1,
4112
				),
4113
				'FilmFile' => array(
4114
					'id' => 2,
4115
					'name' => 'two',
4116
				)
4117
			),
4118
		);
4119
		$this->assertEquals($expected, $result);
4120
	}
4121
4122
/**
4123
 * testFindAllWithConditionsHavingMixedDataTypes method
4124
 *
4125
 * @return void
4126
 */
4127
	public function testFindAllWithConditionsHavingMixedDataTypes() {
4128
		$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
4129
		$TestModel = new Article();
4130
		$expected = array(
4131
			array(
4132
				'Article' => array(
4133
					'id' => 1,
4134
					'user_id' => 1,
4135
					'title' => 'First Article',
4136
					'body' => 'First Article Body',
4137
					'published' => 'Y',
4138
					'created' => '2007-03-18 10:39:23',
4139
					'updated' => '2007-03-18 10:41:31'
4140
				)
4141
			),
4142
			array(
4143
				'Article' => array(
4144
					'id' => 2,
4145
					'user_id' => 3,
4146
					'title' => 'Second Article',
4147
					'body' => 'Second Article Body',
4148
					'published' => 'Y',
4149
					'created' => '2007-03-18 10:41:23',
4150
					'updated' => '2007-03-18 10:43:31'
4151
				)
4152
			)
4153
		);
4154
		$conditions = array('id' => array('1', 2));
4155
		$recursive = -1;
4156
		$order = 'Article.id ASC';
4157
		$result = $TestModel->find('all', compact('conditions', 'recursive', 'order'));
4158
		$this->assertEquals($expected, $result);
4159
4160
		$this->skipIf($this->db instanceof Postgres, 'The rest of testFindAllWithConditionsHavingMixedDataTypes test is not compatible with Postgres.');
4161
4162
		$conditions = array('id' => array('1', 2, '3.0'));
4163
		$order = 'Article.id ASC';
4164
		$result = $TestModel->find('all', compact('recursive', 'conditions', 'order'));
4165
		$expected = array(
4166
			array(
4167
				'Article' => array(
4168
					'id' => 1,
4169
					'user_id' => 1,
4170
					'title' => 'First Article',
4171
					'body' => 'First Article Body',
4172
					'published' => 'Y',
4173
					'created' => '2007-03-18 10:39:23',
4174
					'updated' => '2007-03-18 10:41:31'
4175
				)
4176
			),
4177
			array(
4178
				'Article' => array(
4179
					'id' => 2,
4180
					'user_id' => 3,
4181
					'title' => 'Second Article',
4182
					'body' => 'Second Article Body',
4183
					'published' => 'Y',
4184
					'created' => '2007-03-18 10:41:23',
4185
					'updated' => '2007-03-18 10:43:31'
4186
				)
4187
			),
4188
			array(
4189
				'Article' => array(
4190
					'id' => 3,
4191
					'user_id' => 1,
4192
					'title' => 'Third Article',
4193
					'body' => 'Third Article Body',
4194
					'published' => 'Y',
4195
					'created' => '2007-03-18 10:43:23',
4196
					'updated' => '2007-03-18 10:45:31'
4197
				)
4198
			)
4199
		);
4200
		$this->assertEquals($expected, $result);
4201
	}
4202
4203
/**
4204
 * testBindUnbind method
4205
 *
4206
 * @return void
4207
 */
4208
	public function testBindUnbind() {
4209
		$this->loadFixtures(
4210
			'User',
4211
			'Comment',
4212
			'FeatureSet',
4213
			'DeviceType',
4214
			'DeviceTypeCategory',
4215
			'ExteriorTypeCategory',
4216
			'Device',
4217
			'Document',
4218
			'DocumentDirectory'
4219
		);
4220
		$TestModel = new User();
4221
4222
		$result = $TestModel->hasMany;
4223
		$expected = array();
4224
		$this->assertEquals($expected, $result);
4225
4226
		$result = $TestModel->bindModel(array('hasMany' => array('Comment')));
4227
		$this->assertTrue($result);
4228
4229
		$result = $TestModel->find('all', array(
4230
			'fields' => 'User.id, User.user',
4231
			'order' => array('User.id' => 'ASC'),
4232
		));
4233
		$expected = array(
4234
			array(
4235
				'User' => array(
4236
					'id' => '1',
4237
					'user' => 'mariano'
4238
				),
4239
				'Comment' => array(
4240
					array(
4241
						'id' => '3',
4242
						'article_id' => '1',
4243
						'user_id' => '1',
4244
						'comment' => 'Third Comment for First Article',
4245
						'published' => 'Y',
4246
						'created' => '2007-03-18 10:49:23',
4247
						'updated' => '2007-03-18 10:51:31'
4248
					),
4249
					array(
4250
						'id' => '4',
4251
						'article_id' => '1',
4252
						'user_id' => '1',
4253
						'comment' => 'Fourth Comment for First Article',
4254
						'published' => 'N',
4255
						'created' => '2007-03-18 10:51:23',
4256
						'updated' => '2007-03-18 10:53:31'
4257
					),
4258
					array(
4259
						'id' => '5',
4260
						'article_id' => '2',
4261
						'user_id' => '1',
4262
						'comment' => 'First Comment for Second Article',
4263
						'published' => 'Y',
4264
						'created' => '2007-03-18 10:53:23',
4265
						'updated' => '2007-03-18 10:55:31'
4266
			))),
4267
			array(
4268
				'User' => array(
4269
					'id' => '2',
4270
					'user' => 'nate'
4271
				),
4272
				'Comment' => array(
4273
					array(
4274
						'id' => '1',
4275
						'article_id' => '1',
4276
						'user_id' => '2',
4277
						'comment' => 'First Comment for First Article',
4278
						'published' => 'Y',
4279
						'created' => '2007-03-18 10:45:23',
4280
						'updated' => '2007-03-18 10:47:31'
4281
					),
4282
					array(
4283
						'id' => '6',
4284
						'article_id' => '2',
4285
						'user_id' => '2',
4286
						'comment' => 'Second Comment for Second Article',
4287
						'published' => 'Y',
4288
						'created' => '2007-03-18 10:55:23',
4289
						'updated' => '2007-03-18 10:57:31'
4290
			))),
4291
			array(
4292
				'User' => array(
4293
					'id' => '3',
4294
					'user' => 'larry'
4295
				),
4296
				'Comment' => array()
4297
			),
4298
			array(
4299
				'User' => array(
4300
					'id' => '4',
4301
					'user' => 'garrett'
4302
				),
4303
				'Comment' => array(
4304
					array(
4305
						'id' => '2',
4306
						'article_id' => '1',
4307
						'user_id' => '4',
4308
						'comment' => 'Second Comment for First Article',
4309
						'published' => 'Y',
4310
						'created' => '2007-03-18 10:47:23',
4311
						'updated' => '2007-03-18 10:49:31'
4312
		))));
4313
4314
		$this->assertEquals($expected, $result);
4315
4316
		$TestModel->resetAssociations();
4317
		$result = $TestModel->hasMany;
4318
		$this->assertSame(array(), $result);
4319
4320
		$result = $TestModel->bindModel(array('hasMany' => array('Comment')), false);
4321
		$this->assertTrue($result);
4322
4323
		$result = $TestModel->find('all', array(
4324
			'fields' => 'User.id, User.user',
4325
			'order' => array('User.id' => 'ASC'),
4326
		));
4327
4328
		$expected = array(
4329
			array(
4330
				'User' => array(
4331
					'id' => '1',
4332
					'user' => 'mariano'
4333
				),
4334
				'Comment' => array(
4335
					array(
4336
						'id' => '3',
4337
						'article_id' => '1',
4338
						'user_id' => '1',
4339
						'comment' => 'Third Comment for First Article',
4340
						'published' => 'Y',
4341
						'created' => '2007-03-18 10:49:23',
4342
						'updated' => '2007-03-18 10:51:31'
4343
					),
4344
					array(
4345
						'id' => '4',
4346
						'article_id' => '1',
4347
						'user_id' => '1',
4348
						'comment' => 'Fourth Comment for First Article',
4349
						'published' => 'N',
4350
						'created' => '2007-03-18 10:51:23',
4351
						'updated' => '2007-03-18 10:53:31'
4352
					),
4353
					array(
4354
						'id' => '5',
4355
						'article_id' => '2',
4356
						'user_id' => '1',
4357
						'comment' => 'First Comment for Second Article',
4358
						'published' => 'Y',
4359
						'created' => '2007-03-18 10:53:23',
4360
						'updated' => '2007-03-18 10:55:31'
4361
			))),
4362
			array(
4363
				'User' => array(
4364
					'id' => '2',
4365
					'user' => 'nate'
4366
				),
4367
				'Comment' => array(
4368
					array(
4369
						'id' => '1',
4370
						'article_id' => '1',
4371
						'user_id' => '2',
4372
						'comment' => 'First Comment for First Article',
4373
						'published' => 'Y',
4374
						'created' => '2007-03-18 10:45:23',
4375
						'updated' => '2007-03-18 10:47:31'
4376
					),
4377
					array(
4378
						'id' => '6',
4379
						'article_id' => '2',
4380
						'user_id' => '2',
4381
						'comment' => 'Second Comment for Second Article',
4382
						'published' => 'Y',
4383
						'created' => '2007-03-18 10:55:23',
4384
						'updated' => '2007-03-18 10:57:31'
4385
			))),
4386
			array(
4387
				'User' => array(
4388
					'id' => '3',
4389
					'user' => 'larry'
4390
				),
4391
				'Comment' => array()
4392
			),
4393
			array(
4394
				'User' => array(
4395
					'id' => '4',
4396
					'user' => 'garrett'
4397
				),
4398
				'Comment' => array(
4399
					array(
4400
						'id' => '2',
4401
						'article_id' => '1',
4402
						'user_id' => '4',
4403
						'comment' => 'Second Comment for First Article',
4404
						'published' => 'Y',
4405
						'created' => '2007-03-18 10:47:23',
4406
						'updated' => '2007-03-18 10:49:31'
4407
		))));
4408
4409
		$this->assertEquals($expected, $result);
4410
4411
		$result = $TestModel->hasMany;
4412
		$expected = array(
4413
			'Comment' => array(
4414
				'className' => 'Comment',
4415
				'foreignKey' => 'user_id',
4416
				'conditions' => null,
4417
				'fields' => null,
4418
				'order' => null,
4419
				'limit' => null,
4420
				'offset' => null,
4421
				'dependent' => null,
4422
				'exclusive' => null,
4423
				'finderQuery' => null,
4424
				'counterQuery' => null
4425
		));
4426
		$this->assertEquals($expected, $result);
4427
4428
		$result = $TestModel->unbindModel(array('hasMany' => array('Comment')));
4429
		$this->assertTrue($result);
4430
4431
		$result = $TestModel->hasMany;
4432
		$expected = array();
4433
		$this->assertEquals($expected, $result);
4434
4435
		$result = $TestModel->find('all', array(
4436
			'fields' => 'User.id, User.user',
4437
			'order' => array('User.id' => 'ASC'),
4438
		));
4439
		$expected = array(
4440
			array('User' => array('id' => '1', 'user' => 'mariano')),
4441
			array('User' => array('id' => '2', 'user' => 'nate')),
4442
			array('User' => array('id' => '3', 'user' => 'larry')),
4443
			array('User' => array('id' => '4', 'user' => 'garrett')));
4444
		$this->assertEquals($expected, $result);
4445
4446
		$result = $TestModel->find('all', array(
4447
			'fields' => 'User.id, User.user',
4448
			'order' => array('User.id' => 'ASC'),
4449
		));
4450
		$expected = array(
4451
			array(
4452
				'User' => array(
4453
					'id' => '1',
4454
					'user' => 'mariano'
4455
				),
4456
				'Comment' => array(
4457
					array(
4458
						'id' => '3',
4459
						'article_id' => '1',
4460
						'user_id' => '1',
4461
						'comment' => 'Third Comment for First Article',
4462
						'published' => 'Y',
4463
						'created' => '2007-03-18 10:49:23',
4464
						'updated' => '2007-03-18 10:51:31'
4465
					),
4466
					array(
4467
						'id' => '4',
4468
						'article_id' => '1',
4469
						'user_id' => '1',
4470
						'comment' => 'Fourth Comment for First Article',
4471
						'published' => 'N',
4472
						'created' => '2007-03-18 10:51:23',
4473
						'updated' => '2007-03-18 10:53:31'
4474
					),
4475
					array(
4476
						'id' => '5',
4477
						'article_id' => '2',
4478
						'user_id' => '1',
4479
						'comment' => 'First Comment for Second Article',
4480
						'published' => 'Y',
4481
						'created' => '2007-03-18 10:53:23',
4482
						'updated' => '2007-03-18 10:55:31'
4483
			))),
4484
			array(
4485
				'User' => array(
4486
					'id' => '2',
4487
					'user' => 'nate'
4488
				),
4489
				'Comment' => array(
4490
					array(
4491
						'id' => '1',
4492
						'article_id' => '1',
4493
						'user_id' => '2',
4494
						'comment' => 'First Comment for First Article',
4495
						'published' => 'Y',
4496
						'created' => '2007-03-18 10:45:23',
4497
						'updated' => '2007-03-18 10:47:31'
4498
					),
4499
					array(
4500
						'id' => '6',
4501
						'article_id' => '2',
4502
						'user_id' => '2',
4503
						'comment' => 'Second Comment for Second Article',
4504
						'published' => 'Y',
4505
						'created' => '2007-03-18 10:55:23',
4506
						'updated' => '2007-03-18 10:57:31'
4507
			))),
4508
			array(
4509
				'User' => array(
4510
					'id' => '3',
4511
					'user' => 'larry'
4512
				),
4513
				'Comment' => array()
4514
			),
4515
			array(
4516
				'User' => array(
4517
					'id' => '4',
4518
					'user' => 'garrett'
4519
				),
4520
				'Comment' => array(
4521
					array(
4522
						'id' => '2',
4523
						'article_id' => '1',
4524
						'user_id' => '4',
4525
						'comment' =>
4526
						'Second Comment for First Article',
4527
						'published' => 'Y',
4528
						'created' => '2007-03-18 10:47:23',
4529
						'updated' => '2007-03-18 10:49:31'
4530
		))));
4531
		$this->assertEquals($expected, $result);
4532
4533
		$result = $TestModel->unbindModel(array('hasMany' => array('Comment')), false);
4534
		$this->assertTrue($result);
4535
4536
		$result = $TestModel->find('all', array(
4537
			'fields' => 'User.id, User.user',
4538
			'order' => array('User.id' => 'ASC'),
4539
		));
4540
		$expected = array(
4541
			array('User' => array('id' => '1', 'user' => 'mariano')),
4542
			array('User' => array('id' => '2', 'user' => 'nate')),
4543
			array('User' => array('id' => '3', 'user' => 'larry')),
4544
			array('User' => array('id' => '4', 'user' => 'garrett')));
4545
		$this->assertEquals($expected, $result);
4546
4547
		$result = $TestModel->hasMany;
4548
		$expected = array();
4549
		$this->assertEquals($expected, $result);
4550
4551
		$result = $TestModel->bindModel(array('hasMany' => array(
4552
			'Comment' => array('className' => 'Comment', 'conditions' => 'Comment.published = \'Y\'')
4553
		)));
4554
		$this->assertTrue($result);
4555
4556
		$result = $TestModel->find('all', array(
4557
			'fields' => 'User.id, User.user',
4558
			'order' => array('User.id' => 'ASC'),
4559
		));
4560
		$expected = array(
4561
			array(
4562
				'User' => array(
4563
					'id' => '1',
4564
					'user' => 'mariano'
4565
				),
4566
				'Comment' => array(
4567
					array(
4568
						'id' => '3',
4569
						'article_id' => '1',
4570
						'user_id' => '1',
4571
						'comment' => 'Third Comment for First Article',
4572
						'published' => 'Y',
4573
						'created' => '2007-03-18 10:49:23',
4574
						'updated' => '2007-03-18 10:51:31'
4575
					),
4576
					array(
4577
						'id' => '5',
4578
						'article_id' => '2',
4579
						'user_id' => '1',
4580
						'comment' => 'First Comment for Second Article',
4581
						'published' => 'Y',
4582
						'created' => '2007-03-18 10:53:23',
4583
						'updated' => '2007-03-18 10:55:31'
4584
			))),
4585
			array(
4586
				'User' => array(
4587
					'id' => '2',
4588
					'user' => 'nate'
4589
				),
4590
				'Comment' => array(
4591
					array(
4592
						'id' => '1',
4593
						'article_id' => '1',
4594
						'user_id' => '2',
4595
						'comment' => 'First Comment for First Article',
4596
						'published' => 'Y',
4597
						'created' => '2007-03-18 10:45:23',
4598
						'updated' => '2007-03-18 10:47:31'
4599
					),
4600
					array(
4601
						'id' => '6',
4602
						'article_id' => '2',
4603
						'user_id' => '2',
4604
						'comment' => 'Second Comment for Second Article',
4605
						'published' => 'Y',
4606
						'created' => '2007-03-18 10:55:23',
4607
						'updated' => '2007-03-18 10:57:31'
4608
			))),
4609
			array(
4610
				'User' => array(
4611
					'id' => '3',
4612
					'user' => 'larry'
4613
				),
4614
				'Comment' => array()
4615
			),
4616
			array(
4617
				'User' => array(
4618
					'id' => '4',
4619
					'user' => 'garrett'
4620
				),
4621
				'Comment' => array(
4622
					array(
4623
						'id' => '2',
4624
						'article_id' => '1',
4625
						'user_id' => '4',
4626
						'comment' => 'Second Comment for First Article',
4627
						'published' => 'Y',
4628
						'created' => '2007-03-18 10:47:23',
4629
						'updated' => '2007-03-18 10:49:31'
4630
		))));
4631
4632
		$this->assertEquals($expected, $result);
4633
4634
		$TestModel2 = new DeviceType();
4635
4636
		$expected = array(
4637
			'className' => 'FeatureSet',
4638
			'foreignKey' => 'feature_set_id',
4639
			'conditions' => '',
4640
			'fields' => '',
4641
			'order' => '',
4642
			'counterCache' => ''
4643
		);
4644
		$this->assertEquals($expected, $TestModel2->belongsTo['FeatureSet']);
4645
4646
		$TestModel2->bindModel(array(
4647
			'belongsTo' => array(
4648
				'FeatureSet' => array(
4649
					'className' => 'FeatureSet',
4650
					'conditions' => array('active' => true)
4651
				)
4652
			)
4653
		));
4654
		$expected['conditions'] = array('active' => true);
4655
		$this->assertEquals($expected, $TestModel2->belongsTo['FeatureSet']);
4656
4657
		$TestModel2->bindModel(array(
4658
			'belongsTo' => array(
4659
				'FeatureSet' => array(
4660
					'className' => 'FeatureSet',
4661
					'foreignKey' => false,
4662
					'conditions' => array('Feature.name' => 'DeviceType.name')
4663
				)
4664
			)
4665
		));
4666
		$expected['conditions'] = array('Feature.name' => 'DeviceType.name');
4667
		$expected['foreignKey'] = false;
4668
		$this->assertEquals($expected, $TestModel2->belongsTo['FeatureSet']);
4669
4670
		$TestModel2->bindModel(array(
4671
			'hasMany' => array(
4672
				'NewFeatureSet' => array(
4673
					'className' => 'FeatureSet',
4674
					'conditions' => array('active' => true)
4675
				)
4676
			)
4677
		));
4678
4679
		$expected = array(
4680
			'className' => 'FeatureSet',
4681
			'conditions' => array('active' => true),
4682
			'foreignKey' => 'device_type_id',
4683
			'fields' => '',
4684
			'order' => '',
4685
			'limit' => '',
4686
			'offset' => '',
4687
			'dependent' => '',
4688
			'exclusive' => '',
4689
			'finderQuery' => '',
4690
			'counterQuery' => ''
4691
		);
4692
		$this->assertEquals($expected, $TestModel2->hasMany['NewFeatureSet']);
4693
		$this->assertTrue(is_object($TestModel2->NewFeatureSet));
4694
	}
4695
4696
/**
4697
 * testBindMultipleTimes method
4698
 *
4699
 * @return void
4700
 */
4701
	public function testBindMultipleTimes() {
4702
		$this->loadFixtures('User', 'Comment', 'Article', 'Tag', 'ArticlesTag');
4703
		$TestModel = new User();
4704
4705
		$result = $TestModel->hasMany;
4706
		$expected = array();
4707
		$this->assertEquals($expected, $result);
4708
4709
		$result = $TestModel->bindModel(array(
4710
			'hasMany' => array(
4711
				'Items' => array('className' => 'Comment')
4712
		)));
4713
		$this->assertTrue($result);
4714
4715
		$result = $TestModel->find('all', array(
4716
			'fields' => 'User.id, User.user'
4717
		));
4718
4719
		$expected = array(
4720
			array(
4721
				'User' => array(
4722
					'id' => '1',
4723
					'user' => 'mariano'
4724
				),
4725
				'Items' => array(
4726
					array(
4727
						'id' => '3',
4728
						'article_id' => '1',
4729
						'user_id' => '1',
4730
						'comment' => 'Third Comment for First Article',
4731
						'published' => 'Y',
4732
						'created' => '2007-03-18 10:49:23',
4733
						'updated' => '2007-03-18 10:51:31'
4734
					),
4735
					array(
4736
						'id' => '4',
4737
						'article_id' => '1',
4738
						'user_id' => '1',
4739
						'comment' => 'Fourth Comment for First Article',
4740
						'published' => 'N',
4741
						'created' => '2007-03-18 10:51:23',
4742
						'updated' => '2007-03-18 10:53:31'
4743
					),
4744
					array(
4745
						'id' => '5',
4746
						'article_id' => '2',
4747
						'user_id' => '1',
4748
						'comment' => 'First Comment for Second Article',
4749
						'published' => 'Y',
4750
						'created' => '2007-03-18 10:53:23',
4751
						'updated' => '2007-03-18 10:55:31'
4752
			))),
4753
			array(
4754
				'User' => array(
4755
					'id' => '2',
4756
					'user' => 'nate'
4757
				),
4758
				'Items' => array(
4759
					array(
4760
						'id' => '1',
4761
						'article_id' => '1',
4762
						'user_id' => '2',
4763
						'comment' => 'First Comment for First Article',
4764
						'published' => 'Y',
4765
						'created' => '2007-03-18 10:45:23',
4766
						'updated' => '2007-03-18 10:47:31'
4767
					),
4768
					array(
4769
						'id' => '6',
4770
						'article_id' => '2',
4771
						'user_id' => '2',
4772
						'comment' => 'Second Comment for Second Article',
4773
						'published' => 'Y',
4774
						'created' => '2007-03-18 10:55:23',
4775
						'updated' => '2007-03-18 10:57:31'
4776
			))),
4777
			array(
4778
				'User' => array(
4779
					'id' => '3',
4780
					'user' => 'larry'
4781
				),
4782
				'Items' => array()
4783
			),
4784
			array(
4785
				'User' => array(
4786
					'id' => '4',
4787
					'user' => 'garrett'
4788
				),
4789
					'Items' => array(
4790
						array(
4791
							'id' => '2',
4792
							'article_id' => '1',
4793
							'user_id' => '4',
4794
							'comment' => 'Second Comment for First Article',
4795
							'published' => 'Y',
4796
							'created' => '2007-03-18 10:47:23',
4797
							'updated' => '2007-03-18 10:49:31'
4798
		))));
4799
		$this->assertEquals($expected, $result);
4800
4801
		$result = $TestModel->bindModel(array(
4802
			'hasMany' => array(
4803
				'Items' => array('className' => 'Article')
4804
		)));
4805
		$this->assertTrue($result);
4806
4807
		$result = $TestModel->find('all', array(
4808
			'fields' => 'User.id, User.user'
4809
		));
4810
		$expected = array(
4811
			array(
4812
				'User' => array(
4813
					'id' => '1',
4814
					'user' => 'mariano'
4815
				),
4816
				'Items' => array(
4817
					array(
4818
						'id' => 1,
4819
						'user_id' => 1,
4820
						'title' => 'First Article',
4821
						'body' => 'First Article Body',
4822
						'published' => 'Y',
4823
						'created' => '2007-03-18 10:39:23',
4824
						'updated' => '2007-03-18 10:41:31'
4825
					),
4826
					array(
4827
						'id' => 3,
4828
						'user_id' => 1,
4829
						'title' => 'Third Article',
4830
						'body' => 'Third Article Body',
4831
						'published' => 'Y',
4832
						'created' => '2007-03-18 10:43:23',
4833
						'updated' => '2007-03-18 10:45:31'
4834
			))),
4835
			array(
4836
				'User' => array(
4837
					'id' => '2',
4838
					'user' => 'nate'
4839
				),
4840
				'Items' => array()
4841
			),
4842
			array(
4843
				'User' => array(
4844
					'id' => '3',
4845
					'user' => 'larry'
4846
				),
4847
				'Items' => array(
4848
					array(
4849
						'id' => 2,
4850
						'user_id' => 3,
4851
						'title' => 'Second Article',
4852
						'body' => 'Second Article Body',
4853
						'published' => 'Y',
4854
						'created' => '2007-03-18 10:41:23',
4855
						'updated' => '2007-03-18 10:43:31'
4856
			))),
4857
			array(
4858
				'User' => array(
4859
					'id' => '4',
4860
					'user' => 'garrett'
4861
				),
4862
				'Items' => array()
4863
		));
4864
4865
		$this->assertEquals($expected, $result);
4866
	}
4867
4868
/**
4869
 * test that multiple reset = true calls to bindModel() result in the original associations.
4870
 *
4871
 * @return void
4872
 */
4873 View Code Duplication
	public function testBindModelMultipleTimesResetCorrectly() {
4874
		$this->loadFixtures('User', 'Comment', 'Article');
4875
		$TestModel = new User();
4876
4877
		$TestModel->bindModel(array('hasMany' => array('Comment')));
4878
		$TestModel->bindModel(array('hasMany' => array('Comment')));
4879
		$TestModel->resetAssociations();
4880
4881
		$this->assertFalse(isset($TestModel->hasMany['Comment']), 'Association left behind');
4882
	}
4883
4884
/**
4885
 * testBindMultipleTimes method with different reset settings
4886
 *
4887
 * @return void
4888
 */
4889 View Code Duplication
	public function testBindMultipleTimesWithDifferentResetSettings() {
4890
		$this->loadFixtures('User', 'Comment', 'Article');
4891
		$TestModel = new User();
4892
4893
		$result = $TestModel->hasMany;
4894
		$expected = array();
4895
		$this->assertEquals($expected, $result);
4896
4897
		$result = $TestModel->bindModel(array(
4898
			'hasMany' => array('Comment')
4899
		));
4900
		$this->assertTrue($result);
4901
		$result = $TestModel->bindModel(
4902
			array('hasMany' => array('Article')),
4903
			false
4904
		);
4905
		$this->assertTrue($result);
4906
4907
		$result = array_keys($TestModel->hasMany);
4908
		$expected = array('Comment', 'Article');
4909
		$this->assertEquals($expected, $result);
4910
4911
		$TestModel->resetAssociations();
4912
4913
		$result = array_keys($TestModel->hasMany);
4914
		$expected = array('Article');
4915
		$this->assertEquals($expected, $result);
4916
	}
4917
4918
/**
4919
 * test that bindModel behaves with Custom primary Key associations
4920
 *
4921
 * @return void
4922
 */
4923
	public function testBindWithCustomPrimaryKey() {
4924
		$this->loadFixtures('Story', 'StoriesTag', 'Tag');
4925
		$Model = ClassRegistry::init('StoriesTag');
4926
		$Model->bindModel(array(
4927
			'belongsTo' => array(
4928
				'Tag' => array(
4929
					'className' => 'Tag',
4930
					'foreignKey' => 'story'
4931
		))));
4932
4933
		$result = $Model->find('all');
4934
		$this->assertFalse(empty($result));
4935
	}
4936
4937
/**
4938
 * test that calling unbindModel() with reset == true multiple times
4939
 * leaves associations in the correct state.
4940
 *
4941
 * @return void
4942
 */
4943 View Code Duplication
	public function testUnbindMultipleTimesResetCorrectly() {
4944
		$this->loadFixtures('User', 'Comment', 'Article');
4945
		$TestModel = new Article10();
4946
4947
		$TestModel->unbindModel(array('hasMany' => array('Comment')));
4948
		$TestModel->unbindModel(array('hasMany' => array('Comment')));
4949
		$TestModel->resetAssociations();
4950
4951
		$this->assertTrue(isset($TestModel->hasMany['Comment']), 'Association permanently removed');
4952
	}
4953
4954
/**
4955
 * testBindMultipleTimes method with different reset settings
4956
 *
4957
 * @return void
4958
 */
4959 View Code Duplication
	public function testUnBindMultipleTimesWithDifferentResetSettings() {
4960
		$this->loadFixtures('User', 'Comment', 'Article');
4961
		$TestModel = new Comment();
4962
4963
		$result = array_keys($TestModel->belongsTo);
4964
		$expected = array('Article', 'User');
4965
		$this->assertEquals($expected, $result);
4966
4967
		$result = $TestModel->unbindModel(array(
4968
			'belongsTo' => array('User')
4969
		));
4970
		$this->assertTrue($result);
4971
		$result = $TestModel->unbindModel(
4972
			array('belongsTo' => array('Article')),
4973
			false
4974
		);
4975
		$this->assertTrue($result);
4976
4977
		$result = array_keys($TestModel->belongsTo);
4978
		$expected = array();
4979
		$this->assertEquals($expected, $result);
4980
4981
		$TestModel->resetAssociations();
4982
4983
		$result = array_keys($TestModel->belongsTo);
4984
		$expected = array('User');
4985
		$this->assertEquals($expected, $result);
4986
	}
4987
4988
/**
4989
 * testAssociationAfterFind method
4990
 *
4991
 * @return void
4992
 */
4993
	public function testAssociationAfterFind() {
4994
		$this->loadFixtures('Post', 'Author', 'Comment');
4995
		$TestModel = new Post();
4996
		$result = $TestModel->find('all', array(
4997
			'order' => array('Post.id' => 'ASC')
4998
		));
4999
		$expected = array(
5000
			array(
5001
				'Post' => array(
5002
					'id' => '1',
5003
					'author_id' => '1',
5004
					'title' => 'First Post',
5005
					'body' => 'First Post Body',
5006
					'published' => 'Y',
5007
					'created' => '2007-03-18 10:39:23',
5008
					'updated' => '2007-03-18 10:41:31'
5009
				),
5010
				'Author' => array(
5011
					'id' => '1',
5012
					'user' => 'mariano',
5013
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5014
					'created' => '2007-03-17 01:16:23',
5015
					'updated' => '2007-03-17 01:18:31',
5016
					'test' => 'working'
5017
			)),
5018
			array(
5019
				'Post' => array(
5020
					'id' => '2',
5021
					'author_id' => '3',
5022
					'title' => 'Second Post',
5023
					'body' => 'Second Post Body',
5024
					'published' => 'Y',
5025
					'created' => '2007-03-18 10:41:23',
5026
					'updated' => '2007-03-18 10:43:31'
5027
				),
5028
				'Author' => array(
5029
					'id' => '3',
5030
					'user' => 'larry',
5031
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5032
					'created' => '2007-03-17 01:20:23',
5033
					'updated' => '2007-03-17 01:22:31',
5034
					'test' => 'working'
5035
			)),
5036
			array(
5037
				'Post' => array(
5038
					'id' => '3',
5039
					'author_id' => '1',
5040
					'title' => 'Third Post',
5041
					'body' => 'Third Post Body',
5042
					'published' => 'Y',
5043
					'created' => '2007-03-18 10:43:23',
5044
					'updated' => '2007-03-18 10:45:31'
5045
				),
5046
				'Author' => array(
5047
					'id' => '1',
5048
					'user' => 'mariano',
5049
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5050
					'created' => '2007-03-17 01:16:23',
5051
					'updated' => '2007-03-17 01:18:31',
5052
					'test' => 'working'
5053
		)));
5054
		$this->assertEquals($expected, $result);
5055
		unset($TestModel);
5056
5057
		$Author = new Author();
5058
		$Author->Post->bindModel(array(
5059
			'hasMany' => array(
5060
				'Comment' => array(
5061
					'className' => 'ModifiedComment',
5062
					'foreignKey' => 'article_id',
5063
				)
5064
		)));
5065
		$result = $Author->find('all', array(
5066
			'conditions' => array('Author.id' => 1),
5067
			'order' => array('Author.id' => 'ASC'),
5068
			'recursive' => 2
5069
		));
5070
		$expected = array(
5071
			'id' => 1,
5072
			'article_id' => 1,
5073
			'user_id' => 2,
5074
			'comment' => 'First Comment for First Article',
5075
			'published' => 'Y',
5076
			'created' => '2007-03-18 10:45:23',
5077
			'updated' => '2007-03-18 10:47:31',
5078
			'callback' => 'Fire'
5079
		);
5080
		$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
5081
	}
5082
5083
/**
5084
 * testDeeperAssociationAfterFind method
5085
 *
5086
 * @return void
5087
 */
5088
	public function testDeeperAssociationAfterFind() {
5089
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article');
5090
5091
		$Post = new Post();
5092
		$Post->bindModel(array(
5093
			'hasMany' => array(
5094
				'Comment' => array(
5095
					'className' => 'ModifiedComment',
5096
					'foreignKey' => 'article_id',
5097
				)
5098
		)));
5099
		$Post->Comment->bindModel(array(
5100
			'hasOne' => array(
5101
				'Attachment' => array(
5102
					'className' => 'ModifiedAttachment',
5103
				)
5104
		)));
5105
5106
		$result = $Post->find('first', array(
5107
			'conditions' => array('Post.id' => 2),
5108
			'recursive' => 2
5109
		));
5110
		$this->assertTrue(isset($result['Comment'][0]['callback']));
5111
		$this->assertEquals('Fire', $result['Comment'][0]['callback']);
5112
		$this->assertTrue(isset($result['Comment'][0]['Attachment']['callback']));
5113
		$this->assertEquals('Fired', $result['Comment'][0]['Attachment']['callback']);
5114
	}
5115
5116
/**
5117
 * Tests that callbacks can be properly disabled
5118
 *
5119
 * @return void
5120
 */
5121
	public function testCallbackDisabling() {
5122
		$this->loadFixtures('Author');
5123
		$TestModel = new ModifiedAuthor();
5124
5125
		$result = Hash::extract($TestModel->find('all'), '{n}.Author.user');
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all') targeting Model::find() can also be of type null; however, Hash::extract() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
5126
		$expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
5127
		$this->assertEquals($expected, $result);
5128
5129
		$result = Hash::extract($TestModel->find('all', array('callbacks' => 'after')), '{n}.Author.user');
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ...callbacks' => 'after')) targeting Model::find() can also be of type null; however, Hash::extract() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
5130
		$expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
5131
		$this->assertEquals($expected, $result);
5132
5133
		$result = Hash::extract($TestModel->find('all', array('callbacks' => 'before')), '{n}.Author.user');
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ...allbacks' => 'before')) targeting Model::find() can also be of type null; however, Hash::extract() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
5134
		$expected = array('mariano', 'nate', 'larry', 'garrett');
5135
		$this->assertEquals($expected, $result);
5136
5137
		$result = Hash::extract($TestModel->find('all', array('callbacks' => false)), '{n}.Author.user');
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ...('callbacks' => false)) targeting Model::find() can also be of type null; however, Hash::extract() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
5138
		$expected = array('mariano', 'nate', 'larry', 'garrett');
5139
		$this->assertEquals($expected, $result);
5140
	}
5141
5142
/**
5143
 * testAssociationAfterFindCallbacksDisabled method
5144
 *
5145
 * @return void
5146
 */
5147
	public function testAssociationAfterFindCalbacksDisabled() {
5148
		$this->loadFixtures('Post', 'Author', 'Comment');
5149
		$TestModel = new Post();
5150
		$result = $TestModel->find('all', array(
5151
			'callbacks' => false,
5152
			'order' => array('Post.id' => 'ASC'),
5153
		));
5154
		$expected = array(
5155
			array(
5156
				'Post' => array(
5157
					'id' => '1',
5158
					'author_id' => '1',
5159
					'title' => 'First Post',
5160
					'body' => 'First Post Body',
5161
					'published' => 'Y',
5162
					'created' => '2007-03-18 10:39:23',
5163
					'updated' => '2007-03-18 10:41:31'
5164
				),
5165
				'Author' => array(
5166
					'id' => '1',
5167
					'user' => 'mariano',
5168
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5169
					'created' => '2007-03-17 01:16:23',
5170
					'updated' => '2007-03-17 01:18:31'
5171
			)),
5172
			array(
5173
				'Post' => array(
5174
					'id' => '2',
5175
					'author_id' => '3',
5176
					'title' => 'Second Post',
5177
					'body' => 'Second Post Body',
5178
					'published' => 'Y',
5179
					'created' => '2007-03-18 10:41:23',
5180
					'updated' => '2007-03-18 10:43:31'
5181
				),
5182
				'Author' => array(
5183
					'id' => '3',
5184
					'user' => 'larry',
5185
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5186
					'created' => '2007-03-17 01:20:23',
5187
					'updated' => '2007-03-17 01:22:31'
5188
			)),
5189
			array(
5190
				'Post' => array(
5191
					'id' => '3',
5192
					'author_id' => '1',
5193
					'title' => 'Third Post',
5194
					'body' => 'Third Post Body',
5195
					'published' => 'Y',
5196
					'created' => '2007-03-18 10:43:23',
5197
					'updated' => '2007-03-18 10:45:31'
5198
				),
5199
				'Author' => array(
5200
					'id' => '1',
5201
					'user' => 'mariano',
5202
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5203
					'created' => '2007-03-17 01:16:23',
5204
					'updated' => '2007-03-17 01:18:31'
5205
		)));
5206
		$this->assertEquals($expected, $result);
5207
		unset($TestModel);
5208
5209
		$Author = new Author();
5210
		$Author->Post->bindModel(array(
5211
			'hasMany' => array(
5212
				'Comment' => array(
5213
					'className' => 'ModifiedComment',
5214
					'foreignKey' => 'article_id',
5215
				)
5216
		)));
5217
		$result = $Author->find('all', array(
5218
			'conditions' => array('Author.id' => 1),
5219
			'recursive' => 2,
5220
			'order' => array('Author.id' => 'ASC'),
5221
			'callbacks' => false
5222
		));
5223
		$expected = array(
5224
			'id' => 1,
5225
			'article_id' => 1,
5226
			'user_id' => 2,
5227
			'comment' => 'First Comment for First Article',
5228
			'published' => 'Y',
5229
			'created' => '2007-03-18 10:45:23',
5230
			'updated' => '2007-03-18 10:47:31'
5231
		);
5232
		$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
5233
	}
5234
5235
/**
5236
 * Tests that the database configuration assigned to the model can be changed using
5237
 * (before|after)Find callbacks
5238
 *
5239
 * @return void
5240
 */
5241
	public function testCallbackSourceChange() {
5242
		$this->loadFixtures('Post');
5243
		$TestModel = new Post();
5244
		$this->assertEquals(3, count($TestModel->find('all')));
5245
	}
5246
5247
/**
5248
 * testCallbackSourceChangeUnknownDatasource method
5249
 *
5250
 * @expectedException MissingDatasourceConfigException
5251
 * @return void
5252
 */
5253
	public function testCallbackSourceChangeUnknownDatasource() {
5254
		$this->loadFixtures('Post', 'Author');
5255
		$TestModel = new Post();
5256
		$this->assertFalse($TestModel->find('all', array('connection' => 'foo')));
5257
	}
5258
5259
/**
5260
 * testMultipleBelongsToWithSameClass method
5261
 *
5262
 * @return void
5263
 */
5264
	public function testMultipleBelongsToWithSameClass() {
5265
		$this->loadFixtures(
5266
			'DeviceType',
5267
			'DeviceTypeCategory',
5268
			'FeatureSet',
5269
			'ExteriorTypeCategory',
5270
			'Document',
5271
			'Device',
5272
			'DocumentDirectory'
5273
		);
5274
5275
		$DeviceType = new DeviceType();
5276
5277
		$DeviceType->recursive = 2;
5278
		$result = $DeviceType->read(null, 1);
5279
5280
		$expected = array(
5281
			'DeviceType' => array(
5282
				'id' => 1,
5283
				'device_type_category_id' => 1,
5284
				'feature_set_id' => 1,
5285
				'exterior_type_category_id' => 1,
5286
				'image_id' => 1,
5287
				'extra1_id' => 1,
5288
				'extra2_id' => 1,
5289
				'name' => 'DeviceType 1',
5290
				'order' => 0
5291
			),
5292
			'Image' => array(
5293
				'id' => 1,
5294
				'document_directory_id' => 1,
5295
				'name' => 'Document 1',
5296
				'DocumentDirectory' => array(
5297
					'id' => 1,
5298
					'name' => 'DocumentDirectory 1'
5299
			)),
5300
			'Extra1' => array(
5301
				'id' => 1,
5302
				'document_directory_id' => 1,
5303
				'name' => 'Document 1',
5304
				'DocumentDirectory' => array(
5305
					'id' => 1,
5306
					'name' => 'DocumentDirectory 1'
5307
			)),
5308
			'Extra2' => array(
5309
				'id' => 1,
5310
				'document_directory_id' => 1,
5311
				'name' => 'Document 1',
5312
				'DocumentDirectory' => array(
5313
					'id' => 1,
5314
					'name' => 'DocumentDirectory 1'
5315
			)),
5316
			'DeviceTypeCategory' => array(
5317
				'id' => 1,
5318
				'name' => 'DeviceTypeCategory 1'
5319
			),
5320
			'FeatureSet' => array(
5321
				'id' => 1,
5322
				'name' => 'FeatureSet 1'
5323
			),
5324
			'ExteriorTypeCategory' => array(
5325
				'id' => 1,
5326
				'image_id' => 1,
5327
				'name' => 'ExteriorTypeCategory 1',
5328
				'Image' => array(
5329
					'id' => 1,
5330
					'device_type_id' => 1,
5331
					'name' => 'Device 1',
5332
					'typ' => 1
5333
			)),
5334
			'Device' => array(
5335
				array(
5336
					'id' => 1,
5337
					'device_type_id' => 1,
5338
					'name' => 'Device 1',
5339
					'typ' => 1
5340
				),
5341
				array(
5342
					'id' => 2,
5343
					'device_type_id' => 1,
5344
					'name' => 'Device 2',
5345
					'typ' => 1
5346
				),
5347
				array(
5348
					'id' => 3,
5349
					'device_type_id' => 1,
5350
					'name' => 'Device 3',
5351
					'typ' => 2
5352
		)));
5353
5354
		$this->assertEquals($expected, $result);
5355
	}
5356
5357
/**
5358
 * testHabtmRecursiveBelongsTo method
5359
 *
5360
 * @return void
5361
 */
5362
	public function testHabtmRecursiveBelongsTo() {
5363
		$this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image');
5364
		$Portfolio = new Portfolio();
5365
5366
		$result = $Portfolio->find('first', array('conditions' => array('id' => 2), 'recursive' => 3));
5367
		$expected = array(
5368
			'Portfolio' => array(
5369
				'id' => 2,
5370
				'seller_id' => 1,
5371
				'name' => 'Portfolio 2'
5372
			),
5373
			'Item' => array(
5374
				array(
5375
					'id' => 2,
5376
					'syfile_id' => 2,
5377
					'published' => false,
5378
					'name' => 'Item 2',
5379
					'ItemsPortfolio' => array(
5380
						'id' => 2,
5381
						'item_id' => 2,
5382
						'portfolio_id' => 2
5383
					),
5384
					'Syfile' => array(
5385
						'id' => 2,
5386
						'image_id' => 2,
5387
						'name' => 'Syfile 2',
5388
						'item_count' => null,
5389
						'Image' => array(
5390
							'id' => 2,
5391
							'name' => 'Image 2'
5392
						)
5393
				)),
5394
				array(
5395
					'id' => 6,
5396
					'syfile_id' => 6,
5397
					'published' => false,
5398
					'name' => 'Item 6',
5399
					'ItemsPortfolio' => array(
5400
						'id' => 6,
5401
						'item_id' => 6,
5402
						'portfolio_id' => 2
5403
					),
5404
					'Syfile' => array(
5405
						'id' => 6,
5406
						'image_id' => null,
5407
						'name' => 'Syfile 6',
5408
						'item_count' => null,
5409
						'Image' => array()
5410
		))));
5411
5412
		$this->assertEquals($expected, $result);
5413
	}
5414
5415
/**
5416
 * testNonNumericHabtmJoinKey method
5417
 *
5418
 * @return void
5419
 */
5420
	public function testNonNumericHabtmJoinKey() {
5421
		$this->loadFixtures('Post', 'Tag', 'PostsTag', 'Author');
5422
		$Post = new Post();
5423
		$Post->bindModel(array(
5424
			'hasAndBelongsToMany' => array('Tag')
5425
		));
5426
		$Post->Tag->primaryKey = 'tag';
5427
5428
		$result = $Post->find('all', array(
5429
			'order' => 'Post.id ASC',
5430
		));
5431
		$expected = array(
5432
			array(
5433
				'Post' => array(
5434
					'id' => '1',
5435
					'author_id' => '1',
5436
					'title' => 'First Post',
5437
					'body' => 'First Post Body',
5438
					'published' => 'Y',
5439
					'created' => '2007-03-18 10:39:23',
5440
					'updated' => '2007-03-18 10:41:31'
5441
				),
5442
				'Author' => array(
5443
					'id' => 1,
5444
					'user' => 'mariano',
5445
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5446
					'created' => '2007-03-17 01:16:23',
5447
					'updated' => '2007-03-17 01:18:31',
5448
					'test' => 'working'
5449
				),
5450
				'Tag' => array(
5451
					array(
5452
						'id' => '1',
5453
						'tag' => 'tag1',
5454
						'created' => '2007-03-18 12:22:23',
5455
						'updated' => '2007-03-18 12:24:31'
5456
					),
5457
					array(
5458
						'id' => '2',
5459
						'tag' => 'tag2',
5460
						'created' => '2007-03-18 12:24:23',
5461
						'updated' => '2007-03-18 12:26:31'
5462
			))),
5463
			array(
5464
				'Post' => array(
5465
					'id' => '2',
5466
					'author_id' => '3',
5467
					'title' => 'Second Post',
5468
					'body' => 'Second Post Body',
5469
					'published' => 'Y',
5470
					'created' => '2007-03-18 10:41:23',
5471
					'updated' => '2007-03-18 10:43:31'
5472
				),
5473
				'Author' => array(
5474
					'id' => 3,
5475
					'user' => 'larry',
5476
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5477
					'created' => '2007-03-17 01:20:23',
5478
					'updated' => '2007-03-17 01:22:31',
5479
					'test' => 'working'
5480
				),
5481
				'Tag' => array(
5482
					array(
5483
						'id' => '1',
5484
						'tag' => 'tag1',
5485
						'created' => '2007-03-18 12:22:23',
5486
						'updated' => '2007-03-18 12:24:31'
5487
						),
5488
					array(
5489
						'id' => '3',
5490
						'tag' => 'tag3',
5491
						'created' => '2007-03-18 12:26:23',
5492
						'updated' => '2007-03-18 12:28:31'
5493
			))),
5494
			array(
5495
				'Post' => array(
5496
					'id' => '3',
5497
					'author_id' => '1',
5498
					'title' => 'Third Post',
5499
					'body' => 'Third Post Body',
5500
					'published' => 'Y',
5501
					'created' => '2007-03-18 10:43:23',
5502
					'updated' => '2007-03-18 10:45:31'
5503
				),
5504
				'Author' => array(
5505
					'id' => 1,
5506
					'user' => 'mariano',
5507
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5508
					'created' => '2007-03-17 01:16:23',
5509
					'updated' => '2007-03-17 01:18:31',
5510
					'test' => 'working'
5511
				),
5512
				'Tag' => array()
5513
		));
5514
		$this->assertEquals($expected, $result);
5515
	}
5516
5517
/**
5518
 * testHabtmFinderQuery method
5519
 *
5520
 * @return void
5521
 */
5522
	public function testHabtmFinderQuery() {
5523
		$this->loadFixtures('Article', 'Tag', 'ArticlesTag');
5524
		$Article = new Article();
5525
5526
		$sql = $this->db->buildStatement(
5527
			array(
5528
				'fields' => $this->db->fields($Article->Tag, null, array(
5529
					'Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id'
5530
				)),
5531
				'table' => $this->db->fullTableName('tags'),
5532
				'alias' => 'Tag',
5533
				'limit' => null,
5534
				'offset' => null,
5535
				'group' => null,
5536
				'joins' => array(array(
5537
					'alias' => 'ArticlesTag',
5538
					'table' => 'articles_tags',
5539
					'conditions' => array(
5540
						array("ArticlesTag.article_id" => '{$__cakeID__$}'),
5541
						array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id'))
5542
					)
5543
				)),
5544
				'conditions' => array(),
5545
				'order' => null
5546
			),
5547
			$Article
5548
		);
5549
5550
		$Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql;
5551
		$result = $Article->find('first');
5552
		$expected = array(
5553
			array(
5554
				'id' => '1',
5555
				'tag' => 'tag1'
5556
			),
5557
			array(
5558
				'id' => '2',
5559
				'tag' => 'tag2'
5560
		));
5561
5562
		$this->assertEquals($expected, $result['Tag']);
5563
	}
5564
5565
/**
5566
 * testHabtmLimitOptimization method
5567
 *
5568
 * @return void
5569
 */
5570
	public function testHabtmLimitOptimization() {
5571
		$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
5572
		$TestModel = new Article();
5573
5574
		$TestModel->hasAndBelongsToMany['Tag']['limit'] = 2;
5575
		$result = $TestModel->read(null, 2);
5576
		$expected = array(
5577
			'Article' => array(
5578
				'id' => '2',
5579
				'user_id' => '3',
5580
				'title' => 'Second Article',
5581
				'body' => 'Second Article Body',
5582
				'published' => 'Y',
5583
				'created' => '2007-03-18 10:41:23',
5584
				'updated' => '2007-03-18 10:43:31'
5585
			),
5586
			'User' => array(
5587
				'id' => '3',
5588
				'user' => 'larry',
5589
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
5590
				'created' => '2007-03-17 01:20:23',
5591
				'updated' => '2007-03-17 01:22:31'
5592
			),
5593
			'Comment' => array(
5594
				array(
5595
					'id' => '5',
5596
					'article_id' => '2',
5597
					'user_id' => '1',
5598
					'comment' => 'First Comment for Second Article',
5599
					'published' => 'Y',
5600
					'created' => '2007-03-18 10:53:23',
5601
					'updated' => '2007-03-18 10:55:31'
5602
				),
5603
				array(
5604
					'id' => '6',
5605
					'article_id' => '2',
5606
					'user_id' => '2',
5607
					'comment' => 'Second Comment for Second Article',
5608
					'published' => 'Y',
5609
					'created' => '2007-03-18 10:55:23',
5610
					'updated' => '2007-03-18 10:57:31'
5611
			)),
5612
			'Tag' => array(
5613
				array(
5614
					'id' => '1',
5615
					'tag' => 'tag1',
5616
					'created' => '2007-03-18 12:22:23',
5617
					'updated' => '2007-03-18 12:24:31'
5618
				),
5619
				array(
5620
					'id' => '3',
5621
					'tag' => 'tag3',
5622
					'created' => '2007-03-18 12:26:23',
5623
					'updated' => '2007-03-18 12:28:31'
5624
		)));
5625
5626
		$this->assertEquals($expected, $result);
5627
5628
		$TestModel->hasAndBelongsToMany['Tag']['limit'] = 1;
5629
		$result = $TestModel->read(null, 2);
5630
		unset($expected['Tag'][1]);
5631
5632
		$this->assertEquals($expected, $result);
5633
	}
5634
5635
/**
5636
 * testHasManyLimitOptimization method
5637
 *
5638
 * @return void
5639
 */
5640
	public function testHasManyLimitOptimization() {
5641
		$this->loadFixtures('Project', 'Thread', 'Message', 'Bid');
5642
		$Project = new Project();
5643
		$Project->recursive = 3;
5644
5645
		$result = $Project->find('all', array(
5646
			'order' => 'Project.id ASC',
5647
		));
5648
		$expected = array(
5649
			array(
5650
				'Project' => array(
5651
					'id' => 1,
5652
					'name' => 'Project 1'
5653
				),
5654
				'Thread' => array(
5655
					array(
5656
						'id' => 1,
5657
						'project_id' => 1,
5658
						'name' => 'Project 1, Thread 1',
5659
						'Project' => array(
5660
							'id' => 1,
5661
							'name' => 'Project 1',
5662
							'Thread' => array(
5663
								array(
5664
									'id' => 1,
5665
									'project_id' => 1,
5666
									'name' => 'Project 1, Thread 1'
5667
								),
5668
								array(
5669
									'id' => 2,
5670
									'project_id' => 1,
5671
									'name' => 'Project 1, Thread 2'
5672
						))),
5673
						'Message' => array(
5674
							array(
5675
								'id' => 1,
5676
								'thread_id' => 1,
5677
								'name' => 'Thread 1, Message 1',
5678
								'Bid' => array(
5679
									'id' => 1,
5680
									'message_id' => 1,
5681
									'name' => 'Bid 1.1'
5682
					)))),
5683
					array(
5684
						'id' => 2,
5685
						'project_id' => 1,
5686
						'name' => 'Project 1, Thread 2',
5687
						'Project' => array(
5688
							'id' => 1,
5689
							'name' => 'Project 1',
5690
							'Thread' => array(
5691
								array(
5692
									'id' => 1,
5693
									'project_id' => 1,
5694
									'name' => 'Project 1, Thread 1'
5695
								),
5696
								array(
5697
									'id' => 2,
5698
									'project_id' => 1,
5699
									'name' => 'Project 1, Thread 2'
5700
						))),
5701
						'Message' => array(
5702
							array(
5703
								'id' => 2,
5704
								'thread_id' => 2,
5705
								'name' => 'Thread 2, Message 1',
5706
								'Bid' => array(
5707
									'id' => 4,
5708
									'message_id' => 2,
5709
									'name' => 'Bid 2.1'
5710
			)))))),
5711
			array(
5712
				'Project' => array(
5713
					'id' => 2,
5714
					'name' => 'Project 2'
5715
				),
5716
				'Thread' => array(
5717
					array(
5718
						'id' => 3,
5719
						'project_id' => 2,
5720
						'name' => 'Project 2, Thread 1',
5721
						'Project' => array(
5722
							'id' => 2,
5723
							'name' => 'Project 2',
5724
							'Thread' => array(
5725
								array(
5726
									'id' => 3,
5727
									'project_id' => 2,
5728
									'name' => 'Project 2, Thread 1'
5729
						))),
5730
						'Message' => array(
5731
							array(
5732
								'id' => 3,
5733
								'thread_id' => 3,
5734
								'name' => 'Thread 3, Message 1',
5735
								'Bid' => array(
5736
									'id' => 3,
5737
									'message_id' => 3,
5738
									'name' => 'Bid 3.1'
5739
			)))))),
5740
			array(
5741
				'Project' => array(
5742
					'id' => 3,
5743
					'name' => 'Project 3'
5744
				),
5745
				'Thread' => array()
5746
		));
5747
5748
		$this->assertEquals($expected, $result);
5749
	}
5750
5751
/**
5752
 * testFindAllRecursiveSelfJoin method
5753
 *
5754
 * @return void
5755
 */
5756
	public function testFindAllRecursiveSelfJoin() {
5757
		$this->loadFixtures('Home', 'AnotherArticle', 'Advertisement');
5758
		$TestModel = new Home();
5759
		$TestModel->recursive = 2;
5760
5761
		$result = $TestModel->find('all', array(
5762
			'order' => 'Home.id ASC',
5763
		));
5764
		$expected = array(
5765
			array(
5766
				'Home' => array(
5767
					'id' => '1',
5768
					'another_article_id' => '1',
5769
					'advertisement_id' => '1',
5770
					'title' => 'First Home',
5771
					'created' => '2007-03-18 10:39:23',
5772
					'updated' => '2007-03-18 10:41:31'
5773
				),
5774
				'AnotherArticle' => array(
5775
					'id' => '1',
5776
					'title' => 'First Article',
5777
					'created' => '2007-03-18 10:39:23',
5778
					'updated' => '2007-03-18 10:41:31',
5779
					'Home' => array(
5780
						array(
5781
							'id' => '1',
5782
							'another_article_id' => '1',
5783
							'advertisement_id' => '1',
5784
							'title' => 'First Home',
5785
							'created' => '2007-03-18 10:39:23',
5786
							'updated' => '2007-03-18 10:41:31'
5787
				))),
5788
				'Advertisement' => array(
5789
					'id' => '1',
5790
					'title' => 'First Ad',
5791
					'created' => '2007-03-18 10:39:23',
5792
					'updated' => '2007-03-18 10:41:31',
5793
					'Home' => array(
5794
						array(
5795
							'id' => '1',
5796
							'another_article_id' => '1',
5797
							'advertisement_id' => '1',
5798
							'title' => 'First Home',
5799
							'created' => '2007-03-18 10:39:23',
5800
							'updated' => '2007-03-18 10:41:31'
5801
						),
5802
						array(
5803
							'id' => '2',
5804
							'another_article_id' => '3',
5805
							'advertisement_id' => '1',
5806
							'title' => 'Second Home',
5807
							'created' => '2007-03-18 10:41:23',
5808
							'updated' => '2007-03-18 10:43:31'
5809
			)))),
5810
			array(
5811
				'Home' => array(
5812
					'id' => '2',
5813
					'another_article_id' => '3',
5814
					'advertisement_id' => '1',
5815
					'title' => 'Second Home',
5816
					'created' => '2007-03-18 10:41:23',
5817
					'updated' => '2007-03-18 10:43:31'
5818
				),
5819
				'AnotherArticle' => array(
5820
					'id' => '3',
5821
					'title' => 'Third Article',
5822
					'created' => '2007-03-18 10:43:23',
5823
					'updated' => '2007-03-18 10:45:31',
5824
					'Home' => array(
5825
						array(
5826
							'id' => '2',
5827
							'another_article_id' => '3',
5828
							'advertisement_id' => '1',
5829
							'title' => 'Second Home',
5830
							'created' => '2007-03-18 10:41:23',
5831
							'updated' => '2007-03-18 10:43:31'
5832
				))),
5833
				'Advertisement' => array(
5834
					'id' => '1',
5835
					'title' => 'First Ad',
5836
					'created' => '2007-03-18 10:39:23',
5837
					'updated' => '2007-03-18 10:41:31',
5838
					'Home' => array(
5839
						array(
5840
							'id' => '1',
5841
							'another_article_id' => '1',
5842
							'advertisement_id' => '1',
5843
							'title' => 'First Home',
5844
							'created' => '2007-03-18 10:39:23',
5845
							'updated' => '2007-03-18 10:41:31'
5846
						),
5847
						array(
5848
							'id' => '2',
5849
							'another_article_id' => '3',
5850
							'advertisement_id' => '1',
5851
							'title' => 'Second Home',
5852
							'created' => '2007-03-18 10:41:23',
5853
							'updated' => '2007-03-18 10:43:31'
5854
		)))));
5855
5856
		$this->assertEquals($expected, $result);
5857
	}
5858
5859
/**
5860
 * testFindAllRecursiveWithHabtm method
5861
 *
5862
 * @return void
5863
 */
5864
	public function testFindAllRecursiveWithHabtm() {
5865
		$this->loadFixtures(
5866
			'MyCategoriesMyUsers',
5867
			'MyCategoriesMyProducts',
5868
			'MyCategory',
5869
			'MyUser',
5870
			'MyProduct'
5871
		);
5872
5873
		$MyUser = new MyUser();
5874
		$MyUser->recursive = 2;
5875
5876
		$result = $MyUser->find('all', array(
5877
			'order' => 'MyUser.id ASC'
5878
		));
5879
		$expected = array(
5880
			array(
5881
				'MyUser' => array('id' => '1', 'firstname' => 'userA'),
5882
				'MyCategory' => array(
5883
					array(
5884
						'id' => '1',
5885
						'name' => 'A',
5886
						'MyProduct' => array(
5887
							array(
5888
								'id' => '1',
5889
								'name' => 'book'
5890
					))),
5891
					array(
5892
						'id' => '3',
5893
						'name' => 'C',
5894
						'MyProduct' => array(
5895
							array(
5896
								'id' => '2',
5897
								'name' => 'computer'
5898
			))))),
5899
			array(
5900
				'MyUser' => array(
5901
					'id' => '2',
5902
					'firstname' => 'userB'
5903
				),
5904
				'MyCategory' => array(
5905
					array(
5906
						'id' => '1',
5907
						'name' => 'A',
5908
						'MyProduct' => array(
5909
							array(
5910
								'id' => '1',
5911
								'name' => 'book'
5912
					))),
5913
					array(
5914
						'id' => '2',
5915
						'name' => 'B',
5916
						'MyProduct' => array(
5917
							array(
5918
								'id' => '1',
5919
								'name' => 'book'
5920
							),
5921
							array(
5922
								'id' => '2',
5923
								'name' => 'computer'
5924
		))))));
5925
5926
		$this->assertEquals($expected, $result);
5927
	}
5928
5929
/**
5930
 * testReadFakeThread method
5931
 *
5932
 * @return void
5933
 */
5934
	public function testReadFakeThread() {
5935
		$this->loadFixtures('CategoryThread');
5936
		$TestModel = new CategoryThread();
5937
5938
		$fullDebug = $this->db->fullDebug;
5939
		$this->db->fullDebug = true;
5940
		$TestModel->recursive = 6;
5941
		$TestModel->id = 7;
5942
		$result = $TestModel->read();
5943
		$expected = array(
5944
			'CategoryThread' => array(
5945
				'id' => 7,
5946
				'parent_id' => 6,
5947
				'name' => 'Category 2.1',
5948
				'created' => '2007-03-18 15:30:23',
5949
				'updated' => '2007-03-18 15:32:31'
5950
			),
5951
			'ParentCategory' => array(
5952
				'id' => 6,
5953
				'parent_id' => 5,
5954
				'name' => 'Category 2',
5955
				'created' => '2007-03-18 15:30:23',
5956
				'updated' => '2007-03-18 15:32:31',
5957
				'ParentCategory' => array(
5958
					'id' => 5,
5959
					'parent_id' => 4,
5960
					'name' => 'Category 1.1.1.1',
5961
					'created' => '2007-03-18 15:30:23',
5962
					'updated' => '2007-03-18 15:32:31',
5963
					'ParentCategory' => array(
5964
						'id' => 4,
5965
						'parent_id' => 3,
5966
						'name' => 'Category 1.1.2',
5967
						'created' => '2007-03-18 15:30:23',
5968
						'updated' => '2007-03-18 15:32:31',
5969
						'ParentCategory' => array(
5970
							'id' => 3,
5971
							'parent_id' => 2,
5972
							'name' => 'Category 1.1.1',
5973
							'created' => '2007-03-18 15:30:23',
5974
							'updated' => '2007-03-18 15:32:31',
5975
							'ParentCategory' => array(
5976
								'id' => 2,
5977
								'parent_id' => 1,
5978
								'name' => 'Category 1.1',
5979
								'created' => '2007-03-18 15:30:23',
5980
								'updated' => '2007-03-18 15:32:31',
5981
								'ParentCategory' => array(
5982
									'id' => 1,
5983
									'parent_id' => 0,
5984
									'name' => 'Category 1',
5985
									'created' => '2007-03-18 15:30:23',
5986
									'updated' => '2007-03-18 15:32:31'
5987
		)))))));
5988
5989
		$this->db->fullDebug = $fullDebug;
5990
		$this->assertEquals($expected, $result);
5991
	}
5992
5993
/**
5994
 * testFindFakeThread method
5995
 *
5996
 * @return void
5997
 */
5998
	public function testFindFakeThread() {
5999
		$this->loadFixtures('CategoryThread');
6000
		$TestModel = new CategoryThread();
6001
6002
		$fullDebug = $this->db->fullDebug;
6003
		$this->db->fullDebug = true;
6004
		$TestModel->recursive = 6;
6005
		$result = $TestModel->find('first', array('conditions' => array('CategoryThread.id' => 7)));
6006
6007
		$expected = array(
6008
			'CategoryThread' => array(
6009
				'id' => 7,
6010
				'parent_id' => 6,
6011
				'name' => 'Category 2.1',
6012
				'created' => '2007-03-18 15:30:23',
6013
				'updated' => '2007-03-18 15:32:31'
6014
			),
6015
			'ParentCategory' => array(
6016
				'id' => 6,
6017
				'parent_id' => 5,
6018
				'name' => 'Category 2',
6019
				'created' => '2007-03-18 15:30:23',
6020
				'updated' => '2007-03-18 15:32:31',
6021
				'ParentCategory' => array(
6022
					'id' => 5,
6023
					'parent_id' => 4,
6024
					'name' => 'Category 1.1.1.1',
6025
					'created' => '2007-03-18 15:30:23',
6026
					'updated' => '2007-03-18 15:32:31',
6027
					'ParentCategory' => array(
6028
						'id' => 4,
6029
						'parent_id' => 3,
6030
						'name' => 'Category 1.1.2',
6031
						'created' => '2007-03-18 15:30:23',
6032
						'updated' => '2007-03-18 15:32:31',
6033
						'ParentCategory' => array(
6034
							'id' => 3,
6035
							'parent_id' => 2,
6036
							'name' => 'Category 1.1.1',
6037
							'created' => '2007-03-18 15:30:23',
6038
							'updated' => '2007-03-18 15:32:31',
6039
							'ParentCategory' => array(
6040
								'id' => 2,
6041
								'parent_id' => 1,
6042
								'name' => 'Category 1.1',
6043
								'created' => '2007-03-18 15:30:23',
6044
								'updated' => '2007-03-18 15:32:31',
6045
								'ParentCategory' => array(
6046
									'id' => 1,
6047
									'parent_id' => 0,
6048
									'name' => 'Category 1',
6049
									'created' => '2007-03-18 15:30:23',
6050
									'updated' => '2007-03-18 15:32:31'
6051
		)))))));
6052
6053
		$this->db->fullDebug = $fullDebug;
6054
		$this->assertEquals($expected, $result);
6055
	}
6056
6057
/**
6058
 * testFindAllFakeThread method
6059
 *
6060
 * @return void
6061
 */
6062
	public function testFindAllFakeThread() {
6063
		$this->loadFixtures('CategoryThread');
6064
		$TestModel = new CategoryThread();
6065
6066
		$fullDebug = $this->db->fullDebug;
6067
		$this->db->fullDebug = true;
6068
		$TestModel->recursive = 6;
6069
		$result = $TestModel->find('all');
6070
		$expected = array(
6071
			array(
6072
				'CategoryThread' => array(
6073
				'id' => 1,
6074
				'parent_id' => 0,
6075
				'name' => 'Category 1',
6076
				'created' => '2007-03-18 15:30:23',
6077
				'updated' => '2007-03-18 15:32:31'
6078
				),
6079
				'ParentCategory' => array(
6080
					'id' => null,
6081
					'parent_id' => null,
6082
					'name' => null,
6083
					'created' => null,
6084
					'updated' => null,
6085
					'ParentCategory' => array()
6086
			)),
6087
			array(
6088
				'CategoryThread' => array(
6089
					'id' => 2,
6090
					'parent_id' => 1,
6091
					'name' => 'Category 1.1',
6092
					'created' => '2007-03-18 15:30:23',
6093
					'updated' => '2007-03-18 15:32:31'
6094
				),
6095
				'ParentCategory' => array(
6096
					'id' => 1,
6097
					'parent_id' => 0,
6098
					'name' => 'Category 1',
6099
					'created' => '2007-03-18 15:30:23',
6100
					'updated' => '2007-03-18 15:32:31',
6101
					'ParentCategory' => array()
6102
				)),
6103
			array(
6104
				'CategoryThread' => array(
6105
					'id' => 3,
6106
					'parent_id' => 2,
6107
					'name' => 'Category 1.1.1',
6108
					'created' => '2007-03-18 15:30:23',
6109
					'updated' => '2007-03-18 15:32:31'
6110
				),
6111
				'ParentCategory' => array(
6112
					'id' => 2,
6113
					'parent_id' => 1,
6114
					'name' => 'Category 1.1',
6115
					'created' => '2007-03-18 15:30:23',
6116
					'updated' => '2007-03-18 15:32:31',
6117
					'ParentCategory' => array(
6118
						'id' => 1,
6119
						'parent_id' => 0,
6120
						'name' => 'Category 1',
6121
						'created' => '2007-03-18 15:30:23',
6122
						'updated' => '2007-03-18 15:32:31',
6123
						'ParentCategory' => array()
6124
			))),
6125
			array(
6126
				'CategoryThread' => array(
6127
					'id' => 4,
6128
					'parent_id' => 3,
6129
					'name' => 'Category 1.1.2',
6130
					'created' => '2007-03-18 15:30:23',
6131
					'updated' => '2007-03-18 15:32:31'
6132
				),
6133
				'ParentCategory' => array(
6134
					'id' => 3,
6135
					'parent_id' => 2,
6136
					'name' => 'Category 1.1.1',
6137
					'created' => '2007-03-18 15:30:23',
6138
					'updated' => '2007-03-18 15:32:31',
6139
					'ParentCategory' => array(
6140
						'id' => 2,
6141
						'parent_id' => 1,
6142
						'name' => 'Category 1.1',
6143
						'created' => '2007-03-18 15:30:23',
6144
						'updated' => '2007-03-18 15:32:31',
6145
						'ParentCategory' => array(
6146
							'id' => 1,
6147
							'parent_id' => 0,
6148
							'name' => 'Category 1',
6149
							'created' => '2007-03-18 15:30:23',
6150
							'updated' => '2007-03-18 15:32:31',
6151
							'ParentCategory' => array()
6152
			)))),
6153
			array(
6154
				'CategoryThread' => array(
6155
					'id' => 5,
6156
					'parent_id' => 4,
6157
					'name' => 'Category 1.1.1.1',
6158
					'created' => '2007-03-18 15:30:23',
6159
					'updated' => '2007-03-18 15:32:31'
6160
				),
6161
				'ParentCategory' => array(
6162
					'id' => 4,
6163
					'parent_id' => 3,
6164
					'name' => 'Category 1.1.2',
6165
					'created' => '2007-03-18 15:30:23',
6166
					'updated' => '2007-03-18 15:32:31',
6167
					'ParentCategory' => array(
6168
						'id' => 3,
6169
						'parent_id' => 2,
6170
						'name' => 'Category 1.1.1',
6171
						'created' => '2007-03-18 15:30:23',
6172
						'updated' => '2007-03-18 15:32:31',
6173
						'ParentCategory' => array(
6174
							'id' => 2,
6175
							'parent_id' => 1,
6176
							'name' => 'Category 1.1',
6177
							'created' => '2007-03-18 15:30:23',
6178
							'updated' => '2007-03-18 15:32:31',
6179
							'ParentCategory' => array(
6180
								'id' => 1,
6181
								'parent_id' => 0,
6182
								'name' => 'Category 1',
6183
								'created' => '2007-03-18 15:30:23',
6184
								'updated' => '2007-03-18 15:32:31',
6185
								'ParentCategory' => array()
6186
			))))),
6187
			array(
6188
				'CategoryThread' => array(
6189
					'id' => 6,
6190
					'parent_id' => 5,
6191
					'name' => 'Category 2',
6192
					'created' => '2007-03-18 15:30:23',
6193
					'updated' => '2007-03-18 15:32:31'
6194
				),
6195
				'ParentCategory' => array(
6196
					'id' => 5,
6197
					'parent_id' => 4,
6198
					'name' => 'Category 1.1.1.1',
6199
					'created' => '2007-03-18 15:30:23',
6200
					'updated' => '2007-03-18 15:32:31',
6201
					'ParentCategory' => array(
6202
						'id' => 4,
6203
						'parent_id' => 3,
6204
						'name' => 'Category 1.1.2',
6205
						'created' => '2007-03-18 15:30:23',
6206
						'updated' => '2007-03-18 15:32:31',
6207
						'ParentCategory' => array(
6208
							'id' => 3,
6209
							'parent_id' => 2,
6210
							'name' => 'Category 1.1.1',
6211
							'created' => '2007-03-18 15:30:23',
6212
							'updated' => '2007-03-18 15:32:31',
6213
							'ParentCategory' => array(
6214
								'id' => 2,
6215
								'parent_id' => 1,
6216
								'name' => 'Category 1.1',
6217
								'created' => '2007-03-18 15:30:23',
6218
								'updated' => '2007-03-18 15:32:31',
6219
								'ParentCategory' => array(
6220
									'id' => 1,
6221
									'parent_id' => 0,
6222
									'name' => 'Category 1',
6223
									'created' => '2007-03-18 15:30:23',
6224
									'updated' => '2007-03-18 15:32:31',
6225
									'ParentCategory' => array()
6226
			)))))),
6227
			array(
6228
				'CategoryThread' => array(
6229
					'id' => 7,
6230
					'parent_id' => 6,
6231
					'name' => 'Category 2.1',
6232
					'created' => '2007-03-18 15:30:23',
6233
					'updated' => '2007-03-18 15:32:31'
6234
				),
6235
				'ParentCategory' => array(
6236
					'id' => 6,
6237
					'parent_id' => 5,
6238
					'name' => 'Category 2',
6239
					'created' => '2007-03-18 15:30:23',
6240
					'updated' => '2007-03-18 15:32:31',
6241
					'ParentCategory' => array(
6242
						'id' => 5,
6243
						'parent_id' => 4,
6244
						'name' => 'Category 1.1.1.1',
6245
						'created' => '2007-03-18 15:30:23',
6246
						'updated' => '2007-03-18 15:32:31',
6247
						'ParentCategory' => array(
6248
							'id' => 4,
6249
							'parent_id' => 3,
6250
							'name' => 'Category 1.1.2',
6251
							'created' => '2007-03-18 15:30:23',
6252
							'updated' => '2007-03-18 15:32:31',
6253
							'ParentCategory' => array(
6254
								'id' => 3,
6255
								'parent_id' => 2,
6256
								'name' => 'Category 1.1.1',
6257
								'created' => '2007-03-18 15:30:23',
6258
								'updated' => '2007-03-18 15:32:31',
6259
							'ParentCategory' => array(
6260
								'id' => 2,
6261
								'parent_id' => 1,
6262
								'name' => 'Category 1.1',
6263
								'created' => '2007-03-18 15:30:23',
6264
								'updated' => '2007-03-18 15:32:31',
6265
								'ParentCategory' => array(
6266
									'id' => 1,
6267
									'parent_id' => 0,
6268
									'name' => 'Category 1',
6269
									'created' => '2007-03-18 15:30:23',
6270
									'updated' => '2007-03-18 15:32:31'
6271
		))))))));
6272
6273
		$this->db->fullDebug = $fullDebug;
6274
		$this->assertEquals($expected, $result);
6275
	}
6276
6277
/**
6278
 * testConditionalNumerics method
6279
 *
6280
 * @return void
6281
 */
6282
	public function testConditionalNumerics() {
6283
		$this->loadFixtures('NumericArticle');
6284
		$NumericArticle = new NumericArticle();
6285
		$data = array('conditions' => array('title' => '12345abcde'));
6286
		$result = $NumericArticle->find('first', $data);
6287
		$this->assertTrue(!empty($result));
6288
6289
		$data = array('conditions' => array('title' => '12345'));
6290
		$result = $NumericArticle->find('first', $data);
6291
		$this->assertTrue(empty($result));
6292
	}
6293
6294
/**
6295
 * test buildQuery()
6296
 *
6297
 * @return void
6298
 */
6299
	public function testBuildQuery() {
6300
		$this->loadFixtures('User');
6301
		$TestModel = new User();
6302
		$TestModel->cacheQueries = false;
6303
		$TestModel->order = null;
6304
6305
		$expected = array(
6306
			'conditions' => array(
6307
				'user' => 'larry'
6308
			),
6309
			'fields' => null,
6310
			'joins' => array(),
6311
			'limit' => null,
6312
			'offset' => null,
6313
			'order' => array(
6314
				0 => null
6315
			),
6316
			'page' => 1,
6317
			'group' => null,
6318
			'callbacks' => true,
6319
			'returnQuery' => true
6320
		);
6321
		$result = $TestModel->buildQuery('all', array('returnQuery' => true, 'conditions' => array('user' => 'larry')));
6322
		$this->assertEquals($expected, $result);
6323
	}
6324
6325
/**
6326
 * test find('all') method
6327
 *
6328
 * @return void
6329
 */
6330
	public function testFindAll() {
6331
		$this->loadFixtures('User');
6332
		$TestModel = new User();
6333
		$TestModel->cacheQueries = false;
6334
6335
		$result = $TestModel->find('all');
6336
		$expected = array(
6337
			array(
6338
				'User' => array(
6339
					'id' => '1',
6340
					'user' => 'mariano',
6341
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6342
					'created' => '2007-03-17 01:16:23',
6343
					'updated' => '2007-03-17 01:18:31'
6344
			)),
6345
			array(
6346
				'User' => array(
6347
					'id' => '2',
6348
					'user' => 'nate',
6349
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6350
					'created' => '2007-03-17 01:18:23',
6351
					'updated' => '2007-03-17 01:20:31'
6352
			)),
6353
			array(
6354
				'User' => array(
6355
					'id' => '3',
6356
					'user' => 'larry',
6357
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6358
					'created' => '2007-03-17 01:20:23',
6359
					'updated' => '2007-03-17 01:22:31'
6360
			)),
6361
			array(
6362
				'User' => array(
6363
					'id' => '4',
6364
					'user' => 'garrett',
6365
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6366
					'created' => '2007-03-17 01:22:23',
6367
					'updated' => '2007-03-17 01:24:31'
6368
		)));
6369
		$this->assertEquals($expected, $result);
6370
6371
		$result = $TestModel->find('all', array('conditions' => 'User.id > 2'));
6372
		$expected = array(
6373
			array(
6374
				'User' => array(
6375
					'id' => '3',
6376
					'user' => 'larry',
6377
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6378
					'created' => '2007-03-17 01:20:23',
6379
					'updated' => '2007-03-17 01:22:31'
6380
			)),
6381
			array(
6382
				'User' => array(
6383
					'id' => '4',
6384
					'user' => 'garrett',
6385
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6386
					'created' => '2007-03-17 01:22:23',
6387
					'updated' => '2007-03-17 01:24:31'
6388
		)));
6389
		$this->assertEquals($expected, $result);
6390
6391
		$result = $TestModel->find('all', array(
6392
			'conditions' => array('User.id !=' => '0', 'User.user LIKE' => '%arr%')
6393
		));
6394
		$expected = array(
6395
			array(
6396
				'User' => array(
6397
					'id' => '3',
6398
					'user' => 'larry',
6399
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6400
					'created' => '2007-03-17 01:20:23',
6401
					'updated' => '2007-03-17 01:22:31'
6402
			)),
6403
			array(
6404
				'User' => array(
6405
					'id' => '4',
6406
					'user' => 'garrett',
6407
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6408
					'created' => '2007-03-17 01:22:23',
6409
					'updated' => '2007-03-17 01:24:31'
6410
		)));
6411
		$this->assertEquals($expected, $result);
6412
6413
		$result = $TestModel->find('all', array('conditions' => array('User.id' => '0')));
6414
		$expected = array();
6415
		$this->assertEquals($expected, $result);
6416
6417
		$result = $TestModel->find('all', array(
6418
			'conditions' => array('or' => array('User.id' => '0', 'User.user LIKE' => '%a%')
6419
		)));
6420
6421
		$expected = array(
6422
			array(
6423
				'User' => array(
6424
					'id' => '1',
6425
					'user' => 'mariano',
6426
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6427
					'created' => '2007-03-17 01:16:23',
6428
					'updated' => '2007-03-17 01:18:31'
6429
			)),
6430
			array(
6431
				'User' => array(
6432
					'id' => '2',
6433
					'user' => 'nate',
6434
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6435
					'created' => '2007-03-17 01:18:23',
6436
					'updated' => '2007-03-17 01:20:31'
6437
			)),
6438
			array(
6439
				'User' => array(
6440
					'id' => '3',
6441
					'user' => 'larry',
6442
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6443
					'created' => '2007-03-17 01:20:23',
6444
					'updated' => '2007-03-17 01:22:31'
6445
			)),
6446
			array(
6447
				'User' => array(
6448
					'id' => '4',
6449
					'user' => 'garrett',
6450
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6451
					'created' => '2007-03-17 01:22:23',
6452
					'updated' => '2007-03-17 01:24:31'
6453
		)));
6454
		$this->assertEquals($expected, $result);
6455
6456
		$result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
6457
		$expected = array(
6458
				array('User' => array('id' => '1', 'user' => 'mariano')),
6459
				array('User' => array('id' => '2', 'user' => 'nate')),
6460
				array('User' => array('id' => '3', 'user' => 'larry')),
6461
				array('User' => array('id' => '4', 'user' => 'garrett')));
6462
		$this->assertEquals($expected, $result);
6463
6464
		$result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user ASC'));
6465
		$expected = array(
6466
				array('User' => array('user' => 'garrett')),
6467
				array('User' => array('user' => 'larry')),
6468
				array('User' => array('user' => 'mariano')),
6469
				array('User' => array('user' => 'nate')));
6470
		$this->assertEquals($expected, $result);
6471
6472
		$result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user DESC'));
6473
		$expected = array(
6474
				array('User' => array('user' => 'nate')),
6475
				array('User' => array('user' => 'mariano')),
6476
				array('User' => array('user' => 'larry')),
6477
				array('User' => array('user' => 'garrett')));
6478
		$this->assertEquals($expected, $result);
6479
6480
		$result = $TestModel->find('all', array('limit' => 3, 'page' => 1));
6481
6482
		$expected = array(
6483
			array(
6484
				'User' => array(
6485
					'id' => '1',
6486
					'user' => 'mariano',
6487
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6488
					'created' => '2007-03-17 01:16:23',
6489
					'updated' => '2007-03-17 01:18:31'
6490
			)),
6491
			array(
6492
				'User' => array(
6493
					'id' => '2',
6494
					'user' => 'nate',
6495
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6496
					'created' => '2007-03-17 01:18:23',
6497
					'updated' => '2007-03-17 01:20:31'
6498
			)),
6499
			array(
6500
				'User' => array(
6501
					'id' => '3',
6502
					'user' => 'larry',
6503
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6504
					'created' => '2007-03-17 01:20:23',
6505
					'updated' => '2007-03-17 01:22:31'
6506
		)));
6507
		$this->assertEquals($expected, $result);
6508
6509
		$ids = array(4 => 1, 5 => 3);
6510
		$result = $TestModel->find('all', array(
6511
			'conditions' => array('User.id' => $ids),
6512
			'order' => 'User.id'
6513
		));
6514
		$expected = array(
6515
			array(
6516
				'User' => array(
6517
					'id' => '1',
6518
					'user' => 'mariano',
6519
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6520
					'created' => '2007-03-17 01:16:23',
6521
					'updated' => '2007-03-17 01:18:31'
6522
			)),
6523
			array(
6524
				'User' => array(
6525
					'id' => '3',
6526
					'user' => 'larry',
6527
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6528
					'created' => '2007-03-17 01:20:23',
6529
					'updated' => '2007-03-17 01:22:31'
6530
		)));
6531
		$this->assertEquals($expected, $result);
6532
6533
		// These tests are expected to fail on SQL Server since the LIMIT/OFFSET
6534
		// hack can't handle small record counts.
6535
		if (!($this->db instanceof Sqlserver)) {
6536
			$result = $TestModel->find('all', array('limit' => 3, 'page' => 2));
6537
			$expected = array(
6538
				array(
6539
					'User' => array(
6540
						'id' => '4',
6541
						'user' => 'garrett',
6542
						'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
6543
						'created' => '2007-03-17 01:22:23',
6544
						'updated' => '2007-03-17 01:24:31'
6545
			)));
6546
			$this->assertEquals($expected, $result);
6547
6548
			$result = $TestModel->find('all', array('limit' => 3, 'page' => 3));
6549
			$expected = array();
6550
			$this->assertEquals($expected, $result);
6551
		}
6552
	}
6553
6554
/**
6555
 * test find('list') method
6556
 *
6557
 * @return void
6558
 */
6559
	public function testGenerateFindList() {
6560
		$this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User', 'Comment');
6561
6562
		$TestModel = new Article();
6563
		$TestModel->displayField = 'title';
6564
6565
		$result = $TestModel->find('list', array(
6566
			'order' => 'Article.title ASC'
6567
		));
6568
6569
		$expected = array(
6570
			1 => 'First Article',
6571
			2 => 'Second Article',
6572
			3 => 'Third Article'
6573
		);
6574
		$this->assertEquals($expected, $result);
6575
6576
		$db = ConnectionManager::getDataSource('test');
6577
		if ($db instanceof Mysql) {
6578
			$result = $TestModel->find('list', array(
6579
				'order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC')
6580
			));
6581
			$expected = array(
6582
				1 => 'First Article',
6583
				3 => 'Third Article',
6584
				2 => 'Second Article'
6585
			);
6586
			$this->assertEquals($expected, $result);
6587
		}
6588
6589
		$result = Hash::combine(
6590
			$TestModel->find('all', array(
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ... array('id', 'title'))) targeting Model::find() can also be of type null; however, Hash::combine() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
6591
				'order' => 'Article.title ASC',
6592
				'fields' => array('id', 'title')
6593
			)),
6594
			'{n}.Article.id', '{n}.Article.title'
6595
		);
6596
		$expected = array(
6597
			1 => 'First Article',
6598
			2 => 'Second Article',
6599
			3 => 'Third Article'
6600
		);
6601
		$this->assertEquals($expected, $result);
6602
6603
		$result = Hash::combine(
6604
			$TestModel->find('all', array(
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ...> 'Article.title ASC')) targeting Model::find() can also be of type null; however, Hash::combine() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
6605
				'order' => 'Article.title ASC'
6606
			)),
6607
			'{n}.Article.id', '{n}.Article'
6608
		);
6609
		$expected = array(
6610
			1 => array(
6611
				'id' => 1,
6612
				'user_id' => 1,
6613
				'title' => 'First Article',
6614
				'body' => 'First Article Body',
6615
				'published' => 'Y',
6616
				'created' => '2007-03-18 10:39:23',
6617
				'updated' => '2007-03-18 10:41:31'
6618
			),
6619
			2 => array(
6620
				'id' => 2,
6621
				'user_id' => 3,
6622
				'title' => 'Second Article',
6623
				'body' => 'Second Article Body',
6624
				'published' => 'Y',
6625
				'created' => '2007-03-18 10:41:23',
6626
				'updated' => '2007-03-18 10:43:31'
6627
			),
6628
			3 => array(
6629
				'id' => 3,
6630
				'user_id' => 1,
6631
				'title' => 'Third Article',
6632
				'body' => 'Third Article Body',
6633
				'published' => 'Y',
6634
				'created' => '2007-03-18 10:43:23',
6635
				'updated' => '2007-03-18 10:45:31'
6636
		));
6637
6638
		$this->assertEquals($expected, $result);
6639
6640
		$result = Hash::combine(
6641
			$TestModel->find('all', array(
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ...> 'Article.title ASC')) targeting Model::find() can also be of type null; however, Hash::combine() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
6642
				'order' => 'Article.title ASC'
6643
			)),
6644
			'{n}.Article.id', '{n}.Article', '{n}.Article.user_id'
6645
		);
6646
		$expected = array(
6647
			1 => array(
6648
				1 => array(
6649
					'id' => 1,
6650
					'user_id' => 1,
6651
					'title' => 'First Article',
6652
					'body' => 'First Article Body',
6653
					'published' => 'Y',
6654
					'created' => '2007-03-18 10:39:23',
6655
					'updated' => '2007-03-18 10:41:31'
6656
				),
6657
				3 => array(
6658
					'id' => 3,
6659
					'user_id' => 1,
6660
					'title' => 'Third Article',
6661
					'body' => 'Third Article Body',
6662
					'published' => 'Y',
6663
					'created' => '2007-03-18 10:43:23',
6664
					'updated' => '2007-03-18 10:45:31'
6665
				)),
6666
			3 => array(
6667
				2 => array(
6668
					'id' => 2,
6669
					'user_id' => 3,
6670
					'title' => 'Second Article',
6671
					'body' => 'Second Article Body',
6672
					'published' => 'Y',
6673
					'created' => '2007-03-18 10:41:23',
6674
					'updated' => '2007-03-18 10:43:31'
6675
		)));
6676
6677
		$this->assertEquals($expected, $result);
6678
6679
		$result = Hash::combine(
6680
			$TestModel->find('all', array(
0 ignored issues
show
Bug introduced by
It seems like $TestModel->find('all', ..., 'title', 'user_id'))) targeting Model::find() can also be of type null; however, Hash::combine() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
6681
				'order' => 'Article.title ASC',
6682
				'fields' => array('id', 'title', 'user_id')
6683
			)),
6684
			'{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id'
6685
		);
6686
6687
		$expected = array(
6688
			1 => array(
6689
				1 => 'First Article',
6690
				3 => 'Third Article'
6691
			),
6692
			3 => array(
6693
				2 => 'Second Article'
6694
		));
6695
		$this->assertEquals($expected, $result);
6696
6697
		$TestModel = new Apple();
6698
		$expected = array(
6699
			1 => 'Red Apple 1',
6700
			2 => 'Bright Red Apple',
6701
			3 => 'green blue',
6702
			4 => 'Test Name',
6703
			5 => 'Blue Green',
6704
			6 => 'My new apple',
6705
			7 => 'Some odd color'
6706
		);
6707
6708
		$this->assertEquals($expected, $TestModel->find('list'));
6709
		$this->assertEquals($expected, $TestModel->Parent->find('list'));
6710
6711
		$TestModel = new Post();
6712
		$result = $TestModel->find('list', array(
6713
			'fields' => 'Post.title'
6714
		));
6715
		$expected = array(
6716
			1 => 'First Post',
6717
			2 => 'Second Post',
6718
			3 => 'Third Post'
6719
		);
6720
		$this->assertEquals($expected, $result);
6721
6722
		$result = $TestModel->find('list', array(
6723
			'fields' => 'title'
6724
		));
6725
		$expected = array(
6726
			1 => 'First Post',
6727
			2 => 'Second Post',
6728
			3 => 'Third Post'
6729
		);
6730
		$this->assertEquals($expected, $result);
6731
6732
		$result = $TestModel->find('list', array(
6733
			'fields' => array('title', 'id')
6734
		));
6735
		$expected = array(
6736
			'First Post' => '1',
6737
			'Second Post' => '2',
6738
			'Third Post' => '3'
6739
		);
6740
		$this->assertEquals($expected, $result);
6741
6742
		$result = $TestModel->find('list', array(
6743
			'fields' => array('title', 'id', 'created')
6744
		));
6745
		$expected = array(
6746
			'2007-03-18 10:39:23' => array(
6747
				'First Post' => '1'
6748
			),
6749
			'2007-03-18 10:41:23' => array(
6750
				'Second Post' => '2'
6751
			),
6752
			'2007-03-18 10:43:23' => array(
6753
				'Third Post' => '3'
6754
			),
6755
		);
6756
		$this->assertEquals($expected, $result);
6757
6758
		$result = $TestModel->find('list', array(
6759
			'fields' => array('Post.body')
6760
		));
6761
		$expected = array(
6762
			1 => 'First Post Body',
6763
			2 => 'Second Post Body',
6764
			3 => 'Third Post Body'
6765
		);
6766
		$this->assertEquals($expected, $result);
6767
6768
		$result = $TestModel->find('list', array(
6769
			'fields' => array('Post.title', 'Post.body')
6770
		));
6771
		$expected = array(
6772
			'First Post' => 'First Post Body',
6773
			'Second Post' => 'Second Post Body',
6774
			'Third Post' => 'Third Post Body'
6775
		);
6776
		$this->assertEquals($expected, $result);
6777
6778
		$result = $TestModel->find('list', array(
6779
			'fields' => array('Post.id', 'Post.title', 'Author.user'),
6780
			'recursive' => 1
6781
		));
6782
		$expected = array(
6783
			'mariano' => array(
6784
				1 => 'First Post',
6785
				3 => 'Third Post'
6786
			),
6787
			'larry' => array(
6788
				2 => 'Second Post'
6789
		));
6790
		$this->assertEquals($expected, $result);
6791
6792
		$TestModel = new User();
6793
		$result = $TestModel->find('list', array(
6794
			'fields' => array('User.user', 'User.password')
6795
		));
6796
		$expected = array(
6797
			'mariano' => '5f4dcc3b5aa765d61d8327deb882cf99',
6798
			'nate' => '5f4dcc3b5aa765d61d8327deb882cf99',
6799
			'larry' => '5f4dcc3b5aa765d61d8327deb882cf99',
6800
			'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99'
6801
		);
6802
		$this->assertEquals($expected, $result);
6803
6804
		$TestModel = new ModifiedAuthor();
6805
		$result = $TestModel->find('list', array(
6806
			'fields' => array('Author.id', 'Author.user')
6807
		));
6808
		$expected = array(
6809
			1 => 'mariano (CakePHP)',
6810
			2 => 'nate (CakePHP)',
6811
			3 => 'larry (CakePHP)',
6812
			4 => 'garrett (CakePHP)'
6813
		);
6814
		$this->assertEquals($expected, $result);
6815
6816
		$TestModel = new Article();
6817
		$TestModel->displayField = 'title';
6818
		$result = $TestModel->find('list', array(
6819
			'conditions' => array('User.user' => 'mariano'),
6820
			'recursive' => 0
6821
		));
6822
		$expected = array(
6823
			1 => 'First Article',
6824
			3 => 'Third Article'
6825
		);
6826
		$this->assertEquals($expected, $result);
6827
	}
6828
6829
/**
6830
 * testFindField method
6831
 *
6832
 * @return void
6833
 */
6834
	public function testFindField() {
6835
		$this->loadFixtures('User');
6836
		$TestModel = new User();
6837
6838
		$TestModel->id = 1;
6839
		$result = $TestModel->field('user');
6840
		$this->assertEquals('mariano', $result);
6841
6842
		$result = $TestModel->field('User.user');
6843
		$this->assertEquals('mariano', $result);
6844
6845
		$TestModel->id = false;
6846
		$result = $TestModel->field('user', array(
6847
			'user' => 'mariano'
6848
		));
6849
		$this->assertEquals('mariano', $result);
6850
6851
		$TestModel->order = null;
6852
6853
		$result = $TestModel->field('COUNT(*) AS count', true);
6854
		$this->assertEquals(4, $result);
6855
6856
		$result = $TestModel->field('COUNT(*)', true);
6857
		$this->assertEquals(4, $result);
6858
	}
6859
6860
/**
6861
 * testFindUnique method
6862
 *
6863
 * @return void
6864
 */
6865
	public function testFindUnique() {
6866
		$this->loadFixtures('User');
6867
		$TestModel = new User();
6868
6869
		$this->assertFalse($TestModel->isUnique(array(
6870
			'user' => 'nate'
6871
		)));
6872
		$TestModel->id = 2;
6873
		$this->assertTrue($TestModel->isUnique(array(
6874
			'user' => 'nate'
6875
		)));
6876
		$this->assertFalse($TestModel->isUnique(array(
6877
			'user' => 'nate',
6878
			'password' => '5f4dcc3b5aa765d61d8327deb882cf99'
6879
		)));
6880
	}
6881
6882
/**
6883
 * test find('count') method
6884
 *
6885
 * @return void
6886
 */
6887
	public function testFindCount() {
6888
		$this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag');
6889
6890
		$TestModel = new User();
6891
		$this->db->getLog(false, true);
6892
		$result = $TestModel->find('count');
6893
		$this->assertEquals(4, $result);
6894
6895
		$this->db->getLog(false, true);
6896
		$fullDebug = $this->db->fullDebug;
6897
		$this->db->fullDebug = true;
6898
		$TestModel->order = 'User.id';
6899
		$result = $TestModel->find('count');
6900
		$this->db->fullDebug = $fullDebug;
6901
		$this->assertEquals(4, $result);
6902
6903
		$log = $this->db->getLog();
6904
		$this->assertTrue(isset($log['log'][0]['query']));
6905
		$this->assertNotRegExp('/ORDER\s+BY/', $log['log'][0]['query']);
6906
6907
		$Article = new Article();
6908
		$Article->order = null;
6909
		$Article->recursive = -1;
6910
6911
		$expected = count($Article->find('all', array(
6912
			'fields' => array('Article.user_id'),
6913
			'group' => 'Article.user_id')
6914
		));
6915
		$result = $Article->find('count', array('group' => array('Article.user_id')));
6916
		$this->assertEquals($expected, $result);
6917
6918
		$expected = count($Article->find('all', array(
6919
			'fields' => array('Article.user_id'),
6920
			'conditions' => array('Article.user_id' => 1),
6921
			'group' => 'Article.user_id')
6922
		));
6923
		$result = $Article->find('count', array(
6924
			'conditions' => array('Article.user_id' => 1),
6925
			'group' => array('Article.user_id'),
6926
		));
6927
		$this->assertEquals($expected, $result);
6928
	}
6929
6930
/**
6931
 * Test that find('first') does not use the id set to the object.
6932
 *
6933
 * @return void
6934
 */
6935
	public function testFindFirstNoIdUsed() {
6936
		$this->loadFixtures('Project');
6937
6938
		$Project = new Project();
6939
		$Project->id = 3;
6940
		$result = $Project->find('first');
6941
6942
		$this->assertEquals('Project 1', $result['Project']['name'], 'Wrong record retrieved');
6943
	}
6944
6945
/**
6946
 * test find with COUNT(DISTINCT field)
6947
 *
6948
 * @return void
6949
 */
6950
	public function testFindCountDistinct() {
6951
		$this->skipIf($this->db instanceof Sqlite, 'SELECT COUNT(DISTINCT field) is not compatible with SQLite.');
6952
		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
6953
6954
		$this->loadFixtures('Project', 'Thread');
6955
		$TestModel = new Project();
6956
		$TestModel->create(array('name' => 'project')) && $TestModel->save();
6957
		$TestModel->create(array('name' => 'project')) && $TestModel->save();
6958
		$TestModel->create(array('name' => 'project')) && $TestModel->save();
6959
6960
		$result = $TestModel->find('count', array('fields' => 'DISTINCT name'));
6961
		$this->assertEquals(4, $result);
6962
	}
6963
6964
/**
6965
 * Test find(count) with Db::expression
6966
 *
6967
 * @return void
6968
 */
6969
	public function testFindCountWithDbExpressions() {
6970
		$this->skipIf($this->db instanceof Postgres, 'testFindCountWithDbExpressions is not compatible with Postgres.');
6971
6972
		$this->loadFixtures('Project', 'Thread');
6973
		$db = ConnectionManager::getDataSource('test');
6974
		$TestModel = new Project();
6975
6976
		$result = $TestModel->find('count', array('conditions' => array(
6977
			$db->expression('Project.name = \'Project 3\'')
6978
		)));
6979
		$this->assertEquals(1, $result);
6980
6981
		$result = $TestModel->find('count', array('conditions' => array(
6982
			'Project.name' => $db->expression('\'Project 3\'')
6983
		)));
6984
		$this->assertEquals(1, $result);
6985
	}
6986
6987
/**
6988
 * testFindMagic method
6989
 *
6990
 * @return void
6991
 */
6992
	public function testFindMagic() {
6993
		$this->loadFixtures('User');
6994
		$TestModel = new User();
6995
6996
		$result = $TestModel->findByUser('mariano');
0 ignored issues
show
Documentation Bug introduced by
The method findByUser does not exist on object<User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
6997
		$expected = array(
6998
			'User' => array(
6999
				'id' => '1',
7000
				'user' => 'mariano',
7001
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7002
				'created' => '2007-03-17 01:16:23',
7003
				'updated' => '2007-03-17 01:18:31'
7004
		));
7005
		$this->assertEquals($expected, $result);
7006
7007
		$result = $TestModel->findByPassword('5f4dcc3b5aa765d61d8327deb882cf99');
0 ignored issues
show
Documentation Bug introduced by
The method findByPassword does not exist on object<User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
7008
		$expected = array('User' => array(
7009
			'id' => '1',
7010
			'user' => 'mariano',
7011
			'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7012
			'created' => '2007-03-17 01:16:23',
7013
			'updated' => '2007-03-17 01:18:31'
7014
		));
7015
		$this->assertEquals($expected, $result);
7016
	}
7017
7018
/**
7019
 * testRead method
7020
 *
7021
 * @return void
7022
 */
7023
	public function testRead() {
7024
		$this->loadFixtures('User', 'Article');
7025
		$TestModel = new User();
7026
7027
		$result = $TestModel->read();
7028
		$this->assertFalse($result);
7029
7030
		$TestModel->id = 2;
7031
		$result = $TestModel->read();
7032
		$expected = array(
7033
			'User' => array(
7034
				'id' => '2',
7035
				'user' => 'nate',
7036
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7037
				'created' => '2007-03-17 01:18:23',
7038
				'updated' => '2007-03-17 01:20:31'
7039
		));
7040
		$this->assertEquals($expected, $result);
7041
7042
		$result = $TestModel->read(null, 2);
7043
		$expected = array(
7044
			'User' => array(
7045
				'id' => '2',
7046
				'user' => 'nate',
7047
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7048
				'created' => '2007-03-17 01:18:23',
7049
				'updated' => '2007-03-17 01:20:31'
7050
		));
7051
		$this->assertEquals($expected, $result);
7052
7053
		$TestModel->id = 2;
7054
		$result = $TestModel->read(array('id', 'user'));
7055
		$expected = array('User' => array('id' => '2', 'user' => 'nate'));
7056
		$this->assertEquals($expected, $result);
7057
7058
		$result = $TestModel->read('id, user', 2);
7059
		$expected = array(
7060
			'User' => array(
7061
				'id' => '2',
7062
				'user' => 'nate'
7063
		));
7064
		$this->assertEquals($expected, $result);
7065
7066
		$result = $TestModel->bindModel(array('hasMany' => array('Article')));
7067
		$this->assertTrue($result);
7068
7069
		$TestModel->id = 1;
7070
		$result = $TestModel->read('id, user');
7071
		$expected = array(
7072
			'User' => array(
7073
				'id' => '1',
7074
				'user' => 'mariano'
7075
			),
7076
			'Article' => array(
7077
				array(
7078
					'id' => '1',
7079
					'user_id' => '1',
7080
					'title' => 'First Article',
7081
					'body' => 'First Article Body',
7082
					'published' => 'Y',
7083
					'created' => '2007-03-18 10:39:23',
7084
					'updated' => '2007-03-18 10:41:31'
7085
				),
7086
				array(
7087
					'id' => '3',
7088
					'user_id' => '1',
7089
					'title' => 'Third Article',
7090
					'body' => 'Third Article Body',
7091
					'published' => 'Y',
7092
					'created' => '2007-03-18 10:43:23',
7093
					'updated' => '2007-03-18 10:45:31'
7094
		)));
7095
		$this->assertEquals($expected, $result);
7096
	}
7097
7098
/**
7099
 * testRecursiveRead method
7100
 *
7101
 * @return void
7102
 */
7103
	public function testRecursiveRead() {
7104
		$this->loadFixtures(
7105
			'User',
7106
			'Article',
7107
			'Comment',
7108
			'Tag',
7109
			'ArticlesTag',
7110
			'Featured',
7111
			'ArticleFeatured'
7112
		);
7113
		$TestModel = new User();
7114
7115
		$result = $TestModel->bindModel(array('hasMany' => array('Article')), false);
7116
		$this->assertTrue($result);
7117
7118
		$TestModel->recursive = 0;
7119
		$result = $TestModel->read('id, user', 1);
7120
		$expected = array(
7121
			'User' => array('id' => '1', 'user' => 'mariano'),
7122
		);
7123
		$this->assertEquals($expected, $result);
7124
7125
		$TestModel->recursive = 1;
7126
		$result = $TestModel->read('id, user', 1);
7127
		$expected = array(
7128
			'User' => array(
7129
				'id' => '1',
7130
				'user' => 'mariano'
7131
			),
7132
			'Article' => array(
7133
				array(
7134
					'id' => '1',
7135
					'user_id' => '1',
7136
					'title' => 'First Article',
7137
					'body' => 'First Article Body',
7138
					'published' => 'Y',
7139
					'created' => '2007-03-18 10:39:23',
7140
					'updated' => '2007-03-18 10:41:31'
7141
				),
7142
				array(
7143
					'id' => '3',
7144
					'user_id' => '1',
7145
					'title' => 'Third Article',
7146
					'body' => 'Third Article Body',
7147
					'published' => 'Y',
7148
					'created' => '2007-03-18 10:43:23',
7149
					'updated' => '2007-03-18 10:45:31'
7150
		)));
7151
		$this->assertEquals($expected, $result);
7152
7153
		$TestModel->recursive = 2;
7154
		$result = $TestModel->read('id, user', 3);
7155
		$expected = array(
7156
			'User' => array(
7157
				'id' => '3',
7158
				'user' => 'larry'
7159
			),
7160
			'Article' => array(
7161
				array(
7162
					'id' => '2',
7163
					'user_id' => '3',
7164
					'title' => 'Second Article',
7165
					'body' => 'Second Article Body',
7166
					'published' => 'Y',
7167
					'created' => '2007-03-18 10:41:23',
7168
					'updated' => '2007-03-18 10:43:31',
7169
					'User' => array(
7170
						'id' => '3',
7171
						'user' => 'larry',
7172
						'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7173
						'created' => '2007-03-17 01:20:23',
7174
						'updated' => '2007-03-17 01:22:31'
7175
					),
7176
					'Comment' => array(
7177
						array(
7178
							'id' => '5',
7179
							'article_id' => '2',
7180
							'user_id' => '1',
7181
							'comment' => 'First Comment for Second Article',
7182
							'published' => 'Y',
7183
							'created' => '2007-03-18 10:53:23',
7184
							'updated' => '2007-03-18 10:55:31'
7185
						),
7186
						array(
7187
							'id' => '6',
7188
							'article_id' => '2',
7189
							'user_id' => '2',
7190
							'comment' => 'Second Comment for Second Article',
7191
							'published' => 'Y',
7192
							'created' => '2007-03-18 10:55:23',
7193
							'updated' => '2007-03-18 10:57:31'
7194
					)),
7195
					'Tag' => array(
7196
						array(
7197
							'id' => '1',
7198
							'tag' => 'tag1',
7199
							'created' => '2007-03-18 12:22:23',
7200
							'updated' => '2007-03-18 12:24:31'
7201
						),
7202
						array(
7203
							'id' => '3',
7204
							'tag' => 'tag3',
7205
							'created' => '2007-03-18 12:26:23',
7206
							'updated' => '2007-03-18 12:28:31'
7207
		)))));
7208
		$this->assertEquals($expected, $result);
7209
	}
7210
7211
	public function testRecursiveFindAll() {
7212
		$this->loadFixtures(
7213
			'User',
7214
			'Article',
7215
			'Comment',
7216
			'Tag',
7217
			'ArticlesTag',
7218
			'Attachment',
7219
			'ArticleFeatured',
7220
			'ArticleFeaturedsTags',
7221
			'Featured',
7222
			'Category'
7223
		);
7224
		$TestModel = new Article();
7225
7226
		$result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 1)));
7227
		$expected = array(
7228
			array(
7229
				'Article' => array(
7230
					'id' => '1',
7231
					'user_id' => '1',
7232
					'title' => 'First Article',
7233
					'body' => 'First Article Body',
7234
					'published' => 'Y',
7235
					'created' => '2007-03-18 10:39:23',
7236
					'updated' => '2007-03-18 10:41:31'
7237
				),
7238
				'User' => array(
7239
					'id' => '1',
7240
					'user' => 'mariano',
7241
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7242
					'created' => '2007-03-17 01:16:23',
7243
					'updated' => '2007-03-17 01:18:31'
7244
				),
7245
				'Comment' => array(
7246
					array(
7247
						'id' => '1',
7248
						'article_id' => '1',
7249
						'user_id' => '2',
7250
						'comment' => 'First Comment for First Article',
7251
						'published' => 'Y',
7252
						'created' => '2007-03-18 10:45:23',
7253
						'updated' => '2007-03-18 10:47:31'
7254
					),
7255
					array(
7256
						'id' => '2',
7257
						'article_id' => '1',
7258
						'user_id' => '4',
7259
						'comment' => 'Second Comment for First Article',
7260
						'published' => 'Y',
7261
						'created' => '2007-03-18 10:47:23',
7262
						'updated' => '2007-03-18 10:49:31'
7263
					),
7264
					array(
7265
						'id' => '3',
7266
						'article_id' => '1',
7267
						'user_id' => '1',
7268
						'comment' => 'Third Comment for First Article',
7269
						'published' => 'Y',
7270
						'created' => '2007-03-18 10:49:23',
7271
						'updated' => '2007-03-18 10:51:31'
7272
					),
7273
					array(
7274
						'id' => '4',
7275
						'article_id' => '1',
7276
						'user_id' => '1',
7277
						'comment' => 'Fourth Comment for First Article',
7278
						'published' => 'N',
7279
						'created' => '2007-03-18 10:51:23',
7280
						'updated' => '2007-03-18 10:53:31'
7281
					)
7282
				),
7283
				'Tag' => array(
7284
					array(
7285
						'id' => '1',
7286
						'tag' => 'tag1',
7287
						'created' => '2007-03-18 12:22:23',
7288
						'updated' => '2007-03-18 12:24:31'
7289
					),
7290
					array(
7291
						'id' => '2',
7292
						'tag' => 'tag2',
7293
						'created' => '2007-03-18 12:24:23',
7294
						'updated' => '2007-03-18 12:26:31'
7295
			))),
7296
			array(
7297
				'Article' => array(
7298
					'id' => '3',
7299
					'user_id' => '1',
7300
					'title' => 'Third Article',
7301
					'body' => 'Third Article Body',
7302
					'published' => 'Y',
7303
					'created' => '2007-03-18 10:43:23',
7304
					'updated' => '2007-03-18 10:45:31'
7305
				),
7306
				'User' => array(
7307
					'id' => '1',
7308
					'user' => 'mariano',
7309
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7310
					'created' => '2007-03-17 01:16:23',
7311
					'updated' => '2007-03-17 01:18:31'
7312
				),
7313
				'Comment' => array(),
7314
				'Tag' => array()
7315
			)
7316
		);
7317
		$this->assertEquals($expected, $result);
7318
7319
		$result = $TestModel->find('all', array(
7320
			'conditions' => array('Article.user_id' => 3),
7321
			'limit' => 1,
7322
			'recursive' => 2
7323
		));
7324
7325
		$expected = array(
7326
			array(
7327
				'Article' => array(
7328
					'id' => '2',
7329
					'user_id' => '3',
7330
					'title' => 'Second Article',
7331
					'body' => 'Second Article Body',
7332
					'published' => 'Y',
7333
					'created' => '2007-03-18 10:41:23',
7334
					'updated' => '2007-03-18 10:43:31'
7335
				),
7336
				'User' => array(
7337
					'id' => '3',
7338
					'user' => 'larry',
7339
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7340
					'created' => '2007-03-17 01:20:23',
7341
					'updated' => '2007-03-17 01:22:31'
7342
				),
7343
				'Comment' => array(
7344
					array(
7345
						'id' => '5',
7346
						'article_id' => '2',
7347
						'user_id' => '1',
7348
						'comment' => 'First Comment for Second Article',
7349
						'published' => 'Y',
7350
						'created' => '2007-03-18 10:53:23',
7351
						'updated' => '2007-03-18 10:55:31',
7352
						'Article' => array(
7353
							'id' => '2',
7354
							'user_id' => '3',
7355
							'title' => 'Second Article',
7356
							'body' => 'Second Article Body',
7357
							'published' => 'Y',
7358
							'created' => '2007-03-18 10:41:23',
7359
							'updated' => '2007-03-18 10:43:31'
7360
						),
7361
						'User' => array(
7362
							'id' => '1',
7363
							'user' => 'mariano',
7364
							'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7365
							'created' => '2007-03-17 01:16:23',
7366
							'updated' => '2007-03-17 01:18:31'
7367
						),
7368
						'Attachment' => array(
7369
							'id' => '1',
7370
							'comment_id' => 5,
7371
							'attachment' => 'attachment.zip',
7372
							'created' => '2007-03-18 10:51:23',
7373
							'updated' => '2007-03-18 10:53:31'
7374
						)
7375
					),
7376
					array(
7377
						'id' => '6',
7378
						'article_id' => '2',
7379
						'user_id' => '2',
7380
						'comment' => 'Second Comment for Second Article',
7381
						'published' => 'Y',
7382
						'created' => '2007-03-18 10:55:23',
7383
						'updated' => '2007-03-18 10:57:31',
7384
						'Article' => array(
7385
							'id' => '2',
7386
							'user_id' => '3',
7387
							'title' => 'Second Article',
7388
							'body' => 'Second Article Body',
7389
							'published' => 'Y',
7390
							'created' => '2007-03-18 10:41:23',
7391
							'updated' => '2007-03-18 10:43:31'
7392
						),
7393
						'User' => array(
7394
							'id' => '2',
7395
							'user' => 'nate',
7396
							'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7397
							'created' => '2007-03-17 01:18:23',
7398
							'updated' => '2007-03-17 01:20:31'
7399
						),
7400
						'Attachment' => array()
7401
					)
7402
				),
7403
				'Tag' => array(
7404
					array(
7405
						'id' => '1',
7406
						'tag' => 'tag1',
7407
						'created' => '2007-03-18 12:22:23',
7408
						'updated' => '2007-03-18 12:24:31'
7409
					),
7410
					array(
7411
						'id' => '3',
7412
						'tag' => 'tag3',
7413
						'created' => '2007-03-18 12:26:23',
7414
						'updated' => '2007-03-18 12:28:31'
7415
		))));
7416
7417
		$this->assertEquals($expected, $result);
7418
7419
		$Featured = new Featured();
7420
7421
		$Featured->recursive = 2;
7422
		$Featured->bindModel(array(
7423
			'belongsTo' => array(
7424
				'ArticleFeatured' => array(
7425
					'conditions' => "ArticleFeatured.published = 'Y'",
7426
					'fields' => 'id, title, user_id, published'
7427
				)
7428
			)
7429
		));
7430
7431
		$Featured->ArticleFeatured->unbindModel(array(
7432
			'hasMany' => array('Attachment', 'Comment'),
7433
			'hasAndBelongsToMany' => array('Tag'))
7434
		);
7435
7436
		$orderBy = 'ArticleFeatured.id ASC';
7437
		$result = $Featured->find('all', array(
7438
			'order' => $orderBy, 'limit' => 3
7439
		));
7440
7441
		$expected = array(
7442
			array(
7443
				'Featured' => array(
7444
					'id' => '1',
7445
					'article_featured_id' => '1',
7446
					'category_id' => '1',
7447
					'published_date' => '2007-03-31 10:39:23',
7448
					'end_date' => '2007-05-15 10:39:23',
7449
					'created' => '2007-03-18 10:39:23',
7450
					'updated' => '2007-03-18 10:41:31'
7451
				),
7452
				'ArticleFeatured' => array(
7453
					'id' => '1',
7454
					'title' => 'First Article',
7455
					'user_id' => '1',
7456
					'published' => 'Y',
7457
					'User' => array(
7458
						'id' => '1',
7459
						'user' => 'mariano',
7460
						'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7461
						'created' => '2007-03-17 01:16:23',
7462
						'updated' => '2007-03-17 01:18:31'
7463
					),
7464
					'Category' => array(),
7465
					'Featured' => array(
7466
						'id' => '1',
7467
						'article_featured_id' => '1',
7468
						'category_id' => '1',
7469
						'published_date' => '2007-03-31 10:39:23',
7470
						'end_date' => '2007-05-15 10:39:23',
7471
						'created' => '2007-03-18 10:39:23',
7472
						'updated' => '2007-03-18 10:41:31'
7473
				)),
7474
				'Category' => array(
7475
					'id' => '1',
7476
					'parent_id' => '0',
7477
					'name' => 'Category 1',
7478
					'created' => '2007-03-18 15:30:23',
7479
					'updated' => '2007-03-18 15:32:31'
7480
				)),
7481
			array(
7482
				'Featured' => array(
7483
					'id' => '2',
7484
					'article_featured_id' => '2',
7485
					'category_id' => '1',
7486
					'published_date' => '2007-03-31 10:39:23',
7487
					'end_date' => '2007-05-15 10:39:23',
7488
					'created' => '2007-03-18 10:39:23',
7489
					'updated' => '2007-03-18 10:41:31'
7490
				),
7491
				'ArticleFeatured' => array(
7492
					'id' => '2',
7493
					'title' => 'Second Article',
7494
					'user_id' => '3',
7495
					'published' => 'Y',
7496
					'User' => array(
7497
						'id' => '3',
7498
						'user' => 'larry',
7499
						'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7500
						'created' => '2007-03-17 01:20:23',
7501
						'updated' => '2007-03-17 01:22:31'
7502
					),
7503
					'Category' => array(),
7504
					'Featured' => array(
7505
						'id' => '2',
7506
						'article_featured_id' => '2',
7507
						'category_id' => '1',
7508
						'published_date' => '2007-03-31 10:39:23',
7509
						'end_date' => '2007-05-15 10:39:23',
7510
						'created' => '2007-03-18 10:39:23',
7511
						'updated' => '2007-03-18 10:41:31'
7512
				)),
7513
				'Category' => array(
7514
					'id' => '1',
7515
					'parent_id' => '0',
7516
					'name' => 'Category 1',
7517
					'created' => '2007-03-18 15:30:23',
7518
					'updated' => '2007-03-18 15:32:31'
7519
		)));
7520
		$this->assertEquals($expected, $result);
7521
	}
7522
7523
/**
7524
 * testRecursiveFindAllWithLimit method
7525
 *
7526
 * @return void
7527
 */
7528
	public function testRecursiveFindAllWithLimit() {
7529
		$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment');
7530
		$TestModel = new Article();
7531
7532
		$TestModel->hasMany['Comment']['limit'] = 2;
7533
7534
		$result = $TestModel->find('all', array(
7535
			'conditions' => array('Article.user_id' => 1)
7536
		));
7537
		$expected = array(
7538
			array(
7539
				'Article' => array(
7540
					'id' => '1',
7541
					'user_id' => '1',
7542
					'title' => 'First Article',
7543
					'body' => 'First Article Body',
7544
					'published' => 'Y',
7545
					'created' => '2007-03-18 10:39:23',
7546
					'updated' => '2007-03-18 10:41:31'
7547
				),
7548
				'User' => array(
7549
					'id' => '1',
7550
					'user' => 'mariano',
7551
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7552
					'created' => '2007-03-17 01:16:23',
7553
					'updated' => '2007-03-17 01:18:31'
7554
				),
7555
				'Comment' => array(
7556
					array(
7557
						'id' => '1',
7558
						'article_id' => '1',
7559
						'user_id' => '2',
7560
						'comment' => 'First Comment for First Article',
7561
						'published' => 'Y',
7562
						'created' => '2007-03-18 10:45:23',
7563
						'updated' => '2007-03-18 10:47:31'
7564
					),
7565
					array(
7566
						'id' => '2',
7567
						'article_id' => '1',
7568
						'user_id' => '4',
7569
						'comment' => 'Second Comment for First Article',
7570
						'published' => 'Y',
7571
						'created' => '2007-03-18 10:47:23',
7572
						'updated' => '2007-03-18 10:49:31'
7573
					),
7574
				),
7575
				'Tag' => array(
7576
					array(
7577
						'id' => '1',
7578
						'tag' => 'tag1',
7579
						'created' => '2007-03-18 12:22:23',
7580
						'updated' => '2007-03-18 12:24:31'
7581
					),
7582
					array(
7583
						'id' => '2',
7584
						'tag' => 'tag2',
7585
						'created' => '2007-03-18 12:24:23',
7586
						'updated' => '2007-03-18 12:26:31'
7587
			))),
7588
			array(
7589
				'Article' => array(
7590
					'id' => '3',
7591
					'user_id' => '1',
7592
					'title' => 'Third Article',
7593
					'body' => 'Third Article Body',
7594
					'published' => 'Y',
7595
					'created' => '2007-03-18 10:43:23',
7596
					'updated' => '2007-03-18 10:45:31'
7597
				),
7598
				'User' => array(
7599
					'id' => '1',
7600
					'user' => 'mariano',
7601
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7602
					'created' => '2007-03-17 01:16:23',
7603
					'updated' => '2007-03-17 01:18:31'
7604
				),
7605
				'Comment' => array(),
7606
				'Tag' => array()
7607
			)
7608
		);
7609
		$this->assertEquals($expected, $result);
7610
7611
		$TestModel->hasMany['Comment']['limit'] = 1;
7612
7613
		$result = $TestModel->find('all', array(
7614
			'conditions' => array('Article.user_id' => 3),
7615
			'limit' => 1,
7616
			'recursive' => 2
7617
		));
7618
		$expected = array(
7619
			array(
7620
				'Article' => array(
7621
					'id' => '2',
7622
					'user_id' => '3',
7623
					'title' => 'Second Article',
7624
					'body' => 'Second Article Body',
7625
					'published' => 'Y',
7626
					'created' => '2007-03-18 10:41:23',
7627
					'updated' => '2007-03-18 10:43:31'
7628
				),
7629
				'User' => array(
7630
					'id' => '3',
7631
					'user' => 'larry',
7632
					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7633
					'created' => '2007-03-17 01:20:23',
7634
					'updated' => '2007-03-17 01:22:31'
7635
				),
7636
				'Comment' => array(
7637
					array(
7638
						'id' => '5',
7639
						'article_id' => '2',
7640
						'user_id' => '1',
7641
						'comment' => 'First Comment for Second Article',
7642
						'published' => 'Y',
7643
						'created' => '2007-03-18 10:53:23',
7644
						'updated' => '2007-03-18 10:55:31',
7645
						'Article' => array(
7646
							'id' => '2',
7647
							'user_id' => '3',
7648
							'title' => 'Second Article',
7649
							'body' => 'Second Article Body',
7650
							'published' => 'Y',
7651
							'created' => '2007-03-18 10:41:23',
7652
							'updated' => '2007-03-18 10:43:31'
7653
						),
7654
						'User' => array(
7655
							'id' => '1',
7656
							'user' => 'mariano',
7657
							'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
7658
							'created' => '2007-03-17 01:16:23',
7659
							'updated' => '2007-03-17 01:18:31'
7660
						),
7661
						'Attachment' => array(
7662
							'id' => '1',
7663
							'comment_id' => 5,
7664
							'attachment' => 'attachment.zip',
7665
							'created' => '2007-03-18 10:51:23',
7666
							'updated' => '2007-03-18 10:53:31'
7667
						)
7668
					)
7669
				),
7670
				'Tag' => array(
7671
					array(
7672
						'id' => '1',
7673
						'tag' => 'tag1',
7674
						'created' => '2007-03-18 12:22:23',
7675
						'updated' => '2007-03-18 12:24:31'
7676
					),
7677
					array(
7678
						'id' => '3',
7679
						'tag' => 'tag3',
7680
						'created' => '2007-03-18 12:26:23',
7681
						'updated' => '2007-03-18 12:28:31'
7682
					)
7683
				)
7684
			)
7685
		);
7686
		$this->assertEquals($expected, $result);
7687
	}
7688
7689
/**
7690
 * Testing availability of $this->findQueryType in Model callbacks
7691
 *
7692
 * @return void
7693
 */
7694
	public function testFindQueryTypeInCallbacks() {
7695
		$this->loadFixtures('Comment');
7696
		$Comment = new AgainModifiedComment();
7697
		$comments = $Comment->find('all');
7698
		$this->assertEquals('all', $comments[0]['Comment']['querytype']);
7699
		$comments = $Comment->find('first');
7700
		$this->assertEquals('first', $comments['Comment']['querytype']);
7701
	}
7702
7703
/**
7704
 * testVirtualFields()
7705
 *
7706
 * Test correct fetching of virtual fields
7707
 * currently is not possible to do Relation.virtualField
7708
 *
7709
 * @return void
7710
 */
7711
	public function testVirtualFields() {
7712
		$this->loadFixtures('Post', 'Author');
7713
		$Post = ClassRegistry::init('Post');
7714
		$Post->virtualFields = array('two' => "1 + 1");
7715
		$result = $Post->find('first');
7716
		$this->assertEquals(2, $result['Post']['two']);
7717
7718
		// SQL Server does not support operators in expressions
7719
		if (!($this->db instanceof Sqlserver)) {
7720
			$Post->Author->virtualFields = array('false' => '1 = 2');
7721
			$result = $Post->find('first');
7722
			$this->assertEquals(2, $result['Post']['two']);
7723
			$this->assertFalse((bool)$result['Author']['false']);
7724
		}
7725
7726
		$result = $Post->find('first', array('fields' => array('author_id')));
7727
		$this->assertFalse(isset($result['Post']['two']));
7728
		$this->assertFalse(isset($result['Author']['false']));
7729
7730
		$result = $Post->find('first', array('fields' => array('author_id', 'two')));
7731
		$this->assertEquals(2, $result['Post']['two']);
7732
		$this->assertFalse(isset($result['Author']['false']));
7733
7734
		$result = $Post->find('first', array('fields' => array('two')));
7735
		$this->assertEquals(2, $result['Post']['two']);
7736
7737
		$Post->id = 1;
7738
		$result = $Post->field('two');
7739
		$this->assertEquals(2, $result);
7740
7741
		$result = $Post->find('first', array(
7742
			'conditions' => array('two' => 2),
7743
			'limit' => 1
7744
		));
7745
		$this->assertEquals(2, $result['Post']['two']);
7746
7747
		$result = $Post->find('first', array(
7748
			'conditions' => array('two <' => 3),
7749
			'limit' => 1
7750
		));
7751
		$this->assertEquals(2, $result['Post']['two']);
7752
7753
		$result = $Post->find('first', array(
7754
			'conditions' => array('NOT' => array('two >' => 3)),
7755
			'limit' => 1
7756
		));
7757
		$this->assertEquals(2, $result['Post']['two']);
7758
7759
		$dbo = $Post->getDataSource();
7760
		$Post->virtualFields = array('other_field' => 'Post.id + 1');
7761
		$result = $Post->find('first', array(
7762
			'conditions' => array('other_field' => 3),
7763
			'limit' => 1
7764
		));
7765
		$this->assertEquals(2, $result['Post']['id']);
7766
7767
		$Post->order = null;
7768
7769
		$Post->virtualFields = array('other_field' => 'Post.id + 1');
7770
		$result = $Post->find('all', array(
7771
			'fields' => array($dbo->calculate($Post, 'max', array('other_field')))
7772
		));
7773
		$this->assertEquals(4, $result[0][0]['other_field']);
7774
7775
		ClassRegistry::flush();
7776
		$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'));
7777
		$Writing->virtualFields = array('two' => "1 + 1");
7778
		$result = $Writing->find('first');
7779
		$this->assertEquals(2, $result['Writing']['two']);
7780
7781
		$Post->create();
7782
		$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
7783
		$result = $Post->field('other_field');
7784
		$this->assertEquals(4, $result);
7785
	}
7786
7787
/**
7788
 * testVirtualFieldsOrder()
7789
 *
7790
 * Test correct order on virtual fields
7791
 *
7792
 * @return void
7793
 */
7794
	public function testVirtualFieldsOrder() {
7795
		$this->loadFixtures('Post', 'Author');
7796
		$Post = ClassRegistry::init('Post');
7797
		$Post->virtualFields = array('other_field' => '10 - Post.id');
7798
		$result = $Post->find('list', array('order' => array('Post.other_field' => 'ASC')));
7799
		$expected = array(
7800
			'3' => 'Third Post',
7801
			'2' => 'Second Post',
7802
			'1' => 'First Post'
7803
		);
7804
		$this->assertEquals($expected, $result);
7805
7806
		$result = $Post->find('list', array('order' => array('Post.other_field' => 'DESC')));
7807
		$expected = array(
7808
			'1' => 'First Post',
7809
			'2' => 'Second Post',
7810
			'3' => 'Third Post'
7811
		);
7812
		$this->assertEquals($expected, $result);
7813
7814
		$Post->Author->virtualFields = array('joined' => 'Post.id * Author.id');
7815
		$result = $Post->find('all', array(
7816
			'order' => array('Post.id' => 'ASC')
7817
		));
7818
		$result = Hash::extract($result, '{n}.Author.joined');
7819
		$expected = array(1, 6, 3);
7820
		$this->assertEquals($expected, $result);
7821
7822
		$result = $Post->find('all', array('order' => array('Author.joined' => 'ASC')));
7823
		$result = Hash::extract($result, '{n}.Author.joined');
7824
		$expected = array(1, 3, 6);
7825
		$this->assertEquals($expected, $result);
7826
7827
		$result = $Post->find('all', array('order' => array('Author.joined' => 'DESC')));
7828
		$result = Hash::extract($result, '{n}.Author.joined');
7829
		$expected = array(6, 3, 1);
7830
		$this->assertEquals($expected, $result);
7831
	}
7832
7833
/**
7834
 * testVirtualFieldsMysql()
7835
 *
7836
 * Test correct fetching of virtual fields
7837
 * currently is not possible to do Relation.virtualField
7838
 *
7839
 * @return void
7840
 */
7841
	public function testVirtualFieldsMysql() {
7842
		$this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFields test only compatible with Mysql.');
7843
7844
		$this->loadFixtures('Post', 'Author');
7845
		$Post = ClassRegistry::init('Post');
7846
7847
		$Post->create();
7848
		$Post->virtualFields = array(
7849
			'low_title' => 'lower(Post.title)',
7850
			'unique_test_field' => 'COUNT(Post.id)'
7851
		);
7852
7853
		$expectation = array(
7854
			'Post' => array(
7855
				'low_title' => 'first post',
7856
				'unique_test_field' => 1
7857
			)
7858
		);
7859
7860
		$result = $Post->find('first', array(
7861
			'fields' => array_keys($Post->virtualFields),
7862
			'group' => array('low_title')
7863
		));
7864
7865
		$this->assertEquals($expectation, $result);
7866
7867
		$Author = ClassRegistry::init('Author');
7868
		$Author->virtualFields = array(
7869
			'full_name' => 'CONCAT(Author.user, " ", Author.id)'
7870
		);
7871
7872
		$result = $Author->find('first', array(
7873
			'conditions' => array('Author.user' => 'mariano'),
7874
			'fields' => array('Author.password', 'Author.full_name'),
7875
			'recursive' => -1
7876
		));
7877
		$this->assertTrue(isset($result['Author']['full_name']));
7878
7879
		$result = $Author->find('first', array(
7880
			'conditions' => array('Author.user' => 'mariano'),
7881
			'fields' => array('Author.full_name', 'Author.password'),
7882
			'recursive' => -1
7883
		));
7884
		$this->assertTrue(isset($result['Author']['full_name']));
7885
	}
7886
7887
/**
7888
 * test that virtual fields work when they don't contain functions.
7889
 *
7890
 * @return void
7891
 */
7892
	public function testVirtualFieldAsAString() {
7893
		$this->loadFixtures('Post', 'Author');
7894
		$Post = new Post();
7895
		$Post->virtualFields = array(
7896
			'writer' => 'Author.user'
7897
		);
7898
		$result = $Post->find('first');
7899
		$this->assertTrue(isset($result['Post']['writer']), 'virtual field not fetched %s');
7900
	}
7901
7902
/**
7903
 * test that isVirtualField will accept both aliased and non aliased fieldnames
7904
 *
7905
 * @return void
7906
 */
7907
	public function testIsVirtualField() {
7908
		$this->loadFixtures('Post');
7909
		$Post = ClassRegistry::init('Post');
7910
		$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
7911
7912
		$this->assertTrue($Post->isVirtualField('other_field'));
7913
		$this->assertTrue($Post->isVirtualField('Post.other_field'));
7914
		$this->assertFalse($Post->isVirtualField('Comment.other_field'), 'Other models should not match.');
7915
		$this->assertFalse($Post->isVirtualField('id'));
7916
		$this->assertFalse($Post->isVirtualField('Post.id'));
7917
		$this->assertFalse($Post->isVirtualField(array()));
7918
	}
7919
7920
/**
7921
 * test that getting virtual fields works with and without model alias attached
7922
 *
7923
 * @return void
7924
 */
7925
	public function testGetVirtualField() {
7926
		$this->loadFixtures('Post');
7927
		$Post = ClassRegistry::init('Post');
7928
		$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
7929
7930
		$this->assertEquals($Post->getVirtualField('other_field'), $Post->virtualFields['other_field']);
7931
		$this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']);
7932
	}
7933
7934
/**
7935
 * test that checks for error when NOT condition passed in key and a 1 element array value
7936
 *
7937
 * @return void
7938
 */
7939 View Code Duplication
	public function testNotInArrayWithOneValue() {
7940
		$this->loadFixtures('Article');
7941
		$Article = new Article();
7942
		$Article->recursive = -1;
7943
7944
		$result = $Article->find(
7945
			'all',
7946
			array(
7947
				'conditions' => array(
7948
					'Article.id NOT' => array(1)
7949
				)
7950
			)
7951
		);
7952
		$this->assertTrue(is_array($result) && !empty($result));
7953
	}
7954
7955
/**
7956
 * test to assert that != in key together with a single element array will work
7957
 *
7958
 * @return void
7959
 */
7960 View Code Duplication
	public function testNotEqualsInArrayWithOneValue() {
7961
		$this->loadFixtures('Article');
7962
		$Article = new Article();
7963
		$Article->recursive = -1;
7964
7965
		$result = $Article->find(
7966
			'all',
7967
			array(
7968
				'conditions' => array(
7969
					'Article.id !=' => array(1)
7970
				)
7971
			)
7972
		);
7973
		$this->assertTrue(is_array($result) && !empty($result));
7974
	}
7975
7976
/**
7977
 * test custom find method
7978
 *
7979
 * @return void
7980
 */
7981
	public function testfindCustom() {
7982
		$this->loadFixtures('Article');
7983
		$Article = new CustomArticle();
7984
		$data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
7985
		$Article->create($data);
7986
		$Article->save(null, false);
7987
		$this->assertEquals(4, $Article->id);
7988
7989
		$result = $Article->find('published');
7990
		$this->assertEquals(3, count($result));
7991
7992
		$result = $Article->find('unPublished');
7993
		$this->assertEquals(1, count($result));
7994
	}
7995
7996
}
7997