Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/model/model_delete.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 /** | |
3 * ModelDeleteTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs.model | |
17 * @since CakePHP(tm) v 1.2.0.4206 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 require_once dirname(__FILE__) . DS . 'model.test.php'; | |
21 | |
22 /** | |
23 * ModelDeleteTest | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.tests.cases.libs.model.operations | |
27 */ | |
28 class ModelDeleteTest extends BaseModelTest { | |
29 | |
30 /** | |
31 * testDeleteHabtmReferenceWithConditions method | |
32 * | |
33 * @access public | |
34 * @return void | |
35 */ | |
36 function testDeleteHabtmReferenceWithConditions() { | |
37 $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio'); | |
38 | |
39 $Portfolio =& new Portfolio(); | |
40 $Portfolio->hasAndBelongsToMany['Item']['conditions'] = array('ItemsPortfolio.item_id >' => 1); | |
41 | |
42 $result = $Portfolio->find('first', array( | |
43 'conditions' => array('Portfolio.id' => 1) | |
44 )); | |
45 $expected = array( | |
46 array( | |
47 'id' => 3, | |
48 'syfile_id' => 3, | |
49 'published' => 0, | |
50 'name' => 'Item 3', | |
51 'ItemsPortfolio' => array( | |
52 'id' => 3, | |
53 'item_id' => 3, | |
54 'portfolio_id' => 1 | |
55 )), | |
56 array( | |
57 'id' => 4, | |
58 'syfile_id' => 4, | |
59 'published' => 0, | |
60 'name' => 'Item 4', | |
61 'ItemsPortfolio' => array( | |
62 'id' => 4, | |
63 'item_id' => 4, | |
64 'portfolio_id' => 1 | |
65 )), | |
66 array( | |
67 'id' => 5, | |
68 'syfile_id' => 5, | |
69 'published' => 0, | |
70 'name' => 'Item 5', | |
71 'ItemsPortfolio' => array( | |
72 'id' => 5, | |
73 'item_id' => 5, | |
74 'portfolio_id' => 1 | |
75 ))); | |
76 $this->assertEqual($result['Item'], $expected); | |
77 | |
78 $result = $Portfolio->ItemsPortfolio->find('all', array( | |
79 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) | |
80 )); | |
81 $expected = array( | |
82 array( | |
83 'ItemsPortfolio' => array( | |
84 'id' => 1, | |
85 'item_id' => 1, | |
86 'portfolio_id' => 1 | |
87 )), | |
88 array( | |
89 'ItemsPortfolio' => array( | |
90 'id' => 3, | |
91 'item_id' => 3, | |
92 'portfolio_id' => 1 | |
93 )), | |
94 array( | |
95 'ItemsPortfolio' => array( | |
96 'id' => 4, | |
97 'item_id' => 4, | |
98 'portfolio_id' => 1 | |
99 )), | |
100 array( | |
101 'ItemsPortfolio' => array( | |
102 'id' => 5, | |
103 'item_id' => 5, | |
104 'portfolio_id' => 1 | |
105 ))); | |
106 $this->assertEqual($result, $expected); | |
107 | |
108 $Portfolio->delete(1); | |
109 | |
110 $result = $Portfolio->find('first', array( | |
111 'conditions' => array('Portfolio.id' => 1) | |
112 )); | |
113 $this->assertFalse($result); | |
114 | |
115 $result = $Portfolio->ItemsPortfolio->find('all', array( | |
116 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) | |
117 )); | |
118 $this->assertFalse($result); | |
119 } | |
120 | |
121 /** | |
122 * testDeleteArticleBLinks method | |
123 * | |
124 * @access public | |
125 * @return void | |
126 */ | |
127 function testDeleteArticleBLinks() { | |
128 $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); | |
129 $TestModel =& new ArticleB(); | |
130 | |
131 $result = $TestModel->ArticlesTag->find('all'); | |
132 $expected = array( | |
133 array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')), | |
134 array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')), | |
135 array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), | |
136 array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) | |
137 ); | |
138 $this->assertEqual($result, $expected); | |
139 | |
140 $TestModel->delete(1); | |
141 $result = $TestModel->ArticlesTag->find('all'); | |
142 | |
143 $expected = array( | |
144 array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), | |
145 array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) | |
146 ); | |
147 $this->assertEqual($result, $expected); | |
148 } | |
149 | |
150 /** | |
151 * testDeleteDependentWithConditions method | |
152 * | |
153 * @access public | |
154 * @return void | |
155 */ | |
156 function testDeleteDependentWithConditions() { | |
157 $this->loadFixtures('Cd','Book','OverallFavorite'); | |
158 | |
159 $Cd =& new Cd(); | |
160 $Book =& new Book(); | |
161 $OverallFavorite =& new OverallFavorite(); | |
162 | |
163 $Cd->delete(1); | |
164 | |
165 $result = $OverallFavorite->find('all', array( | |
166 'fields' => array('model_type', 'model_id', 'priority') | |
167 )); | |
168 $expected = array( | |
169 array( | |
170 'OverallFavorite' => array( | |
171 'model_type' => 'Book', | |
172 'model_id' => 1, | |
173 'priority' => 2 | |
174 ))); | |
175 | |
176 $this->assertTrue(is_array($result)); | |
177 $this->assertEqual($result, $expected); | |
178 | |
179 $Book->delete(1); | |
180 | |
181 $result = $OverallFavorite->find('all', array( | |
182 'fields' => array('model_type', 'model_id', 'priority') | |
183 )); | |
184 $expected = array(); | |
185 | |
186 $this->assertTrue(is_array($result)); | |
187 $this->assertEqual($result, $expected); | |
188 } | |
189 | |
190 /** | |
191 * testDel method | |
192 * | |
193 * @access public | |
194 * @return void | |
195 */ | |
196 function testDelete() { | |
197 $this->loadFixtures('Article'); | |
198 $TestModel =& new Article(); | |
199 | |
200 $result = $TestModel->delete(2); | |
201 $this->assertTrue($result); | |
202 | |
203 $result = $TestModel->read(null, 2); | |
204 $this->assertFalse($result); | |
205 | |
206 $TestModel->recursive = -1; | |
207 $result = $TestModel->find('all', array( | |
208 'fields' => array('id', 'title') | |
209 )); | |
210 $expected = array( | |
211 array('Article' => array( | |
212 'id' => 1, | |
213 'title' => 'First Article' | |
214 )), | |
215 array('Article' => array( | |
216 'id' => 3, | |
217 'title' => 'Third Article' | |
218 ))); | |
219 $this->assertEqual($result, $expected); | |
220 | |
221 $result = $TestModel->delete(3); | |
222 $this->assertTrue($result); | |
223 | |
224 $result = $TestModel->read(null, 3); | |
225 $this->assertFalse($result); | |
226 | |
227 $TestModel->recursive = -1; | |
228 $result = $TestModel->find('all', array( | |
229 'fields' => array('id', 'title') | |
230 )); | |
231 $expected = array( | |
232 array('Article' => array( | |
233 'id' => 1, | |
234 'title' => 'First Article' | |
235 ))); | |
236 | |
237 $this->assertEqual($result, $expected); | |
238 | |
239 // make sure deleting a non-existent record doesn't break save() | |
240 // ticket #6293 | |
241 $this->loadFixtures('Uuid'); | |
242 $Uuid =& new Uuid(); | |
243 $data = array( | |
244 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3', | |
245 '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8', | |
246 '8208C7FE-E89C-47C5-B378-DED6C271F9B8'); | |
247 foreach ($data as $id) { | |
248 $Uuid->save(array('id' => $id)); | |
249 } | |
250 $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); | |
251 $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); | |
252 foreach ($data as $id) { | |
253 $Uuid->save(array('id' => $id)); | |
254 } | |
255 $result = $Uuid->find('all', array( | |
256 'conditions' => array('id' => $data), | |
257 'fields' => array('id'), | |
258 'order' => 'id')); | |
259 $expected = array( | |
260 array('Uuid' => array( | |
261 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')), | |
262 array('Uuid' => array( | |
263 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')), | |
264 array('Uuid' => array( | |
265 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3'))); | |
266 $this->assertEqual($result, $expected); | |
267 } | |
268 | |
269 /** | |
270 * test that delete() updates the correct records counterCache() records. | |
271 * | |
272 * @return void | |
273 */ | |
274 function testDeleteUpdatingCounterCacheCorrectly() { | |
275 $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); | |
276 $User =& new CounterCacheUser(); | |
277 | |
278 $User->Post->delete(3); | |
279 $result = $User->read(null, 301); | |
280 $this->assertEqual($result['User']['post_count'], 0); | |
281 | |
282 $result = $User->read(null, 66); | |
283 $this->assertEqual($result['User']['post_count'], 2); | |
284 } | |
285 | |
286 /** | |
287 * testDeleteAll method | |
288 * | |
289 * @access public | |
290 * @return void | |
291 */ | |
292 function testDeleteAll() { | |
293 $this->loadFixtures('Article'); | |
294 $TestModel =& new Article(); | |
295 | |
296 $data = array('Article' => array( | |
297 'user_id' => 2, | |
298 'id' => 4, | |
299 'title' => 'Fourth Article', | |
300 'published' => 'N' | |
301 )); | |
302 $result = $TestModel->set($data) && $TestModel->save(); | |
303 $this->assertTrue($result); | |
304 | |
305 $data = array('Article' => array( | |
306 'user_id' => 2, | |
307 'id' => 5, | |
308 'title' => 'Fifth Article', | |
309 'published' => 'Y' | |
310 )); | |
311 $result = $TestModel->set($data) && $TestModel->save(); | |
312 $this->assertTrue($result); | |
313 | |
314 $data = array('Article' => array( | |
315 'user_id' => 1, | |
316 'id' => 6, | |
317 'title' => 'Sixth Article', | |
318 'published' => 'N' | |
319 )); | |
320 $result = $TestModel->set($data) && $TestModel->save(); | |
321 $this->assertTrue($result); | |
322 | |
323 $TestModel->recursive = -1; | |
324 $result = $TestModel->find('all', array( | |
325 'fields' => array('id', 'user_id', 'title', 'published') | |
326 )); | |
327 | |
328 $expected = array( | |
329 array('Article' => array( | |
330 'id' => 1, | |
331 'user_id' => 1, | |
332 'title' => 'First Article', | |
333 'published' => 'Y' | |
334 )), | |
335 array('Article' => array( | |
336 'id' => 2, | |
337 'user_id' => 3, | |
338 'title' => 'Second Article', | |
339 'published' => 'Y' | |
340 )), | |
341 array('Article' => array( | |
342 'id' => 3, | |
343 'user_id' => 1, | |
344 'title' => 'Third Article', | |
345 'published' => 'Y')), | |
346 array('Article' => array( | |
347 'id' => 4, | |
348 'user_id' => 2, | |
349 'title' => 'Fourth Article', | |
350 'published' => 'N' | |
351 )), | |
352 array('Article' => array( | |
353 'id' => 5, | |
354 'user_id' => 2, | |
355 'title' => 'Fifth Article', | |
356 'published' => 'Y' | |
357 )), | |
358 array('Article' => array( | |
359 'id' => 6, | |
360 'user_id' => 1, | |
361 'title' => 'Sixth Article', | |
362 'published' => 'N' | |
363 ))); | |
364 | |
365 $this->assertEqual($result, $expected); | |
366 | |
367 $result = $TestModel->deleteAll(array('Article.published' => 'N')); | |
368 $this->assertTrue($result); | |
369 | |
370 $TestModel->recursive = -1; | |
371 $result = $TestModel->find('all', array( | |
372 'fields' => array('id', 'user_id', 'title', 'published') | |
373 )); | |
374 $expected = array( | |
375 array('Article' => array( | |
376 'id' => 1, | |
377 'user_id' => 1, | |
378 'title' => 'First Article', | |
379 'published' => 'Y' | |
380 )), | |
381 array('Article' => array( | |
382 'id' => 2, | |
383 'user_id' => 3, | |
384 'title' => 'Second Article', | |
385 'published' => 'Y' | |
386 )), | |
387 array('Article' => array( | |
388 'id' => 3, | |
389 'user_id' => 1, | |
390 'title' => 'Third Article', | |
391 'published' => 'Y' | |
392 )), | |
393 array('Article' => array( | |
394 'id' => 5, | |
395 'user_id' => 2, | |
396 'title' => 'Fifth Article', | |
397 'published' => 'Y' | |
398 ))); | |
399 $this->assertEqual($result, $expected); | |
400 | |
401 $data = array('Article.user_id' => array(2, 3)); | |
402 $result = $TestModel->deleteAll($data, true, true); | |
403 $this->assertTrue($result); | |
404 | |
405 $TestModel->recursive = -1; | |
406 $result = $TestModel->find('all', array( | |
407 'fields' => array('id', 'user_id', 'title', 'published') | |
408 )); | |
409 $expected = array( | |
410 array('Article' => array( | |
411 'id' => 1, | |
412 'user_id' => 1, | |
413 'title' => 'First Article', | |
414 'published' => 'Y' | |
415 )), | |
416 array('Article' => array( | |
417 'id' => 3, | |
418 'user_id' => 1, | |
419 'title' => 'Third Article', | |
420 'published' => 'Y' | |
421 ))); | |
422 $this->assertEqual($result, $expected); | |
423 | |
424 $result = $TestModel->deleteAll(array('Article.user_id' => 999)); | |
425 $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); | |
426 | |
427 $this->expectError(); | |
428 ob_start(); | |
429 $result = $TestModel->deleteAll(array('Article.non_existent_field' => 999)); | |
430 ob_get_clean(); | |
431 $this->assertFalse($result, 'deleteAll returned true when find query generated sql error. %s'); | |
432 } | |
433 | |
434 /** | |
435 * testRecursiveDel method | |
436 * | |
437 * @access public | |
438 * @return void | |
439 */ | |
440 function testRecursiveDel() { | |
441 $this->loadFixtures('Article', 'Comment', 'Attachment'); | |
442 $TestModel =& new Article(); | |
443 | |
444 $result = $TestModel->delete(2); | |
445 $this->assertTrue($result); | |
446 | |
447 $TestModel->recursive = 2; | |
448 $result = $TestModel->read(null, 2); | |
449 $this->assertFalse($result); | |
450 | |
451 $result = $TestModel->Comment->read(null, 5); | |
452 $this->assertFalse($result); | |
453 | |
454 $result = $TestModel->Comment->read(null, 6); | |
455 $this->assertFalse($result); | |
456 | |
457 $result = $TestModel->Comment->Attachment->read(null, 1); | |
458 $this->assertFalse($result); | |
459 | |
460 $result = $TestModel->find('count'); | |
461 $this->assertEqual($result, 2); | |
462 | |
463 $result = $TestModel->Comment->find('count'); | |
464 $this->assertEqual($result, 4); | |
465 | |
466 $result = $TestModel->Comment->Attachment->find('count'); | |
467 $this->assertEqual($result, 0); | |
468 } | |
469 | |
470 /** | |
471 * testDependentExclusiveDelete method | |
472 * | |
473 * @access public | |
474 * @return void | |
475 */ | |
476 function testDependentExclusiveDelete() { | |
477 $this->loadFixtures('Article', 'Comment'); | |
478 $TestModel =& new Article10(); | |
479 | |
480 $result = $TestModel->find('all'); | |
481 $this->assertEqual(count($result[0]['Comment']), 4); | |
482 $this->assertEqual(count($result[1]['Comment']), 2); | |
483 $this->assertEqual($TestModel->Comment->find('count'), 6); | |
484 | |
485 $TestModel->delete(1); | |
486 $this->assertEqual($TestModel->Comment->find('count'), 2); | |
487 } | |
488 | |
489 /** | |
490 * testDeleteLinks method | |
491 * | |
492 * @access public | |
493 * @return void | |
494 */ | |
495 function testDeleteLinks() { | |
496 $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); | |
497 $TestModel =& new Article(); | |
498 | |
499 $result = $TestModel->ArticlesTag->find('all'); | |
500 $expected = array( | |
501 array('ArticlesTag' => array( | |
502 'article_id' => '1', | |
503 'tag_id' => '1' | |
504 )), | |
505 array('ArticlesTag' => array( | |
506 'article_id' => '1', | |
507 'tag_id' => '2' | |
508 )), | |
509 array('ArticlesTag' => array( | |
510 'article_id' => '2', | |
511 'tag_id' => '1' | |
512 )), | |
513 array('ArticlesTag' => array( | |
514 'article_id' => '2', | |
515 'tag_id' => '3' | |
516 ))); | |
517 $this->assertEqual($result, $expected); | |
518 | |
519 $TestModel->delete(1); | |
520 $result = $TestModel->ArticlesTag->find('all'); | |
521 | |
522 $expected = array( | |
523 array('ArticlesTag' => array( | |
524 'article_id' => '2', | |
525 'tag_id' => '1' | |
526 )), | |
527 array('ArticlesTag' => array( | |
528 'article_id' => '2', | |
529 'tag_id' => '3' | |
530 ))); | |
531 $this->assertEqual($result, $expected); | |
532 | |
533 $result = $TestModel->deleteAll(array('Article.user_id' => 999)); | |
534 $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); | |
535 } | |
536 | |
537 /** | |
538 * test deleteLinks with Multiple habtm associations | |
539 * | |
540 * @return void | |
541 */ | |
542 function testDeleteLinksWithMultipleHabtmAssociations() { | |
543 $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); | |
544 $JoinA =& new JoinA(); | |
545 | |
546 //create two new join records to expose the issue. | |
547 $JoinA->JoinAsJoinC->create(array( | |
548 'join_a_id' => 1, | |
549 'join_c_id' => 2, | |
550 )); | |
551 $JoinA->JoinAsJoinC->save(); | |
552 $JoinA->JoinAsJoinB->create(array( | |
553 'join_a_id' => 1, | |
554 'join_b_id' => 2, | |
555 )); | |
556 $JoinA->JoinAsJoinB->save(); | |
557 | |
558 $result = $JoinA->delete(1); | |
559 $this->assertTrue($result, 'Delete failed %s'); | |
560 | |
561 $joinedBs = $JoinA->JoinAsJoinB->find('count', array( | |
562 'conditions' => array('JoinAsJoinB.join_a_id' => 1) | |
563 )); | |
564 $this->assertEqual($joinedBs, 0, 'JoinA/JoinB link records left over. %s'); | |
565 | |
566 $joinedBs = $JoinA->JoinAsJoinC->find('count', array( | |
567 'conditions' => array('JoinAsJoinC.join_a_id' => 1) | |
568 )); | |
569 $this->assertEqual($joinedBs, 0, 'JoinA/JoinC link records left over. %s'); | |
570 } | |
571 | |
572 /** | |
573 * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method | |
574 * | |
575 * @access public | |
576 * @return void | |
577 */ | |
578 function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() { | |
579 | |
580 $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); | |
581 $ThePaper =& new ThePaper(); | |
582 $ThePaper->id = 1; | |
583 $ThePaper->save(array('Monkey' => array(2, 3))); | |
584 | |
585 $result = $ThePaper->findById(1); | |
586 $expected = array( | |
587 array( | |
588 'id' => '2', | |
589 'device_type_id' => '1', | |
590 'name' => 'Device 2', | |
591 'typ' => '1' | |
592 ), | |
593 array( | |
594 'id' => '3', | |
595 'device_type_id' => '1', | |
596 'name' => 'Device 3', | |
597 'typ' => '2' | |
598 )); | |
599 $this->assertEqual($result['Monkey'], $expected); | |
600 | |
601 $ThePaper =& new ThePaper(); | |
602 $ThePaper->id = 2; | |
603 $ThePaper->save(array('Monkey' => array(2, 3))); | |
604 | |
605 $result = $ThePaper->findById(2); | |
606 $expected = array( | |
607 array( | |
608 'id' => '2', | |
609 'device_type_id' => '1', | |
610 'name' => 'Device 2', | |
611 'typ' => '1' | |
612 ), | |
613 array( | |
614 'id' => '3', | |
615 'device_type_id' => '1', | |
616 'name' => 'Device 3', | |
617 'typ' => '2' | |
618 )); | |
619 $this->assertEqual($result['Monkey'], $expected); | |
620 | |
621 $ThePaper->delete(1); | |
622 $result = $ThePaper->findById(2); | |
623 $expected = array( | |
624 array( | |
625 'id' => '2', | |
626 'device_type_id' => '1', | |
627 'name' => 'Device 2', | |
628 'typ' => '1' | |
629 ), | |
630 array( | |
631 'id' => '3', | |
632 'device_type_id' => '1', | |
633 'name' => 'Device 3', | |
634 'typ' => '2' | |
635 )); | |
636 $this->assertEqual($result['Monkey'], $expected); | |
637 } | |
638 | |
639 /** | |
640 * test that beforeDelete returning false can abort deletion. | |
641 * | |
642 * @return void | |
643 */ | |
644 function testBeforeDeleteDeleteAbortion() { | |
645 $this->loadFixtures('Post'); | |
646 $Model =& new CallbackPostTestModel(); | |
647 $Model->beforeDeleteReturn = false; | |
648 | |
649 $result = $Model->delete(1); | |
650 $this->assertFalse($result); | |
651 | |
652 $exists = $Model->findById(1); | |
653 $this->assertTrue(is_array($exists)); | |
654 } | |
655 | |
656 /** | |
657 * test for a habtm deletion error that occurs in postgres but should not. | |
658 * And should not occur in any dbo. | |
659 * | |
660 * @return void | |
661 */ | |
662 function testDeleteHabtmPostgresFailure() { | |
663 $this->loadFixtures('Article', 'Tag', 'ArticlesTag'); | |
664 | |
665 $Article =& ClassRegistry::init('Article'); | |
666 $Article->hasAndBelongsToMany['Tag']['unique'] = true; | |
667 | |
668 $Tag =& ClassRegistry::init('Tag'); | |
669 $Tag->bindModel(array('hasAndBelongsToMany' => array( | |
670 'Article' => array( | |
671 'className' => 'Article', | |
672 'unique' => true | |
673 ) | |
674 )), true); | |
675 | |
676 // Article 1 should have Tag.1 and Tag.2 | |
677 $before = $Article->find("all", array( | |
678 "conditions" => array("Article.id" => 1), | |
679 )); | |
680 $this->assertEqual(count($before[0]['Tag']), 2, 'Tag count for Article.id = 1 is incorrect, should be 2 %s'); | |
681 | |
682 // From now on, Tag #1 is only associated with Post #1 | |
683 $submitted_data = array( | |
684 "Tag" => array("id" => 1, 'tag' => 'tag1'), | |
685 "Article" => array( | |
686 "Article" => array(1) | |
687 ) | |
688 ); | |
689 $Tag->save($submitted_data); | |
690 | |
691 // One more submission (The other way around) to make sure the reverse save looks good. | |
692 $submitted_data = array( | |
693 "Article" => array("id" => 2, 'title' => 'second article'), | |
694 "Tag" => array( | |
695 "Tag" => array(2, 3) | |
696 ) | |
697 ); | |
698 // ERROR: | |
699 // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3') | |
700 // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3) | |
701 $Article->save($submitted_data); | |
702 | |
703 // Want to make sure Article #1 has Tag #1 and Tag #2 still. | |
704 $after = $Article->find("all", array( | |
705 "conditions" => array("Article.id" => 1), | |
706 )); | |
707 | |
708 // Removing Article #2 from Tag #1 is all that should have happened. | |
709 $this->assertEqual(count($before[0]["Tag"]), count($after[0]["Tag"])); | |
710 } | |
711 | |
712 /** | |
713 * test that deleting records inside the beforeDelete doesn't truncate the table. | |
714 * | |
715 * @return void | |
716 */ | |
717 function testBeforeDeleteWipingTable() { | |
718 $this->loadFixtures('Comment'); | |
719 | |
720 $Comment =& new BeforeDeleteComment(); | |
721 // Delete 3 records. | |
722 $Comment->delete(4); | |
723 $result = $Comment->find('count'); | |
724 | |
725 $this->assertTrue($result > 1, 'Comments are all gone.'); | |
726 $Comment->create(array( | |
727 'article_id' => 1, | |
728 'user_id' => 2, | |
729 'comment' => 'new record', | |
730 'published' => 'Y' | |
731 )); | |
732 $Comment->save(); | |
733 | |
734 $Comment->delete(5); | |
735 $result = $Comment->find('count'); | |
736 | |
737 $this->assertTrue($result > 1, 'Comments are all gone.'); | |
738 } | |
739 | |
740 /** | |
741 * test that deleting the same record from the beforeDelete and the delete doesn't truncate the table. | |
742 * | |
743 * @return void | |
744 */ | |
745 function testBeforeDeleteWipingTableWithDuplicateDelete() { | |
746 $this->loadFixtures('Comment'); | |
747 | |
748 $Comment =& new BeforeDeleteComment(); | |
749 $Comment->delete(1); | |
750 | |
751 $result = $Comment->find('count'); | |
752 $this->assertTrue($result > 1, 'Comments are all gone.'); | |
753 } | |
754 } |