PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/model/model_delete.test.php

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