PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/Cake/Test/Case/Model/ModelWriteTest.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 7998 lines | 6545 code | 725 blank | 728 comment | 29 complexity | 851260c45540b8606a9786eb12af8f0b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * ModelWriteTest 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. App::uses('MockTransactionDboSource', 'Model/Datasource');
  19. App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
  20. App::uses('MockManyTransactionDboSource', 'Model/Datasource');
  21. App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
  22. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  23. /**
  24. * Helper class for testing with mocked datasources
  25. */
  26. class TestAuthor extends Author {
  27. public $hasMany = array(
  28. 'Post' => array(
  29. 'className' => 'TestPost'
  30. )
  31. );
  32. protected $_dataSourceObject;
  33. public $dataForAfterSave;
  34. /**
  35. * Helper method to set a datasource object
  36. *
  37. * @param Object $object The datasource object
  38. * @return void
  39. */
  40. public function setDataSourceObject($object) {
  41. $this->_dataSourceObject = $object;
  42. }
  43. /**
  44. * Overwritten in order to return the directly set datasource object if
  45. * available
  46. *
  47. * @return DataSource
  48. */
  49. public function getDataSource() {
  50. if ($this->_dataSourceObject !== null) {
  51. return $this->_dataSourceObject;
  52. }
  53. return parent::getDataSource();
  54. }
  55. }
  56. /**
  57. * Helper class for testing with mocked datasources
  58. */
  59. class TestPost extends Post {
  60. public $belongsTo = array(
  61. 'Author' => array(
  62. 'className' => 'TestAuthor'
  63. )
  64. );
  65. protected $_dataSourceObject;
  66. public $dataForAfterSave;
  67. /**
  68. * Helper method to set a datasource object
  69. *
  70. * @param Object $object The datasource object
  71. * @return void
  72. */
  73. public function setDataSourceObject($object) {
  74. $this->_dataSourceObject = $object;
  75. }
  76. /**
  77. * Overwritten in order to return the directly set datasource object if
  78. * available
  79. *
  80. * @return DataSource
  81. */
  82. public function getDataSource() {
  83. if ($this->_dataSourceObject !== null) {
  84. return $this->_dataSourceObject;
  85. }
  86. return parent::getDataSource();
  87. }
  88. }
  89. /**
  90. * ModelWriteTest
  91. *
  92. * @package Cake.Test.Case.Model
  93. */
  94. class ModelWriteTest extends BaseModelTest {
  95. /**
  96. * override locale to the default (eng).
  97. *
  98. * @return void
  99. */
  100. public function setUp() {
  101. parent::setUp();
  102. Configure::write('Config.language', 'eng');
  103. }
  104. /**
  105. * Test save() failing when there is no data.
  106. *
  107. * @return void
  108. */
  109. public function testInsertNoData() {
  110. $this->loadFixtures('Bid');
  111. $Bid = ClassRegistry::init('Bid');
  112. $this->assertFalse($Bid->save());
  113. $result = $Bid->save(array('Bid' => array()));
  114. $this->assertFalse($result);
  115. $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
  116. $this->assertFalse($result);
  117. }
  118. /**
  119. * testInsertAnotherHabtmRecordWithSameForeignKey method
  120. *
  121. * @return void
  122. */
  123. public function testInsertAnotherHabtmRecordWithSameForeignKey() {
  124. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  125. $TestModel = new JoinA();
  126. $result = $TestModel->JoinAsJoinB->findById(1);
  127. $expected = array(
  128. 'JoinAsJoinB' => array(
  129. 'id' => 1,
  130. 'join_a_id' => 1,
  131. 'join_b_id' => 2,
  132. 'other' => 'Data for Join A 1 Join B 2',
  133. 'created' => '2008-01-03 10:56:33',
  134. 'updated' => '2008-01-03 10:56:33'
  135. ));
  136. $this->assertEquals($expected, $result);
  137. $TestModel->JoinAsJoinB->create();
  138. $data = array(
  139. 'join_a_id' => 1,
  140. 'join_b_id' => 1,
  141. 'other' => 'Data for Join A 1 Join B 1',
  142. 'created' => '2008-01-03 10:56:44',
  143. 'updated' => '2008-01-03 10:56:44'
  144. );
  145. $result = $TestModel->JoinAsJoinB->save($data);
  146. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  147. $data['id'] = $lastInsertId;
  148. $this->assertEquals(array('JoinAsJoinB' => $data), $result);
  149. $this->assertTrue($lastInsertId > 0);
  150. $result = $TestModel->JoinAsJoinB->findById(1);
  151. $expected = array(
  152. 'JoinAsJoinB' => array(
  153. 'id' => 1,
  154. 'join_a_id' => 1,
  155. 'join_b_id' => 2,
  156. 'other' => 'Data for Join A 1 Join B 2',
  157. 'created' => '2008-01-03 10:56:33',
  158. 'updated' => '2008-01-03 10:56:33'
  159. ));
  160. $this->assertEquals($expected, $result);
  161. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  162. $TestModel->JoinAsJoinB->id = 1;
  163. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  164. $this->assertFalse(empty($result));
  165. $result = $TestModel->JoinAsJoinB->findById(1);
  166. $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
  167. }
  168. /**
  169. * testSaveDateAsFirstEntry method
  170. *
  171. * @return void
  172. */
  173. public function testSaveDateAsFirstEntry() {
  174. $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
  175. $Article = new Article();
  176. $data = array(
  177. 'Article' => array(
  178. 'created' => array(
  179. 'day' => '1',
  180. 'month' => '1',
  181. 'year' => '2008'
  182. ),
  183. 'title' => 'Test Title',
  184. 'user_id' => 1
  185. ));
  186. $Article->create();
  187. $result = $Article->save($data);
  188. $this->assertFalse(empty($result));
  189. $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
  190. $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
  191. $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
  192. }
  193. /**
  194. * testUnderscoreFieldSave method
  195. *
  196. * @return void
  197. */
  198. public function testUnderscoreFieldSave() {
  199. $this->loadFixtures('UnderscoreField');
  200. $UnderscoreField = new UnderscoreField();
  201. $currentCount = $UnderscoreField->find('count');
  202. $this->assertEquals(3, $currentCount);
  203. $data = array('UnderscoreField' => array(
  204. 'user_id' => '1',
  205. 'my_model_has_a_field' => 'Content here',
  206. 'body' => 'Body',
  207. 'published' => 'Y',
  208. 'another_field' => 4
  209. ));
  210. $ret = $UnderscoreField->save($data);
  211. $this->assertFalse(empty($ret));
  212. $currentCount = $UnderscoreField->find('count');
  213. $this->assertEquals(4, $currentCount);
  214. }
  215. /**
  216. * testAutoSaveUuid method
  217. *
  218. * @return void
  219. */
  220. public function testAutoSaveUuid() {
  221. // SQLite does not support non-integer primary keys
  222. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  223. $this->loadFixtures('Uuid');
  224. $TestModel = new Uuid();
  225. $TestModel->save(array('title' => 'Test record'));
  226. $result = $TestModel->findByTitle('Test record');
  227. $this->assertEquals(
  228. array('id', 'title', 'count', 'created', 'updated'),
  229. array_keys($result['Uuid'])
  230. );
  231. $this->assertEquals(36, strlen($result['Uuid']['id']));
  232. }
  233. /**
  234. * Ensure that if the id key is null but present the save doesn't fail (with an
  235. * x sql error: "Column id specified twice")
  236. *
  237. * @return void
  238. */
  239. public function testSaveUuidNull() {
  240. // SQLite does not support non-integer primary keys
  241. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  242. $this->loadFixtures('Uuid');
  243. $TestModel = new Uuid();
  244. $TestModel->save(array('title' => 'Test record', 'id' => null));
  245. $result = $TestModel->findByTitle('Test record');
  246. $this->assertEquals(
  247. array('id', 'title', 'count', 'created', 'updated'),
  248. array_keys($result['Uuid'])
  249. );
  250. $this->assertEquals(36, strlen($result['Uuid']['id']));
  251. }
  252. /**
  253. * testZeroDefaultFieldValue method
  254. *
  255. * @return void
  256. */
  257. public function testZeroDefaultFieldValue() {
  258. $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
  259. $this->loadFixtures('DataTest');
  260. $TestModel = new DataTest();
  261. $TestModel->create(array());
  262. $TestModel->save();
  263. $result = $TestModel->findById($TestModel->id);
  264. $this->assertEquals(0, $result['DataTest']['count']);
  265. $this->assertEquals(0, $result['DataTest']['float']);
  266. }
  267. /**
  268. * Tests validation parameter order in custom validation methods
  269. *
  270. * @return void
  271. */
  272. public function testAllowSimulatedFields() {
  273. $TestModel = new ValidationTest1();
  274. $TestModel->create(array(
  275. 'title' => 'foo',
  276. 'bar' => 'baz'
  277. ));
  278. $expected = array(
  279. 'ValidationTest1' => array(
  280. 'title' => 'foo',
  281. 'bar' => 'baz'
  282. ));
  283. $this->assertEquals($expected, $TestModel->data);
  284. }
  285. /**
  286. * test that Caches are getting cleared on save().
  287. * ensure that both inflections of controller names are getting cleared
  288. * as URL for controller could be either overallFavorites/index or overall_favorites/index
  289. *
  290. * @return void
  291. */
  292. public function testCacheClearOnSave() {
  293. $_back = array(
  294. 'check' => Configure::read('Cache.check'),
  295. 'disable' => Configure::read('Cache.disable'),
  296. );
  297. Configure::write('Cache.check', true);
  298. Configure::write('Cache.disable', false);
  299. $this->loadFixtures('OverallFavorite');
  300. $OverallFavorite = new OverallFavorite();
  301. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  302. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  303. $data = array(
  304. 'OverallFavorite' => array(
  305. 'id' => 22,
  306. 'model_type' => '8-track',
  307. 'model_id' => '3',
  308. 'priority' => '1'
  309. )
  310. );
  311. $OverallFavorite->create($data);
  312. $OverallFavorite->save();
  313. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  314. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  315. Configure::write('Cache.check', $_back['check']);
  316. Configure::write('Cache.disable', $_back['disable']);
  317. }
  318. /**
  319. * test that save() resets whitelist on failed save
  320. *
  321. * @return void
  322. */
  323. public function testSaveFieldListResetsWhitelistOnFailedSave() {
  324. $this->loadFixtures('Bidding');
  325. $model = new Bidding();
  326. $whitelist = array('title');
  327. $model->whitelist = $whitelist;
  328. $result = $model->save(
  329. array(),
  330. array('fieldList' => array('body'))
  331. );
  332. $this->assertFalse($result);
  333. $this->assertEquals($whitelist, $model->whitelist);
  334. }
  335. /**
  336. * Test save() resets the whitelist after afterSave
  337. *
  338. * @return void
  339. */
  340. public function testSaveResetWhitelistOnSuccess() {
  341. $this->loadFixtures('Post');
  342. $callback = array($this, 'callbackForWhitelistReset');
  343. $model = ClassRegistry::init('Post');
  344. $model->whitelist = array('author_id', 'title', 'body');
  345. $model->getEventManager()->attach($callback, 'Model.afterSave');
  346. $data = array(
  347. 'title' => 'New post',
  348. 'body' => 'Post body',
  349. 'author_id' => 1
  350. );
  351. $result = $model->save($data);
  352. $this->assertNotEmpty($result);
  353. }
  354. /**
  355. * Callback for testing whitelist in afterSave
  356. *
  357. * @param Model $model The model having save called.
  358. * @return void
  359. */
  360. public function callbackForWhitelistReset($event) {
  361. $expected = array('author_id', 'title', 'body', 'updated', 'created');
  362. $this->assertEquals($expected, $event->subject()->whitelist);
  363. }
  364. /**
  365. * testSaveWithCounterCache method
  366. *
  367. * @return void
  368. */
  369. public function testSaveWithCounterCache() {
  370. $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
  371. $TestModel = new Syfile();
  372. $TestModel2 = new Item();
  373. $result = $TestModel->findById(1);
  374. $this->assertNull($result['Syfile']['item_count']);
  375. $TestModel2->save(array(
  376. 'name' => 'Item 7',
  377. 'syfile_id' => 1,
  378. 'published' => false
  379. ));
  380. $result = $TestModel->findById(1);
  381. $this->assertEquals(2, $result['Syfile']['item_count']);
  382. $TestModel2->delete(1);
  383. $result = $TestModel->findById(1);
  384. $this->assertEquals(1, $result['Syfile']['item_count']);
  385. $TestModel2->id = 2;
  386. $TestModel2->saveField('syfile_id', 1);
  387. $result = $TestModel->findById(1);
  388. $this->assertEquals(2, $result['Syfile']['item_count']);
  389. $result = $TestModel->findById(2);
  390. $this->assertEquals(0, $result['Syfile']['item_count']);
  391. }
  392. /**
  393. * Tests that counter caches are updated when records are added
  394. *
  395. * @return void
  396. */
  397. public function testCounterCacheIncrease() {
  398. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  399. $User = new CounterCacheUser();
  400. $Post = new CounterCachePost();
  401. $data = array('Post' => array(
  402. 'id' => 22,
  403. 'title' => 'New Post',
  404. 'user_id' => 66
  405. ));
  406. $Post->save($data);
  407. $user = $User->find('first', array(
  408. 'conditions' => array('id' => 66),
  409. 'recursive' => -1
  410. ));
  411. $result = $user[$User->alias]['post_count'];
  412. $expected = 3;
  413. $this->assertEquals($expected, $result);
  414. }
  415. /**
  416. * Tests that counter caches are updated when records are deleted
  417. *
  418. * @return void
  419. */
  420. public function testCounterCacheDecrease() {
  421. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  422. $User = new CounterCacheUser();
  423. $Post = new CounterCachePost();
  424. $Post->delete(2);
  425. $user = $User->find('first', array(
  426. 'conditions' => array('id' => 66),
  427. 'recursive' => -1
  428. ));
  429. $result = $user[$User->alias]['post_count'];
  430. $expected = 1;
  431. $this->assertEquals($expected, $result);
  432. }
  433. /**
  434. * Tests that counter caches are updated when foreign keys of counted records change
  435. *
  436. * @return void
  437. */
  438. public function testCounterCacheUpdated() {
  439. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  440. $User = new CounterCacheUser();
  441. $Post = new CounterCachePost();
  442. $data = $Post->find('first', array(
  443. 'conditions' => array('id' => 1),
  444. 'recursive' => -1
  445. ));
  446. $data[$Post->alias]['user_id'] = 301;
  447. $Post->save($data);
  448. $users = $User->find('all', array('order' => 'User.id'));
  449. $this->assertEquals(1, $users[0]['User']['post_count']);
  450. $this->assertEquals(2, $users[1]['User']['post_count']);
  451. }
  452. /**
  453. * Test counter cache with models that use a non-standard (i.e. not using 'id')
  454. * as their primary key.
  455. *
  456. * @return void
  457. */
  458. public function testCounterCacheWithNonstandardPrimaryKey() {
  459. $this->loadFixtures(
  460. 'CounterCacheUserNonstandardPrimaryKey',
  461. 'CounterCachePostNonstandardPrimaryKey'
  462. );
  463. $User = new CounterCacheUserNonstandardPrimaryKey();
  464. $Post = new CounterCachePostNonstandardPrimaryKey();
  465. $data = $Post->find('first', array(
  466. 'conditions' => array('pid' => 1),
  467. 'recursive' => -1
  468. ));
  469. $data[$Post->alias]['uid'] = 301;
  470. $Post->save($data);
  471. $users = $User->find('all', array('order' => 'User.uid'));
  472. $this->assertEquals(1, $users[0]['User']['post_count']);
  473. $this->assertEquals(2, $users[1]['User']['post_count']);
  474. }
  475. /**
  476. * test Counter Cache With Self Joining table
  477. *
  478. * @return void
  479. */
  480. public function testCounterCacheWithSelfJoin() {
  481. $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
  482. $this->loadFixtures('CategoryThread');
  483. $column = 'COLUMN ';
  484. if ($this->db instanceof Sqlserver) {
  485. $column = '';
  486. }
  487. $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
  488. $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
  489. $this->db->flushMethodCache();
  490. $Category = new CategoryThread();
  491. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  492. $this->assertFalse(empty($result));
  493. $Category = new CategoryThread();
  494. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  495. $Category->updateCounterCache(array('parent_id' => 5));
  496. $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  497. $expected = array(1);
  498. $this->assertEquals($expected, $result);
  499. }
  500. /**
  501. * testSaveWithCounterCacheScope method
  502. *
  503. * @return void
  504. */
  505. public function testSaveWithCounterCacheScope() {
  506. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  507. $TestModel = new Syfile();
  508. $TestModel2 = new Item();
  509. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  510. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  511. $result = $TestModel->findById(1);
  512. $this->assertNull($result['Syfile']['item_count']);
  513. $TestModel2->save(array(
  514. 'name' => 'Item 7',
  515. 'syfile_id' => 1,
  516. 'published' => true
  517. ));
  518. $result = $TestModel->findById(1);
  519. $this->assertEquals(1, $result['Syfile']['item_count']);
  520. $TestModel2->id = 1;
  521. $TestModel2->saveField('published', true);
  522. $result = $TestModel->findById(1);
  523. $this->assertEquals(2, $result['Syfile']['item_count']);
  524. $TestModel2->save(array(
  525. 'id' => 1,
  526. 'syfile_id' => 1,
  527. 'published' => false
  528. ));
  529. $result = $TestModel->findById(1);
  530. $this->assertEquals(1, $result['Syfile']['item_count']);
  531. }
  532. /**
  533. * Tests having multiple counter caches for an associated model
  534. *
  535. * @return void
  536. */
  537. public function testCounterCacheMultipleCaches() {
  538. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  539. $User = new CounterCacheUser();
  540. $Post = new CounterCachePost();
  541. $Post->unbindModel(array('belongsTo' => array('User')), false);
  542. $Post->bindModel(array(
  543. 'belongsTo' => array(
  544. 'User' => array(
  545. 'className' => 'CounterCacheUser',
  546. 'foreignKey' => 'user_id',
  547. 'counterCache' => array(
  548. true,
  549. 'posts_published' => array('Post.published' => true)
  550. )
  551. )
  552. )
  553. ), false);
  554. // Count Increase
  555. $data = array('Post' => array(
  556. 'id' => 22,
  557. 'title' => 'New Post',
  558. 'user_id' => 66,
  559. 'published' => true
  560. ));
  561. $Post->save($data);
  562. $result = $User->find('first', array(
  563. 'conditions' => array('id' => 66),
  564. 'recursive' => -1
  565. ));
  566. $this->assertEquals(3, $result[$User->alias]['post_count']);
  567. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  568. // Count decrease
  569. $Post->delete(1);
  570. $result = $User->find('first', array(
  571. 'conditions' => array('id' => 66),
  572. 'recursive' => -1
  573. ));
  574. $this->assertEquals(2, $result[$User->alias]['post_count']);
  575. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  576. // Count update
  577. $data = $Post->find('first', array(
  578. 'conditions' => array('id' => 1),
  579. 'recursive' => -1
  580. ));
  581. $data[$Post->alias]['user_id'] = 301;
  582. $Post->save($data);
  583. $result = $User->find('all', array('order' => 'User.id'));
  584. $this->assertEquals(2, $result[0]['User']['post_count']);
  585. $this->assertEquals(1, $result[1]['User']['posts_published']);
  586. }
  587. /**
  588. * Tests that counter caches are unchanged when using 'counterCache' => false
  589. *
  590. * @return void
  591. */
  592. public function testCounterCacheSkip() {
  593. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  594. $User = new CounterCacheUser();
  595. $Post = new CounterCachePost();
  596. $data = $Post->find('first', array(
  597. 'conditions' => array('id' => 1),
  598. 'recursive' => -1
  599. ));
  600. $data[$Post->alias]['user_id'] = 301;
  601. $Post->save($data, array('counterCache' => false));
  602. $users = $User->find('all', array('order' => 'User.id'));
  603. $this->assertEquals(2, $users[0]['User']['post_count']);
  604. $this->assertEquals(1, $users[1]['User']['post_count']);
  605. }
  606. /**
  607. * test that beforeValidate returning false can abort saves.
  608. *
  609. * @return void
  610. */
  611. public function testBeforeValidateSaveAbortion() {
  612. $this->loadFixtures('Post');
  613. $Model = new CallbackPostTestModel();
  614. $Model->beforeValidateReturn = false;
  615. $data = array(
  616. 'title' => 'new article',
  617. 'body' => 'this is some text.'
  618. );
  619. $Model->create();
  620. $result = $Model->save($data);
  621. $this->assertFalse($result);
  622. }
  623. /**
  624. * test that beforeSave returning false can abort saves.
  625. *
  626. * @return void
  627. */
  628. public function testBeforeSaveSaveAbortion() {
  629. $this->loadFixtures('Post');
  630. $Model = new CallbackPostTestModel();
  631. $Model->beforeSaveReturn = false;
  632. $data = array(
  633. 'title' => 'new article',
  634. 'body' => 'this is some text.'
  635. );
  636. $Model->create();
  637. $result = $Model->save($data);
  638. $this->assertFalse($result);
  639. }
  640. /**
  641. * testSaveAtomic method
  642. *
  643. * @return void
  644. */
  645. public function testSaveAtomic() {
  646. $this->loadFixtures('Article');
  647. $TestModel = new Article();
  648. // Create record with 'atomic' = false
  649. $data = array(
  650. 'Article' => array(
  651. 'user_id' => '1',
  652. 'title' => 'Fourth Article',
  653. 'body' => 'Fourth Article Body',
  654. 'published' => 'Y'
  655. )
  656. );
  657. $TestModel->create();
  658. $result = $TestModel->save($data, array('atomic' => false));
  659. $this->assertTrue((bool)$result);
  660. // Check record we created
  661. $TestModel->recursive = -1;
  662. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  663. $expected = array(
  664. 'Article' => array(
  665. 'id' => '4',
  666. 'user_id' => '1',
  667. 'title' => 'Fourth Article',
  668. 'body' => 'Fourth Article Body',
  669. 'published' => 'Y'
  670. )
  671. );
  672. $this->assertEquals($expected, $result);
  673. // Create record with 'atomic' = true
  674. $data = array(
  675. 'Article' => array(
  676. 'user_id' => '4',
  677. 'title' => 'Fifth Article',
  678. 'body' => 'Fifth Article Body',
  679. 'published' => 'Y'
  680. )
  681. );
  682. $TestModel->create();
  683. $result = $TestModel->save($data, array('atomic' => true));
  684. $this->assertTrue((bool)$result);
  685. // Check record we created
  686. $TestModel->recursive = -1;
  687. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  688. $expected = array(
  689. 'Article' => array(
  690. 'id' => '5',
  691. 'user_id' => '4',
  692. 'title' => 'Fifth Article',
  693. 'body' => 'Fifth Article Body',
  694. 'published' => 'Y'
  695. )
  696. );
  697. $this->assertEquals($expected, $result);
  698. }
  699. /**
  700. * test save with transaction and ensure there is no missing rollback.
  701. *
  702. * @return void
  703. */
  704. public function testSaveTransactionNoRollback() {
  705. $this->loadFixtures('Post', 'Article');
  706. $db = $this->getMock('DboSource', array('begin', 'connect', 'rollback', 'describe'));
  707. $db->expects($this->once())
  708. ->method('describe')
  709. ->will($this->returnValue(array()));
  710. $db->expects($this->once())
  711. ->method('begin')
  712. ->will($this->returnValue(true));
  713. $db->expects($this->once())
  714. ->method('rollback');
  715. $Post = new TestPost();
  716. $Post->setDataSourceObject($db);
  717. $callback = array($this, 'callbackForTestSaveTransaction');
  718. $Post->getEventManager()->attach($callback, 'Model.beforeSave');
  719. $data = array(
  720. 'Post' => array(
  721. 'author_id' => 1,
  722. 'title' => 'New Fourth Post'
  723. )
  724. );
  725. $Post->save($data, array('atomic' => true));
  726. }
  727. /**
  728. * test callback used in testSaveTransaction method
  729. *
  730. * @return bool false to stop event propagation
  731. */
  732. public function callbackForTestSaveTransaction($event) {
  733. $TestModel = new Article();
  734. // Create record. Do not use same model as in testSaveTransaction
  735. // to avoid infinite loop.
  736. $data = array(
  737. 'Article' => array(
  738. 'user_id' => '1',
  739. 'title' => 'Fourth Article',
  740. 'body' => 'Fourth Article Body',
  741. 'published' => 'Y'
  742. )
  743. );
  744. $TestModel->create();
  745. $result = $TestModel->save($data);
  746. $this->assertTrue((bool)$result);
  747. // force transaction to be rolled back in Post model
  748. $event->stopPropagation();
  749. return false;
  750. }
  751. /**
  752. * testSaveTransaction method
  753. *
  754. * @return void
  755. */
  756. public function testSaveTransaction() {
  757. $this->loadFixtures('Post', 'Article');
  758. $PostModel = new Post();
  759. // Check if Database supports transactions
  760. $PostModel->validate = array('title' => 'notEmpty');
  761. $data = array(
  762. array('author_id' => 1, 'title' => 'New Fourth Post'),
  763. array('author_id' => 1, 'title' => 'New Fifth Post'),
  764. array('author_id' => 1, 'title' => '')
  765. );
  766. $this->assertFalse($PostModel->saveAll($data));
  767. $result = $PostModel->find('all', array('recursive' => -1));
  768. $expectedPosts = array(
  769. array(
  770. 'Post' => array(
  771. 'id' => '1',
  772. 'author_id' => 1,
  773. 'title' => 'First Post',
  774. 'body' => 'First Post Body',
  775. 'published' => 'Y',
  776. 'created' => '2007-03-18 10:39:23',
  777. 'updated' => '2007-03-18 10:41:31'
  778. )
  779. ),
  780. array(
  781. 'Post' => array(
  782. 'id' => '2',
  783. 'author_id' => 3,
  784. 'title' => 'Second Post',
  785. 'body' => 'Second Post Body',
  786. 'published' => 'Y',
  787. 'created' => '2007-03-18 10:41:23',
  788. 'updated' => '2007-03-18 10:43:31'
  789. )
  790. ),
  791. array(
  792. 'Post' => array(
  793. 'id' => '3',
  794. 'author_id' => 1,
  795. 'title' => 'Third Post',
  796. 'body' => 'Third Post Body',
  797. 'published' => 'Y',
  798. 'created' => '2007-03-18 10:43:23',
  799. 'updated' => '2007-03-18 10:45:31'
  800. )
  801. )
  802. );
  803. $this->skipIf(count($result) !== 3, 'Database does not support transactions.');
  804. $this->assertEquals($expectedPosts, $result);
  805. // Database supports transactions --> continue tests
  806. $data = array(
  807. 'Post' => array(
  808. 'author_id' => 1,
  809. 'title' => 'New Fourth Post'
  810. )
  811. );
  812. $callback = array($this, 'callbackForTestSaveTransaction');
  813. $PostModel->getEventManager()->attach($callback, 'Model.beforeSave');
  814. $PostModel->create();
  815. $result = $PostModel->save($data, array('atomic' => true));
  816. $this->assertFalse($result);
  817. $result = $PostModel->find('all', array('recursive' => -1));
  818. $this->assertEquals($expectedPosts, $result);
  819. // Check record we created in callbackForTestSaveTransaction method.
  820. // record should not exist due to rollback
  821. $ArticleModel = new Article();
  822. $result = $ArticleModel->find('all', array('recursive' => -1));
  823. $expectedArticles = array(
  824. array(
  825. 'Article' => array(
  826. 'user_id' => '1',
  827. 'title' => 'First Article',
  828. 'body' => 'First Article Body',
  829. 'published' => 'Y',
  830. 'created' => '2007-03-18 10:39:23',
  831. 'updated' => '2007-03-18 10:41:31',
  832. 'id' => '1'
  833. )
  834. ),
  835. array(
  836. 'Article' => array(
  837. 'user_id' => '3',
  838. 'title' => 'Second Article',
  839. 'body' => 'Second Article Body',
  840. 'published' => 'Y',
  841. 'created' => '2007-03-18 10:41:23',
  842. 'updated' => '2007-03-18 10:43:31',
  843. 'id' => '2'
  844. )
  845. ),
  846. array(
  847. 'Article' => array(
  848. 'user_id' => '1',
  849. 'title' => 'Third Article',
  850. 'body' => 'Third Article Body',
  851. 'published' => 'Y',
  852. 'created' => '2007-03-18 10:43:23',
  853. 'updated' => '2007-03-18 10:45:31',
  854. 'id' => '3'
  855. )
  856. )
  857. );
  858. $this->assertEquals($expectedArticles, $result);
  859. }
  860. /**
  861. * testSaveField method
  862. *
  863. * @return void
  864. */
  865. public function testSaveField() {
  866. $this->loadFixtures('Article');
  867. $TestModel = new Article();
  868. $TestModel->id = 1;
  869. $result = $TestModel->saveField('title', 'New First Article');
  870. $this->assertFalse(empty($result));
  871. $TestModel->recursive = -1;
  872. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  873. $expected = array('Article' => array(
  874. 'id' => '1',
  875. 'user_id' => '1',
  876. 'title' => 'New First Article',
  877. 'body' => 'First Article Body'
  878. ));
  879. $this->assertEquals($expected, $result);
  880. $TestModel->id = 1;
  881. $result = $TestModel->saveField('title', '');
  882. $this->assertFalse(empty($result));
  883. $TestModel->recursive = -1;
  884. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  885. $expected = array('Article' => array(
  886. 'id' => '1',
  887. 'user_id' => '1',
  888. 'title' => '',
  889. 'body' => 'First Article Body'
  890. ));
  891. $result['Article']['title'] = trim($result['Article']['title']);
  892. $this->assertEquals($expected, $result);
  893. $TestModel->id = 1;
  894. $TestModel->set('body', 'Messed up data');
  895. $result = $TestModel->saveField('title', 'First Article');
  896. $this->assertFalse(empty($result));
  897. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  898. $expected = array('Article' => array(
  899. 'id' => '1',
  900. 'user_id' => '1',
  901. 'title' => 'First Article',
  902. 'body' => 'First Article Body'
  903. ));
  904. $this->assertEquals($expected, $result);
  905. $TestModel->recursive = -1;
  906. $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  907. $TestModel->id = 1;
  908. $result = $TestModel->saveField('title', '', true);
  909. $this->assertFalse($result);
  910. $TestModel->recursive = -1;
  911. $TestModel->id = 1;
  912. $result = $TestModel->saveField('user_id', 9999);
  913. $this->assertTrue((bool)$result);
  914. $result = $TestModel->read(array('id', 'user_id'), 1);
  915. $expected = array('Article' => array(
  916. 'id' => '1',
  917. 'user_id' => '9999',
  918. ));
  919. $this->assertEquals($expected, $result);
  920. $this->loadFixtures('Node', 'Dependency');
  921. $Node = new Node();
  922. $Node->set('id', 1);
  923. $result = $Node->read();
  924. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  925. $Node->saveField('state', 10);
  926. $result = $Node->read();
  927. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  928. }
  929. /**
  930. * testSaveWithCreate method
  931. *
  932. * @return void
  933. */
  934. public function testSaveWithCreate() {
  935. $this->loadFixtures(
  936. 'User',
  937. 'Article',
  938. 'User',
  939. 'Comment',
  940. 'Tag',
  941. 'ArticlesTag',
  942. 'Attachment'
  943. );
  944. $TestModel = new User();
  945. $data = array('User' => array(
  946. 'user' => 'user',
  947. 'password' => ''
  948. ));
  949. $result = $TestModel->save($data);
  950. $this->assertFalse($result);
  951. $this->assertTrue(!empty($TestModel->validationErrors));
  952. $TestModel = new Article();
  953. $data = array('Article' => array(
  954. 'user_id' => '',
  955. 'title' => '',
  956. 'body' => ''
  957. ));
  958. $result = $TestModel->create($data) && $TestModel->save();
  959. $this->assertFalse($result);
  960. $this->assertTrue(!empty($TestModel->validationErrors));
  961. $data = array('Article' => array(
  962. 'id' => 1,
  963. 'user_id' => '1',
  964. 'title' => 'New First Article',
  965. 'body' => ''
  966. ));
  967. $result = $TestModel->create($data) && $TestModel->save();
  968. $this->assertFalse($result);
  969. $data = array('Article' => array(
  970. 'id' => 1,
  971. 'title' => 'New First Article'
  972. ));
  973. $result = $TestModel->create() && $TestModel->save($data, false);
  974. $this->assertFalse(empty($result));
  975. $TestModel->recursive = -1;
  976. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  977. $expected = array('Article' => array(
  978. 'id' => '1',
  979. 'user_id' => '1',
  980. 'title' => 'New First Article',
  981. 'body' => 'First Article Body',
  982. 'published' => 'N'
  983. ));
  984. $this->assertEquals($expected, $result);
  985. $data = array('Article' => array(
  986. 'id' => 1,
  987. 'user_id' => '2',
  988. 'title' => 'First Article',
  989. 'body' => 'New First Article Body',
  990. 'published' => 'Y'
  991. ));
  992. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  993. $this->assertFalse(empty($result));
  994. $TestModel->recursive = -1;
  995. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  996. $expected = array('Article' => array(
  997. 'id' => '1',
  998. 'user_id' => '1',
  999. 'title' => 'First Article',
  1000. 'body' => 'First Article Body',
  1001. 'published' => 'Y'
  1002. ));
  1003. $this->assertEquals($expected, $result);
  1004. $data = array(
  1005. 'Article' => array(
  1006. 'user_id' => '2',
  1007. 'title' => 'New Article',
  1008. 'body' => 'New Article Body',
  1009. 'created' => '2007-03-18 14:55:23',
  1010. 'updated' => '2007-03-18 14:57:31'
  1011. ),
  1012. 'Tag' => array('Tag' => array(1, 3))
  1013. );
  1014. $TestModel->create();
  1015. $result = $TestModel->create() && $TestModel->save($data);
  1016. $this->assertFalse(empty($result));
  1017. $TestModel->recursive = 2;
  1018. $result = $TestModel->read(null, 4);
  1019. $expected = array(
  1020. 'Article' => array(
  1021. 'id' => '4',
  1022. 'user_id' => '2',
  1023. 'title' => 'New Article',
  1024. 'body' => 'New Article Body',
  1025. 'published' => 'N',
  1026. 'created' => '2007-03-18 14:55:23',
  1027. 'updated' => '2007-03-18 14:57:31'
  1028. ),
  1029. 'User' => array(
  1030. 'id' => '2',
  1031. 'user' => 'nate',
  1032. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1033. 'created' => '2007-03-17 01:18:23',
  1034. 'updated' => '2007-03-17 01:20:31'
  1035. ),
  1036. 'Comment' => array(),
  1037. 'Tag' => array(
  1038. array(
  1039. 'id' => '1',
  1040. 'tag' => 'tag1',
  1041. 'created' => '2007-03-18 12:22:23',
  1042. 'updated' => '2007-03-18 12:24:31'
  1043. ),
  1044. array(
  1045. 'id' => '3',
  1046. 'tag' => 'tag3',
  1047. 'created' => '2007-03-18 12:26:23',
  1048. 'updated' => '2007-03-18 12:28:31'
  1049. )));
  1050. $this->assertEquals($expected, $result);
  1051. $data = array('Comment' => array(
  1052. 'article_id' => '4',
  1053. 'user_id' => '1',
  1054. 'comment' => 'Comment New Article',
  1055. 'published' => 'Y',
  1056. 'created' => '2007-03-18 14:57:23',
  1057. 'updated' => '2007-03-18 14:59:31'
  1058. ));
  1059. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  1060. $this->assertFalse(empty($result));
  1061. $data = array('Attachment' => array(
  1062. 'comment_id' => '7',
  1063. 'attachment' => 'newattachment.zip',
  1064. 'created' => '2007-03-18 15:02:23',
  1065. 'updated' => '2007-03-18 15:04:31'
  1066. ));
  1067. $result = $TestModel->Comment->Attachment->save($data);
  1068. $this->assertFalse(empty($result));
  1069. $TestModel->recursive = 2;
  1070. $result = $TestModel->read(null, 4);
  1071. $expected = array(
  1072. 'Article' => array(
  1073. 'id' => '4',
  1074. 'user_id' => '2',
  1075. 'title' => 'New Article',
  1076. 'body' => 'New Article Body',
  1077. 'published' => 'N',
  1078. 'created' => '2007-03-18 14:55:23',
  1079. 'updated' => '2007-03-18 14:57:31'
  1080. ),
  1081. 'User' => array(
  1082. 'id' => '2',
  1083. 'user' => 'nate',
  1084. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1085. 'created' => '2007-03-17 01:18:23',
  1086. 'updated' => '2007-03-17 01:20:31'
  1087. ),
  1088. 'Comment' => array(
  1089. array(
  1090. 'id' => '7',
  1091. 'article_id' => '4',
  1092. 'user_id' => '1',
  1093. 'comment' => 'Comment New Article',
  1094. 'published' => 'Y',
  1095. 'created' => '2007-03-18 14:57:23',
  1096. 'updated' => '2007-03-18 14:59:31',
  1097. 'Article' => array(
  1098. 'id' => '4',
  1099. 'user_id' => '2',
  1100. 'title' => 'New Article',
  1101. 'body' => 'New Article Body',
  1102. 'published' => 'N',
  1103. 'created' => '2007-03-18 14:55:23',
  1104. 'updated' => '2007-03-18 14:57:31'
  1105. ),
  1106. 'User' => array(
  1107. 'id' => '1',
  1108. 'user' => 'mariano',
  1109. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1110. 'created' => '2007-03-17 01:16:23',
  1111. 'updated' => '2007-03-17 01:18:31'
  1112. ),
  1113. 'Attachment' => array(
  1114. 'id' => '2',
  1115. 'comment_id' => '7',
  1116. 'attachment' => 'newattachment.zip',
  1117. 'created' => '2007-03-18 15:02:23',
  1118. 'updated' => '2007-03-18 15:04:31'
  1119. ))),
  1120. 'Tag' => array(
  1121. array(
  1122. 'id' => '1',
  1123. 'tag' => 'tag1',
  1124. 'created' => '2007-03-18 12:22:23',
  1125. 'updated' => '2007-03-18 12:24:31'
  1126. ),
  1127. array(
  1128. 'id' => '3',
  1129. 'tag' => 'tag3',
  1130. 'created' => '2007-03-18 12:26:23',
  1131. 'updated' => '2007-03-18 12:28:31'
  1132. )));
  1133. $this->assertEquals($expected, $result);
  1134. }
  1135. /**
  1136. * test that a null Id doesn't cause errors
  1137. *
  1138. * @return void
  1139. */
  1140. public function testSaveWithNullId() {
  1141. $this->loadFixtures('User');
  1142. $User = new User();
  1143. $User->read(null, 1);
  1144. $User->data['User']['id'] = null;
  1145. $result = $User->save(array('password' => 'test'));
  1146. $this->assertFalse(empty($result));
  1147. $this->assertTrue($User->id > 0);
  1148. $User->read(null, 2);
  1149. $User->data['User']['id'] = null;
  1150. $result = $User->save(array('password' => 'test'));
  1151. $this->assertFalse(empty($result));
  1152. $this->assertTrue($User->id > 0);
  1153. $User->data['User'] = array('password' => 'something');
  1154. $result = $User->save();
  1155. $this->assertFalse(empty($result));
  1156. $result = $User->read();
  1157. $this->assertEquals('something', $User->data['User']['password']);
  1158. }
  1159. /**
  1160. * testSaveWithSet method
  1161. *
  1162. * @return void
  1163. */
  1164. public function testSaveWithSet() {
  1165. $this->loadFixtures('Article');
  1166. $TestModel = new Article();
  1167. // Create record we will be updating later
  1168. $data = array('Article' => array(
  1169. 'user_id' => '1',
  1170. 'title' => 'Fourth Article',
  1171. 'body' => 'Fourth Article Body',
  1172. 'published' => 'Y'
  1173. ));
  1174. $result = $TestModel->create() && $TestModel->save($data);
  1175. $this->assertFalse(empty($result));
  1176. // Check record we created
  1177. $TestModel->recursive = -1;
  1178. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  1179. $expected = array('Article' => array(
  1180. 'id' => '4',
  1181. 'user_id' => '1',
  1182. 'title' => 'Fourth Article',
  1183. 'body' => 'Fourth Article Body',
  1184. 'published' => 'Y'
  1185. ));
  1186. $this->assertEquals($expected, $result);
  1187. // Create new record just to overlap Model->id on previously created record
  1188. $data = array('Article' => array(
  1189. 'user_id' => '4',
  1190. 'title' => 'Fifth Article',
  1191. 'body' => 'Fifth Article Body',
  1192. 'published' => 'Y'
  1193. ));
  1194. $result = $TestModel->create() && $TestModel->save($data);
  1195. $this->assertFalse(empty($result));
  1196. $TestModel->recursive = -1;
  1197. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  1198. $expected = array('Article' => array(
  1199. 'id' => '5',
  1200. 'user_id' => '4',
  1201. 'title' => 'Fifth Article',
  1202. 'body' => 'Fifth Article Body',
  1203. 'published' => 'Y'
  1204. ));
  1205. $this->assertEquals($expected, $result);
  1206. // Go back and edit the first article we created, starting by checking it's still there
  1207. $TestModel->recursive = -1;
  1208. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  1209. $expected = array('Article' => array(
  1210. 'id' => '4',
  1211. 'user_id' => '1',
  1212. 'title' => 'Fourth Article',
  1213. 'body' => 'Fourth Article Body',
  1214. 'published' => 'Y'
  1215. ));
  1216. $this->assertEquals($expected, $result);
  1217. // And now do the update with set()
  1218. $data = array('Article' => array(
  1219. 'id' => '4',
  1220. 'title' => 'Fourth Article - New Title',
  1221. 'published' => 'N'
  1222. ));
  1223. $result = $TestModel->set($data) && $TestModel->save();
  1224. $this->assertFalse(empty($result));
  1225. $TestModel->recursive = -1;
  1226. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  1227. $expected = array('Article' => array(
  1228. 'id' => '4',
  1229. 'user_id' => '1',
  1230. 'title' => 'Fourth Article - New Title',
  1231. 'body' => 'Fourth Article Body',
  1232. 'published' => 'N'
  1233. ));
  1234. $this->assertEquals($expected, $result);
  1235. $TestModel->recursive = -1;
  1236. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  1237. $expected = array('Article' => array(
  1238. 'id' => '5',
  1239. 'user_id' => '4',
  1240. 'title' => 'Fifth Article',
  1241. 'body' => 'Fifth Article Body',
  1242. 'published' => 'Y'
  1243. ));
  1244. $this->assertEquals($expected, $result);
  1245. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  1246. $result = ($TestModel->set($data) && $TestModel->save());
  1247. $this->assertFalse(empty($result));
  1248. $TestModel->recursive = -1;
  1249. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  1250. $expected = array('Article' => array(
  1251. 'id' => '5',
  1252. 'user_id' => '4',
  1253. 'title' => 'Fifth Article - New Title 5',
  1254. 'body' => 'Fifth Article Body',
  1255. 'published' => 'Y'
  1256. ));
  1257. $this->assertEquals($expected, $result);
  1258. $TestModel->recursive = -1;
  1259. $result = $TestModel->find('all', array(
  1260. 'fields' => array('id', 'title'),
  1261. 'order' => array('Article.id' => 'ASC')
  1262. ));
  1263. $expected = array(
  1264. array('Article' => array('id' => 1, 'title' => 'First Article')),
  1265. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  1266. array('Article' => array('id' => 3, 'title' => 'Third Article')),
  1267. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
  1268. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
  1269. );
  1270. $this->assertEquals($expected, $result);
  1271. }
  1272. /**
  1273. * testSaveWithNonExistentFields method
  1274. *
  1275. * @return void
  1276. */
  1277. public function testSaveWithNonExistentFields() {
  1278. $this->loadFixtures('Article');
  1279. $TestModel = new Article();
  1280. $TestModel->recursive = -1;
  1281. $data = array(
  1282. 'non_existent' => 'This field does not exist',
  1283. 'user_id' => '1',
  1284. 'title' => 'Fourth Article - New Title',
  1285. 'body' => 'Fourth Article Body',
  1286. 'published' => 'N'
  1287. );
  1288. $result = $TestModel->create() && $TestModel->save($data);
  1289. $this->assertFalse(empty($result));
  1290. $expected = array('Article' => array(
  1291. 'id' => '4',
  1292. 'user_id' => '1',
  1293. 'title' => 'Fourth Article - New Title',
  1294. 'body' => 'Fourth Article Body',
  1295. 'published' => 'N'
  1296. ));
  1297. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  1298. $this->assertEquals($expected, $result);
  1299. $data = array(
  1300. 'user_id' => '1',
  1301. 'non_existent' => 'This field does not exist',
  1302. 'title' => 'Fifth Article - New Title',
  1303. 'body' => 'Fifth Article Body',
  1304. 'published' => 'N'
  1305. );
  1306. $result = $TestModel->create() && $TestModel->save($data);
  1307. $this->assertFalse(empty($result));
  1308. $expected = array('Article' => array(
  1309. 'id' => '5',
  1310. 'user_id' => '1',
  1311. 'title' => 'Fifth Article - New Title',
  1312. 'body' => 'Fifth Article Body',
  1313. 'published' => 'N'
  1314. ));
  1315. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  1316. $this->assertEquals($expected, $result);
  1317. }
  1318. /**
  1319. * testSaveFromXml method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testSaveFromXml() {
  1324. $this->markTestSkipped('This feature needs to be fixed or dropped');
  1325. $this->loadFixtures('Article');
  1326. App::uses('Xml', 'Utility');
  1327. $Article = new Article();
  1328. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  1329. $this->assertFalse(empty($result));
  1330. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  1331. $this->assertFalse(empty($results));
  1332. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  1333. $this->assertFalse(empty($result));
  1334. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  1335. $this->assertFalse(empty($results));
  1336. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  1337. $this->assertFalse(empty($result));
  1338. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  1339. $this->assertFalse(empty($results));
  1340. }
  1341. /**
  1342. * testSaveHabtm method
  1343. *
  1344. * @return void
  1345. */
  1346. public function testSaveHabtm() {
  1347. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1348. $TestModel = new Article();
  1349. $result = $TestModel->findById(2);
  1350. $expected = array(
  1351. 'Article' => array(
  1352. 'id' => '2',
  1353. 'user_id' => '3',
  1354. 'title' => 'Second Article',
  1355. 'body' => 'Second Article Body',
  1356. 'published' => 'Y',
  1357. 'created' => '2007-03-18 10:41:23',
  1358. 'updated' => '2007-03-18 10:43:31'
  1359. ),
  1360. 'User' => array(
  1361. 'id' => '3',
  1362. 'user' => 'larry',
  1363. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1364. 'created' => '2007-03-17 01:20:23',
  1365. 'updated' => '2007-03-17 01:22:31'
  1366. ),
  1367. 'Comment' => array(
  1368. array(
  1369. 'id' => '5',
  1370. 'article_id' => '2',
  1371. 'user_id' => '1',
  1372. 'comment' => 'First Comment for Second Article',
  1373. 'published' => 'Y',
  1374. 'created' => '2007-03-18 10:53:23',
  1375. 'updated' => '2007-03-18 10:55:31'
  1376. ),
  1377. array(
  1378. 'id' => '6',
  1379. 'article_id' => '2',
  1380. 'user_id' => '2',
  1381. 'comment' => 'Second Comment for Second Article',
  1382. 'published' => 'Y',
  1383. 'created' => '2007-03-18 10:55:23',
  1384. 'updated' => '2007-03-18 10:57:31'
  1385. )),
  1386. 'Tag' => array(
  1387. array(
  1388. 'id' => '1',
  1389. 'tag' => 'tag1',
  1390. 'created' => '2007-03-18 12:22:23',
  1391. 'updated' => '2007-03-18 12:24:31'
  1392. ),
  1393. array(
  1394. 'id' => '3',
  1395. 'tag' => 'tag3',
  1396. 'created' => '2007-03-18 12:26:23',
  1397. 'updated' => '2007-03-18 12:28:31'
  1398. )
  1399. )
  1400. );
  1401. $this->assertEquals($expected, $result);
  1402. $data = array(
  1403. 'Article' => array(
  1404. 'id' => '2',
  1405. 'title' => 'New Second Article'
  1406. ),
  1407. 'Tag' => array('Tag' => array(1, 2))
  1408. );
  1409. $result = $TestModel->set($data);
  1410. $this->assertFalse(empty($result));
  1411. $result = $TestModel->save();
  1412. $this->assertFalse(empty($result));
  1413. $this->assertEquals($data['Tag'], $result['Tag']);
  1414. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1415. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1416. $expected = array(
  1417. 'Article' => array(
  1418. 'id' => '2',
  1419. 'user_id' => '3',
  1420. 'title' => 'New Second Article',
  1421. 'body' => 'Second Article Body'
  1422. ),
  1423. 'Tag' => array(
  1424. array(
  1425. 'id' => '1',
  1426. 'tag' => 'tag1',
  1427. 'created' => '2007-03-18 12:22:23',
  1428. 'updated' => '2007-03-18 12:24:31'
  1429. ),
  1430. array(
  1431. 'id' => '2',
  1432. 'tag' => 'tag2',
  1433. 'created' => '2007-03-18 12:24:23',
  1434. 'updated' => '2007-03-18 12:26:31'
  1435. )));
  1436. $this->assertEquals($expected, $result);
  1437. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1438. $result = $TestModel->set($data);
  1439. $this->assertFalse(empty($result));
  1440. $result = $TestModel->save();
  1441. $this->assertFalse(empty($result));
  1442. $TestModel->unbindModel(array(
  1443. 'belongsTo' => array('User'),
  1444. 'hasMany' => array('Comment')
  1445. ));
  1446. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1447. $expected = array(
  1448. 'Article' => array(
  1449. 'id' => '2',
  1450. 'user_id' => '3',
  1451. 'title' => 'New Second Article',
  1452. 'body' => 'Second Article Body'
  1453. ),
  1454. 'Tag' => array(
  1455. array(
  1456. 'id' => '2',
  1457. 'tag' => 'tag2',
  1458. 'created' => '2007-03-18 12:24:23',
  1459. 'updated' => '2007-03-18 12:26:31'
  1460. ),
  1461. array(
  1462. 'id' => '3',
  1463. 'tag' => 'tag3',
  1464. 'created' => '2007-03-18 12:26:23',
  1465. 'updated' => '2007-03-18 12:28:31'
  1466. )));
  1467. $this->assertEquals($expected, $result);
  1468. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1469. $result = $TestModel->set($data);
  1470. $this->assertFalse(empty($result));
  1471. $result = $TestModel->save();
  1472. $this->assertFalse(empty($result));
  1473. $TestModel->unbindModel(array(
  1474. 'belongsTo' => array('User'),
  1475. 'hasMany' => array('Comment')
  1476. ));
  1477. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1478. $expected = array(
  1479. 'Article' => array(
  1480. 'id' => '2',
  1481. 'user_id' => '3',
  1482. 'title' => 'New Second Article',
  1483. 'body' => 'Second Article Body'
  1484. ),
  1485. 'Tag' => array(
  1486. array(
  1487. 'id' => '1',
  1488. 'tag' => 'tag1',
  1489. 'created' => '2007-03-18 12:22:23',
  1490. 'updated' => '2007-03-18 12:24:31'
  1491. ),
  1492. array(
  1493. 'id' => '2',
  1494. 'tag' => 'tag2',
  1495. 'created' => '2007-03-18 12:24:23',
  1496. 'updated' => '2007-03-18 12:26:31'
  1497. ),
  1498. array(
  1499. 'id' => '3',
  1500. 'tag' => 'tag3',
  1501. 'created' => '2007-03-18 12:26:23',
  1502. 'updated' => '2007-03-18 12:28:31'
  1503. )));
  1504. $this->assertEquals($expected, $result);
  1505. $data = array('Tag' => array('Tag' => array()));
  1506. $result = $TestModel->set($data);
  1507. $this->assertFalse(empty($result));
  1508. $result = $TestModel->save();
  1509. $this->assertFalse(empty($result));
  1510. $data = array('Tag' => array('Tag' => ''));
  1511. $result = $TestModel->set($data);
  1512. $this->assertFalse(empty($result));
  1513. $result = $TestModel->save();
  1514. $this->assertFalse(empty($result));
  1515. $TestModel->unbindModel(array(
  1516. 'belongsTo' => array('User'),
  1517. 'hasMany' => array('Comment')
  1518. ));
  1519. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1520. $expected = array(
  1521. 'Article' => array(
  1522. 'id' => '2',
  1523. 'user_id' => '3',
  1524. 'title' => 'New Second Article',
  1525. 'body' => 'Second Article Body'
  1526. ),
  1527. 'Tag' => array()
  1528. );
  1529. $this->assertEquals($expected, $result);
  1530. $data = array('Tag' => array('Tag' => array(2, 3)));
  1531. $result = $TestModel->set($data);
  1532. $this->assertFalse(empty($result));
  1533. $result = $TestModel->save();
  1534. $this->assertFalse(empty($result));
  1535. $TestModel->unbindModel(array(
  1536. 'belongsTo' => array('User'),
  1537. 'hasMany' => array('Comment')
  1538. ));
  1539. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1540. $expected = array(
  1541. 'Article' => array(
  1542. 'id' => '2',
  1543. 'user_id' => '3',
  1544. 'title' => 'New Second Article',
  1545. 'body' => 'Second Article Body'
  1546. ),
  1547. 'Tag' => array(
  1548. array(
  1549. 'id' => '2',
  1550. 'tag' => 'tag2',
  1551. 'created' => '2007-03-18 12:24:23',
  1552. 'updated' => '2007-03-18 12:26:31'
  1553. ),
  1554. array(
  1555. 'id' => '3',
  1556. 'tag' => 'tag3',
  1557. 'created' => '2007-03-18 12:26:23',
  1558. 'updated' => '2007-03-18 12:28:31'
  1559. )));
  1560. $this->assertEquals($expected, $result);
  1561. $data = array(
  1562. 'Tag' => array(
  1563. 'Tag' => array(1, 2)
  1564. ),
  1565. 'Article' => array(
  1566. 'id' => '2',
  1567. 'title' => 'New Second Article'
  1568. ));
  1569. $result = $TestModel->set($data);
  1570. $this->assertFalse(empty($result));
  1571. $result = $TestModel->save();
  1572. $this->assertFalse(empty($result));
  1573. $TestModel->unbindModel(array(
  1574. 'belongsTo' => array('User'),
  1575. 'hasMany' => array('Comment')
  1576. ));
  1577. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1578. $expected = array(
  1579. 'Article' => array(
  1580. 'id' => '2',
  1581. 'user_id' => '3',
  1582. 'title' => 'New Second Article',
  1583. 'body' => 'Second Article Body'
  1584. ),
  1585. 'Tag' => array(
  1586. array(
  1587. 'id' => '1',
  1588. 'tag' => 'tag1',
  1589. 'created' => '2007-03-18 12:22:23',
  1590. 'updated' => '2007-03-18 12:24:31'
  1591. ),
  1592. array(
  1593. 'id' => '2',
  1594. 'tag' => 'tag2',
  1595. 'created' => '2007-03-18 12:24:23',
  1596. 'updated' => '2007-03-18 12:26:31'
  1597. )));
  1598. $this->assertEquals($expected, $result);
  1599. $data = array(
  1600. 'Tag' => array(
  1601. 'Tag' => array(1, 2)
  1602. ),
  1603. 'Article' => array(
  1604. 'id' => '2',
  1605. 'title' => 'New Second Article Title'
  1606. ));
  1607. $result = $TestModel->set($data);
  1608. $this->assertFalse(empty($result));
  1609. $result = $TestModel->save();
  1610. $this->assertFalse(empty($result));
  1611. $TestModel->unbindModel(array(
  1612. 'belongsTo' => array('User'),
  1613. 'hasMany' => array('Comment')
  1614. ));
  1615. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1616. $expected = array(
  1617. 'Article' => array(
  1618. 'id' => '2',
  1619. 'user_id' => '3',
  1620. 'title' => 'New Second Article Title',
  1621. 'body' => 'Second Article Body'
  1622. ),
  1623. 'Tag' => array(
  1624. array(
  1625. 'id' => '1',
  1626. 'tag' => 'tag1',
  1627. 'created' => '2007-03-18 12:22:23',
  1628. 'updated' => '2007-03-18 12:24:31'
  1629. ),
  1630. array(

Large files files are truncated, but you can click here to view the full file