Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/model/model_write.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /* SVN FILE: $Id: model.test.php 8225 2009-07-08 03:25:30Z mark_story $ */ | |
3 | |
4 /** | |
5 * ModelWriteTest file | |
6 * | |
7 * PHP versions 4 and 5 | |
8 * | |
9 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
10 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
11 * | |
12 * Licensed under The Open Group Test Suite License | |
13 * Redistributions of files must retain the above copyright notice. | |
14 * | |
15 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
16 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
17 * @package cake | |
18 * @subpackage cake.tests.cases.libs.model | |
19 * @since CakePHP(tm) v 1.2.0.4206 | |
20 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
21 */ | |
22 require_once dirname(__FILE__) . DS . 'model.test.php'; | |
23 /** | |
24 * ModelWriteTest | |
25 * | |
26 * @package cake | |
27 * @subpackage cake.tests.cases.libs.model.operations | |
28 */ | |
29 class ModelWriteTest extends BaseModelTest { | |
30 | |
31 /** | |
32 * testInsertAnotherHabtmRecordWithSameForeignKey method | |
33 * | |
34 * @access public | |
35 * @return void | |
36 */ | |
37 function testInsertAnotherHabtmRecordWithSameForeignKey() { | |
38 $this->loadFixtures('JoinA', 'JoinB', 'JoinAB'); | |
39 $TestModel = new JoinA(); | |
40 | |
41 $result = $TestModel->JoinAsJoinB->findById(1); | |
42 $expected = array( | |
43 'JoinAsJoinB' => array( | |
44 'id' => 1, | |
45 'join_a_id' => 1, | |
46 'join_b_id' => 2, | |
47 'other' => 'Data for Join A 1 Join B 2', | |
48 'created' => '2008-01-03 10:56:33', | |
49 'updated' => '2008-01-03 10:56:33' | |
50 )); | |
51 $this->assertEqual($result, $expected); | |
52 | |
53 $TestModel->JoinAsJoinB->create(); | |
54 $result = $TestModel->JoinAsJoinB->save(array( | |
55 'join_a_id' => 1, | |
56 'join_b_id' => 1, | |
57 'other' => 'Data for Join A 1 Join B 1', | |
58 'created' => '2008-01-03 10:56:44', | |
59 'updated' => '2008-01-03 10:56:44' | |
60 )); | |
61 $this->assertTrue($result); | |
62 $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID(); | |
63 $this->assertTrue($lastInsertId != null); | |
64 | |
65 $result = $TestModel->JoinAsJoinB->findById(1); | |
66 $expected = array( | |
67 'JoinAsJoinB' => array( | |
68 'id' => 1, | |
69 'join_a_id' => 1, | |
70 'join_b_id' => 2, | |
71 'other' => 'Data for Join A 1 Join B 2', | |
72 'created' => '2008-01-03 10:56:33', | |
73 'updated' => '2008-01-03 10:56:33' | |
74 )); | |
75 $this->assertEqual($result, $expected); | |
76 | |
77 $updatedValue = 'UPDATED Data for Join A 1 Join B 2'; | |
78 $TestModel->JoinAsJoinB->id = 1; | |
79 $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false); | |
80 $this->assertTrue($result); | |
81 | |
82 $result = $TestModel->JoinAsJoinB->findById(1); | |
83 $this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue); | |
84 } | |
85 | |
86 /** | |
87 * testSaveDateAsFirstEntry method | |
88 * | |
89 * @access public | |
90 * @return void | |
91 */ | |
92 function testSaveDateAsFirstEntry() { | |
93 $this->loadFixtures('Article'); | |
94 | |
95 $Article =& new Article(); | |
96 | |
97 $data = array( | |
98 'Article' => array( | |
99 'created' => array( | |
100 'day' => '1', | |
101 'month' => '1', | |
102 'year' => '2008' | |
103 ), | |
104 'title' => 'Test Title', | |
105 'user_id' => 1 | |
106 )); | |
107 $Article->create(); | |
108 $this->assertTrue($Article->save($data)); | |
109 | |
110 $testResult = $Article->find(array('Article.title' => 'Test Title')); | |
111 | |
112 $this->assertEqual($testResult['Article']['title'], $data['Article']['title']); | |
113 $this->assertEqual($testResult['Article']['created'], '2008-01-01 00:00:00'); | |
114 | |
115 } | |
116 | |
117 /** | |
118 * testUnderscoreFieldSave method | |
119 * | |
120 * @access public | |
121 * @return void | |
122 */ | |
123 function testUnderscoreFieldSave() { | |
124 $this->loadFixtures('UnderscoreField'); | |
125 $UnderscoreField =& new UnderscoreField(); | |
126 | |
127 $currentCount = $UnderscoreField->find('count'); | |
128 $this->assertEqual($currentCount, 3); | |
129 $data = array('UnderscoreField' => array( | |
130 'user_id' => '1', | |
131 'my_model_has_a_field' => 'Content here', | |
132 'body' => 'Body', | |
133 'published' => 'Y', | |
134 'another_field' => 4 | |
135 )); | |
136 $ret = $UnderscoreField->save($data); | |
137 $this->assertTrue($ret); | |
138 | |
139 $currentCount = $UnderscoreField->find('count'); | |
140 $this->assertEqual($currentCount, 4); | |
141 } | |
142 | |
143 /** | |
144 * testAutoSaveUuid method | |
145 * | |
146 * @access public | |
147 * @return void | |
148 */ | |
149 function testAutoSaveUuid() { | |
150 // SQLite does not support non-integer primary keys | |
151 $this->skipIf($this->db->config['driver'] == 'sqlite'); | |
152 | |
153 $this->loadFixtures('Uuid'); | |
154 $TestModel =& new Uuid(); | |
155 | |
156 $TestModel->save(array('title' => 'Test record')); | |
157 $result = $TestModel->findByTitle('Test record'); | |
158 $this->assertEqual( | |
159 array_keys($result['Uuid']), | |
160 array('id', 'title', 'count', 'created', 'updated') | |
161 ); | |
162 $this->assertEqual(strlen($result['Uuid']['id']), 36); | |
163 } | |
164 | |
165 /** | |
166 * Ensure that if the id key is null but present the save doesn't fail (with an | |
167 * x sql error: "Column id specified twice") | |
168 * | |
169 * @return void | |
170 * @access public | |
171 */ | |
172 function testSaveUuidNull() { | |
173 // SQLite does not support non-integer primary keys | |
174 $this->skipIf($this->db->config['driver'] == 'sqlite'); | |
175 | |
176 $this->loadFixtures('Uuid'); | |
177 $TestModel =& new Uuid(); | |
178 | |
179 $TestModel->save(array('title' => 'Test record', 'id' => null)); | |
180 $result = $TestModel->findByTitle('Test record'); | |
181 $this->assertEqual( | |
182 array_keys($result['Uuid']), | |
183 array('id', 'title', 'count', 'created', 'updated') | |
184 ); | |
185 $this->assertEqual(strlen($result['Uuid']['id']), 36); | |
186 } | |
187 | |
188 /** | |
189 * testZeroDefaultFieldValue method | |
190 * | |
191 * @access public | |
192 * @return void | |
193 */ | |
194 function testZeroDefaultFieldValue() { | |
195 $this->skipIf( | |
196 $this->db->config['driver'] == 'sqlite', | |
197 '%s SQLite uses loose typing, this operation is unsupported' | |
198 ); | |
199 $this->loadFixtures('DataTest'); | |
200 $TestModel =& new DataTest(); | |
201 | |
202 $TestModel->create(array()); | |
203 $TestModel->save(); | |
204 $result = $TestModel->findById($TestModel->id); | |
205 $this->assertIdentical($result['DataTest']['count'], '0'); | |
206 $this->assertIdentical($result['DataTest']['float'], '0'); | |
207 } | |
208 | |
209 /** | |
210 * testNonNumericHabtmJoinKey method | |
211 * | |
212 * @access public | |
213 * @return void | |
214 */ | |
215 function testNonNumericHabtmJoinKey() { | |
216 $this->loadFixtures('Post', 'Tag', 'PostsTag'); | |
217 $Post =& new Post(); | |
218 $Post->bindModel(array( | |
219 'hasAndBelongsToMany' => array('Tag') | |
220 )); | |
221 $Post->Tag->primaryKey = 'tag'; | |
222 | |
223 $result = $Post->find('all'); | |
224 $expected = array( | |
225 array( | |
226 'Post' => array( | |
227 'id' => '1', | |
228 'author_id' => '1', | |
229 'title' => 'First Post', | |
230 'body' => 'First Post Body', | |
231 'published' => 'Y', | |
232 'created' => '2007-03-18 10:39:23', | |
233 'updated' => '2007-03-18 10:41:31' | |
234 ), | |
235 'Author' => array( | |
236 'id' => null, | |
237 'user' => null, | |
238 'password' => null, | |
239 'created' => null, | |
240 'updated' => null, | |
241 'test' => 'working' | |
242 ), | |
243 'Tag' => array( | |
244 array( | |
245 'id' => '1', | |
246 'tag' => 'tag1', | |
247 'created' => '2007-03-18 12:22:23', | |
248 'updated' => '2007-03-18 12:24:31' | |
249 ), | |
250 array( | |
251 'id' => '2', | |
252 'tag' => 'tag2', | |
253 'created' => '2007-03-18 12:24:23', | |
254 'updated' => '2007-03-18 12:26:31' | |
255 ))), | |
256 array( | |
257 'Post' => array( | |
258 'id' => '2', | |
259 'author_id' => '3', | |
260 'title' => 'Second Post', | |
261 'body' => 'Second Post Body', | |
262 'published' => 'Y', | |
263 'created' => '2007-03-18 10:41:23', | |
264 'updated' => '2007-03-18 10:43:31' | |
265 ), | |
266 'Author' => array( | |
267 'id' => null, | |
268 'user' => null, | |
269 'password' => null, | |
270 'created' => null, | |
271 'updated' => null, | |
272 'test' => 'working' | |
273 ), | |
274 'Tag' => array( | |
275 array( | |
276 'id' => '1', | |
277 'tag' => 'tag1', | |
278 'created' => '2007-03-18 12:22:23', | |
279 'updated' => '2007-03-18 12:24:31' | |
280 ), | |
281 array( | |
282 'id' => '3', | |
283 'tag' => 'tag3', | |
284 'created' => '2007-03-18 12:26:23', | |
285 'updated' => '2007-03-18 12:28:31' | |
286 ))), | |
287 array( | |
288 'Post' => array( | |
289 'id' => '3', | |
290 'author_id' => '1', | |
291 'title' => 'Third Post', | |
292 'body' => 'Third Post Body', | |
293 'published' => 'Y', | |
294 'created' => '2007-03-18 10:43:23', | |
295 'updated' => '2007-03-18 10:45:31' | |
296 ), | |
297 'Author' => array( | |
298 'id' => null, | |
299 'user' => null, | |
300 'password' => null, | |
301 'created' => null, | |
302 'updated' => null, | |
303 'test' => 'working' | |
304 ), | |
305 'Tag' => array() | |
306 )); | |
307 $this->assertEqual($result, $expected); | |
308 } | |
309 | |
310 /** | |
311 * Tests validation parameter order in custom validation methods | |
312 * | |
313 * @access public | |
314 * @return void | |
315 */ | |
316 function testAllowSimulatedFields() { | |
317 $TestModel =& new ValidationTest1(); | |
318 | |
319 $TestModel->create(array( | |
320 'title' => 'foo', | |
321 'bar' => 'baz' | |
322 )); | |
323 $expected = array( | |
324 'ValidationTest1' => array( | |
325 'title' => 'foo', | |
326 'bar' => 'baz' | |
327 )); | |
328 $this->assertEqual($TestModel->data, $expected); | |
329 } | |
330 | |
331 /** | |
332 * test that Caches are getting cleared on save(). | |
333 * ensure that both inflections of controller names are getting cleared | |
334 * as url for controller could be either overallFavorites/index or overall_favorites/index | |
335 * | |
336 * @return void | |
337 */ | |
338 function testCacheClearOnSave() { | |
339 $_back = array( | |
340 'check' => Configure::read('Cache.check'), | |
341 'disable' => Configure::read('Cache.disable'), | |
342 ); | |
343 Configure::write('Cache.check', true); | |
344 Configure::write('Cache.disable', false); | |
345 | |
346 $this->loadFixtures('OverallFavorite'); | |
347 $OverallFavorite =& new OverallFavorite(); | |
348 | |
349 touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'); | |
350 touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'); | |
351 | |
352 $data = array( | |
353 'OverallFavorite' => array( | |
354 'id' => 22, | |
355 'model_type' => '8-track', | |
356 'model_id' => '3', | |
357 'priority' => '1' | |
358 ) | |
359 ); | |
360 $OverallFavorite->create($data); | |
361 $OverallFavorite->save(); | |
362 | |
363 $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php')); | |
364 $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php')); | |
365 | |
366 Configure::write('Cache.check', $_back['check']); | |
367 Configure::write('Cache.disable', $_back['disable']); | |
368 } | |
369 | |
370 /** | |
371 * testSaveWithCounterCache method | |
372 * | |
373 * @access public | |
374 * @return void | |
375 */ | |
376 function testSaveWithCounterCache() { | |
377 $this->loadFixtures('Syfile', 'Item'); | |
378 $TestModel =& new Syfile(); | |
379 $TestModel2 =& new Item(); | |
380 | |
381 $result = $TestModel->findById(1); | |
382 $this->assertIdentical($result['Syfile']['item_count'], null); | |
383 | |
384 $TestModel2->save(array( | |
385 'name' => 'Item 7', | |
386 'syfile_id' => 1, | |
387 'published' => false | |
388 )); | |
389 | |
390 $result = $TestModel->findById(1); | |
391 $this->assertIdentical($result['Syfile']['item_count'], '2'); | |
392 | |
393 $TestModel2->delete(1); | |
394 $result = $TestModel->findById(1); | |
395 $this->assertIdentical($result['Syfile']['item_count'], '1'); | |
396 | |
397 $TestModel2->id = 2; | |
398 $TestModel2->saveField('syfile_id', 1); | |
399 | |
400 $result = $TestModel->findById(1); | |
401 $this->assertIdentical($result['Syfile']['item_count'], '2'); | |
402 | |
403 $result = $TestModel->findById(2); | |
404 $this->assertIdentical($result['Syfile']['item_count'], '0'); | |
405 } | |
406 | |
407 /** | |
408 * Tests that counter caches are updated when records are added | |
409 * | |
410 * @access public | |
411 * @return void | |
412 */ | |
413 function testCounterCacheIncrease() { | |
414 $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); | |
415 $User = new CounterCacheUser(); | |
416 $Post = new CounterCachePost(); | |
417 $data = array('Post' => array( | |
418 'id' => 22, | |
419 'title' => 'New Post', | |
420 'user_id' => 66 | |
421 )); | |
422 | |
423 $Post->save($data); | |
424 $user = $User->find('first', array( | |
425 'conditions' => array('id' => 66), | |
426 'recursive' => -1 | |
427 )); | |
428 | |
429 $result = $user[$User->alias]['post_count']; | |
430 $expected = 3; | |
431 $this->assertEqual($result, $expected); | |
432 } | |
433 | |
434 /** | |
435 * Tests that counter caches are updated when records are deleted | |
436 * | |
437 * @access public | |
438 * @return void | |
439 */ | |
440 function testCounterCacheDecrease() { | |
441 $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); | |
442 $User = new CounterCacheUser(); | |
443 $Post = new CounterCachePost(); | |
444 | |
445 $Post->delete(2); | |
446 $user = $User->find('first', array( | |
447 'conditions' => array('id' => 66), | |
448 'recursive' => -1 | |
449 )); | |
450 | |
451 $result = $user[$User->alias]['post_count']; | |
452 $expected = 1; | |
453 $this->assertEqual($result, $expected); | |
454 } | |
455 | |
456 /** | |
457 * Tests that counter caches are updated when foreign keys of counted records change | |
458 * | |
459 * @access public | |
460 * @return void | |
461 */ | |
462 function testCounterCacheUpdated() { | |
463 $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); | |
464 $User = new CounterCacheUser(); | |
465 $Post = new CounterCachePost(); | |
466 | |
467 $data = $Post->find('first', array( | |
468 'conditions' => array('id' => 1), | |
469 'recursive' => -1 | |
470 )); | |
471 $data[$Post->alias]['user_id'] = 301; | |
472 $Post->save($data); | |
473 | |
474 $users = $User->find('all',array('order' => 'User.id')); | |
475 $this->assertEqual($users[0]['User']['post_count'], 1); | |
476 $this->assertEqual($users[1]['User']['post_count'], 2); | |
477 } | |
478 | |
479 /** | |
480 * Test counter cache with models that use a non-standard (i.e. not using 'id') | |
481 * as their primary key. | |
482 * | |
483 * @access public | |
484 * @return void | |
485 */ | |
486 function testCounterCacheWithNonstandardPrimaryKey() { | |
487 $this->loadFixtures( | |
488 'CounterCacheUserNonstandardPrimaryKey', | |
489 'CounterCachePostNonstandardPrimaryKey' | |
490 ); | |
491 | |
492 $User = new CounterCacheUserNonstandardPrimaryKey(); | |
493 $Post = new CounterCachePostNonstandardPrimaryKey(); | |
494 | |
495 $data = $Post->find('first', array( | |
496 'conditions' => array('pid' => 1), | |
497 'recursive' => -1 | |
498 )); | |
499 $data[$Post->alias]['uid'] = 301; | |
500 $Post->save($data); | |
501 | |
502 $users = $User->find('all',array('order' => 'User.uid')); | |
503 $this->assertEqual($users[0]['User']['post_count'], 1); | |
504 $this->assertEqual($users[1]['User']['post_count'], 2); | |
505 } | |
506 | |
507 /** | |
508 * test Counter Cache With Self Joining table | |
509 * | |
510 * @return void | |
511 * @access public | |
512 */ | |
513 function testCounterCacheWithSelfJoin() { | |
514 $skip = $this->skipIf( | |
515 ($this->db->config['driver'] == 'sqlite'), | |
516 'SQLite 2.x does not support ALTER TABLE ADD COLUMN' | |
517 ); | |
518 if ($skip) { | |
519 return; | |
520 } | |
521 | |
522 $this->loadFixtures('CategoryThread'); | |
523 $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . " ADD COLUMN child_count INTEGER"); | |
524 $Category =& new CategoryThread(); | |
525 $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); | |
526 $this->assertTrue($result); | |
527 | |
528 $Category =& new CategoryThread(); | |
529 $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; | |
530 $Category->updateCounterCache(array('parent_id' => 5)); | |
531 $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count'); | |
532 $expected = array_fill(0, 1, 1); | |
533 $this->assertEqual($result, $expected); | |
534 } | |
535 | |
536 /** | |
537 * testSaveWithCounterCacheScope method | |
538 * | |
539 * @access public | |
540 * @return void | |
541 */ | |
542 function testSaveWithCounterCacheScope() { | |
543 $this->loadFixtures('Syfile', 'Item'); | |
544 $TestModel =& new Syfile(); | |
545 $TestModel2 =& new Item(); | |
546 $TestModel2->belongsTo['Syfile']['counterCache'] = true; | |
547 $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true); | |
548 | |
549 $result = $TestModel->findById(1); | |
550 $this->assertIdentical($result['Syfile']['item_count'], null); | |
551 | |
552 $TestModel2->save(array( | |
553 'name' => 'Item 7', | |
554 'syfile_id' => 1, | |
555 'published'=> true | |
556 )); | |
557 | |
558 $result = $TestModel->findById(1); | |
559 $this->assertIdentical($result['Syfile']['item_count'], '1'); | |
560 | |
561 $TestModel2->id = 1; | |
562 $TestModel2->saveField('published', true); | |
563 $result = $TestModel->findById(1); | |
564 $this->assertIdentical($result['Syfile']['item_count'], '2'); | |
565 | |
566 $TestModel2->save(array( | |
567 'id' => 1, | |
568 'syfile_id' => 1, | |
569 'published'=> false | |
570 )); | |
571 | |
572 $result = $TestModel->findById(1); | |
573 $this->assertIdentical($result['Syfile']['item_count'], '1'); | |
574 } | |
575 | |
576 /** | |
577 * test that beforeValidate returning false can abort saves. | |
578 * | |
579 * @return void | |
580 */ | |
581 function testBeforeValidateSaveAbortion() { | |
582 $Model =& new CallbackPostTestModel(); | |
583 $Model->beforeValidateReturn = false; | |
584 | |
585 $data = array( | |
586 'title' => 'new article', | |
587 'body' => 'this is some text.' | |
588 ); | |
589 $Model->create(); | |
590 $result = $Model->save($data); | |
591 $this->assertFalse($result); | |
592 } | |
593 /** | |
594 * test that beforeSave returning false can abort saves. | |
595 * | |
596 * @return void | |
597 */ | |
598 function testBeforeSaveSaveAbortion() { | |
599 $Model =& new CallbackPostTestModel(); | |
600 $Model->beforeSaveReturn = false; | |
601 | |
602 $data = array( | |
603 'title' => 'new article', | |
604 'body' => 'this is some text.' | |
605 ); | |
606 $Model->create(); | |
607 $result = $Model->save($data); | |
608 $this->assertFalse($result); | |
609 } | |
610 | |
611 /** | |
612 * testSaveField method | |
613 * | |
614 * @access public | |
615 * @return void | |
616 */ | |
617 function testSaveField() { | |
618 $this->loadFixtures('Article'); | |
619 $TestModel =& new Article(); | |
620 | |
621 $TestModel->id = 1; | |
622 $result = $TestModel->saveField('title', 'New First Article'); | |
623 $this->assertTrue($result); | |
624 | |
625 $TestModel->recursive = -1; | |
626 $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); | |
627 $expected = array('Article' => array( | |
628 'id' => '1', | |
629 'user_id' => '1', | |
630 'title' => 'New First Article', | |
631 'body' => 'First Article Body' | |
632 )); | |
633 $this->assertEqual($result, $expected); | |
634 | |
635 $TestModel->id = 1; | |
636 $result = $TestModel->saveField('title', ''); | |
637 $this->assertTrue($result); | |
638 | |
639 $TestModel->recursive = -1; | |
640 $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); | |
641 $expected = array('Article' => array( | |
642 'id' => '1', | |
643 'user_id' => '1', | |
644 'title' => '', | |
645 'body' => 'First Article Body' | |
646 )); | |
647 $result['Article']['title'] = trim($result['Article']['title']); | |
648 $this->assertEqual($result, $expected); | |
649 | |
650 $TestModel->id = 1; | |
651 $TestModel->set('body', 'Messed up data'); | |
652 $this->assertTrue($TestModel->saveField('title', 'First Article')); | |
653 $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); | |
654 $expected = array('Article' => array( | |
655 'id' => '1', | |
656 'user_id' => '1', | |
657 'title' => 'First Article', | |
658 'body' => 'First Article Body' | |
659 )); | |
660 $this->assertEqual($result, $expected); | |
661 | |
662 $TestModel->recursive = -1; | |
663 $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); | |
664 | |
665 $TestModel->id = 1; | |
666 $result = $TestModel->saveField('title', '', true); | |
667 $this->assertFalse($result); | |
668 | |
669 $this->loadFixtures('Node', 'Dependency'); | |
670 $Node =& new Node(); | |
671 $Node->set('id', 1); | |
672 $result = $Node->read(); | |
673 $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); | |
674 | |
675 $Node->saveField('state', 10); | |
676 $result = $Node->read(); | |
677 $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); | |
678 } | |
679 | |
680 /** | |
681 * testSaveWithCreate method | |
682 * | |
683 * @access public | |
684 * @return void | |
685 */ | |
686 function testSaveWithCreate() { | |
687 $this->loadFixtures( | |
688 'User', | |
689 'Article', | |
690 'User', | |
691 'Comment', | |
692 'Tag', | |
693 'ArticlesTag', | |
694 'Attachment' | |
695 ); | |
696 $TestModel =& new User(); | |
697 | |
698 $data = array('User' => array( | |
699 'user' => 'user', | |
700 'password' => '' | |
701 )); | |
702 $result = $TestModel->save($data); | |
703 $this->assertFalse($result); | |
704 $this->assertTrue(!empty($TestModel->validationErrors)); | |
705 | |
706 $TestModel =& new Article(); | |
707 | |
708 $data = array('Article' => array( | |
709 'user_id' => '', | |
710 'title' => '', | |
711 'body' => '' | |
712 )); | |
713 $result = $TestModel->create($data) && $TestModel->save(); | |
714 $this->assertFalse($result); | |
715 $this->assertTrue(!empty($TestModel->validationErrors)); | |
716 | |
717 $data = array('Article' => array( | |
718 'id' => 1, | |
719 'user_id' => '1', | |
720 'title' => 'New First Article', | |
721 'body' => '' | |
722 )); | |
723 $result = $TestModel->create($data) && $TestModel->save(); | |
724 $this->assertFalse($result); | |
725 | |
726 $data = array('Article' => array( | |
727 'id' => 1, | |
728 'title' => 'New First Article' | |
729 )); | |
730 $result = $TestModel->create() && $TestModel->save($data, false); | |
731 $this->assertTrue($result); | |
732 | |
733 $TestModel->recursive = -1; | |
734 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1); | |
735 $expected = array('Article' => array( | |
736 'id' => '1', | |
737 'user_id' => '1', | |
738 'title' => 'New First Article', | |
739 'body' => 'First Article Body', | |
740 'published' => 'N' | |
741 )); | |
742 $this->assertEqual($result, $expected); | |
743 | |
744 $data = array('Article' => array( | |
745 'id' => 1, | |
746 'user_id' => '2', | |
747 'title' => 'First Article', | |
748 'body' => 'New First Article Body', | |
749 'published' => 'Y' | |
750 )); | |
751 $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published')); | |
752 $this->assertTrue($result); | |
753 | |
754 $TestModel->recursive = -1; | |
755 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1); | |
756 $expected = array('Article' => array( | |
757 'id' => '1', | |
758 'user_id' => '1', | |
759 'title' => 'First Article', | |
760 'body' => 'First Article Body', | |
761 'published' => 'Y' | |
762 )); | |
763 $this->assertEqual($result, $expected); | |
764 | |
765 $data = array( | |
766 'Article' => array( | |
767 'user_id' => '2', | |
768 'title' => 'New Article', | |
769 'body' => 'New Article Body', | |
770 'created' => '2007-03-18 14:55:23', | |
771 'updated' => '2007-03-18 14:57:31' | |
772 ), | |
773 'Tag' => array('Tag' => array(1, 3)) | |
774 ); | |
775 $TestModel->create(); | |
776 $result = $TestModel->create() && $TestModel->save($data); | |
777 $this->assertTrue($result); | |
778 | |
779 $TestModel->recursive = 2; | |
780 $result = $TestModel->read(null, 4); | |
781 $expected = array( | |
782 'Article' => array( | |
783 'id' => '4', | |
784 'user_id' => '2', | |
785 'title' => 'New Article', | |
786 'body' => 'New Article Body', | |
787 'published' => 'N', | |
788 'created' => '2007-03-18 14:55:23', | |
789 'updated' => '2007-03-18 14:57:31' | |
790 ), | |
791 'User' => array( | |
792 'id' => '2', | |
793 'user' => 'nate', | |
794 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | |
795 'created' => '2007-03-17 01:18:23', | |
796 'updated' => '2007-03-17 01:20:31' | |
797 ), | |
798 'Comment' => array(), | |
799 'Tag' => array( | |
800 array( | |
801 'id' => '1', | |
802 'tag' => 'tag1', | |
803 'created' => '2007-03-18 12:22:23', | |
804 'updated' => '2007-03-18 12:24:31' | |
805 ), | |
806 array( | |
807 'id' => '3', | |
808 'tag' => 'tag3', | |
809 'created' => '2007-03-18 12:26:23', | |
810 'updated' => '2007-03-18 12:28:31' | |
811 ))); | |
812 $this->assertEqual($result, $expected); | |
813 | |
814 $data = array('Comment' => array( | |
815 'article_id' => '4', | |
816 'user_id' => '1', | |
817 'comment' => 'Comment New Article', | |
818 'published' => 'Y', | |
819 'created' => '2007-03-18 14:57:23', | |
820 'updated' => '2007-03-18 14:59:31' | |
821 )); | |
822 $result = $TestModel->Comment->create() && $TestModel->Comment->save($data); | |
823 $this->assertTrue($result); | |
824 | |
825 $data = array('Attachment' => array( | |
826 'comment_id' => '7', | |
827 'attachment' => 'newattachment.zip', | |
828 'created' => '2007-03-18 15:02:23', | |
829 'updated' => '2007-03-18 15:04:31' | |
830 )); | |
831 $result = $TestModel->Comment->Attachment->save($data); | |
832 $this->assertTrue($result); | |
833 | |
834 $TestModel->recursive = 2; | |
835 $result = $TestModel->read(null, 4); | |
836 $expected = array( | |
837 'Article' => array( | |
838 'id' => '4', | |
839 'user_id' => '2', | |
840 'title' => 'New Article', | |
841 'body' => 'New Article Body', | |
842 'published' => 'N', | |
843 'created' => '2007-03-18 14:55:23', | |
844 'updated' => '2007-03-18 14:57:31' | |
845 ), | |
846 'User' => array( | |
847 'id' => '2', | |
848 'user' => 'nate', | |
849 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | |
850 'created' => '2007-03-17 01:18:23', | |
851 'updated' => '2007-03-17 01:20:31' | |
852 ), | |
853 'Comment' => array( | |
854 array( | |
855 'id' => '7', | |
856 'article_id' => '4', | |
857 'user_id' => '1', | |
858 'comment' => 'Comment New Article', | |
859 'published' => 'Y', | |
860 'created' => '2007-03-18 14:57:23', | |
861 'updated' => '2007-03-18 14:59:31', | |
862 'Article' => array( | |
863 'id' => '4', | |
864 'user_id' => '2', | |
865 'title' => 'New Article', | |
866 'body' => 'New Article Body', | |
867 'published' => 'N', | |
868 'created' => '2007-03-18 14:55:23', | |
869 'updated' => '2007-03-18 14:57:31' | |
870 ), | |
871 'User' => array( | |
872 'id' => '1', | |
873 'user' => 'mariano', | |
874 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | |
875 'created' => '2007-03-17 01:16:23', | |
876 'updated' => '2007-03-17 01:18:31' | |
877 ), | |
878 'Attachment' => array( | |
879 'id' => '2', | |
880 'comment_id' => '7', | |
881 'attachment' => 'newattachment.zip', | |
882 'created' => '2007-03-18 15:02:23', | |
883 'updated' => '2007-03-18 15:04:31' | |
884 ))), | |
885 'Tag' => array( | |
886 array( | |
887 'id' => '1', | |
888 'tag' => 'tag1', | |
889 'created' => '2007-03-18 12:22:23', | |
890 'updated' => '2007-03-18 12:24:31' | |
891 ), | |
892 array( | |
893 'id' => '3', | |
894 'tag' => 'tag3', | |
895 'created' => '2007-03-18 12:26:23', | |
896 'updated' => '2007-03-18 12:28:31' | |
897 ))); | |
898 | |
899 $this->assertEqual($result, $expected); | |
900 } | |
901 | |
902 /** | |
903 * test that a null Id doesn't cause errors | |
904 * | |
905 * @return void | |
906 */ | |
907 function testSaveWithNullId() { | |
908 $this->loadFixtures('User'); | |
909 $User =& new User(); | |
910 $User->read(null, 1); | |
911 $User->data['User']['id'] = null; | |
912 $this->assertTrue($User->save(array('password' => 'test'))); | |
913 $this->assertTrue($User->id > 0); | |
914 | |
915 $result = $User->read(null, 2); | |
916 $User->data['User']['id'] = null; | |
917 $this->assertTrue($User->save(array('password' => 'test'))); | |
918 $this->assertTrue($User->id > 0); | |
919 | |
920 $User->data['User'] = array('password' => 'something'); | |
921 $this->assertTrue($User->save()); | |
922 $result = $User->read(); | |
923 $this->assertEqual($User->data['User']['password'], 'something'); | |
924 } | |
925 | |
926 /** | |
927 * testSaveWithSet method | |
928 * | |
929 * @access public | |
930 * @return void | |
931 */ | |
932 function testSaveWithSet() { | |
933 $this->loadFixtures('Article'); | |
934 $TestModel =& new Article(); | |
935 | |
936 // Create record we will be updating later | |
937 | |
938 $data = array('Article' => array( | |
939 'user_id' => '1', | |
940 'title' => 'Fourth Article', | |
941 'body' => 'Fourth Article Body', | |
942 'published' => 'Y' | |
943 )); | |
944 $result = $TestModel->create() && $TestModel->save($data); | |
945 $this->assertTrue($result); | |
946 | |
947 // Check record we created | |
948 | |
949 $TestModel->recursive = -1; | |
950 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); | |
951 $expected = array('Article' => array( | |
952 'id' => '4', | |
953 'user_id' => '1', | |
954 'title' => 'Fourth Article', | |
955 'body' => 'Fourth Article Body', | |
956 'published' => 'Y' | |
957 )); | |
958 $this->assertEqual($result, $expected); | |
959 | |
960 // Create new record just to overlap Model->id on previously created record | |
961 | |
962 $data = array('Article' => array( | |
963 'user_id' => '4', | |
964 'title' => 'Fifth Article', | |
965 'body' => 'Fifth Article Body', | |
966 'published' => 'Y' | |
967 )); | |
968 $result = $TestModel->create() && $TestModel->save($data); | |
969 $this->assertTrue($result); | |
970 | |
971 $TestModel->recursive = -1; | |
972 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); | |
973 $expected = array('Article' => array( | |
974 'id' => '5', | |
975 'user_id' => '4', | |
976 'title' => 'Fifth Article', | |
977 'body' => 'Fifth Article Body', | |
978 'published' => 'Y' | |
979 )); | |
980 $this->assertEqual($result, $expected); | |
981 | |
982 // Go back and edit the first article we created, starting by checking it's still there | |
983 | |
984 $TestModel->recursive = -1; | |
985 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); | |
986 $expected = array('Article' => array( | |
987 'id' => '4', | |
988 'user_id' => '1', | |
989 'title' => 'Fourth Article', | |
990 'body' => 'Fourth Article Body', | |
991 'published' => 'Y' | |
992 )); | |
993 $this->assertEqual($result, $expected); | |
994 | |
995 // And now do the update with set() | |
996 | |
997 $data = array('Article' => array( | |
998 'id' => '4', | |
999 'title' => 'Fourth Article - New Title', | |
1000 'published' => 'N' | |
1001 )); | |
1002 $result = $TestModel->set($data) && $TestModel->save(); | |
1003 $this->assertTrue($result); | |
1004 | |
1005 $TestModel->recursive = -1; | |
1006 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); | |
1007 $expected = array('Article' => array( | |
1008 'id' => '4', | |
1009 'user_id' => '1', | |
1010 'title' => 'Fourth Article - New Title', | |
1011 'body' => 'Fourth Article Body', | |
1012 'published' => 'N' | |
1013 )); | |
1014 $this->assertEqual($result, $expected); | |
1015 | |
1016 $TestModel->recursive = -1; | |
1017 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); | |
1018 $expected = array('Article' => array( | |
1019 'id' => '5', | |
1020 'user_id' => '4', | |
1021 'title' => 'Fifth Article', | |
1022 'body' => 'Fifth Article Body', | |
1023 'published' => 'Y' | |
1024 )); | |
1025 $this->assertEqual($result, $expected); | |
1026 | |
1027 $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5')); | |
1028 $result = ($TestModel->set($data) && $TestModel->save()); | |
1029 $this->assertTrue($result); | |
1030 | |
1031 $TestModel->recursive = -1; | |
1032 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); | |
1033 $expected = array('Article' => array( | |
1034 'id' => '5', | |
1035 'user_id' => '4', | |
1036 'title' => 'Fifth Article - New Title 5', | |
1037 'body' => 'Fifth Article Body', | |
1038 'published' => 'Y' | |
1039 )); | |
1040 $this->assertEqual($result, $expected); | |
1041 | |
1042 $TestModel->recursive = -1; | |
1043 $result = $TestModel->find('all', array('fields' => array('id', 'title'))); | |
1044 $expected = array( | |
1045 array('Article' => array('id' => 1, 'title' => 'First Article' )), | |
1046 array('Article' => array('id' => 2, 'title' => 'Second Article' )), | |
1047 array('Article' => array('id' => 3, 'title' => 'Third Article' )), | |
1048 array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title' )), | |
1049 array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5' )) | |
1050 ); | |
1051 $this->assertEqual($result, $expected); | |
1052 } | |
1053 | |
1054 /** | |
1055 * testSaveWithNonExistentFields method | |
1056 * | |
1057 * @access public | |
1058 * @return void | |
1059 */ | |
1060 function testSaveWithNonExistentFields() { | |
1061 $this->loadFixtures('Article'); | |
1062 $TestModel =& new Article(); | |
1063 $TestModel->recursive = -1; | |
1064 | |
1065 $data = array( | |
1066 'non_existent' => 'This field does not exist', | |
1067 'user_id' => '1', | |
1068 'title' => 'Fourth Article - New Title', | |
1069 'body' => 'Fourth Article Body', | |
1070 'published' => 'N' | |
1071 ); | |
1072 $result = $TestModel->create() && $TestModel->save($data); | |
1073 $this->assertTrue($result); | |
1074 | |
1075 $expected = array('Article' => array( | |
1076 'id' => '4', | |
1077 'user_id' => '1', | |
1078 'title' => 'Fourth Article - New Title', | |
1079 'body' => 'Fourth Article Body', | |
1080 'published' => 'N' | |
1081 )); | |
1082 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); | |
1083 $this->assertEqual($result, $expected); | |
1084 | |
1085 $data = array( | |
1086 'user_id' => '1', | |
1087 'non_existent' => 'This field does not exist', | |
1088 'title' => 'Fiveth Article - New Title', | |
1089 'body' => 'Fiveth Article Body', | |
1090 'published' => 'N' | |
1091 ); | |
1092 $result = $TestModel->create() && $TestModel->save($data); | |
1093 $this->assertTrue($result); | |
1094 | |
1095 $expected = array('Article' => array( | |
1096 'id' => '5', | |
1097 'user_id' => '1', | |
1098 'title' => 'Fiveth Article - New Title', | |
1099 'body' => 'Fiveth Article Body', | |
1100 'published' => 'N' | |
1101 )); | |
1102 $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); | |
1103 $this->assertEqual($result, $expected); | |
1104 } | |
1105 | |
1106 /** | |
1107 * testSaveFromXml method | |
1108 * | |
1109 * @access public | |
1110 * @return void | |
1111 */ | |
1112 function testSaveFromXml() { | |
1113 $this->loadFixtures('Article'); | |
1114 App::import('Core', 'Xml'); | |
1115 | |
1116 $Article = new Article(); | |
1117 $Article->save(new Xml('<article title="test xml" user_id="5" />')); | |
1118 $this->assertTrue($Article->save(new Xml('<article title="test xml" user_id="5" />'))); | |
1119 | |
1120 $results = $Article->find(array('Article.title' => 'test xml')); | |
1121 $this->assertTrue($results); | |
1122 } | |
1123 | |
1124 /** | |
1125 * testSaveHabtm method | |
1126 * | |
1127 * @access public | |
1128 * @return void | |
1129 */ | |
1130 function testSaveHabtm() { | |
1131 $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); | |
1132 $TestModel =& new Article(); | |
1133 | |
1134 $result = $TestModel->findById(2); | |
1135 $expected = array( | |
1136 'Article' => array( | |
1137 'id' => '2', | |
1138 'user_id' => '3', | |
1139 'title' => 'Second Article', | |
1140 'body' => 'Second Article Body', | |
1141 'published' => 'Y', | |
1142 'created' => '2007-03-18 10:41:23', | |
1143 'updated' => '2007-03-18 10:43:31' | |
1144 ), | |
1145 'User' => array( | |
1146 'id' => '3', | |
1147 'user' => 'larry', | |
1148 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | |
1149 'created' => '2007-03-17 01:20:23', | |
1150 'updated' => '2007-03-17 01:22:31' | |
1151 ), | |
1152 'Comment' => array( | |
1153 array( | |
1154 'id' => '5', | |
1155 'article_id' => '2', | |
1156 'user_id' => '1', | |
1157 'comment' => 'First Comment for Second Article', | |
1158 'published' => 'Y', | |
1159 'created' => '2007-03-18 10:53:23', | |
1160 'updated' => '2007-03-18 10:55:31' | |
1161 ), | |
1162 array( | |
1163 'id' => '6', | |
1164 'article_id' => '2', | |
1165 'user_id' => '2', | |
1166 'comment' => 'Second Comment for Second Article', | |
1167 'published' => 'Y', | |
1168 'created' => '2007-03-18 10:55:23', | |
1169 'updated' => '2007-03-18 10:57:31' | |
1170 )), | |
1171 'Tag' => array( | |
1172 array( | |
1173 'id' => '1', | |
1174 'tag' => 'tag1', | |
1175 'created' => '2007-03-18 12:22:23', | |
1176 'updated' => '2007-03-18 12:24:31' | |
1177 ), | |
1178 array( | |
1179 'id' => '3', | |
1180 'tag' => 'tag3', | |
1181 'created' => '2007-03-18 12:26:23', | |
1182 'updated' => '2007-03-18 12:28:31' | |
1183 ) | |
1184 ) | |
1185 ); | |
1186 $this->assertEqual($result, $expected); | |
1187 | |
1188 $data = array( | |
1189 'Article' => array( | |
1190 'id' => '2', | |
1191 'title' => 'New Second Article' | |
1192 ), | |
1193 'Tag' => array('Tag' => array(1, 2)) | |
1194 ); | |
1195 | |
1196 $this->assertTrue($TestModel->set($data)); | |
1197 $this->assertTrue($TestModel->save()); | |
1198 | |
1199 $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); | |
1200 $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body')); | |
1201 $expected = array( | |
1202 'Article' => array( | |
1203 'id' => '2', | |
1204 'user_id' => '3', | |
1205 'title' => 'New Second Article', | |
1206 'body' => 'Second Article Body' | |
1207 ), | |
1208 'Tag' => array( | |
1209 array( | |
1210 'id' => '1', | |
1211 'tag' => 'tag1', | |
1212 'created' => '2007-03-18 12:22:23', | |
1213 'updated' => '2007-03-18 12:24:31' | |
1214 ), | |
1215 array( | |
1216 'id' => '2', | |
1217 'tag' => 'tag2', | |
1218 'created' => '2007-03-18 12:24:23', | |
1219 'updated' => '2007-03-18 12:26:31' | |
1220 ))); | |
1221 $this->assertEqual($result, $expected); | |
1222 | |
1223 $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3))); | |
1224 $result = $TestModel->set($data); | |
1225 $this->assertTrue($result); | |
1226 | |
1227 $result = $TestModel->save(); | |
1228 $this->assertTrue($result); | |
1229 | |
1230 $TestModel->unbindModel(array( | |
1231 'belongsTo' => array('User'), | |
1232 'hasMany' => array('Comment') | |
1233 )); | |
1234 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1235 $expected = array( | |
1236 'Article' => array( | |
1237 'id' => '2', | |
1238 'user_id' => '3', | |
1239 'title' => 'New Second Article', | |
1240 'body' => 'Second Article Body' | |
1241 ), | |
1242 'Tag' => array( | |
1243 array( | |
1244 'id' => '2', | |
1245 'tag' => 'tag2', | |
1246 'created' => '2007-03-18 12:24:23', | |
1247 'updated' => '2007-03-18 12:26:31' | |
1248 ), | |
1249 array( | |
1250 'id' => '3', | |
1251 'tag' => 'tag3', | |
1252 'created' => '2007-03-18 12:26:23', | |
1253 'updated' => '2007-03-18 12:28:31' | |
1254 ))); | |
1255 $this->assertEqual($result, $expected); | |
1256 | |
1257 $data = array('Tag' => array('Tag' => array(1, 2, 3))); | |
1258 | |
1259 $result = $TestModel->set($data); | |
1260 $this->assertTrue($result); | |
1261 | |
1262 $result = $TestModel->save(); | |
1263 $this->assertTrue($result); | |
1264 | |
1265 $TestModel->unbindModel(array( | |
1266 'belongsTo' => array('User'), | |
1267 'hasMany' => array('Comment') | |
1268 )); | |
1269 $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body')); | |
1270 $expected = array( | |
1271 'Article' => array( | |
1272 'id' => '2', | |
1273 'user_id' => '3', | |
1274 'title' => 'New Second Article', | |
1275 'body' => 'Second Article Body' | |
1276 ), | |
1277 'Tag' => array( | |
1278 array( | |
1279 'id' => '1', | |
1280 'tag' => 'tag1', | |
1281 'created' => '2007-03-18 12:22:23', | |
1282 'updated' => '2007-03-18 12:24:31' | |
1283 ), | |
1284 array( | |
1285 'id' => '2', | |
1286 'tag' => 'tag2', | |
1287 'created' => '2007-03-18 12:24:23', | |
1288 'updated' => '2007-03-18 12:26:31' | |
1289 ), | |
1290 array( | |
1291 'id' => '3', | |
1292 'tag' => 'tag3', | |
1293 'created' => '2007-03-18 12:26:23', | |
1294 'updated' => '2007-03-18 12:28:31' | |
1295 ))); | |
1296 $this->assertEqual($result, $expected); | |
1297 | |
1298 $data = array('Tag' => array('Tag' => array())); | |
1299 $result = $TestModel->set($data); | |
1300 $this->assertTrue($result); | |
1301 | |
1302 $result = $TestModel->save(); | |
1303 $this->assertTrue($result); | |
1304 | |
1305 $data = array('Tag' => array('Tag' => '')); | |
1306 $result = $TestModel->set($data); | |
1307 $this->assertTrue($result); | |
1308 | |
1309 $result = $TestModel->save(); | |
1310 $this->assertTrue($result); | |
1311 | |
1312 $TestModel->unbindModel(array( | |
1313 'belongsTo' => array('User'), | |
1314 'hasMany' => array('Comment') | |
1315 )); | |
1316 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1317 $expected = array( | |
1318 'Article' => array( | |
1319 'id' => '2', | |
1320 'user_id' => '3', | |
1321 'title' => 'New Second Article', | |
1322 'body' => 'Second Article Body' | |
1323 ), | |
1324 'Tag' => array() | |
1325 ); | |
1326 $this->assertEqual($result, $expected); | |
1327 | |
1328 $data = array('Tag' => array('Tag' => array(2, 3))); | |
1329 $result = $TestModel->set($data); | |
1330 $this->assertTrue($result); | |
1331 | |
1332 $result = $TestModel->save(); | |
1333 $this->assertTrue($result); | |
1334 | |
1335 $TestModel->unbindModel(array( | |
1336 'belongsTo' => array('User'), | |
1337 'hasMany' => array('Comment') | |
1338 )); | |
1339 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1340 $expected = array( | |
1341 'Article' => array( | |
1342 'id' => '2', | |
1343 'user_id' => '3', | |
1344 'title' => 'New Second Article', | |
1345 'body' => 'Second Article Body' | |
1346 ), | |
1347 'Tag' => array( | |
1348 array( | |
1349 'id' => '2', | |
1350 'tag' => 'tag2', | |
1351 'created' => '2007-03-18 12:24:23', | |
1352 'updated' => '2007-03-18 12:26:31' | |
1353 ), | |
1354 array( | |
1355 'id' => '3', | |
1356 'tag' => 'tag3', | |
1357 'created' => '2007-03-18 12:26:23', | |
1358 'updated' => '2007-03-18 12:28:31' | |
1359 ))); | |
1360 $this->assertEqual($result, $expected); | |
1361 | |
1362 $data = array( | |
1363 'Tag' => array( | |
1364 'Tag' => array(1, 2) | |
1365 ), | |
1366 'Article' => array( | |
1367 'id' => '2', | |
1368 'title' => 'New Second Article' | |
1369 )); | |
1370 $this->assertTrue($TestModel->set($data)); | |
1371 $this->assertTrue($TestModel->save()); | |
1372 | |
1373 $TestModel->unbindModel(array( | |
1374 'belongsTo' => array('User'), | |
1375 'hasMany' => array('Comment') | |
1376 )); | |
1377 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1378 $expected = array( | |
1379 'Article' => array( | |
1380 'id' => '2', | |
1381 'user_id' => '3', | |
1382 'title' => 'New Second Article', | |
1383 'body' => 'Second Article Body' | |
1384 ), | |
1385 'Tag' => array( | |
1386 array( | |
1387 'id' => '1', | |
1388 'tag' => 'tag1', | |
1389 'created' => '2007-03-18 12:22:23', | |
1390 'updated' => '2007-03-18 12:24:31' | |
1391 ), | |
1392 array( | |
1393 'id' => '2', | |
1394 'tag' => 'tag2', | |
1395 'created' => '2007-03-18 12:24:23', | |
1396 'updated' => '2007-03-18 12:26:31' | |
1397 ))); | |
1398 $this->assertEqual($result, $expected); | |
1399 | |
1400 $data = array( | |
1401 'Tag' => array( | |
1402 'Tag' => array(1, 2) | |
1403 ), | |
1404 'Article' => array( | |
1405 'id' => '2', | |
1406 'title' => 'New Second Article Title' | |
1407 )); | |
1408 $result = $TestModel->set($data); | |
1409 $this->assertTrue($result); | |
1410 $this->assertTrue($TestModel->save()); | |
1411 | |
1412 $TestModel->unbindModel(array( | |
1413 'belongsTo' => array('User'), | |
1414 'hasMany' => array('Comment') | |
1415 )); | |
1416 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1417 $expected = array( | |
1418 'Article' => array( | |
1419 'id' => '2', | |
1420 'user_id' => '3', | |
1421 'title' => 'New Second Article Title', | |
1422 'body' => 'Second Article Body' | |
1423 ), | |
1424 'Tag' => array( | |
1425 array( | |
1426 'id' => '1', | |
1427 'tag' => 'tag1', | |
1428 'created' => '2007-03-18 12:22:23', | |
1429 'updated' => '2007-03-18 12:24:31' | |
1430 ), | |
1431 array( | |
1432 'id' => '2', | |
1433 'tag' => 'tag2', | |
1434 'created' => '2007-03-18 12:24:23', | |
1435 'updated' => '2007-03-18 12:26:31' | |
1436 ) | |
1437 ) | |
1438 ); | |
1439 $this->assertEqual($result, $expected); | |
1440 | |
1441 $data = array( | |
1442 'Tag' => array( | |
1443 'Tag' => array(2, 3) | |
1444 ), | |
1445 'Article' => array( | |
1446 'id' => '2', | |
1447 'title' => 'Changed Second Article' | |
1448 )); | |
1449 $this->assertTrue($TestModel->set($data)); | |
1450 $this->assertTrue($TestModel->save()); | |
1451 | |
1452 $TestModel->unbindModel(array( | |
1453 'belongsTo' => array('User'), | |
1454 'hasMany' => array('Comment') | |
1455 )); | |
1456 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1457 $expected = array( | |
1458 'Article' => array( | |
1459 'id' => '2', | |
1460 'user_id' => '3', | |
1461 'title' => 'Changed Second Article', | |
1462 'body' => 'Second Article Body' | |
1463 ), | |
1464 'Tag' => array( | |
1465 array( | |
1466 'id' => '2', | |
1467 'tag' => 'tag2', | |
1468 'created' => '2007-03-18 12:24:23', | |
1469 'updated' => '2007-03-18 12:26:31' | |
1470 ), | |
1471 array( | |
1472 'id' => '3', | |
1473 'tag' => 'tag3', | |
1474 'created' => '2007-03-18 12:26:23', | |
1475 'updated' => '2007-03-18 12:28:31' | |
1476 ) | |
1477 ) | |
1478 ); | |
1479 $this->assertEqual($result, $expected); | |
1480 | |
1481 $data = array( | |
1482 'Tag' => array( | |
1483 'Tag' => array(1, 3) | |
1484 ), | |
1485 'Article' => array('id' => '2'), | |
1486 ); | |
1487 | |
1488 $result = $TestModel->set($data); | |
1489 $this->assertTrue($result); | |
1490 | |
1491 $result = $TestModel->save(); | |
1492 $this->assertTrue($result); | |
1493 | |
1494 $TestModel->unbindModel(array( | |
1495 'belongsTo' => array('User'), | |
1496 'hasMany' => array('Comment') | |
1497 )); | |
1498 $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); | |
1499 $expected = array( | |
1500 'Article' => array( | |
1501 'id' => '2', | |
1502 'user_id' => '3', | |
1503 'title' => 'Changed Second Article', | |
1504 'body' => 'Second Article Body' | |
1505 ), | |
1506 'Tag' => array( | |
1507 array( | |
1508 'id' => '1', | |
1509 'tag' => 'tag1', | |
1510 'created' => '2007-03-18 12:22:23', | |
1511 'updated' => '2007-03-18 12:24:31' | |
1512 ), | |
1513 array( | |
1514 'id' => '3', | |
1515 'tag' => 'tag3', | |
1516 'created' => '2007-03-18 12:26:23', | |
1517 'updated' => '2007-03-18 12:28:31' | |
1518 ))); | |
1519 $this->assertEqual($result, $expected); | |
1520 | |
1521 $data = array( | |
1522 'Article' => array( | |
1523 'id' => 10, | |
1524 'user_id' => '2', | |
1525 'title' => 'New Article With Tags and fieldList', | |
1526 'body' => 'New Article Body with Tags and fieldList', | |
1527 'created' => '2007-03-18 14:55:23', | |
1528 'updated' => '2007-03-18 14:57:31' | |
1529 ), | |
1530 'Tag' => array( | |
1531 'Tag' => array(1, 2, 3) | |
1532 )); | |
1533 $result = $TestModel->create() | |
1534 && $TestModel->save($data, true, array('user_id', 'title', 'published')); | |
1535 $this->assertTrue($result); | |
1536 | |
1537 $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); | |
1538 $result = $TestModel->read(); | |
1539 $expected = array( | |
1540 'Article' => array( | |
1541 'id' => 4, | |
1542 'user_id' => 2, | |
1543 'title' => 'New Article With Tags and fieldList', | |
1544 'body' => '', | |
1545 'published' => 'N', | |
1546 'created' => '', | |
1547 'updated' => '' | |
1548 ), | |
1549 'Tag' => array( | |
1550 0 => array( | |
1551 'id' => 1, | |
1552 'tag' => 'tag1', | |
1553 'created' => '2007-03-18 12:22:23', | |
1554 'updated' => '2007-03-18 12:24:31' | |
1555 ), | |
1556 1 => array( | |
1557 'id' => 2, | |
1558 'tag' => 'tag2', | |
1559 'created' => '2007-03-18 12:24:23', | |
1560 'updated' => '2007-03-18 12:26:31' | |
1561 ), | |
1562 2 => array( | |
1563 'id' => 3, | |
1564 'tag' => 'tag3', | |
1565 'created' => '2007-03-18 12:26:23', | |
1566 'updated' => '2007-03-18 12:28:31' | |
1567 ))); | |
1568 $this->assertEqual($result, $expected); | |
1569 | |
1570 | |
1571 $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB'); | |
1572 $TestModel = new JoinA(); | |
1573 $TestModel->hasBelongsToMany['JoinC']['unique'] = true; | |
1574 $data = array( | |
1575 'JoinA' => array( | |
1576 'id' => 1, | |
1577 'name' => 'Join A 1', | |
1578 'body' => 'Join A 1 Body', | |
1579 ), | |
1580 'JoinC' => array( | |
1581 'JoinC' => array( | |
1582 array('join_c_id' => 2, 'other' => 'new record'), | |
1583 array('join_c_id' => 3, 'other' => 'new record') | |
1584 ) | |
1585 ) | |
1586 ); | |
1587 $TestModel->save($data); | |
1588 $result = $TestModel->read(null, 1); | |
1589 $expected = array(4, 5); | |
1590 $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected); | |
1591 $expected = array('new record', 'new record'); | |
1592 $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected); | |
1593 } | |
1594 | |
1595 /** | |
1596 * testSaveHabtmCustomKeys method | |
1597 * | |
1598 * @access public | |
1599 * @return void | |
1600 */ | |
1601 function testSaveHabtmCustomKeys() { | |
1602 $this->loadFixtures('Story', 'StoriesTag', 'Tag'); | |
1603 $Story =& new Story(); | |
1604 | |
1605 $data = array( | |
1606 'Story' => array('story' => '1'), | |
1607 'Tag' => array( | |
1608 'Tag' => array(2, 3) | |
1609 )); | |
1610 $result = $Story->set($data); | |
1611 $this->assertTrue($result); | |
1612 | |
1613 $result = $Story->save(); | |
1614 $this->assertTrue($result); | |
1615 | |
1616 $result = $Story->find('all', array('order' => array('Story.story'))); | |
1617 $expected = array( | |
1618 array( | |
1619 'Story' => array( | |
1620 'story' => 1, | |
1621 'title' => 'First Story' | |
1622 ), | |
1623 'Tag' => array( | |
1624 array( | |
1625 'id' => 2, | |
1626 'tag' => 'tag2', | |
1627 'created' => '2007-03-18 12:24:23', | |
1628 'updated' => '2007-03-18 12:26:31' | |
1629 ), | |
1630 array( | |
1631 'id' => 3, | |
1632 'tag' => 'tag3', | |
1633 'created' => '2007-03-18 12:26:23', | |
1634 'updated' => '2007-03-18 12:28:31' | |
1635 ))), | |
1636 array( | |
1637 'Story' => array( | |
1638 'story' => 2, | |
1639 'title' => 'Second Story' | |
1640 ), | |
1641 'Tag' => array() | |
1642 )); | |
1643 $this->assertEqual($result, $expected); | |
1644 } | |
1645 | |
1646 /** | |
1647 * test that saving habtm records respects conditions set in the 'conditions' key | |
1648 * for the association. | |
1649 * | |
1650 * @return void | |
1651 */ | |
1652 function testHabtmSaveWithConditionsInAssociation() { | |
1653 $this->loadFixtures('JoinThing', 'Something', 'SomethingElse'); | |
1654 $Something =& new Something(); | |
1655 $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false); | |
1656 | |
1657 $Something->bindModel(array( | |
1658 'hasAndBelongsToMany' => array( | |
1659 'DoomedSomethingElse' => array( | |
1660 'className' => 'SomethingElse', | |
1661 'joinTable' => 'join_things', | |
1662 'conditions' => 'JoinThing.doomed = true', | |
1663 'unique' => true | |
1664 ), | |
1665 'NotDoomedSomethingElse' => array( | |
1666 'className' => 'SomethingElse', | |
1667 'joinTable' => 'join_things', | |
1668 'conditions' => array('JoinThing.doomed' => 0), | |
1669 'unique' => true | |
1670 ) | |
1671 ) | |
1672 ), false); | |
1673 $result = $Something->read(null, 1); | |
1674 $this->assertTrue(empty($result['NotDoomedSomethingElse'])); | |
1675 $this->assertEqual(count($result['DoomedSomethingElse']), 1); | |
1676 | |
1677 $data = array( | |
1678 'Something' => array('id' => 1), | |
1679 'NotDoomedSomethingElse' => array( | |
1680 'NotDoomedSomethingElse' => array( | |
1681 array('something_else_id' => 2, 'doomed' => 0), | |
1682 array('something_else_id' => 3, 'doomed' => 0) | |
1683 ) | |
1684 ) | |
1685 ); | |
1686 $Something->create($data); | |
1687 $result = $Something->save(); | |
1688 $this->assertTrue($result); | |
1689 | |
1690 $result = $Something->read(null, 1); | |
1691 $this->assertEqual(count($result['NotDoomedSomethingElse']), 2); | |
1692 $this->assertEqual(count($result['DoomedSomethingElse']), 1); | |
1693 } | |
1694 /** | |
1695 * testHabtmSaveKeyResolution method | |
1696 * | |
1697 * @access public | |
1698 * @return void | |
1699 */ | |
1700 function testHabtmSaveKeyResolution() { | |
1701 $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); | |
1702 $ThePaper =& new ThePaper(); | |
1703 | |
1704 $ThePaper->id = 1; | |
1705 $ThePaper->save(array('Monkey' => array(2, 3))); | |
1706 | |
1707 $result = $ThePaper->findById(1); | |
1708 $expected = array( | |
1709 array( | |
1710 'id' => '2', | |
1711 'device_type_id' => '1', | |
1712 'name' => 'Device 2', | |
1713 'typ' => '1' | |
1714 ), | |
1715 array( | |
1716 'id' => '3', | |
1717 'device_type_id' => '1', | |
1718 'name' => 'Device 3', | |
1719 'typ' => '2' | |
1720 )); | |
1721 $this->assertEqual($result['Monkey'], $expected); | |
1722 | |
1723 $ThePaper->id = 2; | |
1724 $ThePaper->save(array('Monkey' => array(1, 2, 3))); | |
1725 | |
1726 $result = $ThePaper->findById(2); | |
1727 $expected = array( | |
1728 array( | |
1729 'id' => '1', | |
1730 'device_type_id' => '1', | |
1731 'name' => 'Device 1', | |
1732 'typ' => '1' | |
1733 ), | |
1734 array( | |
1735 'id' => '2', | |
1736 'device_type_id' => '1', | |
1737 'name' => 'Device 2', | |
1738 'typ' => '1' | |
1739 ), | |
1740 array( | |
1741 'id' => '3', | |
1742 'device_type_id' => '1', | |
1743 'name' => 'Device 3', | |
1744 'typ' => '2' | |
1745 )); | |
1746 $this->assertEqual($result['Monkey'], $expected); | |
1747 | |
1748 $ThePaper->id = 2; | |
1749 $ThePaper->save(array('Monkey' => array(1, 3))); | |
1750 | |
1751 $result = $ThePaper->findById(2); | |
1752 $expected = array( | |
1753 array( | |
1754 'id' => '1', | |
1755 'device_type_id' => '1', | |
1756 'name' => 'Device 1', | |
1757 'typ' => '1' | |
1758 ), | |
1759 array( | |
1760 'id' => '3', | |
1761 'device_type_id' => '1', | |
1762 'name' => 'Device 3', | |
1763 'typ' => '2' | |
1764 )); | |
1765 $this->assertEqual($result['Monkey'], $expected); | |
1766 | |
1767 $result = $ThePaper->findById(1); | |
1768 $expected = array( | |
1769 array( | |
1770 'id' => '2', | |
1771 'device_type_id' => '1', | |
1772 'name' => 'Device 2', | |
1773 'typ' => '1' | |
1774 ), | |
1775 array( | |
1776 'id' => '3', | |
1777 'device_type_id' => '1', | |
1778 'name' => 'Device 3', | |
1779 'typ' => '2' | |
1780 )); | |
1781 $this->assertEqual($result['Monkey'], $expected); | |
1782 } | |
1783 | |
1784 /** | |
1785 * testCreationOfEmptyRecord method | |
1786 * | |
1787 * @access public | |
1788 * @return void | |
1789 */ | |
1790 function testCreationOfEmptyRecord() { | |
1791 $this->loadFixtures('Author'); | |
1792 $TestModel =& new Author(); | |
1793 $this->assertEqual($TestModel->find('count'), 4); | |
1794 | |
1795 $TestModel->deleteAll(true, false, false); | |
1796 $this->assertEqual($TestModel->find('count'), 0); | |
1797 | |
1798 $result = $TestModel->save(); | |
1799 $this->assertTrue(isset($result['Author']['created'])); | |
1800 $this->assertTrue(isset($result['Author']['updated'])); | |
1801 $this->assertEqual($TestModel->find('count'), 1); | |
1802 } | |
1803 | |
1804 /** | |
1805 * testCreateWithPKFiltering method | |
1806 * | |
1807 * @access public | |
1808 * @return void | |
1809 */ | |
1810 function testCreateWithPKFiltering() { | |
1811 $TestModel =& new Article(); | |
1812 $data = array( | |
1813 'id' => 5, | |
1814 'user_id' => 2, | |
1815 'title' => 'My article', | |
1816 'body' => 'Some text' | |
1817 ); | |
1818 | |
1819 $result = $TestModel->create($data); | |
1820 $expected = array( | |
1821 'Article' => array( | |
1822 'published' => 'N', | |
1823 'id' => 5, | |
1824 'user_id' => 2, | |
1825 'title' => 'My article', | |
1826 'body' => 'Some text' | |
1827 )); | |
1828 | |
1829 $this->assertEqual($result, $expected); | |
1830 $this->assertEqual($TestModel->id, 5); | |
1831 | |
1832 $result = $TestModel->create($data, true); | |
1833 $expected = array( | |
1834 'Article' => array( | |
1835 'published' => 'N', | |
1836 'id' => false, | |
1837 'user_id' => 2, | |
1838 'title' => 'My article', | |
1839 'body' => 'Some text' | |
1840 )); | |
1841 | |
1842 $this->assertEqual($result, $expected); | |
1843 $this->assertFalse($TestModel->id); | |
1844 | |
1845 $result = $TestModel->create(array('Article' => $data), true); | |
1846 $expected = array( | |
1847 'Article' => array( | |
1848 'published' => 'N', | |
1849 'id' => false, | |
1850 'user_id' => 2, | |
1851 'title' => 'My article', | |
1852 'body' => 'Some text' | |
1853 )); | |
1854 | |
1855 $this->assertEqual($result, $expected); | |
1856 $this->assertFalse($TestModel->id); | |
1857 | |
1858 $data = array( | |
1859 'id' => 6, | |
1860 'user_id' => 2, | |
1861 'title' => 'My article', | |
1862 'body' => 'Some text', | |
1863 'created' => '1970-01-01 00:00:00', | |
1864 'updated' => '1970-01-01 12:00:00', | |
1865 'modified' => '1970-01-01 12:00:00' | |
1866 ); | |
1867 | |
1868 $result = $TestModel->create($data); | |
1869 $expected = array( | |
1870 'Article' => array( | |
1871 'published' => 'N', | |
1872 'id' => 6, | |
1873 'user_id' => 2, | |
1874 'title' => 'My article', | |
1875 'body' => 'Some text', | |
1876 'created' => '1970-01-01 00:00:00', | |
1877 'updated' => '1970-01-01 12:00:00', | |
1878 'modified' => '1970-01-01 12:00:00' | |
1879 )); | |
1880 $this->assertEqual($result, $expected); | |
1881 $this->assertEqual($TestModel->id, 6); | |
1882 | |
1883 $result = $TestModel->create(array( | |
1884 'Article' => array_diff_key($data, array( | |
1885 'created' => true, | |
1886 'updated' => true, | |
1887 'modified' => true | |
1888 ))), true); | |
1889 $expected = array( | |
1890 'Article' => array( | |
1891 'published' => 'N', | |
1892 'id' => false, | |
1893 'user_id' => 2, | |
1894 'title' => 'My article', | |
1895 'body' => 'Some text' | |
1896 )); | |
1897 $this->assertEqual($result, $expected); | |
1898 $this->assertFalse($TestModel->id); | |
1899 } | |
1900 | |
1901 /** | |
1902 * testCreationWithMultipleData method | |
1903 * | |
1904 * @access public | |
1905 * @return void | |
1906 */ | |
1907 function testCreationWithMultipleData() { | |
1908 $this->loadFixtures('Article', 'Comment'); | |
1909 $Article =& new Article(); | |
1910 $Comment =& new Comment(); | |
1911 | |
1912 $articles = $Article->find('all', array( | |
1913 'fields' => array('id','title'), | |
1914 'recursive' => -1 | |
1915 )); | |
1916 | |
1917 $comments = $Comment->find('all', array( | |
1918 'fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1)); | |
1919 | |
1920 $this->assertEqual($articles, array( | |
1921 array('Article' => array( | |
1922 'id' => 1, | |
1923 'title' => 'First Article' | |
1924 )), | |
1925 array('Article' => array( | |
1926 'id' => 2, | |
1927 'title' => 'Second Article' | |
1928 )), | |
1929 array('Article' => array( | |
1930 'id' => 3, | |
1931 'title' => 'Third Article' | |
1932 )))); | |
1933 | |
1934 $this->assertEqual($comments, array( | |
1935 array('Comment' => array( | |
1936 'id' => 1, | |
1937 'article_id' => 1, | |
1938 'user_id' => 2, | |
1939 'comment' => 'First Comment for First Article', | |
1940 'published' => 'Y' | |
1941 )), | |
1942 array('Comment' => array( | |
1943 'id' => 2, | |
1944 'article_id' => 1, | |
1945 'user_id' => 4, | |
1946 'comment' => 'Second Comment for First Article', | |
1947 'published' => 'Y' | |
1948 )), | |
1949 array('Comment' => array( | |
1950 'id' => 3, | |
1951 'article_id' => 1, | |
1952 'user_id' => 1, | |
1953 'comment' => 'Third Comment for First Article', | |
1954 'published' => 'Y' | |
1955 )), | |
1956 array('Comment' => array( | |
1957 'id' => 4, | |
1958 'article_id' => 1, | |
1959 'user_id' => 1, | |
1960 'comment' => 'Fourth Comment for First Article', | |
1961 'published' => 'N' | |
1962 )), | |
1963 array('Comment' => array( | |
1964 'id' => 5, | |
1965 'article_id' => 2, | |
1966 'user_id' => 1, | |
1967 'comment' => 'First Comment for Second Article', | |
1968 'published' => 'Y' | |
1969 )), | |
1970 array('Comment' => array( | |
1971 'id' => 6, | |
1972 'article_id' => 2, | |
1973 'user_id' => 2, | |
1974 'comment' => 'Second Comment for Second Article', | |
1975 'published' => 'Y' | |
1976 )))); | |
1977 | |
1978 $data = array( | |
1979 'Comment' => array( | |
1980 'article_id' => 2, | |
1981 'user_id' => 4, | |
1982 'comment' => 'Brand New Comment', | |
1983 'published' => 'N' | |
1984 ), | |
1985 'Article' => array( | |
1986 'id' => 2, | |
1987 'title' => 'Second Article Modified' | |
1988 )); | |
1989 | |
1990 $result = $Comment->create($data); | |
1991 | |
1992 $this->assertTrue($result); | |
1993 $result = $Comment->save(); | |
1994 $this->assertTrue($result); | |
1995 | |
1996 $articles = $Article->find('all', array( | |
1997 'fields' => array('id','title'), | |
1998 'recursive' => -1 | |
1999 )); | |
2000 | |
2001 $comments = $Comment->find('all', array( | |
2002 'fields' => array('id','article_id','user_id','comment','published'), | |
2003 'recursive' => -1 | |
2004 )); | |
2005 | |
2006 $this->assertEqual($articles, array( | |
2007 array('Article' => array( | |
2008 'id' => 1, | |
2009 'title' => 'First Article' | |
2010 )), | |
2011 array('Article' => array( | |
2012 'id' => 2, | |
2013 'title' => 'Second Article' | |
2014 )), | |
2015 array('Article' => array( | |
2016 'id' => 3, | |
2017 'title' => 'Third Article' | |
2018 )))); | |
2019 | |
2020 $this->assertEqual($comments, array( | |
2021 array('Comment' => array( | |
2022 'id' => 1, | |
2023 'article_id' => 1, | |
2024 'user_id' => 2, | |
2025 'comment' => 'First Comment for First Article', | |
2026 'published' => 'Y' | |
2027 )), | |
2028 array('Comment' => array( | |
2029 'id' => 2, | |
2030 'article_id' => 1, | |
2031 'user_id' => 4, | |
2032 'comment' => 'Second Comment for First Article', | |
2033 'published' => 'Y' | |
2034 )), | |
2035 array('Comment' => array( | |
2036 'id' => 3, | |
2037 'article_id' => 1, | |
2038 'user_id' => 1, | |
2039 'comment' => 'Third Comment for First Article', | |
2040 'published' => 'Y' | |
2041 )), | |
2042 array('Comment' => array( | |
2043 'id' => 4, | |
2044 'article_id' => 1, | |
2045 'user_id' => 1, | |
2046 'comment' => 'Fourth Comment for First Article', | |
2047 'published' => 'N' | |
2048 )), | |
2049 array('Comment' => array( | |
2050 'id' => 5, | |
2051 'article_id' => 2, | |
2052 'user_id' => 1, | |
2053 'comment' => 'First Comment for Second Article', | |
2054 'published' => 'Y' | |
2055 )), | |
2056 array('Comment' => array( | |
2057 'id' => 6, | |
2058 'article_id' => 2, | |
2059 'user_id' => 2, 'comment' => | |
2060 'Second Comment for Second Article', | |
2061 'published' => 'Y' | |
2062 )), | |
2063 array('Comment' => array( | |
2064 'id' => 7, | |
2065 'article_id' => 2, | |
2066 'user_id' => 4, | |
2067 'comment' => 'Brand New Comment', | |
2068 'published' => 'N' | |
2069 )))); | |
2070 | |
2071 } | |
2072 | |
2073 /** | |
2074 * testCreationWithMultipleDataSameModel method | |
2075 * | |
2076 * @access public | |
2077 * @return void | |
2078 */ | |
2079 function testCreationWithMultipleDataSameModel() { | |
2080 $this->loadFixtures('Article'); | |
2081 $Article =& new Article(); | |
2082 $SecondaryArticle =& new Article(); | |
2083 | |
2084 $result = $Article->field('title', array('id' => 1)); | |
2085 $this->assertEqual($result, 'First Article'); | |
2086 | |
2087 $data = array( | |
2088 'Article' => array( | |
2089 'user_id' => 2, | |
2090 'title' => 'Brand New Article', | |
2091 'body' => 'Brand New Article Body', | |
2092 'published' => 'Y' | |
2093 ), | |
2094 'SecondaryArticle' => array( | |
2095 'id' => 1 | |
2096 )); | |
2097 | |
2098 $Article->create(); | |
2099 $result = $Article->save($data); | |
2100 $this->assertTrue($result); | |
2101 | |
2102 $result = $Article->getInsertID(); | |
2103 $this->assertTrue(!empty($result)); | |
2104 | |
2105 $result = $Article->field('title', array('id' => 1)); | |
2106 $this->assertEqual($result, 'First Article'); | |
2107 | |
2108 $articles = $Article->find('all', array( | |
2109 'fields' => array('id','title'), | |
2110 'recursive' => -1 | |
2111 )); | |
2112 | |
2113 $this->assertEqual($articles, array( | |
2114 array('Article' => array( | |
2115 'id' => 1, | |
2116 'title' => 'First Article' | |
2117 )), | |
2118 array('Article' => array( | |
2119 'id' => 2, | |
2120 'title' => 'Second Article' | |
2121 )), | |
2122 array('Article' => array( | |
2123 'id' => 3, | |
2124 'title' => 'Third Article' | |
2125 )), | |
2126 array('Article' => array( | |
2127 'id' => 4, | |
2128 'title' => 'Brand New Article' | |
2129 )))); | |
2130 } | |
2131 | |
2132 /** | |
2133 * testCreationWithMultipleDataSameModelManualInstances method | |
2134 * | |
2135 * @access public | |
2136 * @return void | |
2137 */ | |
2138 function testCreationWithMultipleDataSameModelManualInstances() { | |
2139 $this->loadFixtures('PrimaryModel'); | |
2140 $Primary =& new PrimaryModel(); | |
2141 $Secondary =& new PrimaryModel(); | |
2142 | |
2143 $result = $Primary->field('primary_name', array('id' => 1)); | |
2144 $this->assertEqual($result, 'Primary Name Existing'); | |
2145 | |
2146 $data = array( | |
2147 'PrimaryModel' => array( | |
2148 'primary_name' => 'Primary Name New' | |
2149 ), | |
2150 'SecondaryModel' => array( | |
2151 'id' => array(1) | |
2152 )); | |
2153 | |
2154 $Primary->create(); | |
2155 $result = $Primary->save($data); | |
2156 $this->assertTrue($result); | |
2157 | |
2158 $result = $Primary->field('primary_name', array('id' => 1)); | |
2159 $this->assertEqual($result, 'Primary Name Existing'); | |
2160 | |
2161 $result = $Primary->getInsertID(); | |
2162 $this->assertTrue(!empty($result)); | |
2163 | |
2164 $result = $Primary->field('primary_name', array('id' => $result)); | |
2165 $this->assertEqual($result, 'Primary Name New'); | |
2166 | |
2167 $result = $Primary->find('count'); | |
2168 $this->assertEqual($result, 2); | |
2169 } | |
2170 | |
2171 /** | |
2172 * testRecordExists method | |
2173 * | |
2174 * @access public | |
2175 * @return void | |
2176 */ | |
2177 function testRecordExists() { | |
2178 $this->loadFixtures('User'); | |
2179 $TestModel =& new User(); | |
2180 | |
2181 $this->assertFalse($TestModel->exists()); | |
2182 $TestModel->read(null, 1); | |
2183 $this->assertTrue($TestModel->exists()); | |
2184 $TestModel->create(); | |
2185 $this->assertFalse($TestModel->exists()); | |
2186 $TestModel->id = 4; | |
2187 $this->assertTrue($TestModel->exists()); | |
2188 | |
2189 $TestModel =& new TheVoid(); | |
2190 $this->assertFalse($TestModel->exists()); | |
2191 | |
2192 $TestModel->id = 5; | |
2193 $this->expectError(); | |
2194 ob_start(); | |
2195 $this->assertFalse($TestModel->exists()); | |
2196 $output = ob_get_clean(); | |
2197 } | |
2198 | |
2199 /** | |
2200 * testUpdateExisting method | |
2201 * | |
2202 * @access public | |
2203 * @return void | |
2204 */ | |
2205 function testUpdateExisting() { | |
2206 $this->loadFixtures('User', 'Article', 'Comment'); | |
2207 $TestModel =& new User(); | |
2208 $TestModel->create(); | |
2209 | |
2210 $TestModel->save(array( | |
2211 'User' => array( | |
2212 'user' => 'some user', | |
2213 'password' => 'some password' | |
2214 ))); | |
2215 $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5)); | |
2216 $id = $TestModel->id; | |
2217 | |
2218 $TestModel->save(array( | |
2219 'User' => array( | |
2220 'user' => 'updated user' | |
2221 ))); | |
2222 $this->assertEqual($TestModel->id, $id); | |
2223 | |
2224 $result = $TestModel->findById($id); | |
2225 $this->assertEqual($result['User']['user'], 'updated user'); | |
2226 $this->assertEqual($result['User']['password'], 'some password'); | |
2227 | |
2228 $Article =& new Article(); | |
2229 $Comment =& new Comment(); | |
2230 $data = array( | |
2231 'Comment' => array( | |
2232 'id' => 1, | |
2233 'comment' => 'First Comment for First Article' | |
2234 ), | |
2235 'Article' => array( | |
2236 'id' => 2, | |
2237 'title' => 'Second Article' | |
2238 )); | |
2239 | |
2240 $result = $Article->save($data); | |
2241 $this->assertTrue($result); | |
2242 | |
2243 $result = $Comment->save($data); | |
2244 $this->assertTrue($result); | |
2245 } | |
2246 | |
2247 /** | |
2248 * test updating records and saving blank values. | |
2249 * | |
2250 * @return void | |
2251 */ | |
2252 function testUpdateSavingBlankValues() { | |
2253 $this->loadFixtures('Article'); | |
2254 $Article =& new Article(); | |
2255 $Article->validate = array(); | |
2256 $Article->create(); | |
2257 $result = $Article->save(array( | |
2258 'id' => 1, | |
2259 'title' => '', | |
2260 'body' => '' | |
2261 )); | |
2262 $this->assertTrue($result); | |
2263 $result = $Article->find('first', array('conditions' => array('Article.id' => 1))); | |
2264 $this->assertEqual('', $result['Article']['title'], 'Title is not blank'); | |
2265 $this->assertEqual('', $result['Article']['body'], 'Body is not blank'); | |
2266 } | |
2267 | |
2268 /** | |
2269 * testUpdateMultiple method | |
2270 * | |
2271 * @access public | |
2272 * @return void | |
2273 */ | |
2274 function testUpdateMultiple() { | |
2275 $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread'); | |
2276 $TestModel =& new Comment(); | |
2277 $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id'); | |
2278 $expected = array('2', '4', '1', '1', '1', '2'); | |
2279 $this->assertEqual($result, $expected); | |
2280 | |
2281 $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2)); | |
2282 $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id'); | |
2283 $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5); | |
2284 $this->assertEqual($result, $expected); | |
2285 | |
2286 $result = $TestModel->updateAll( | |
2287 array('Comment.comment' => "'Updated today'"), | |
2288 array('Comment.user_id' => 5) | |
2289 ); | |
2290 $this->assertTrue($result); | |
2291 $result = Set::extract( | |
2292 $TestModel->find('all', array( | |
2293 'conditions' => array( | |
2294 'Comment.user_id' => 5 | |
2295 ))), | |
2296 '{n}.Comment.comment' | |
2297 ); | |
2298 $expected = array_fill(0, 2, 'Updated today'); | |
2299 $this->assertEqual($result, $expected); | |
2300 } | |
2301 | |
2302 /** | |
2303 * testHabtmUuidWithUuidId method | |
2304 * | |
2305 * @access public | |
2306 * @return void | |
2307 */ | |
2308 function testHabtmUuidWithUuidId() { | |
2309 $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio'); | |
2310 $TestModel =& new Uuidportfolio(); | |
2311 | |
2312 $data = array('Uuidportfolio' => array('name' => 'Portfolio 3')); | |
2313 $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569'); | |
2314 $TestModel->create($data); | |
2315 $TestModel->save(); | |
2316 $id = $TestModel->id; | |
2317 $result = $TestModel->read(null, $id); | |
2318 $this->assertEqual(1, count($result['Uuiditem'])); | |
2319 $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36); | |
2320 } | |
2321 | |
2322 /** | |
2323 * test HABTM saving when join table has no primary key and only 2 columns. | |
2324 * | |
2325 * @return void | |
2326 */ | |
2327 function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() { | |
2328 $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); | |
2329 $Fruit =& new Fruit(); | |
2330 $data = array( | |
2331 'Fruit' => array( | |
2332 'color' => 'Red', | |
2333 'shape' => 'Heart-shaped', | |
2334 'taste' => 'sweet', | |
2335 'name' => 'Strawberry', | |
2336 ), | |
2337 'UuidTag' => array( | |
2338 'UuidTag' => array( | |
2339 '481fc6d0-b920-43e0-e50f-6d1740cf8569' | |
2340 ) | |
2341 ) | |
2342 ); | |
2343 $this->assertTrue($Fruit->save($data)); | |
2344 } | |
2345 | |
2346 /** | |
2347 * test HABTM saving when join table has no primary key and only 2 columns, no with model is used. | |
2348 * | |
2349 * @return void | |
2350 */ | |
2351 function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() { | |
2352 $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); | |
2353 $Fruit =& new FruitNoWith(); | |
2354 $data = array( | |
2355 'Fruit' => array( | |
2356 'color' => 'Red', | |
2357 'shape' => 'Heart-shaped', | |
2358 'taste' => 'sweet', | |
2359 'name' => 'Strawberry', | |
2360 ), | |
2361 'UuidTag' => array( | |
2362 'UuidTag' => array( | |
2363 '481fc6d0-b920-43e0-e50f-6d1740cf8569' | |
2364 ) | |
2365 ) | |
2366 ); | |
2367 $this->assertTrue($Fruit->save($data)); | |
2368 } | |
2369 | |
2370 /** | |
2371 * testHabtmUuidWithNumericId method | |
2372 * | |
2373 * @access public | |
2374 * @return void | |
2375 */ | |
2376 function testHabtmUuidWithNumericId() { | |
2377 $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid'); | |
2378 $TestModel =& new Uuiditem(); | |
2379 | |
2380 $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0)); | |
2381 $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569'); | |
2382 $TestModel->create($data); | |
2383 $TestModel->save(); | |
2384 $id = $TestModel->id; | |
2385 $result = $TestModel->read(null, $id); | |
2386 $this->assertEqual(1, count($result['Uuidportfolio'])); | |
2387 } | |
2388 | |
2389 /** | |
2390 * testSaveMultipleHabtm method | |
2391 * | |
2392 * @access public | |
2393 * @return void | |
2394 */ | |
2395 function testSaveMultipleHabtm() { | |
2396 $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); | |
2397 $TestModel = new JoinA(); | |
2398 $result = $TestModel->findById(1); | |
2399 | |
2400 $expected = array( | |
2401 'JoinA' => array( | |
2402 'id' => 1, | |
2403 'name' => 'Join A 1', | |
2404 'body' => 'Join A 1 Body', | |
2405 'created' => '2008-01-03 10:54:23', | |
2406 'updated' => '2008-01-03 10:54:23' | |
2407 ), | |
2408 'JoinB' => array( | |
2409 0 => array( | |
2410 'id' => 2, | |
2411 'name' => 'Join B 2', | |
2412 'created' => '2008-01-03 10:55:02', | |
2413 'updated' => '2008-01-03 10:55:02', | |
2414 'JoinAsJoinB' => array( | |
2415 'id' => 1, | |
2416 'join_a_id' => 1, | |
2417 'join_b_id' => 2, | |
2418 'other' => 'Data for Join A 1 Join B 2', | |
2419 'created' => '2008-01-03 10:56:33', | |
2420 'updated' => '2008-01-03 10:56:33' | |
2421 ))), | |
2422 'JoinC' => array( | |
2423 0 => array( | |
2424 'id' => 2, | |
2425 'name' => 'Join C 2', | |
2426 'created' => '2008-01-03 10:56:12', | |
2427 'updated' => '2008-01-03 10:56:12', | |
2428 'JoinAsJoinC' => array( | |
2429 'id' => 1, | |
2430 'join_a_id' => 1, | |
2431 'join_c_id' => 2, | |
2432 'other' => 'Data for Join A 1 Join C 2', | |
2433 'created' => '2008-01-03 10:57:22', | |
2434 'updated' => '2008-01-03 10:57:22' | |
2435 )))); | |
2436 | |
2437 $this->assertEqual($result, $expected); | |
2438 | |
2439 $ts = date('Y-m-d H:i:s'); | |
2440 $TestModel->id = 1; | |
2441 $data = array( | |
2442 'JoinA' => array( | |
2443 'id' => '1', | |
2444 'name' => 'New name for Join A 1', | |
2445 'updated' => $ts | |
2446 ), | |
2447 'JoinB' => array( | |
2448 array( | |
2449 'id' => 1, | |
2450 'join_b_id' => 2, | |
2451 'other' => 'New data for Join A 1 Join B 2', | |
2452 'created' => $ts, | |
2453 'updated' => $ts | |
2454 )), | |
2455 'JoinC' => array( | |
2456 array( | |
2457 'id' => 1, | |
2458 'join_c_id' => 2, | |
2459 'other' => 'New data for Join A 1 Join C 2', | |
2460 'created' => $ts, | |
2461 'updated' => $ts | |
2462 ))); | |
2463 | |
2464 $TestModel->set($data); | |
2465 $TestModel->save(); | |
2466 | |
2467 $result = $TestModel->findById(1); | |
2468 $expected = array( | |
2469 'JoinA' => array( | |
2470 'id' => 1, | |
2471 'name' => 'New name for Join A 1', | |
2472 'body' => 'Join A 1 Body', | |
2473 'created' => '2008-01-03 10:54:23', | |
2474 'updated' => $ts | |
2475 ), | |
2476 'JoinB' => array( | |
2477 0 => array( | |
2478 'id' => 2, | |
2479 'name' => 'Join B 2', | |
2480 'created' => '2008-01-03 10:55:02', | |
2481 'updated' => '2008-01-03 10:55:02', | |
2482 'JoinAsJoinB' => array( | |
2483 'id' => 1, | |
2484 'join_a_id' => 1, | |
2485 'join_b_id' => 2, | |
2486 'other' => 'New data for Join A 1 Join B 2', | |
2487 'created' => $ts, | |
2488 'updated' => $ts | |
2489 ))), | |
2490 'JoinC' => array( | |
2491 0 => array( | |
2492 'id' => 2, | |
2493 'name' => 'Join C 2', | |
2494 'created' => '2008-01-03 10:56:12', | |
2495 'updated' => '2008-01-03 10:56:12', | |
2496 'JoinAsJoinC' => array( | |
2497 'id' => 1, | |
2498 'join_a_id' => 1, | |
2499 'join_c_id' => 2, | |
2500 'other' => 'New data for Join A 1 Join C 2', | |
2501 'created' => $ts, | |
2502 'updated' => $ts | |
2503 )))); | |
2504 | |
2505 $this->assertEqual($result, $expected); | |
2506 } | |
2507 | |
2508 /** | |
2509 * testSaveAll method | |
2510 * | |
2511 * @access public | |
2512 * @return void | |
2513 */ | |
2514 function testSaveAll() { | |
2515 $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); | |
2516 $TestModel =& new Post(); | |
2517 | |
2518 $result = $TestModel->find('all'); | |
2519 $this->assertEqual(count($result), 3); | |
2520 $this->assertFalse(isset($result[3])); | |
2521 $ts = date('Y-m-d H:i:s'); | |
2522 | |
2523 $TestModel->saveAll(array( | |
2524 'Post' => array( | |
2525 'title' => 'Post with Author', | |
2526 'body' => 'This post will be saved with an author' | |
2527 ), | |
2528 'Author' => array( | |
2529 'user' => 'bob', | |
2530 'password' => '5f4dcc3b5aa765d61d8327deb882cf90' | |
2531 ))); | |
2532 | |
2533 $result = $TestModel->find('all'); | |
2534 $expected = array( | |
2535 'Post' => array( | |
2536 'id' => '4', | |
2537 'author_id' => '5', | |
2538 'title' => 'Post with Author', | |
2539 'body' => 'This post will be saved with an author', | |
2540 'published' => 'N', | |
2541 'created' => $ts, | |
2542 'updated' => $ts | |
2543 ), | |
2544 'Author' => array( | |
2545 'id' => '5', | |
2546 'user' => 'bob', | |
2547 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', | |
2548 'created' => $ts, | |
2549 'updated' => $ts, | |
2550 'test' => 'working' | |
2551 )); | |
2552 $this->assertEqual($result[3], $expected); | |
2553 $this->assertEqual(count($result), 4); | |
2554 | |
2555 $TestModel->deleteAll(true); | |
2556 $this->assertEqual($TestModel->find('all'), array()); | |
2557 | |
2558 // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass | |
2559 $this->db->truncate($TestModel); | |
2560 | |
2561 $ts = date('Y-m-d H:i:s'); | |
2562 $TestModel->saveAll(array( | |
2563 array( | |
2564 'title' => 'Multi-record post 1', | |
2565 'body' => 'First multi-record post', | |
2566 'author_id' => 2 | |
2567 ), | |
2568 array( | |
2569 'title' => 'Multi-record post 2', | |
2570 'body' => 'Second multi-record post', | |
2571 'author_id' => 2 | |
2572 ))); | |
2573 | |
2574 $result = $TestModel->find('all', array( | |
2575 'recursive' => -1, | |
2576 'order' => 'Post.id ASC' | |
2577 )); | |
2578 $expected = array( | |
2579 array( | |
2580 'Post' => array( | |
2581 'id' => '1', | |
2582 'author_id' => '2', | |
2583 'title' => 'Multi-record post 1', | |
2584 'body' => 'First multi-record post', | |
2585 'published' => 'N', | |
2586 'created' => $ts, | |
2587 'updated' => $ts | |
2588 )), | |
2589 array( | |
2590 'Post' => array( | |
2591 'id' => '2', | |
2592 'author_id' => '2', | |
2593 'title' => 'Multi-record post 2', | |
2594 'body' => 'Second multi-record post', | |
2595 'published' => 'N', | |
2596 'created' => $ts, | |
2597 'updated' => $ts | |
2598 ))); | |
2599 $this->assertEqual($result, $expected); | |
2600 | |
2601 $TestModel =& new Comment(); | |
2602 $ts = date('Y-m-d H:i:s'); | |
2603 $result = $TestModel->saveAll(array( | |
2604 'Comment' => array( | |
2605 'article_id' => 2, | |
2606 'user_id' => 2, | |
2607 'comment' => 'New comment with attachment', | |
2608 'published' => 'Y' | |
2609 ), | |
2610 'Attachment' => array( | |
2611 'attachment' => 'some_file.tgz' | |
2612 ))); | |
2613 $this->assertTrue($result); | |
2614 | |
2615 $result = $TestModel->find('all'); | |
2616 $expected = array( | |
2617 'id' => '7', | |
2618 'article_id' => '2', | |
2619 'user_id' => '2', | |
2620 'comment' => 'New comment with attachment', | |
2621 'published' => 'Y', | |
2622 'created' => $ts, | |
2623 'updated' => $ts | |
2624 ); | |
2625 $this->assertEqual($result[6]['Comment'], $expected); | |
2626 | |
2627 $expected = array( | |
2628 'id' => '7', | |
2629 'article_id' => '2', | |
2630 'user_id' => '2', | |
2631 'comment' => 'New comment with attachment', | |
2632 'published' => 'Y', | |
2633 'created' => $ts, | |
2634 'updated' => $ts | |
2635 ); | |
2636 $this->assertEqual($result[6]['Comment'], $expected); | |
2637 | |
2638 $expected = array( | |
2639 'id' => '2', | |
2640 'comment_id' => '7', | |
2641 'attachment' => 'some_file.tgz', | |
2642 'created' => $ts, | |
2643 'updated' => $ts | |
2644 ); | |
2645 $this->assertEqual($result[6]['Attachment'], $expected); | |
2646 } | |
2647 | |
2648 /** | |
2649 * Test SaveAll with Habtm relations | |
2650 * | |
2651 * @access public | |
2652 * @return void | |
2653 */ | |
2654 function testSaveAllHabtm() { | |
2655 $this->loadFixtures('Article', 'Tag', 'Comment', 'User'); | |
2656 $data = array( | |
2657 'Article' => array( | |
2658 'user_id' => 1, | |
2659 'title' => 'Article Has and belongs to Many Tags' | |
2660 ), | |
2661 'Tag' => array( | |
2662 'Tag' => array(1, 2) | |
2663 ), | |
2664 'Comment' => array( | |
2665 array( | |
2666 'comment' => 'Article comment', | |
2667 'user_id' => 1 | |
2668 ))); | |
2669 $Article =& new Article(); | |
2670 $result = $Article->saveAll($data); | |
2671 $this->assertTrue($result); | |
2672 | |
2673 $result = $Article->read(); | |
2674 $this->assertEqual(count($result['Tag']), 2); | |
2675 $this->assertEqual($result['Tag'][0]['tag'], 'tag1'); | |
2676 $this->assertEqual(count($result['Comment']), 1); | |
2677 $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1); | |
2678 } | |
2679 | |
2680 /** | |
2681 * Test SaveAll with Habtm relations and extra join table fields | |
2682 * | |
2683 * @access public | |
2684 * @return void | |
2685 */ | |
2686 function testSaveAllHabtmWithExtraJoinTableFields() { | |
2687 $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); | |
2688 | |
2689 $data = array( | |
2690 'Something' => array( | |
2691 'id' => 4, | |
2692 'title' => 'Extra Fields', | |
2693 'body' => 'Extra Fields Body', | |
2694 'published' => '1' | |
2695 ), | |
2696 'SomethingElse' => array( | |
2697 array('something_else_id' => 1, 'doomed' => '1'), | |
2698 array('something_else_id' => 2, 'doomed' => '0'), | |
2699 array('something_else_id' => 3, 'doomed' => '1') | |
2700 ) | |
2701 ); | |
2702 | |
2703 $Something =& new Something(); | |
2704 $result = $Something->saveAll($data); | |
2705 $this->assertTrue($result); | |
2706 $result = $Something->read(); | |
2707 | |
2708 $this->assertEqual(count($result['SomethingElse']), 3); | |
2709 $this->assertTrue(Set::matches('/Something[id=4]', $result)); | |
2710 | |
2711 $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result)); | |
2712 $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result)); | |
2713 $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result)); | |
2714 | |
2715 $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result)); | |
2716 $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result)); | |
2717 $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result)); | |
2718 | |
2719 $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result)); | |
2720 $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result)); | |
2721 $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result)); | |
2722 } | |
2723 | |
2724 /** | |
2725 * testSaveAllHasOne method | |
2726 * | |
2727 * @access public | |
2728 * @return void | |
2729 */ | |
2730 function testSaveAllHasOne() { | |
2731 $model = new Comment(); | |
2732 $model->deleteAll(true); | |
2733 $this->assertEqual($model->find('all'), array()); | |
2734 | |
2735 $model->Attachment->deleteAll(true); | |
2736 $this->assertEqual($model->Attachment->find('all'), array()); | |
2737 | |
2738 $this->assertTrue($model->saveAll(array( | |
2739 'Comment' => array( | |
2740 'comment' => 'Comment with attachment', | |
2741 'article_id' => 1, | |
2742 'user_id' => 1 | |
2743 ), | |
2744 'Attachment' => array( | |
2745 'attachment' => 'some_file.zip' | |
2746 )))); | |
2747 $result = $model->find('all', array('fields' => array( | |
2748 'Comment.id', 'Comment.comment', 'Attachment.id', | |
2749 'Attachment.comment_id', 'Attachment.attachment' | |
2750 ))); | |
2751 $expected = array(array( | |
2752 'Comment' => array( | |
2753 'id' => '1', | |
2754 'comment' => 'Comment with attachment' | |
2755 ), | |
2756 'Attachment' => array( | |
2757 'id' => '1', | |
2758 'comment_id' => '1', | |
2759 'attachment' => 'some_file.zip' | |
2760 ))); | |
2761 $this->assertEqual($result, $expected); | |
2762 | |
2763 | |
2764 $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false); | |
2765 $data = array( | |
2766 'Comment' => array( | |
2767 'comment' => 'Comment with attachment', | |
2768 'article_id' => 1, | |
2769 'user_id' => 1 | |
2770 ), | |
2771 'Attachment' => array( | |
2772 'attachment' => 'some_file.zip' | |
2773 )); | |
2774 $this->assertTrue($model->saveAll($data, array('validate' => 'first'))); | |
2775 } | |
2776 | |
2777 /** | |
2778 * testSaveAllBelongsTo method | |
2779 * | |
2780 * @access public | |
2781 * @return void | |
2782 */ | |
2783 function testSaveAllBelongsTo() { | |
2784 $model = new Comment(); | |
2785 $model->deleteAll(true); | |
2786 $this->assertEqual($model->find('all'), array()); | |
2787 | |
2788 $model->Article->deleteAll(true); | |
2789 $this->assertEqual($model->Article->find('all'), array()); | |
2790 | |
2791 $this->assertTrue($model->saveAll(array( | |
2792 'Comment' => array( | |
2793 'comment' => 'Article comment', | |
2794 'article_id' => 1, | |
2795 'user_id' => 1 | |
2796 ), | |
2797 'Article' => array( | |
2798 'title' => 'Model Associations 101', | |
2799 'user_id' => 1 | |
2800 )))); | |
2801 $result = $model->find('all', array('fields' => array( | |
2802 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title' | |
2803 ))); | |
2804 $expected = array(array( | |
2805 'Comment' => array( | |
2806 'id' => '1', | |
2807 'article_id' => '1', | |
2808 'comment' => 'Article comment' | |
2809 ), | |
2810 'Article' => array( | |
2811 'id' => '1', | |
2812 'title' => 'Model Associations 101' | |
2813 ))); | |
2814 $this->assertEqual($result, $expected); | |
2815 } | |
2816 | |
2817 /** | |
2818 * testSaveAllHasOneValidation method | |
2819 * | |
2820 * @access public | |
2821 * @return void | |
2822 */ | |
2823 function testSaveAllHasOneValidation() { | |
2824 $model = new Comment(); | |
2825 $model->deleteAll(true); | |
2826 $this->assertEqual($model->find('all'), array()); | |
2827 | |
2828 $model->Attachment->deleteAll(true); | |
2829 $this->assertEqual($model->Attachment->find('all'), array()); | |
2830 | |
2831 $model->validate = array('comment' => 'notEmpty'); | |
2832 $model->Attachment->validate = array('attachment' => 'notEmpty'); | |
2833 $model->Attachment->bindModel(array('belongsTo' => array('Comment'))); | |
2834 | |
2835 $this->assertFalse($model->saveAll( | |
2836 array( | |
2837 'Comment' => array( | |
2838 'comment' => '', | |
2839 'article_id' => 1, | |
2840 'user_id' => 1 | |
2841 ), | |
2842 'Attachment' => array('attachment' => '') | |
2843 ), | |
2844 array('validate' => 'first') | |
2845 )); | |
2846 $expected = array( | |
2847 'Comment' => array('comment' => 'This field cannot be left blank'), | |
2848 'Attachment' => array('attachment' => 'This field cannot be left blank') | |
2849 ); | |
2850 $this->assertEqual($model->validationErrors, $expected['Comment']); | |
2851 $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']); | |
2852 | |
2853 $this->assertFalse($model->saveAll( | |
2854 array( | |
2855 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1), | |
2856 'Attachment' => array('attachment' => '') | |
2857 ), | |
2858 array('validate' => 'only') | |
2859 )); | |
2860 $this->assertEqual($model->validationErrors, $expected['Comment']); | |
2861 $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']); | |
2862 } | |
2863 | |
2864 /** | |
2865 * testSaveAllAtomic method | |
2866 * | |
2867 * @access public | |
2868 * @return void | |
2869 */ | |
2870 function testSaveAllAtomic() { | |
2871 $this->loadFixtures('Article', 'User'); | |
2872 $TestModel =& new Article(); | |
2873 | |
2874 $result = $TestModel->saveAll(array( | |
2875 'Article' => array( | |
2876 'title' => 'Post with Author', | |
2877 'body' => 'This post will be saved with an author', | |
2878 'user_id' => 2 | |
2879 ), | |
2880 'Comment' => array( | |
2881 array('comment' => 'First new comment', 'user_id' => 2)) | |
2882 ), array('atomic' => false)); | |
2883 | |
2884 $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true))); | |
2885 | |
2886 $result = $TestModel->saveAll(array( | |
2887 array( | |
2888 'id' => '1', | |
2889 'title' => 'Baleeted First Post', | |
2890 'body' => 'Baleeted!', | |
2891 'published' => 'N' | |
2892 ), | |
2893 array( | |
2894 'id' => '2', | |
2895 'title' => 'Just update the title' | |
2896 ), | |
2897 array( | |
2898 'title' => 'Creating a fourth post', | |
2899 'body' => 'Fourth post body', | |
2900 'user_id' => 2 | |
2901 ) | |
2902 ), array('atomic' => false)); | |
2903 $this->assertIdentical($result, array(true, true, true)); | |
2904 | |
2905 $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); | |
2906 $result = $TestModel->saveAll(array( | |
2907 array( | |
2908 'id' => '1', | |
2909 'title' => 'Un-Baleeted First Post', | |
2910 'body' => 'Not Baleeted!', | |
2911 'published' => 'Y' | |
2912 ), | |
2913 array( | |
2914 'id' => '2', | |
2915 'title' => '', | |
2916 'body' => 'Trying to get away with an empty title' | |
2917 ) | |
2918 ), array('validate' => true, 'atomic' => false)); | |
2919 | |
2920 $this->assertIdentical($result, array(true, false)); | |
2921 | |
2922 $result = $TestModel->saveAll(array( | |
2923 'Article' => array('id' => 2), | |
2924 'Comment' => array( | |
2925 array( | |
2926 'comment' => 'First new comment', | |
2927 'published' => 'Y', | |
2928 'user_id' => 1 | |
2929 ), | |
2930 array( | |
2931 'comment' => 'Second new comment', | |
2932 'published' => 'Y', | |
2933 'user_id' => 2 | |
2934 )) | |
2935 ), array('validate' => true, 'atomic' => false)); | |
2936 $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true))); | |
2937 } | |
2938 | |
2939 /** | |
2940 * testSaveAllHasMany method | |
2941 * | |
2942 * @access public | |
2943 * @return void | |
2944 */ | |
2945 function testSaveAllHasMany() { | |
2946 $this->loadFixtures('Article', 'Comment'); | |
2947 $TestModel =& new Article(); | |
2948 $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); | |
2949 | |
2950 $result = $TestModel->saveAll(array( | |
2951 'Article' => array('id' => 2), | |
2952 'Comment' => array( | |
2953 array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1), | |
2954 array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2) | |
2955 ) | |
2956 )); | |
2957 $this->assertTrue($result); | |
2958 | |
2959 $result = $TestModel->findById(2); | |
2960 $expected = array( | |
2961 'First Comment for Second Article', | |
2962 'Second Comment for Second Article', | |
2963 'First new comment', | |
2964 'Second new comment' | |
2965 ); | |
2966 $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); | |
2967 | |
2968 $result = $TestModel->saveAll( | |
2969 array( | |
2970 'Article' => array('id' => 2), | |
2971 'Comment' => array( | |
2972 array( | |
2973 'comment' => 'Third new comment', | |
2974 'published' => 'Y', | |
2975 'user_id' => 1 | |
2976 ))), | |
2977 array('atomic' => false) | |
2978 ); | |
2979 $this->assertTrue($result); | |
2980 | |
2981 $result = $TestModel->findById(2); | |
2982 $expected = array( | |
2983 'First Comment for Second Article', | |
2984 'Second Comment for Second Article', | |
2985 'First new comment', | |
2986 'Second new comment', | |
2987 'Third new comment' | |
2988 ); | |
2989 $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); | |
2990 | |
2991 $TestModel->beforeSaveReturn = false; | |
2992 $result = $TestModel->saveAll( | |
2993 array( | |
2994 'Article' => array('id' => 2), | |
2995 'Comment' => array( | |
2996 array( | |
2997 'comment' => 'Fourth new comment', | |
2998 'published' => 'Y', | |
2999 'user_id' => 1 | |
3000 ))), | |
3001 array('atomic' => false) | |
3002 ); | |
3003 $this->assertEqual($result, array('Article' => false)); | |
3004 | |
3005 $result = $TestModel->findById(2); | |
3006 $expected = array( | |
3007 'First Comment for Second Article', | |
3008 'Second Comment for Second Article', | |
3009 'First new comment', | |
3010 'Second new comment', | |
3011 'Third new comment' | |
3012 ); | |
3013 $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); | |
3014 } | |
3015 | |
3016 /** | |
3017 * testSaveAllHasManyValidation method | |
3018 * | |
3019 * @access public | |
3020 * @return void | |
3021 */ | |
3022 function testSaveAllHasManyValidation() { | |
3023 $this->loadFixtures('Article', 'Comment'); | |
3024 $TestModel =& new Article(); | |
3025 $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); | |
3026 $TestModel->Comment->validate = array('comment' => 'notEmpty'); | |
3027 | |
3028 $result = $TestModel->saveAll(array( | |
3029 'Article' => array('id' => 2), | |
3030 'Comment' => array( | |
3031 array('comment' => '', 'published' => 'Y', 'user_id' => 1), | |
3032 ) | |
3033 ), array('validate' => true)); | |
3034 $expected = array('Comment' => array(false)); | |
3035 $this->assertEqual($result, $expected); | |
3036 | |
3037 $expected = array('Comment' => array( | |
3038 array('comment' => 'This field cannot be left blank') | |
3039 )); | |
3040 $this->assertEqual($TestModel->validationErrors, $expected); | |
3041 $expected = array( | |
3042 array('comment' => 'This field cannot be left blank') | |
3043 ); | |
3044 $this->assertEqual($TestModel->Comment->validationErrors, $expected); | |
3045 | |
3046 $result = $TestModel->saveAll(array( | |
3047 'Article' => array('id' => 2), | |
3048 'Comment' => array( | |
3049 array( | |
3050 'comment' => '', | |
3051 'published' => 'Y', | |
3052 'user_id' => 1 | |
3053 )) | |
3054 ), array('validate' => 'first')); | |
3055 $this->assertFalse($result); | |
3056 } | |
3057 | |
3058 /** | |
3059 * test saveAll with transactions and ensure there is no missing rollback. | |
3060 * | |
3061 * @return void | |
3062 */ | |
3063 function testSaveAllManyRowsTransactionNoRollback() { | |
3064 $this->loadFixtures('Post'); | |
3065 | |
3066 Mock::generate('DboSource', 'MockTransactionDboSource'); | |
3067 $db = ConnectionManager::create('mock_transaction', array( | |
3068 'datasource' => 'MockTransactionDbo', | |
3069 )); | |
3070 $db->expectOnce('rollback'); | |
3071 | |
3072 $Post =& new Post(); | |
3073 $Post->useDbConfig = 'mock_transaction'; | |
3074 | |
3075 $Post->validate = array( | |
3076 'title' => array('rule' => array('notEmpty')) | |
3077 ); | |
3078 | |
3079 $data = array( | |
3080 array('author_id' => 1, 'title' => 'New Fourth Post'), | |
3081 array('author_id' => 1, 'title' => '') | |
3082 ); | |
3083 $Post->saveAll($data, array('atomic' => true)); | |
3084 } | |
3085 | |
3086 /** | |
3087 * test saveAll with transactions and ensure there is no missing rollback. | |
3088 * | |
3089 * @return void | |
3090 */ | |
3091 function testSaveAllAssociatedTransactionNoRollback() { | |
3092 $testDb = ConnectionManager::getDataSource('test_suite'); | |
3093 | |
3094 Mock::generate('DboSource', 'MockTransactionAssociatedDboSource'); | |
3095 $db = ConnectionManager::create('mock_transaction_assoc', array( | |
3096 'datasource' => 'MockTransactionAssociatedDbo', | |
3097 )); | |
3098 $db->columns = $testDb->columns; | |
3099 | |
3100 $db->expectOnce('rollback'); | |
3101 | |
3102 $Post =& new Post(); | |
3103 $Post->useDbConfig = 'mock_transaction_assoc'; | |
3104 $Post->Author->useDbConfig = 'mock_transaction_assoc'; | |
3105 | |
3106 $Post->Author->validate = array( | |
3107 'user' => array('rule' => array('notEmpty')) | |
3108 ); | |
3109 | |
3110 $data = array( | |
3111 'Post' => array( | |
3112 'title' => 'New post', | |
3113 'body' => 'Content', | |
3114 'published' => 'Y' | |
3115 ), | |
3116 'Author' => array( | |
3117 'user' => '', | |
3118 'password' => "sekret" | |
3119 ) | |
3120 ); | |
3121 $Post->saveAll($data); | |
3122 } | |
3123 | |
3124 /** | |
3125 * test saveAll with nested saveAll call. | |
3126 * | |
3127 * @return void | |
3128 */ | |
3129 function testSaveAllNestedSaveAll() { | |
3130 $this->loadFixtures('Sample'); | |
3131 $TransactionTestModel =& new TransactionTestModel(); | |
3132 | |
3133 $data = array( | |
3134 array('apple_id' => 1, 'name' => 'sample5'), | |
3135 ); | |
3136 | |
3137 $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true))); | |
3138 } | |
3139 | |
3140 /** | |
3141 * testSaveAllTransaction method | |
3142 * | |
3143 * @access public | |
3144 * @return void | |
3145 */ | |
3146 function testSaveAllTransaction() { | |
3147 $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); | |
3148 $TestModel =& new Post(); | |
3149 | |
3150 $TestModel->validate = array('title' => 'notEmpty'); | |
3151 $data = array( | |
3152 array('author_id' => 1, 'title' => 'New Fourth Post'), | |
3153 array('author_id' => 1, 'title' => 'New Fifth Post'), | |
3154 array('author_id' => 1, 'title' => '') | |
3155 ); | |
3156 $ts = date('Y-m-d H:i:s'); | |
3157 $this->assertFalse($TestModel->saveAll($data)); | |
3158 | |
3159 $result = $TestModel->find('all', array('recursive' => -1)); | |
3160 $expected = array( | |
3161 array('Post' => array( | |
3162 'id' => '1', | |
3163 'author_id' => 1, | |
3164 'title' => 'First Post', | |
3165 'body' => 'First Post Body', | |
3166 'published' => 'Y', | |
3167 'created' => '2007-03-18 10:39:23', | |
3168 'updated' => '2007-03-18 10:41:31' | |
3169 )), | |
3170 array('Post' => array( | |
3171 'id' => '2', | |
3172 'author_id' => 3, | |
3173 'title' => 'Second Post', | |
3174 'body' => 'Second Post Body', | |
3175 'published' => 'Y', | |
3176 'created' => '2007-03-18 10:41:23', | |
3177 'updated' => '2007-03-18 10:43:31' | |
3178 )), | |
3179 array('Post' => array( | |
3180 'id' => '3', | |
3181 'author_id' => 1, | |
3182 'title' => 'Third Post', | |
3183 'body' => 'Third Post Body', | |
3184 'published' => 'Y', | |
3185 'created' => '2007-03-18 10:43:23', | |
3186 'updated' => '2007-03-18 10:45:31' | |
3187 ))); | |
3188 | |
3189 if (count($result) != 3) { | |
3190 // Database doesn't support transactions | |
3191 $expected[] = array( | |
3192 'Post' => array( | |
3193 'id' => '4', | |
3194 'author_id' => 1, | |
3195 'title' => 'New Fourth Post', | |
3196 'body' => null, | |
3197 'published' => 'N', | |
3198 'created' => $ts, | |
3199 'updated' => $ts | |
3200 )); | |
3201 | |
3202 $expected[] = array( | |
3203 'Post' => array( | |
3204 'id' => '5', | |
3205 'author_id' => 1, | |
3206 'title' => 'New Fifth Post', | |
3207 'body' => null, | |
3208 'published' => 'N', | |
3209 'created' => $ts, | |
3210 'updated' => $ts | |
3211 )); | |
3212 | |
3213 $this->assertEqual($result, $expected); | |
3214 // Skip the rest of the transactional tests | |
3215 return; | |
3216 } | |
3217 | |
3218 $this->assertEqual($result, $expected); | |
3219 | |
3220 $data = array( | |
3221 array('author_id' => 1, 'title' => 'New Fourth Post'), | |
3222 array('author_id' => 1, 'title' => ''), | |
3223 array('author_id' => 1, 'title' => 'New Sixth Post') | |
3224 ); | |
3225 $ts = date('Y-m-d H:i:s'); | |
3226 $this->assertFalse($TestModel->saveAll($data)); | |
3227 | |
3228 $result = $TestModel->find('all', array('recursive' => -1)); | |
3229 $expected = array( | |
3230 array('Post' => array( | |
3231 'id' => '1', | |
3232 'author_id' => 1, | |
3233 'title' => 'First Post', | |
3234 'body' => 'First Post Body', | |
3235 'published' => 'Y', | |
3236 'created' => '2007-03-18 10:39:23', | |
3237 'updated' => '2007-03-18 10:41:31' | |
3238 )), | |
3239 array('Post' => array( | |
3240 'id' => '2', | |
3241 'author_id' => 3, | |
3242 'title' => 'Second Post', | |
3243 'body' => 'Second Post Body', | |
3244 'published' => 'Y', | |
3245 'created' => '2007-03-18 10:41:23', | |
3246 'updated' => '2007-03-18 10:43:31' | |
3247 )), | |
3248 array('Post' => array( | |
3249 'id' => '3', | |
3250 'author_id' => 1, | |
3251 'title' => 'Third Post', | |
3252 'body' => 'Third Post Body', | |
3253 'published' => 'Y', | |
3254 'created' => '2007-03-18 10:43:23', | |
3255 'updated' => '2007-03-18 10:45:31' | |
3256 ))); | |
3257 | |
3258 if (count($result) != 3) { | |
3259 // Database doesn't support transactions | |
3260 $expected[] = array( | |
3261 'Post' => array( | |
3262 'id' => '4', | |
3263 'author_id' => 1, | |
3264 'title' => 'New Fourth Post', | |
3265 'body' => 'Third Post Body', | |
3266 'published' => 'N', | |
3267 'created' => $ts, | |
3268 'updated' => $ts | |
3269 )); | |
3270 | |
3271 $expected[] = array( | |
3272 'Post' => array( | |
3273 'id' => '5', | |
3274 'author_id' => 1, | |
3275 'title' => 'Third Post', | |
3276 'body' => 'Third Post Body', | |
3277 'published' => 'N', | |
3278 'created' => $ts, | |
3279 'updated' => $ts | |
3280 )); | |
3281 } | |
3282 $this->assertEqual($result, $expected); | |
3283 | |
3284 $TestModel->validate = array('title' => 'notEmpty'); | |
3285 $data = array( | |
3286 array('author_id' => 1, 'title' => 'New Fourth Post'), | |
3287 array('author_id' => 1, 'title' => 'New Fifth Post'), | |
3288 array('author_id' => 1, 'title' => 'New Sixth Post') | |
3289 ); | |
3290 $this->assertTrue($TestModel->saveAll($data)); | |
3291 | |
3292 $result = $TestModel->find('all', array( | |
3293 'recursive' => -1, | |
3294 'fields' => array('author_id', 'title','body','published') | |
3295 )); | |
3296 | |
3297 $expected = array( | |
3298 array('Post' => array( | |
3299 'author_id' => 1, | |
3300 'title' => 'First Post', | |
3301 'body' => 'First Post Body', | |
3302 'published' => 'Y' | |
3303 )), | |
3304 array('Post' => array( | |
3305 'author_id' => 3, | |
3306 'title' => 'Second Post', | |
3307 'body' => 'Second Post Body', | |
3308 'published' => 'Y' | |
3309 )), | |
3310 array('Post' => array( | |
3311 'author_id' => 1, | |
3312 'title' => 'Third Post', | |
3313 'body' => 'Third Post Body', | |
3314 'published' => 'Y' | |
3315 )), | |
3316 array('Post' => array( | |
3317 'author_id' => 1, | |
3318 'title' => 'New Fourth Post', | |
3319 'body' => '', | |
3320 'published' => 'N' | |
3321 )), | |
3322 array('Post' => array( | |
3323 'author_id' => 1, | |
3324 'title' => 'New Fifth Post', | |
3325 'body' => '', | |
3326 'published' => 'N' | |
3327 )), | |
3328 array('Post' => array( | |
3329 'author_id' => 1, | |
3330 'title' => 'New Sixth Post', | |
3331 'body' => '', | |
3332 'published' => 'N' | |
3333 ))); | |
3334 $this->assertEqual($result, $expected); | |
3335 } | |
3336 | |
3337 /** | |
3338 * testSaveAllValidation method | |
3339 * | |
3340 * @access public | |
3341 * @return void | |
3342 */ | |
3343 function testSaveAllValidation() { | |
3344 $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); | |
3345 $TestModel =& new Post(); | |
3346 | |
3347 $data = array( | |
3348 array( | |
3349 'id' => '1', | |
3350 'title' => 'Baleeted First Post', | |
3351 'body' => 'Baleeted!', | |
3352 'published' => 'N' | |
3353 ), | |
3354 array( | |
3355 'id' => '2', | |
3356 'title' => 'Just update the title' | |
3357 ), | |
3358 array( | |
3359 'title' => 'Creating a fourth post', | |
3360 'body' => 'Fourth post body', | |
3361 'author_id' => 2 | |
3362 )); | |
3363 | |
3364 $this->assertTrue($TestModel->saveAll($data)); | |
3365 | |
3366 $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); | |
3367 $ts = date('Y-m-d H:i:s'); | |
3368 $expected = array( | |
3369 array( | |
3370 'Post' => array( | |
3371 'id' => '1', | |
3372 'author_id' => '1', | |
3373 'title' => 'Baleeted First Post', | |
3374 'body' => 'Baleeted!', | |
3375 'published' => 'N', | |
3376 'created' => '2007-03-18 10:39:23', | |
3377 'updated' => $ts | |
3378 )), | |
3379 array( | |
3380 'Post' => array( | |
3381 'id' => '2', | |
3382 'author_id' => '3', | |
3383 'title' => 'Just update the title', | |
3384 'body' => 'Second Post Body', | |
3385 'published' => 'Y', | |
3386 'created' => '2007-03-18 10:41:23', 'updated' => $ts | |
3387 )), | |
3388 array( | |
3389 'Post' => array( | |
3390 'id' => '3', | |
3391 'author_id' => '1', | |
3392 'title' => 'Third Post', | |
3393 'body' => 'Third Post Body', | |
3394 'published' => 'Y', | |
3395 'created' => '2007-03-18 10:43:23', | |
3396 'updated' => '2007-03-18 10:45:31' | |
3397 )), | |
3398 array( | |
3399 'Post' => array( | |
3400 'id' => '4', | |
3401 'author_id' => '2', | |
3402 'title' => 'Creating a fourth post', | |
3403 'body' => 'Fourth post body', | |
3404 'published' => 'N', | |
3405 'created' => $ts, | |
3406 'updated' => $ts | |
3407 ))); | |
3408 $this->assertEqual($result, $expected); | |
3409 | |
3410 $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); | |
3411 $data = array( | |
3412 array( | |
3413 'id' => '1', | |
3414 'title' => 'Un-Baleeted First Post', | |
3415 'body' => 'Not Baleeted!', | |
3416 'published' => 'Y' | |
3417 ), | |
3418 array( | |
3419 'id' => '2', | |
3420 'title' => '', | |
3421 'body' => 'Trying to get away with an empty title' | |
3422 )); | |
3423 $result = $TestModel->saveAll($data); | |
3424 $this->assertEqual($result, false); | |
3425 | |
3426 $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); | |
3427 $errors = array(1 => array('title' => 'This field cannot be left blank')); | |
3428 $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result); | |
3429 if (!$transactionWorked) { | |
3430 $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result)); | |
3431 $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result)); | |
3432 } | |
3433 | |
3434 $this->assertEqual($TestModel->validationErrors, $errors); | |
3435 | |
3436 $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); | |
3437 $data = array( | |
3438 array( | |
3439 'id' => '1', | |
3440 'title' => 'Un-Baleeted First Post', | |
3441 'body' => 'Not Baleeted!', | |
3442 'published' => 'Y' | |
3443 ), | |
3444 array( | |
3445 'id' => '2', | |
3446 'title' => '', | |
3447 'body' => 'Trying to get away with an empty title' | |
3448 )); | |
3449 $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false)); | |
3450 $this->assertEqual($result, array(true, false)); | |
3451 $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); | |
3452 $errors = array(1 => array('title' => 'This field cannot be left blank')); | |
3453 $newTs = date('Y-m-d H:i:s'); | |
3454 $expected = array( | |
3455 array( | |
3456 'Post' => array( | |
3457 'id' => '1', | |
3458 'author_id' => '1', | |
3459 'title' => 'Un-Baleeted First Post', | |
3460 'body' => 'Not Baleeted!', | |
3461 'published' => 'Y', | |
3462 'created' => '2007-03-18 10:39:23', | |
3463 'updated' => $newTs | |
3464 )), | |
3465 array( | |
3466 'Post' => array( | |
3467 'id' => '2', | |
3468 'author_id' => '3', | |
3469 'title' => 'Just update the title', | |
3470 'body' => 'Second Post Body', | |
3471 'published' => 'Y', | |
3472 'created' => '2007-03-18 10:41:23', | |
3473 'updated' => $ts | |
3474 )), | |
3475 array( | |
3476 'Post' => array( | |
3477 'id' => '3', | |
3478 'author_id' => '1', | |
3479 'title' => 'Third Post', | |
3480 'body' => 'Third Post Body', | |
3481 'published' => 'Y', | |
3482 'created' => '2007-03-18 10:43:23', | |
3483 'updated' => '2007-03-18 10:45:31' | |
3484 )), | |
3485 array( | |
3486 'Post' => array( | |
3487 'id' => '4', | |
3488 'author_id' => '2', | |
3489 'title' => 'Creating a fourth post', | |
3490 'body' => 'Fourth post body', | |
3491 'published' => 'N', | |
3492 'created' => $ts, | |
3493 'updated' => $ts | |
3494 ))); | |
3495 $this->assertEqual($result, $expected); | |
3496 $this->assertEqual($TestModel->validationErrors, $errors); | |
3497 | |
3498 $data = array( | |
3499 array( | |
3500 'id' => '1', | |
3501 'title' => 'Re-Baleeted First Post', | |
3502 'body' => 'Baleeted!', | |
3503 'published' => 'N' | |
3504 ), | |
3505 array( | |
3506 'id' => '2', | |
3507 'title' => '', | |
3508 'body' => 'Trying to get away with an empty title' | |
3509 )); | |
3510 $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first'))); | |
3511 | |
3512 $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); | |
3513 $this->assertEqual($result, $expected); | |
3514 $this->assertEqual($TestModel->validationErrors, $errors); | |
3515 | |
3516 $data = array( | |
3517 array( | |
3518 'title' => 'First new post', | |
3519 'body' => 'Woohoo!', | |
3520 'published' => 'Y' | |
3521 ), | |
3522 array( | |
3523 'title' => 'Empty body', | |
3524 'body' => '' | |
3525 )); | |
3526 | |
3527 $TestModel->validate['body'] = 'notEmpty'; | |
3528 } | |
3529 | |
3530 /** | |
3531 * testSaveAllValidationOnly method | |
3532 * | |
3533 * @access public | |
3534 * @return void | |
3535 */ | |
3536 function testSaveAllValidationOnly() { | |
3537 $TestModel =& new Comment(); | |
3538 $TestModel->Attachment->validate = array('attachment' => 'notEmpty'); | |
3539 | |
3540 $data = array( | |
3541 'Comment' => array( | |
3542 'comment' => 'This is the comment' | |
3543 ), | |
3544 'Attachment' => array( | |
3545 'attachment' => '' | |
3546 ) | |
3547 ); | |
3548 | |
3549 $result = $TestModel->saveAll($data, array('validate' => 'only')); | |
3550 $this->assertFalse($result); | |
3551 | |
3552 $TestModel =& new Article(); | |
3553 $TestModel->validate = array('title' => 'notEmpty'); | |
3554 $result = $TestModel->saveAll( | |
3555 array( | |
3556 0 => array('title' => ''), | |
3557 1 => array('title' => 'title 1'), | |
3558 2 => array('title' => 'title 2'), | |
3559 ), | |
3560 array('validate'=>'only') | |
3561 ); | |
3562 $this->assertFalse($result); | |
3563 $expected = array( | |
3564 0 => array('title' => 'This field cannot be left blank'), | |
3565 ); | |
3566 $this->assertEqual($TestModel->validationErrors, $expected); | |
3567 | |
3568 $result = $TestModel->saveAll( | |
3569 array( | |
3570 0 => array('title' => 'title 0'), | |
3571 1 => array('title' => ''), | |
3572 2 => array('title' => 'title 2'), | |
3573 ), | |
3574 array('validate'=>'only') | |
3575 ); | |
3576 $this->assertFalse($result); | |
3577 $expected = array( | |
3578 1 => array('title' => 'This field cannot be left blank'), | |
3579 ); | |
3580 $this->assertEqual($TestModel->validationErrors, $expected); | |
3581 } | |
3582 | |
3583 /** | |
3584 * testSaveAllValidateFirst method | |
3585 * | |
3586 * @access public | |
3587 * @return void | |
3588 */ | |
3589 function testSaveAllValidateFirst() { | |
3590 $model =& new Article(); | |
3591 $model->deleteAll(true); | |
3592 | |
3593 $model->Comment->validate = array('comment' => 'notEmpty'); | |
3594 $result = $model->saveAll(array( | |
3595 'Article' => array( | |
3596 'title' => 'Post with Author', | |
3597 'body' => 'This post will be saved author' | |
3598 ), | |
3599 'Comment' => array( | |
3600 array('comment' => 'First new comment'), | |
3601 array('comment' => '') | |
3602 ) | |
3603 ), array('validate' => 'first')); | |
3604 | |
3605 $this->assertFalse($result); | |
3606 | |
3607 $result = $model->find('all'); | |
3608 $this->assertEqual($result, array()); | |
3609 $expected = array('Comment' => array( | |
3610 1 => array('comment' => 'This field cannot be left blank') | |
3611 )); | |
3612 | |
3613 $this->assertEqual($model->Comment->validationErrors, $expected['Comment']); | |
3614 | |
3615 $this->assertIdentical($model->Comment->find('count'), 0); | |
3616 | |
3617 $result = $model->saveAll( | |
3618 array( | |
3619 'Article' => array( | |
3620 'title' => 'Post with Author', | |
3621 'body' => 'This post will be saved with an author', | |
3622 'user_id' => 2 | |
3623 ), | |
3624 'Comment' => array( | |
3625 array( | |
3626 'comment' => 'Only new comment', | |
3627 'user_id' => 2 | |
3628 ))), | |
3629 array('validate' => 'first') | |
3630 ); | |
3631 | |
3632 $this->assertIdentical($result, true); | |
3633 | |
3634 $result = $model->Comment->find('all'); | |
3635 $this->assertIdentical(count($result), 1); | |
3636 $result = Set::extract('/Comment/article_id', $result); | |
3637 $this->assertTrue($result[0] === 1 || $result[0] === '1'); | |
3638 | |
3639 | |
3640 $model->deleteAll(true); | |
3641 $data = array( | |
3642 'Article' => array( | |
3643 'title' => 'Post with Author saveAlled from comment', | |
3644 'body' => 'This post will be saved with an author', | |
3645 'user_id' => 2 | |
3646 ), | |
3647 'Comment' => array( | |
3648 'comment' => 'Only new comment', 'user_id' => 2 | |
3649 )); | |
3650 | |
3651 $result = $model->Comment->saveAll($data, array('validate' => 'first')); | |
3652 $this->assertTrue($result); | |
3653 | |
3654 $result = $model->find('all'); | |
3655 $this->assertEqual( | |
3656 $result[0]['Article']['title'], | |
3657 'Post with Author saveAlled from comment' | |
3658 ); | |
3659 $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment'); | |
3660 } | |
3661 | |
3662 /** | |
3663 * test saveAll()'s return is correct when using atomic = false and validate = first. | |
3664 * | |
3665 * @return void | |
3666 */ | |
3667 function testSaveAllValidateFirstAtomicFalse() { | |
3668 $Something =& new Something(); | |
3669 $invalidData = array( | |
3670 array( | |
3671 'title' => 'foo', | |
3672 'body' => 'bar', | |
3673 'published' => 'baz', | |
3674 ), | |
3675 array( | |
3676 'body' => 3, | |
3677 'published' =>'sd', | |
3678 ), | |
3679 ); | |
3680 $Something->create(); | |
3681 $Something->validate = array( | |
3682 'title' => array( | |
3683 'rule' => 'alphaNumeric', | |
3684 'required' => true, | |
3685 ), | |
3686 'body' => array( | |
3687 'rule' => 'alphaNumeric', | |
3688 'required' => true, | |
3689 'allowEmpty' => true, | |
3690 ), | |
3691 ); | |
3692 $result = $Something->saveAll($invalidData, array( | |
3693 'atomic' => false, | |
3694 'validate' => 'first', | |
3695 )); | |
3696 $expected = array(true, false); | |
3697 $this->assertEqual($result, $expected); | |
3698 | |
3699 $Something =& new Something(); | |
3700 $validData = array( | |
3701 array( | |
3702 'title' => 'title value', | |
3703 'body' => 'body value', | |
3704 'published' => 'baz', | |
3705 ), | |
3706 array( | |
3707 'title' => 'valid', | |
3708 'body' => 'this body', | |
3709 'published' =>'sd', | |
3710 ), | |
3711 ); | |
3712 $Something->create(); | |
3713 $result = $Something->saveAll($validData, array( | |
3714 'atomic' => false, | |
3715 'validate' => 'first', | |
3716 )); | |
3717 $expected = array(true, true); | |
3718 $this->assertEqual($result, $expected); | |
3719 } | |
3720 | |
3721 /** | |
3722 * testUpdateWithCalculation method | |
3723 * | |
3724 * @access public | |
3725 * @return void | |
3726 */ | |
3727 function testUpdateWithCalculation() { | |
3728 $this->loadFixtures('DataTest'); | |
3729 $model =& new DataTest(); | |
3730 $model->deleteAll(true); | |
3731 $result = $model->saveAll(array( | |
3732 array('count' => 5, 'float' => 1.1), | |
3733 array('count' => 3, 'float' => 1.2), | |
3734 array('count' => 4, 'float' => 1.3), | |
3735 array('count' => 1, 'float' => 2.0), | |
3736 )); | |
3737 $this->assertTrue($result); | |
3738 | |
3739 $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); | |
3740 $this->assertEqual($result, array(5, 3, 4, 1)); | |
3741 | |
3742 $this->assertTrue($model->updateAll(array('count' => 'count + 2'))); | |
3743 $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); | |
3744 $this->assertEqual($result, array(7, 5, 6, 3)); | |
3745 | |
3746 $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1'))); | |
3747 $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); | |
3748 $this->assertEqual($result, array(6, 4, 5, 2)); | |
3749 } | |
3750 | |
3751 /** | |
3752 * testSaveAllHasManyValidationOnly method | |
3753 * | |
3754 * @access public | |
3755 * @return void | |
3756 */ | |
3757 function testSaveAllHasManyValidationOnly() { | |
3758 $this->loadFixtures('Article', 'Comment'); | |
3759 $TestModel =& new Article(); | |
3760 $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); | |
3761 $TestModel->Comment->validate = array('comment' => 'notEmpty'); | |
3762 | |
3763 $result = $TestModel->saveAll( | |
3764 array( | |
3765 'Article' => array('id' => 2), | |
3766 'Comment' => array( | |
3767 array( | |
3768 'id' => 1, | |
3769 'comment' => '', | |
3770 'published' => 'Y', | |
3771 'user_id' => 1), | |
3772 array( | |
3773 'id' => 2, | |
3774 'comment' => | |
3775 'comment', | |
3776 'published' => 'Y', | |
3777 'user_id' => 1 | |
3778 ))), | |
3779 array('validate' => 'only') | |
3780 ); | |
3781 $this->assertFalse($result); | |
3782 | |
3783 $result = $TestModel->saveAll( | |
3784 array( | |
3785 'Article' => array('id' => 2), | |
3786 'Comment' => array( | |
3787 array( | |
3788 'id' => 1, | |
3789 'comment' => '', | |
3790 'published' => 'Y', | |
3791 'user_id' => 1 | |
3792 ), | |
3793 array( | |
3794 'id' => 2, | |
3795 'comment' => 'comment', | |
3796 'published' => 'Y', | |
3797 'user_id' => 1 | |
3798 ), | |
3799 array( | |
3800 'id' => 3, | |
3801 'comment' => '', | |
3802 'published' => 'Y', | |
3803 'user_id' => 1 | |
3804 ))), | |
3805 array( | |
3806 'validate' => 'only', | |
3807 'atomic' => false | |
3808 )); | |
3809 $expected = array( | |
3810 'Article' => true, | |
3811 'Comment' => array(false, true, false) | |
3812 ); | |
3813 $this->assertIdentical($result, $expected); | |
3814 | |
3815 $expected = array('Comment' => array( | |
3816 0 => array('comment' => 'This field cannot be left blank'), | |
3817 2 => array('comment' => 'This field cannot be left blank') | |
3818 )); | |
3819 $this->assertEqual($TestModel->validationErrors, $expected); | |
3820 | |
3821 $expected = array( | |
3822 0 => array('comment' => 'This field cannot be left blank'), | |
3823 2 => array('comment' => 'This field cannot be left blank') | |
3824 ); | |
3825 $this->assertEqual($TestModel->Comment->validationErrors, $expected); | |
3826 } | |
3827 | |
3828 /** | |
3829 * TestFindAllWithoutForeignKey | |
3830 * | |
3831 * @link http://code.cakephp.org/tickets/view/69 | |
3832 * @access public | |
3833 * @return void | |
3834 */ | |
3835 function testFindAllForeignKey() { | |
3836 $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll'); | |
3837 $ProductUpdateAll =& new ProductUpdateAll(); | |
3838 | |
3839 $conditions = array('Group.name' => 'group one'); | |
3840 | |
3841 $ProductUpdateAll->bindModel(array( | |
3842 'belongsTo' => array( | |
3843 'Group' => array('className' => 'GroupUpdateAll') | |
3844 ) | |
3845 )); | |
3846 | |
3847 $ProductUpdateAll->belongsTo = array( | |
3848 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id') | |
3849 ); | |
3850 | |
3851 $results = $ProductUpdateAll->find('all', compact('conditions')); | |
3852 $this->assertTrue(!empty($results)); | |
3853 | |
3854 $ProductUpdateAll->bindModel(array('belongsTo'=>array('Group'))); | |
3855 $ProductUpdateAll->belongsTo = array( | |
3856 'Group' => array( | |
3857 'className' => 'GroupUpdateAll', | |
3858 'foreignKey' => false, | |
3859 'conditions' => 'ProductUpdateAll.groupcode = Group.code' | |
3860 )); | |
3861 | |
3862 $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions')); | |
3863 $this->assertTrue(!empty($resultsFkFalse)); | |
3864 $expected = array( | |
3865 '0' => array( | |
3866 'ProductUpdateAll' => array( | |
3867 'id' => 1, | |
3868 'name' => 'product one', | |
3869 'groupcode' => 120, | |
3870 'group_id' => 1), | |
3871 'Group' => array( | |
3872 'id' => 1, | |
3873 'name' => 'group one', | |
3874 'code' => 120) | |
3875 ), | |
3876 '1' => array( | |
3877 'ProductUpdateAll' => array( | |
3878 'id' => 2, | |
3879 'name' => 'product two', | |
3880 'groupcode' => 120, | |
3881 'group_id' => 1), | |
3882 'Group' => array( | |
3883 'id' => 1, | |
3884 'name' => 'group one', | |
3885 'code' => 120) | |
3886 ) | |
3887 | |
3888 ); | |
3889 $this->assertEqual($results, $expected); | |
3890 $this->assertEqual($resultsFkFalse, $expected); | |
3891 } | |
3892 | |
3893 /** | |
3894 * test updateAll with empty values. | |
3895 * | |
3896 * @return void | |
3897 */ | |
3898 function testUpdateAllEmptyValues() { | |
3899 $this->loadFixtures('Author', 'Post'); | |
3900 $model = new Author(); | |
3901 $result = $model->updateAll(array('user' => '""')); | |
3902 $this->assertTrue($result); | |
3903 } | |
3904 | |
3905 /** | |
3906 * testUpdateAllWithJoins | |
3907 * | |
3908 * @link http://code.cakephp.org/tickets/view/69 | |
3909 * @access public | |
3910 * @return void | |
3911 */ | |
3912 function testUpdateAllWithJoins() { | |
3913 $this->skipIf( | |
3914 $this->db->config['driver'] == 'postgres', | |
3915 '%s Currently, there is no way of doing joins in an update statement in postgresql' | |
3916 ); | |
3917 $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll'); | |
3918 $ProductUpdateAll =& new ProductUpdateAll(); | |
3919 | |
3920 $conditions = array('Group.name' => 'group one'); | |
3921 | |
3922 $ProductUpdateAll->bindModel(array('belongsTo' => array( | |
3923 'Group' => array('className' => 'GroupUpdateAll'))) | |
3924 ); | |
3925 | |
3926 $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions); | |
3927 $results = $ProductUpdateAll->find('all', array( | |
3928 'conditions' => array('ProductUpdateAll.name' => 'new product') | |
3929 )); | |
3930 $expected = array( | |
3931 '0' => array( | |
3932 'ProductUpdateAll' => array( | |
3933 'id' => 1, | |
3934 'name' => 'new product', | |
3935 'groupcode' => 120, | |
3936 'group_id' => 1), | |
3937 'Group' => array( | |
3938 'id' => 1, | |
3939 'name' => 'group one', | |
3940 'code' => 120) | |
3941 ), | |
3942 '1' => array( | |
3943 'ProductUpdateAll' => array( | |
3944 'id' => 2, | |
3945 'name' => 'new product', | |
3946 'groupcode' => 120, | |
3947 'group_id' => 1), | |
3948 'Group' => array( | |
3949 'id' => 1, | |
3950 'name' => 'group one', | |
3951 'code' => 120))); | |
3952 | |
3953 $this->assertEqual($results, $expected); | |
3954 } | |
3955 | |
3956 /** | |
3957 * testUpdateAllWithoutForeignKey | |
3958 * | |
3959 * @link http://code.cakephp.org/tickets/view/69 | |
3960 * @access public | |
3961 * @return void | |
3962 */ | |
3963 function testUpdateAllWithoutForeignKey() { | |
3964 $this->skipIf( | |
3965 $this->db->config['driver'] == 'postgres', | |
3966 '%s Currently, there is no way of doing joins in an update statement in postgresql' | |
3967 ); | |
3968 $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll'); | |
3969 $ProductUpdateAll =& new ProductUpdateAll(); | |
3970 | |
3971 $conditions = array('Group.name' => 'group one'); | |
3972 | |
3973 $ProductUpdateAll->bindModel(array('belongsTo' => array( | |
3974 'Group' => array('className' => 'GroupUpdateAll') | |
3975 ))); | |
3976 | |
3977 $ProductUpdateAll->belongsTo = array( | |
3978 'Group' => array( | |
3979 'className' => 'GroupUpdateAll', | |
3980 'foreignKey' => false, | |
3981 'conditions' => 'ProductUpdateAll.groupcode = Group.code' | |
3982 ) | |
3983 ); | |
3984 | |
3985 $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions); | |
3986 $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name'=>'new product'))); | |
3987 $expected = array( | |
3988 '0' => array( | |
3989 'ProductUpdateAll' => array( | |
3990 'id' => 1, | |
3991 'name' => 'new product', | |
3992 'groupcode' => 120, | |
3993 'group_id' => 1), | |
3994 'Group' => array( | |
3995 'id' => 1, | |
3996 'name' => 'group one', | |
3997 'code' => 120) | |
3998 ), | |
3999 '1' => array( | |
4000 'ProductUpdateAll' => array( | |
4001 'id' => 2, | |
4002 'name' => 'new product', | |
4003 'groupcode' => 120, | |
4004 'group_id' => 1), | |
4005 'Group' => array( | |
4006 'id' => 1, | |
4007 'name' => 'group one', | |
4008 'code' => 120))); | |
4009 $this->assertEqual($resultsFkFalse, $expected); | |
4010 } | |
4011 | |
4012 /** | |
4013 * test that saveAll behaves like plain save() when suplied empty data | |
4014 * | |
4015 * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data | |
4016 * @access public | |
4017 * @return void | |
4018 */ | |
4019 function testSaveAllEmptyData() { | |
4020 $this->loadFixtures('Article', 'ProductUpdateAll'); | |
4021 $model =& new Article(); | |
4022 $result = $model->saveAll(array(), array('validate' => 'first')); | |
4023 $this->assertTrue($result); | |
4024 | |
4025 $model =& new ProductUpdateAll(); | |
4026 $result = $model->saveAll(array()); | |
4027 $this->assertFalse($result); | |
4028 } | |
4029 | |
4030 /** | |
4031 * test writing floats in german locale. | |
4032 * | |
4033 * @return void | |
4034 */ | |
4035 function testWriteFloatAsGerman() { | |
4036 $restore = setlocale(LC_ALL, null); | |
4037 setlocale(LC_ALL, 'de_DE'); | |
4038 | |
4039 $model = new DataTest(); | |
4040 $result = $model->save(array( | |
4041 'count' => 1, | |
4042 'float' => 3.14593 | |
4043 )); | |
4044 $this->assertTrue($result); | |
4045 setlocale(LC_ALL, $restore); | |
4046 } | |
4047 | |
4048 } |