ModelDeleteTest.php 22 KB

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