ModelDeleteTest.php 24 KB

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