ModelDeleteTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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.Test.Case.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.Test.Case.Model
  24. */
  25. class ModelDeleteTest extends BaseModelTest {
  26. /**
  27. * testDeleteHabtmReferenceWithConditions method
  28. *
  29. * @return void
  30. */
  31. public function testDeleteHabtmReferenceWithConditions() {
  32. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image');
  33. $Portfolio = new Portfolio();
  34. $Portfolio->hasAndBelongsToMany['Item']['conditions'] = array('ItemsPortfolio.item_id >' => 1);
  35. $result = $Portfolio->find('first', array(
  36. 'conditions' => array('Portfolio.id' => 1)
  37. ));
  38. $expected = array(
  39. array(
  40. 'id' => 3,
  41. 'syfile_id' => 3,
  42. 'published' => false,
  43. 'name' => 'Item 3',
  44. 'ItemsPortfolio' => array(
  45. 'id' => 3,
  46. 'item_id' => 3,
  47. 'portfolio_id' => 1
  48. )),
  49. array(
  50. 'id' => 4,
  51. 'syfile_id' => 4,
  52. 'published' => false,
  53. 'name' => 'Item 4',
  54. 'ItemsPortfolio' => array(
  55. 'id' => 4,
  56. 'item_id' => 4,
  57. 'portfolio_id' => 1
  58. )),
  59. array(
  60. 'id' => 5,
  61. 'syfile_id' => 5,
  62. 'published' => false,
  63. 'name' => 'Item 5',
  64. 'ItemsPortfolio' => array(
  65. 'id' => 5,
  66. 'item_id' => 5,
  67. 'portfolio_id' => 1
  68. )));
  69. $this->assertEqual($result['Item'], $expected);
  70. $result = $Portfolio->ItemsPortfolio->find('all', array(
  71. 'conditions' => array('ItemsPortfolio.portfolio_id' => 1)
  72. ));
  73. $expected = array(
  74. array(
  75. 'ItemsPortfolio' => array(
  76. 'id' => 1,
  77. 'item_id' => 1,
  78. 'portfolio_id' => 1
  79. )),
  80. array(
  81. 'ItemsPortfolio' => array(
  82. 'id' => 3,
  83. 'item_id' => 3,
  84. 'portfolio_id' => 1
  85. )),
  86. array(
  87. 'ItemsPortfolio' => array(
  88. 'id' => 4,
  89. 'item_id' => 4,
  90. 'portfolio_id' => 1
  91. )),
  92. array(
  93. 'ItemsPortfolio' => array(
  94. 'id' => 5,
  95. 'item_id' => 5,
  96. 'portfolio_id' => 1
  97. )));
  98. $this->assertEqual($expected, $result);
  99. $Portfolio->delete(1);
  100. $result = $Portfolio->find('first', array(
  101. 'conditions' => array('Portfolio.id' => 1)
  102. ));
  103. $this->assertFalse($result);
  104. $result = $Portfolio->ItemsPortfolio->find('all', array(
  105. 'conditions' => array('ItemsPortfolio.portfolio_id' => 1)
  106. ));
  107. $this->assertEquals($result, array());
  108. }
  109. /**
  110. * testDeleteArticleBLinks method
  111. *
  112. * @return void
  113. */
  114. public function testDeleteArticleBLinks() {
  115. $this->loadFixtures('Article', 'ArticlesTag', 'Tag', 'User');
  116. $TestModel = new ArticleB();
  117. $result = $TestModel->ArticlesTag->find('all');
  118. $expected = array(
  119. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')),
  120. array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')),
  121. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  122. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  123. );
  124. $this->assertEqual($expected, $result);
  125. $TestModel->delete(1);
  126. $result = $TestModel->ArticlesTag->find('all');
  127. $expected = array(
  128. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')),
  129. array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3'))
  130. );
  131. $this->assertEqual($expected, $result);
  132. }
  133. /**
  134. * testDeleteDependentWithConditions method
  135. *
  136. * @return void
  137. */
  138. public function testDeleteDependentWithConditions() {
  139. $this->loadFixtures('Cd','Book','OverallFavorite');
  140. $Cd = new Cd();
  141. $Book = new Book();
  142. $OverallFavorite = new OverallFavorite();
  143. $Cd->delete(1);
  144. $result = $OverallFavorite->find('all', array(
  145. 'fields' => array('model_type', 'model_id', 'priority')
  146. ));
  147. $expected = array(
  148. array(
  149. 'OverallFavorite' => array(
  150. 'model_type' => 'Book',
  151. 'model_id' => 1,
  152. 'priority' => 2
  153. )));
  154. $this->assertTrue(is_array($result));
  155. $this->assertEqual($expected, $result);
  156. $Book->delete(1);
  157. $result = $OverallFavorite->find('all', array(
  158. 'fields' => array('model_type', 'model_id', 'priority')
  159. ));
  160. $expected = array();
  161. $this->assertTrue(is_array($result));
  162. $this->assertEqual($expected, $result);
  163. }
  164. /**
  165. * testDel method
  166. *
  167. * @return void
  168. */
  169. public function testDelete() {
  170. $this->loadFixtures('Article', 'Comment', 'Attachment');
  171. $TestModel = new Article();
  172. $result = $TestModel->delete(2);
  173. $this->assertTrue($result);
  174. $result = $TestModel->read(null, 2);
  175. $this->assertFalse($result);
  176. $TestModel->recursive = -1;
  177. $result = $TestModel->find('all', array(
  178. 'fields' => array('id', 'title')
  179. ));
  180. $expected = array(
  181. array('Article' => array(
  182. 'id' => 1,
  183. 'title' => 'First Article'
  184. )),
  185. array('Article' => array(
  186. 'id' => 3,
  187. 'title' => 'Third Article'
  188. )));
  189. $this->assertEqual($expected, $result);
  190. $result = $TestModel->delete(3);
  191. $this->assertTrue($result);
  192. $result = $TestModel->read(null, 3);
  193. $this->assertFalse($result);
  194. $TestModel->recursive = -1;
  195. $result = $TestModel->find('all', array(
  196. 'fields' => array('id', 'title')
  197. ));
  198. $expected = array(
  199. array('Article' => array(
  200. 'id' => 1,
  201. 'title' => 'First Article'
  202. )));
  203. $this->assertEqual($expected, $result);
  204. // make sure deleting a non-existent record doesn't break save()
  205. // ticket #6293
  206. $this->loadFixtures('Uuid');
  207. $Uuid = new Uuid();
  208. $data = array(
  209. 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3',
  210. '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8',
  211. '8208C7FE-E89C-47C5-B378-DED6C271F9B8');
  212. foreach ($data as $id) {
  213. $Uuid->save(array('id' => $id));
  214. }
  215. $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8');
  216. $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8');
  217. foreach ($data as $id) {
  218. $Uuid->save(array('id' => $id));
  219. }
  220. $result = $Uuid->find('all', array(
  221. 'conditions' => array('id' => $data),
  222. 'fields' => array('id'),
  223. 'order' => 'id'));
  224. $expected = array(
  225. array('Uuid' => array(
  226. 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')),
  227. array('Uuid' => array(
  228. 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')),
  229. array('Uuid' => array(
  230. 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3')));
  231. $this->assertEqual($expected, $result);
  232. }
  233. /**
  234. * test that delete() updates the correct records counterCache() records.
  235. *
  236. * @return void
  237. */
  238. public function testDeleteUpdatingCounterCacheCorrectly() {
  239. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  240. $User = new CounterCacheUser();
  241. $User->Post->delete(3);
  242. $result = $User->read(null, 301);
  243. $this->assertEqual($result['User']['post_count'], 0);
  244. $result = $User->read(null, 66);
  245. $this->assertEqual($result['User']['post_count'], 2);
  246. }
  247. /**
  248. * testDeleteAll method
  249. *
  250. * @return void
  251. */
  252. public function testDeleteAll() {
  253. $this->loadFixtures('Article');
  254. $TestModel = new Article();
  255. $data = array('Article' => array(
  256. 'user_id' => 2,
  257. 'id' => 4,
  258. 'title' => 'Fourth Article',
  259. 'published' => 'N'
  260. ));
  261. $result = $TestModel->set($data) && $TestModel->save();
  262. $this->assertTrue($result);
  263. $data = array('Article' => array(
  264. 'user_id' => 2,
  265. 'id' => 5,
  266. 'title' => 'Fifth Article',
  267. 'published' => 'Y'
  268. ));
  269. $result = $TestModel->set($data) && $TestModel->save();
  270. $this->assertTrue($result);
  271. $data = array('Article' => array(
  272. 'user_id' => 1,
  273. 'id' => 6,
  274. 'title' => 'Sixth Article',
  275. 'published' => 'N'
  276. ));
  277. $result = $TestModel->set($data) && $TestModel->save();
  278. $this->assertTrue($result);
  279. $TestModel->recursive = -1;
  280. $result = $TestModel->find('all', array(
  281. 'fields' => array('id', 'user_id', 'title', 'published')
  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->assertEqual($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. ));
  326. $expected = array(
  327. array('Article' => array(
  328. 'id' => 1,
  329. 'user_id' => 1,
  330. 'title' => 'First Article',
  331. 'published' => 'Y'
  332. )),
  333. array('Article' => array(
  334. 'id' => 2,
  335. 'user_id' => 3,
  336. 'title' => 'Second Article',
  337. 'published' => 'Y'
  338. )),
  339. array('Article' => array(
  340. 'id' => 3,
  341. 'user_id' => 1,
  342. 'title' => 'Third Article',
  343. 'published' => 'Y'
  344. )),
  345. array('Article' => array(
  346. 'id' => 5,
  347. 'user_id' => 2,
  348. 'title' => 'Fifth Article',
  349. 'published' => 'Y'
  350. )));
  351. $this->assertEqual($expected, $result);
  352. $data = array('Article.user_id' => array(2, 3));
  353. $result = $TestModel->deleteAll($data, true, true);
  354. $this->assertTrue($result);
  355. $TestModel->recursive = -1;
  356. $result = $TestModel->find('all', array(
  357. 'fields' => array('id', 'user_id', 'title', 'published')
  358. ));
  359. $expected = array(
  360. array('Article' => array(
  361. 'id' => 1,
  362. 'user_id' => 1,
  363. 'title' => 'First Article',
  364. 'published' => 'Y'
  365. )),
  366. array('Article' => array(
  367. 'id' => 3,
  368. 'user_id' => 1,
  369. 'title' => 'Third Article',
  370. 'published' => 'Y'
  371. )));
  372. $this->assertEqual($expected, $result);
  373. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  374. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  375. }
  376. /**
  377. * testDeleteAllUnknownColumn method
  378. *
  379. * @expectedException PDOException
  380. * @return void
  381. */
  382. public function testDeleteAllUnknownColumn() {
  383. $this->loadFixtures('Article');
  384. $TestModel = new Article();
  385. $result = $TestModel->deleteAll(array('Article.non_existent_field' => 999));
  386. $this->assertFalse($result, 'deleteAll returned true when find query generated sql error. %s');
  387. }
  388. /**
  389. * testRecursiveDel method
  390. *
  391. * @return void
  392. */
  393. public function testRecursiveDel() {
  394. $this->loadFixtures('Article', 'Comment', 'Attachment');
  395. $TestModel = new Article();
  396. $result = $TestModel->delete(2);
  397. $this->assertTrue($result);
  398. $TestModel->recursive = 2;
  399. $result = $TestModel->read(null, 2);
  400. $this->assertFalse($result);
  401. $result = $TestModel->Comment->read(null, 5);
  402. $this->assertFalse($result);
  403. $result = $TestModel->Comment->read(null, 6);
  404. $this->assertFalse($result);
  405. $result = $TestModel->Comment->Attachment->read(null, 1);
  406. $this->assertFalse($result);
  407. $result = $TestModel->find('count');
  408. $this->assertEqual($result, 2);
  409. $result = $TestModel->Comment->find('count');
  410. $this->assertEqual($result, 4);
  411. $result = $TestModel->Comment->Attachment->find('count');
  412. $this->assertEqual($result, 0);
  413. }
  414. /**
  415. * testDependentExclusiveDelete method
  416. *
  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. * @return void
  433. */
  434. public function testDeleteLinks() {
  435. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  436. $TestModel = new Article();
  437. $result = $TestModel->ArticlesTag->find('all');
  438. $expected = array(
  439. array('ArticlesTag' => array(
  440. 'article_id' => '1',
  441. 'tag_id' => '1'
  442. )),
  443. array('ArticlesTag' => array(
  444. 'article_id' => '1',
  445. 'tag_id' => '2'
  446. )),
  447. array('ArticlesTag' => array(
  448. 'article_id' => '2',
  449. 'tag_id' => '1'
  450. )),
  451. array('ArticlesTag' => array(
  452. 'article_id' => '2',
  453. 'tag_id' => '3'
  454. )));
  455. $this->assertEqual($expected, $result);
  456. $TestModel->delete(1);
  457. $result = $TestModel->ArticlesTag->find('all');
  458. $expected = array(
  459. array('ArticlesTag' => array(
  460. 'article_id' => '2',
  461. 'tag_id' => '1'
  462. )),
  463. array('ArticlesTag' => array(
  464. 'article_id' => '2',
  465. 'tag_id' => '3'
  466. )));
  467. $this->assertEqual($expected, $result);
  468. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  469. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  470. }
  471. /**
  472. * test that a plugin model as the 'with' model doesn't have issues
  473. *
  474. * @return void
  475. */
  476. public function testDeleteLinksWithPLuginJoinModel() {
  477. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  478. $Article = new Article();
  479. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')), false);
  480. unset($Article->Tag, $Article->ArticleTags);
  481. $Article->bindModel(array('hasAndBelongsToMany' => array(
  482. 'Tag' => array('with' => 'TestPlugin.ArticlesTag')
  483. )), false);
  484. $this->assertTrue($Article->delete(1));
  485. }
  486. /**
  487. * test deleteLinks with Multiple habtm associations
  488. *
  489. * @return void
  490. */
  491. public function testDeleteLinksWithMultipleHabtmAssociations() {
  492. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  493. $JoinA = new JoinA();
  494. //create two new join records to expose the issue.
  495. $JoinA->JoinAsJoinC->create(array(
  496. 'join_a_id' => 1,
  497. 'join_c_id' => 2,
  498. ));
  499. $JoinA->JoinAsJoinC->save();
  500. $JoinA->JoinAsJoinB->create(array(
  501. 'join_a_id' => 1,
  502. 'join_b_id' => 2,
  503. ));
  504. $JoinA->JoinAsJoinB->save();
  505. $result = $JoinA->delete(1);
  506. $this->assertTrue($result, 'Delete failed %s');
  507. $joinedBs = $JoinA->JoinAsJoinB->find('count', array(
  508. 'conditions' => array('JoinAsJoinB.join_a_id' => 1)
  509. ));
  510. $this->assertEqual($joinedBs, 0, 'JoinA/JoinB link records left over. %s');
  511. $joinedBs = $JoinA->JoinAsJoinC->find('count', array(
  512. 'conditions' => array('JoinAsJoinC.join_a_id' => 1)
  513. ));
  514. $this->assertEqual($joinedBs, 0, 'JoinA/JoinC link records left over. %s');
  515. }
  516. /**
  517. * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
  518. *
  519. * @return void
  520. */
  521. public function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
  522. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  523. $ThePaper = new ThePaper();
  524. $ThePaper->id = 1;
  525. $ThePaper->save(array('Monkey' => array(2, 3)));
  526. $result = $ThePaper->findById(1);
  527. $expected = array(
  528. array(
  529. 'id' => '2',
  530. 'device_type_id' => '1',
  531. 'name' => 'Device 2',
  532. 'typ' => '1'
  533. ),
  534. array(
  535. 'id' => '3',
  536. 'device_type_id' => '1',
  537. 'name' => 'Device 3',
  538. 'typ' => '2'
  539. ));
  540. $this->assertEqual($result['Monkey'], $expected);
  541. $ThePaper = new ThePaper();
  542. $ThePaper->id = 2;
  543. $ThePaper->save(array('Monkey' => array(2, 3)));
  544. $result = $ThePaper->findById(2);
  545. $expected = array(
  546. array(
  547. 'id' => '2',
  548. 'device_type_id' => '1',
  549. 'name' => 'Device 2',
  550. 'typ' => '1'
  551. ),
  552. array(
  553. 'id' => '3',
  554. 'device_type_id' => '1',
  555. 'name' => 'Device 3',
  556. 'typ' => '2'
  557. ));
  558. $this->assertEqual($result['Monkey'], $expected);
  559. $ThePaper->delete(1);
  560. $result = $ThePaper->findById(2);
  561. $expected = array(
  562. array(
  563. 'id' => '2',
  564. 'device_type_id' => '1',
  565. 'name' => 'Device 2',
  566. 'typ' => '1'
  567. ),
  568. array(
  569. 'id' => '3',
  570. 'device_type_id' => '1',
  571. 'name' => 'Device 3',
  572. 'typ' => '2'
  573. ));
  574. $this->assertEqual($result['Monkey'], $expected);
  575. }
  576. /**
  577. * test that beforeDelete returning false can abort deletion.
  578. *
  579. * @return void
  580. */
  581. public function testBeforeDeleteDeleteAbortion() {
  582. $this->loadFixtures('Post');
  583. $Model = new CallbackPostTestModel();
  584. $Model->beforeDeleteReturn = false;
  585. $result = $Model->delete(1);
  586. $this->assertFalse($result);
  587. $exists = $Model->findById(1);
  588. $this->assertTrue(is_array($exists));
  589. }
  590. /**
  591. * test for a habtm deletion error that occurs in postgres but should not.
  592. * And should not occur in any dbo.
  593. *
  594. * @return void
  595. */
  596. public function testDeleteHabtmPostgresFailure() {
  597. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  598. $Article = ClassRegistry::init('Article');
  599. $Article->hasAndBelongsToMany['Tag']['unique'] = true;
  600. $Tag = ClassRegistry::init('Tag');
  601. $Tag->bindModel(array('hasAndBelongsToMany' => array(
  602. 'Article' => array(
  603. 'className' => 'Article',
  604. 'unique' => true
  605. )
  606. )), true);
  607. // Article 1 should have Tag.1 and Tag.2
  608. $before = $Article->find("all", array(
  609. "conditions" => array("Article.id" => 1),
  610. ));
  611. $this->assertEqual(count($before[0]['Tag']), 2, 'Tag count for Article.id = 1 is incorrect, should be 2 %s');
  612. // From now on, Tag #1 is only associated with Post #1
  613. $submitted_data = array(
  614. "Tag" => array("id" => 1, 'tag' => 'tag1'),
  615. "Article" => array(
  616. "Article" => array(1)
  617. )
  618. );
  619. $Tag->save($submitted_data);
  620. // One more submission (The other way around) to make sure the reverse save looks good.
  621. $submitted_data = array(
  622. "Article" => array("id" => 2, 'title' => 'second article'),
  623. "Tag" => array(
  624. "Tag" => array(2, 3)
  625. )
  626. );
  627. // ERROR:
  628. // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
  629. // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
  630. $Article->save($submitted_data);
  631. // Want to make sure Article #1 has Tag #1 and Tag #2 still.
  632. $after = $Article->find("all", array(
  633. "conditions" => array("Article.id" => 1),
  634. ));
  635. // Removing Article #2 from Tag #1 is all that should have happened.
  636. $this->assertEqual(count($before[0]["Tag"]), count($after[0]["Tag"]));
  637. }
  638. /**
  639. * test that deleting records inside the beforeDelete doesn't truncate the table.
  640. *
  641. * @return void
  642. */
  643. public function testBeforeDeleteWipingTable() {
  644. $this->loadFixtures('Comment');
  645. $Comment = new BeforeDeleteComment();
  646. // Delete 3 records.
  647. $Comment->delete(4);
  648. $result = $Comment->find('count');
  649. $this->assertTrue($result > 1, 'Comments are all gone.');
  650. $Comment->create(array(
  651. 'article_id' => 1,
  652. 'user_id' => 2,
  653. 'comment' => 'new record',
  654. 'published' => 'Y'
  655. ));
  656. $Comment->save();
  657. $Comment->delete(5);
  658. $result = $Comment->find('count');
  659. $this->assertTrue($result > 1, 'Comments are all gone.');
  660. }
  661. /**
  662. * test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
  663. *
  664. * @return void
  665. */
  666. public function testBeforeDeleteWipingTableWithDuplicateDelete() {
  667. $this->loadFixtures('Comment');
  668. $Comment = new BeforeDeleteComment();
  669. $Comment->delete(1);
  670. $result = $Comment->find('count');
  671. $this->assertTrue($result > 1, 'Comments are all gone.');
  672. }
  673. }