pictcode / lib / Cake / Test / Case / Model / ModelDeleteTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (24.428 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * ModelDeleteTest file
|
||
4 | *
|
||
5 | * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
||
6 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
7 | *
|
||
8 | * Licensed under The MIT License
|
||
9 | * For full copyright and license information, please see the LICENSE.txt
|
||
10 | * Redistributions of files must retain the above copyright notice
|
||
11 | *
|
||
12 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
13 | * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
||
14 | * @package Cake.Test.Case.Model
|
||
15 | * @since CakePHP(tm) v 1.2.0.4206
|
||
16 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
17 | */
|
||
18 | |||
19 | require_once dirname(__FILE__) . DS . 'ModelTestBase.php'; |
||
20 | |||
21 | /**
|
||
22 | * ModelDeleteTest
|
||
23 | *
|
||
24 | * @package Cake.Test.Case.Model
|
||
25 | */
|
||
26 | class ModelDeleteTest extends BaseModelTest { |
||
27 | |||
28 | /**
|
||
29 | * testDeleteHabtmReferenceWithConditions method
|
||
30 | *
|
||
31 | * @return void
|
||
32 | */
|
||
33 | public function testDeleteHabtmReferenceWithConditions() { |
||
34 | $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image'); |
||
35 | |||
36 | $Portfolio = new Portfolio(); |
||
37 | $Portfolio->hasAndBelongsToMany['Item']['conditions'] = array('ItemsPortfolio.item_id >' => 1); |
||
38 | |||
39 | $result = $Portfolio->find('first', array( |
||
40 | 'conditions' => array('Portfolio.id' => 1) |
||
41 | )); |
||
42 | $expected = array( |
||
43 | array(
|
||
44 | 'id' => 3, |
||
45 | 'syfile_id' => 3, |
||
46 | 'published' => false, |
||
47 | 'name' => 'Item 3', |
||
48 | 'ItemsPortfolio' => array( |
||
49 | 'id' => 3, |
||
50 | 'item_id' => 3, |
||
51 | 'portfolio_id' => 1 |
||
52 | )), |
||
53 | array(
|
||
54 | 'id' => 4, |
||
55 | 'syfile_id' => 4, |
||
56 | 'published' => false, |
||
57 | 'name' => 'Item 4', |
||
58 | 'ItemsPortfolio' => array( |
||
59 | 'id' => 4, |
||
60 | 'item_id' => 4, |
||
61 | 'portfolio_id' => 1 |
||
62 | )), |
||
63 | array(
|
||
64 | 'id' => 5, |
||
65 | 'syfile_id' => 5, |
||
66 | 'published' => false, |
||
67 | 'name' => 'Item 5', |
||
68 | 'ItemsPortfolio' => array( |
||
69 | 'id' => 5, |
||
70 | 'item_id' => 5, |
||
71 | 'portfolio_id' => 1 |
||
72 | ))); |
||
73 | $this->assertEquals($expected, $result['Item']); |
||
74 | |||
75 | $result = $Portfolio->ItemsPortfolio->find('all', array( |
||
76 | 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) |
||
77 | )); |
||
78 | $expected = array( |
||
79 | array(
|
||
80 | 'ItemsPortfolio' => array( |
||
81 | 'id' => 1, |
||
82 | 'item_id' => 1, |
||
83 | 'portfolio_id' => 1 |
||
84 | )), |
||
85 | array(
|
||
86 | 'ItemsPortfolio' => array( |
||
87 | 'id' => 3, |
||
88 | 'item_id' => 3, |
||
89 | 'portfolio_id' => 1 |
||
90 | )), |
||
91 | array(
|
||
92 | 'ItemsPortfolio' => array( |
||
93 | 'id' => 4, |
||
94 | 'item_id' => 4, |
||
95 | 'portfolio_id' => 1 |
||
96 | )), |
||
97 | array(
|
||
98 | 'ItemsPortfolio' => array( |
||
99 | 'id' => 5, |
||
100 | 'item_id' => 5, |
||
101 | 'portfolio_id' => 1 |
||
102 | ))); |
||
103 | $this->assertEquals($expected, $result); |
||
104 | |||
105 | $Portfolio->delete(1); |
||
106 | |||
107 | $result = $Portfolio->find('first', array( |
||
108 | 'conditions' => array('Portfolio.id' => 1) |
||
109 | )); |
||
110 | $this->assertSame(array(), $result); |
||
111 | |||
112 | $result = $Portfolio->ItemsPortfolio->find('all', array( |
||
113 | 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) |
||
114 | )); |
||
115 | $this->assertSame(array(), $result); |
||
116 | } |
||
117 | |||
118 | /**
|
||
119 | * testDeleteArticleBLinks method
|
||
120 | *
|
||
121 | * @return void
|
||
122 | */
|
||
123 | public function testDeleteArticleBLinks() { |
||
124 | $this->loadFixtures('Article', 'ArticlesTag', 'Tag', 'User'); |
||
125 | $TestModel = new ArticleB(); |
||
126 | |||
127 | $result = $TestModel->ArticlesTag->find('all'); |
||
128 | $expected = array( |
||
129 | array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')), |
||
130 | array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')), |
||
131 | array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), |
||
132 | array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) |
||
133 | ); |
||
134 | $this->assertEquals($expected, $result); |
||
135 | |||
136 | $TestModel->delete(1); |
||
137 | $result = $TestModel->ArticlesTag->find('all'); |
||
138 | |||
139 | $expected = array( |
||
140 | array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), |
||
141 | array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) |
||
142 | ); |
||
143 | $this->assertEquals($expected, $result); |
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * testDeleteDependentWithConditions method
|
||
148 | *
|
||
149 | * @return void
|
||
150 | */
|
||
151 | public function testDeleteDependentWithConditions() { |
||
152 | $this->loadFixtures('Cd', 'Book', 'OverallFavorite'); |
||
153 | |||
154 | $Cd = new Cd(); |
||
155 | $Book = new Book(); |
||
156 | $OverallFavorite = new OverallFavorite(); |
||
157 | |||
158 | $Cd->delete(1); |
||
159 | |||
160 | $result = $OverallFavorite->find('all', array( |
||
161 | 'fields' => array('model_type', 'model_id', 'priority') |
||
162 | )); |
||
163 | $expected = array( |
||
164 | array(
|
||
165 | 'OverallFavorite' => array( |
||
166 | 'model_type' => 'Book', |
||
167 | 'model_id' => 1, |
||
168 | 'priority' => 2 |
||
169 | ))); |
||
170 | |||
171 | $this->assertTrue(is_array($result)); |
||
172 | $this->assertEquals($expected, $result); |
||
173 | |||
174 | $Book->delete(1); |
||
175 | |||
176 | $result = $OverallFavorite->find('all', array( |
||
177 | 'fields' => array('model_type', 'model_id', 'priority') |
||
178 | )); |
||
179 | $expected = array(); |
||
180 | |||
181 | $this->assertTrue(is_array($result)); |
||
182 | $this->assertEquals($expected, $result); |
||
183 | } |
||
184 | |||
185 | /**
|
||
186 | * testDel method
|
||
187 | *
|
||
188 | * @return void
|
||
189 | */
|
||
190 | public function testDelete() { |
||
191 | $this->loadFixtures('Article', 'Comment', 'Attachment'); |
||
192 | $TestModel = new Article(); |
||
193 | |||
194 | $result = $TestModel->delete(2); |
||
195 | $this->assertTrue($result); |
||
196 | |||
197 | $result = $TestModel->read(null, 2); |
||
198 | $this->assertSame(array(), $result); |
||
199 | |||
200 | $TestModel->recursive = -1; |
||
201 | $result = $TestModel->find('all', array( |
||
202 | 'fields' => array('id', 'title') |
||
203 | )); |
||
204 | $expected = array( |
||
205 | array('Article' => array( |
||
206 | 'id' => 1, |
||
207 | 'title' => 'First Article' |
||
208 | )), |
||
209 | array('Article' => array( |
||
210 | 'id' => 3, |
||
211 | 'title' => 'Third Article' |
||
212 | ))); |
||
213 | $this->assertEquals($expected, $result); |
||
214 | |||
215 | $result = $TestModel->delete(3); |
||
216 | $this->assertTrue($result); |
||
217 | |||
218 | $result = $TestModel->read(null, 3); |
||
219 | $this->assertSame(array(), $result); |
||
220 | |||
221 | $TestModel->recursive = -1; |
||
222 | $result = $TestModel->find('all', array( |
||
223 | 'fields' => array('id', 'title') |
||
224 | )); |
||
225 | $expected = array( |
||
226 | array('Article' => array( |
||
227 | 'id' => 1, |
||
228 | 'title' => 'First Article' |
||
229 | ))); |
||
230 | |||
231 | $this->assertEquals($expected, $result); |
||
232 | |||
233 | // make sure deleting a non-existent record doesn't break save()
|
||
234 | // ticket #6293
|
||
235 | $this->loadFixtures('Uuid'); |
||
236 | $Uuid = new Uuid(); |
||
237 | $data = array( |
||
238 | 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3',
|
||
239 | '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8',
|
||
240 | '8208C7FE-E89C-47C5-B378-DED6C271F9B8');
|
||
241 | foreach ($data as $id) { |
||
242 | $Uuid->save(array('id' => $id)); |
||
243 | } |
||
244 | $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); |
||
245 | $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); |
||
246 | foreach ($data as $id) { |
||
247 | $Uuid->save(array('id' => $id)); |
||
248 | } |
||
249 | $result = $Uuid->find('all', array( |
||
250 | 'conditions' => array('id' => $data), |
||
251 | 'fields' => array('id'), |
||
252 | 'order' => 'id')); |
||
253 | $expected = array( |
||
254 | array('Uuid' => array( |
||
255 | 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')), |
||
256 | array('Uuid' => array( |
||
257 | 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')), |
||
258 | array('Uuid' => array( |
||
259 | 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3'))); |
||
260 | $this->assertEquals($expected, $result); |
||
261 | } |
||
262 | |||
263 | /**
|
||
264 | * test that delete() updates the correct records counterCache() records.
|
||
265 | *
|
||
266 | * @return void
|
||
267 | */
|
||
268 | public function testDeleteUpdatingCounterCacheCorrectly() { |
||
269 | $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); |
||
270 | $User = new CounterCacheUser(); |
||
271 | |||
272 | $User->Post->delete(3); |
||
273 | $result = $User->read(null, 301); |
||
274 | $this->assertEquals(0, $result['User']['post_count']); |
||
275 | |||
276 | $result = $User->read(null, 66); |
||
277 | $this->assertEquals(2, $result['User']['post_count']); |
||
278 | } |
||
279 | |||
280 | /**
|
||
281 | * testDeleteAll method
|
||
282 | *
|
||
283 | * @return void
|
||
284 | */
|
||
285 | public function testDeleteAll() { |
||
286 | $this->loadFixtures('Article'); |
||
287 | $TestModel = new Article(); |
||
288 | |||
289 | $data = array('Article' => array( |
||
290 | 'user_id' => 2, |
||
291 | 'id' => 4, |
||
292 | 'title' => 'Fourth Article', |
||
293 | 'published' => 'N' |
||
294 | )); |
||
295 | $result = $TestModel->set($data) && $TestModel->save(); |
||
296 | $this->assertTrue($result); |
||
297 | |||
298 | $data = array('Article' => array( |
||
299 | 'user_id' => 2, |
||
300 | 'id' => 5, |
||
301 | 'title' => 'Fifth Article', |
||
302 | 'published' => 'Y' |
||
303 | )); |
||
304 | $result = $TestModel->set($data) && $TestModel->save(); |
||
305 | $this->assertTrue($result); |
||
306 | |||
307 | $data = array('Article' => array( |
||
308 | 'user_id' => 1, |
||
309 | 'id' => 6, |
||
310 | 'title' => 'Sixth Article', |
||
311 | 'published' => 'N' |
||
312 | )); |
||
313 | $result = $TestModel->set($data) && $TestModel->save(); |
||
314 | $this->assertTrue($result); |
||
315 | |||
316 | $TestModel->recursive = -1; |
||
317 | $result = $TestModel->find('all', array( |
||
318 | 'fields' => array('id', 'user_id', 'title', 'published'), |
||
319 | 'order' => array('Article.id' => 'ASC') |
||
320 | )); |
||
321 | |||
322 | $expected = array( |
||
323 | array('Article' => array( |
||
324 | 'id' => 1, |
||
325 | 'user_id' => 1, |
||
326 | 'title' => 'First Article', |
||
327 | 'published' => 'Y' |
||
328 | )), |
||
329 | array('Article' => array( |
||
330 | 'id' => 2, |
||
331 | 'user_id' => 3, |
||
332 | 'title' => 'Second Article', |
||
333 | 'published' => 'Y' |
||
334 | )), |
||
335 | array('Article' => array( |
||
336 | 'id' => 3, |
||
337 | 'user_id' => 1, |
||
338 | 'title' => 'Third Article', |
||
339 | 'published' => 'Y')), |
||
340 | array('Article' => array( |
||
341 | 'id' => 4, |
||
342 | 'user_id' => 2, |
||
343 | 'title' => 'Fourth Article', |
||
344 | 'published' => 'N' |
||
345 | )), |
||
346 | array('Article' => array( |
||
347 | 'id' => 5, |
||
348 | 'user_id' => 2, |
||
349 | 'title' => 'Fifth Article', |
||
350 | 'published' => 'Y' |
||
351 | )), |
||
352 | array('Article' => array( |
||
353 | 'id' => 6, |
||
354 | 'user_id' => 1, |
||
355 | 'title' => 'Sixth Article', |
||
356 | 'published' => 'N' |
||
357 | ))); |
||
358 | |||
359 | $this->assertEquals($expected, $result); |
||
360 | |||
361 | $result = $TestModel->deleteAll(array('Article.published' => 'N')); |
||
362 | $this->assertTrue($result); |
||
363 | |||
364 | $TestModel->recursive = -1; |
||
365 | $result = $TestModel->find('all', array( |
||
366 | 'fields' => array('id', 'user_id', 'title', 'published'), |
||
367 | 'order' => array('Article.id' => 'ASC') |
||
368 | )); |
||
369 | $expected = array( |
||
370 | array('Article' => array( |
||
371 | 'id' => 1, |
||
372 | 'user_id' => 1, |
||
373 | 'title' => 'First Article', |
||
374 | 'published' => 'Y' |
||
375 | )), |
||
376 | array('Article' => array( |
||
377 | 'id' => 2, |
||
378 | 'user_id' => 3, |
||
379 | 'title' => 'Second Article', |
||
380 | 'published' => 'Y' |
||
381 | )), |
||
382 | array('Article' => array( |
||
383 | 'id' => 3, |
||
384 | 'user_id' => 1, |
||
385 | 'title' => 'Third Article', |
||
386 | 'published' => 'Y' |
||
387 | )), |
||
388 | array('Article' => array( |
||
389 | 'id' => 5, |
||
390 | 'user_id' => 2, |
||
391 | 'title' => 'Fifth Article', |
||
392 | 'published' => 'Y' |
||
393 | ))); |
||
394 | $this->assertEquals($expected, $result); |
||
395 | |||
396 | $data = array('Article.user_id' => array(2, 3)); |
||
397 | $result = $TestModel->deleteAll($data, true, true); |
||
398 | $this->assertTrue($result); |
||
399 | |||
400 | $TestModel->recursive = -1; |
||
401 | $result = $TestModel->find('all', array( |
||
402 | 'fields' => array('id', 'user_id', 'title', 'published'), |
||
403 | 'order' => array('Article.id' => 'ASC') |
||
404 | )); |
||
405 | $expected = array( |
||
406 | array('Article' => array( |
||
407 | 'id' => 1, |
||
408 | 'user_id' => 1, |
||
409 | 'title' => 'First Article', |
||
410 | 'published' => 'Y' |
||
411 | )), |
||
412 | array('Article' => array( |
||
413 | 'id' => 3, |
||
414 | 'user_id' => 1, |
||
415 | 'title' => 'Third Article', |
||
416 | 'published' => 'Y' |
||
417 | ))); |
||
418 | $this->assertEquals($expected, $result); |
||
419 | |||
420 | $result = $TestModel->deleteAll(array('Article.user_id' => 999)); |
||
421 | $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); |
||
422 | } |
||
423 | |||
424 | /**
|
||
425 | * testDeleteAllUnknownColumn method
|
||
426 | *
|
||
427 | * @expectedException PDOException
|
||
428 | * @return void
|
||
429 | */
|
||
430 | public function testDeleteAllUnknownColumn() { |
||
431 | $this->loadFixtures('Article'); |
||
432 | $TestModel = new Article(); |
||
433 | $result = $TestModel->deleteAll(array('Article.non_existent_field' => 999)); |
||
434 | $this->assertFalse($result, 'deleteAll returned true when find query generated sql error. %s'); |
||
435 | } |
||
436 | |||
437 | /**
|
||
438 | * testDeleteAllFailedFind method
|
||
439 | *
|
||
440 | * Eg: Behavior callback stops the event, find returns null
|
||
441 | *
|
||
442 | * @return void
|
||
443 | */
|
||
444 | public function testDeleteAllFailedFind() { |
||
445 | $this->loadFixtures('Article'); |
||
446 | $TestModel = $this->getMock('Article', array('find')); |
||
447 | $TestModel->expects($this->once()) |
||
448 | ->method('find')
|
||
449 | ->will($this->returnValue(null)); |
||
450 | |||
451 | $result = $TestModel->deleteAll(array('Article.user_id' => 999)); |
||
452 | $this->assertFalse($result); |
||
453 | } |
||
454 | |||
455 | /**
|
||
456 | * testDeleteAllMultipleRowsPerId method
|
||
457 | *
|
||
458 | * Ensure find done in deleteAll only returns distinct ids. A wacky combination
|
||
459 | * of association and conditions can sometimes generate multiple rows per id.
|
||
460 | *
|
||
461 | * @return void
|
||
462 | */
|
||
463 | public function testDeleteAllMultipleRowsPerId() { |
||
464 | $this->loadFixtures('Article', 'User'); |
||
465 | |||
466 | $TestModel = new Article(); |
||
467 | $TestModel->unbindModel(array( |
||
468 | 'belongsTo' => array('User'), |
||
469 | 'hasMany' => array('Comment'), |
||
470 | 'hasAndBelongsToMany' => array('Tag') |
||
471 | ), false);
|
||
472 | $TestModel->bindModel(array( |
||
473 | 'belongsTo' => array( |
||
474 | 'User' => array( |
||
475 | 'foreignKey' => false, |
||
476 | 'conditions' => array( |
||
477 | 'Article.user_id = 1'
|
||
478 | ) |
||
479 | ) |
||
480 | ) |
||
481 | ), false);
|
||
482 | |||
483 | $result = $TestModel->deleteAll( |
||
484 | array('Article.user_id' => array(1, 3)), |
||
485 | true,
|
||
486 | true
|
||
487 | ); |
||
488 | |||
489 | $this->assertTrue($result); |
||
490 | } |
||
491 | |||
492 | /**
|
||
493 | * testDeleteAllWithOrderProperty
|
||
494 | *
|
||
495 | * Ensure find done in deleteAll works with models that has $order property set
|
||
496 | *
|
||
497 | * @return void
|
||
498 | */
|
||
499 | public function testDeleteAllWithOrderProperty() { |
||
500 | $this->loadFixtures('Article', 'User'); |
||
501 | |||
502 | $TestModel = new Article(); |
||
503 | $TestModel->order = 'Article.published desc'; |
||
504 | $TestModel->unbindModel(array( |
||
505 | 'belongsTo' => array('User'), |
||
506 | 'hasMany' => array('Comment'), |
||
507 | 'hasAndBelongsToMany' => array('Tag') |
||
508 | ), false);
|
||
509 | |||
510 | $result = $TestModel->deleteAll( |
||
511 | array('Article.user_id' => array(1, 3)), |
||
512 | true,
|
||
513 | true
|
||
514 | ); |
||
515 | |||
516 | $this->assertTrue($result); |
||
517 | } |
||
518 | |||
519 | /**
|
||
520 | * testRecursiveDel method
|
||
521 | *
|
||
522 | * @return void
|
||
523 | */
|
||
524 | public function testRecursiveDel() { |
||
525 | $this->loadFixtures('Article', 'Comment', 'Attachment'); |
||
526 | $TestModel = new Article(); |
||
527 | |||
528 | $result = $TestModel->delete(2); |
||
529 | $this->assertTrue($result); |
||
530 | |||
531 | $TestModel->recursive = 2; |
||
532 | $result = $TestModel->read(null, 2); |
||
533 | $this->assertSame(array(), $result); |
||
534 | |||
535 | $result = $TestModel->Comment->read(null, 5); |
||
536 | $this->assertSame(array(), $result); |
||
537 | |||
538 | $result = $TestModel->Comment->read(null, 6); |
||
539 | $this->assertSame(array(), $result); |
||
540 | |||
541 | $result = $TestModel->Comment->Attachment->read(null, 1); |
||
542 | $this->assertSame(array(), $result); |
||
543 | |||
544 | $result = $TestModel->find('count'); |
||
545 | $this->assertEquals(2, $result); |
||
546 | |||
547 | $result = $TestModel->Comment->find('count'); |
||
548 | $this->assertEquals(4, $result); |
||
549 | |||
550 | $result = $TestModel->Comment->Attachment->find('count'); |
||
551 | $this->assertEquals(0, $result); |
||
552 | } |
||
553 | |||
554 | /**
|
||
555 | * testDependentExclusiveDelete method
|
||
556 | *
|
||
557 | * @return void
|
||
558 | */
|
||
559 | public function testDependentExclusiveDelete() { |
||
560 | $this->loadFixtures('Article', 'Comment'); |
||
561 | $TestModel = new Article10(); |
||
562 | |||
563 | $result = $TestModel->find('all'); |
||
564 | $this->assertEquals(4, count($result[0]['Comment'])); |
||
565 | $this->assertEquals(2, count($result[1]['Comment'])); |
||
566 | $this->assertEquals(6, $TestModel->Comment->find('count')); |
||
567 | |||
568 | $TestModel->delete(1); |
||
569 | $this->assertEquals(2, $TestModel->Comment->find('count')); |
||
570 | } |
||
571 | |||
572 | /**
|
||
573 | * testDeleteLinks method
|
||
574 | *
|
||
575 | * @return void
|
||
576 | */
|
||
577 | public function testDeleteLinks() { |
||
578 | $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); |
||
579 | $TestModel = new Article(); |
||
580 | |||
581 | $result = $TestModel->ArticlesTag->find('all'); |
||
582 | $expected = array( |
||
583 | array('ArticlesTag' => array( |
||
584 | 'article_id' => '1', |
||
585 | 'tag_id' => '1' |
||
586 | )), |
||
587 | array('ArticlesTag' => array( |
||
588 | 'article_id' => '1', |
||
589 | 'tag_id' => '2' |
||
590 | )), |
||
591 | array('ArticlesTag' => array( |
||
592 | 'article_id' => '2', |
||
593 | 'tag_id' => '1' |
||
594 | )), |
||
595 | array('ArticlesTag' => array( |
||
596 | 'article_id' => '2', |
||
597 | 'tag_id' => '3' |
||
598 | ))); |
||
599 | $this->assertEquals($expected, $result); |
||
600 | |||
601 | $TestModel->delete(1); |
||
602 | $result = $TestModel->ArticlesTag->find('all'); |
||
603 | |||
604 | $expected = array( |
||
605 | array('ArticlesTag' => array( |
||
606 | 'article_id' => '2', |
||
607 | 'tag_id' => '1' |
||
608 | )), |
||
609 | array('ArticlesTag' => array( |
||
610 | 'article_id' => '2', |
||
611 | 'tag_id' => '3' |
||
612 | ))); |
||
613 | $this->assertEquals($expected, $result); |
||
614 | |||
615 | $result = $TestModel->deleteAll(array('Article.user_id' => 999)); |
||
616 | $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); |
||
617 | } |
||
618 | |||
619 | /**
|
||
620 | * test that a plugin model as the 'with' model doesn't have issues
|
||
621 | *
|
||
622 | * @return void
|
||
623 | */
|
||
624 | public function testDeleteLinksWithPLuginJoinModel() { |
||
625 | $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); |
||
626 | $Article = new Article(); |
||
627 | $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')), false); |
||
628 | unset($Article->Tag, $Article->ArticleTags); |
||
629 | $Article->bindModel(array('hasAndBelongsToMany' => array( |
||
630 | 'Tag' => array('with' => 'TestPlugin.ArticlesTag') |
||
631 | )), false);
|
||
632 | |||
633 | $Article->ArticlesTag->order = null; |
||
634 | $this->assertTrue($Article->delete(1)); |
||
635 | } |
||
636 | |||
637 | /**
|
||
638 | * testDeleteDependent method
|
||
639 | *
|
||
640 | * @return void
|
||
641 | */
|
||
642 | public function testDeleteDependent() { |
||
643 | $this->loadFixtures('Bidding', 'BiddingMessage', 'Article', |
||
644 | 'ArticlesTag', 'Comment', 'User', 'Attachment' |
||
645 | ); |
||
646 | $Bidding = new Bidding(); |
||
647 | $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC'))); |
||
648 | $expected = array( |
||
649 | array(
|
||
650 | 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'), |
||
651 | 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'), |
||
652 | ), |
||
653 | array(
|
||
654 | 'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'), |
||
655 | 'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'), |
||
656 | ), |
||
657 | array(
|
||
658 | 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'), |
||
659 | 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'), |
||
660 | ), |
||
661 | array(
|
||
662 | 'Bidding' => array('id' => 4, 'bid' => 'Five', 'name' => 'Bid 5'), |
||
663 | 'BiddingMessage' => array('bidding' => '', 'name' => ''), |
||
664 | ), |
||
665 | ); |
||
666 | $this->assertEquals($expected, $result); |
||
667 | |||
668 | $Bidding->delete(4, true); |
||
669 | $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC'))); |
||
670 | $expected = array( |
||
671 | array(
|
||
672 | 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'), |
||
673 | 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'), |
||
674 | ), |
||
675 | array(
|
||
676 | 'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'), |
||
677 | 'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'), |
||
678 | ), |
||
679 | array(
|
||
680 | 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'), |
||
681 | 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'), |
||
682 | ), |
||
683 | ); |
||
684 | $this->assertEquals($expected, $result); |
||
685 | |||
686 | $Bidding->delete(2, true); |
||
687 | $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC'))); |
||
688 | $expected = array( |
||
689 | array(
|
||
690 | 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'), |
||
691 | 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'), |
||
692 | ), |
||
693 | array(
|
||
694 | 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'), |
||
695 | 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'), |
||
696 | ), |
||
697 | ); |
||
698 | $this->assertEquals($expected, $result); |
||
699 | |||
700 | $result = $Bidding->BiddingMessage->find('all', array('order' => array('BiddingMessage.name' => 'ASC'))); |
||
701 | $expected = array( |
||
702 | array(
|
||
703 | 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'), |
||
704 | 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'), |
||
705 | ), |
||
706 | array(
|
||
707 | 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'), |
||
708 | 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'), |
||
709 | ), |
||
710 | array(
|
||
711 | 'BiddingMessage' => array('bidding' => 'Four', 'name' => 'Message 4'), |
||
712 | 'Bidding' => array('id' => '', 'bid' => '', 'name' => ''), |
||
713 | ), |
||
714 | ); |
||
715 | $this->assertEquals($expected, $result); |
||
716 | |||
717 | $Article = new Article(); |
||
718 | $result = $Article->Comment->find('count', array( |
||
719 | 'conditions' => array('Comment.article_id' => 1) |
||
720 | )); |
||
721 | $this->assertEquals(4, $result); |
||
722 | |||
723 | $result = $Article->delete(1, true); |
||
724 | $this->assertTrue($result); |
||
725 | |||
726 | $result = $Article->Comment->find('count', array( |
||
727 | 'conditions' => array('Comment.article_id' => 1) |
||
728 | )); |
||
729 | $this->assertEquals(0, $result); |
||
730 | } |
||
731 | |||
732 | /**
|
||
733 | * test deleteLinks with Multiple habtm associations
|
||
734 | *
|
||
735 | * @return void
|
||
736 | */
|
||
737 | public function testDeleteLinksWithMultipleHabtmAssociations() { |
||
738 | $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); |
||
739 | $JoinA = new JoinA(); |
||
740 | |||
741 | //create two new join records to expose the issue.
|
||
742 | $JoinA->JoinAsJoinC->create(array( |
||
743 | 'join_a_id' => 1, |
||
744 | 'join_c_id' => 2, |
||
745 | )); |
||
746 | $JoinA->JoinAsJoinC->save(); |
||
747 | $JoinA->JoinAsJoinB->create(array( |
||
748 | 'join_a_id' => 1, |
||
749 | 'join_b_id' => 2, |
||
750 | )); |
||
751 | $JoinA->JoinAsJoinB->save(); |
||
752 | |||
753 | $result = $JoinA->delete(1); |
||
754 | $this->assertTrue($result, 'Delete failed %s'); |
||
755 | |||
756 | $joinedBs = $JoinA->JoinAsJoinB->find('count', array( |
||
757 | 'conditions' => array('JoinAsJoinB.join_a_id' => 1) |
||
758 | )); |
||
759 | $this->assertEquals(0, $joinedBs, 'JoinA/JoinB link records left over. %s'); |
||
760 | |||
761 | $joinedBs = $JoinA->JoinAsJoinC->find('count', array( |
||
762 | 'conditions' => array('JoinAsJoinC.join_a_id' => 1) |
||
763 | )); |
||
764 | $this->assertEquals(0, $joinedBs, 'JoinA/JoinC link records left over. %s'); |
||
765 | } |
||
766 | |||
767 | /**
|
||
768 | * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
|
||
769 | *
|
||
770 | * @return void
|
||
771 | */
|
||
772 | public function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() { |
||
773 | $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); |
||
774 | $ThePaper = new ThePaper(); |
||
775 | $ThePaper->id = 1; |
||
776 | $ThePaper->save(array('Monkey' => array(2, 3))); |
||
777 | |||
778 | $result = $ThePaper->findById(1); |
||
779 | $expected = array( |
||
780 | array(
|
||
781 | 'id' => '2', |
||
782 | 'device_type_id' => '1', |
||
783 | 'name' => 'Device 2', |
||
784 | 'typ' => '1' |
||
785 | ), |
||
786 | array(
|
||
787 | 'id' => '3', |
||
788 | 'device_type_id' => '1', |
||
789 | 'name' => 'Device 3', |
||
790 | 'typ' => '2' |
||
791 | )); |
||
792 | $this->assertEquals($expected, $result['Monkey']); |
||
793 | |||
794 | $ThePaper = new ThePaper(); |
||
795 | $ThePaper->id = 2; |
||
796 | $ThePaper->save(array('Monkey' => array(2, 3))); |
||
797 | |||
798 | $result = $ThePaper->findById(2); |
||
799 | $expected = array( |
||
800 | array(
|
||
801 | 'id' => '2', |
||
802 | 'device_type_id' => '1', |
||
803 | 'name' => 'Device 2', |
||
804 | 'typ' => '1' |
||
805 | ), |
||
806 | array(
|
||
807 | 'id' => '3', |
||
808 | 'device_type_id' => '1', |
||
809 | 'name' => 'Device 3', |
||
810 | 'typ' => '2' |
||
811 | )); |
||
812 | $this->assertEquals($expected, $result['Monkey']); |
||
813 | |||
814 | $ThePaper->delete(1); |
||
815 | $result = $ThePaper->findById(2); |
||
816 | $expected = array( |
||
817 | array(
|
||
818 | 'id' => '2', |
||
819 | 'device_type_id' => '1', |
||
820 | 'name' => 'Device 2', |
||
821 | 'typ' => '1' |
||
822 | ), |
||
823 | array(
|
||
824 | 'id' => '3', |
||
825 | 'device_type_id' => '1', |
||
826 | 'name' => 'Device 3', |
||
827 | 'typ' => '2' |
||
828 | )); |
||
829 | $this->assertEquals($expected, $result['Monkey']); |
||
830 | } |
||
831 | |||
832 | /**
|
||
833 | * test that beforeDelete returning false can abort deletion.
|
||
834 | *
|
||
835 | * @return void
|
||
836 | */
|
||
837 | public function testBeforeDeleteDeleteAbortion() { |
||
838 | $this->loadFixtures('Post'); |
||
839 | $Model = new CallbackPostTestModel(); |
||
840 | $Model->beforeDeleteReturn = false; |
||
841 | |||
842 | $result = $Model->delete(1); |
||
843 | $this->assertFalse($result); |
||
844 | |||
845 | $exists = $Model->findById(1); |
||
846 | $this->assertTrue(is_array($exists)); |
||
847 | } |
||
848 | |||
849 | /**
|
||
850 | * test for a habtm deletion error that occurs in postgres but should not.
|
||
851 | * And should not occur in any dbo.
|
||
852 | *
|
||
853 | * @return void
|
||
854 | */
|
||
855 | public function testDeleteHabtmPostgresFailure() { |
||
856 | $this->loadFixtures('Article', 'Tag', 'ArticlesTag'); |
||
857 | |||
858 | $Article = ClassRegistry::init('Article'); |
||
859 | $Article->hasAndBelongsToMany['Tag']['unique'] = true; |
||
860 | |||
861 | $Tag = ClassRegistry::init('Tag'); |
||
862 | $Tag->bindModel(array('hasAndBelongsToMany' => array( |
||
863 | 'Article' => array( |
||
864 | 'className' => 'Article', |
||
865 | 'unique' => true |
||
866 | ) |
||
867 | )), true);
|
||
868 | |||
869 | // Article 1 should have Tag.1 and Tag.2
|
||
870 | $before = $Article->find("all", array( |
||
871 | "conditions" => array("Article.id" => 1), |
||
872 | )); |
||
873 | $this->assertEquals(2, count($before[0]['Tag']), 'Tag count for Article.id = 1 is incorrect, should be 2 %s'); |
||
874 | |||
875 | // From now on, Tag #1 is only associated with Post #1
|
||
876 | $submittedData = array( |
||
877 | "Tag" => array("id" => 1, 'tag' => 'tag1'), |
||
878 | "Article" => array( |
||
879 | "Article" => array(1) |
||
880 | ) |
||
881 | ); |
||
882 | $Tag->save($submittedData); |
||
883 | |||
884 | // One more submission (The other way around) to make sure the reverse save looks good.
|
||
885 | $submittedData = array( |
||
886 | "Article" => array("id" => 2, 'title' => 'second article'), |
||
887 | "Tag" => array( |
||
888 | "Tag" => array(2, 3) |
||
889 | ) |
||
890 | ); |
||
891 | |||
892 | // ERROR:
|
||
893 | // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
|
||
894 | // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
|
||
895 | $Article->save($submittedData); |
||
896 | |||
897 | // Want to make sure Article #1 has Tag #1 and Tag #2 still.
|
||
898 | $after = $Article->find("all", array( |
||
899 | "conditions" => array("Article.id" => 1), |
||
900 | )); |
||
901 | |||
902 | // Removing Article #2 from Tag #1 is all that should have happened.
|
||
903 | $this->assertEquals(count($before[0]["Tag"]), count($after[0]["Tag"])); |
||
904 | } |
||
905 | |||
906 | /**
|
||
907 | * test that deleting records inside the beforeDelete doesn't truncate the table.
|
||
908 | *
|
||
909 | * @return void
|
||
910 | */
|
||
911 | public function testBeforeDeleteWipingTable() { |
||
912 | $this->loadFixtures('Comment'); |
||
913 | |||
914 | $Comment = new BeforeDeleteComment(); |
||
915 | // Delete 3 records.
|
||
916 | $Comment->delete(4); |
||
917 | $result = $Comment->find('count'); |
||
918 | |||
919 | $this->assertTrue($result > 1, 'Comments are all gone.'); |
||
920 | $Comment->create(array( |
||
921 | 'article_id' => 1, |
||
922 | 'user_id' => 2, |
||
923 | 'comment' => 'new record', |
||
924 | 'published' => 'Y' |
||
925 | )); |
||
926 | $Comment->save();
|
||
927 | |||
928 | $Comment->delete(5); |
||
929 | $result = $Comment->find('count'); |
||
930 | |||
931 | $this->assertTrue($result > 1, 'Comments are all gone.'); |
||
932 | } |
||
933 | |||
934 | /**
|
||
935 | * test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
|
||
936 | *
|
||
937 | * @return void
|
||
938 | */
|
||
939 | public function testBeforeDeleteWipingTableWithDuplicateDelete() { |
||
940 | $this->loadFixtures('Comment'); |
||
941 | |||
942 | $Comment = new BeforeDeleteComment(); |
||
943 | $Comment->delete(1); |
||
944 | |||
945 | $result = $Comment->find('count'); |
||
946 | $this->assertTrue($result > 1, 'Comments are all gone.'); |
||
947 | } |
||
948 | } |