ModelDeleteTest.php 23 KB

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