ModelDeleteTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. $this->assertTrue($Article->delete(1));
  506. }
  507. /**
  508. * testDeleteDependent method
  509. *
  510. * @return void
  511. */
  512. public function testDeleteDependent() {
  513. $this->loadFixtures('Bidding', 'BiddingMessage', 'Article',
  514. 'ArticlesTag', 'Comment', 'User', 'Attachment'
  515. );
  516. $Bidding = new Bidding();
  517. $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
  518. $expected = array(
  519. array(
  520. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  521. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  522. ),
  523. array(
  524. 'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
  525. 'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
  526. ),
  527. array(
  528. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  529. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  530. ),
  531. array(
  532. 'Bidding' => array('id' => 4, 'bid' => 'Five', 'name' => 'Bid 5'),
  533. 'BiddingMessage' => array('bidding' => '', 'name' => ''),
  534. ),
  535. );
  536. $this->assertEquals($expected, $result);
  537. $Bidding->delete(4, true);
  538. $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
  539. $expected = array(
  540. array(
  541. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  542. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  543. ),
  544. array(
  545. 'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
  546. 'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
  547. ),
  548. array(
  549. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  550. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  551. ),
  552. );
  553. $this->assertEquals($expected, $result);
  554. $Bidding->delete(2, true);
  555. $result = $Bidding->find('all', array('order' => array('Bidding.id' => 'ASC')));
  556. $expected = array(
  557. array(
  558. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  559. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  560. ),
  561. array(
  562. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  563. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  564. ),
  565. );
  566. $this->assertEquals($expected, $result);
  567. $result = $Bidding->BiddingMessage->find('all', array('order' => array('BiddingMessage.name' => 'ASC')));
  568. $expected = array(
  569. array(
  570. 'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
  571. 'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
  572. ),
  573. array(
  574. 'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
  575. 'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
  576. ),
  577. array(
  578. 'BiddingMessage' => array('bidding' => 'Four', 'name' => 'Message 4'),
  579. 'Bidding' => array('id' => '', 'bid' => '', 'name' => ''),
  580. ),
  581. );
  582. $this->assertEquals($expected, $result);
  583. $Article = new Article();
  584. $result = $Article->Comment->find('count', array(
  585. 'conditions' => array('Comment.article_id' => 1)
  586. ));
  587. $this->assertEquals(4, $result);
  588. $result = $Article->delete(1, true);
  589. $this->assertSame($result, true);
  590. $result = $Article->Comment->find('count', array(
  591. 'conditions' => array('Comment.article_id' => 1)
  592. ));
  593. $this->assertEquals(0, $result);
  594. }
  595. /**
  596. * test deleteLinks with Multiple habtm associations
  597. *
  598. * @return void
  599. */
  600. public function testDeleteLinksWithMultipleHabtmAssociations() {
  601. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  602. $JoinA = new JoinA();
  603. //create two new join records to expose the issue.
  604. $JoinA->JoinAsJoinC->create(array(
  605. 'join_a_id' => 1,
  606. 'join_c_id' => 2,
  607. ));
  608. $JoinA->JoinAsJoinC->save();
  609. $JoinA->JoinAsJoinB->create(array(
  610. 'join_a_id' => 1,
  611. 'join_b_id' => 2,
  612. ));
  613. $JoinA->JoinAsJoinB->save();
  614. $result = $JoinA->delete(1);
  615. $this->assertTrue($result, 'Delete failed %s');
  616. $joinedBs = $JoinA->JoinAsJoinB->find('count', array(
  617. 'conditions' => array('JoinAsJoinB.join_a_id' => 1)
  618. ));
  619. $this->assertEquals(0, $joinedBs, 'JoinA/JoinB link records left over. %s');
  620. $joinedBs = $JoinA->JoinAsJoinC->find('count', array(
  621. 'conditions' => array('JoinAsJoinC.join_a_id' => 1)
  622. ));
  623. $this->assertEquals(0, $joinedBs, 'JoinA/JoinC link records left over. %s');
  624. }
  625. /**
  626. * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
  627. *
  628. * @return void
  629. */
  630. public function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
  631. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  632. $ThePaper = new ThePaper();
  633. $ThePaper->id = 1;
  634. $ThePaper->save(array('Monkey' => array(2, 3)));
  635. $result = $ThePaper->findById(1);
  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($expected, $result['Monkey']);
  650. $ThePaper = new ThePaper();
  651. $ThePaper->id = 2;
  652. $ThePaper->save(array('Monkey' => array(2, 3)));
  653. $result = $ThePaper->findById(2);
  654. $expected = array(
  655. array(
  656. 'id' => '2',
  657. 'device_type_id' => '1',
  658. 'name' => 'Device 2',
  659. 'typ' => '1'
  660. ),
  661. array(
  662. 'id' => '3',
  663. 'device_type_id' => '1',
  664. 'name' => 'Device 3',
  665. 'typ' => '2'
  666. ));
  667. $this->assertEquals($expected, $result['Monkey']);
  668. $ThePaper->delete(1);
  669. $result = $ThePaper->findById(2);
  670. $expected = array(
  671. array(
  672. 'id' => '2',
  673. 'device_type_id' => '1',
  674. 'name' => 'Device 2',
  675. 'typ' => '1'
  676. ),
  677. array(
  678. 'id' => '3',
  679. 'device_type_id' => '1',
  680. 'name' => 'Device 3',
  681. 'typ' => '2'
  682. ));
  683. $this->assertEquals($expected, $result['Monkey']);
  684. }
  685. /**
  686. * test that beforeDelete returning false can abort deletion.
  687. *
  688. * @return void
  689. */
  690. public function testBeforeDeleteDeleteAbortion() {
  691. $this->loadFixtures('Post');
  692. $Model = new CallbackPostTestModel();
  693. $Model->beforeDeleteReturn = false;
  694. $result = $Model->delete(1);
  695. $this->assertFalse($result);
  696. $exists = $Model->findById(1);
  697. $this->assertTrue(is_array($exists));
  698. }
  699. /**
  700. * test for a habtm deletion error that occurs in postgres but should not.
  701. * And should not occur in any dbo.
  702. *
  703. * @return void
  704. */
  705. public function testDeleteHabtmPostgresFailure() {
  706. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  707. $Article = ClassRegistry::init('Article');
  708. $Article->hasAndBelongsToMany['Tag']['unique'] = true;
  709. $Tag = ClassRegistry::init('Tag');
  710. $Tag->bindModel(array('hasAndBelongsToMany' => array(
  711. 'Article' => array(
  712. 'className' => 'Article',
  713. 'unique' => true
  714. )
  715. )), true);
  716. // Article 1 should have Tag.1 and Tag.2
  717. $before = $Article->find("all", array(
  718. "conditions" => array("Article.id" => 1),
  719. ));
  720. $this->assertEquals(2, count($before[0]['Tag']), 'Tag count for Article.id = 1 is incorrect, should be 2 %s');
  721. // From now on, Tag #1 is only associated with Post #1
  722. $submittedData = array(
  723. "Tag" => array("id" => 1, 'tag' => 'tag1'),
  724. "Article" => array(
  725. "Article" => array(1)
  726. )
  727. );
  728. $Tag->save($submittedData);
  729. // One more submission (The other way around) to make sure the reverse save looks good.
  730. $submittedData = array(
  731. "Article" => array("id" => 2, 'title' => 'second article'),
  732. "Tag" => array(
  733. "Tag" => array(2, 3)
  734. )
  735. );
  736. // ERROR:
  737. // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
  738. // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
  739. $Article->save($submittedData);
  740. // Want to make sure Article #1 has Tag #1 and Tag #2 still.
  741. $after = $Article->find("all", array(
  742. "conditions" => array("Article.id" => 1),
  743. ));
  744. // Removing Article #2 from Tag #1 is all that should have happened.
  745. $this->assertEquals(count($before[0]["Tag"]), count($after[0]["Tag"]));
  746. }
  747. /**
  748. * test that deleting records inside the beforeDelete doesn't truncate the table.
  749. *
  750. * @return void
  751. */
  752. public function testBeforeDeleteWipingTable() {
  753. $this->loadFixtures('Comment');
  754. $Comment = new BeforeDeleteComment();
  755. // Delete 3 records.
  756. $Comment->delete(4);
  757. $result = $Comment->find('count');
  758. $this->assertTrue($result > 1, 'Comments are all gone.');
  759. $Comment->create(array(
  760. 'article_id' => 1,
  761. 'user_id' => 2,
  762. 'comment' => 'new record',
  763. 'published' => 'Y'
  764. ));
  765. $Comment->save();
  766. $Comment->delete(5);
  767. $result = $Comment->find('count');
  768. $this->assertTrue($result > 1, 'Comments are all gone.');
  769. }
  770. /**
  771. * test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
  772. *
  773. * @return void
  774. */
  775. public function testBeforeDeleteWipingTableWithDuplicateDelete() {
  776. $this->loadFixtures('Comment');
  777. $Comment = new BeforeDeleteComment();
  778. $Comment->delete(1);
  779. $result = $Comment->find('count');
  780. $this->assertTrue($result > 1, 'Comments are all gone.');
  781. }
  782. }