PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/bb-dev/cakephp2x
PHP | 587 lines | 438 code | 64 blank | 85 comment | 3 complexity | 28eb8964e00f6769024c3bb9ace1f379 MD5 | raw file
  1. <?php
  2. /**
  3. * ModelDeleteTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  8. * Copyright 2005-2009, 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-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link https://trac.cakephp.org/wiki/Developement/TestSuite 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. $OverallFavorite =& new OverallFavorite();
  147. $Cd->delete(1);
  148. $result = $OverallFavorite->find('all', array(
  149. 'fields' => array('model_type', 'model_id', 'priority')
  150. ));
  151. $expected = array(
  152. array(
  153. 'OverallFavorite' => array(
  154. 'model_type' => 'Book',
  155. 'model_id' => 1,
  156. 'priority' => 2
  157. )));
  158. $this->assertTrue(is_array($result));
  159. $this->assertEqual($result, $expected);
  160. }
  161. /**
  162. * testDel method
  163. *
  164. * @access public
  165. * @return void
  166. */
  167. function testDelete() {
  168. $this->loadFixtures('Article');
  169. $TestModel =& new Article();
  170. $result = $TestModel->delete(2);
  171. $this->assertTrue($result);
  172. $result = $TestModel->read(null, 2);
  173. $this->assertFalse($result);
  174. $TestModel->recursive = -1;
  175. $result = $TestModel->find('all', array(
  176. 'fields' => array('id', 'title')
  177. ));
  178. $expected = array(
  179. array('Article' => array(
  180. 'id' => 1,
  181. 'title' => 'First Article'
  182. )),
  183. array('Article' => array(
  184. 'id' => 3,
  185. 'title' => 'Third Article'
  186. )));
  187. $this->assertEqual($result, $expected);
  188. $result = $TestModel->delete(3);
  189. $this->assertTrue($result);
  190. $result = $TestModel->read(null, 3);
  191. $this->assertFalse($result);
  192. $TestModel->recursive = -1;
  193. $result = $TestModel->find('all', array(
  194. 'fields' => array('id', 'title')
  195. ));
  196. $expected = array(
  197. array('Article' => array(
  198. 'id' => 1,
  199. 'title' => 'First Article'
  200. )));
  201. $this->assertEqual($result, $expected);
  202. // make sure deleting a non-existent record doesn't break save()
  203. // ticket #6293
  204. $this->loadFixtures('Uuid');
  205. $Uuid =& new Uuid();
  206. $data = array(
  207. 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3',
  208. '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8',
  209. '8208C7FE-E89C-47C5-B378-DED6C271F9B8');
  210. foreach ($data as $id) {
  211. $Uuid->save(array('id' => $id));
  212. }
  213. $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8');
  214. $Uuid->delete('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8');
  215. foreach ($data as $id) {
  216. $Uuid->save(array('id' => $id));
  217. }
  218. $result = $Uuid->find('all', array(
  219. 'conditions' => array('id' => $data),
  220. 'fields' => array('id'),
  221. 'order' => 'id'));
  222. $expected = array(
  223. array('Uuid' => array(
  224. 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')),
  225. array('Uuid' => array(
  226. 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')),
  227. array('Uuid' => array(
  228. 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3')));
  229. $this->assertEqual($result, $expected);
  230. }
  231. /**
  232. * testDeleteAll method
  233. *
  234. * @access public
  235. * @return void
  236. */
  237. function testDeleteAll() {
  238. $this->loadFixtures('Article');
  239. $TestModel =& new Article();
  240. $data = array('Article' => array(
  241. 'user_id' => 2,
  242. 'id' => 4,
  243. 'title' => 'Fourth Article',
  244. 'published' => 'N'
  245. ));
  246. $result = $TestModel->set($data) && $TestModel->save();
  247. $this->assertTrue($result);
  248. $data = array('Article' => array(
  249. 'user_id' => 2,
  250. 'id' => 5,
  251. 'title' => 'Fifth Article',
  252. 'published' => 'Y'
  253. ));
  254. $result = $TestModel->set($data) && $TestModel->save();
  255. $this->assertTrue($result);
  256. $data = array('Article' => array(
  257. 'user_id' => 1,
  258. 'id' => 6,
  259. 'title' => 'Sixth Article',
  260. 'published' => 'N'
  261. ));
  262. $result = $TestModel->set($data) && $TestModel->save();
  263. $this->assertTrue($result);
  264. $TestModel->recursive = -1;
  265. $result = $TestModel->find('all', array(
  266. 'fields' => array('id', 'user_id', 'title', 'published')
  267. ));
  268. $expected = array(
  269. array('Article' => array(
  270. 'id' => 1,
  271. 'user_id' => 1,
  272. 'title' => 'First Article',
  273. 'published' => 'Y'
  274. )),
  275. array('Article' => array(
  276. 'id' => 2,
  277. 'user_id' => 3,
  278. 'title' => 'Second Article',
  279. 'published' => 'Y'
  280. )),
  281. array('Article' => array(
  282. 'id' => 3,
  283. 'user_id' => 1,
  284. 'title' => 'Third Article',
  285. 'published' => 'Y')),
  286. array('Article' => array(
  287. 'id' => 4,
  288. 'user_id' => 2,
  289. 'title' => 'Fourth Article',
  290. 'published' => 'N'
  291. )),
  292. array('Article' => array(
  293. 'id' => 5,
  294. 'user_id' => 2,
  295. 'title' => 'Fifth Article',
  296. 'published' => 'Y'
  297. )),
  298. array('Article' => array(
  299. 'id' => 6,
  300. 'user_id' => 1,
  301. 'title' => 'Sixth Article',
  302. 'published' => 'N'
  303. )));
  304. $this->assertEqual($result, $expected);
  305. $result = $TestModel->deleteAll(array('Article.published' => 'N'));
  306. $this->assertTrue($result);
  307. $TestModel->recursive = -1;
  308. $result = $TestModel->find('all', array(
  309. 'fields' => array('id', 'user_id', 'title', 'published')
  310. ));
  311. $expected = array(
  312. array('Article' => array(
  313. 'id' => 1,
  314. 'user_id' => 1,
  315. 'title' => 'First Article',
  316. 'published' => 'Y'
  317. )),
  318. array('Article' => array(
  319. 'id' => 2,
  320. 'user_id' => 3,
  321. 'title' => 'Second Article',
  322. 'published' => 'Y'
  323. )),
  324. array('Article' => array(
  325. 'id' => 3,
  326. 'user_id' => 1,
  327. 'title' => 'Third Article',
  328. 'published' => 'Y'
  329. )),
  330. array('Article' => array(
  331. 'id' => 5,
  332. 'user_id' => 2,
  333. 'title' => 'Fifth Article',
  334. 'published' => 'Y'
  335. )));
  336. $this->assertEqual($result, $expected);
  337. $data = array('Article.user_id' => array(2, 3));
  338. $result = $TestModel->deleteAll($data, true, true);
  339. $this->assertTrue($result);
  340. $TestModel->recursive = -1;
  341. $result = $TestModel->find('all', array(
  342. 'fields' => array('id', 'user_id', 'title', 'published')
  343. ));
  344. $expected = array(
  345. array('Article' => array(
  346. 'id' => 1,
  347. 'user_id' => 1,
  348. 'title' => 'First Article',
  349. 'published' => 'Y'
  350. )),
  351. array('Article' => array(
  352. 'id' => 3,
  353. 'user_id' => 1,
  354. 'title' => 'Third Article',
  355. 'published' => 'Y'
  356. )));
  357. $this->assertEqual($result, $expected);
  358. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  359. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  360. }
  361. /**
  362. * testRecursiveDel method
  363. *
  364. * @access public
  365. * @return void
  366. */
  367. function testRecursiveDel() {
  368. $this->loadFixtures('Article', 'Comment', 'Attachment');
  369. $TestModel =& new Article();
  370. $result = $TestModel->delete(2);
  371. $this->assertTrue($result);
  372. $TestModel->recursive = 2;
  373. $result = $TestModel->read(null, 2);
  374. $this->assertFalse($result);
  375. $result = $TestModel->Comment->read(null, 5);
  376. $this->assertFalse($result);
  377. $result = $TestModel->Comment->read(null, 6);
  378. $this->assertFalse($result);
  379. $result = $TestModel->Comment->Attachment->read(null, 1);
  380. $this->assertFalse($result);
  381. $result = $TestModel->find('count');
  382. $this->assertEqual($result, 2);
  383. $result = $TestModel->Comment->find('count');
  384. $this->assertEqual($result, 4);
  385. $result = $TestModel->Comment->Attachment->find('count');
  386. $this->assertEqual($result, 0);
  387. }
  388. /**
  389. * testDependentExclusiveDelete method
  390. *
  391. * @access public
  392. * @return void
  393. */
  394. function testDependentExclusiveDelete() {
  395. $this->loadFixtures('Article', 'Comment');
  396. $TestModel =& new Article10();
  397. $result = $TestModel->find('all');
  398. $this->assertEqual(count($result[0]['Comment']), 4);
  399. $this->assertEqual(count($result[1]['Comment']), 2);
  400. $this->assertEqual($TestModel->Comment->find('count'), 6);
  401. $TestModel->delete(1);
  402. $this->assertEqual($TestModel->Comment->find('count'), 2);
  403. }
  404. /**
  405. * testDeleteLinks method
  406. *
  407. * @access public
  408. * @return void
  409. */
  410. function testDeleteLinks() {
  411. $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
  412. $TestModel =& new Article();
  413. $result = $TestModel->ArticlesTag->find('all');
  414. $expected = array(
  415. array('ArticlesTag' => array(
  416. 'article_id' => '1',
  417. 'tag_id' => '1'
  418. )),
  419. array('ArticlesTag' => array(
  420. 'article_id' => '1',
  421. 'tag_id' => '2'
  422. )),
  423. array('ArticlesTag' => array(
  424. 'article_id' => '2',
  425. 'tag_id' => '1'
  426. )),
  427. array('ArticlesTag' => array(
  428. 'article_id' => '2',
  429. 'tag_id' => '3'
  430. )));
  431. $this->assertEqual($result, $expected);
  432. $TestModel->delete(1);
  433. $result = $TestModel->ArticlesTag->find('all');
  434. $expected = array(
  435. array('ArticlesTag' => array(
  436. 'article_id' => '2',
  437. 'tag_id' => '1'
  438. )),
  439. array('ArticlesTag' => array(
  440. 'article_id' => '2',
  441. 'tag_id' => '3'
  442. )));
  443. $this->assertEqual($result, $expected);
  444. $result = $TestModel->deleteAll(array('Article.user_id' => 999));
  445. $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
  446. }
  447. /**
  448. * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method
  449. *
  450. * @access public
  451. * @return void
  452. */
  453. function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
  454. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  455. $ThePaper =& new ThePaper();
  456. $ThePaper->id = 1;
  457. $ThePaper->save(array('Monkey' => array(2, 3)));
  458. $result = $ThePaper->findById(1);
  459. $expected = array(
  460. array(
  461. 'id' => '2',
  462. 'device_type_id' => '1',
  463. 'name' => 'Device 2',
  464. 'typ' => '1'
  465. ),
  466. array(
  467. 'id' => '3',
  468. 'device_type_id' => '1',
  469. 'name' => 'Device 3',
  470. 'typ' => '2'
  471. ));
  472. $this->assertEqual($result['Monkey'], $expected);
  473. $ThePaper =& new ThePaper();
  474. $ThePaper->id = 2;
  475. $ThePaper->save(array('Monkey' => array(2, 3)));
  476. $result = $ThePaper->findById(2);
  477. $expected = array(
  478. array(
  479. 'id' => '2',
  480. 'device_type_id' => '1',
  481. 'name' => 'Device 2',
  482. 'typ' => '1'
  483. ),
  484. array(
  485. 'id' => '3',
  486. 'device_type_id' => '1',
  487. 'name' => 'Device 3',
  488. 'typ' => '2'
  489. ));
  490. $this->assertEqual($result['Monkey'], $expected);
  491. $ThePaper->delete(1);
  492. $result = $ThePaper->findById(2);
  493. $expected = array(
  494. array(
  495. 'id' => '2',
  496. 'device_type_id' => '1',
  497. 'name' => 'Device 2',
  498. 'typ' => '1'
  499. ),
  500. array(
  501. 'id' => '3',
  502. 'device_type_id' => '1',
  503. 'name' => 'Device 3',
  504. 'typ' => '2'
  505. ));
  506. $this->assertEqual($result['Monkey'], $expected);
  507. }
  508. /**
  509. * test that beforeDelete returning false can abort deletion.
  510. *
  511. * @return void
  512. */
  513. function testBeforeDeleteDeleteAbortion() {
  514. $this->loadFixtures('Post');
  515. $Model =& new CallbackPostTestModel();
  516. $Model->beforeDeleteReturn = false;
  517. $result = $Model->delete(1);
  518. $this->assertFalse($result);
  519. $exists = $Model->findById(1);
  520. $this->assertTrue(is_array($exists));
  521. }
  522. }
  523. ?>