ModelDeleteTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 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/2.0/en/development/testing.html 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($expected, $result['Item']);
  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->assertSame(array(), $result);
  104. $result = $Portfolio->ItemsPortfolio->find('all', array(
  105. 'conditions' => array('ItemsPortfolio.portfolio_id' => 1)
  106. ));
  107. $this->assertSame(array(), $result);
  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->assertSame(array(), $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->assertSame(array(), $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(0, $result['User']['post_count']);
  244. $result = $User->read(null, 66);
  245. $this->assertEquals(2, $result['User']['post_count']);
  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. * testDeleteAllFailedFind method
  393. *
  394. * Eg: Behavior callback stops the event, find returns null
  395. *
  396. * @return void
  397. */
  398. public function testDeleteAllFailedFind() {
  399. $this->loadFixtures('Article');
  400. $this->getMock('Article', array('find'), array(), 'ArticleDeleteAll');
  401. $TestModel = new ArticleDeleteAll();
  402. $TestModel->expects($this->once())
  403. ->method('find')
  404. ->will($this->returnValue(null));
  405. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  406. $this->assertFalse($result);
  407. }
  408. /**
  409. * testRecursiveDel method
  410. *
  411. * @return void
  412. */
  413. public function testRecursiveDel() {
  414. $this->loadFixtures('Article', 'Comment', 'Attachment');
  415. $TestModel = new Article();
  416. $result = $TestModel->delete(2);
  417. $this->assertTrue($result);
  418. $TestModel->recursive = 2;
  419. $result = $TestModel->read(null, 2);
  420. $this->assertSame(array(), $result);
  421. $result = $TestModel->Comment->read(null, 5);
  422. $this->assertSame(array(), $result);
  423. $result = $TestModel->Comment->read(null, 6);
  424. $this->assertSame(array(), $result);
  425. $result = $TestModel->Comment->Attachment->read(null, 1);
  426. $this->assertSame(array(), $result);
  427. $result = $TestModel->find('count');
  428. $this->assertEquals(2, $result);
  429. $result = $TestModel->Comment->find('count');
  430. $this->assertEquals(4, $result);
  431. $result = $TestModel->Comment->Attachment->find('count');
  432. $this->assertEquals(0, $result);
  433. }
  434. /**
  435. * testDependentExclusiveDelete method
  436. *
  437. * @return void
  438. */
  439. public function testDependentExclusiveDelete() {
  440. $this->loadFixtures('Article', 'Comment');
  441. $TestModel = new Article10();
  442. $result = $TestModel->find('all');
  443. $this->assertEquals(4, count($result[0]['Comment']));
  444. $this->assertEquals(2, count($result[1]['Comment']));
  445. $this->assertEquals(6, $TestModel->Comment->find('count'));
  446. $TestModel->delete(1);
  447. $this->assertEquals(2, $TestModel->Comment->find('count'));
  448. }
  449. /**
  450. * testDeleteLinks method
  451. *
  452. * @return void
  453. */
  454. public function testDeleteLinks() {
  455. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  456. $TestModel = new Article();
  457. $result = $TestModel->ArticlesTag->find('all');
  458. $expected = array(
  459. array('ArticlesTag' => array(
  460. 'article_id' => '1',
  461. 'tag_id' => '1'
  462. )),
  463. array('ArticlesTag' => array(
  464. 'article_id' => '1',
  465. 'tag_id' => '2'
  466. )),
  467. array('ArticlesTag' => array(
  468. 'article_id' => '2',
  469. 'tag_id' => '1'
  470. )),
  471. array('ArticlesTag' => array(
  472. 'article_id' => '2',
  473. 'tag_id' => '3'
  474. )));
  475. $this->assertEquals($expected, $result);
  476. $TestModel->delete(1);
  477. $result = $TestModel->ArticlesTag->find('all');
  478. $expected = array(
  479. array('ArticlesTag' => array(
  480. 'article_id' => '2',
  481. 'tag_id' => '1'
  482. )),
  483. array('ArticlesTag' => array(
  484. 'article_id' => '2',
  485. 'tag_id' => '3'
  486. )));
  487. $this->assertEquals($expected, $result);
  488. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  489. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  490. }
  491. /**
  492. * test that a plugin model as the 'with' model doesn't have issues
  493. *
  494. * @return void
  495. */
  496. public function testDeleteLinksWithPLuginJoinModel() {
  497. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  498. $Article = new Article();
  499. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')), false);
  500. unset($Article->Tag, $Article->ArticleTags);
  501. $Article->bindModel(array('hasAndBelongsToMany' => array(
  502. 'Tag' => array('with' => 'TestPlugin.ArticlesTag')
  503. )), false);
  504. $this->assertTrue($Article->delete(1));
  505. }
  506. /**
  507. * testDeleteDependent method
  508. *
  509. * @return void
  510. */
  511. public function testDeleteDependent() {
  512. $this->loadFixtures('Bidding', 'BiddingMessage', 'Article',
  513. 'ArticlesTag', 'Comment', 'User', 'Attachment'
  514. );
  515. $Bidding = new Bidding();
  516. $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
  517. $expected = array(
  518. array(
  519. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  520. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  521. ),
  522. array(
  523. 'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
  524. 'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
  525. ),
  526. array(
  527. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  528. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  529. ),
  530. array(
  531. 'Bidding' => array('id' => 4, 'bid' => 'Five', 'name' => 'Bid 5'),
  532. 'BiddingMessage' => array('bidding' => '', 'name' => ''),
  533. ),
  534. );
  535. $this->assertEquals($expected, $result);
  536. $Bidding->delete(4, 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' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
  545. 'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
  546. ),
  547. array(
  548. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  549. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  550. ),
  551. );
  552. $this->assertEquals($expected, $result);
  553. $Bidding->delete(2, true);
  554. $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
  555. $expected = array(
  556. array(
  557. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  558. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  559. ),
  560. array(
  561. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  562. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  563. ),
  564. );
  565. $this->assertEquals($expected, $result);
  566. $result = $Bidding->BiddingMessage->find('all', array('order' => array('BiddingMessage.name' => 'ASC')));
  567. $expected = array(
  568. array(
  569. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  570. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  571. ),
  572. array(
  573. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  574. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  575. ),
  576. array(
  577. 'BiddingMessage' => array('bidding' => 'Four', 'name' => 'Message 4'),
  578. 'Bidding' => array('id' => '', 'bid' => '', 'name' => ''),
  579. ),
  580. );
  581. $this->assertEquals($expected, $result);
  582. $Article = new Article();
  583. $result = $Article->Comment->find('count', array(
  584. 'conditions' => array('Comment.article_id' => 1)
  585. ));
  586. $this->assertEquals(4, $result);
  587. $result = $Article->delete(1, true);
  588. $this->assertSame($result, true);
  589. $result = $Article->Comment->find('count', array(
  590. 'conditions' => array('Comment.article_id' => 1)
  591. ));
  592. $this->assertEquals(0, $result);
  593. }
  594. /**
  595. * test deleteLinks with Multiple habtm associations
  596. *
  597. * @return void
  598. */
  599. public function testDeleteLinksWithMultipleHabtmAssociations() {
  600. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  601. $JoinA = new JoinA();
  602. //create two new join records to expose the issue.
  603. $JoinA->JoinAsJoinC->create(array(
  604. 'join_a_id' => 1,
  605. 'join_c_id' => 2,
  606. ));
  607. $JoinA->JoinAsJoinC->save();
  608. $JoinA->JoinAsJoinB->create(array(
  609. 'join_a_id' => 1,
  610. 'join_b_id' => 2,
  611. ));
  612. $JoinA->JoinAsJoinB->save();
  613. $result = $JoinA->delete(1);
  614. $this->assertTrue($result, 'Delete failed %s');
  615. $joinedBs = $JoinA->JoinAsJoinB->find('count', array(
  616. 'conditions' => array('JoinAsJoinB.join_a_id' => 1)
  617. ));
  618. $this->assertEquals(0, $joinedBs, 'JoinA/JoinB link records left over. %s');
  619. $joinedBs = $JoinA->JoinAsJoinC->find('count', array(
  620. 'conditions' => array('JoinAsJoinC.join_a_id' => 1)
  621. ));
  622. $this->assertEquals(0, $joinedBs, 'JoinA/JoinC link records left over. %s');
  623. }
  624. /**
  625. * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
  626. *
  627. * @return void
  628. */
  629. public function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
  630. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  631. $ThePaper = new ThePaper();
  632. $ThePaper->id = 1;
  633. $ThePaper->save(array('Monkey' => array(2, 3)));
  634. $result = $ThePaper->findById(1);
  635. $expected = array(
  636. array(
  637. 'id' => '2',
  638. 'device_type_id' => '1',
  639. 'name' => 'Device 2',
  640. 'typ' => '1'
  641. ),
  642. array(
  643. 'id' => '3',
  644. 'device_type_id' => '1',
  645. 'name' => 'Device 3',
  646. 'typ' => '2'
  647. ));
  648. $this->assertEquals($expected, $result['Monkey']);
  649. $ThePaper = new ThePaper();
  650. $ThePaper->id = 2;
  651. $ThePaper->save(array('Monkey' => array(2, 3)));
  652. $result = $ThePaper->findById(2);
  653. $expected = array(
  654. array(
  655. 'id' => '2',
  656. 'device_type_id' => '1',
  657. 'name' => 'Device 2',
  658. 'typ' => '1'
  659. ),
  660. array(
  661. 'id' => '3',
  662. 'device_type_id' => '1',
  663. 'name' => 'Device 3',
  664. 'typ' => '2'
  665. ));
  666. $this->assertEquals($expected, $result['Monkey']);
  667. $ThePaper->delete(1);
  668. $result = $ThePaper->findById(2);
  669. $expected = array(
  670. array(
  671. 'id' => '2',
  672. 'device_type_id' => '1',
  673. 'name' => 'Device 2',
  674. 'typ' => '1'
  675. ),
  676. array(
  677. 'id' => '3',
  678. 'device_type_id' => '1',
  679. 'name' => 'Device 3',
  680. 'typ' => '2'
  681. ));
  682. $this->assertEquals($expected, $result['Monkey']);
  683. }
  684. /**
  685. * test that beforeDelete returning false can abort deletion.
  686. *
  687. * @return void
  688. */
  689. public function testBeforeDeleteDeleteAbortion() {
  690. $this->loadFixtures('Post');
  691. $Model = new CallbackPostTestModel();
  692. $Model->beforeDeleteReturn = false;
  693. $result = $Model->delete(1);
  694. $this->assertFalse($result);
  695. $exists = $Model->findById(1);
  696. $this->assertTrue(is_array($exists));
  697. }
  698. /**
  699. * test for a habtm deletion error that occurs in postgres but should not.
  700. * And should not occur in any dbo.
  701. *
  702. * @return void
  703. */
  704. public function testDeleteHabtmPostgresFailure() {
  705. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  706. $Article = ClassRegistry::init('Article');
  707. $Article->hasAndBelongsToMany['Tag']['unique'] = true;
  708. $Tag = ClassRegistry::init('Tag');
  709. $Tag->bindModel(array('hasAndBelongsToMany' => array(
  710. 'Article' => array(
  711. 'className' => 'Article',
  712. 'unique' => true
  713. )
  714. )), true);
  715. // Article 1 should have Tag.1 and Tag.2
  716. $before = $Article->find("all", array(
  717. "conditions" => array("Article.id" => 1),
  718. ));
  719. $this->assertEquals(2, count($before[0]['Tag']), 'Tag count for Article.id = 1 is incorrect, should be 2 %s');
  720. // From now on, Tag #1 is only associated with Post #1
  721. $submittedData = array(
  722. "Tag" => array("id" => 1, 'tag' => 'tag1'),
  723. "Article" => array(
  724. "Article" => array(1)
  725. )
  726. );
  727. $Tag->save($submittedData);
  728. // One more submission (The other way around) to make sure the reverse save looks good.
  729. $submittedData = array(
  730. "Article" => array("id" => 2, 'title' => 'second article'),
  731. "Tag" => array(
  732. "Tag" => array(2, 3)
  733. )
  734. );
  735. // ERROR:
  736. // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
  737. // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
  738. $Article->save($submittedData);
  739. // Want to make sure Article #1 has Tag #1 and Tag #2 still.
  740. $after = $Article->find("all", array(
  741. "conditions" => array("Article.id" => 1),
  742. ));
  743. // Removing Article #2 from Tag #1 is all that should have happened.
  744. $this->assertEquals(count($before[0]["Tag"]), count($after[0]["Tag"]));
  745. }
  746. /**
  747. * test that deleting records inside the beforeDelete doesn't truncate the table.
  748. *
  749. * @return void
  750. */
  751. public function testBeforeDeleteWipingTable() {
  752. $this->loadFixtures('Comment');
  753. $Comment = new BeforeDeleteComment();
  754. // Delete 3 records.
  755. $Comment->delete(4);
  756. $result = $Comment->find('count');
  757. $this->assertTrue($result > 1, 'Comments are all gone.');
  758. $Comment->create(array(
  759. 'article_id' => 1,
  760. 'user_id' => 2,
  761. 'comment' => 'new record',
  762. 'published' => 'Y'
  763. ));
  764. $Comment->save();
  765. $Comment->delete(5);
  766. $result = $Comment->find('count');
  767. $this->assertTrue($result > 1, 'Comments are all gone.');
  768. }
  769. /**
  770. * test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
  771. *
  772. * @return void
  773. */
  774. public function testBeforeDeleteWipingTableWithDuplicateDelete() {
  775. $this->loadFixtures('Comment');
  776. $Comment = new BeforeDeleteComment();
  777. $Comment->delete(1);
  778. $result = $Comment->find('count');
  779. $this->assertTrue($result > 1, 'Comments are all gone.');
  780. }
  781. }