| 13532 |
anikendra |
1 |
<?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 |
$this->getMock('Article', array('find'), array(), 'ArticleDeleteAll');
|
|
|
447 |
|
|
|
448 |
$TestModel = new ArticleDeleteAll();
|
|
|
449 |
$TestModel->expects($this->once())
|
|
|
450 |
->method('find')
|
|
|
451 |
->will($this->returnValue(null));
|
|
|
452 |
|
|
|
453 |
$result = $TestModel->deleteAll(array('Article.user_id' => 999));
|
|
|
454 |
$this->assertFalse($result);
|
|
|
455 |
}
|
|
|
456 |
|
|
|
457 |
/**
|
|
|
458 |
* testDeleteAllMultipleRowsPerId method
|
|
|
459 |
*
|
|
|
460 |
* Ensure find done in deleteAll only returns distinct ids. A wacky combination
|
|
|
461 |
* of association and conditions can sometimes generate multiple rows per id.
|
|
|
462 |
*
|
|
|
463 |
* @return void
|
|
|
464 |
*/
|
|
|
465 |
public function testDeleteAllMultipleRowsPerId() {
|
|
|
466 |
$this->loadFixtures('Article', 'User');
|
|
|
467 |
|
|
|
468 |
$TestModel = new Article();
|
|
|
469 |
$TestModel->unbindModel(array(
|
|
|
470 |
'belongsTo' => array('User'),
|
|
|
471 |
'hasMany' => array('Comment'),
|
|
|
472 |
'hasAndBelongsToMany' => array('Tag')
|
|
|
473 |
), false);
|
|
|
474 |
$TestModel->bindModel(array(
|
|
|
475 |
'belongsTo' => array(
|
|
|
476 |
'User' => array(
|
|
|
477 |
'foreignKey' => false,
|
|
|
478 |
'conditions' => array(
|
|
|
479 |
'Article.user_id = 1'
|
|
|
480 |
)
|
|
|
481 |
)
|
|
|
482 |
)
|
|
|
483 |
), false);
|
|
|
484 |
|
|
|
485 |
$result = $TestModel->deleteAll(
|
|
|
486 |
array('Article.user_id' => array(1, 3)),
|
|
|
487 |
true,
|
|
|
488 |
true
|
|
|
489 |
);
|
|
|
490 |
|
|
|
491 |
$this->assertTrue($result);
|
|
|
492 |
}
|
|
|
493 |
|
|
|
494 |
/**
|
|
|
495 |
* testDeleteAllWithOrderProperty
|
|
|
496 |
*
|
|
|
497 |
* Ensure find done in deleteAll works with models that has $order property set
|
|
|
498 |
*
|
|
|
499 |
* @return void
|
|
|
500 |
*/
|
|
|
501 |
public function testDeleteAllWithOrderProperty() {
|
|
|
502 |
$this->loadFixtures('Article', 'User');
|
|
|
503 |
|
|
|
504 |
$TestModel = new Article();
|
|
|
505 |
$TestModel->order = 'Article.published desc';
|
|
|
506 |
$TestModel->unbindModel(array(
|
|
|
507 |
'belongsTo' => array('User'),
|
|
|
508 |
'hasMany' => array('Comment'),
|
|
|
509 |
'hasAndBelongsToMany' => array('Tag')
|
|
|
510 |
), false);
|
|
|
511 |
|
|
|
512 |
$result = $TestModel->deleteAll(
|
|
|
513 |
array('Article.user_id' => array(1, 3)),
|
|
|
514 |
true,
|
|
|
515 |
true
|
|
|
516 |
);
|
|
|
517 |
|
|
|
518 |
$this->assertTrue($result);
|
|
|
519 |
}
|
|
|
520 |
|
|
|
521 |
/**
|
|
|
522 |
* testRecursiveDel method
|
|
|
523 |
*
|
|
|
524 |
* @return void
|
|
|
525 |
*/
|
|
|
526 |
public function testRecursiveDel() {
|
|
|
527 |
$this->loadFixtures('Article', 'Comment', 'Attachment');
|
|
|
528 |
$TestModel = new Article();
|
|
|
529 |
|
|
|
530 |
$result = $TestModel->delete(2);
|
|
|
531 |
$this->assertTrue($result);
|
|
|
532 |
|
|
|
533 |
$TestModel->recursive = 2;
|
|
|
534 |
$result = $TestModel->read(null, 2);
|
|
|
535 |
$this->assertSame(array(), $result);
|
|
|
536 |
|
|
|
537 |
$result = $TestModel->Comment->read(null, 5);
|
|
|
538 |
$this->assertSame(array(), $result);
|
|
|
539 |
|
|
|
540 |
$result = $TestModel->Comment->read(null, 6);
|
|
|
541 |
$this->assertSame(array(), $result);
|
|
|
542 |
|
|
|
543 |
$result = $TestModel->Comment->Attachment->read(null, 1);
|
|
|
544 |
$this->assertSame(array(), $result);
|
|
|
545 |
|
|
|
546 |
$result = $TestModel->find('count');
|
|
|
547 |
$this->assertEquals(2, $result);
|
|
|
548 |
|
|
|
549 |
$result = $TestModel->Comment->find('count');
|
|
|
550 |
$this->assertEquals(4, $result);
|
|
|
551 |
|
|
|
552 |
$result = $TestModel->Comment->Attachment->find('count');
|
|
|
553 |
$this->assertEquals(0, $result);
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
/**
|
|
|
557 |
* testDependentExclusiveDelete method
|
|
|
558 |
*
|
|
|
559 |
* @return void
|
|
|
560 |
*/
|
|
|
561 |
public function testDependentExclusiveDelete() {
|
|
|
562 |
$this->loadFixtures('Article', 'Comment');
|
|
|
563 |
$TestModel = new Article10();
|
|
|
564 |
|
|
|
565 |
$result = $TestModel->find('all');
|
|
|
566 |
$this->assertEquals(4, count($result[0]['Comment']));
|
|
|
567 |
$this->assertEquals(2, count($result[1]['Comment']));
|
|
|
568 |
$this->assertEquals(6, $TestModel->Comment->find('count'));
|
|
|
569 |
|
|
|
570 |
$TestModel->delete(1);
|
|
|
571 |
$this->assertEquals(2, $TestModel->Comment->find('count'));
|
|
|
572 |
}
|
|
|
573 |
|
|
|
574 |
/**
|
|
|
575 |
* testDeleteLinks method
|
|
|
576 |
*
|
|
|
577 |
* @return void
|
|
|
578 |
*/
|
|
|
579 |
public function testDeleteLinks() {
|
|
|
580 |
$this->loadFixtures('Article', 'ArticlesTag', 'Tag');
|
|
|
581 |
$TestModel = new Article();
|
|
|
582 |
|
|
|
583 |
$result = $TestModel->ArticlesTag->find('all');
|
|
|
584 |
$expected = array(
|
|
|
585 |
array('ArticlesTag' => array(
|
|
|
586 |
'article_id' => '1',
|
|
|
587 |
'tag_id' => '1'
|
|
|
588 |
)),
|
|
|
589 |
array('ArticlesTag' => array(
|
|
|
590 |
'article_id' => '1',
|
|
|
591 |
'tag_id' => '2'
|
|
|
592 |
)),
|
|
|
593 |
array('ArticlesTag' => array(
|
|
|
594 |
'article_id' => '2',
|
|
|
595 |
'tag_id' => '1'
|
|
|
596 |
)),
|
|
|
597 |
array('ArticlesTag' => array(
|
|
|
598 |
'article_id' => '2',
|
|
|
599 |
'tag_id' => '3'
|
|
|
600 |
)));
|
|
|
601 |
$this->assertEquals($expected, $result);
|
|
|
602 |
|
|
|
603 |
$TestModel->delete(1);
|
|
|
604 |
$result = $TestModel->ArticlesTag->find('all');
|
|
|
605 |
|
|
|
606 |
$expected = array(
|
|
|
607 |
array('ArticlesTag' => array(
|
|
|
608 |
'article_id' => '2',
|
|
|
609 |
'tag_id' => '1'
|
|
|
610 |
)),
|
|
|
611 |
array('ArticlesTag' => array(
|
|
|
612 |
'article_id' => '2',
|
|
|
613 |
'tag_id' => '3'
|
|
|
614 |
)));
|
|
|
615 |
$this->assertEquals($expected, $result);
|
|
|
616 |
|
|
|
617 |
$result = $TestModel->deleteAll(array('Article.user_id' => 999));
|
|
|
618 |
$this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
|
|
|
619 |
}
|
|
|
620 |
|
|
|
621 |
/**
|
|
|
622 |
* test that a plugin model as the 'with' model doesn't have issues
|
|
|
623 |
*
|
|
|
624 |
* @return void
|
|
|
625 |
*/
|
|
|
626 |
public function testDeleteLinksWithPLuginJoinModel() {
|
|
|
627 |
$this->loadFixtures('Article', 'ArticlesTag', 'Tag');
|
|
|
628 |
$Article = new Article();
|
|
|
629 |
$Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')), false);
|
|
|
630 |
unset($Article->Tag, $Article->ArticleTags);
|
|
|
631 |
$Article->bindModel(array('hasAndBelongsToMany' => array(
|
|
|
632 |
'Tag' => array('with' => 'TestPlugin.ArticlesTag')
|
|
|
633 |
)), false);
|
|
|
634 |
|
|
|
635 |
$Article->ArticlesTag->order = null;
|
|
|
636 |
$this->assertTrue($Article->delete(1));
|
|
|
637 |
}
|
|
|
638 |
|
|
|
639 |
/**
|
|
|
640 |
* testDeleteDependent method
|
|
|
641 |
*
|
|
|
642 |
* @return void
|
|
|
643 |
*/
|
|
|
644 |
public function testDeleteDependent() {
|
|
|
645 |
$this->loadFixtures('Bidding', 'BiddingMessage', 'Article',
|
|
|
646 |
'ArticlesTag', 'Comment', 'User', 'Attachment'
|
|
|
647 |
);
|
|
|
648 |
$Bidding = new Bidding();
|
|
|
649 |
$result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
|
|
|
650 |
$expected = array(
|
|
|
651 |
array(
|
|
|
652 |
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
|
|
|
653 |
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
|
|
|
654 |
),
|
|
|
655 |
array(
|
|
|
656 |
'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
|
|
|
657 |
'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
|
|
|
658 |
),
|
|
|
659 |
array(
|
|
|
660 |
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
|
|
|
661 |
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
|
|
|
662 |
),
|
|
|
663 |
array(
|
|
|
664 |
'Bidding' => array('id' => 4, 'bid' => 'Five', 'name' => 'Bid 5'),
|
|
|
665 |
'BiddingMessage' => array('bidding' => '', 'name' => ''),
|
|
|
666 |
),
|
|
|
667 |
);
|
|
|
668 |
$this->assertEquals($expected, $result);
|
|
|
669 |
|
|
|
670 |
$Bidding->delete(4, true);
|
|
|
671 |
$result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
|
|
|
672 |
$expected = array(
|
|
|
673 |
array(
|
|
|
674 |
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
|
|
|
675 |
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
|
|
|
676 |
),
|
|
|
677 |
array(
|
|
|
678 |
'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
|
|
|
679 |
'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
|
|
|
680 |
),
|
|
|
681 |
array(
|
|
|
682 |
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
|
|
|
683 |
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
|
|
|
684 |
),
|
|
|
685 |
);
|
|
|
686 |
$this->assertEquals($expected, $result);
|
|
|
687 |
|
|
|
688 |
$Bidding->delete(2, true);
|
|
|
689 |
$result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
|
|
|
690 |
$expected = array(
|
|
|
691 |
array(
|
|
|
692 |
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
|
|
|
693 |
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
|
|
|
694 |
),
|
|
|
695 |
array(
|
|
|
696 |
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
|
|
|
697 |
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
|
|
|
698 |
),
|
|
|
699 |
);
|
|
|
700 |
$this->assertEquals($expected, $result);
|
|
|
701 |
|
|
|
702 |
$result = $Bidding->BiddingMessage->find('all', array('order' => array('BiddingMessage.name' => 'ASC')));
|
|
|
703 |
$expected = array(
|
|
|
704 |
array(
|
|
|
705 |
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
|
|
|
706 |
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
|
|
|
707 |
),
|
|
|
708 |
array(
|
|
|
709 |
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
|
|
|
710 |
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
|
|
|
711 |
),
|
|
|
712 |
array(
|
|
|
713 |
'BiddingMessage' => array('bidding' => 'Four', 'name' => 'Message 4'),
|
|
|
714 |
'Bidding' => array('id' => '', 'bid' => '', 'name' => ''),
|
|
|
715 |
),
|
|
|
716 |
);
|
|
|
717 |
$this->assertEquals($expected, $result);
|
|
|
718 |
|
|
|
719 |
$Article = new Article();
|
|
|
720 |
$result = $Article->Comment->find('count', array(
|
|
|
721 |
'conditions' => array('Comment.article_id' => 1)
|
|
|
722 |
));
|
|
|
723 |
$this->assertEquals(4, $result);
|
|
|
724 |
|
|
|
725 |
$result = $Article->delete(1, true);
|
|
|
726 |
$this->assertTrue($result);
|
|
|
727 |
|
|
|
728 |
$result = $Article->Comment->find('count', array(
|
|
|
729 |
'conditions' => array('Comment.article_id' => 1)
|
|
|
730 |
));
|
|
|
731 |
$this->assertEquals(0, $result);
|
|
|
732 |
}
|
|
|
733 |
|
|
|
734 |
/**
|
|
|
735 |
* test deleteLinks with Multiple habtm associations
|
|
|
736 |
*
|
|
|
737 |
* @return void
|
|
|
738 |
*/
|
|
|
739 |
public function testDeleteLinksWithMultipleHabtmAssociations() {
|
|
|
740 |
$this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
|
|
|
741 |
$JoinA = new JoinA();
|
|
|
742 |
|
|
|
743 |
//create two new join records to expose the issue.
|
|
|
744 |
$JoinA->JoinAsJoinC->create(array(
|
|
|
745 |
'join_a_id' => 1,
|
|
|
746 |
'join_c_id' => 2,
|
|
|
747 |
));
|
|
|
748 |
$JoinA->JoinAsJoinC->save();
|
|
|
749 |
$JoinA->JoinAsJoinB->create(array(
|
|
|
750 |
'join_a_id' => 1,
|
|
|
751 |
'join_b_id' => 2,
|
|
|
752 |
));
|
|
|
753 |
$JoinA->JoinAsJoinB->save();
|
|
|
754 |
|
|
|
755 |
$result = $JoinA->delete(1);
|
|
|
756 |
$this->assertTrue($result, 'Delete failed %s');
|
|
|
757 |
|
|
|
758 |
$joinedBs = $JoinA->JoinAsJoinB->find('count', array(
|
|
|
759 |
'conditions' => array('JoinAsJoinB.join_a_id' => 1)
|
|
|
760 |
));
|
|
|
761 |
$this->assertEquals(0, $joinedBs, 'JoinA/JoinB link records left over. %s');
|
|
|
762 |
|
|
|
763 |
$joinedBs = $JoinA->JoinAsJoinC->find('count', array(
|
|
|
764 |
'conditions' => array('JoinAsJoinC.join_a_id' => 1)
|
|
|
765 |
));
|
|
|
766 |
$this->assertEquals(0, $joinedBs, 'JoinA/JoinC link records left over. %s');
|
|
|
767 |
}
|
|
|
768 |
|
|
|
769 |
/**
|
|
|
770 |
* testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
|
|
|
771 |
*
|
|
|
772 |
* @return void
|
|
|
773 |
*/
|
|
|
774 |
public function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
|
|
|
775 |
$this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
|
|
|
776 |
$ThePaper = new ThePaper();
|
|
|
777 |
$ThePaper->id = 1;
|
|
|
778 |
$ThePaper->save(array('Monkey' => array(2, 3)));
|
|
|
779 |
|
|
|
780 |
$result = $ThePaper->findById(1);
|
|
|
781 |
$expected = array(
|
|
|
782 |
array(
|
|
|
783 |
'id' => '2',
|
|
|
784 |
'device_type_id' => '1',
|
|
|
785 |
'name' => 'Device 2',
|
|
|
786 |
'typ' => '1'
|
|
|
787 |
),
|
|
|
788 |
array(
|
|
|
789 |
'id' => '3',
|
|
|
790 |
'device_type_id' => '1',
|
|
|
791 |
'name' => 'Device 3',
|
|
|
792 |
'typ' => '2'
|
|
|
793 |
));
|
|
|
794 |
$this->assertEquals($expected, $result['Monkey']);
|
|
|
795 |
|
|
|
796 |
$ThePaper = new ThePaper();
|
|
|
797 |
$ThePaper->id = 2;
|
|
|
798 |
$ThePaper->save(array('Monkey' => array(2, 3)));
|
|
|
799 |
|
|
|
800 |
$result = $ThePaper->findById(2);
|
|
|
801 |
$expected = array(
|
|
|
802 |
array(
|
|
|
803 |
'id' => '2',
|
|
|
804 |
'device_type_id' => '1',
|
|
|
805 |
'name' => 'Device 2',
|
|
|
806 |
'typ' => '1'
|
|
|
807 |
),
|
|
|
808 |
array(
|
|
|
809 |
'id' => '3',
|
|
|
810 |
'device_type_id' => '1',
|
|
|
811 |
'name' => 'Device 3',
|
|
|
812 |
'typ' => '2'
|
|
|
813 |
));
|
|
|
814 |
$this->assertEquals($expected, $result['Monkey']);
|
|
|
815 |
|
|
|
816 |
$ThePaper->delete(1);
|
|
|
817 |
$result = $ThePaper->findById(2);
|
|
|
818 |
$expected = array(
|
|
|
819 |
array(
|
|
|
820 |
'id' => '2',
|
|
|
821 |
'device_type_id' => '1',
|
|
|
822 |
'name' => 'Device 2',
|
|
|
823 |
'typ' => '1'
|
|
|
824 |
),
|
|
|
825 |
array(
|
|
|
826 |
'id' => '3',
|
|
|
827 |
'device_type_id' => '1',
|
|
|
828 |
'name' => 'Device 3',
|
|
|
829 |
'typ' => '2'
|
|
|
830 |
));
|
|
|
831 |
$this->assertEquals($expected, $result['Monkey']);
|
|
|
832 |
}
|
|
|
833 |
|
|
|
834 |
/**
|
|
|
835 |
* test that beforeDelete returning false can abort deletion.
|
|
|
836 |
*
|
|
|
837 |
* @return void
|
|
|
838 |
*/
|
|
|
839 |
public function testBeforeDeleteDeleteAbortion() {
|
|
|
840 |
$this->loadFixtures('Post');
|
|
|
841 |
$Model = new CallbackPostTestModel();
|
|
|
842 |
$Model->beforeDeleteReturn = false;
|
|
|
843 |
|
|
|
844 |
$result = $Model->delete(1);
|
|
|
845 |
$this->assertFalse($result);
|
|
|
846 |
|
|
|
847 |
$exists = $Model->findById(1);
|
|
|
848 |
$this->assertTrue(is_array($exists));
|
|
|
849 |
}
|
|
|
850 |
|
|
|
851 |
/**
|
|
|
852 |
* test for a habtm deletion error that occurs in postgres but should not.
|
|
|
853 |
* And should not occur in any dbo.
|
|
|
854 |
*
|
|
|
855 |
* @return void
|
|
|
856 |
*/
|
|
|
857 |
public function testDeleteHabtmPostgresFailure() {
|
|
|
858 |
$this->loadFixtures('Article', 'Tag', 'ArticlesTag');
|
|
|
859 |
|
|
|
860 |
$Article = ClassRegistry::init('Article');
|
|
|
861 |
$Article->hasAndBelongsToMany['Tag']['unique'] = true;
|
|
|
862 |
|
|
|
863 |
$Tag = ClassRegistry::init('Tag');
|
|
|
864 |
$Tag->bindModel(array('hasAndBelongsToMany' => array(
|
|
|
865 |
'Article' => array(
|
|
|
866 |
'className' => 'Article',
|
|
|
867 |
'unique' => true
|
|
|
868 |
)
|
|
|
869 |
)), true);
|
|
|
870 |
|
|
|
871 |
// Article 1 should have Tag.1 and Tag.2
|
|
|
872 |
$before = $Article->find("all", array(
|
|
|
873 |
"conditions" => array("Article.id" => 1),
|
|
|
874 |
));
|
|
|
875 |
$this->assertEquals(2, count($before[0]['Tag']), 'Tag count for Article.id = 1 is incorrect, should be 2 %s');
|
|
|
876 |
|
|
|
877 |
// From now on, Tag #1 is only associated with Post #1
|
|
|
878 |
$submittedData = array(
|
|
|
879 |
"Tag" => array("id" => 1, 'tag' => 'tag1'),
|
|
|
880 |
"Article" => array(
|
|
|
881 |
"Article" => array(1)
|
|
|
882 |
)
|
|
|
883 |
);
|
|
|
884 |
$Tag->save($submittedData);
|
|
|
885 |
|
|
|
886 |
// One more submission (The other way around) to make sure the reverse save looks good.
|
|
|
887 |
$submittedData = array(
|
|
|
888 |
"Article" => array("id" => 2, 'title' => 'second article'),
|
|
|
889 |
"Tag" => array(
|
|
|
890 |
"Tag" => array(2, 3)
|
|
|
891 |
)
|
|
|
892 |
);
|
|
|
893 |
|
|
|
894 |
// ERROR:
|
|
|
895 |
// Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
|
|
|
896 |
// MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
|
|
|
897 |
$Article->save($submittedData);
|
|
|
898 |
|
|
|
899 |
// Want to make sure Article #1 has Tag #1 and Tag #2 still.
|
|
|
900 |
$after = $Article->find("all", array(
|
|
|
901 |
"conditions" => array("Article.id" => 1),
|
|
|
902 |
));
|
|
|
903 |
|
|
|
904 |
// Removing Article #2 from Tag #1 is all that should have happened.
|
|
|
905 |
$this->assertEquals(count($before[0]["Tag"]), count($after[0]["Tag"]));
|
|
|
906 |
}
|
|
|
907 |
|
|
|
908 |
/**
|
|
|
909 |
* test that deleting records inside the beforeDelete doesn't truncate the table.
|
|
|
910 |
*
|
|
|
911 |
* @return void
|
|
|
912 |
*/
|
|
|
913 |
public function testBeforeDeleteWipingTable() {
|
|
|
914 |
$this->loadFixtures('Comment');
|
|
|
915 |
|
|
|
916 |
$Comment = new BeforeDeleteComment();
|
|
|
917 |
// Delete 3 records.
|
|
|
918 |
$Comment->delete(4);
|
|
|
919 |
$result = $Comment->find('count');
|
|
|
920 |
|
|
|
921 |
$this->assertTrue($result > 1, 'Comments are all gone.');
|
|
|
922 |
$Comment->create(array(
|
|
|
923 |
'article_id' => 1,
|
|
|
924 |
'user_id' => 2,
|
|
|
925 |
'comment' => 'new record',
|
|
|
926 |
'published' => 'Y'
|
|
|
927 |
));
|
|
|
928 |
$Comment->save();
|
|
|
929 |
|
|
|
930 |
$Comment->delete(5);
|
|
|
931 |
$result = $Comment->find('count');
|
|
|
932 |
|
|
|
933 |
$this->assertTrue($result > 1, 'Comments are all gone.');
|
|
|
934 |
}
|
|
|
935 |
|
|
|
936 |
/**
|
|
|
937 |
* test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
|
|
|
938 |
*
|
|
|
939 |
* @return void
|
|
|
940 |
*/
|
|
|
941 |
public function testBeforeDeleteWipingTableWithDuplicateDelete() {
|
|
|
942 |
$this->loadFixtures('Comment');
|
|
|
943 |
|
|
|
944 |
$Comment = new BeforeDeleteComment();
|
|
|
945 |
$Comment->delete(1);
|
|
|
946 |
|
|
|
947 |
$result = $Comment->find('count');
|
|
|
948 |
$this->assertTrue($result > 1, 'Comments are all gone.');
|
|
|
949 |
}
|
|
|
950 |
}
|