ModelDeleteTest.php 23 KB

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