Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ModelWriteTest 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
App::uses('MockTransactionDboSource', 'Model/Datasource');
20
App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
21
App::uses('MockManyTransactionDboSource', 'Model/Datasource');
22
App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
23
 
24
require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
25
 
26
/**
27
 * ModelWriteTest
28
 *
29
 * @package       Cake.Test.Case.Model
30
 */
31
class ModelWriteTest extends BaseModelTest {
32
 
33
/**
34
 * override locale to the default (eng).
35
 *
36
 * @return void
37
 */
38
	public function setUp() {
39
		parent::setUp();
40
		Configure::write('Config.language', 'eng');
41
	}
42
 
43
/**
44
 * Test save() failing when there is no data.
45
 *
46
 * @return void
47
 */
48
	public function testInsertNoData() {
49
		$this->loadFixtures('Bid');
50
		$Bid = ClassRegistry::init('Bid');
51
 
52
		$this->assertFalse($Bid->save());
53
 
54
		$result = $Bid->save(array('Bid' => array()));
55
		$this->assertFalse($result);
56
 
57
		$result = $Bid->save(array('Bid' => array('not in schema' => 1)));
58
		$this->assertFalse($result);
59
	}
60
 
61
/**
62
 * testInsertAnotherHabtmRecordWithSameForeignKey method
63
 *
64
 * @return void
65
 */
66
	public function testInsertAnotherHabtmRecordWithSameForeignKey() {
67
		$this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
68
		$TestModel = new JoinA();
69
 
70
		$result = $TestModel->JoinAsJoinB->findById(1);
71
		$expected = array(
72
			'JoinAsJoinB' => array(
73
				'id' => 1,
74
				'join_a_id' => 1,
75
				'join_b_id' => 2,
76
				'other' => 'Data for Join A 1 Join B 2',
77
				'created' => '2008-01-03 10:56:33',
78
				'updated' => '2008-01-03 10:56:33'
79
		));
80
		$this->assertEquals($expected, $result);
81
 
82
		$TestModel->JoinAsJoinB->create();
83
		$data = array(
84
			'join_a_id' => 1,
85
			'join_b_id' => 1,
86
			'other' => 'Data for Join A 1 Join B 1',
87
			'created' => '2008-01-03 10:56:44',
88
			'updated' => '2008-01-03 10:56:44'
89
		);
90
		$result = $TestModel->JoinAsJoinB->save($data);
91
		$lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
92
		$data['id'] = $lastInsertId;
93
		$this->assertEquals(array('JoinAsJoinB' => $data), $result);
94
		$this->assertTrue($lastInsertId > 0);
95
 
96
		$result = $TestModel->JoinAsJoinB->findById(1);
97
		$expected = array(
98
			'JoinAsJoinB' => array(
99
				'id' => 1,
100
				'join_a_id' => 1,
101
				'join_b_id' => 2,
102
				'other' => 'Data for Join A 1 Join B 2',
103
				'created' => '2008-01-03 10:56:33',
104
				'updated' => '2008-01-03 10:56:33'
105
		));
106
		$this->assertEquals($expected, $result);
107
 
108
		$updatedValue = 'UPDATED Data for Join A 1 Join B 2';
109
		$TestModel->JoinAsJoinB->id = 1;
110
		$result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
111
		$this->assertFalse(empty($result));
112
 
113
		$result = $TestModel->JoinAsJoinB->findById(1);
114
		$this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
115
	}
116
 
117
/**
118
 * testSaveDateAsFirstEntry method
119
 *
120
 * @return void
121
 */
122
	public function testSaveDateAsFirstEntry() {
123
		$this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
124
 
125
		$Article = new Article();
126
 
127
		$data = array(
128
			'Article' => array(
129
				'created' => array(
130
					'day' => '1',
131
					'month' => '1',
132
					'year' => '2008'
133
				),
134
				'title' => 'Test Title',
135
				'user_id' => 1
136
		));
137
		$Article->create();
138
		$result = $Article->save($data);
139
		$this->assertFalse(empty($result));
140
 
141
		$testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
142
 
143
		$this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
144
		$this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
145
	}
146
 
147
/**
148
 * testUnderscoreFieldSave method
149
 *
150
 * @return void
151
 */
152
	public function testUnderscoreFieldSave() {
153
		$this->loadFixtures('UnderscoreField');
154
		$UnderscoreField = new UnderscoreField();
155
 
156
		$currentCount = $UnderscoreField->find('count');
157
		$this->assertEquals(3, $currentCount);
158
		$data = array('UnderscoreField' => array(
159
			'user_id' => '1',
160
			'my_model_has_a_field' => 'Content here',
161
			'body' => 'Body',
162
			'published' => 'Y',
163
			'another_field' => 4
164
		));
165
		$ret = $UnderscoreField->save($data);
166
		$this->assertFalse(empty($ret));
167
 
168
		$currentCount = $UnderscoreField->find('count');
169
		$this->assertEquals(4, $currentCount);
170
	}
171
 
172
/**
173
 * testAutoSaveUuid method
174
 *
175
 * @return void
176
 */
177
	public function testAutoSaveUuid() {
178
		// SQLite does not support non-integer primary keys
179
		$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
180
 
181
		$this->loadFixtures('Uuid');
182
		$TestModel = new Uuid();
183
 
184
		$TestModel->save(array('title' => 'Test record'));
185
		$result = $TestModel->findByTitle('Test record');
186
		$this->assertEquals(
187
			array('id', 'title', 'count', 'created', 'updated'),
188
			array_keys($result['Uuid'])
189
		);
190
		$this->assertEquals(36, strlen($result['Uuid']['id']));
191
	}
192
 
193
/**
194
 * Ensure that if the id key is null but present the save doesn't fail (with an
195
 * x sql error: "Column id specified twice")
196
 *
197
 * @return void
198
 */
199
	public function testSaveUuidNull() {
200
		// SQLite does not support non-integer primary keys
201
		$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
202
 
203
		$this->loadFixtures('Uuid');
204
		$TestModel = new Uuid();
205
 
206
		$TestModel->save(array('title' => 'Test record', 'id' => null));
207
		$result = $TestModel->findByTitle('Test record');
208
		$this->assertEquals(
209
			array('id', 'title', 'count', 'created', 'updated'),
210
			array_keys($result['Uuid'])
211
		);
212
		$this->assertEquals(36, strlen($result['Uuid']['id']));
213
	}
214
 
215
/**
216
 * testZeroDefaultFieldValue method
217
 *
218
 * @return void
219
 */
220
	public function testZeroDefaultFieldValue() {
221
		$this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
222
 
223
		$this->loadFixtures('DataTest');
224
		$TestModel = new DataTest();
225
 
226
		$TestModel->create(array());
227
		$TestModel->save();
228
		$result = $TestModel->findById($TestModel->id);
229
		$this->assertEquals(0, $result['DataTest']['count']);
230
		$this->assertEquals(0, $result['DataTest']['float']);
231
	}
232
 
233
/**
234
 * Tests validation parameter order in custom validation methods
235
 *
236
 * @return void
237
 */
238
	public function testAllowSimulatedFields() {
239
		$TestModel = new ValidationTest1();
240
 
241
		$TestModel->create(array(
242
			'title' => 'foo',
243
			'bar' => 'baz'
244
		));
245
		$expected = array(
246
			'ValidationTest1' => array(
247
				'title' => 'foo',
248
				'bar' => 'baz'
249
		));
250
		$this->assertEquals($expected, $TestModel->data);
251
	}
252
 
253
/**
254
 * test that Caches are getting cleared on save().
255
 * ensure that both inflections of controller names are getting cleared
256
 * as URL for controller could be either overallFavorites/index or overall_favorites/index
257
 *
258
 * @return void
259
 */
260
	public function testCacheClearOnSave() {
261
		$_back = array(
262
			'check' => Configure::read('Cache.check'),
263
			'disable' => Configure::read('Cache.disable'),
264
		);
265
		Configure::write('Cache.check', true);
266
		Configure::write('Cache.disable', false);
267
 
268
		$this->loadFixtures('OverallFavorite');
269
		$OverallFavorite = new OverallFavorite();
270
 
271
		touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
272
		touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
273
 
274
		$data = array(
275
			'OverallFavorite' => array(
276
				'id' => 22,
277
				'model_type' => '8-track',
278
				'model_id' => '3',
279
				'priority' => '1'
280
			)
281
		);
282
		$OverallFavorite->create($data);
283
		$OverallFavorite->save();
284
 
285
		$this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
286
		$this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
287
 
288
		Configure::write('Cache.check', $_back['check']);
289
		Configure::write('Cache.disable', $_back['disable']);
290
	}
291
 
292
/**
293
 * test that save() resets whitelist on failed save
294
 */
295
	public function testSaveFieldListResetsWhitelistOnFailedSave() {
296
		$this->loadFixtures('Bidding');
297
		$model = new Bidding();
298
		$whitelist = array('title');
299
		$model->whitelist = $whitelist;
300
		$result = $model->save(
301
			array(),
302
			array('fieldList' => array('body'))
303
		);
304
		$this->assertFalse($result);
305
		$this->assertEquals($whitelist, $model->whitelist);
306
	}
307
 
308
/**
309
 * testSaveWithCounterCache method
310
 *
311
 * @return void
312
 */
313
	public function testSaveWithCounterCache() {
314
		$this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
315
		$TestModel = new Syfile();
316
		$TestModel2 = new Item();
317
 
318
		$result = $TestModel->findById(1);
319
		$this->assertNull($result['Syfile']['item_count']);
320
 
321
		$TestModel2->save(array(
322
			'name' => 'Item 7',
323
			'syfile_id' => 1,
324
			'published' => false
325
		));
326
 
327
		$result = $TestModel->findById(1);
328
		$this->assertEquals(2, $result['Syfile']['item_count']);
329
 
330
		$TestModel2->delete(1);
331
		$result = $TestModel->findById(1);
332
		$this->assertEquals(1, $result['Syfile']['item_count']);
333
 
334
		$TestModel2->id = 2;
335
		$TestModel2->saveField('syfile_id', 1);
336
 
337
		$result = $TestModel->findById(1);
338
		$this->assertEquals(2, $result['Syfile']['item_count']);
339
 
340
		$result = $TestModel->findById(2);
341
		$this->assertEquals(0, $result['Syfile']['item_count']);
342
	}
343
 
344
/**
345
 * Tests that counter caches are updated when records are added
346
 *
347
 * @return void
348
 */
349
	public function testCounterCacheIncrease() {
350
		$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
351
		$User = new CounterCacheUser();
352
		$Post = new CounterCachePost();
353
		$data = array('Post' => array(
354
			'id' => 22,
355
			'title' => 'New Post',
356
			'user_id' => 66
357
		));
358
 
359
		$Post->save($data);
360
		$user = $User->find('first', array(
361
			'conditions' => array('id' => 66),
362
			'recursive' => -1
363
		));
364
 
365
		$result = $user[$User->alias]['post_count'];
366
		$expected = 3;
367
		$this->assertEquals($expected, $result);
368
	}
369
 
370
/**
371
 * Tests that counter caches are updated when records are deleted
372
 *
373
 * @return void
374
 */
375
	public function testCounterCacheDecrease() {
376
		$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
377
		$User = new CounterCacheUser();
378
		$Post = new CounterCachePost();
379
 
380
		$Post->delete(2);
381
		$user = $User->find('first', array(
382
			'conditions' => array('id' => 66),
383
			'recursive' => -1
384
		));
385
 
386
		$result = $user[$User->alias]['post_count'];
387
		$expected = 1;
388
		$this->assertEquals($expected, $result);
389
	}
390
 
391
/**
392
 * Tests that counter caches are updated when foreign keys of counted records change
393
 *
394
 * @return void
395
 */
396
	public function testCounterCacheUpdated() {
397
		$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
398
		$User = new CounterCacheUser();
399
		$Post = new CounterCachePost();
400
 
401
		$data = $Post->find('first', array(
402
			'conditions' => array('id' => 1),
403
			'recursive' => -1
404
		));
405
		$data[$Post->alias]['user_id'] = 301;
406
		$Post->save($data);
407
 
408
		$users = $User->find('all', array('order' => 'User.id'));
409
		$this->assertEquals(1, $users[0]['User']['post_count']);
410
		$this->assertEquals(2, $users[1]['User']['post_count']);
411
	}
412
 
413
/**
414
 * Test counter cache with models that use a non-standard (i.e. not using 'id')
415
 * as their primary key.
416
 *
417
 * @return void
418
 */
419
	public function testCounterCacheWithNonstandardPrimaryKey() {
420
		$this->loadFixtures(
421
			'CounterCacheUserNonstandardPrimaryKey',
422
			'CounterCachePostNonstandardPrimaryKey'
423
		);
424
 
425
		$User = new CounterCacheUserNonstandardPrimaryKey();
426
		$Post = new CounterCachePostNonstandardPrimaryKey();
427
 
428
		$data = $Post->find('first', array(
429
			'conditions' => array('pid' => 1),
430
			'recursive' => -1
431
		));
432
		$data[$Post->alias]['uid'] = 301;
433
		$Post->save($data);
434
 
435
		$users = $User->find('all', array('order' => 'User.uid'));
436
		$this->assertEquals(1, $users[0]['User']['post_count']);
437
		$this->assertEquals(2, $users[1]['User']['post_count']);
438
	}
439
 
440
/**
441
 * test Counter Cache With Self Joining table
442
 *
443
 * @return void
444
 */
445
	public function testCounterCacheWithSelfJoin() {
446
		$this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
447
 
448
		$this->loadFixtures('CategoryThread');
449
		$column = 'COLUMN ';
450
		if ($this->db instanceof Sqlserver) {
451
			$column = '';
452
		}
453
		$column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
454
		$this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
455
		$this->db->flushMethodCache();
456
		$Category = new CategoryThread();
457
		$result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
458
		$this->assertFalse(empty($result));
459
 
460
		$Category = new CategoryThread();
461
		$Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
462
		$Category->updateCounterCache(array('parent_id' => 5));
463
		$result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
464
		$expected = array(1);
465
		$this->assertEquals($expected, $result);
466
	}
467
 
468
/**
469
 * testSaveWithCounterCacheScope method
470
 *
471
 * @return void
472
 */
473
	public function testSaveWithCounterCacheScope() {
474
		$this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
475
		$TestModel = new Syfile();
476
		$TestModel2 = new Item();
477
		$TestModel2->belongsTo['Syfile']['counterCache'] = true;
478
		$TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
479
 
480
		$result = $TestModel->findById(1);
481
		$this->assertNull($result['Syfile']['item_count']);
482
 
483
		$TestModel2->save(array(
484
			'name' => 'Item 7',
485
			'syfile_id' => 1,
486
			'published' => true
487
		));
488
 
489
		$result = $TestModel->findById(1);
490
 
491
		$this->assertEquals(1, $result['Syfile']['item_count']);
492
 
493
		$TestModel2->id = 1;
494
		$TestModel2->saveField('published', true);
495
		$result = $TestModel->findById(1);
496
		$this->assertEquals(2, $result['Syfile']['item_count']);
497
 
498
		$TestModel2->save(array(
499
			'id' => 1,
500
			'syfile_id' => 1,
501
			'published' => false
502
		));
503
 
504
		$result = $TestModel->findById(1);
505
		$this->assertEquals(1, $result['Syfile']['item_count']);
506
	}
507
 
508
/**
509
 * Tests having multiple counter caches for an associated model
510
 *
511
 * @return void
512
 */
513
	public function testCounterCacheMultipleCaches() {
514
		$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
515
		$User = new CounterCacheUser();
516
		$Post = new CounterCachePost();
517
		$Post->unbindModel(array('belongsTo' => array('User')), false);
518
		$Post->bindModel(array(
519
			'belongsTo' => array(
520
				'User' => array(
521
					'className' => 'CounterCacheUser',
522
					'foreignKey' => 'user_id',
523
					'counterCache' => array(
524
						true,
525
						'posts_published' => array('Post.published' => true)
526
					)
527
				)
528
			)
529
		), false);
530
 
531
		// Count Increase
532
		$data = array('Post' => array(
533
			'id' => 22,
534
			'title' => 'New Post',
535
			'user_id' => 66,
536
			'published' => true
537
		));
538
		$Post->save($data);
539
		$result = $User->find('first', array(
540
			'conditions' => array('id' => 66),
541
			'recursive' => -1
542
		));
543
		$this->assertEquals(3, $result[$User->alias]['post_count']);
544
		$this->assertEquals(2, $result[$User->alias]['posts_published']);
545
 
546
		// Count decrease
547
		$Post->delete(1);
548
		$result = $User->find('first', array(
549
			'conditions' => array('id' => 66),
550
			'recursive' => -1
551
		));
552
		$this->assertEquals(2, $result[$User->alias]['post_count']);
553
		$this->assertEquals(2, $result[$User->alias]['posts_published']);
554
 
555
		// Count update
556
		$data = $Post->find('first', array(
557
			'conditions' => array('id' => 1),
558
			'recursive' => -1
559
		));
560
		$data[$Post->alias]['user_id'] = 301;
561
		$Post->save($data);
562
		$result = $User->find('all', array('order' => 'User.id'));
563
		$this->assertEquals(2, $result[0]['User']['post_count']);
564
		$this->assertEquals(1, $result[1]['User']['posts_published']);
565
	}
566
 
567
/**
568
 * Tests that counter caches are unchanged when using 'counterCache' => false
569
 *
570
 * @return void
571
 */
572
	public function testCounterCacheSkip() {
573
		$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
574
		$User = new CounterCacheUser();
575
		$Post = new CounterCachePost();
576
 
577
		$data = $Post->find('first', array(
578
			'conditions' => array('id' => 1),
579
			'recursive' => -1
580
		));
581
		$data[$Post->alias]['user_id'] = 301;
582
		$Post->save($data, array('counterCache' => false));
583
 
584
		$users = $User->find('all', array('order' => 'User.id'));
585
		$this->assertEquals(2, $users[0]['User']['post_count']);
586
		$this->assertEquals(1, $users[1]['User']['post_count']);
587
	}
588
 
589
/**
590
 * test that beforeValidate returning false can abort saves.
591
 *
592
 * @return void
593
 */
594
	public function testBeforeValidateSaveAbortion() {
595
		$this->loadFixtures('Post');
596
		$Model = new CallbackPostTestModel();
597
		$Model->beforeValidateReturn = false;
598
 
599
		$data = array(
600
			'title' => 'new article',
601
			'body' => 'this is some text.'
602
		);
603
		$Model->create();
604
		$result = $Model->save($data);
605
		$this->assertFalse($result);
606
	}
607
 
608
/**
609
 * test that beforeSave returning false can abort saves.
610
 *
611
 * @return void
612
 */
613
	public function testBeforeSaveSaveAbortion() {
614
		$this->loadFixtures('Post');
615
		$Model = new CallbackPostTestModel();
616
		$Model->beforeSaveReturn = false;
617
 
618
		$data = array(
619
			'title' => 'new article',
620
			'body' => 'this is some text.'
621
		);
622
		$Model->create();
623
		$result = $Model->save($data);
624
		$this->assertFalse($result);
625
	}
626
 
627
/**
628
 * testSaveField method
629
 *
630
 * @return void
631
 */
632
	public function testSaveField() {
633
		$this->loadFixtures('Article');
634
		$TestModel = new Article();
635
 
636
		$TestModel->id = 1;
637
		$result = $TestModel->saveField('title', 'New First Article');
638
		$this->assertFalse(empty($result));
639
 
640
		$TestModel->recursive = -1;
641
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
642
		$expected = array('Article' => array(
643
			'id' => '1',
644
			'user_id' => '1',
645
			'title' => 'New First Article',
646
			'body' => 'First Article Body'
647
		));
648
		$this->assertEquals($expected, $result);
649
 
650
		$TestModel->id = 1;
651
		$result = $TestModel->saveField('title', '');
652
		$this->assertFalse(empty($result));
653
 
654
		$TestModel->recursive = -1;
655
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
656
		$expected = array('Article' => array(
657
			'id' => '1',
658
			'user_id' => '1',
659
			'title' => '',
660
			'body' => 'First Article Body'
661
		));
662
		$result['Article']['title'] = trim($result['Article']['title']);
663
		$this->assertEquals($expected, $result);
664
 
665
		$TestModel->id = 1;
666
		$TestModel->set('body', 'Messed up data');
667
		$result = $TestModel->saveField('title', 'First Article');
668
		$this->assertFalse(empty($result));
669
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
670
		$expected = array('Article' => array(
671
			'id' => '1',
672
			'user_id' => '1',
673
			'title' => 'First Article',
674
			'body' => 'First Article Body'
675
		));
676
		$this->assertEquals($expected, $result);
677
 
678
		$TestModel->recursive = -1;
679
		$TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
680
 
681
		$TestModel->id = 1;
682
		$result = $TestModel->saveField('title', '', true);
683
		$this->assertFalse($result);
684
 
685
		$TestModel->recursive = -1;
686
		$TestModel->id = 1;
687
		$result = $TestModel->saveField('user_id', 9999);
688
		$this->assertTrue((bool)$result);
689
 
690
		$result = $TestModel->read(array('id', 'user_id'), 1);
691
		$expected = array('Article' => array(
692
			'id' => '1',
693
			'user_id' => '9999',
694
		));
695
		$this->assertEquals($expected, $result);
696
 
697
		$this->loadFixtures('Node', 'Dependency');
698
		$Node = new Node();
699
		$Node->set('id', 1);
700
		$result = $Node->read();
701
		$this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
702
 
703
		$Node->saveField('state', 10);
704
		$result = $Node->read();
705
		$this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
706
	}
707
 
708
/**
709
 * testSaveWithCreate method
710
 *
711
 * @return void
712
 */
713
	public function testSaveWithCreate() {
714
		$this->loadFixtures(
715
			'User',
716
			'Article',
717
			'User',
718
			'Comment',
719
			'Tag',
720
			'ArticlesTag',
721
			'Attachment'
722
		);
723
		$TestModel = new User();
724
 
725
		$data = array('User' => array(
726
			'user' => 'user',
727
			'password' => ''
728
		));
729
		$result = $TestModel->save($data);
730
		$this->assertFalse($result);
731
		$this->assertTrue(!empty($TestModel->validationErrors));
732
 
733
		$TestModel = new Article();
734
 
735
		$data = array('Article' => array(
736
			'user_id' => '',
737
			'title' => '',
738
			'body' => ''
739
		));
740
		$result = $TestModel->create($data) && $TestModel->save();
741
		$this->assertFalse($result);
742
		$this->assertTrue(!empty($TestModel->validationErrors));
743
 
744
		$data = array('Article' => array(
745
			'id' => 1,
746
			'user_id' => '1',
747
			'title' => 'New First Article',
748
			'body' => ''
749
		));
750
		$result = $TestModel->create($data) && $TestModel->save();
751
		$this->assertFalse($result);
752
 
753
		$data = array('Article' => array(
754
			'id' => 1,
755
			'title' => 'New First Article'
756
		));
757
		$result = $TestModel->create() && $TestModel->save($data, false);
758
		$this->assertFalse(empty($result));
759
 
760
		$TestModel->recursive = -1;
761
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
762
		$expected = array('Article' => array(
763
			'id' => '1',
764
			'user_id' => '1',
765
			'title' => 'New First Article',
766
			'body' => 'First Article Body',
767
			'published' => 'N'
768
		));
769
		$this->assertEquals($expected, $result);
770
 
771
		$data = array('Article' => array(
772
			'id' => 1,
773
			'user_id' => '2',
774
			'title' => 'First Article',
775
			'body' => 'New First Article Body',
776
			'published' => 'Y'
777
		));
778
		$result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
779
		$this->assertFalse(empty($result));
780
 
781
		$TestModel->recursive = -1;
782
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
783
		$expected = array('Article' => array(
784
			'id' => '1',
785
			'user_id' => '1',
786
			'title' => 'First Article',
787
			'body' => 'First Article Body',
788
			'published' => 'Y'
789
		));
790
		$this->assertEquals($expected, $result);
791
 
792
		$data = array(
793
			'Article' => array(
794
				'user_id' => '2',
795
				'title' => 'New Article',
796
				'body' => 'New Article Body',
797
				'created' => '2007-03-18 14:55:23',
798
				'updated' => '2007-03-18 14:57:31'
799
			),
800
			'Tag' => array('Tag' => array(1, 3))
801
		);
802
		$TestModel->create();
803
		$result = $TestModel->create() && $TestModel->save($data);
804
		$this->assertFalse(empty($result));
805
 
806
		$TestModel->recursive = 2;
807
		$result = $TestModel->read(null, 4);
808
		$expected = array(
809
			'Article' => array(
810
				'id' => '4',
811
				'user_id' => '2',
812
				'title' => 'New Article',
813
				'body' => 'New Article Body',
814
				'published' => 'N',
815
				'created' => '2007-03-18 14:55:23',
816
				'updated' => '2007-03-18 14:57:31'
817
			),
818
			'User' => array(
819
				'id' => '2',
820
				'user' => 'nate',
821
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
822
				'created' => '2007-03-17 01:18:23',
823
				'updated' => '2007-03-17 01:20:31'
824
			),
825
			'Comment' => array(),
826
			'Tag' => array(
827
				array(
828
					'id' => '1',
829
					'tag' => 'tag1',
830
					'created' => '2007-03-18 12:22:23',
831
					'updated' => '2007-03-18 12:24:31'
832
				),
833
				array(
834
					'id' => '3',
835
					'tag' => 'tag3',
836
					'created' => '2007-03-18 12:26:23',
837
					'updated' => '2007-03-18 12:28:31'
838
		)));
839
		$this->assertEquals($expected, $result);
840
 
841
		$data = array('Comment' => array(
842
			'article_id' => '4',
843
			'user_id' => '1',
844
			'comment' => 'Comment New Article',
845
			'published' => 'Y',
846
			'created' => '2007-03-18 14:57:23',
847
			'updated' => '2007-03-18 14:59:31'
848
		));
849
		$result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
850
		$this->assertFalse(empty($result));
851
 
852
		$data = array('Attachment' => array(
853
			'comment_id' => '7',
854
			'attachment' => 'newattachment.zip',
855
			'created' => '2007-03-18 15:02:23',
856
			'updated' => '2007-03-18 15:04:31'
857
		));
858
		$result = $TestModel->Comment->Attachment->save($data);
859
		$this->assertFalse(empty($result));
860
 
861
		$TestModel->recursive = 2;
862
		$result = $TestModel->read(null, 4);
863
		$expected = array(
864
			'Article' => array(
865
				'id' => '4',
866
				'user_id' => '2',
867
				'title' => 'New Article',
868
				'body' => 'New Article Body',
869
				'published' => 'N',
870
				'created' => '2007-03-18 14:55:23',
871
				'updated' => '2007-03-18 14:57:31'
872
			),
873
			'User' => array(
874
				'id' => '2',
875
				'user' => 'nate',
876
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
877
				'created' => '2007-03-17 01:18:23',
878
				'updated' => '2007-03-17 01:20:31'
879
			),
880
			'Comment' => array(
881
				array(
882
					'id' => '7',
883
					'article_id' => '4',
884
					'user_id' => '1',
885
					'comment' => 'Comment New Article',
886
					'published' => 'Y',
887
					'created' => '2007-03-18 14:57:23',
888
					'updated' => '2007-03-18 14:59:31',
889
					'Article' => array(
890
						'id' => '4',
891
						'user_id' => '2',
892
						'title' => 'New Article',
893
						'body' => 'New Article Body',
894
						'published' => 'N',
895
						'created' => '2007-03-18 14:55:23',
896
						'updated' => '2007-03-18 14:57:31'
897
					),
898
					'User' => array(
899
						'id' => '1',
900
						'user' => 'mariano',
901
						'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
902
						'created' => '2007-03-17 01:16:23',
903
						'updated' => '2007-03-17 01:18:31'
904
					),
905
					'Attachment' => array(
906
						'id' => '2',
907
						'comment_id' => '7',
908
						'attachment' => 'newattachment.zip',
909
						'created' => '2007-03-18 15:02:23',
910
						'updated' => '2007-03-18 15:04:31'
911
			))),
912
			'Tag' => array(
913
				array(
914
					'id' => '1',
915
					'tag' => 'tag1',
916
					'created' => '2007-03-18 12:22:23',
917
					'updated' => '2007-03-18 12:24:31'
918
				),
919
				array(
920
					'id' => '3',
921
					'tag' => 'tag3',
922
					'created' => '2007-03-18 12:26:23',
923
					'updated' => '2007-03-18 12:28:31'
924
		)));
925
 
926
		$this->assertEquals($expected, $result);
927
	}
928
 
929
/**
930
 * test that a null Id doesn't cause errors
931
 *
932
 * @return void
933
 */
934
	public function testSaveWithNullId() {
935
		$this->loadFixtures('User');
936
		$User = new User();
937
		$User->read(null, 1);
938
		$User->data['User']['id'] = null;
939
		$result = $User->save(array('password' => 'test'));
940
		$this->assertFalse(empty($result));
941
		$this->assertTrue($User->id > 0);
942
 
943
		$User->read(null, 2);
944
		$User->data['User']['id'] = null;
945
		$result = $User->save(array('password' => 'test'));
946
		$this->assertFalse(empty($result));
947
		$this->assertTrue($User->id > 0);
948
 
949
		$User->data['User'] = array('password' => 'something');
950
		$result = $User->save();
951
		$this->assertFalse(empty($result));
952
		$result = $User->read();
953
		$this->assertEquals('something', $User->data['User']['password']);
954
	}
955
 
956
/**
957
 * testSaveWithSet method
958
 *
959
 * @return void
960
 */
961
	public function testSaveWithSet() {
962
		$this->loadFixtures('Article');
963
		$TestModel = new Article();
964
 
965
		// Create record we will be updating later
966
 
967
		$data = array('Article' => array(
968
			'user_id' => '1',
969
			'title' => 'Fourth Article',
970
			'body' => 'Fourth Article Body',
971
			'published' => 'Y'
972
		));
973
		$result = $TestModel->create() && $TestModel->save($data);
974
		$this->assertFalse(empty($result));
975
 
976
		// Check record we created
977
 
978
		$TestModel->recursive = -1;
979
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
980
		$expected = array('Article' => array(
981
			'id' => '4',
982
			'user_id' => '1',
983
			'title' => 'Fourth Article',
984
			'body' => 'Fourth Article Body',
985
			'published' => 'Y'
986
		));
987
		$this->assertEquals($expected, $result);
988
 
989
		// Create new record just to overlap Model->id on previously created record
990
 
991
		$data = array('Article' => array(
992
			'user_id' => '4',
993
			'title' => 'Fifth Article',
994
			'body' => 'Fifth Article Body',
995
			'published' => 'Y'
996
		));
997
		$result = $TestModel->create() && $TestModel->save($data);
998
		$this->assertFalse(empty($result));
999
 
1000
		$TestModel->recursive = -1;
1001
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
1002
		$expected = array('Article' => array(
1003
			'id' => '5',
1004
			'user_id' => '4',
1005
			'title' => 'Fifth Article',
1006
			'body' => 'Fifth Article Body',
1007
			'published' => 'Y'
1008
		));
1009
		$this->assertEquals($expected, $result);
1010
 
1011
		// Go back and edit the first article we created, starting by checking it's still there
1012
 
1013
		$TestModel->recursive = -1;
1014
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
1015
		$expected = array('Article' => array(
1016
			'id' => '4',
1017
			'user_id' => '1',
1018
			'title' => 'Fourth Article',
1019
			'body' => 'Fourth Article Body',
1020
			'published' => 'Y'
1021
		));
1022
		$this->assertEquals($expected, $result);
1023
 
1024
		// And now do the update with set()
1025
 
1026
		$data = array('Article' => array(
1027
			'id' => '4',
1028
			'title' => 'Fourth Article - New Title',
1029
			'published' => 'N'
1030
		));
1031
		$result = $TestModel->set($data) && $TestModel->save();
1032
		$this->assertFalse(empty($result));
1033
 
1034
		$TestModel->recursive = -1;
1035
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
1036
		$expected = array('Article' => array(
1037
			'id' => '4',
1038
			'user_id' => '1',
1039
			'title' => 'Fourth Article - New Title',
1040
			'body' => 'Fourth Article Body',
1041
			'published' => 'N'
1042
		));
1043
		$this->assertEquals($expected, $result);
1044
 
1045
		$TestModel->recursive = -1;
1046
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
1047
		$expected = array('Article' => array(
1048
			'id' => '5',
1049
			'user_id' => '4',
1050
			'title' => 'Fifth Article',
1051
			'body' => 'Fifth Article Body',
1052
			'published' => 'Y'
1053
		));
1054
		$this->assertEquals($expected, $result);
1055
 
1056
		$data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
1057
		$result = ($TestModel->set($data) && $TestModel->save());
1058
		$this->assertFalse(empty($result));
1059
 
1060
		$TestModel->recursive = -1;
1061
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
1062
		$expected = array('Article' => array(
1063
			'id' => '5',
1064
			'user_id' => '4',
1065
			'title' => 'Fifth Article - New Title 5',
1066
			'body' => 'Fifth Article Body',
1067
			'published' => 'Y'
1068
		));
1069
		$this->assertEquals($expected, $result);
1070
 
1071
		$TestModel->recursive = -1;
1072
		$result = $TestModel->find('all', array(
1073
			'fields' => array('id', 'title'),
1074
			'order' => array('Article.id' => 'ASC')
1075
		));
1076
		$expected = array(
1077
			array('Article' => array('id' => 1, 'title' => 'First Article')),
1078
			array('Article' => array('id' => 2, 'title' => 'Second Article')),
1079
			array('Article' => array('id' => 3, 'title' => 'Third Article')),
1080
			array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
1081
			array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
1082
		);
1083
		$this->assertEquals($expected, $result);
1084
	}
1085
 
1086
/**
1087
 * testSaveWithNonExistentFields method
1088
 *
1089
 * @return void
1090
 */
1091
	public function testSaveWithNonExistentFields() {
1092
		$this->loadFixtures('Article');
1093
		$TestModel = new Article();
1094
		$TestModel->recursive = -1;
1095
 
1096
		$data = array(
1097
			'non_existent' => 'This field does not exist',
1098
			'user_id' => '1',
1099
			'title' => 'Fourth Article - New Title',
1100
			'body' => 'Fourth Article Body',
1101
			'published' => 'N'
1102
		);
1103
		$result = $TestModel->create() && $TestModel->save($data);
1104
		$this->assertFalse(empty($result));
1105
 
1106
		$expected = array('Article' => array(
1107
			'id' => '4',
1108
			'user_id' => '1',
1109
			'title' => 'Fourth Article - New Title',
1110
			'body' => 'Fourth Article Body',
1111
			'published' => 'N'
1112
		));
1113
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
1114
		$this->assertEquals($expected, $result);
1115
 
1116
		$data = array(
1117
			'user_id' => '1',
1118
			'non_existent' => 'This field does not exist',
1119
			'title' => 'Fifth Article - New Title',
1120
			'body' => 'Fifth Article Body',
1121
			'published' => 'N'
1122
		);
1123
		$result = $TestModel->create() && $TestModel->save($data);
1124
		$this->assertFalse(empty($result));
1125
 
1126
		$expected = array('Article' => array(
1127
			'id' => '5',
1128
			'user_id' => '1',
1129
			'title' => 'Fifth Article - New Title',
1130
			'body' => 'Fifth Article Body',
1131
			'published' => 'N'
1132
		));
1133
		$result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
1134
		$this->assertEquals($expected, $result);
1135
	}
1136
 
1137
/**
1138
 * testSaveFromXml method
1139
 *
1140
 * @return void
1141
 */
1142
	public function testSaveFromXml() {
1143
		$this->markTestSkipped('This feature needs to be fixed or dropped');
1144
		$this->loadFixtures('Article');
1145
		App::uses('Xml', 'Utility');
1146
 
1147
		$Article = new Article();
1148
		$result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
1149
		$this->assertFalse(empty($result));
1150
		$results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
1151
		$this->assertFalse(empty($results));
1152
 
1153
		$result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
1154
		$this->assertFalse(empty($result));
1155
		$results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
1156
		$this->assertFalse(empty($results));
1157
 
1158
		$result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
1159
		$this->assertFalse(empty($result));
1160
		$results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
1161
		$this->assertFalse(empty($results));
1162
	}
1163
 
1164
/**
1165
 * testSaveHabtm method
1166
 *
1167
 * @return void
1168
 */
1169
	public function testSaveHabtm() {
1170
		$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
1171
		$TestModel = new Article();
1172
 
1173
		$result = $TestModel->findById(2);
1174
		$expected = array(
1175
			'Article' => array(
1176
				'id' => '2',
1177
				'user_id' => '3',
1178
				'title' => 'Second Article',
1179
				'body' => 'Second Article Body',
1180
				'published' => 'Y',
1181
				'created' => '2007-03-18 10:41:23',
1182
				'updated' => '2007-03-18 10:43:31'
1183
			),
1184
			'User' => array(
1185
				'id' => '3',
1186
				'user' => 'larry',
1187
				'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1188
				'created' => '2007-03-17 01:20:23',
1189
				'updated' => '2007-03-17 01:22:31'
1190
			),
1191
			'Comment' => array(
1192
				array(
1193
					'id' => '5',
1194
					'article_id' => '2',
1195
					'user_id' => '1',
1196
					'comment' => 'First Comment for Second Article',
1197
					'published' => 'Y',
1198
					'created' => '2007-03-18 10:53:23',
1199
					'updated' => '2007-03-18 10:55:31'
1200
				),
1201
				array(
1202
					'id' => '6',
1203
					'article_id' => '2',
1204
					'user_id' => '2',
1205
					'comment' => 'Second Comment for Second Article',
1206
					'published' => 'Y',
1207
					'created' => '2007-03-18 10:55:23',
1208
					'updated' => '2007-03-18 10:57:31'
1209
			)),
1210
			'Tag' => array(
1211
				array(
1212
					'id' => '1',
1213
					'tag' => 'tag1',
1214
					'created' => '2007-03-18 12:22:23',
1215
					'updated' => '2007-03-18 12:24:31'
1216
				),
1217
				array(
1218
					'id' => '3',
1219
					'tag' => 'tag3',
1220
					'created' => '2007-03-18 12:26:23',
1221
					'updated' => '2007-03-18 12:28:31'
1222
				)
1223
			)
1224
		);
1225
		$this->assertEquals($expected, $result);
1226
 
1227
		$data = array(
1228
			'Article' => array(
1229
				'id' => '2',
1230
				'title' => 'New Second Article'
1231
			),
1232
			'Tag' => array('Tag' => array(1, 2))
1233
		);
1234
 
1235
		$result = $TestModel->set($data);
1236
		$this->assertFalse(empty($result));
1237
		$result = $TestModel->save();
1238
		$this->assertFalse(empty($result));
1239
		$this->assertEquals($data['Tag'], $result['Tag']);
1240
 
1241
		$TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
1242
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1243
		$expected = array(
1244
			'Article' => array(
1245
				'id' => '2',
1246
				'user_id' => '3',
1247
				'title' => 'New Second Article',
1248
				'body' => 'Second Article Body'
1249
			),
1250
			'Tag' => array(
1251
				array(
1252
					'id' => '1',
1253
					'tag' => 'tag1',
1254
					'created' => '2007-03-18 12:22:23',
1255
					'updated' => '2007-03-18 12:24:31'
1256
				),
1257
				array(
1258
					'id' => '2',
1259
					'tag' => 'tag2',
1260
					'created' => '2007-03-18 12:24:23',
1261
					'updated' => '2007-03-18 12:26:31'
1262
		)));
1263
		$this->assertEquals($expected, $result);
1264
 
1265
		$data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
1266
		$result = $TestModel->set($data);
1267
		$this->assertFalse(empty($result));
1268
 
1269
		$result = $TestModel->save();
1270
		$this->assertFalse(empty($result));
1271
 
1272
		$TestModel->unbindModel(array(
1273
			'belongsTo' => array('User'),
1274
			'hasMany' => array('Comment')
1275
		));
1276
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1277
		$expected = array(
1278
			'Article' => array(
1279
				'id' => '2',
1280
				'user_id' => '3',
1281
				'title' => 'New Second Article',
1282
				'body' => 'Second Article Body'
1283
			),
1284
			'Tag' => array(
1285
				array(
1286
					'id' => '2',
1287
					'tag' => 'tag2',
1288
					'created' => '2007-03-18 12:24:23',
1289
					'updated' => '2007-03-18 12:26:31'
1290
				),
1291
				array(
1292
					'id' => '3',
1293
					'tag' => 'tag3',
1294
					'created' => '2007-03-18 12:26:23',
1295
					'updated' => '2007-03-18 12:28:31'
1296
		)));
1297
		$this->assertEquals($expected, $result);
1298
 
1299
		$data = array('Tag' => array('Tag' => array(1, 2, 3)));
1300
 
1301
		$result = $TestModel->set($data);
1302
		$this->assertFalse(empty($result));
1303
 
1304
		$result = $TestModel->save();
1305
		$this->assertFalse(empty($result));
1306
 
1307
		$TestModel->unbindModel(array(
1308
			'belongsTo' => array('User'),
1309
			'hasMany' => array('Comment')
1310
		));
1311
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1312
		$expected = array(
1313
			'Article' => array(
1314
				'id' => '2',
1315
				'user_id' => '3',
1316
				'title' => 'New Second Article',
1317
				'body' => 'Second Article Body'
1318
			),
1319
			'Tag' => array(
1320
				array(
1321
					'id' => '1',
1322
					'tag' => 'tag1',
1323
					'created' => '2007-03-18 12:22:23',
1324
					'updated' => '2007-03-18 12:24:31'
1325
				),
1326
				array(
1327
					'id' => '2',
1328
					'tag' => 'tag2',
1329
					'created' => '2007-03-18 12:24:23',
1330
					'updated' => '2007-03-18 12:26:31'
1331
				),
1332
				array(
1333
					'id' => '3',
1334
					'tag' => 'tag3',
1335
					'created' => '2007-03-18 12:26:23',
1336
					'updated' => '2007-03-18 12:28:31'
1337
		)));
1338
		$this->assertEquals($expected, $result);
1339
 
1340
		$data = array('Tag' => array('Tag' => array()));
1341
		$result = $TestModel->set($data);
1342
		$this->assertFalse(empty($result));
1343
 
1344
		$result = $TestModel->save();
1345
		$this->assertFalse(empty($result));
1346
 
1347
		$data = array('Tag' => array('Tag' => ''));
1348
		$result = $TestModel->set($data);
1349
		$this->assertFalse(empty($result));
1350
 
1351
		$result = $TestModel->save();
1352
		$this->assertFalse(empty($result));
1353
 
1354
		$TestModel->unbindModel(array(
1355
			'belongsTo' => array('User'),
1356
			'hasMany' => array('Comment')
1357
		));
1358
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1359
		$expected = array(
1360
			'Article' => array(
1361
				'id' => '2',
1362
				'user_id' => '3',
1363
				'title' => 'New Second Article',
1364
				'body' => 'Second Article Body'
1365
			),
1366
			'Tag' => array()
1367
		);
1368
		$this->assertEquals($expected, $result);
1369
 
1370
		$data = array('Tag' => array('Tag' => array(2, 3)));
1371
		$result = $TestModel->set($data);
1372
		$this->assertFalse(empty($result));
1373
 
1374
		$result = $TestModel->save();
1375
		$this->assertFalse(empty($result));
1376
 
1377
		$TestModel->unbindModel(array(
1378
			'belongsTo' => array('User'),
1379
			'hasMany' => array('Comment')
1380
		));
1381
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1382
		$expected = array(
1383
			'Article' => array(
1384
				'id' => '2',
1385
				'user_id' => '3',
1386
				'title' => 'New Second Article',
1387
				'body' => 'Second Article Body'
1388
			),
1389
			'Tag' => array(
1390
				array(
1391
					'id' => '2',
1392
					'tag' => 'tag2',
1393
					'created' => '2007-03-18 12:24:23',
1394
					'updated' => '2007-03-18 12:26:31'
1395
				),
1396
				array(
1397
					'id' => '3',
1398
					'tag' => 'tag3',
1399
					'created' => '2007-03-18 12:26:23',
1400
					'updated' => '2007-03-18 12:28:31'
1401
		)));
1402
		$this->assertEquals($expected, $result);
1403
 
1404
		$data = array(
1405
			'Tag' => array(
1406
				'Tag' => array(1, 2)
1407
			),
1408
			'Article' => array(
1409
				'id' => '2',
1410
				'title' => 'New Second Article'
1411
		));
1412
		$result = $TestModel->set($data);
1413
		$this->assertFalse(empty($result));
1414
		$result = $TestModel->save();
1415
		$this->assertFalse(empty($result));
1416
 
1417
		$TestModel->unbindModel(array(
1418
			'belongsTo' => array('User'),
1419
			'hasMany' => array('Comment')
1420
		));
1421
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1422
		$expected = array(
1423
			'Article' => array(
1424
				'id' => '2',
1425
				'user_id' => '3',
1426
				'title' => 'New Second Article',
1427
				'body' => 'Second Article Body'
1428
			),
1429
			'Tag' => array(
1430
				array(
1431
					'id' => '1',
1432
					'tag' => 'tag1',
1433
					'created' => '2007-03-18 12:22:23',
1434
					'updated' => '2007-03-18 12:24:31'
1435
				),
1436
				array(
1437
					'id' => '2',
1438
					'tag' => 'tag2',
1439
					'created' => '2007-03-18 12:24:23',
1440
					'updated' => '2007-03-18 12:26:31'
1441
		)));
1442
		$this->assertEquals($expected, $result);
1443
 
1444
		$data = array(
1445
			'Tag' => array(
1446
				'Tag' => array(1, 2)
1447
			),
1448
			'Article' => array(
1449
				'id' => '2',
1450
				'title' => 'New Second Article Title'
1451
		));
1452
		$result = $TestModel->set($data);
1453
		$this->assertFalse(empty($result));
1454
		$result = $TestModel->save();
1455
		$this->assertFalse(empty($result));
1456
 
1457
		$TestModel->unbindModel(array(
1458
			'belongsTo' => array('User'),
1459
			'hasMany' => array('Comment')
1460
		));
1461
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1462
		$expected = array(
1463
			'Article' => array(
1464
				'id' => '2',
1465
				'user_id' => '3',
1466
				'title' => 'New Second Article Title',
1467
				'body' => 'Second Article Body'
1468
			),
1469
			'Tag' => array(
1470
				array(
1471
					'id' => '1',
1472
					'tag' => 'tag1',
1473
					'created' => '2007-03-18 12:22:23',
1474
					'updated' => '2007-03-18 12:24:31'
1475
				),
1476
				array(
1477
					'id' => '2',
1478
					'tag' => 'tag2',
1479
					'created' => '2007-03-18 12:24:23',
1480
					'updated' => '2007-03-18 12:26:31'
1481
				)
1482
			)
1483
		);
1484
		$this->assertEquals($expected, $result);
1485
 
1486
		$data = array(
1487
			'Tag' => array(
1488
				'Tag' => array(2, 3)
1489
			),
1490
			'Article' => array(
1491
				'id' => '2',
1492
				'title' => 'Changed Second Article'
1493
		));
1494
		$result = $TestModel->set($data);
1495
		$this->assertFalse(empty($result));
1496
		$result = $TestModel->save();
1497
		$this->assertFalse(empty($result));
1498
 
1499
		$TestModel->unbindModel(array(
1500
			'belongsTo' => array('User'),
1501
			'hasMany' => array('Comment')
1502
		));
1503
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1504
		$expected = array(
1505
			'Article' => array(
1506
				'id' => '2',
1507
				'user_id' => '3',
1508
				'title' => 'Changed Second Article',
1509
				'body' => 'Second Article Body'
1510
			),
1511
			'Tag' => array(
1512
				array(
1513
					'id' => '2',
1514
					'tag' => 'tag2',
1515
					'created' => '2007-03-18 12:24:23',
1516
					'updated' => '2007-03-18 12:26:31'
1517
				),
1518
				array(
1519
					'id' => '3',
1520
					'tag' => 'tag3',
1521
					'created' => '2007-03-18 12:26:23',
1522
					'updated' => '2007-03-18 12:28:31'
1523
				)
1524
			)
1525
		);
1526
		$this->assertEquals($expected, $result);
1527
 
1528
		$data = array(
1529
			'Tag' => array(
1530
				'Tag' => array(1, 3)
1531
			),
1532
			'Article' => array('id' => '2'),
1533
		);
1534
 
1535
		$result = $TestModel->set($data);
1536
		$this->assertFalse(empty($result));
1537
 
1538
		$result = $TestModel->save();
1539
		$this->assertFalse(empty($result));
1540
 
1541
		$TestModel->unbindModel(array(
1542
			'belongsTo' => array('User'),
1543
			'hasMany' => array('Comment')
1544
		));
1545
		$result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
1546
		$expected = array(
1547
			'Article' => array(
1548
				'id' => '2',
1549
				'user_id' => '3',
1550
				'title' => 'Changed Second Article',
1551
				'body' => 'Second Article Body'
1552
			),
1553
			'Tag' => array(
1554
				array(
1555
					'id' => '1',
1556
					'tag' => 'tag1',
1557
					'created' => '2007-03-18 12:22:23',
1558
					'updated' => '2007-03-18 12:24:31'
1559
				),
1560
				array(
1561
					'id' => '3',
1562
					'tag' => 'tag3',
1563
					'created' => '2007-03-18 12:26:23',
1564
					'updated' => '2007-03-18 12:28:31'
1565
		)));
1566
		$this->assertEquals($expected, $result);
1567
 
1568
		$data = array(
1569
			'Article' => array(
1570
				'id' => 10,
1571
				'user_id' => '2',
1572
				'title' => 'New Article With Tags and fieldList',
1573
				'body' => 'New Article Body with Tags and fieldList',
1574
				'created' => '2007-03-18 14:55:23',
1575
				'updated' => '2007-03-18 14:57:31'
1576
			),
1577
			'Tag' => array(
1578
				'Tag' => array(1, 2, 3)
1579
			)
1580
		);
1581
		$result = $TestModel->create()
1582
				&& $TestModel->save($data, true, array('user_id', 'title', 'published'));
1583
		$this->assertFalse(empty($result));
1584
 
1585
		$TestModel->unbindModel(array(
1586
			'belongsTo' => array('User'),
1587
			'hasMany' => array('Comment')
1588
		));
1589
		$result = $TestModel->read();
1590
		$expected = array(
1591
			'Article' => array(
1592
				'id' => 4,
1593
				'user_id' => 2,
1594
				'title' => 'New Article With Tags and fieldList',
1595
				'body' => '',
1596
				'published' => 'N',
1597
				'created' => '',
1598
				'updated' => ''
1599
			),
1600
			'Tag' => array(
1601
 
1602
					'id' => 1,
1603
					'tag' => 'tag1',
1604
					'created' => '2007-03-18 12:22:23',
1605
					'updated' => '2007-03-18 12:24:31'
1606
				),
1607
				1 => array(
1608
					'id' => 2,
1609
					'tag' => 'tag2',
1610
					'created' => '2007-03-18 12:24:23',
1611
					'updated' => '2007-03-18 12:26:31'
1612
				),
1613
				2 => array(
1614
					'id' => 3,
1615
					'tag' => 'tag3',
1616
					'created' => '2007-03-18 12:26:23',
1617
					'updated' => '2007-03-18 12:28:31'
1618
		)));
1619
		$this->assertEquals($expected, $result);
1620
 
1621
		$this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
1622
		$TestModel = new JoinA();
1623
		$TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
1624
		$data = array(
1625
			'JoinA' => array(
1626
				'id' => 1,
1627
				'name' => 'Join A 1',
1628
				'body' => 'Join A 1 Body',
1629
			),
1630
			'JoinC' => array(
1631
				'JoinC' => array(
1632
					array('join_c_id' => 2, 'other' => 'new record'),
1633
					array('join_c_id' => 3, 'other' => 'new record')
1634
				)
1635
			)
1636
		);
1637
		$TestModel->save($data);
1638
		$result = $TestModel->read(null, 1);
1639
		$expected = array(4, 5);
1640
		$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
1641
		$expected = array('new record', 'new record');
1642
		$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
1643
	}
1644
 
1645
/**
1646
 * testSaveHabtmNoPrimaryData method
1647
 *
1648
 * @return void
1649
 */
1650
	public function testSaveHabtmNoPrimaryData() {
1651
		$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
1652
		$TestModel = new Article();
1653
 
1654
		$TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
1655
		$result = $TestModel->findById(2);
1656
		$expected = array(
1657
			'Article' => array(
1658
				'id' => '2',
1659
				'user_id' => '3',
1660
				'title' => 'Second Article',
1661
				'body' => 'Second Article Body',
1662
				'published' => 'Y',
1663
				'created' => '2007-03-18 10:41:23',
1664
				'updated' => '2007-03-18 10:43:31'
1665
			),
1666
			'Tag' => array(
1667
				array(
1668
					'id' => '1',
1669
					'tag' => 'tag1',
1670
					'created' => '2007-03-18 12:22:23',
1671
					'updated' => '2007-03-18 12:24:31'
1672
				),
1673
				array(
1674
					'id' => '3',
1675
					'tag' => 'tag3',
1676
					'created' => '2007-03-18 12:26:23',
1677
					'updated' => '2007-03-18 12:28:31'
1678
				)
1679
			)
1680
		);
1681
		$this->assertEquals($expected, $result);
1682
 
1683
		$TestModel->id = 2;
1684
		$data = array('Tag' => array('Tag' => array(2)));
1685
		$result = $TestModel->save($data);
1686
 
1687
		$this->assertEquals($data['Tag'], $result['Tag']);
1688
 
1689
		$result = $TestModel->findById(2);
1690
		$expected = array(
1691
			'Article' => array(
1692
				'id' => '2',
1693
				'user_id' => '3',
1694
				'title' => 'Second Article',
1695
				'body' => 'Second Article Body',
1696
				'published' => 'Y',
1697
				'created' => '2007-03-18 10:41:23',
1698
				'updated' => self::date()
1699
			),
1700
			'Tag' => array(
1701
				array(
1702
					'id' => '2',
1703
					'tag' => 'tag2',
1704
					'created' => '2007-03-18 12:24:23',
1705
					'updated' => '2007-03-18 12:26:31'
1706
				)
1707
			)
1708
		);
1709
		$this->assertEquals($expected, $result);
1710
 
1711
		$this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
1712
		$TestModel = new Portfolio();
1713
		$result = $TestModel->findById(2);
1714
		$expected = array(
1715
			'Portfolio' => array(
1716
				'id' => 2,
1717
				'seller_id' => 1,
1718
				'name' => 'Portfolio 2'
1719
			),
1720
			'Item' => array(
1721
				array(
1722
					'id' => 2,
1723
					'syfile_id' => 2,
1724
					'published' => '',
1725
					'name' => 'Item 2',
1726
					'ItemsPortfolio' => array(
1727
						'id' => 2,
1728
						'item_id' => 2,
1729
						'portfolio_id' => 2
1730
					)
1731
				),
1732
				array(
1733
					'id' => 6,
1734
					'syfile_id' => 6,
1735
					'published' => '',
1736
					'name' => 'Item 6',
1737
					'ItemsPortfolio' => array(
1738
						'id' => 6,
1739
						'item_id' => 6,
1740
						'portfolio_id' => 2
1741
					)
1742
				)
1743
			)
1744
		);
1745
		$this->assertEquals($expected, $result);
1746
 
1747
		$data = array('Item' => array('Item' => array(1, 2)));
1748
		$TestModel->id = 2;
1749
		$TestModel->save($data);
1750
		$result = $TestModel->findById(2);
1751
		$result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
1752
		$expected = array(
1753
			'Portfolio' => array(
1754
				'id' => 2,
1755
				'seller_id' => 1,
1756
				'name' => 'Portfolio 2'
1757
			),
1758
			'Item' => array(
1759
				array(
1760
					'id' => 1,
1761
					'syfile_id' => 1,
1762
					'published' => '',
1763
					'name' => 'Item 1',
1764
					'ItemsPortfolio' => array(
1765
						'id' => 7,
1766
						'item_id' => 1,
1767
						'portfolio_id' => 2
1768
					)
1769
				),
1770
				array(
1771
					'id' => 2,
1772
					'syfile_id' => 2,
1773
					'published' => '',
1774
					'name' => 'Item 2',
1775
					'ItemsPortfolio' => array(
1776
						'id' => 8,
1777
						'item_id' => 2,
1778
						'portfolio_id' => 2
1779
					)
1780
				)
1781
			)
1782
		);
1783
		$this->assertEquals($expected, $result);
1784
	}
1785
 
1786
/**
1787
 * testSaveHabtmCustomKeys method
1788
 *
1789
 * @return void
1790
 */
1791
	public function testSaveHabtmCustomKeys() {
1792
		$this->loadFixtures('Story', 'StoriesTag', 'Tag');
1793
		$Story = new Story();
1794
 
1795
		$data = array(
1796
			'Story' => array('story' => '1'),
1797
			'Tag' => array(
1798
				'Tag' => array(2, 3)
1799
		));
1800
		$result = $Story->set($data);
1801
		$this->assertFalse(empty($result));
1802
 
1803
		$result = $Story->save();
1804
		$this->assertFalse(empty($result));
1805
 
1806
		$result = $Story->find('all', array('order' => array('Story.story')));
1807
		$expected = array(
1808
			array(
1809
				'Story' => array(
1810
					'story' => 1,
1811
					'title' => 'First Story'
1812
				),
1813
				'Tag' => array(
1814
					array(
1815
						'id' => 2,
1816
						'tag' => 'tag2',
1817
						'created' => '2007-03-18 12:24:23',
1818
						'updated' => '2007-03-18 12:26:31'
1819
					),
1820
					array(
1821
						'id' => 3,
1822
						'tag' => 'tag3',
1823
						'created' => '2007-03-18 12:26:23',
1824
						'updated' => '2007-03-18 12:28:31'
1825
			))),
1826
			array(
1827
				'Story' => array(
1828
					'story' => 2,
1829
					'title' => 'Second Story'
1830
				),
1831
				'Tag' => array()
1832
		));
1833
		$this->assertEquals($expected, $result);
1834
	}
1835
 
1836
/**
1837
 * test that saving habtm records respects conditions set in the 'conditions' key
1838
 * for the association.
1839
 *
1840
 * @return void
1841
 */
1842
	public function testHabtmSaveWithConditionsInAssociation() {
1843
		$this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
1844
		$Something = new Something();
1845
		$Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
1846
 
1847
		$Something->bindModel(array(
1848
			'hasAndBelongsToMany' => array(
1849
				'DoomedSomethingElse' => array(
1850
					'className' => 'SomethingElse',
1851
					'joinTable' => 'join_things',
1852
					'conditions' => array('JoinThing.doomed' => true),
1853
					'unique' => true
1854
				),
1855
				'NotDoomedSomethingElse' => array(
1856
					'className' => 'SomethingElse',
1857
					'joinTable' => 'join_things',
1858
					'conditions' => array('JoinThing.doomed' => 0),
1859
					'unique' => true
1860
				)
1861
			)
1862
		), false);
1863
		$result = $Something->read(null, 1);
1864
		$this->assertTrue(empty($result['NotDoomedSomethingElse']));
1865
		$this->assertEquals(1, count($result['DoomedSomethingElse']));
1866
 
1867
		$data = array(
1868
			'Something' => array('id' => 1),
1869
			'NotDoomedSomethingElse' => array(
1870
				'NotDoomedSomethingElse' => array(
1871
					array('something_else_id' => 2, 'doomed' => 0),
1872
					array('something_else_id' => 3, 'doomed' => 0)
1873
				)
1874
			)
1875
		);
1876
		$Something->create($data);
1877
		$result = $Something->save();
1878
		$this->assertFalse(empty($result));
1879
 
1880
		$result = $Something->read(null, 1);
1881
		$this->assertEquals(2, count($result['NotDoomedSomethingElse']));
1882
		$this->assertEquals(1, count($result['DoomedSomethingElse']));
1883
	}
1884
 
1885
/**
1886
 * testHabtmSaveKeyResolution method
1887
 *
1888
 * @return void
1889
 */
1890
	public function testHabtmSaveKeyResolution() {
1891
		$this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
1892
		$ThePaper = new ThePaper();
1893
 
1894
		$ThePaper->id = 1;
1895
		$ThePaper->save(array('Monkey' => array(2, 3)));
1896
 
1897
		$result = $ThePaper->findById(1);
1898
		$expected = array(
1899
			array(
1900
				'id' => '2',
1901
				'device_type_id' => '1',
1902
				'name' => 'Device 2',
1903
				'typ' => '1'
1904
			),
1905
			array(
1906
				'id' => '3',
1907
				'device_type_id' => '1',
1908
				'name' => 'Device 3',
1909
				'typ' => '2'
1910
		));
1911
		$this->assertEquals($expected, $result['Monkey']);
1912
 
1913
		$ThePaper->id = 2;
1914
		$ThePaper->save(array('Monkey' => array(1, 2, 3)));
1915
 
1916
		$result = $ThePaper->findById(2);
1917
		$expected = array(
1918
			array(
1919
				'id' => '1',
1920
				'device_type_id' => '1',
1921
				'name' => 'Device 1',
1922
				'typ' => '1'
1923
			),
1924
			array(
1925
				'id' => '2',
1926
				'device_type_id' => '1',
1927
				'name' => 'Device 2',
1928
				'typ' => '1'
1929
			),
1930
			array(
1931
				'id' => '3',
1932
				'device_type_id' => '1',
1933
				'name' => 'Device 3',
1934
				'typ' => '2'
1935
		));
1936
		$this->assertEquals($expected, $result['Monkey']);
1937
 
1938
		$ThePaper->id = 2;
1939
		$ThePaper->save(array('Monkey' => array(1, 3)));
1940
 
1941
		$result = $ThePaper->findById(2);
1942
		$expected = array(
1943
			array(
1944
				'id' => '1',
1945
				'device_type_id' => '1',
1946
				'name' => 'Device 1',
1947
				'typ' => '1'
1948
			),
1949
			array(
1950
				'id' => '3',
1951
				'device_type_id' => '1',
1952
				'name' => 'Device 3',
1953
				'typ' => '2'
1954
			));
1955
		$this->assertEquals($expected, $result['Monkey']);
1956
 
1957
		$result = $ThePaper->findById(1);
1958
		$expected = array(
1959
			array(
1960
				'id' => '2',
1961
				'device_type_id' => '1',
1962
				'name' => 'Device 2',
1963
				'typ' => '1'
1964
			),
1965
			array(
1966
				'id' => '3',
1967
				'device_type_id' => '1',
1968
				'name' => 'Device 3',
1969
				'typ' => '2'
1970
		));
1971
		$this->assertEquals($expected, $result['Monkey']);
1972
	}
1973
 
1974
/**
1975
 * testCreationOfEmptyRecord method
1976
 *
1977
 * @return void
1978
 */
1979
	public function testCreationOfEmptyRecord() {
1980
		$this->loadFixtures('Author');
1981
		$TestModel = new Author();
1982
		$this->assertEquals(4, $TestModel->find('count'));
1983
 
1984
		$TestModel->deleteAll(true, false, false);
1985
		$this->assertEquals(0, $TestModel->find('count'));
1986
 
1987
		$result = $TestModel->save();
1988
		$this->assertTrue(isset($result['Author']['created']));
1989
		$this->assertTrue(isset($result['Author']['updated']));
1990
		$this->assertEquals(1, $TestModel->find('count'));
1991
	}
1992
 
1993
/**
1994
 * testCreateWithPKFiltering method
1995
 *
1996
 * @return void
1997
 */
1998
	public function testCreateWithPKFiltering() {
1999
		$TestModel = new Article();
2000
		$data = array(
2001
			'id' => 5,
2002
			'user_id' => 2,
2003
			'title' => 'My article',
2004
			'body' => 'Some text'
2005
		);
2006
 
2007
		$result = $TestModel->create($data);
2008
		$expected = array(
2009
			'Article' => array(
2010
				'published' => 'N',
2011
				'id' => 5,
2012
				'user_id' => 2,
2013
				'title' => 'My article',
2014
				'body' => 'Some text'
2015
		));
2016
 
2017
		$this->assertEquals($expected, $result);
2018
		$this->assertEquals(5, $TestModel->id);
2019
 
2020
		$result = $TestModel->create($data, true);
2021
		$expected = array(
2022
			'Article' => array(
2023
				'published' => 'N',
2024
				'id' => false,
2025
				'user_id' => 2,
2026
				'title' => 'My article',
2027
				'body' => 'Some text'
2028
		));
2029
 
2030
		$this->assertEquals($expected, $result);
2031
		$this->assertFalse($TestModel->id);
2032
 
2033
		$result = $TestModel->create(array('Article' => $data), true);
2034
		$expected = array(
2035
			'Article' => array(
2036
				'published' => 'N',
2037
				'id' => false,
2038
				'user_id' => 2,
2039
				'title' => 'My article',
2040
				'body' => 'Some text'
2041
		));
2042
 
2043
		$this->assertEquals($expected, $result);
2044
		$this->assertFalse($TestModel->id);
2045
 
2046
		$data = array(
2047
			'id' => 6,
2048
			'user_id' => 2,
2049
			'title' => 'My article',
2050
			'body' => 'Some text',
2051
			'created' => '1970-01-01 00:00:00',
2052
			'updated' => '1970-01-01 12:00:00',
2053
			'modified' => '1970-01-01 12:00:00'
2054
		);
2055
 
2056
		$result = $TestModel->create($data);
2057
		$expected = array(
2058
			'Article' => array(
2059
				'published' => 'N',
2060
				'id' => 6,
2061
				'user_id' => 2,
2062
				'title' => 'My article',
2063
				'body' => 'Some text',
2064
				'created' => '1970-01-01 00:00:00',
2065
				'updated' => '1970-01-01 12:00:00',
2066
				'modified' => '1970-01-01 12:00:00'
2067
		));
2068
		$this->assertEquals($expected, $result);
2069
		$this->assertEquals(6, $TestModel->id);
2070
 
2071
		$result = $TestModel->create(array(
2072
			'Article' => array_diff_key($data, array(
2073
				'created' => true,
2074
				'updated' => true,
2075
				'modified' => true
2076
		))), true);
2077
		$expected = array(
2078
			'Article' => array(
2079
				'published' => 'N',
2080
				'id' => false,
2081
				'user_id' => 2,
2082
				'title' => 'My article',
2083
				'body' => 'Some text'
2084
		));
2085
		$this->assertEquals($expected, $result);
2086
		$this->assertFalse($TestModel->id);
2087
	}
2088
 
2089
/**
2090
 * testCreationWithMultipleData method
2091
 *
2092
 * @return void
2093
 */
2094
	public function testCreationWithMultipleData() {
2095
		$this->loadFixtures('Article', 'Comment');
2096
		$Article = new Article();
2097
		$Comment = new Comment();
2098
 
2099
		$articles = $Article->find('all', array(
2100
			'fields' => array('id', 'title'),
2101
			'recursive' => -1,
2102
			'order' => array('Article.id' => 'ASC')
2103
		));
2104
		$expected = array(
2105
			array('Article' => array(
2106
				'id' => 1,
2107
				'title' => 'First Article'
2108
			)),
2109
			array('Article' => array(
2110
				'id' => 2,
2111
				'title' => 'Second Article'
2112
			)),
2113
			array('Article' => array(
2114
				'id' => 3,
2115
				'title' => 'Third Article'
2116
		)));
2117
		$this->assertEquals($expected, $articles);
2118
 
2119
		$comments = $Comment->find('all', array(
2120
			'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
2121
			'recursive' => -1,
2122
			'order' => array('Comment.id' => 'ASC')
2123
		));
2124
		$expected = array(
2125
			array('Comment' => array(
2126
				'id' => 1,
2127
				'article_id' => 1,
2128
				'user_id' => 2,
2129
				'comment' => 'First Comment for First Article',
2130
				'published' => 'Y'
2131
			)),
2132
			array('Comment' => array(
2133
				'id' => 2,
2134
				'article_id' => 1,
2135
				'user_id' => 4,
2136
				'comment' => 'Second Comment for First Article',
2137
				'published' => 'Y'
2138
			)),
2139
			array('Comment' => array(
2140
				'id' => 3,
2141
				'article_id' => 1,
2142
				'user_id' => 1,
2143
				'comment' => 'Third Comment for First Article',
2144
				'published' => 'Y'
2145
			)),
2146
			array('Comment' => array(
2147
				'id' => 4,
2148
				'article_id' => 1,
2149
				'user_id' => 1,
2150
				'comment' => 'Fourth Comment for First Article',
2151
				'published' => 'N'
2152
			)),
2153
			array('Comment' => array(
2154
				'id' => 5,
2155
				'article_id' => 2,
2156
				'user_id' => 1,
2157
				'comment' => 'First Comment for Second Article',
2158
				'published' => 'Y'
2159
			)),
2160
			array('Comment' => array(
2161
				'id' => 6,
2162
				'article_id' => 2,
2163
				'user_id' => 2,
2164
				'comment' => 'Second Comment for Second Article',
2165
				'published' => 'Y'
2166
		)));
2167
		$this->assertEquals($expected, $comments);
2168
 
2169
		$data = array(
2170
			'Comment' => array(
2171
				'article_id' => 2,
2172
				'user_id' => 4,
2173
				'comment' => 'Brand New Comment',
2174
				'published' => 'N'
2175
			),
2176
			'Article' => array(
2177
				'id' => 2,
2178
				'title' => 'Second Article Modified'
2179
		));
2180
		$result = $Comment->create($data);
2181
		$this->assertFalse(empty($result));
2182
 
2183
		$result = $Comment->save();
2184
		$this->assertFalse(empty($result));
2185
 
2186
		$articles = $Article->find('all', array(
2187
			'fields' => array('id', 'title'),
2188
			'recursive' => -1,
2189
			'order' => array('Article.id' => 'ASC')
2190
		));
2191
		$expected = array(
2192
			array('Article' => array(
2193
				'id' => 1,
2194
				'title' => 'First Article'
2195
			)),
2196
			array('Article' => array(
2197
				'id' => 2,
2198
				'title' => 'Second Article'
2199
			)),
2200
			array('Article' => array(
2201
				'id' => 3,
2202
				'title' => 'Third Article'
2203
		)));
2204
		$this->assertEquals($expected, $articles);
2205
 
2206
		$comments = $Comment->find('all', array(
2207
			'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
2208
			'recursive' => -1,
2209
			'order' => array('Comment.id' => 'ASC')
2210
		));
2211
		$expected = array(
2212
			array('Comment' => array(
2213
				'id' => 1,
2214
				'article_id' => 1,
2215
				'user_id' => 2,
2216
				'comment' => 'First Comment for First Article',
2217
				'published' => 'Y'
2218
			)),
2219
			array('Comment' => array(
2220
				'id' => 2,
2221
				'article_id' => 1,
2222
				'user_id' => 4,
2223
				'comment' => 'Second Comment for First Article',
2224
				'published' => 'Y'
2225
			)),
2226
			array('Comment' => array(
2227
				'id' => 3,
2228
				'article_id' => 1,
2229
				'user_id' => 1,
2230
				'comment' => 'Third Comment for First Article',
2231
				'published' => 'Y'
2232
			)),
2233
			array('Comment' => array(
2234
				'id' => 4,
2235
				'article_id' => 1,
2236
				'user_id' => 1,
2237
				'comment' => 'Fourth Comment for First Article',
2238
				'published' => 'N'
2239
			)),
2240
			array('Comment' => array(
2241
				'id' => 5,
2242
				'article_id' => 2,
2243
				'user_id' => 1,
2244
				'comment' => 'First Comment for Second Article',
2245
				'published' => 'Y'
2246
			)),
2247
			array('Comment' => array(
2248
				'id' => 6,
2249
				'article_id' => 2,
2250
				'user_id' => 2, 'comment' =>
2251
				'Second Comment for Second Article',
2252
				'published' => 'Y'
2253
			)),
2254
			array('Comment' => array(
2255
				'id' => 7,
2256
				'article_id' => 2,
2257
				'user_id' => 4,
2258
				'comment' => 'Brand New Comment',
2259
				'published' => 'N'
2260
		)));
2261
		$this->assertEquals($expected, $comments);
2262
	}
2263
 
2264
/**
2265
 * testCreationWithMultipleDataSameModel method
2266
 *
2267
 * @return void
2268
 */
2269
	public function testCreationWithMultipleDataSameModel() {
2270
		$this->loadFixtures('Article');
2271
		$Article = new Article();
2272
 
2273
		$result = $Article->field('title', array('id' => 1));
2274
		$this->assertEquals('First Article', $result);
2275
 
2276
		$data = array(
2277
			'Article' => array(
2278
				'user_id' => 2,
2279
				'title' => 'Brand New Article',
2280
				'body' => 'Brand New Article Body',
2281
				'published' => 'Y'
2282
			),
2283
			'SecondaryArticle' => array(
2284
				'id' => 1
2285
		));
2286
 
2287
		$Article->create();
2288
		$result = $Article->save($data);
2289
		$this->assertFalse(empty($result));
2290
 
2291
		$result = $Article->getInsertID();
2292
		$this->assertTrue(!empty($result));
2293
 
2294
		$result = $Article->field('title', array('id' => 1));
2295
		$this->assertEquals('First Article', $result);
2296
 
2297
		$articles = $Article->find('all', array(
2298
			'fields' => array('id', 'title'),
2299
			'recursive' => -1,
2300
			'order' => array('Article.id' => 'ASC')
2301
		));
2302
		$expected = array(
2303
			array('Article' => array(
2304
				'id' => 1,
2305
				'title' => 'First Article'
2306
			)),
2307
			array('Article' => array(
2308
				'id' => 2,
2309
				'title' => 'Second Article'
2310
			)),
2311
			array('Article' => array(
2312
				'id' => 3,
2313
				'title' => 'Third Article'
2314
			)),
2315
			array('Article' => array(
2316
				'id' => 4,
2317
				'title' => 'Brand New Article'
2318
		)));
2319
 
2320
		$this->assertEquals($expected, $articles);
2321
	}
2322
 
2323
/**
2324
 * testCreationWithMultipleDataSameModelManualInstances method
2325
 *
2326
 * @return void
2327
 */
2328
	public function testCreationWithMultipleDataSameModelManualInstances() {
2329
		$this->loadFixtures('PrimaryModel');
2330
		$Primary = new PrimaryModel();
2331
 
2332
		$result = $Primary->field('primary_name', array('id' => 1));
2333
		$this->assertEquals('Primary Name Existing', $result);
2334
 
2335
		$data = array(
2336
			'PrimaryModel' => array(
2337
				'primary_name' => 'Primary Name New'
2338
			),
2339
			'SecondaryModel' => array(
2340
				'id' => array(1)
2341
		));
2342
 
2343
		$Primary->create();
2344
		$result = $Primary->save($data);
2345
		$this->assertFalse(empty($result));
2346
 
2347
		$result = $Primary->field('primary_name', array('id' => 1));
2348
		$this->assertEquals('Primary Name Existing', $result);
2349
 
2350
		$result = $Primary->getInsertID();
2351
		$this->assertTrue(!empty($result));
2352
 
2353
		$result = $Primary->field('primary_name', array('id' => $result));
2354
		$this->assertEquals('Primary Name New', $result);
2355
 
2356
		$result = $Primary->find('count');
2357
		$this->assertEquals(2, $result);
2358
	}
2359
 
2360
/**
2361
 * testRecordExists method
2362
 *
2363
 * @return void
2364
 */
2365
	public function testRecordExists() {
2366
		$this->loadFixtures('User');
2367
		$TestModel = new User();
2368
 
2369
		$this->assertFalse($TestModel->exists());
2370
		$TestModel->read(null, 1);
2371
		$this->assertTrue($TestModel->exists());
2372
		$TestModel->create();
2373
		$this->assertFalse($TestModel->exists());
2374
		$TestModel->id = 4;
2375
		$this->assertTrue($TestModel->exists());
2376
 
2377
		$TestModel = new TheVoid();
2378
		$this->assertFalse($TestModel->exists());
2379
	}
2380
 
2381
/**
2382
 * testRecordExistsMissingTable method
2383
 *
2384
 * @expectedException PDOException
2385
 * @return void
2386
 */
2387
	public function testRecordExistsMissingTable() {
2388
		$TestModel = new TheVoid();
2389
		$TestModel->id = 5;
2390
		$TestModel->exists();
2391
	}
2392
 
2393
/**
2394
 * testUpdateExisting method
2395
 *
2396
 * @return void
2397
 */
2398
	public function testUpdateExisting() {
2399
		$this->loadFixtures('User', 'Article', 'Comment');
2400
		$TestModel = new User();
2401
		$TestModel->create();
2402
 
2403
		$TestModel->save(array(
2404
			'User' => array(
2405
				'user' => 'some user',
2406
				'password' => 'some password'
2407
		)));
2408
		$this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
2409
		$id = $TestModel->id;
2410
 
2411
		$TestModel->save(array(
2412
			'User' => array(
2413
				'user' => 'updated user'
2414
		)));
2415
		$this->assertEquals($id, $TestModel->id);
2416
 
2417
		$result = $TestModel->findById($id);
2418
		$this->assertEquals('updated user', $result['User']['user']);
2419
		$this->assertEquals('some password', $result['User']['password']);
2420
 
2421
		$Article = new Article();
2422
		$Comment = new Comment();
2423
		$data = array(
2424
			'Comment' => array(
2425
				'id' => 1,
2426
				'comment' => 'First Comment for First Article'
2427
			),
2428
			'Article' => array(
2429
				'id' => 2,
2430
				'title' => 'Second Article'
2431
		));
2432
 
2433
		$result = $Article->save($data);
2434
		$this->assertFalse(empty($result));
2435
 
2436
		$result = $Comment->save($data);
2437
		$this->assertFalse(empty($result));
2438
	}
2439
 
2440
/**
2441
 * test updating records and saving blank values.
2442
 *
2443
 * @return void
2444
 */
2445
	public function testUpdateSavingBlankValues() {
2446
		$this->loadFixtures('Article');
2447
		$Article = new Article();
2448
		$Article->validate = array();
2449
		$Article->create();
2450
		$result = $Article->save(array(
2451
			'id' => 1,
2452
			'title' => '',
2453
			'body' => ''
2454
		));
2455
		$this->assertTrue((bool)$result);
2456
		$result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
2457
		$this->assertEquals('', $result['Article']['title'], 'Title is not blank');
2458
		$this->assertEquals('', $result['Article']['body'], 'Body is not blank');
2459
	}
2460
 
2461
/**
2462
 * testUpdateMultiple method
2463
 *
2464
 * @return void
2465
 */
2466
	public function testUpdateMultiple() {
2467
		$this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
2468
		$TestModel = new Comment();
2469
		$result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
2470
		$expected = array('2', '4', '1', '1', '1', '2');
2471
		$this->assertEquals($expected, $result);
2472
 
2473
		$TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
2474
		$result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
2475
		$expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
2476
		$this->assertEquals($expected, $result);
2477
 
2478
		$result = $TestModel->updateAll(
2479
			array('Comment.comment' => "'Updated today'"),
2480
			array('Comment.user_id' => 5)
2481
		);
2482
		$this->assertFalse(empty($result));
2483
		$result = Hash::extract(
2484
			$TestModel->find('all', array(
2485
				'conditions' => array(
2486
					'Comment.user_id' => 5
2487
			))),
2488
			'{n}.Comment.comment'
2489
		);
2490
		$expected = array_fill(0, 2, 'Updated today');
2491
		$this->assertEquals($expected, $result);
2492
	}
2493
 
2494
/**
2495
 * testHabtmUuidWithUuidId method
2496
 *
2497
 * @return void
2498
 */
2499
	public function testHabtmUuidWithUuidId() {
2500
		$this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
2501
		$TestModel = new Uuidportfolio();
2502
 
2503
		$data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
2504
		$data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
2505
		$TestModel->create($data);
2506
		$TestModel->save();
2507
		$id = $TestModel->id;
2508
		$result = $TestModel->read(null, $id);
2509
		$this->assertEquals(1, count($result['Uuiditem']));
2510
		$this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
2511
	}
2512
 
2513
/**
2514
 * test HABTM saving when join table has no primary key and only 2 columns.
2515
 *
2516
 * @return void
2517
 */
2518
	public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
2519
		$this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
2520
		$Fruit = new Fruit();
2521
		$Fruit->FruitsUuidTag->order = null;
2522
		$data = array(
2523
			'Fruit' => array(
2524
				'color' => 'Red',
2525
				'shape' => 'Heart-shaped',
2526
				'taste' => 'sweet',
2527
				'name' => 'Strawberry',
2528
			),
2529
			'UuidTag' => array(
2530
				'UuidTag' => array(
2531
					'481fc6d0-b920-43e0-e50f-6d1740cf8569'
2532
				)
2533
			)
2534
		);
2535
		$result = $Fruit->save($data);
2536
		$this->assertFalse(empty($result));
2537
	}
2538
 
2539
/**
2540
 * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
2541
 *
2542
 * @return void
2543
 */
2544
	public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
2545
		$this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
2546
		$Fruit = new FruitNoWith();
2547
		$data = array(
2548
			'Fruit' => array(
2549
				'color' => 'Red',
2550
				'shape' => 'Heart-shaped',
2551
				'taste' => 'sweet',
2552
				'name' => 'Strawberry',
2553
			),
2554
			'UuidTag' => array(
2555
				'UuidTag' => array(
2556
					'481fc6d0-b920-43e0-e50f-6d1740cf8569'
2557
				)
2558
			)
2559
		);
2560
		$result = $Fruit->save($data);
2561
		$this->assertFalse(empty($result));
2562
	}
2563
 
2564
/**
2565
 * testHabtmUuidWithNumericId method
2566
 *
2567
 * @return void
2568
 */
2569
	public function testHabtmUuidWithNumericId() {
2570
		$this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
2571
		$TestModel = new Uuiditem();
2572
 
2573
		$data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
2574
		$data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
2575
		$TestModel->create($data);
2576
		$TestModel->save();
2577
		$id = $TestModel->id;
2578
		$result = $TestModel->read(null, $id);
2579
		$this->assertEquals(1, count($result['Uuidportfolio']));
2580
	}
2581
 
2582
/**
2583
 * testSaveMultipleHabtm method
2584
 *
2585
 * @return void
2586
 */
2587
	public function testSaveMultipleHabtm() {
2588
		$this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
2589
		$TestModel = new JoinA();
2590
		$result = $TestModel->findById(1);
2591
 
2592
		$expected = array(
2593
			'JoinA' => array(
2594
				'id' => 1,
2595
				'name' => 'Join A 1',
2596
				'body' => 'Join A 1 Body',
2597
				'created' => '2008-01-03 10:54:23',
2598
				'updated' => '2008-01-03 10:54:23'
2599
			),
2600
			'JoinB' => array(
2601
 
2602
					'id' => 2,
2603
					'name' => 'Join B 2',
2604
					'created' => '2008-01-03 10:55:02',
2605
					'updated' => '2008-01-03 10:55:02',
2606
					'JoinAsJoinB' => array(
2607
						'id' => 1,
2608
						'join_a_id' => 1,
2609
						'join_b_id' => 2,
2610
						'other' => 'Data for Join A 1 Join B 2',
2611
						'created' => '2008-01-03 10:56:33',
2612
						'updated' => '2008-01-03 10:56:33'
2613
			))),
2614
			'JoinC' => array(
2615
 
2616
					'id' => 2,
2617
					'name' => 'Join C 2',
2618
					'created' => '2008-01-03 10:56:12',
2619
					'updated' => '2008-01-03 10:56:12',
2620
					'JoinAsJoinC' => array(
2621
						'id' => 1,
2622
						'join_a_id' => 1,
2623
						'join_c_id' => 2,
2624
						'other' => 'Data for Join A 1 Join C 2',
2625
						'created' => '2008-01-03 10:57:22',
2626
						'updated' => '2008-01-03 10:57:22'
2627
		))));
2628
 
2629
		$this->assertEquals($expected, $result);
2630
 
2631
		$TestModel->id = 1;
2632
		$data = array(
2633
			'JoinA' => array(
2634
				'id' => '1',
2635
				'name' => 'New name for Join A 1',
2636
				'updated' => self::date()
2637
			),
2638
			'JoinB' => array(
2639
				array(
2640
					'id' => 1,
2641
					'join_b_id' => 2,
2642
					'other' => 'New data for Join A 1 Join B 2',
2643
					'created' => self::date(),
2644
					'updated' => self::date()
2645
			)),
2646
			'JoinC' => array(
2647
				array(
2648
					'id' => 1,
2649
					'join_c_id' => 2,
2650
					'other' => 'New data for Join A 1 Join C 2',
2651
					'created' => self::date(),
2652
					'updated' => self::date()
2653
		)));
2654
 
2655
		$TestModel->set($data);
2656
		$TestModel->save();
2657
 
2658
		$result = $TestModel->findById(1);
2659
		$expected = array(
2660
			'JoinA' => array(
2661
				'id' => 1,
2662
				'name' => 'New name for Join A 1',
2663
				'body' => 'Join A 1 Body',
2664
				'created' => '2008-01-03 10:54:23',
2665
				'updated' => self::date()
2666
			),
2667
			'JoinB' => array(
2668
 
2669
					'id' => 2,
2670
					'name' => 'Join B 2',
2671
					'created' => '2008-01-03 10:55:02',
2672
					'updated' => '2008-01-03 10:55:02',
2673
					'JoinAsJoinB' => array(
2674
						'id' => 1,
2675
						'join_a_id' => 1,
2676
						'join_b_id' => 2,
2677
						'other' => 'New data for Join A 1 Join B 2',
2678
						'created' => self::date(),
2679
						'updated' => self::date()
2680
			))),
2681
			'JoinC' => array(
2682
 
2683
					'id' => 2,
2684
					'name' => 'Join C 2',
2685
					'created' => '2008-01-03 10:56:12',
2686
					'updated' => '2008-01-03 10:56:12',
2687
					'JoinAsJoinC' => array(
2688
						'id' => 1,
2689
						'join_a_id' => 1,
2690
						'join_c_id' => 2,
2691
						'other' => 'New data for Join A 1 Join C 2',
2692
						'created' => self::date(),
2693
						'updated' => self::date()
2694
		))));
2695
 
2696
		$this->assertEquals($expected, $result);
2697
	}
2698
 
2699
/**
2700
 * testSaveAll method
2701
 *
2702
 * @return void
2703
 */
2704
	public function testSaveAll() {
2705
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
2706
		$TestModel = new Post();
2707
 
2708
		$result = $TestModel->find('all');
2709
		$this->assertEquals(3, count($result));
2710
		$this->assertFalse(isset($result[3]));
2711
 
2712
		$TestModel->saveAll(array(
2713
			'Post' => array(
2714
				'title' => 'Post with Author',
2715
				'body' => 'This post will be saved with an author'
2716
			),
2717
			'Author' => array(
2718
				'user' => 'bob',
2719
				'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
2720
		)));
2721
 
2722
		$result = $TestModel->find('all');
2723
		$expected = array(
2724
			'Post' => array(
2725
				'id' => '4',
2726
				'author_id' => '5',
2727
				'title' => 'Post with Author',
2728
				'body' => 'This post will be saved with an author',
2729
				'published' => 'N'
2730
			),
2731
			'Author' => array(
2732
				'id' => '5',
2733
				'user' => 'bob',
2734
				'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
2735
				'test' => 'working'
2736
		));
2737
		$this->assertEquals(self::date(), $result[3]['Post']['created']);
2738
		$this->assertEquals(self::date(), $result[3]['Post']['updated']);
2739
		$this->assertEquals(self::date(), $result[3]['Author']['created']);
2740
		$this->assertEquals(self::date(), $result[3]['Author']['updated']);
2741
		unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
2742
		unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
2743
		$this->assertEquals($expected, $result[3]);
2744
		$this->assertEquals(4, count($result));
2745
 
2746
		$TestModel->deleteAll(true);
2747
		$this->assertEquals(array(), $TestModel->find('all'));
2748
 
2749
		// SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
2750
		$this->db->truncate($TestModel);
2751
 
2752
		$TestModel->saveAll(array(
2753
			array(
2754
				'title' => 'Multi-record post 1',
2755
				'body' => 'First multi-record post',
2756
				'author_id' => 2
2757
			),
2758
			array(
2759
				'title' => 'Multi-record post 2',
2760
				'body' => 'Second multi-record post',
2761
				'author_id' => 2
2762
		)));
2763
 
2764
		$result = $TestModel->find('all', array(
2765
			'recursive' => -1,
2766
			'order' => 'Post.id ASC'
2767
		));
2768
		$expected = array(
2769
			array(
2770
				'Post' => array(
2771
					'id' => '1',
2772
					'author_id' => '2',
2773
					'title' => 'Multi-record post 1',
2774
					'body' => 'First multi-record post',
2775
					'published' => 'N'
2776
			)),
2777
			array(
2778
				'Post' => array(
2779
					'id' => '2',
2780
					'author_id' => '2',
2781
					'title' => 'Multi-record post 2',
2782
					'body' => 'Second multi-record post',
2783
					'published' => 'N'
2784
		)));
2785
		$this->assertEquals(self::date(), $result[0]['Post']['created']);
2786
		$this->assertEquals(self::date(), $result[0]['Post']['updated']);
2787
		$this->assertEquals(self::date(), $result[1]['Post']['created']);
2788
		$this->assertEquals(self::date(), $result[1]['Post']['updated']);
2789
		unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
2790
		unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
2791
		$this->assertEquals($expected, $result);
2792
 
2793
		$TestModel = new Comment();
2794
		$result = $TestModel->saveAll(array(
2795
			'Comment' => array(
2796
				'article_id' => 2,
2797
				'user_id' => 2,
2798
				'comment' => 'New comment with attachment',
2799
				'published' => 'Y'
2800
			),
2801
			'Attachment' => array(
2802
				'attachment' => 'some_file.tgz'
2803
			)));
2804
		$this->assertFalse(empty($result));
2805
 
2806
		$result = $TestModel->find('all');
2807
		$expected = array(
2808
			'id' => '7',
2809
			'article_id' => '2',
2810
			'user_id' => '2',
2811
			'comment' => 'New comment with attachment',
2812
			'published' => 'Y'
2813
		);
2814
		$this->assertEquals(self::date(), $result[6]['Comment']['created']);
2815
		$this->assertEquals(self::date(), $result[6]['Comment']['updated']);
2816
		unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
2817
		$this->assertEquals($expected, $result[6]['Comment']);
2818
 
2819
		$expected = array(
2820
			'id' => '2',
2821
			'comment_id' => '7',
2822
			'attachment' => 'some_file.tgz'
2823
		);
2824
		$this->assertEquals(self::date(), $result[6]['Attachment']['created']);
2825
		$this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
2826
		unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
2827
		$this->assertEquals($expected, $result[6]['Attachment']);
2828
	}
2829
 
2830
/**
2831
 * Test SaveAll with Habtm relations
2832
 *
2833
 * @return void
2834
 */
2835
	public function testSaveAllHabtm() {
2836
		$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
2837
		$data = array(
2838
			'Article' => array(
2839
				'user_id' => 1,
2840
				'title' => 'Article Has and belongs to Many Tags'
2841
			),
2842
			'Tag' => array(
2843
				'Tag' => array(1, 2)
2844
			),
2845
			'Comment' => array(
2846
				array(
2847
					'comment' => 'Article comment',
2848
					'user_id' => 1
2849
		)));
2850
		$Article = new Article();
2851
		$result = $Article->saveAll($data);
2852
		$this->assertFalse(empty($result));
2853
 
2854
		$result = $Article->read();
2855
		$this->assertEquals(2, count($result['Tag']));
2856
		$this->assertEquals('tag1', $result['Tag'][0]['tag']);
2857
		$this->assertEquals(1, count($result['Comment']));
2858
		$this->assertEquals(1, count($result['Comment'][0]['comment']));
2859
	}
2860
 
2861
/**
2862
 * Test SaveAll with Habtm relations and extra join table fields
2863
 *
2864
 * @return void
2865
 */
2866
	public function testSaveAllHabtmWithExtraJoinTableFields() {
2867
		$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
2868
 
2869
		$data = array(
2870
			'Something' => array(
2871
				'id' => 4,
2872
				'title' => 'Extra Fields',
2873
				'body' => 'Extra Fields Body',
2874
				'published' => '1'
2875
			),
2876
			'SomethingElse' => array(
2877
				array('something_else_id' => 1, 'doomed' => '1'),
2878
				array('something_else_id' => 2, 'doomed' => '0'),
2879
				array('something_else_id' => 3, 'doomed' => '1')
2880
			)
2881
		);
2882
 
2883
		$Something = new Something();
2884
		$result = $Something->saveAll($data);
2885
		$this->assertFalse(empty($result));
2886
		$result = $Something->read();
2887
 
2888
		$this->assertEquals(3, count($result['SomethingElse']));
2889
		$this->assertTrue(Set::matches('/Something[id=4]', $result));
2890
 
2891
		$this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
2892
		$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
2893
		$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
2894
 
2895
		$this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
2896
		$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
2897
		$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
2898
 
2899
		$this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
2900
		$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
2901
		$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
2902
	}
2903
 
2904
/**
2905
 * testSaveAllHasOne method
2906
 *
2907
 * @return void
2908
 */
2909
	public function testSaveAllHasOne() {
2910
		$model = new Comment();
2911
		$model->deleteAll(true);
2912
		$this->assertEquals(array(), $model->find('all'));
2913
 
2914
		$model->Attachment->deleteAll(true);
2915
		$this->assertEquals(array(), $model->Attachment->find('all'));
2916
 
2917
		$this->assertTrue($model->saveAll(array(
2918
			'Comment' => array(
2919
				'comment' => 'Comment with attachment',
2920
				'article_id' => 1,
2921
				'user_id' => 1
2922
			),
2923
			'Attachment' => array(
2924
				'attachment' => 'some_file.zip'
2925
		))));
2926
		$result = $model->find('all', array('fields' => array(
2927
			'Comment.id', 'Comment.comment', 'Attachment.id',
2928
			'Attachment.comment_id', 'Attachment.attachment'
2929
		)));
2930
		$expected = array(array(
2931
			'Comment' => array(
2932
				'id' => '1',
2933
				'comment' => 'Comment with attachment'
2934
			),
2935
			'Attachment' => array(
2936
				'id' => '1',
2937
				'comment_id' => '1',
2938
				'attachment' => 'some_file.zip'
2939
		)));
2940
		$this->assertEquals($expected, $result);
2941
 
2942
		$model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
2943
		$data = array(
2944
			'Comment' => array(
2945
				'comment' => 'Comment with attachment',
2946
				'article_id' => 1,
2947
				'user_id' => 1
2948
			),
2949
			'Attachment' => array(
2950
				'attachment' => 'some_file.zip'
2951
		));
2952
		$this->assertTrue($model->saveAll($data, array('validate' => 'first')));
2953
	}
2954
 
2955
/**
2956
 * testSaveAllBelongsTo method
2957
 *
2958
 * @return void
2959
 */
2960
	public function testSaveAllBelongsTo() {
2961
		$model = new Comment();
2962
		$model->deleteAll(true);
2963
		$this->assertEquals(array(), $model->find('all'));
2964
 
2965
		$model->Article->deleteAll(true);
2966
		$this->assertEquals(array(), $model->Article->find('all'));
2967
 
2968
		$this->assertTrue($model->saveAll(array(
2969
			'Comment' => array(
2970
				'comment' => 'Article comment',
2971
				'article_id' => 1,
2972
				'user_id' => 1
2973
			),
2974
			'Article' => array(
2975
				'title' => 'Model Associations 101',
2976
				'user_id' => 1
2977
		))));
2978
		$result = $model->find('all', array('fields' => array(
2979
			'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
2980
		)));
2981
		$expected = array(array(
2982
			'Comment' => array(
2983
				'id' => '1',
2984
				'article_id' => '1',
2985
				'comment' => 'Article comment'
2986
			),
2987
			'Article' => array(
2988
				'id' => '1',
2989
				'title' => 'Model Associations 101'
2990
		)));
2991
		$this->assertEquals($expected, $result);
2992
	}
2993
 
2994
/**
2995
 * testSaveAllHasOneValidation method
2996
 *
2997
 * @return void
2998
 */
2999
	public function testSaveAllHasOneValidation() {
3000
		$model = new Comment();
3001
		$model->deleteAll(true);
3002
		$this->assertEquals(array(), $model->find('all'));
3003
 
3004
		$model->Attachment->deleteAll(true);
3005
		$this->assertEquals(array(), $model->Attachment->find('all'));
3006
 
3007
		$model->validate = array('comment' => 'notEmpty');
3008
		$model->Attachment->validate = array('attachment' => 'notEmpty');
3009
		$model->Attachment->bindModel(array('belongsTo' => array('Comment')));
3010
 
3011
		$result = $model->saveAll(
3012
			array(
3013
				'Comment' => array(
3014
					'comment' => '',
3015
					'article_id' => 1,
3016
					'user_id' => 1
3017
				),
3018
				'Attachment' => array('attachment' => '')
3019
			),
3020
			array('validate' => 'first')
3021
		);
3022
		$this->assertEquals(false, $result);
3023
		$expected = array(
3024
			'comment' => array('This field cannot be left blank'),
3025
			'Attachment' => array(
3026
				'attachment' => array('This field cannot be left blank')
3027
			)
3028
		);
3029
		$this->assertEquals($expected, $model->validationErrors);
3030
		$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
3031
	}
3032
 
3033
/**
3034
 * testSaveAllAtomic method
3035
 *
3036
 * @return void
3037
 */
3038
	public function testSaveAllAtomic() {
3039
		$this->loadFixtures('Article', 'User', 'Comment');
3040
		$TestModel = new Article();
3041
 
3042
		$result = $TestModel->saveAll(array(
3043
			'Article' => array(
3044
				'title' => 'Post with Author',
3045
				'body' => 'This post will be saved with an author',
3046
				'user_id' => 2
3047
			),
3048
			'Comment' => array(
3049
				array('comment' => 'First new comment', 'user_id' => 2))
3050
		), array('atomic' => false));
3051
 
3052
		$this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
3053
 
3054
		$result = $TestModel->saveAll(array(
3055
			array(
3056
				'id' => '1',
3057
				'title' => 'Baleeted First Post',
3058
				'body' => 'Baleeted!',
3059
				'published' => 'N'
3060
			),
3061
			array(
3062
				'id' => '2',
3063
				'title' => 'Just update the title'
3064
			),
3065
			array(
3066
				'title' => 'Creating a fourth post',
3067
				'body' => 'Fourth post body',
3068
				'user_id' => 2
3069
			)
3070
		), array('atomic' => false));
3071
		$this->assertSame($result, array(true, true, true));
3072
 
3073
		$result = $TestModel->saveAll(array(
3074
			'Article' => array('id' => 2),
3075
			'Comment' => array(
3076
				array(
3077
					'comment' => 'First new comment',
3078
					'published' => 'Y',
3079
					'user_id' => 1
3080
				),
3081
				array(
3082
					'comment' => 'Second new comment',
3083
					'published' => 'Y',
3084
					'user_id' => 2
3085
			))
3086
		), array('validate' => true, 'atomic' => false));
3087
		$this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
3088
 
3089
		$TestModel->validate = array(
3090
			'title' => 'notEmpty',
3091
			'author_id' => 'numeric'
3092
		);
3093
		$result = $TestModel->saveAll(array(
3094
			array(
3095
				'id' => '1',
3096
				'title' => 'Un-Baleeted First Post',
3097
				'body' => 'Not Baleeted!',
3098
				'published' => 'Y'
3099
			),
3100
			array(
3101
				'id' => '2',
3102
				'title' => '',
3103
				'body' => 'Trying to get away with an empty title'
3104
			)
3105
		), array('validate' => true, 'atomic' => false));
3106
		$this->assertSame(array(true, false), $result);
3107
	}
3108
 
3109
/**
3110
 * testSaveAllDeepAssociated method
3111
 *
3112
 * @return void
3113
 */
3114
	public function testSaveAllDeepAssociated() {
3115
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
3116
		$TestModel = new Article();
3117
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3118
		$TestModel->hasAndBelongsToMany = array();
3119
 
3120
		$result = $TestModel->saveAll(array(
3121
			'Article' => array('id' => 2),
3122
			'Comment' => array(
3123
				array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
3124
				array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3125
			)
3126
		), array('deep' => true));
3127
		$this->assertTrue($result);
3128
 
3129
		$result = $TestModel->findById(2);
3130
		$expected = array(
3131
			'First Comment for Second Article',
3132
			'Second Comment for Second Article',
3133
			'First new comment',
3134
			'Second new comment'
3135
		);
3136
		$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
3137
		$this->assertEquals($expected, $result);
3138
 
3139
		$result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
3140
		$this->assertEquals(5, $result);
3141
		$result = $TestModel->saveAll(array(
3142
			'Article' => array('id' => 2),
3143
			'Comment' => array(
3144
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3145
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
3146
			)
3147
		), array('deep' => true));
3148
		$this->assertTrue($result);
3149
 
3150
		$result = $TestModel->findById(2);
3151
		$expected = array(
3152
			'First Comment for Second Article',
3153
			'Second Comment for Second Article',
3154
			'First new comment',
3155
			'Second new comment',
3156
			'Third new comment',
3157
			'Fourth new comment'
3158
		);
3159
		$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
3160
		$this->assertEquals($expected, $result);
3161
 
3162
		$result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
3163
		$this->assertEquals(2, $result);
3164
		$data = array(
3165
			'Attachment' => array(
3166
				'attachment' => 'deepsave insert',
3167
			),
3168
			'Comment' => array(
3169
				'comment' => 'First comment deepsave insert',
3170
				'published' => 'Y',
3171
				'user_id' => 5,
3172
				'Article' => array(
3173
					'title' => 'First Article deepsave insert',
3174
					'body' => 'First Article Body deepsave insert',
3175
					'User' => array(
3176
						'user' => '',
3177
						'password' => 'magic'
3178
					),
3179
				),
3180
			)
3181
		);
3182
 
3183
		$TestModel->Comment->Attachment->create();
3184
		$result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
3185
		$this->assertFalse($result);
3186
 
3187
		$expected = array('User' => array('user' => array('This field cannot be left blank')));
3188
		$this->assertEquals($expected, $TestModel->validationErrors);
3189
 
3190
		$data['Comment']['Article']['User']['user'] = 'deepsave';
3191
		$TestModel->Comment->Attachment->create();
3192
		$result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
3193
		$this->assertTrue($result);
3194
 
3195
		$result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
3196
		$expected = array(
3197
			'Attachment' => array(
3198
				'id' => '3',
3199
				'comment_id' => '11',
3200
				'attachment' => 'deepsave insert',
3201
			),
3202
			'Comment' => array(
3203
				'id' => '11',
3204
				'article_id' => '4',
3205
				'user_id' => '5',
3206
				'comment' => 'First comment deepsave insert',
3207
				'published' => 'Y',
3208
			)
3209
		);
3210
		unset($result['Attachment']['created'], $result['Attachment']['updated']);
3211
		$this->assertEquals($expected['Attachment'], $result['Attachment']);
3212
 
3213
		unset($result['Comment']['created'], $result['Comment']['updated']);
3214
		$this->assertEquals($expected['Comment'], $result['Comment']);
3215
 
3216
		$result = $TestModel->findById($result['Comment']['article_id']);
3217
		$expected = array(
3218
			'Article' => array(
3219
				'id' => '4',
3220
				'user_id' => '6',
3221
				'title' => 'First Article deepsave insert',
3222
				'body' => 'First Article Body deepsave insert',
3223
				'published' => 'N',
3224
			),
3225
			'User' => array(
3226
				'id' => '6',
3227
				'user' => 'deepsave',
3228
				'password' => 'magic',
3229
			),
3230
			'Comment' => array(
3231
				array(
3232
					'id' => '11',
3233
					'article_id' => '4',
3234
					'user_id' => '5',
3235
					'comment' => 'First comment deepsave insert',
3236
					'published' => 'Y',
3237
				)
3238
			)
3239
		);
3240
		unset(
3241
			$result['Article']['created'], $result['Article']['updated'],
3242
			$result['User']['created'], $result['User']['updated'],
3243
			$result['Comment'][0]['created'], $result['Comment'][0]['updated']
3244
		);
3245
		$this->assertEquals($expected, $result);
3246
	}
3247
 
3248
/**
3249
 * testSaveAllDeepMany
3250
 * tests the validate methods with deeper recursive data
3251
 *
3252
 * @return void
3253
 */
3254
	public function testSaveAllDeepMany() {
3255
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
3256
		$TestModel = new Article();
3257
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3258
		$TestModel->hasAndBelongsToMany = array();
3259
 
3260
		$data = array(
3261
			array(
3262
				'Article' => array('id' => 1),
3263
				'Comment' => array(
3264
					array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
3265
					array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
3266
				)
3267
			),
3268
			array(
3269
				'Article' => array('id' => 2),
3270
				'Comment' => array(
3271
					array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
3272
					array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
3273
				)
3274
			)
3275
		);
3276
		$result = $TestModel->saveAll($data, array('deep' => true));
3277
		$this->assertTrue($result);
3278
 
3279
		$data = array(
3280
			array(
3281
				'id' => 1, 'body' => '',
3282
				'Comment' => array(
3283
					array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
3284
					array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
3285
				)
3286
			),
3287
			array(
3288
				'Article' => array('id' => 2),
3289
				'Comment' => array(
3290
					array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
3291
					array('comment' => '', 'published' => 'Y', 'user_id' => 2)
3292
				)
3293
			)
3294
		);
3295
		$TestModel->Comment->validate['comment'] = 'notEmpty';
3296
		$result = $TestModel->saveAll($data, array('deep' => true));
3297
		$this->assertFalse($result);
3298
 
3299
		$expected = array(
3300
 
3301
				'body' => array('This field cannot be left blank'),
3302
				'Comment' => array(
3303
 
3304
						'comment' => array('This field cannot be left blank'),
3305
						'User' => array(
3306
							'user' => array('This field cannot be left blank')
3307
						)
3308
					)
3309
				)
3310
			),
3311
			1 => array(
3312
				'Comment' => array(
3313
 
3314
						'User' => array(
3315
							'password' => array('This field cannot be left blank')
3316
						)
3317
					),
3318
					1 => array(
3319
						'comment' => array('This field cannot be left blank')
3320
					)
3321
				)
3322
			)
3323
		);
3324
		$result = $TestModel->validationErrors;
3325
		$this->assertSame($expected, $result);
3326
	}
3327
/**
3328
 * testSaveAllDeepValidateOnly
3329
 * tests the validate methods with deeper recursive data
3330
 *
3331
 * @return void
3332
 */
3333
	public function testSaveAllDeepValidateOnly() {
3334
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
3335
		$TestModel = new Article();
3336
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3337
		$TestModel->hasAndBelongsToMany = array();
3338
		$TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
3339
		$TestModel->Comment->validate['comment'] = 'notEmpty';
3340
 
3341
		$result = $TestModel->saveAll(
3342
			array(
3343
				'Article' => array('id' => 2),
3344
				'Comment' => array(
3345
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
3346
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3347
				)
3348
			),
3349
			array('validate' => 'only', 'deep' => true)
3350
		);
3351
		$this->assertTrue($result);
3352
 
3353
		$result = $TestModel->saveAll(
3354
			array(
3355
				'Article' => array('id' => 2),
3356
				'Comment' => array(
3357
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
3358
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3359
				)
3360
			),
3361
			array('validate' => 'only', 'deep' => true)
3362
		);
3363
		$this->assertFalse($result);
3364
 
3365
		$result = $TestModel->saveAll(
3366
			array(
3367
				'Article' => array('id' => 2),
3368
				'Comment' => array(
3369
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
3370
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3371
				)
3372
			),
3373
			array('validate' => 'only', 'atomic' => false, 'deep' => true)
3374
		);
3375
		$expected = array(
3376
			'Article' => true,
3377
			'Comment' => array(
3378
				true,
3379
				true
3380
			)
3381
		);
3382
		$this->assertSame($expected, $result);
3383
 
3384
		$result = $TestModel->saveAll(
3385
			array(
3386
				'Article' => array('id' => 2),
3387
				'Comment' => array(
3388
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
3389
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3390
				)
3391
			),
3392
			array('validate' => 'only', 'atomic' => false, 'deep' => true)
3393
		);
3394
		$expected = array(
3395
			'Article' => true,
3396
			'Comment' => array(
3397
				false,
3398
				true
3399
			)
3400
		);
3401
		$this->assertSame($expected, $result);
3402
 
3403
		$result = $TestModel->saveAll(array(
3404
			'Article' => array('id' => 2),
3405
			'Comment' => array(
3406
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3407
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
3408
			)
3409
		),
3410
		array('validate' => 'only', 'deep' => true)
3411
		);
3412
		$this->assertTrue($result);
3413
 
3414
		$result = $TestModel->saveAll(array(
3415
			'Article' => array('id' => 2),
3416
			'Comment' => array(
3417
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3418
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
3419
			)
3420
		),
3421
		array('validate' => 'only', 'deep' => true)
3422
		);
3423
		$this->assertFalse($result);
3424
 
3425
		$result = $TestModel->saveAll(array(
3426
			'Article' => array('id' => 2),
3427
			'Comment' => array(
3428
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3429
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
3430
			)
3431
		),
3432
		array('validate' => 'only', 'atomic' => false, 'deep' => true)
3433
		);
3434
		$expected = array(
3435
			'Article' => true,
3436
			'Comment' => array(
3437
				true,
3438
				true
3439
			)
3440
		);
3441
		$this->assertSame($expected, $result);
3442
 
3443
		$result = $TestModel->saveAll(array(
3444
			'Article' => array('id' => 2),
3445
			'Comment' => array(
3446
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3447
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
3448
			)
3449
		),
3450
		array('validate' => 'only', 'atomic' => false, 'deep' => true)
3451
		);
3452
		$expected = array(
3453
			'Article' => true,
3454
			'Comment' => array(
3455
				true,
3456
				false
3457
			)
3458
		);
3459
		$this->assertSame($expected, $result);
3460
 
3461
		$expected = array(
3462
			'Comment' => array(
3463
				1 => array(
3464
					'Attachment' => array(
3465
						'attachment' => array('This field cannot be left blank')
3466
					)
3467
				)
3468
			)
3469
		);
3470
		$result = $TestModel->validationErrors;
3471
		$this->assertSame($expected, $result);
3472
 
3473
		$data = array(
3474
			'Attachment' => array(
3475
				'attachment' => 'deepsave insert',
3476
			),
3477
			'Comment' => array(
3478
				'comment' => 'First comment deepsave insert',
3479
				'published' => 'Y',
3480
				'user_id' => 5,
3481
				'Article' => array(
3482
					'title' => 'First Article deepsave insert',
3483
					'body' => 'First Article Body deepsave insert',
3484
					'User' => array(
3485
						'user' => 'deepsave',
3486
						'password' => 'magic'
3487
					),
3488
				),
3489
			)
3490
		);
3491
 
3492
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
3493
		$this->assertTrue($result);
3494
 
3495
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
3496
		$expected = array(
3497
			'Attachment' => true,
3498
			'Comment' => true
3499
		);
3500
		$this->assertSame($expected, $result);
3501
 
3502
		$data = array(
3503
			'Attachment' => array(
3504
				'attachment' => 'deepsave insert',
3505
			),
3506
			'Comment' => array(
3507
				'comment' => 'First comment deepsave insert',
3508
				'published' => 'Y',
3509
				'user_id' => 5,
3510
				'Article' => array(
3511
					'title' => 'First Article deepsave insert',
3512
					'body' => 'First Article Body deepsave insert',
3513
					'User' => array(
3514
						'user' => '',
3515
						'password' => 'magic'
3516
					),
3517
				),
3518
			)
3519
		);
3520
 
3521
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
3522
		$this->assertFalse($result);
3523
 
3524
		$result = $TestModel->Comment->Attachment->validationErrors;
3525
		$expected = array(
3526
			'Comment' => array(
3527
				'Article' => array(
3528
					'User' => array(
3529
						'user' => array('This field cannot be left blank')
3530
					)
3531
				)
3532
			)
3533
		);
3534
		$this->assertSame($expected, $result);
3535
 
3536
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
3537
		$expected = array(
3538
			'Attachment' => true,
3539
			'Comment' => false
3540
		);
3541
		$this->assertEquals($expected, $result);
3542
 
3543
		$data['Comment']['Article']['body'] = '';
3544
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
3545
		$this->assertFalse($result);
3546
 
3547
		$result = $TestModel->Comment->Attachment->validationErrors;
3548
		$expected = array(
3549
			'Comment' => array(
3550
				'Article' => array(
3551
					'body' => array('This field cannot be left blank'),
3552
					'User' => array(
3553
						'user' => array('This field cannot be left blank')
3554
					)
3555
				)
3556
			)
3557
		);
3558
		$this->assertSame($expected, $result);
3559
 
3560
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
3561
		$expected = array(
3562
			'Attachment' => true,
3563
			'Comment' => false
3564
		);
3565
		$this->assertEquals($expected, $result);
3566
 
3567
		$data['Comment']['comment'] = '';
3568
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
3569
		$this->assertFalse($result);
3570
 
3571
		$result = $TestModel->Comment->Attachment->validationErrors;
3572
		$expected = array(
3573
			'Comment' => array(
3574
				'comment' => array('This field cannot be left blank'),
3575
				'Article' => array(
3576
					'body' => array('This field cannot be left blank'),
3577
					'User' => array(
3578
						'user' => array('This field cannot be left blank')
3579
					)
3580
				)
3581
			)
3582
		);
3583
		$this->assertSame($expected, $result);
3584
 
3585
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
3586
		$expected = array(
3587
			'Attachment' => true,
3588
			'Comment' => false
3589
		);
3590
		$this->assertEquals($expected, $result);
3591
 
3592
		$data['Attachment']['attachment'] = '';
3593
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
3594
		$this->assertFalse($result);
3595
 
3596
		$result = $TestModel->Comment->Attachment->validationErrors;
3597
		$expected = array(
3598
			'attachment' => array('This field cannot be left blank'),
3599
			'Comment' => array(
3600
				'comment' => array('This field cannot be left blank'),
3601
				'Article' => array(
3602
					'body' => array('This field cannot be left blank'),
3603
					'User' => array(
3604
						'user' => array('This field cannot be left blank')
3605
					)
3606
				)
3607
			)
3608
		);
3609
		$this->assertSame($expected, $result);
3610
 
3611
		$result = $TestModel->Comment->validationErrors;
3612
		$expected = array(
3613
			'comment' => array('This field cannot be left blank'),
3614
			'Article' => array(
3615
				'body' => array('This field cannot be left blank'),
3616
				'User' => array(
3617
					'user' => array('This field cannot be left blank')
3618
				)
3619
			)
3620
		);
3621
		$this->assertSame($expected, $result);
3622
 
3623
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
3624
		$expected = array(
3625
			'Attachment' => false,
3626
			'Comment' => false
3627
		);
3628
		$this->assertEquals($expected, $result);
3629
	}
3630
 
3631
/**
3632
 * testSaveAllNotDeepAssociated method
3633
 * test that only directly associated data gets saved
3634
 *
3635
 * @return void
3636
 */
3637
	public function testSaveAllNotDeepAssociated() {
3638
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
3639
		$TestModel = new Article();
3640
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3641
		$TestModel->hasAndBelongsToMany = array();
3642
 
3643
		$result = $TestModel->saveAll(array(
3644
			'Article' => array('id' => 2),
3645
			'Comment' => array(
3646
				array(
3647
					'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
3648
					'User' => array('user' => 'newuser', 'password' => 'newuserpass')
3649
				),
3650
				array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3651
			)
3652
		), array('deep' => false));
3653
		$this->assertTrue($result);
3654
 
3655
		$result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
3656
		$this->assertFalse($result);
3657
 
3658
		$result = $TestModel->saveAll(array(
3659
			'Article' => array('id' => 2),
3660
			'Comment' => array(
3661
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
3662
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
3663
			)
3664
		), array('deep' => false));
3665
		$this->assertTrue($result);
3666
 
3667
		$result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
3668
		$this->assertFalse($result);
3669
 
3670
		$data = array(
3671
			'Attachment' => array(
3672
				'attachment' => 'deepsave insert',
3673
			),
3674
			'Comment' => array(
3675
				'comment' => 'First comment deepsave insert',
3676
				'published' => 'Y',
3677
				'user_id' => 4,
3678
				'article_id' => 1,
3679
				'Article' => array(
3680
					'title' => 'First Article deepsave insert',
3681
					'body' => 'First Article Body deepsave insert',
3682
					'User' => array(
3683
						'user' => 'deepsave',
3684
						'password' => 'magic'
3685
					),
3686
				),
3687
			)
3688
		);
3689
		$expected = $TestModel->User->find('count');
3690
 
3691
		$TestModel->Comment->Attachment->create();
3692
		$result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
3693
		$this->assertTrue($result);
3694
 
3695
		$result = $TestModel->User->find('count');
3696
		$this->assertEquals($expected, $result);
3697
 
3698
		$result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
3699
		$expected = array(
3700
			'Attachment' => array(
3701
				'id' => '2',
3702
				'comment_id' => '11',
3703
				'attachment' => 'deepsave insert',
3704
			),
3705
			'Comment' => array(
3706
				'id' => '11',
3707
				'article_id' => 1,
3708
				'user_id' => '4',
3709
				'comment' => 'First comment deepsave insert',
3710
				'published' => 'Y',
3711
			)
3712
		);
3713
		unset($result['Attachment']['created'], $result['Attachment']['updated']);
3714
		$this->assertEquals($expected['Attachment'], $result['Attachment']);
3715
 
3716
		unset($result['Comment']['created'], $result['Comment']['updated']);
3717
		$this->assertEquals($expected['Comment'], $result['Comment']);
3718
	}
3719
 
3720
/**
3721
 * testSaveAllNotDeepMany
3722
 * tests the save methods to not save deeper recursive data
3723
 *
3724
 * @return void
3725
 */
3726
	public function testSaveAllNotDeepMany() {
3727
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
3728
		$TestModel = new Article();
3729
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3730
		$TestModel->hasAndBelongsToMany = array();
3731
 
3732
		$data = array(
3733
			array(
3734
				'id' => 1,
3735
				'body' => '',
3736
				'Comment' => array(
3737
					array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
3738
					array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
3739
				)
3740
			),
3741
			array(
3742
				'Article' => array('id' => 2),
3743
				'Comment' => array(
3744
					array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
3745
					array('comment' => '', 'published' => 'Y', 'user_id' => 2)
3746
				)
3747
			)
3748
		);
3749
		$TestModel->Comment->validate['comment'] = 'notEmpty';
3750
		$result = $TestModel->saveAll($data, array('deep' => false));
3751
		$this->assertFalse($result);
3752
 
3753
		$expected = array(
3754
 
3755
				'body' => array('This field cannot be left blank')
3756
			)
3757
		);
3758
		$result = $TestModel->validationErrors;
3759
		$this->assertSame($expected, $result);
3760
 
3761
		$data = array(
3762
			array(
3763
				'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
3764
				'Comment' => array(
3765
					array('comment' => '', 'published' => 'Y', 'user_id' => 2)
3766
				)
3767
			),
3768
			array(
3769
				'Article' => array('id' => 2),
3770
				'Comment' => array(
3771
					array('comment' => '', 'published' => 'Y', 'user_id' => 2)
3772
				)
3773
			)
3774
		);
3775
		$result = $TestModel->saveAll($data, array('deep' => false));
3776
		$this->assertTrue($result);
3777
	}
3778
/**
3779
 * testSaveAllNotDeepValidateOnly
3780
 * tests the validate methods to not validate deeper recursive data
3781
 *
3782
 * @return void
3783
 */
3784
	public function testSaveAllNotDeepValidateOnly() {
3785
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
3786
		$TestModel = new Article();
3787
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3788
		$TestModel->hasAndBelongsToMany = array();
3789
		$TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
3790
		$TestModel->Comment->validate['comment'] = 'notEmpty';
3791
 
3792
		$result = $TestModel->saveAll(
3793
			array(
3794
				'Article' => array('id' => 2, 'body' => ''),
3795
				'Comment' => array(
3796
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
3797
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3798
				)
3799
			),
3800
			array('validate' => 'only', 'deep' => false)
3801
		);
3802
		$this->assertFalse($result);
3803
 
3804
		$expected = array('body' => array('This field cannot be left blank'));
3805
		$result = $TestModel->validationErrors;
3806
		$this->assertSame($expected, $result);
3807
 
3808
		$result = $TestModel->saveAll(
3809
			array(
3810
				'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
3811
				'Comment' => array(
3812
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
3813
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3814
				)
3815
			),
3816
			array('validate' => 'only', 'deep' => false)
3817
		);
3818
		$this->assertTrue($result);
3819
 
3820
		$result = $TestModel->saveAll(
3821
			array(
3822
				'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
3823
				'Comment' => array(
3824
					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
3825
					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3826
				)
3827
			),
3828
			array('validate' => 'only', 'atomic' => false, 'deep' => false)
3829
		);
3830
		$expected = array(
3831
			'Article' => true,
3832
			'Comment' => array(
3833
				true,
3834
				true
3835
			)
3836
		);
3837
		$this->assertSame($expected, $result);
3838
 
3839
		$result = $TestModel->saveAll(array(
3840
			'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
3841
			'Comment' => array(
3842
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3843
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
3844
			)
3845
		),
3846
		array('validate' => 'only', 'deep' => false)
3847
		);
3848
		$this->assertTrue($result);
3849
 
3850
		$result = $TestModel->saveAll(array(
3851
			'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
3852
			'Comment' => array(
3853
				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
3854
				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
3855
			)
3856
		),
3857
		array('validate' => 'only', 'atomic' => false, 'deep' => false)
3858
		);
3859
		$expected = array(
3860
			'Article' => true,
3861
			'Comment' => array(
3862
				true,
3863
				true
3864
			)
3865
		);
3866
		$this->assertSame($expected, $result);
3867
 
3868
		$expected = array();
3869
		$result = $TestModel->validationErrors;
3870
		$this->assertSame($expected, $result);
3871
 
3872
		$data = array(
3873
			'Attachment' => array(
3874
				'attachment' => 'deepsave insert',
3875
			),
3876
			'Comment' => array(
3877
				'comment' => 'First comment deepsave insert',
3878
				'published' => 'Y',
3879
				'user_id' => 5,
3880
				'Article' => array(
3881
					'title' => 'First Article deepsave insert ignored',
3882
					'body' => 'First Article Body deepsave insert',
3883
					'User' => array(
3884
						'user' => '',
3885
						'password' => 'magic'
3886
					),
3887
				),
3888
			)
3889
		);
3890
 
3891
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
3892
		$this->assertTrue($result);
3893
 
3894
		$result = $TestModel->Comment->Attachment->validationErrors;
3895
		$expected = array();
3896
		$this->assertSame($expected, $result);
3897
 
3898
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
3899
		$expected = array(
3900
			'Attachment' => true,
3901
			'Comment' => true
3902
		);
3903
		$this->assertEquals($expected, $result);
3904
 
3905
		$data['Comment']['Article']['body'] = '';
3906
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
3907
		$this->assertTrue($result);
3908
 
3909
		$result = $TestModel->Comment->Attachment->validationErrors;
3910
		$expected = array();
3911
		$this->assertSame($expected, $result);
3912
 
3913
		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
3914
		$expected = array(
3915
			'Attachment' => true,
3916
			'Comment' => true
3917
		);
3918
		$this->assertEquals($expected, $result);
3919
	}
3920
 
3921
/**
3922
 * testSaveAllHasMany method
3923
 *
3924
 * @return void
3925
 */
3926
	public function testSaveAllHasMany() {
3927
		$this->loadFixtures('Article', 'Comment');
3928
		$TestModel = new Article();
3929
		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
3930
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
3931
 
3932
		$result = $TestModel->saveAll(array(
3933
			'Article' => array('id' => 2),
3934
			'Comment' => array(
3935
				array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
3936
				array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
3937
			)
3938
		));
3939
		$this->assertFalse(empty($result));
3940
 
3941
		$result = $TestModel->findById(2);
3942
		$expected = array(
3943
			'First Comment for Second Article',
3944
			'Second Comment for Second Article',
3945
			'First new comment',
3946
			'Second new comment'
3947
		);
3948
		$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
3949
		$this->assertEquals($expected, $result);
3950
 
3951
		$result = $TestModel->saveAll(
3952
			array(
3953
				'Article' => array('id' => 2),
3954
				'Comment' => array(
3955
					array(
3956
						'comment' => 'Third new comment',
3957
						'published' => 'Y',
3958
						'user_id' => 1
3959
			))),
3960
			array('atomic' => false)
3961
		);
3962
		$this->assertFalse(empty($result));
3963
 
3964
		$result = $TestModel->findById(2);
3965
		$expected = array(
3966
			'First Comment for Second Article',
3967
			'Second Comment for Second Article',
3968
			'First new comment',
3969
			'Second new comment',
3970
			'Third new comment'
3971
		);
3972
		$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
3973
		$this->assertEquals($expected, $result);
3974
 
3975
		$TestModel->beforeSaveReturn = false;
3976
		$result = $TestModel->saveAll(
3977
			array(
3978
				'Article' => array('id' => 2),
3979
				'Comment' => array(
3980
					array(
3981
						'comment' => 'Fourth new comment',
3982
						'published' => 'Y',
3983
						'user_id' => 1
3984
			))),
3985
			array('atomic' => false)
3986
		);
3987
		$this->assertEquals(array('Article' => false), $result);
3988
 
3989
		$result = $TestModel->findById(2);
3990
		$expected = array(
3991
			'First Comment for Second Article',
3992
			'Second Comment for Second Article',
3993
			'First new comment',
3994
			'Second new comment',
3995
			'Third new comment'
3996
		);
3997
		$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
3998
		$this->assertEquals($expected, $result);
3999
	}
4000
 
4001
/**
4002
 * testSaveAllHasManyValidation method
4003
 *
4004
 * @return void
4005
 */
4006
	public function testSaveAllHasManyValidation() {
4007
		$this->loadFixtures('Article', 'Comment');
4008
		$TestModel = new Article();
4009
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
4010
		$TestModel->Comment->validate = array('comment' => 'notEmpty');
4011
 
4012
		$result = $TestModel->saveAll(array(
4013
			'Article' => array('id' => 2),
4014
			'Comment' => array(
4015
				array('comment' => '', 'published' => 'Y', 'user_id' => 1),
4016
			)
4017
		), array('validate' => true));
4018
		$this->assertFalse($result);
4019
 
4020
		$expected = array('Comment' => array(
4021
			array('comment' => array('This field cannot be left blank'))
4022
		));
4023
		$this->assertEquals($expected, $TestModel->validationErrors);
4024
		$expected = array(
4025
			array('comment' => array('This field cannot be left blank'))
4026
		);
4027
		$this->assertEquals($expected, $TestModel->Comment->validationErrors);
4028
 
4029
		$result = $TestModel->saveAll(array(
4030
			'Article' => array('id' => 2),
4031
			'Comment' => array(
4032
				array(
4033
					'comment' => '',
4034
					'published' => 'Y',
4035
					'user_id' => 1
4036
			))
4037
		), array('validate' => 'first'));
4038
		$this->assertFalse($result);
4039
	}
4040
 
4041
/**
4042
 * test saveAll with transactions and ensure there is no missing rollback.
4043
 *
4044
 * @return void
4045
 */
4046
	public function testSaveAllManyRowsTransactionNoRollback() {
4047
		$this->loadFixtures('Post');
4048
 
4049
		$this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
4050
		$db = ConnectionManager::create('mock_transaction', array(
4051
			'datasource' => 'MockTransactionDboSource',
4052
		));
4053
 
4054
		$db->expects($this->once())
4055
			->method('describe')
4056
			->will($this->returnValue(array()));
4057
		$db->expects($this->once())->method('rollback');
4058
 
4059
		$Post = new Post('mock_transaction');
4060
 
4061
		$Post->validate = array(
4062
			'title' => array('rule' => array('notEmpty'))
4063
		);
4064
 
4065
		$data = array(
4066
			array('author_id' => 1, 'title' => 'New Fourth Post'),
4067
			array('author_id' => 1, 'title' => '')
4068
		);
4069
		$Post->saveAll($data, array('atomic' => true));
4070
	}
4071
 
4072
/**
4073
 * test saveAll with transactions and ensure there is no missing rollback.
4074
 *
4075
 * @return void
4076
 */
4077
	public function testSaveAllAssociatedTransactionNoRollback() {
4078
		$testDb = ConnectionManager::getDataSource('test');
4079
 
4080
		$this->getMock(
4081
			'DboSource',
4082
			array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
4083
			array(),
4084
			'MockTransactionAssociatedDboSource'
4085
		);
4086
		$db = ConnectionManager::create('mock_transaction_assoc', array(
4087
			'datasource' => 'MockTransactionAssociatedDboSource',
4088
		));
4089
		$this->mockObjects[] = $db;
4090
		$db->columns = $testDb->columns;
4091
 
4092
		$db->expects($this->once())->method('rollback');
4093
		$db->expects($this->any())->method('describe')
4094
			->will($this->returnValue(array(
4095
				'id' => array('type' => 'integer', 'length' => 11),
4096
				'title' => array('type' => 'string'),
4097
				'body' => array('type' => 'text'),
4098
				'published' => array('type' => 'string')
4099
			)));
4100
 
4101
		$Post = new Post();
4102
		$Post->useDbConfig = 'mock_transaction_assoc';
4103
		$Post->Author->useDbConfig = 'mock_transaction_assoc';
4104
 
4105
		$Post->Author->validate = array(
4106
			'user' => array('rule' => array('notEmpty'))
4107
		);
4108
 
4109
		$data = array(
4110
			'Post' => array(
4111
				'title' => 'New post',
4112
				'body' => 'Content',
4113
				'published' => 'Y'
4114
			),
4115
			'Author' => array(
4116
				'user' => '',
4117
				'password' => "sekret"
4118
			)
4119
		);
4120
		$Post->saveAll($data, array('validate' => true));
4121
	}
4122
 
4123
/**
4124
 * test saveAll with nested saveAll call.
4125
 *
4126
 * @return void
4127
 */
4128
	public function testSaveAllNestedSaveAll() {
4129
		$this->loadFixtures('Sample');
4130
		$TransactionTestModel = new TransactionTestModel();
4131
 
4132
		$data = array(
4133
			array('apple_id' => 1, 'name' => 'sample5'),
4134
		);
4135
 
4136
		$this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
4137
	}
4138
 
4139
/**
4140
 * testSaveAllTransaction method
4141
 *
4142
 * @return void
4143
 */
4144
	public function testSaveAllTransaction() {
4145
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
4146
		$TestModel = new Post();
4147
 
4148
		$TestModel->validate = array('title' => 'notEmpty');
4149
		$data = array(
4150
			array('author_id' => 1, 'title' => 'New Fourth Post'),
4151
			array('author_id' => 1, 'title' => 'New Fifth Post'),
4152
			array('author_id' => 1, 'title' => '')
4153
		);
4154
		$this->assertFalse($TestModel->saveAll($data));
4155
 
4156
		$result = $TestModel->find('all', array('recursive' => -1));
4157
		$expected = array(
4158
			array('Post' => array(
4159
				'id' => '1',
4160
				'author_id' => 1,
4161
				'title' => 'First Post',
4162
				'body' => 'First Post Body',
4163
				'published' => 'Y',
4164
				'created' => '2007-03-18 10:39:23',
4165
				'updated' => '2007-03-18 10:41:31'
4166
			)),
4167
			array('Post' => array(
4168
				'id' => '2',
4169
				'author_id' => 3,
4170
				'title' => 'Second Post',
4171
				'body' => 'Second Post Body',
4172
				'published' => 'Y',
4173
				'created' => '2007-03-18 10:41:23',
4174
				'updated' => '2007-03-18 10:43:31'
4175
			)),
4176
			array('Post' => array(
4177
				'id' => '3',
4178
				'author_id' => 1,
4179
				'title' => 'Third Post',
4180
				'body' => 'Third Post Body',
4181
				'published' => 'Y',
4182
				'created' => '2007-03-18 10:43:23',
4183
				'updated' => '2007-03-18 10:45:31'
4184
		)));
4185
 
4186
		if (count($result) != 3) {
4187
			// Database doesn't support transactions
4188
			$expected[] = array(
4189
				'Post' => array(
4190
					'id' => '4',
4191
					'author_id' => 1,
4192
					'title' => 'New Fourth Post',
4193
					'body' => null,
4194
					'published' => 'N',
4195
					'created' => self::date(),
4196
					'updated' => self::date()
4197
			));
4198
 
4199
			$expected[] = array(
4200
				'Post' => array(
4201
					'id' => '5',
4202
					'author_id' => 1,
4203
					'title' => 'New Fifth Post',
4204
					'body' => null,
4205
					'published' => 'N',
4206
					'created' => self::date(),
4207
					'updated' => self::date()
4208
			));
4209
 
4210
			$this->assertEquals($expected, $result);
4211
			// Skip the rest of the transactional tests
4212
			return;
4213
		}
4214
 
4215
		$this->assertEquals($expected, $result);
4216
 
4217
		$data = array(
4218
			array('author_id' => 1, 'title' => 'New Fourth Post'),
4219
			array('author_id' => 1, 'title' => ''),
4220
			array('author_id' => 1, 'title' => 'New Sixth Post')
4221
		);
4222
		$this->assertFalse($TestModel->saveAll($data));
4223
 
4224
		$result = $TestModel->find('all', array('recursive' => -1));
4225
		$expected = array(
4226
			array('Post' => array(
4227
				'id' => '1',
4228
				'author_id' => 1,
4229
				'title' => 'First Post',
4230
				'body' => 'First Post Body',
4231
				'published' => 'Y',
4232
				'created' => '2007-03-18 10:39:23',
4233
				'updated' => '2007-03-18 10:41:31'
4234
			)),
4235
			array('Post' => array(
4236
				'id' => '2',
4237
				'author_id' => 3,
4238
				'title' => 'Second Post',
4239
				'body' => 'Second Post Body',
4240
				'published' => 'Y',
4241
				'created' => '2007-03-18 10:41:23',
4242
				'updated' => '2007-03-18 10:43:31'
4243
			)),
4244
			array('Post' => array(
4245
				'id' => '3',
4246
				'author_id' => 1,
4247
				'title' => 'Third Post',
4248
				'body' => 'Third Post Body',
4249
				'published' => 'Y',
4250
				'created' => '2007-03-18 10:43:23',
4251
				'updated' => '2007-03-18 10:45:31'
4252
		)));
4253
 
4254
		if (count($result) != 3) {
4255
			// Database doesn't support transactions
4256
			$expected[] = array(
4257
				'Post' => array(
4258
					'id' => '4',
4259
					'author_id' => 1,
4260
					'title' => 'New Fourth Post',
4261
					'body' => 'Third Post Body',
4262
					'published' => 'N',
4263
					'created' => self::date(),
4264
					'updated' => self::date()
4265
			));
4266
 
4267
			$expected[] = array(
4268
				'Post' => array(
4269
					'id' => '5',
4270
					'author_id' => 1,
4271
					'title' => 'Third Post',
4272
					'body' => 'Third Post Body',
4273
					'published' => 'N',
4274
					'created' => self::date(),
4275
					'updated' => self::date()
4276
			));
4277
		}
4278
		$this->assertEquals($expected, $result);
4279
 
4280
		$TestModel->validate = array('title' => 'notEmpty');
4281
		$data = array(
4282
			array('author_id' => 1, 'title' => 'New Fourth Post'),
4283
			array('author_id' => 1, 'title' => 'New Fifth Post'),
4284
			array('author_id' => 1, 'title' => 'New Sixth Post')
4285
		);
4286
		$this->assertTrue($TestModel->saveAll($data));
4287
 
4288
		$result = $TestModel->find('all', array(
4289
			'recursive' => -1,
4290
			'fields' => array('author_id', 'title', 'body', 'published'),
4291
			'order' => array('Post.created' => 'ASC')
4292
		));
4293
 
4294
		$expected = array(
4295
			array('Post' => array(
4296
				'author_id' => 1,
4297
				'title' => 'First Post',
4298
				'body' => 'First Post Body',
4299
				'published' => 'Y'
4300
			)),
4301
			array('Post' => array(
4302
				'author_id' => 3,
4303
				'title' => 'Second Post',
4304
				'body' => 'Second Post Body',
4305
				'published' => 'Y'
4306
			)),
4307
			array('Post' => array(
4308
				'author_id' => 1,
4309
				'title' => 'Third Post',
4310
				'body' => 'Third Post Body',
4311
				'published' => 'Y'
4312
			)),
4313
			array('Post' => array(
4314
				'author_id' => 1,
4315
				'title' => 'New Fourth Post',
4316
				'body' => '',
4317
				'published' => 'N'
4318
			)),
4319
			array('Post' => array(
4320
				'author_id' => 1,
4321
				'title' => 'New Fifth Post',
4322
				'body' => '',
4323
				'published' => 'N'
4324
			)),
4325
			array('Post' => array(
4326
				'author_id' => 1,
4327
				'title' => 'New Sixth Post',
4328
				'body' => '',
4329
				'published' => 'N'
4330
		)));
4331
		$this->assertEquals($expected, $result);
4332
	}
4333
 
4334
/**
4335
 * testSaveAllValidation method
4336
 *
4337
 * @return void
4338
 */
4339
	public function testSaveAllValidation() {
4340
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
4341
		$TestModel = new Post();
4342
 
4343
		$data = array(
4344
			array(
4345
				'id' => '1',
4346
				'title' => 'Baleeted First Post',
4347
				'body' => 'Baleeted!',
4348
				'published' => 'N'
4349
			),
4350
			array(
4351
				'id' => '2',
4352
				'title' => 'Just update the title'
4353
			),
4354
			array(
4355
				'title' => 'Creating a fourth post',
4356
				'body' => 'Fourth post body',
4357
				'author_id' => 2
4358
		));
4359
 
4360
		$this->assertTrue($TestModel->saveAll($data));
4361
 
4362
		$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
4363
		$expected = array(
4364
			array(
4365
				'Post' => array(
4366
					'id' => '1',
4367
					'author_id' => '1',
4368
					'title' => 'Baleeted First Post',
4369
					'body' => 'Baleeted!',
4370
					'published' => 'N',
4371
					'created' => '2007-03-18 10:39:23'
4372
			)),
4373
			array(
4374
				'Post' => array(
4375
					'id' => '2',
4376
					'author_id' => '3',
4377
					'title' => 'Just update the title',
4378
					'body' => 'Second Post Body',
4379
					'published' => 'Y',
4380
					'created' => '2007-03-18 10:41:23'
4381
			)),
4382
			array(
4383
				'Post' => array(
4384
					'id' => '3',
4385
					'author_id' => '1',
4386
					'title' => 'Third Post',
4387
					'body' => 'Third Post Body',
4388
					'published' => 'Y',
4389
					'created' => '2007-03-18 10:43:23',
4390
					'updated' => '2007-03-18 10:45:31'
4391
			)),
4392
			array(
4393
				'Post' => array(
4394
					'id' => '4',
4395
					'author_id' => '2',
4396
					'title' => 'Creating a fourth post',
4397
					'body' => 'Fourth post body',
4398
					'published' => 'N'
4399
		)));
4400
		$this->assertEquals(self::date(), $result[0]['Post']['updated']);
4401
		$this->assertEquals(self::date(), $result[1]['Post']['updated']);
4402
		$this->assertEquals(self::date(), $result[3]['Post']['created']);
4403
		$this->assertEquals(self::date(), $result[3]['Post']['updated']);
4404
		unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
4405
		unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
4406
		$this->assertEquals($expected, $result);
4407
 
4408
		$TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
4409
		$data = array(
4410
			array(
4411
				'id' => '1',
4412
				'title' => 'Un-Baleeted First Post',
4413
				'body' => 'Not Baleeted!',
4414
				'published' => 'Y'
4415
			),
4416
			array(
4417
				'id' => '2',
4418
				'title' => '',
4419
				'body' => 'Trying to get away with an empty title'
4420
		));
4421
		$result = $TestModel->saveAll($data);
4422
		$this->assertFalse($result);
4423
 
4424
		$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
4425
		$errors = array(1 => array('title' => array('This field cannot be left blank')));
4426
		$transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
4427
		if (!$transactionWorked) {
4428
			$this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
4429
			$this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
4430
		}
4431
 
4432
		$this->assertEquals($errors, $TestModel->validationErrors);
4433
 
4434
		$TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
4435
		$data = array(
4436
			array(
4437
				'id' => '1',
4438
				'title' => 'Un-Baleeted First Post',
4439
				'body' => 'Not Baleeted!',
4440
				'published' => 'Y'
4441
			),
4442
			array(
4443
				'id' => '2',
4444
				'title' => '',
4445
				'body' => 'Trying to get away with an empty title'
4446
		));
4447
		$result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
4448
		$this->assertEquals(array(true, false), $result);
4449
		$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
4450
		$errors = array(1 => array('title' => array('This field cannot be left blank')));
4451
		$expected = array(
4452
			array(
4453
				'Post' => array(
4454
					'id' => '1',
4455
					'author_id' => '1',
4456
					'title' => 'Un-Baleeted First Post',
4457
					'body' => 'Not Baleeted!',
4458
					'published' => 'Y',
4459
					'created' => '2007-03-18 10:39:23'
4460
				)
4461
			),
4462
			array(
4463
				'Post' => array(
4464
					'id' => '2',
4465
					'author_id' => '3',
4466
					'title' => 'Just update the title',
4467
					'body' => 'Second Post Body',
4468
					'published' => 'Y',
4469
					'created' => '2007-03-18 10:41:23'
4470
				)
4471
			),
4472
			array(
4473
				'Post' => array(
4474
					'id' => '3',
4475
					'author_id' => '1',
4476
					'title' => 'Third Post',
4477
					'body' => 'Third Post Body',
4478
					'published' => 'Y',
4479
					'created' => '2007-03-18 10:43:23',
4480
					'updated' => '2007-03-18 10:45:31'
4481
				)
4482
			),
4483
			array(
4484
				'Post' => array(
4485
					'id' => '4',
4486
					'author_id' => '2',
4487
					'title' => 'Creating a fourth post',
4488
					'body' => 'Fourth post body',
4489
					'published' => 'N'
4490
				)
4491
			)
4492
		);
4493
 
4494
		$this->assertEquals(self::date(), $result[0]['Post']['updated']);
4495
		$this->assertEquals(self::date(), $result[1]['Post']['updated']);
4496
		$this->assertEquals(self::date(), $result[3]['Post']['updated']);
4497
		$this->assertEquals(self::date(), $result[3]['Post']['created']);
4498
		unset(
4499
			$result[0]['Post']['updated'], $result[1]['Post']['updated'],
4500
			$result[3]['Post']['updated'], $result[3]['Post']['created']
4501
		);
4502
		$this->assertEquals($expected, $result);
4503
		$this->assertEquals($errors, $TestModel->validationErrors);
4504
 
4505
		$data = array(
4506
			array(
4507
				'id' => '1',
4508
				'title' => 'Re-Baleeted First Post',
4509
				'body' => 'Baleeted!',
4510
				'published' => 'N'
4511
			),
4512
			array(
4513
				'id' => '2',
4514
				'title' => '',
4515
				'body' => 'Trying to get away with an empty title'
4516
		));
4517
		$this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
4518
 
4519
		$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
4520
		unset(
4521
			$result[0]['Post']['updated'], $result[1]['Post']['updated'],
4522
			$result[3]['Post']['updated'], $result[3]['Post']['created']
4523
		);
4524
		$this->assertEquals($expected, $result);
4525
		$this->assertEquals($errors, $TestModel->validationErrors);
4526
	}
4527
 
4528
/**
4529
 * testSaveAllValidationOnly method
4530
 *
4531
 * @return void
4532
 */
4533
	public function testSaveAllValidationOnly() {
4534
		$this->loadFixtures('Comment', 'Attachment');
4535
		$TestModel = new Comment();
4536
		$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
4537
 
4538
		$data = array(
4539
			'Comment' => array(
4540
				'comment' => 'This is the comment'
4541
			),
4542
			'Attachment' => array(
4543
				'attachment' => ''
4544
			)
4545
		);
4546
 
4547
		$result = $TestModel->saveAll($data, array('validate' => 'only'));
4548
		$this->assertFalse($result);
4549
 
4550
		$TestModel = new Article();
4551
		$TestModel->validate = array('title' => 'notEmpty');
4552
		$result = $TestModel->saveAll(
4553
			array(
4554
 
4555
				1 => array('title' => 'title 1'),
4556
				2 => array('title' => 'title 2'),
4557
			),
4558
			array('validate' => 'only')
4559
		);
4560
		$this->assertFalse($result);
4561
		$expected = array(
4562
 
4563
		);
4564
		$this->assertEquals($expected, $TestModel->validationErrors);
4565
 
4566
		$result = $TestModel->saveAll(
4567
			array(
4568
 
4569
				1 => array('title' => ''),
4570
				2 => array('title' => 'title 2'),
4571
			),
4572
			array('validate' => 'only')
4573
		);
4574
		$this->assertFalse($result);
4575
		$expected = array(
4576
			1 => array('title' => array('This field cannot be left blank')),
4577
		);
4578
		$this->assertEquals($expected, $TestModel->validationErrors);
4579
	}
4580
 
4581
/**
4582
 * testSaveAllValidateFirst method
4583
 *
4584
 * @return void
4585
 */
4586
	public function testSaveAllValidateFirst() {
4587
		$this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
4588
		$model = new Article();
4589
		$model->deleteAll(true);
4590
 
4591
		$model->Comment->validate = array('comment' => 'notEmpty');
4592
		$result = $model->saveAll(array(
4593
			'Article' => array(
4594
				'title' => 'Post with Author',
4595
				'body' => 'This post will be saved author'
4596
			),
4597
			'Comment' => array(
4598
				array('comment' => 'First new comment'),
4599
				array('comment' => '')
4600
			)
4601
		), array('validate' => 'first'));
4602
 
4603
		$this->assertFalse($result);
4604
 
4605
		$result = $model->find('all');
4606
		$this->assertSame(array(), $result);
4607
		$expected = array('Comment' => array(
4608
			1 => array('comment' => array('This field cannot be left blank'))
4609
		));
4610
 
4611
		$this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
4612
 
4613
		$this->assertSame($model->Comment->find('count'), 0);
4614
 
4615
		$result = $model->saveAll(
4616
			array(
4617
				'Article' => array(
4618
					'title' => 'Post with Author',
4619
					'body' => 'This post will be saved with an author',
4620
					'user_id' => 2
4621
				),
4622
				'Comment' => array(
4623
					array(
4624
						'comment' => 'Only new comment',
4625
						'user_id' => 2
4626
			))),
4627
			array('validate' => 'first')
4628
		);
4629
 
4630
		$this->assertTrue($result);
4631
 
4632
		$result = $model->Comment->find('all');
4633
		$this->assertSame(count($result), 1);
4634
		$result = Hash::extract($result, '{n}.Comment.article_id');
4635
		$this->assertEquals(4, $result[0]);
4636
 
4637
		$model->deleteAll(true);
4638
		$data = array(
4639
			'Article' => array(
4640
				'title' => 'Post with Author saveAlled from comment',
4641
				'body' => 'This post will be saved with an author',
4642
				'user_id' => 2
4643
			),
4644
			'Comment' => array(
4645
				'comment' => 'Only new comment', 'user_id' => 2
4646
		));
4647
 
4648
		$result = $model->Comment->saveAll($data, array('validate' => 'first'));
4649
		$this->assertFalse(empty($result));
4650
 
4651
		$result = $model->find('all');
4652
		$this->assertEquals(
4653
			$result[0]['Article']['title'],
4654
			'Post with Author saveAlled from comment'
4655
		);
4656
		$this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
4657
	}
4658
 
4659
/**
4660
 * test saveAll()'s return is correct when using atomic = false and validate = first.
4661
 *
4662
 * @return void
4663
 */
4664
	public function testSaveAllValidateFirstAtomicFalse() {
4665
		$this->loadFixtures('Something');
4666
		$Something = new Something();
4667
		$invalidData = array(
4668
			array(
4669
				'title' => 'foo',
4670
				'body' => 'bar',
4671
				'published' => 'baz',
4672
			),
4673
			array(
4674
				'body' => 3,
4675
				'published' => 'sd',
4676
			),
4677
		);
4678
		$Something->create();
4679
		$Something->validate = array(
4680
			'title' => array(
4681
				'rule' => 'alphaNumeric',
4682
				'required' => true,
4683
			),
4684
			'body' => array(
4685
				'rule' => 'alphaNumeric',
4686
				'required' => true,
4687
				'allowEmpty' => true,
4688
			),
4689
		);
4690
		$result = $Something->saveAll($invalidData, array(
4691
			'atomic' => false,
4692
			'validate' => 'first',
4693
		));
4694
		$expected = array(true, false);
4695
		$this->assertEquals($expected, $result);
4696
 
4697
		$Something = new Something();
4698
		$validData = array(
4699
			array(
4700
				'title' => 'title value',
4701
				'body' => 'body value',
4702
				'published' => 'baz',
4703
			),
4704
			array(
4705
				'title' => 'valid',
4706
				'body' => 'this body',
4707
				'published' => 'sd',
4708
			),
4709
		);
4710
		$Something->create();
4711
		$result = $Something->saveAll($validData, array(
4712
			'atomic' => false,
4713
			'validate' => 'first',
4714
		));
4715
		$expected = array(true, true);
4716
		$this->assertEquals($expected, $result);
4717
	}
4718
 
4719
/**
4720
 * testSaveAllHasManyValidationOnly method
4721
 *
4722
 * @return void
4723
 */
4724
	public function testSaveAllHasManyValidationOnly() {
4725
		$this->loadFixtures('Article', 'Comment', 'Attachment');
4726
		$TestModel = new Article();
4727
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
4728
		$TestModel->Comment->validate = array('comment' => 'notEmpty');
4729
 
4730
		$result = $TestModel->saveAll(
4731
			array(
4732
				'Article' => array('id' => 2),
4733
				'Comment' => array(
4734
					array(
4735
						'id' => 1,
4736
						'comment' => '',
4737
						'published' => 'Y',
4738
						'user_id' => 1),
4739
					array(
4740
						'id' => 2,
4741
						'comment' =>
4742
						'comment',
4743
						'published' => 'Y',
4744
						'user_id' => 1
4745
			))),
4746
			array('validate' => 'only')
4747
		);
4748
		$this->assertFalse($result);
4749
 
4750
		$result = $TestModel->saveAll(
4751
			array(
4752
				'Article' => array('id' => 2),
4753
				'Comment' => array(
4754
					array(
4755
						'id' => 1,
4756
						'comment' => '',
4757
						'published' => 'Y',
4758
						'user_id' => 1
4759
					),
4760
					array(
4761
						'id' => 2,
4762
						'comment' => 'comment',
4763
						'published' => 'Y',
4764
						'user_id' => 1
4765
					),
4766
					array(
4767
						'id' => 3,
4768
						'comment' => '',
4769
						'published' => 'Y',
4770
						'user_id' => 1
4771
			))),
4772
			array(
4773
				'validate' => 'only',
4774
				'atomic' => false
4775
		));
4776
		$expected = array(
4777
			'Article' => true,
4778
			'Comment' => array(false, true, false)
4779
		);
4780
		$this->assertSame($expected, $result);
4781
 
4782
		$expected = array('Comment' => array(
4783
 
4784
			2 => array('comment' => array('This field cannot be left blank'))
4785
		));
4786
		$this->assertEquals($expected, $TestModel->validationErrors);
4787
 
4788
		$expected = array(
4789
 
4790
			2 => array('comment' => array('This field cannot be left blank'))
4791
		);
4792
		$this->assertEquals($expected, $TestModel->Comment->validationErrors);
4793
	}
4794
 
4795
/**
4796
 * test that saveAll still behaves like previous versions (does not necessarily need a first argument)
4797
 *
4798
 * @return void
4799
 */
4800
	public function testSaveAllWithSet() {
4801
		$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
4802
		$data = array(
4803
			'Article' => array(
4804
				'user_id' => 1,
4805
				'title' => 'Article Has and belongs to Many Tags'
4806
			),
4807
			'Tag' => array(
4808
				'Tag' => array(1, 2)
4809
			),
4810
			'Comment' => array(
4811
				array(
4812
					'comment' => 'Article comment',
4813
					'user_id' => 1
4814
		)));
4815
		$Article = new Article();
4816
		$Article->set($data);
4817
		$result = $Article->saveAll();
4818
		$this->assertFalse(empty($result));
4819
	}
4820
 
4821
/**
4822
 * test that saveAll behaves like plain save() when supplied empty data
4823
 *
4824
 * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
4825
 * @return void
4826
 */
4827
	public function testSaveAllEmptyData() {
4828
		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
4829
 
4830
		$this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
4831
		$model = new Article();
4832
		$result = $model->saveAll(array(), array('validate' => 'first'));
4833
		$this->assertFalse(empty($result));
4834
 
4835
		$model = new ProductUpdateAll();
4836
		$result = $model->saveAll();
4837
		$this->assertFalse($result);
4838
	}
4839
 
4840
/**
4841
 * testSaveAssociated method
4842
 *
4843
 * @return void
4844
 */
4845
	public function testSaveAssociated() {
4846
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
4847
		$TestModel = new Post();
4848
 
4849
		$result = $TestModel->find('all');
4850
		$this->assertEquals(3, count($result));
4851
		$this->assertFalse(isset($result[3]));
4852
 
4853
		$TestModel->saveAssociated(array(
4854
			'Post' => array(
4855
				'title' => 'Post with Author',
4856
				'body' => 'This post will be saved with an author'
4857
			),
4858
			'Author' => array(
4859
				'user' => 'bob',
4860
				'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
4861
		)));
4862
 
4863
		$result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
4864
		$expected = array(
4865
			'Post' => array(
4866
				'id' => '4',
4867
				'author_id' => '5',
4868
				'title' => 'Post with Author',
4869
				'body' => 'This post will be saved with an author',
4870
				'published' => 'N'
4871
			),
4872
			'Author' => array(
4873
				'id' => '5',
4874
				'user' => 'bob',
4875
				'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
4876
				'test' => 'working'
4877
		));
4878
		$this->assertEquals(self::date(), $result[3]['Post']['updated']);
4879
		$this->assertEquals(self::date(), $result[3]['Post']['created']);
4880
		$this->assertEquals(self::date(), $result[3]['Author']['created']);
4881
		$this->assertEquals(self::date(), $result[3]['Author']['updated']);
4882
		unset(
4883
			$result[3]['Post']['updated'], $result[3]['Post']['created'],
4884
			$result[3]['Author']['updated'], $result[3]['Author']['created']
4885
		);
4886
		$this->assertEquals($expected, $result[3]);
4887
		$this->assertEquals(4, count($result));
4888
 
4889
		$TestModel = new Comment();
4890
		$result = $TestModel->saveAssociated(array(
4891
			'Comment' => array(
4892
				'article_id' => 2,
4893
				'user_id' => 2,
4894
				'comment' => 'New comment with attachment',
4895
				'published' => 'Y'
4896
			),
4897
			'Attachment' => array(
4898
				'attachment' => 'some_file.tgz'
4899
			)));
4900
		$this->assertFalse(empty($result));
4901
 
4902
		$result = $TestModel->find('all');
4903
		$expected = array(
4904
			'id' => '7',
4905
			'article_id' => '2',
4906
			'user_id' => '2',
4907
			'comment' => 'New comment with attachment',
4908
			'published' => 'Y'
4909
		);
4910
		$this->assertEquals(self::date(), $result[6]['Comment']['updated']);
4911
		$this->assertEquals(self::date(), $result[6]['Comment']['created']);
4912
		unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
4913
		$this->assertEquals($expected, $result[6]['Comment']);
4914
 
4915
		$expected = array(
4916
			'id' => '2',
4917
			'comment_id' => '7',
4918
			'attachment' => 'some_file.tgz'
4919
		);
4920
		$this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
4921
		$this->assertEquals(self::date(), $result[6]['Attachment']['created']);
4922
		unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
4923
		$this->assertEquals($expected, $result[6]['Attachment']);
4924
	}
4925
 
4926
/**
4927
 * Test that validate = first, atomic = false works when associated records
4928
 * fail validation.
4929
 *
4930
 * @return void
4931
 */
4932
	public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
4933
		$this->loadFixtures('Comment', 'Article', 'User');
4934
		$Article = ClassRegistry::init('Article');
4935
		$Article->Comment->validator()->add('comment', array(
4936
			array('rule' => 'notEmpty')
4937
		));
4938
 
4939
		$data = array(
4940
			'Article' => array(
4941
				'user_id' => 1,
4942
				'title' => 'Foo',
4943
				'body' => 'text',
4944
				'published' => 'N'
4945
			),
4946
			'Comment' => array(
4947
				array(
4948
					'user_id' => 1,
4949
					'comment' => '',
4950
					'published' => 'N',
4951
				)
4952
			),
4953
		);
4954
 
4955
		$Article->saveAssociated(
4956
			$data,
4957
			array('validate' => 'first', 'atomic' => false)
4958
		);
4959
 
4960
		$result = $Article->validationErrors;
4961
		$expected = array(
4962
			'Comment' => array(
4963
				array(
4964
					'comment' => array('This field cannot be left blank')
4965
				)
4966
			)
4967
		);
4968
		$this->assertEquals($expected, $result);
4969
	}
4970
 
4971
/**
4972
 * testSaveMany method
4973
 *
4974
 * @return void
4975
 */
4976
	public function testSaveMany() {
4977
		$this->loadFixtures('Post');
4978
		$TestModel = new Post();
4979
		$TestModel->deleteAll(true);
4980
		$this->assertEquals(array(), $TestModel->find('all'));
4981
 
4982
		// SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
4983
		$this->db->truncate($TestModel);
4984
 
4985
		$TestModel->saveMany(array(
4986
			array(
4987
				'title' => 'Multi-record post 1',
4988
				'body' => 'First multi-record post',
4989
				'author_id' => 2
4990
			),
4991
			array(
4992
				'title' => 'Multi-record post 2',
4993
				'body' => 'Second multi-record post',
4994
				'author_id' => 2
4995
		)));
4996
 
4997
		$result = $TestModel->find('all', array(
4998
			'recursive' => -1,
4999
			'order' => 'Post.id ASC'
5000
		));
5001
		$expected = array(
5002
			array(
5003
				'Post' => array(
5004
					'id' => '1',
5005
					'author_id' => '2',
5006
					'title' => 'Multi-record post 1',
5007
					'body' => 'First multi-record post',
5008
					'published' => 'N'
5009
				)
5010
			),
5011
			array(
5012
				'Post' => array(
5013
					'id' => '2',
5014
					'author_id' => '2',
5015
					'title' => 'Multi-record post 2',
5016
					'body' => 'Second multi-record post',
5017
					'published' => 'N'
5018
				)
5019
			)
5020
		);
5021
		$this->assertEquals(self::date(), $result[0]['Post']['updated']);
5022
		$this->assertEquals(self::date(), $result[0]['Post']['created']);
5023
		$this->assertEquals(self::date(), $result[1]['Post']['updated']);
5024
		$this->assertEquals(self::date(), $result[1]['Post']['created']);
5025
		unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
5026
		unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
5027
		$this->assertEquals($expected, $result);
5028
	}
5029
 
5030
/**
5031
 * Test SaveMany with validate=false.
5032
 *
5033
 * @return void
5034
 */
5035
	public function testSaveManyValidateFalse() {
5036
		$this->loadFixtures('Post');
5037
		$TestModel = new Post();
5038
		$TestModel->deleteAll(true);
5039
		$data = array(
5040
			array('id' => 1, 'author_id' => 1, 'title' => 'hi'),
5041
			array('id' => 2, 'author_id' => 1, 'title' => 'bye')
5042
		);
5043
		$result = $TestModel->saveAll($data, array('validate' => false));
5044
		$this->assertTrue($result);
5045
	}
5046
 
5047
/**
5048
 * Test SaveAssociated with Habtm relations
5049
 *
5050
 * @return void
5051
 */
5052
	public function testSaveAssociatedHabtm() {
5053
		$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
5054
		$data = array(
5055
			'Article' => array(
5056
				'user_id' => 1,
5057
				'title' => 'Article Has and belongs to Many Tags'
5058
			),
5059
			'Tag' => array(
5060
				'Tag' => array(1, 2)
5061
			),
5062
			'Comment' => array(
5063
				array(
5064
					'comment' => 'Article comment',
5065
					'user_id' => 1
5066
		)));
5067
		$Article = new Article();
5068
		$result = $Article->saveAssociated($data);
5069
		$this->assertFalse(empty($result));
5070
 
5071
		$result = $Article->read();
5072
		$this->assertEquals(2, count($result['Tag']));
5073
		$this->assertEquals('tag1', $result['Tag'][0]['tag']);
5074
		$this->assertEquals(1, count($result['Comment']));
5075
		$this->assertEquals(1, count($result['Comment'][0]['comment']));
5076
	}
5077
 
5078
/**
5079
 * Test SaveAssociated with Habtm relations and extra join table fields
5080
 *
5081
 * @return void
5082
 */
5083
	public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
5084
		$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
5085
 
5086
		$data = array(
5087
			'Something' => array(
5088
				'id' => 4,
5089
				'title' => 'Extra Fields',
5090
				'body' => 'Extra Fields Body',
5091
				'published' => '1'
5092
			),
5093
			'SomethingElse' => array(
5094
				array('something_else_id' => 1, 'doomed' => '1'),
5095
				array('something_else_id' => 2, 'doomed' => '0'),
5096
				array('something_else_id' => 3, 'doomed' => '1')
5097
			)
5098
		);
5099
 
5100
		$Something = new Something();
5101
		$result = $Something->saveAssociated($data);
5102
		$this->assertFalse(empty($result));
5103
		$result = $Something->read();
5104
 
5105
		$this->assertEquals(3, count($result['SomethingElse']));
5106
		$this->assertTrue(Set::matches('/Something[id=4]', $result));
5107
 
5108
		$this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
5109
		$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
5110
		$this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
5111
 
5112
		$this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
5113
		$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
5114
		$this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
5115
 
5116
		$this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
5117
		$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
5118
		$this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
5119
	}
5120
 
5121
/**
5122
 * testSaveAssociatedHasOne method
5123
 *
5124
 * @return void
5125
 */
5126
	public function testSaveAssociatedHasOne() {
5127
		$model = new Comment();
5128
		$model->deleteAll(true);
5129
		$this->assertEquals(array(), $model->find('all'));
5130
 
5131
		$model->Attachment->deleteAll(true);
5132
		$this->assertEquals(array(), $model->Attachment->find('all'));
5133
 
5134
		$this->assertTrue($model->saveAssociated(array(
5135
			'Comment' => array(
5136
				'comment' => 'Comment with attachment',
5137
				'article_id' => 1,
5138
				'user_id' => 1
5139
			),
5140
			'Attachment' => array(
5141
				'attachment' => 'some_file.zip'
5142
		))));
5143
		$result = $model->find('all', array('fields' => array(
5144
			'Comment.id', 'Comment.comment', 'Attachment.id',
5145
			'Attachment.comment_id', 'Attachment.attachment'
5146
		)));
5147
		$expected = array(array(
5148
			'Comment' => array(
5149
				'id' => '1',
5150
				'comment' => 'Comment with attachment'
5151
			),
5152
			'Attachment' => array(
5153
				'id' => '1',
5154
				'comment_id' => '1',
5155
				'attachment' => 'some_file.zip'
5156
		)));
5157
		$this->assertEquals($expected, $result);
5158
 
5159
		$model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
5160
		$data = array(
5161
			'Comment' => array(
5162
				'comment' => 'Comment with attachment',
5163
				'article_id' => 1,
5164
				'user_id' => 1
5165
			),
5166
			'Attachment' => array(
5167
				'attachment' => 'some_file.zip'
5168
		));
5169
		$this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
5170
	}
5171
 
5172
/**
5173
 * testSaveAssociatedBelongsTo method
5174
 *
5175
 * @return void
5176
 */
5177
	public function testSaveAssociatedBelongsTo() {
5178
		$model = new Comment();
5179
		$model->deleteAll(true);
5180
		$this->assertEquals(array(), $model->find('all'));
5181
 
5182
		$model->Article->deleteAll(true);
5183
		$this->assertEquals(array(), $model->Article->find('all'));
5184
 
5185
		$this->assertTrue($model->saveAssociated(array(
5186
			'Comment' => array(
5187
				'comment' => 'Article comment',
5188
				'article_id' => 1,
5189
				'user_id' => 1
5190
			),
5191
			'Article' => array(
5192
				'title' => 'Model Associations 101',
5193
				'user_id' => 1
5194
		))));
5195
		$result = $model->find('all', array('fields' => array(
5196
			'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
5197
		)));
5198
		$expected = array(array(
5199
			'Comment' => array(
5200
				'id' => '1',
5201
				'article_id' => '1',
5202
				'comment' => 'Article comment'
5203
			),
5204
			'Article' => array(
5205
				'id' => '1',
5206
				'title' => 'Model Associations 101'
5207
		)));
5208
		$this->assertEquals($expected, $result);
5209
	}
5210
 
5211
/**
5212
 * testSaveAssociatedHasOneValidation method
5213
 *
5214
 * @return void
5215
 */
5216
	public function testSaveAssociatedHasOneValidation() {
5217
		$model = new Comment();
5218
		$model->deleteAll(true);
5219
		$this->assertEquals(array(), $model->find('all'));
5220
 
5221
		$model->Attachment->deleteAll(true);
5222
		$this->assertEquals(array(), $model->Attachment->find('all'));
5223
 
5224
		$model->validate = array('comment' => 'notEmpty');
5225
		$model->Attachment->validate = array('attachment' => 'notEmpty');
5226
		$model->Attachment->bindModel(array('belongsTo' => array('Comment')));
5227
 
5228
		$result = $model->saveAssociated(
5229
			array(
5230
				'Comment' => array(
5231
					'comment' => '',
5232
					'article_id' => 1,
5233
					'user_id' => 1
5234
				),
5235
				'Attachment' => array('attachment' => '')
5236
			)
5237
		);
5238
		$this->assertFalse($result);
5239
		$expected = array(
5240
			'comment' => array(
5241
				'This field cannot be left blank'
5242
			),
5243
			'Attachment' => array(
5244
				'attachment' => array(
5245
					'This field cannot be left blank'
5246
				)
5247
			)
5248
		);
5249
		$this->assertEquals($expected, $model->validationErrors);
5250
		$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
5251
	}
5252
 
5253
/**
5254
 * testSaveAssociatedAtomic method
5255
 *
5256
 * @return void
5257
 */
5258
	public function testSaveAssociatedAtomic() {
5259
		$this->loadFixtures('Article', 'User');
5260
		$TestModel = new Article();
5261
 
5262
		$result = $TestModel->saveAssociated(array(
5263
			'Article' => array(
5264
				'title' => 'Post with Author',
5265
				'body' => 'This post will be saved with an author',
5266
				'user_id' => 2
5267
			),
5268
			'Comment' => array(
5269
				array('comment' => 'First new comment', 'user_id' => 2))
5270
		), array('atomic' => false));
5271
 
5272
		$this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
5273
 
5274
		$result = $TestModel->saveAssociated(array(
5275
			'Article' => array('id' => 2),
5276
			'Comment' => array(
5277
				array(
5278
					'comment' => 'First new comment',
5279
					'published' => 'Y',
5280
					'user_id' => 1
5281
				),
5282
				array(
5283
					'comment' => 'Second new comment',
5284
					'published' => 'Y',
5285
					'user_id' => 2
5286
			))
5287
		), array('validate' => true, 'atomic' => false));
5288
		$this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
5289
	}
5290
 
5291
/**
5292
 * testSaveManyAtomic method
5293
 *
5294
 * @return void
5295
 */
5296
	public function testSaveManyAtomic() {
5297
		$this->loadFixtures('Article', 'User');
5298
		$TestModel = new Article();
5299
 
5300
		$result = $TestModel->saveMany(array(
5301
			array(
5302
				'id' => '1',
5303
				'title' => 'Baleeted First Post',
5304
				'body' => 'Baleeted!',
5305
				'published' => 'N'
5306
			),
5307
			array(
5308
				'id' => '2',
5309
				'title' => 'Just update the title'
5310
			),
5311
			array(
5312
				'title' => 'Creating a fourth post',
5313
				'body' => 'Fourth post body',
5314
				'user_id' => 2
5315
			)
5316
		), array('atomic' => false));
5317
		$this->assertSame($result, array(true, true, true));
5318
 
5319
		$TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
5320
		$result = $TestModel->saveMany(array(
5321
			array(
5322
				'id' => '1',
5323
				'title' => 'Un-Baleeted First Post',
5324
				'body' => 'Not Baleeted!',
5325
				'published' => 'Y'
5326
			),
5327
			array(
5328
				'id' => '2',
5329
				'title' => '',
5330
				'body' => 'Trying to get away with an empty title'
5331
			)
5332
		), array('validate' => true, 'atomic' => false));
5333
 
5334
		$this->assertSame(array(true, false), $result);
5335
	}
5336
 
5337
/**
5338
 * testSaveAssociatedHasMany method
5339
 *
5340
 * @return void
5341
 */
5342
	public function testSaveAssociatedHasMany() {
5343
		$this->loadFixtures('Article', 'Comment');
5344
		$TestModel = new Article();
5345
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
5346
 
5347
		$result = $TestModel->saveAssociated(array(
5348
			'Article' => array('id' => 2),
5349
			'Comment' => array(
5350
				array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
5351
				array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
5352
			)
5353
		));
5354
		$this->assertFalse(empty($result));
5355
 
5356
		$result = $TestModel->findById(2);
5357
		$expected = array(
5358
			'First Comment for Second Article',
5359
			'Second Comment for Second Article',
5360
			'First new comment',
5361
			'Second new comment'
5362
		);
5363
		$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
5364
 
5365
		$result = $TestModel->saveAssociated(
5366
			array(
5367
				'Article' => array('id' => 2),
5368
				'Comment' => array(
5369
					array(
5370
						'comment' => 'Third new comment',
5371
						'published' => 'Y',
5372
						'user_id' => 1
5373
			))),
5374
			array('atomic' => false)
5375
		);
5376
		$this->assertFalse(empty($result));
5377
 
5378
		$result = $TestModel->findById(2);
5379
		$expected = array(
5380
			'First Comment for Second Article',
5381
			'Second Comment for Second Article',
5382
			'First new comment',
5383
			'Second new comment',
5384
			'Third new comment'
5385
		);
5386
		$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
5387
 
5388
		$TestModel->beforeSaveReturn = false;
5389
		$result = $TestModel->saveAssociated(
5390
			array(
5391
				'Article' => array('id' => 2),
5392
				'Comment' => array(
5393
					array(
5394
						'comment' => 'Fourth new comment',
5395
						'published' => 'Y',
5396
						'user_id' => 1
5397
			))),
5398
			array('atomic' => false)
5399
		);
5400
		$this->assertEquals(array('Article' => false), $result);
5401
 
5402
		$result = $TestModel->findById(2);
5403
		$expected = array(
5404
			'First Comment for Second Article',
5405
			'Second Comment for Second Article',
5406
			'First new comment',
5407
			'Second new comment',
5408
			'Third new comment'
5409
		);
5410
		$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
5411
	}
5412
 
5413
/**
5414
 * testSaveAssociatedHasManyEmpty method
5415
 *
5416
 * @return void
5417
 */
5418
	public function testSaveAssociatedHasManyEmpty() {
5419
		$this->loadFixtures('Article', 'Comment');
5420
		$TestModel = new Article();
5421
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
5422
		$TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
5423
 
5424
		//empty hasMany data is ignored in save
5425
		$result = $TestModel->saveAssociated(array(
5426
			'Article' => array('title' => 'title', 'user_id' => 1),
5427
			'Comment' => array()
5428
		), array('validate' => true));
5429
		$this->assertTrue($result);
5430
 
5431
		$result = $TestModel->saveAssociated(array(
5432
			'Article' => array('title' => 'title', 'user_id' => 1),
5433
			'Comment' => array()
5434
		), array('validate' => true, 'atomic' => false));
5435
		$this->assertEquals(array('Article' => true), $result);
5436
 
5437
		//empty primary data is not ignored
5438
		$result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
5439
		$this->assertFalse($result);
5440
 
5441
		$result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
5442
		$this->assertEquals(array('Article' => false), $result);
5443
	}
5444
 
5445
/**
5446
 * testSaveAssociatedHasManyValidation method
5447
 *
5448
 * @return void
5449
 */
5450
	public function testSaveAssociatedHasManyValidation() {
5451
		$this->loadFixtures('Article', 'Comment');
5452
		$TestModel = new Article();
5453
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
5454
		$TestModel->Comment->validate = array('comment' => 'notEmpty');
5455
 
5456
		$result = $TestModel->saveAssociated(array(
5457
			'Article' => array('id' => 2),
5458
			'Comment' => array(
5459
				array('comment' => '', 'published' => 'Y', 'user_id' => 1),
5460
			)
5461
		), array('validate' => true));
5462
		$this->assertFalse($result);
5463
 
5464
		$expected = array('Comment' => array(
5465
			array('comment' => array('This field cannot be left blank'))
5466
		));
5467
		$this->assertEquals($expected, $TestModel->validationErrors);
5468
		$expected = array(
5469
			array('comment' => array('This field cannot be left blank'))
5470
		);
5471
		$this->assertEquals($expected, $TestModel->Comment->validationErrors);
5472
 
5473
		$result = $TestModel->saveAssociated(array(
5474
			'Article' => array('id' => 2),
5475
			'Comment' => array(
5476
				array(
5477
					'comment' => '',
5478
					'published' => 'Y',
5479
					'user_id' => 1
5480
			))
5481
		), array('validate' => 'first'));
5482
		$this->assertFalse($result);
5483
	}
5484
 
5485
/**
5486
 * test saveMany with transactions and ensure there is no missing rollback.
5487
 *
5488
 * @return void
5489
 */
5490
	public function testSaveManyTransactionNoRollback() {
5491
		$this->loadFixtures('Post');
5492
 
5493
		$this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
5494
		$db = ConnectionManager::create('mock_many_transaction', array(
5495
			'datasource' => 'MockManyTransactionDboSource',
5496
		));
5497
 
5498
		$db->expects($this->once())
5499
			->method('describe')
5500
			->will($this->returnValue(array()));
5501
		$db->expects($this->once())->method('rollback');
5502
 
5503
		$Post = new Post('mock_many_transaction');
5504
 
5505
		$Post->validate = array(
5506
			'title' => array('rule' => array('notEmpty'))
5507
		);
5508
 
5509
		$data = array(
5510
			array('author_id' => 1, 'title' => 'New Fourth Post'),
5511
			array('author_id' => 1, 'title' => '')
5512
		);
5513
		$Post->saveMany($data);
5514
	}
5515
 
5516
/**
5517
 * test saveAssociated with transactions and ensure there is no missing rollback.
5518
 *
5519
 * @return void
5520
 */
5521
	public function testSaveAssociatedTransactionNoRollback() {
5522
		$testDb = ConnectionManager::getDataSource('test');
5523
 
5524
		$this->getMock(
5525
			'DboSource',
5526
			array('connect', 'rollback', 'describe', 'create', 'begin'),
5527
			array(),
5528
			'MockAssociatedTransactionDboSource',
5529
			false
5530
		);
5531
		$db = ConnectionManager::create('mock_assoc_transaction', array(
5532
			'datasource' => 'MockAssociatedTransactionDboSource',
5533
		));
5534
		$this->mockObjects[] = $db;
5535
		$db->columns = $testDb->columns;
5536
 
5537
		$db->expects($this->once())->method('rollback');
5538
		$db->expects($this->any())->method('describe')
5539
			->will($this->returnValue(array(
5540
				'id' => array('type' => 'integer', 'length' => 11),
5541
				'title' => array('type' => 'string'),
5542
				'body' => array('type' => 'text'),
5543
				'published' => array('type' => 'string')
5544
			)));
5545
 
5546
		$Post = new Post();
5547
		$Post->useDbConfig = 'mock_assoc_transaction';
5548
		$Post->Author->useDbConfig = 'mock_assoc_transaction';
5549
 
5550
		$Post->Author->validate = array(
5551
			'user' => array('rule' => array('notEmpty'))
5552
		);
5553
 
5554
		$data = array(
5555
			'Post' => array(
5556
				'title' => 'New post',
5557
				'body' => 'Content',
5558
				'published' => 'Y'
5559
			),
5560
			'Author' => array(
5561
				'user' => '',
5562
				'password' => "sekret"
5563
			)
5564
		);
5565
		$Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
5566
	}
5567
 
5568
/**
5569
 * test saveMany with nested saveMany call.
5570
 *
5571
 * @return void
5572
 */
5573
	public function testSaveManyNestedSaveMany() {
5574
		$this->loadFixtures('Sample');
5575
		$TransactionManyTestModel = new TransactionManyTestModel();
5576
 
5577
		$data = array(
5578
			array('apple_id' => 1, 'name' => 'sample5'),
5579
		);
5580
 
5581
		$this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
5582
	}
5583
 
5584
/**
5585
 * testSaveManyTransaction method
5586
 *
5587
 * @return void
5588
 */
5589
	public function testSaveManyTransaction() {
5590
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
5591
		$TestModel = new Post();
5592
 
5593
		$TestModel->validate = array('title' => 'notEmpty');
5594
		$data = array(
5595
			array('author_id' => 1, 'title' => 'New Fourth Post'),
5596
			array('author_id' => 1, 'title' => 'New Fifth Post'),
5597
			array('author_id' => 1, 'title' => '')
5598
		);
5599
		$this->assertFalse($TestModel->saveMany($data));
5600
 
5601
		$result = $TestModel->find('all', array('recursive' => -1));
5602
		$expected = array(
5603
			array('Post' => array(
5604
				'id' => '1',
5605
				'author_id' => 1,
5606
				'title' => 'First Post',
5607
				'body' => 'First Post Body',
5608
				'published' => 'Y',
5609
				'created' => '2007-03-18 10:39:23',
5610
				'updated' => '2007-03-18 10:41:31'
5611
			)),
5612
			array('Post' => array(
5613
				'id' => '2',
5614
				'author_id' => 3,
5615
				'title' => 'Second Post',
5616
				'body' => 'Second Post Body',
5617
				'published' => 'Y',
5618
				'created' => '2007-03-18 10:41:23',
5619
				'updated' => '2007-03-18 10:43:31'
5620
			)),
5621
			array('Post' => array(
5622
				'id' => '3',
5623
				'author_id' => 1,
5624
				'title' => 'Third Post',
5625
				'body' => 'Third Post Body',
5626
				'published' => 'Y',
5627
				'created' => '2007-03-18 10:43:23',
5628
				'updated' => '2007-03-18 10:45:31'
5629
		)));
5630
 
5631
		if (count($result) != 3) {
5632
			// Database doesn't support transactions
5633
			$expected[] = array(
5634
				'Post' => array(
5635
					'id' => '4',
5636
					'author_id' => 1,
5637
					'title' => 'New Fourth Post',
5638
					'body' => null,
5639
					'published' => 'N'
5640
			));
5641
 
5642
			$expected[] = array(
5643
				'Post' => array(
5644
					'id' => '5',
5645
					'author_id' => 1,
5646
					'title' => 'New Fifth Post',
5647
					'body' => null,
5648
					'published' => 'N',
5649
			));
5650
 
5651
			$this->assertEquals(self::date(), $result[3]['Post']['created']);
5652
			$this->assertEquals(self::date(), $result[3]['Post']['updated']);
5653
			$this->assertEquals(self::date(), $result[4]['Post']['created']);
5654
			$this->assertEquals(self::date(), $result[4]['Post']['updated']);
5655
			unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
5656
			unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
5657
			$this->assertEquals($expected, $result);
5658
			// Skip the rest of the transactional tests
5659
			return;
5660
		}
5661
 
5662
		$this->assertEquals($expected, $result);
5663
 
5664
		$data = array(
5665
			array('author_id' => 1, 'title' => 'New Fourth Post'),
5666
			array('author_id' => 1, 'title' => ''),
5667
			array('author_id' => 1, 'title' => 'New Sixth Post')
5668
		);
5669
		$this->assertFalse($TestModel->saveMany($data));
5670
 
5671
		$result = $TestModel->find('all', array('recursive' => -1));
5672
		$expected = array(
5673
			array('Post' => array(
5674
				'id' => '1',
5675
				'author_id' => 1,
5676
				'title' => 'First Post',
5677
				'body' => 'First Post Body',
5678
				'published' => 'Y',
5679
				'created' => '2007-03-18 10:39:23',
5680
				'updated' => '2007-03-18 10:41:31'
5681
			)),
5682
			array('Post' => array(
5683
				'id' => '2',
5684
				'author_id' => 3,
5685
				'title' => 'Second Post',
5686
				'body' => 'Second Post Body',
5687
				'published' => 'Y',
5688
				'created' => '2007-03-18 10:41:23',
5689
				'updated' => '2007-03-18 10:43:31'
5690
			)),
5691
			array('Post' => array(
5692
				'id' => '3',
5693
				'author_id' => 1,
5694
				'title' => 'Third Post',
5695
				'body' => 'Third Post Body',
5696
				'published' => 'Y',
5697
				'created' => '2007-03-18 10:43:23',
5698
				'updated' => '2007-03-18 10:45:31'
5699
		)));
5700
 
5701
		if (count($result) != 3) {
5702
			// Database doesn't support transactions
5703
			$expected[] = array(
5704
				'Post' => array(
5705
					'id' => '4',
5706
					'author_id' => 1,
5707
					'title' => 'New Fourth Post',
5708
					'body' => 'Third Post Body',
5709
					'published' => 'N'
5710
			));
5711
 
5712
			$expected[] = array(
5713
				'Post' => array(
5714
					'id' => '5',
5715
					'author_id' => 1,
5716
					'title' => 'Third Post',
5717
					'body' => 'Third Post Body',
5718
					'published' => 'N'
5719
			));
5720
			$this->assertEquals(self::date(), $result[3]['Post']['created']);
5721
			$this->assertEquals(self::date(), $result[3]['Post']['updated']);
5722
			$this->assertEquals(self::date(), $result[4]['Post']['created']);
5723
			$this->assertEquals(self::date(), $result[4]['Post']['updated']);
5724
			unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
5725
			unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
5726
		}
5727
		$this->assertEquals($expected, $result);
5728
 
5729
		$TestModel->validate = array('title' => 'notEmpty');
5730
		$data = array(
5731
			array('author_id' => 1, 'title' => 'New Fourth Post'),
5732
			array('author_id' => 1, 'title' => 'New Fifth Post'),
5733
			array('author_id' => 1, 'title' => 'New Sixth Post')
5734
		);
5735
		$this->assertTrue($TestModel->saveMany($data));
5736
 
5737
		$result = $TestModel->find('all', array(
5738
			'recursive' => -1,
5739
			'fields' => array('author_id', 'title', 'body', 'published'),
5740
			'order' => array('Post.created' => 'ASC')
5741
		));
5742
 
5743
		$expected = array(
5744
			array('Post' => array(
5745
				'author_id' => 1,
5746
				'title' => 'First Post',
5747
				'body' => 'First Post Body',
5748
				'published' => 'Y'
5749
			)),
5750
			array('Post' => array(
5751
				'author_id' => 3,
5752
				'title' => 'Second Post',
5753
				'body' => 'Second Post Body',
5754
				'published' => 'Y'
5755
			)),
5756
			array('Post' => array(
5757
				'author_id' => 1,
5758
				'title' => 'Third Post',
5759
				'body' => 'Third Post Body',
5760
				'published' => 'Y'
5761
			)),
5762
			array('Post' => array(
5763
				'author_id' => 1,
5764
				'title' => 'New Fourth Post',
5765
				'body' => '',
5766
				'published' => 'N'
5767
			)),
5768
			array('Post' => array(
5769
				'author_id' => 1,
5770
				'title' => 'New Fifth Post',
5771
				'body' => '',
5772
				'published' => 'N'
5773
			)),
5774
			array('Post' => array(
5775
				'author_id' => 1,
5776
				'title' => 'New Sixth Post',
5777
				'body' => '',
5778
				'published' => 'N'
5779
		)));
5780
		$this->assertEquals($expected, $result);
5781
	}
5782
 
5783
/**
5784
 * testSaveManyValidation method
5785
 *
5786
 * @return void
5787
 */
5788
	public function testSaveManyValidation() {
5789
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
5790
		$TestModel = new Post();
5791
 
5792
		$data = array(
5793
			array(
5794
				'id' => '1',
5795
				'title' => 'Baleeted First Post',
5796
				'body' => 'Baleeted!',
5797
				'published' => 'N'
5798
			),
5799
			array(
5800
				'id' => '2',
5801
				'title' => 'Just update the title'
5802
			),
5803
			array(
5804
				'title' => 'Creating a fourth post',
5805
				'body' => 'Fourth post body',
5806
				'author_id' => 2
5807
		));
5808
 
5809
		$this->assertTrue($TestModel->saveMany($data));
5810
 
5811
		$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
5812
		$expected = array(
5813
			array(
5814
				'Post' => array(
5815
					'id' => '1',
5816
					'author_id' => '1',
5817
					'title' => 'Baleeted First Post',
5818
					'body' => 'Baleeted!',
5819
					'published' => 'N',
5820
					'created' => '2007-03-18 10:39:23'
5821
				)
5822
			),
5823
			array(
5824
				'Post' => array(
5825
					'id' => '2',
5826
					'author_id' => '3',
5827
					'title' => 'Just update the title',
5828
					'body' => 'Second Post Body',
5829
					'published' => 'Y',
5830
					'created' => '2007-03-18 10:41:23'
5831
				)
5832
			),
5833
			array(
5834
				'Post' => array(
5835
					'id' => '3',
5836
					'author_id' => '1',
5837
					'title' => 'Third Post',
5838
					'body' => 'Third Post Body',
5839
					'published' => 'Y',
5840
					'created' => '2007-03-18 10:43:23',
5841
					'updated' => '2007-03-18 10:45:31'
5842
			)),
5843
			array(
5844
				'Post' => array(
5845
					'id' => '4',
5846
					'author_id' => '2',
5847
					'title' => 'Creating a fourth post',
5848
					'body' => 'Fourth post body',
5849
					'published' => 'N'
5850
				)
5851
			)
5852
		);
5853
 
5854
		$this->assertEquals(self::date(), $result[0]['Post']['updated']);
5855
		$this->assertEquals(self::date(), $result[1]['Post']['updated']);
5856
		$this->assertEquals(self::date(), $result[3]['Post']['created']);
5857
		$this->assertEquals(self::date(), $result[3]['Post']['updated']);
5858
		unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
5859
		unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
5860
		$this->assertEquals($expected, $result);
5861
 
5862
		$TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
5863
		$data = array(
5864
			array(
5865
				'id' => '1',
5866
				'title' => 'Un-Baleeted First Post',
5867
				'body' => 'Not Baleeted!',
5868
				'published' => 'Y'
5869
			),
5870
			array(
5871
				'id' => '2',
5872
				'title' => '',
5873
				'body' => 'Trying to get away with an empty title'
5874
		));
5875
		$result = $TestModel->saveMany($data);
5876
		$this->assertFalse($result);
5877
 
5878
		$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
5879
		$errors = array(1 => array('title' => array('This field cannot be left blank')));
5880
		$transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
5881
		if (!$transactionWorked) {
5882
			$this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
5883
			$this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
5884
		}
5885
 
5886
		$this->assertEquals($errors, $TestModel->validationErrors);
5887
 
5888
		$TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
5889
		$data = array(
5890
			array(
5891
				'id' => '1',
5892
				'title' => 'Un-Baleeted First Post',
5893
				'body' => 'Not Baleeted!',
5894
				'published' => 'Y'
5895
			),
5896
			array(
5897
				'id' => '2',
5898
				'title' => '',
5899
				'body' => 'Trying to get away with an empty title'
5900
		));
5901
		$result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
5902
		$this->assertEquals(array(true, false), $result);
5903
 
5904
		$result = $TestModel->find('all', array(
5905
			'fields' => array('id', 'author_id', 'title', 'body', 'published'),
5906
			'recursive' => -1,
5907
			'order' => 'Post.id ASC'
5908
		));
5909
		$errors = array(1 => array('title' => array('This field cannot be left blank')));
5910
		$expected = array(
5911
			array(
5912
				'Post' => array(
5913
					'id' => '1',
5914
					'author_id' => '1',
5915
					'title' => 'Un-Baleeted First Post',
5916
					'body' => 'Not Baleeted!',
5917
					'published' => 'Y',
5918
			)),
5919
			array(
5920
				'Post' => array(
5921
					'id' => '2',
5922
					'author_id' => '3',
5923
					'title' => 'Just update the title',
5924
					'body' => 'Second Post Body',
5925
					'published' => 'Y',
5926
			)),
5927
			array(
5928
				'Post' => array(
5929
					'id' => '3',
5930
					'author_id' => '1',
5931
					'title' => 'Third Post',
5932
					'body' => 'Third Post Body',
5933
					'published' => 'Y',
5934
			)),
5935
			array(
5936
				'Post' => array(
5937
					'id' => '4',
5938
					'author_id' => '2',
5939
					'title' => 'Creating a fourth post',
5940
					'body' => 'Fourth post body',
5941
					'published' => 'N',
5942
		)));
5943
		$this->assertEquals($expected, $result);
5944
		$this->assertEquals($errors, $TestModel->validationErrors);
5945
 
5946
		$data = array(
5947
			array(
5948
				'id' => '1',
5949
				'title' => 'Re-Baleeted First Post',
5950
				'body' => 'Baleeted!',
5951
				'published' => 'N'
5952
			),
5953
			array(
5954
				'id' => '2',
5955
				'title' => '',
5956
				'body' => 'Trying to get away with an empty title'
5957
		));
5958
		$this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
5959
 
5960
		$result = $TestModel->find('all', array(
5961
			'fields' => array('id', 'author_id', 'title', 'body', 'published'),
5962
			'recursive' => -1,
5963
			'order' => 'Post.id ASC'
5964
		));
5965
		$this->assertEquals($expected, $result);
5966
		$this->assertEquals($errors, $TestModel->validationErrors);
5967
	}
5968
 
5969
/**
5970
 * testValidateMany method
5971
 *
5972
 * @return void
5973
 */
5974
	public function testValidateMany() {
5975
		$TestModel = new Article();
5976
		$TestModel->validate = array('title' => 'notEmpty');
5977
		$data = array(
5978
 
5979
				1 => array('title' => 'title 1'),
5980
				2 => array('title' => 'title 2'),
5981
		);
5982
		$result = $TestModel->validateMany($data);
5983
		$this->assertFalse($result);
5984
		$expected = array(
5985
 
5986
		);
5987
		$this->assertEquals($expected, $TestModel->validationErrors);
5988
 
5989
		$data = array(
5990
 
5991
				1 => array('title' => ''),
5992
				2 => array('title' => 'title 2'),
5993
		);
5994
		$result = $TestModel->validateMany($data);
5995
		$this->assertFalse($result);
5996
		$expected = array(
5997
			1 => array('title' => array('This field cannot be left blank')),
5998
		);
5999
		$this->assertEquals($expected, $TestModel->validationErrors);
6000
	}
6001
 
6002
/**
6003
 * testSaveAssociatedValidateFirst method
6004
 *
6005
 * @return void
6006
 */
6007
	public function testSaveAssociatedValidateFirst() {
6008
		$this->loadFixtures('Article', 'Comment', 'Attachment');
6009
		$model = new Article();
6010
		$model->deleteAll(true);
6011
 
6012
		$model->Comment->validate = array('comment' => 'notEmpty');
6013
		$result = $model->saveAssociated(array(
6014
			'Article' => array(
6015
				'title' => 'Post with Author',
6016
				'body' => 'This post will be saved author'
6017
			),
6018
			'Comment' => array(
6019
				array('comment' => 'First new comment'),
6020
				array('comment' => '')
6021
			)
6022
		), array('validate' => 'first'));
6023
 
6024
		$this->assertFalse($result);
6025
 
6026
		$result = $model->find('all');
6027
		$this->assertSame(array(), $result);
6028
		$expected = array('Comment' => array(
6029
			1 => array('comment' => array('This field cannot be left blank'))
6030
		));
6031
 
6032
		$this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
6033
 
6034
		$this->assertSame($model->Comment->find('count'), 0);
6035
 
6036
		$result = $model->saveAssociated(
6037
			array(
6038
				'Article' => array(
6039
					'title' => 'Post with Author',
6040
					'body' => 'This post will be saved with an author',
6041
					'user_id' => 2
6042
				),
6043
				'Comment' => array(
6044
					array(
6045
						'comment' => 'Only new comment',
6046
						'user_id' => 2
6047
			))),
6048
			array('validate' => 'first')
6049
		);
6050
 
6051
		$this->assertTrue($result);
6052
 
6053
		$result = $model->Comment->find('all');
6054
		$this->assertSame(count($result), 1);
6055
		$result = Hash::extract($result, '{n}.Comment.article_id');
6056
		$this->assertEquals(4, $result[0]);
6057
 
6058
		$model->deleteAll(true);
6059
		$data = array(
6060
			'Article' => array(
6061
				'title' => 'Post with Author saveAlled from comment',
6062
				'body' => 'This post will be saved with an author',
6063
				'user_id' => 2
6064
			),
6065
			'Comment' => array(
6066
				'comment' => 'Only new comment', 'user_id' => 2
6067
		));
6068
 
6069
		$result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
6070
		$this->assertFalse(empty($result));
6071
 
6072
		$result = $model->find('all');
6073
		$this->assertEquals(
6074
			'Post with Author saveAlled from comment',
6075
			$result[0]['Article']['title']
6076
		);
6077
		$this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
6078
	}
6079
 
6080
/**
6081
 * test saveMany()'s return is correct when using atomic = false and validate = first.
6082
 *
6083
 * @return void
6084
 */
6085
	public function testSaveManyValidateFirstAtomicFalse() {
6086
		$Something = new Something();
6087
		$invalidData = array(
6088
			array(
6089
				'title' => 'foo',
6090
				'body' => 'bar',
6091
				'published' => 'baz',
6092
			),
6093
			array(
6094
				'body' => 3,
6095
				'published' => 'sd',
6096
			),
6097
		);
6098
		$Something->create();
6099
		$Something->validate = array(
6100
			'title' => array(
6101
				'rule' => 'alphaNumeric',
6102
				'required' => true,
6103
			),
6104
			'body' => array(
6105
				'rule' => 'alphaNumeric',
6106
				'required' => true,
6107
				'allowEmpty' => true,
6108
			),
6109
		);
6110
		$result = $Something->saveMany($invalidData, array(
6111
			'atomic' => false,
6112
			'validate' => 'first',
6113
		));
6114
		$expected = array(true, false);
6115
		$this->assertEquals($expected, $result);
6116
 
6117
		$Something = new Something();
6118
		$validData = array(
6119
			array(
6120
				'title' => 'title value',
6121
				'body' => 'body value',
6122
				'published' => 'baz',
6123
			),
6124
			array(
6125
				'title' => 'valid',
6126
				'body' => 'this body',
6127
				'published' => 'sd',
6128
			),
6129
		);
6130
		$Something->create();
6131
		$result = $Something->saveMany($validData, array(
6132
			'atomic' => false,
6133
			'validate' => 'first',
6134
		));
6135
		$expected = array(true, true);
6136
		$this->assertEquals($expected, $result);
6137
	}
6138
 
6139
/**
6140
 * testValidateAssociated method
6141
 *
6142
 * @return void
6143
 */
6144
	public function testValidateAssociated() {
6145
		$this->loadFixtures('Attachment', 'Article', 'Comment');
6146
		$TestModel = new Comment();
6147
		$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
6148
 
6149
		$data = array(
6150
			'Comment' => array(
6151
				'comment' => 'This is the comment'
6152
			),
6153
			'Attachment' => array(
6154
				'attachment' => ''
6155
			)
6156
		);
6157
 
6158
		$result = $TestModel->validateAssociated($data);
6159
		$this->assertFalse($result);
6160
 
6161
		$TestModel = new Article();
6162
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
6163
		$TestModel->Comment->validate = array('comment' => 'notEmpty');
6164
 
6165
		$data = array(
6166
			'Article' => array('id' => 2),
6167
			'Comment' => array(
6168
				array(
6169
					'id' => 1,
6170
					'comment' => '',
6171
					'published' => 'Y',
6172
					'user_id' => 1),
6173
				array(
6174
					'id' => 2,
6175
					'comment' =>
6176
					'comment',
6177
					'published' => 'Y',
6178
					'user_id' => 1
6179
		)));
6180
		$result = $TestModel->validateAssociated($data);
6181
		$this->assertFalse($result);
6182
 
6183
		$data = array(
6184
			'Article' => array('id' => 2),
6185
			'Comment' => array(
6186
				array(
6187
					'id' => 1,
6188
					'comment' => '',
6189
					'published' => 'Y',
6190
					'user_id' => 1
6191
				),
6192
				array(
6193
					'id' => 2,
6194
					'comment' => 'comment',
6195
					'published' => 'Y',
6196
					'user_id' => 1
6197
				),
6198
				array(
6199
					'id' => 3,
6200
					'comment' => '',
6201
					'published' => 'Y',
6202
					'user_id' => 1
6203
		)));
6204
		$result = $TestModel->validateAssociated($data, array(
6205
				'atomic' => false
6206
		));
6207
		$expected = array(
6208
			'Article' => true,
6209
			'Comment' => array(false, true, false)
6210
		);
6211
		$this->assertSame($expected, $result);
6212
 
6213
		$expected = array('Comment' => array(
6214
 
6215
			2 => array('comment' => array('This field cannot be left blank'))
6216
		));
6217
		$this->assertEquals($expected, $TestModel->validationErrors);
6218
 
6219
		$expected = array(
6220
 
6221
			2 => array('comment' => array('This field cannot be left blank'))
6222
		);
6223
		$this->assertEquals($expected, $TestModel->Comment->validationErrors);
6224
	}
6225
 
6226
/**
6227
 * test that saveMany behaves like plain save() when suplied empty data
6228
 *
6229
 * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
6230
 * @return void
6231
 */
6232
	public function testSaveManyEmptyData() {
6233
		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
6234
 
6235
		$this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
6236
		$model = new Article();
6237
		$result = $model->saveMany(array(), array('validate' => true));
6238
		$this->assertFalse(empty($result));
6239
 
6240
		$model = new ProductUpdateAll();
6241
		$result = $model->saveMany(array());
6242
		$this->assertFalse($result);
6243
	}
6244
 
6245
/**
6246
 * test that saveAssociated behaves like plain save() when supplied empty data
6247
 *
6248
 * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
6249
 * @return void
6250
 */
6251
	public function testSaveAssociatedEmptyData() {
6252
		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
6253
 
6254
		$this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
6255
		$model = new Article();
6256
		$result = $model->saveAssociated(array(), array('validate' => true));
6257
		$this->assertFalse(empty($result));
6258
 
6259
		$model = new ProductUpdateAll();
6260
		$result = $model->saveAssociated(array());
6261
		$this->assertFalse($result);
6262
	}
6263
 
6264
/**
6265
 * Test that saveAssociated will accept expression object values when saving.
6266
 *
6267
 * @return void
6268
 */
6269
	public function testSaveAssociatedExpressionObjects() {
6270
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
6271
		$TestModel = new Post();
6272
		$db = $TestModel->getDataSource();
6273
 
6274
		$TestModel->saveAssociated(array(
6275
			'Post' => array(
6276
				'title' => $db->expression("(SELECT 'Post with Author')"),
6277
				'body' => 'This post will be saved with an author'
6278
			),
6279
			'Author' => array(
6280
				'user' => 'bob',
6281
				'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
6282
		)), array('atomic' => false));
6283
 
6284
		$result = $TestModel->find('first', array(
6285
			'order' => array('Post.id ' => 'DESC')
6286
		));
6287
		$this->assertEquals('Post with Author', $result['Post']['title']);
6288
	}
6289
 
6290
/**
6291
 * testUpdateWithCalculation method
6292
 *
6293
 * @return void
6294
 */
6295
	public function testUpdateWithCalculation() {
6296
		$this->loadFixtures('DataTest');
6297
		$model = new DataTest();
6298
		$model->deleteAll(true);
6299
		$result = $model->saveMany(array(
6300
			array('count' => 5, 'float' => 1.1),
6301
			array('count' => 3, 'float' => 1.2),
6302
			array('count' => 4, 'float' => 1.3),
6303
			array('count' => 1, 'float' => 2.0),
6304
		));
6305
		$this->assertFalse(empty($result));
6306
 
6307
		$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
6308
		$this->assertEquals(array(5, 3, 4, 1), $result);
6309
 
6310
		$this->assertTrue($model->updateAll(array('count' => 'count + 2')));
6311
		$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
6312
		$this->assertEquals(array(7, 5, 6, 3), $result);
6313
 
6314
		$this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
6315
		$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
6316
		$this->assertEquals(array(6, 4, 5, 2), $result);
6317
	}
6318
 
6319
	public function testToggleBoolFields() {
6320
		$this->loadFixtures('CounterCacheUser', 'CounterCachePost');
6321
		$Post = new CounterCachePost();
6322
		$Post->unbindModel(array('belongsTo' => array('User')), true);
6323
 
6324
		$true = array('Post' => array('published' => true, 'id' => 2));
6325
		$false = array('Post' => array('published' => false, 'id' => 2));
6326
		$fields = array('Post.published', 'Post.id');
6327
		$updateConditions = array('Post.id' => 2);
6328
 
6329
		// check its true
6330
		$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
6331
		$this->assertEquals($true, $result);
6332
 
6333
		// Testing without the alias
6334
		$this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
6335
		$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
6336
		$this->assertEquals($false, $result);
6337
 
6338
		$this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
6339
		$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
6340
		$this->assertEquals($true, $result);
6341
 
6342
		$db = ConnectionManager::getDataSource('test');
6343
		$alias = $db->name('Post.published');
6344
 
6345
		// Testing with the alias
6346
		$this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
6347
		$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
6348
		$this->assertEquals($false, $result);
6349
 
6350
		$this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
6351
		$result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
6352
		$this->assertEquals($true, $result);
6353
	}
6354
 
6355
/**
6356
 * TestFindAllWithoutForeignKey
6357
 *
6358
 * @return void
6359
 */
6360
	public function testFindAllForeignKey() {
6361
		$this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
6362
		$ProductUpdateAll = new ProductUpdateAll();
6363
 
6364
		$conditions = array('Group.name' => 'group one');
6365
 
6366
		$ProductUpdateAll->bindModel(array(
6367
			'belongsTo' => array(
6368
				'Group' => array('className' => 'GroupUpdateAll')
6369
			)
6370
		));
6371
 
6372
		$ProductUpdateAll->belongsTo = array(
6373
			'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
6374
		);
6375
 
6376
		$results = $ProductUpdateAll->find('all', compact('conditions'));
6377
		$this->assertTrue(!empty($results));
6378
 
6379
		$ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
6380
		$ProductUpdateAll->belongsTo = array(
6381
			'Group' => array(
6382
				'className' => 'GroupUpdateAll',
6383
				'foreignKey' => false,
6384
				'conditions' => 'ProductUpdateAll.groupcode = Group.code'
6385
			));
6386
 
6387
		$resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
6388
		$this->assertTrue(!empty($resultsFkFalse));
6389
		$expected = array(
6390
			'0' => array(
6391
				'ProductUpdateAll' => array(
6392
					'id' => 1,
6393
					'name'	=> 'product one',
6394
					'groupcode' => 120,
6395
					'group_id' => 1),
6396
				'Group' => array(
6397
					'id' => 1,
6398
					'name' => 'group one',
6399
					'code' => 120)
6400
				),
6401
			'1' => array(
6402
				'ProductUpdateAll' => array(
6403
					'id' => 2,
6404
					'name'	=> 'product two',
6405
					'groupcode'	=> 120,
6406
					'group_id'	=> 1),
6407
				'Group' => array(
6408
					'id' => 1,
6409
					'name' => 'group one',
6410
					'code' => 120)
6411
				)
6412
 
6413
			);
6414
		$this->assertEquals($expected, $results);
6415
		$this->assertEquals($expected, $resultsFkFalse);
6416
	}
6417
 
6418
/**
6419
 * test updateAll with empty values.
6420
 *
6421
 * @return void
6422
 */
6423
	public function testUpdateAllEmptyValues() {
6424
		$this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
6425
 
6426
		$this->loadFixtures('Author', 'Post');
6427
		$model = new Author();
6428
		$result = $model->updateAll(array('user' => '""'));
6429
		$this->assertTrue($result);
6430
	}
6431
 
6432
/**
6433
 * testUpdateAllWithJoins
6434
 *
6435
 * @return void
6436
 */
6437
	public function testUpdateAllWithJoins() {
6438
		$this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
6439
 
6440
		$this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
6441
		$ProductUpdateAll = new ProductUpdateAll();
6442
 
6443
		$conditions = array('Group.name' => 'group one');
6444
 
6445
		$ProductUpdateAll->bindModel(array('belongsTo' => array(
6446
			'Group' => array('className' => 'GroupUpdateAll')))
6447
		);
6448
 
6449
		$ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
6450
		$results = $ProductUpdateAll->find('all', array(
6451
			'conditions' => array('ProductUpdateAll.name' => 'new product')
6452
		));
6453
		$expected = array(
6454
			'0' => array(
6455
				'ProductUpdateAll' => array(
6456
					'id' => 1,
6457
					'name' => 'new product',
6458
					'groupcode'	=> 120,
6459
					'group_id' => 1),
6460
				'Group' => array(
6461
					'id' => 1,
6462
					'name' => 'group one',
6463
					'code' => 120)
6464
				),
6465
			'1' => array(
6466
				'ProductUpdateAll' => array(
6467
					'id' => 2,
6468
					'name' => 'new product',
6469
					'groupcode' => 120,
6470
					'group_id' => 1),
6471
				'Group' => array(
6472
					'id' => 1,
6473
					'name' => 'group one',
6474
					'code' => 120)));
6475
 
6476
		$this->assertEquals($expected, $results);
6477
	}
6478
 
6479
/**
6480
 * testUpdateAllWithoutForeignKey
6481
 *
6482
 * @return void
6483
 */
6484
	public function testUpdateAllWithoutForeignKey() {
6485
		$this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
6486
 
6487
		$this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
6488
		$ProductUpdateAll = new ProductUpdateAll();
6489
 
6490
		$conditions = array('Group.name' => 'group one');
6491
 
6492
		$ProductUpdateAll->bindModel(array('belongsTo' => array(
6493
			'Group' => array('className' => 'GroupUpdateAll')
6494
		)));
6495
 
6496
		$ProductUpdateAll->belongsTo = array(
6497
			'Group' => array(
6498
				'className' => 'GroupUpdateAll',
6499
				'foreignKey' => false,
6500
				'conditions' => 'ProductUpdateAll.groupcode = Group.code'
6501
			)
6502
		);
6503
 
6504
		$ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
6505
		$resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
6506
		$expected = array(
6507
			'0' => array(
6508
				'ProductUpdateAll' => array(
6509
					'id' => 1,
6510
					'name' => 'new product',
6511
					'groupcode'	=> 120,
6512
					'group_id' => 1),
6513
				'Group' => array(
6514
					'id' => 1,
6515
					'name' => 'group one',
6516
					'code' => 120)
6517
				),
6518
			'1' => array(
6519
				'ProductUpdateAll' => array(
6520
					'id' => 2,
6521
					'name' => 'new product',
6522
					'groupcode' => 120,
6523
					'group_id' => 1),
6524
				'Group' => array(
6525
					'id' => 1,
6526
					'name' => 'group one',
6527
					'code' => 120)));
6528
		$this->assertEquals($expected, $resultsFkFalse);
6529
	}
6530
 
6531
/**
6532
 * test writing floats in german locale.
6533
 *
6534
 * @return void
6535
 */
6536
	public function testWriteFloatAsGerman() {
6537
		$restore = setlocale(LC_NUMERIC, 0);
6538
 
6539
		$this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
6540
 
6541
		$model = new DataTest();
6542
		$result = $model->save(array(
6543
			'count' => 1,
6544
			'float' => 3.14593
6545
		));
6546
		$this->assertTrue((bool)$result);
6547
		setlocale(LC_NUMERIC, $restore);
6548
	}
6549
 
6550
/**
6551
 * Test returned array contains primary key when save creates a new record
6552
 *
6553
 * @return void
6554
 */
6555
	public function testPkInReturnArrayForCreate() {
6556
		$this->loadFixtures('Article');
6557
		$TestModel = new Article();
6558
 
6559
		$data = array('Article' => array(
6560
			'user_id' => '1',
6561
			'title' => 'Fourth Article',
6562
			'body' => 'Fourth Article Body',
6563
			'published' => 'Y'
6564
		));
6565
		$result = $TestModel->save($data);
6566
		$this->assertSame($result['Article']['id'], $TestModel->id);
6567
	}
6568
 
6569
/**
6570
 * testSaveAllFieldListValidateBelongsTo
6571
 *
6572
 * @return void
6573
 */
6574
	public function testSaveAllFieldListValidateBelongsTo() {
6575
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
6576
		$TestModel = new Post();
6577
 
6578
		$result = $TestModel->find('all');
6579
		$this->assertEquals(3, count($result));
6580
		$this->assertFalse(isset($result[3]));
6581
 
6582
		// test belongsTo
6583
		$fieldList = array(
6584
			'Post' => array('title'),
6585
			'Author' => array('user')
6586
		);
6587
		$data = array(
6588
			'Post' => array(
6589
				'title' => 'Post without body',
6590
				'body' => 'This will not be saved',
6591
			),
6592
			'Author' => array(
6593
				'user' => 'bob',
6594
				'test' => 'This will not be saved',
6595
 
6596
		));
6597
		$TestModel->saveAll($data, array('fieldList' => $fieldList));
6598
 
6599
		$result = $TestModel->find('all', array(
6600
			'order' => 'Post.id ASC',
6601
		));
6602
		$expected = array(
6603
			'Post' => array(
6604
				'id' => '4',
6605
				'author_id' => '5',
6606
				'title' => 'Post without body',
6607
				'body' => null,
6608
				'published' => 'N',
6609
				'created' => self::date(),
6610
				'updated' => self::date(),
6611
			),
6612
			'Author' => array(
6613
				'id' => '5',
6614
				'user' => 'bob',
6615
				'password' => null,
6616
				'created' => self::date(),
6617
				'updated' => self::date(),
6618
				'test' => 'working',
6619
			),
6620
		);
6621
		$this->assertEquals($expected, $result[3]);
6622
		$this->assertEquals(4, count($result));
6623
		$this->assertEquals('', $result[3]['Post']['body']);
6624
		$this->assertEquals('working', $result[3]['Author']['test']);
6625
 
6626
		$fieldList = array(
6627
			'Post' => array('title')
6628
		);
6629
		$data = array(
6630
			'Post' => array(
6631
				'title' => 'Post without body 2',
6632
				'body' => 'This will not be saved'
6633
			),
6634
			'Author' => array(
6635
				'user' => 'jack'
6636
			)
6637
		);
6638
		$TestModel->saveAll($data, array('fieldList' => $fieldList));
6639
		$result = $TestModel->find('all', array(
6640
			'order' => 'Post.id ASC',
6641
		));
6642
		$this->assertNull($result[4]['Post']['body']);
6643
 
6644
		$fieldList = array(
6645
			'Author' => array('password')
6646
		);
6647
		$data = array(
6648
			'Post' => array(
6649
				'id' => '5',
6650
				'title' => 'Post title',
6651
				'body' => 'Post body'
6652
			),
6653
			'Author' => array(
6654
				'id' => '6',
6655
				'user' => 'will not change',
6656
				'password' => 'foobar'
6657
			)
6658
		);
6659
		$result = $TestModel->saveAll($data, array('fieldList' => $fieldList));
6660
		$this->assertTrue($result);
6661
 
6662
		$result = $TestModel->find('all', array(
6663
			'order' => 'Post.id ASC',
6664
		));
6665
		$expected = array(
6666
			'Post' => array(
6667
				'id' => '5',
6668
				'author_id' => '6',
6669
				'title' => 'Post title',
6670
				'body' => 'Post body',
6671
				'published' => 'N',
6672
				'created' => self::date(),
6673
				'updated' => self::date()
6674
			),
6675
			'Author' => array(
6676
				'id' => '6',
6677
				'user' => 'jack',
6678
				'password' => 'foobar',
6679
				'created' => self::date(),
6680
				'updated' => self::date(),
6681
				'test' => 'working'
6682
			),
6683
		);
6684
		$this->assertEquals($expected, $result[4]);
6685
 
6686
		// test multirecord
6687
		$this->db->truncate($TestModel);
6688
 
6689
		$fieldList = array('title', 'author_id');
6690
		$TestModel->saveAll(array(
6691
			array(
6692
				'title' => 'Multi-record post 1',
6693
				'body' => 'First multi-record post',
6694
				'author_id' => 2
6695
			),
6696
			array(
6697
				'title' => 'Multi-record post 2',
6698
				'body' => 'Second multi-record post',
6699
				'author_id' => 2
6700
		)), array('fieldList' => $fieldList));
6701
 
6702
		$result = $TestModel->find('all', array(
6703
			'recursive' => -1,
6704
			'order' => 'Post.id ASC'
6705
		));
6706
		$expected = array(
6707
			array(
6708
				'Post' => array(
6709
					'id' => '1',
6710
					'author_id' => '2',
6711
					'title' => 'Multi-record post 1',
6712
					'body' => '',
6713
					'published' => 'N',
6714
					'created' => self::date(),
6715
					'updated' => self::date()
6716
				)
6717
			),
6718
			array(
6719
				'Post' => array(
6720
					'id' => '2',
6721
					'author_id' => '2',
6722
					'title' => 'Multi-record post 2',
6723
					'body' => '',
6724
					'published' => 'N',
6725
					'created' => self::date(),
6726
					'updated' => self::date()
6727
				)
6728
			)
6729
		);
6730
		$this->assertEquals($expected, $result);
6731
	}
6732
 
6733
/**
6734
 * testSaveAllFieldListHasMany method
6735
 *
6736
 * return @void
6737
 */
6738
	public function testSaveAllFieldListHasMany() {
6739
		$this->loadFixtures('Article', 'Comment');
6740
		$TestModel = new Article();
6741
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
6742
 
6743
		$this->db->truncate($TestModel);
6744
		$this->db->truncate(new Comment());
6745
 
6746
		$data = array(
6747
			'Article' => array('title' => 'I will not save'),
6748
			'Comment' => array(
6749
				array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
6750
				array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
6751
			)
6752
		);
6753
 
6754
		$fieldList = array(
6755
			'Article' => array('id'),
6756
			'Comment' => array('article_id', 'user_id')
6757
		);
6758
		$TestModel->saveAll($data, array('fieldList' => $fieldList));
6759
 
6760
		$result = $TestModel->find('all');
6761
		$this->assertEquals('', $result[0]['Article']['title']);
6762
		$this->assertEquals('', $result[0]['Comment'][0]['comment']);
6763
		$this->assertEquals('', $result[0]['Comment'][1]['comment']);
6764
 
6765
		$fieldList = array(
6766
			'Article' => array('id'),
6767
			'Comment' => array('user_id')
6768
		);
6769
		$TestModel->saveAll($data, array('fieldList' => $fieldList));
6770
		$result = $TestModel->find('all');
6771
 
6772
		$this->assertEquals('', $result[1]['Article']['title']);
6773
		$this->assertEquals(2, count($result[1]['Comment']));
6774
 
6775
		$TestModel->whitelist = array('id');
6776
		$TestModel->Comment->whitelist = array('user_id');
6777
		$TestModel->saveAll($data);
6778
		$result = $TestModel->find('all');
6779
 
6780
		$this->assertEquals('', $result[2]['Article']['title']);
6781
		$this->assertEquals(2, count($result[2]['Comment']));
6782
	}
6783
 
6784
/**
6785
 * testSaveAllFieldListHasOne method
6786
 *
6787
 * @return void
6788
 */
6789
	public function testSaveAllFieldListHasOne() {
6790
		$this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
6791
		$TestModel = new Comment();
6792
 
6793
		$TestModel->validate = array('comment' => 'notEmpty');
6794
		$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
6795
 
6796
		$record = array(
6797
			'Comment' => array(
6798
				'user_id' => 1,
6799
				'article_id' => 1,
6800
				'comment' => '',
6801
			),
6802
			'Attachment' => array(
6803
				'attachment' => ''
6804
			)
6805
		);
6806
		$result = $TestModel->saveAll($record, array('validate' => 'only'));
6807
		$this->assertFalse($result);
6808
 
6809
		$fieldList = array(
6810
			'Comment' => array('id', 'article_id', 'user_id'),
6811
			'Attachment' => array('comment_id')
6812
		);
6813
		$result = $TestModel->saveAll($record, array(
6814
			'fieldList' => $fieldList, 'validate' => 'only'
6815
		));
6816
		$this->assertTrue($result);
6817
		$this->assertEmpty($TestModel->validationErrors);
6818
	}
6819
 
6820
/**
6821
 * testSaveAllFieldListHasOneAddFkToWhitelist method
6822
 *
6823
 * @return void
6824
 */
6825
	public function testSaveAllFieldListHasOneAddFkToWhitelist() {
6826
		$this->loadFixtures('ArticleFeatured', 'Featured');
6827
		$Article = new ArticleFeatured();
6828
		$Article->belongsTo = $Article->hasMany = array();
6829
		$Article->Featured->validate = array('end_date' => 'notEmpty');
6830
 
6831
		$record = array(
6832
			'ArticleFeatured' => array(
6833
				'user_id' => 1,
6834
				'title' => 'First Article',
6835
				'body' => '',
6836
				'published' => 'Y'
6837
			),
6838
			'Featured' => array(
6839
				'category_id' => 1,
6840
				'end_date' => ''
6841
			)
6842
		);
6843
		$result = $Article->saveAll($record, array('validate' => 'only'));
6844
		$this->assertFalse($result);
6845
		$expected = array(
6846
			'body' => array(
6847
				'This field cannot be left blank'
6848
			),
6849
			'Featured' => array(
6850
				'end_date' => array(
6851
					'This field cannot be left blank'
6852
				)
6853
			)
6854
		);
6855
		$this->assertEquals($expected, $Article->validationErrors);
6856
 
6857
		$fieldList = array(
6858
			'ArticleFeatured' => array('user_id', 'title'),
6859
			'Featured' => array('category_id')
6860
		);
6861
 
6862
		$result = $Article->saveAll($record, array(
6863
			'fieldList' => $fieldList, 'validate' => 'first'
6864
		));
6865
		$this->assertTrue($result);
6866
		$this->assertEmpty($Article->validationErrors);
6867
 
6868
		$Article->recursive = 0;
6869
		$result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
6870
		$this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
6871
	}
6872
 
6873
/**
6874
 * testSaveAllDeepFieldListValidateBelongsTo
6875
 *
6876
 * @return void
6877
 */
6878
	public function testSaveAllDeepFieldListValidateBelongsTo() {
6879
		$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
6880
		$TestModel = new Post();
6881
		$TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
6882
		$TestModel->recursive = 2;
6883
 
6884
		$result = $TestModel->find('all');
6885
		$this->assertEquals(3, count($result));
6886
		$this->assertFalse(isset($result[3]));
6887
 
6888
		// test belongsTo
6889
		$fieldList = array(
6890
			'Post' => array('title', 'author_id'),
6891
			'Author' => array('user'),
6892
			'Comment' => array('comment')
6893
		);
6894
		$TestModel->saveAll(array(
6895
			'Post' => array(
6896
				'title' => 'Post without body',
6897
				'body' => 'This will not be saved',
6898
			),
6899
			'Author' => array(
6900
				'user' => 'bob',
6901
				'test' => 'This will not be saved',
6902
				'Comment' => array(
6903
					array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
6904
 
6905
		)), array('fieldList' => $fieldList, 'deep' => true));
6906
 
6907
		$result = $TestModel->Author->Comment->find('first', array(
6908
			'conditions' => array('Comment.id' => 5),
6909
			'fields' => array('comment', 'published')
6910
		));
6911
		$expected = array(
6912
			'Comment' => array(
6913
				'comment' => 'I am still published',
6914
				'published' => 'Y'
6915
			)
6916
		);
6917
		$this->assertEquals($expected, $result);
6918
	}
6919
 
6920
/**
6921
 * testSaveAllDeepFieldListHasMany method
6922
 *
6923
 * return @void
6924
 */
6925
	public function testSaveAllDeepFieldListHasMany() {
6926
		$this->loadFixtures('Article', 'Comment', 'User');
6927
		$TestModel = new Article();
6928
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
6929
 
6930
		$this->db->truncate($TestModel);
6931
		$this->db->truncate(new Comment());
6932
 
6933
		$fieldList = array(
6934
			'Article' => array('id'),
6935
			'Comment' => array('article_id', 'user_id'),
6936
			'User' => array('user')
6937
		);
6938
 
6939
		$result = $TestModel->saveAll(array(
6940
			'Article' => array('id' => 2, 'title' => 'I will not save'),
6941
			'Comment' => array(
6942
				array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
6943
				array(
6944
					'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
6945
					'User' => array('user' => 'nopassword', 'password' => 'not saved')
6946
				)
6947
			)
6948
		), array('fieldList' => $fieldList, 'deep' => true));
6949
 
6950
		$result = $TestModel->Comment->User->find('first', array(
6951
			'conditions' => array('User.user' => 'nopassword'),
6952
			'fields' => array('user', 'password')
6953
		));
6954
		$expected = array(
6955
			'User' => array(
6956
				'user' => 'nopassword',
6957
				'password' => ''
6958
			)
6959
		);
6960
		$this->assertEquals($expected, $result);
6961
	}
6962
 
6963
/**
6964
 * testSaveAllDeepHasManyBelongsTo method
6965
 *
6966
 * return @void
6967
 */
6968
	public function testSaveAllDeepHasManyBelongsTo() {
6969
		$this->loadFixtures('Article', 'Comment', 'User');
6970
		$TestModel = new Article();
6971
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
6972
 
6973
		$this->db->truncate($TestModel);
6974
		$this->db->truncate(new Comment());
6975
 
6976
		$result = $TestModel->saveAll(array(
6977
			'Article' => array('id' => 2, 'title' => 'The title'),
6978
			'Comment' => array(
6979
				array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
6980
				array(
6981
					'comment' => 'belongsto', 'published' => 'Y',
6982
					'User' => array('user' => 'findme', 'password' => 'somepass')
6983
				)
6984
			)
6985
		), array('deep' => true));
6986
 
6987
		$result = $TestModel->Comment->User->find('first', array(
6988
			'conditions' => array('User.user' => 'findme'),
6989
			'fields' => array('id', 'user', 'password')
6990
		));
6991
		$expected = array(
6992
			'User' => array(
6993
				'id' => 5,
6994
				'user' => 'findme',
6995
				'password' => 'somepass',
6996
			)
6997
		);
6998
		$this->assertEquals($expected, $result);
6999
 
7000
		$result = $TestModel->Comment->find('first', array(
7001
			'conditions' => array('Comment.user_id' => 5),
7002
			'fields' => array('id', 'comment', 'published', 'user_id')
7003
		));
7004
		$expected = array(
7005
			'Comment' => array(
7006
				'id' => 2,
7007
				'comment' => 'belongsto',
7008
				'published' => 'Y',
7009
				'user_id' => 5
7010
			)
7011
		);
7012
		$this->assertEquals($expected, $result);
7013
	}
7014
 
7015
/**
7016
 * testSaveAllDeepHasManyhasMany method
7017
 *
7018
 * return @void
7019
 */
7020
	public function testSaveAllDeepHasManyHasMany() {
7021
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
7022
		$TestModel = new Article();
7023
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
7024
		$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
7025
		$TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
7026
 
7027
		$this->db->truncate($TestModel);
7028
		$this->db->truncate(new Comment());
7029
		$this->db->truncate(new Attachment());
7030
 
7031
		$result = $TestModel->saveAll(array(
7032
			'Article' => array('id' => 2, 'title' => 'The title'),
7033
			'Comment' => array(
7034
				array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
7035
				array(
7036
					'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
7037
					'Attachment' => array(
7038
						array('attachment' => 'first deep attachment'),
7039
						array('attachment' => 'second deep attachment'),
7040
					)
7041
				)
7042
			)
7043
		), array('deep' => true));
7044
 
7045
		$result = $TestModel->Comment->find('first', array(
7046
			'conditions' => array('Comment.comment' => 'hasmany'),
7047
			'fields' => array('id', 'comment', 'published', 'user_id'),
7048
			'recursive' => -1
7049
		));
7050
		$expected = array(
7051
			'Comment' => array(
7052
				'id' => 2,
7053
				'comment' => 'hasmany',
7054
				'published' => 'Y',
7055
				'user_id' => 5
7056
			)
7057
		);
7058
		$this->assertEquals($expected, $result);
7059
 
7060
		$result = $TestModel->Comment->Attachment->find('all', array(
7061
			'fields' => array('attachment', 'comment_id'),
7062
			'order' => array('Attachment.id' => 'ASC')
7063
		));
7064
		$expected = array(
7065
			array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
7066
			array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
7067
		);
7068
		$this->assertEquals($expected, $result);
7069
	}
7070
 
7071
/**
7072
 * testSaveAllDeepOrderHasManyHasMany method
7073
 *
7074
 * return @void
7075
 */
7076
	public function testSaveAllDeepOrderHasManyHasMany() {
7077
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
7078
		$TestModel = new Article();
7079
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
7080
		$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
7081
		$TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
7082
 
7083
		$this->db->truncate($TestModel);
7084
		$this->db->truncate(new Comment());
7085
		$this->db->truncate(new Attachment());
7086
 
7087
		$result = $TestModel->saveAll(array(
7088
			'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
7089
			'Comment' => array(
7090
				array(
7091
					'Attachment' => array(
7092
						array('attachment' => 'attachment should be created with comment_id'),
7093
						array('attachment' => 'comment should be created with article_id'),
7094
					),
7095
					'comment' => 'after associated data',
7096
					'user_id' => 1
7097
				)
7098
			)
7099
		), array('deep' => true));
7100
		$result = $TestModel->Comment->find('first', array(
7101
			'conditions' => array('Comment.article_id' => 2),
7102
		));
7103
 
7104
		$this->assertEquals(2, $result['Comment']['article_id']);
7105
		$this->assertEquals(2, count($result['Attachment']));
7106
	}
7107
 
7108
/**
7109
 * testSaveAllDeepEmptyHasManyHasMany method
7110
 *
7111
 * return @void
7112
 */
7113
	public function testSaveAllDeepEmptyHasManyHasMany() {
7114
		$this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
7115
 
7116
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
7117
		$TestModel = new Article();
7118
		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
7119
		$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
7120
		$TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
7121
 
7122
		$this->db->truncate($TestModel);
7123
		$this->db->truncate(new Comment());
7124
		$this->db->truncate(new Attachment());
7125
 
7126
		$result = $TestModel->saveAll(array(
7127
			'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
7128
			'Comment' => array(
7129
				array(
7130
					'user_id' => 1,
7131
					'Attachment' => array(
7132
						array('attachment' => 'attachment should be created with comment_id'),
7133
						array('attachment' => 'comment should be created with article_id'),
7134
					),
7135
				)
7136
			)
7137
		), array('deep' => true));
7138
		$result = $TestModel->Comment->find('first', array(
7139
			'conditions' => array('Comment.article_id' => 3),
7140
		));
7141
 
7142
		$this->assertEquals(3, $result['Comment']['article_id']);
7143
		$this->assertEquals(2, count($result['Attachment']));
7144
	}
7145
 
7146
/**
7147
 * testUpdateAllBoolean
7148
 *
7149
 * return @void
7150
 */
7151
	public function testUpdateAllBoolean() {
7152
		$this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
7153
		$TestModel = new Item();
7154
		$result = $TestModel->updateAll(array('published' => true));
7155
		$this->assertTrue($result);
7156
 
7157
		$result = $TestModel->find('first', array('fields' => array('id', 'published')));
7158
		$this->assertEquals(true, $result['Item']['published']);
7159
	}
7160
 
7161
/**
7162
 * testUpdateAllBooleanConditions
7163
 *
7164
 * return @void
7165
 */
7166
	public function testUpdateAllBooleanConditions() {
7167
		$this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
7168
		$TestModel = new Item();
7169
 
7170
		$result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
7171
		$this->assertTrue($result);
7172
		$result = $TestModel->find('first', array(
7173
			'fields' => array('id', 'published'),
7174
			'conditions' => array('Item.id' => 1)));
7175
		$this->assertEquals(true, $result['Item']['published']);
7176
	}
7177
 
7178
/**
7179
 * testUpdateBoolean
7180
 *
7181
 * return @void
7182
 */
7183
	public function testUpdateBoolean() {
7184
		$this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
7185
		$TestModel = new Item();
7186
 
7187
		$result = $TestModel->save(array('published' => true, 'id' => 1));
7188
		$this->assertTrue((bool)$result);
7189
		$result = $TestModel->find('first', array(
7190
			'fields' => array('id', 'published'),
7191
			'conditions' => array('Item.id' => 1)));
7192
		$this->assertEquals(true, $result['Item']['published']);
7193
	}
7194
 
7195
/**
7196
 * Test the clear() method.
7197
 *
7198
 * @return void
7199
 */
7200
	public function testClear() {
7201
		$this->loadFixtures('Bid');
7202
		$model = ClassRegistry::init('Bid');
7203
		$model->set(array('name' => 'Testing', 'message_id' => 3));
7204
		$this->assertTrue(isset($model->data['Bid']['name']));
7205
		$this->assertTrue($model->clear());
7206
		$this->assertFalse(isset($model->data['Bid']['name']));
7207
		$this->assertFalse(isset($model->data['Bid']['message_id']));
7208
	}
7209
}