ModelDeleteTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. <?php
  2. /**
  3. * ModelDeleteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. /**
  21. * ModelDeleteTest
  22. *
  23. * @package cake.tests.cases.libs.model.operations
  24. */
  25. class ModelDeleteTest extends BaseModelTest {
  26. /**
  27. * testDeleteHabtmReferenceWithConditions method
  28. *
  29. * @access public
  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->assertEqual($result['Item'], $expected);
  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->assertEqual($expected, $result);
  100. $Portfolio->delete(1);
  101. $result = $Portfolio->find('first', array(
  102. 'conditions' => array('Portfolio.id' => 1)
  103. ));
  104. $this->assertFalse($result);
  105. $result = $Portfolio->ItemsPortfolio->find('all', array(
  106. 'conditions' => array('ItemsPortfolio.portfolio_id' => 1)
  107. ));
  108. $this->assertEquals($result, array());
  109. }
  110. /**
  111. * testDeleteArticleBLinks method
  112. *
  113. * @access public
  114. * @return void
  115. */
  116. public function testDeleteArticleBLinks() {
  117. $this->loadFixtures('Article', 'ArticlesTag', 'Tag', 'User');
  118. $TestModel = new ArticleB();
  119. $result = $TestModel->ArticlesTag->find('all');
  120. $expected = array(
  121. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')),
  122. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')),
  123. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  124. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  125. );
  126. $this->assertEqual($expected, $result);
  127. $TestModel->delete(1);
  128. $result = $TestModel->ArticlesTag->find('all');
  129. $expected = array(
  130. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  131. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  132. );
  133. $this->assertEqual($expected, $result);
  134. }
  135. /**
  136. * testDeleteDependentWithConditions method
  137. *
  138. * @access public
  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->assertEqual($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->assertEqual($expected, $result);
  166. }
  167. /**
  168. * testDel method
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. public function testDelete() {
  174. $this->loadFixtures('Article', 'Comment', 'Attachment');
  175. $TestModel = new Article();
  176. $result = $TestModel->delete(2);
  177. $this->assertTrue($result);
  178. $result = $TestModel->read(null, 2);
  179. $this->assertFalse($result);
  180. $TestModel->recursive = -1;
  181. $result = $TestModel->find('all', array(
  182. 'fields' => array('id', 'title')
  183. ));
  184. $expected = array(
  185. array('Article' => array(
  186. 'id' => 1,
  187. 'title' => 'First Article'
  188. )),
  189. array('Article' => array(
  190. 'id' => 3,
  191. 'title' => 'Third Article'
  192. )));
  193. $this->assertEqual($expected, $result);
  194. $result = $TestModel->delete(3);
  195. $this->assertTrue($result);
  196. $result = $TestModel->read(null, 3);
  197. $this->assertFalse($result);
  198. $TestModel->recursive = -1;
  199. $result = $TestModel->find('all', array(
  200. 'fields' => array('id', 'title')
  201. ));
  202. $expected = array(
  203. array('Article' => array(
  204. 'id' => 1,
  205. 'title' => 'First Article'
  206. )));
  207. $this->assertEqual($expected, $result);
  208. // make sure deleting a non-existent record doesn't break save()
  209. // ticket #6293
  210. $this->loadFixtures('Uuid');
  211. $Uuid = new Uuid();
  212. $data = array(
  213. 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3',
  214. '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8',
  215. '8208C7FE-E89C-47C5-B378-DED6C271F9B8');
  216. foreach ($data as $id) {
  217. $Uuid->save(array('id' => $id));
  218. }
  219. $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8');
  220. $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8');
  221. foreach ($data as $id) {
  222. $Uuid->save(array('id' => $id));
  223. }
  224. $result = $Uuid->find('all', array(
  225. 'conditions' => array('id' => $data),
  226. 'fields' => array('id'),
  227. 'order' => 'id'));
  228. $expected = array(
  229. array('Uuid' => array(
  230. 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')),
  231. array('Uuid' => array(
  232. 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')),
  233. array('Uuid' => array(
  234. 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3')));
  235. $this->assertEqual($expected, $result);
  236. }
  237. /**
  238. * test that delete() updates the correct records counterCache() records.
  239. *
  240. * @return void
  241. */
  242. public function testDeleteUpdatingCounterCacheCorrectly() {
  243. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  244. $User = new CounterCacheUser();
  245. $User->Post->delete(3);
  246. $result = $User->read(null, 301);
  247. $this->assertEqual($result['User']['post_count'], 0);
  248. $result = $User->read(null, 66);
  249. $this->assertEqual($result['User']['post_count'], 2);
  250. }
  251. /**
  252. * testDeleteAll method
  253. *
  254. * @access public
  255. * @return void
  256. */
  257. public function testDeleteAll() {
  258. $this->loadFixtures('Article');
  259. $TestModel = new Article();
  260. $data = array('Article' => array(
  261. 'user_id' => 2,
  262. 'id' => 4,
  263. 'title' => 'Fourth Article',
  264. 'published' => 'N'
  265. ));
  266. $result = $TestModel->set($data) && $TestModel->save();
  267. $this->assertTrue($result);
  268. $data = array('Article' => array(
  269. 'user_id' => 2,
  270. 'id' => 5,
  271. 'title' => 'Fifth Article',
  272. 'published' => 'Y'
  273. ));
  274. $result = $TestModel->set($data) && $TestModel->save();
  275. $this->assertTrue($result);
  276. $data = array('Article' => array(
  277. 'user_id' => 1,
  278. 'id' => 6,
  279. 'title' => 'Sixth Article',
  280. 'published' => 'N'
  281. ));
  282. $result = $TestModel->set($data) && $TestModel->save();
  283. $this->assertTrue($result);
  284. $TestModel->recursive = -1;
  285. $result = $TestModel->find('all', array(
  286. 'fields' => array('id', 'user_id', 'title', 'published')
  287. ));
  288. $expected = array(
  289. array('Article' => array(
  290. 'id' => 1,
  291. 'user_id' => 1,
  292. 'title' => 'First Article',
  293. 'published' => 'Y'
  294. )),
  295. array('Article' => array(
  296. 'id' => 2,
  297. 'user_id' => 3,
  298. 'title' => 'Second Article',
  299. 'published' => 'Y'
  300. )),
  301. array('Article' => array(
  302. 'id' => 3,
  303. 'user_id' => 1,
  304. 'title' => 'Third Article',
  305. 'published' => 'Y')),
  306. array('Article' => array(
  307. 'id' => 4,
  308. 'user_id' => 2,
  309. 'title' => 'Fourth Article',
  310. 'published' => 'N'
  311. )),
  312. array('Article' => array(
  313. 'id' => 5,
  314. 'user_id' => 2,
  315. 'title' => 'Fifth Article',
  316. 'published' => 'Y'
  317. )),
  318. array('Article' => array(
  319. 'id' => 6,
  320. 'user_id' => 1,
  321. 'title' => 'Sixth Article',
  322. 'published' => 'N'
  323. )));
  324. $this->assertEqual($expected, $result);
  325. $result = $TestModel->deleteAll(array('Article.published' => 'N'));
  326. $this->assertTrue($result);
  327. $TestModel->recursive = -1;
  328. $result = $TestModel->find('all', array(
  329. 'fields' => array('id', 'user_id', 'title', 'published')
  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->assertEqual($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. ));
  364. $expected = array(
  365. array('Article' => array(
  366. 'id' => 1,
  367. 'user_id' => 1,
  368. 'title' => 'First Article',
  369. 'published' => 'Y'
  370. )),
  371. array('Article' => array(
  372. 'id' => 3,
  373. 'user_id' => 1,
  374. 'title' => 'Third Article',
  375. 'published' => 'Y'
  376. )));
  377. $this->assertEqual($expected, $result);
  378. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  379. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  380. $this->expectError();
  381. ob_start();
  382. $result = $TestModel->deleteAll(array('Article.non_existent_field' => 999));
  383. ob_get_clean();
  384. $this->assertFalse($result, 'deleteAll returned true when find query generated sql error. %s');
  385. }
  386. /**
  387. * testRecursiveDel method
  388. *
  389. * @access public
  390. * @return void
  391. */
  392. public function testRecursiveDel() {
  393. $this->loadFixtures('Article', 'Comment', 'Attachment');
  394. $TestModel = new Article();
  395. $result = $TestModel->delete(2);
  396. $this->assertTrue($result);
  397. $TestModel->recursive = 2;
  398. $result = $TestModel->read(null, 2);
  399. $this->assertFalse($result);
  400. $result = $TestModel->Comment->read(null, 5);
  401. $this->assertFalse($result);
  402. $result = $TestModel->Comment->read(null, 6);
  403. $this->assertFalse($result);
  404. $result = $TestModel->Comment->Attachment->read(null, 1);
  405. $this->assertFalse($result);
  406. $result = $TestModel->find('count');
  407. $this->assertEqual($result, 2);
  408. $result = $TestModel->Comment->find('count');
  409. $this->assertEqual($result, 4);
  410. $result = $TestModel->Comment->Attachment->find('count');
  411. $this->assertEqual($result, 0);
  412. }
  413. /**
  414. * testDependentExclusiveDelete method
  415. *
  416. * @access public
  417. * @return void
  418. */
  419. public function testDependentExclusiveDelete() {
  420. $this->loadFixtures('Article', 'Comment');
  421. $TestModel = new Article10();
  422. $result = $TestModel->find('all');
  423. $this->assertEqual(count($result[0]['Comment']), 4);
  424. $this->assertEqual(count($result[1]['Comment']), 2);
  425. $this->assertEqual($TestModel->Comment->find('count'), 6);
  426. $TestModel->delete(1);
  427. $this->assertEqual($TestModel->Comment->find('count'), 2);
  428. }
  429. /**
  430. * testDeleteLinks method
  431. *
  432. * @access public
  433. * @return void
  434. */
  435. public function testDeleteLinks() {
  436. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  437. $TestModel = new Article();
  438. $result = $TestModel->ArticlesTag->find('all');
  439. $expected = array(
  440. array('ArticlesTag' => array(
  441. 'article_id' => '1',
  442. 'tag_id' => '1'
  443. )),
  444. array('ArticlesTag' => array(
  445. 'article_id' => '1',
  446. 'tag_id' => '2'
  447. )),
  448. array('ArticlesTag' => array(
  449. 'article_id' => '2',
  450. 'tag_id' => '1'
  451. )),
  452. array('ArticlesTag' => array(
  453. 'article_id' => '2',
  454. 'tag_id' => '3'
  455. )));
  456. $this->assertEqual($expected, $result);
  457. $TestModel->delete(1);
  458. $result = $TestModel->ArticlesTag->find('all');
  459. $expected = array(
  460. array('ArticlesTag' => array(
  461. 'article_id' => '2',
  462. 'tag_id' => '1'
  463. )),
  464. array('ArticlesTag' => array(
  465. 'article_id' => '2',
  466. 'tag_id' => '3'
  467. )));
  468. $this->assertEqual($expected, $result);
  469. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  470. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  471. }
  472. /**
  473. * test that a plugin model as the 'with' model doesn't have issues
  474. *
  475. * @return void
  476. */
  477. public function testDeleteLinksWithPLuginJoinModel() {
  478. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  479. $Article = new Article();
  480. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')), false);
  481. unset($Article->Tag, $Article->ArticleTags);
  482. $Article->bindModel(array('hasAndBelongsToMany' => array(
  483. 'Tag' => array('with' => 'TestPlugin.ArticlesTag')
  484. )), false);
  485. $this->assertTrue($Article->delete(1));
  486. }
  487. /**
  488. * test deleteLinks with Multiple habtm associations
  489. *
  490. * @return void
  491. */
  492. public function testDeleteLinksWithMultipleHabtmAssociations() {
  493. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  494. $JoinA = new JoinA();
  495. //create two new join records to expose the issue.
  496. $JoinA->JoinAsJoinC->create(array(
  497. 'join_a_id' => 1,
  498. 'join_c_id' => 2,
  499. ));
  500. $JoinA->JoinAsJoinC->save();
  501. $JoinA->JoinAsJoinB->create(array(
  502. 'join_a_id' => 1,
  503. 'join_b_id' => 2,
  504. ));
  505. $JoinA->JoinAsJoinB->save();
  506. $result = $JoinA->delete(1);
  507. $this->assertTrue($result, 'Delete failed %s');
  508. $joinedBs = $JoinA->JoinAsJoinB->find('count', array(
  509. 'conditions' => array('JoinAsJoinB.join_a_id' => 1)
  510. ));
  511. $this->assertEqual($joinedBs, 0, 'JoinA/JoinB link records left over. %s');
  512. $joinedBs = $JoinA->JoinAsJoinC->find('count', array(
  513. 'conditions' => array('JoinAsJoinC.join_a_id' => 1)
  514. ));
  515. $this->assertEqual($joinedBs, 0, 'JoinA/JoinC link records left over. %s');
  516. }
  517. /**
  518. * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
  519. *
  520. * @access public
  521. * @return void
  522. */
  523. public function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
  524. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  525. $ThePaper = new ThePaper();
  526. $ThePaper->id = 1;
  527. $ThePaper->save(array('Monkey' => array(2, 3)));
  528. $result = $ThePaper->findById(1);
  529. $expected = array(
  530. array(
  531. 'id' => '2',
  532. 'device_type_id' => '1',
  533. 'name' => 'Device 2',
  534. 'typ' => '1'
  535. ),
  536. array(
  537. 'id' => '3',
  538. 'device_type_id' => '1',
  539. 'name' => 'Device 3',
  540. 'typ' => '2'
  541. ));
  542. $this->assertEqual($result['Monkey'], $expected);
  543. $ThePaper = new ThePaper();
  544. $ThePaper->id = 2;
  545. $ThePaper->save(array('Monkey' => array(2, 3)));
  546. $result = $ThePaper->findById(2);
  547. $expected = array(
  548. array(
  549. 'id' => '2',
  550. 'device_type_id' => '1',
  551. 'name' => 'Device 2',
  552. 'typ' => '1'
  553. ),
  554. array(
  555. 'id' => '3',
  556. 'device_type_id' => '1',
  557. 'name' => 'Device 3',
  558. 'typ' => '2'
  559. ));
  560. $this->assertEqual($result['Monkey'], $expected);
  561. $ThePaper->delete(1);
  562. $result = $ThePaper->findById(2);
  563. $expected = array(
  564. array(
  565. 'id' => '2',
  566. 'device_type_id' => '1',
  567. 'name' => 'Device 2',
  568. 'typ' => '1'
  569. ),
  570. array(
  571. 'id' => '3',
  572. 'device_type_id' => '1',
  573. 'name' => 'Device 3',
  574. 'typ' => '2'
  575. ));
  576. $this->assertEqual($result['Monkey'], $expected);
  577. }
  578. /**
  579. * test that beforeDelete returning false can abort deletion.
  580. *
  581. * @return void
  582. */
  583. public function testBeforeDeleteDeleteAbortion() {
  584. $this->loadFixtures('Post');
  585. $Model = new CallbackPostTestModel();
  586. $Model->beforeDeleteReturn = false;
  587. $result = $Model->delete(1);
  588. $this->assertFalse($result);
  589. $exists = $Model->findById(1);
  590. $this->assertTrue(is_array($exists));
  591. }
  592. /**
  593. * test for a habtm deletion error that occurs in postgres but should not.
  594. * And should not occur in any dbo.
  595. *
  596. * @return void
  597. */
  598. public function testDeleteHabtmPostgresFailure() {
  599. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  600. $Article = ClassRegistry::init('Article');
  601. $Article->hasAndBelongsToMany['Tag']['unique'] = true;
  602. $Tag = ClassRegistry::init('Tag');
  603. $Tag->bindModel(array('hasAndBelongsToMany' => array(
  604. 'Article' => array(
  605. 'className' => 'Article',
  606. 'unique' => true
  607. )
  608. )), true);
  609. // Article 1 should have Tag.1 and Tag.2
  610. $before = $Article->find("all", array(
  611. "conditions" => array("Article.id" => 1),
  612. ));
  613. $this->assertEqual(count($before[0]['Tag']), 2, 'Tag count for Article.id = 1 is incorrect, should be 2 %s');
  614. // From now on, Tag #1 is only associated with Post #1
  615. $submitted_data = array(
  616. "Tag" => array("id" => 1, 'tag' => 'tag1'),
  617. "Article" => array(
  618. "Article" => array(1)
  619. )
  620. );
  621. $Tag->save($submitted_data);
  622. // One more submission (The other way around) to make sure the reverse save looks good.
  623. $submitted_data = array(
  624. "Article" => array("id" => 2, 'title' => 'second article'),
  625. "Tag" => array(
  626. "Tag" => array(2, 3)
  627. )
  628. );
  629. // ERROR:
  630. // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
  631. // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
  632. $Article->save($submitted_data);
  633. // Want to make sure Article #1 has Tag #1 and Tag #2 still.
  634. $after = $Article->find("all", array(
  635. "conditions" => array("Article.id" => 1),
  636. ));
  637. // Removing Article #2 from Tag #1 is all that should have happened.
  638. $this->assertEqual(count($before[0]["Tag"]), count($after[0]["Tag"]));
  639. }
  640. /**
  641. * test that deleting records inside the beforeDelete doesn't truncate the table.
  642. *
  643. * @return void
  644. */
  645. public function testBeforeDeleteWipingTable() {
  646. $this->loadFixtures('Comment');
  647. $Comment = new BeforeDeleteComment();
  648. // Delete 3 records.
  649. $Comment->delete(4);
  650. $result = $Comment->find('count');
  651. $this->assertTrue($result > 1, 'Comments are all gone.');
  652. $Comment->create(array(
  653. 'article_id' => 1,
  654. 'user_id' => 2,
  655. 'comment' => 'new record',
  656. 'published' => 'Y'
  657. ));
  658. $Comment->save();
  659. $Comment->delete(5);
  660. $result = $Comment->find('count');
  661. $this->assertTrue($result > 1, 'Comments are all gone.');
  662. }
  663. /**
  664. * test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
  665. *
  666. * @return void
  667. */
  668. public function testBeforeDeleteWipingTableWithDuplicateDelete() {
  669. $this->loadFixtures('Comment');
  670. $Comment = new BeforeDeleteComment();
  671. $Comment->delete(1);
  672. $result = $Comment->find('count');
  673. $this->assertTrue($result > 1, 'Comments are all gone.');
  674. }
  675. }