ModelDeleteTest.php 24 KB

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