PageRenderTime 86ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 2ms

/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
  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(
  1631. 'id' => '2',
  1632. 'tag' => 'tag2',
  1633. 'created' => '2007-03-18 12:24:23',
  1634. 'updated' => '2007-03-18 12:26:31'
  1635. )
  1636. )
  1637. );
  1638. $this->assertEquals($expected, $result);
  1639. $data = array(
  1640. 'Tag' => array(
  1641. 'Tag' => array(2, 3)
  1642. ),
  1643. 'Article' => array(
  1644. 'id' => '2',
  1645. 'title' => 'Changed Second Article'
  1646. ));
  1647. $result = $TestModel->set($data);
  1648. $this->assertFalse(empty($result));
  1649. $result = $TestModel->save();
  1650. $this->assertFalse(empty($result));
  1651. $TestModel->unbindModel(array(
  1652. 'belongsTo' => array('User'),
  1653. 'hasMany' => array('Comment')
  1654. ));
  1655. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1656. $expected = array(
  1657. 'Article' => array(
  1658. 'id' => '2',
  1659. 'user_id' => '3',
  1660. 'title' => 'Changed Second Article',
  1661. 'body' => 'Second Article Body'
  1662. ),
  1663. 'Tag' => array(
  1664. array(
  1665. 'id' => '2',
  1666. 'tag' => 'tag2',
  1667. 'created' => '2007-03-18 12:24:23',
  1668. 'updated' => '2007-03-18 12:26:31'
  1669. ),
  1670. array(
  1671. 'id' => '3',
  1672. 'tag' => 'tag3',
  1673. 'created' => '2007-03-18 12:26:23',
  1674. 'updated' => '2007-03-18 12:28:31'
  1675. )
  1676. )
  1677. );
  1678. $this->assertEquals($expected, $result);
  1679. $data = array(
  1680. 'Tag' => array(
  1681. 'Tag' => array(1, 3)
  1682. ),
  1683. 'Article' => array('id' => '2'),
  1684. );
  1685. $result = $TestModel->set($data);
  1686. $this->assertFalse(empty($result));
  1687. $result = $TestModel->save();
  1688. $this->assertFalse(empty($result));
  1689. $TestModel->unbindModel(array(
  1690. 'belongsTo' => array('User'),
  1691. 'hasMany' => array('Comment')
  1692. ));
  1693. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1694. $expected = array(
  1695. 'Article' => array(
  1696. 'id' => '2',
  1697. 'user_id' => '3',
  1698. 'title' => 'Changed Second Article',
  1699. 'body' => 'Second Article Body'
  1700. ),
  1701. 'Tag' => array(
  1702. array(
  1703. 'id' => '1',
  1704. 'tag' => 'tag1',
  1705. 'created' => '2007-03-18 12:22:23',
  1706. 'updated' => '2007-03-18 12:24:31'
  1707. ),
  1708. array(
  1709. 'id' => '3',
  1710. 'tag' => 'tag3',
  1711. 'created' => '2007-03-18 12:26:23',
  1712. 'updated' => '2007-03-18 12:28:31'
  1713. )));
  1714. $this->assertEquals($expected, $result);
  1715. $data = array(
  1716. 'Article' => array(
  1717. 'id' => 10,
  1718. 'user_id' => '2',
  1719. 'title' => 'New Article With Tags and fieldList',
  1720. 'body' => 'New Article Body with Tags and fieldList',
  1721. 'created' => '2007-03-18 14:55:23',
  1722. 'updated' => '2007-03-18 14:57:31'
  1723. ),
  1724. 'Tag' => array(
  1725. 'Tag' => array(1, 2, 3)
  1726. )
  1727. );
  1728. $result = $TestModel->create()
  1729. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1730. $this->assertFalse(empty($result));
  1731. $TestModel->unbindModel(array(
  1732. 'belongsTo' => array('User'),
  1733. 'hasMany' => array('Comment')
  1734. ));
  1735. $result = $TestModel->read();
  1736. $expected = array(
  1737. 'Article' => array(
  1738. 'id' => 4,
  1739. 'user_id' => 2,
  1740. 'title' => 'New Article With Tags and fieldList',
  1741. 'body' => '',
  1742. 'published' => 'N',
  1743. 'created' => '',
  1744. 'updated' => ''
  1745. ),
  1746. 'Tag' => array(
  1747. 0 => array(
  1748. 'id' => 1,
  1749. 'tag' => 'tag1',
  1750. 'created' => '2007-03-18 12:22:23',
  1751. 'updated' => '2007-03-18 12:24:31'
  1752. ),
  1753. 1 => array(
  1754. 'id' => 2,
  1755. 'tag' => 'tag2',
  1756. 'created' => '2007-03-18 12:24:23',
  1757. 'updated' => '2007-03-18 12:26:31'
  1758. ),
  1759. 2 => array(
  1760. 'id' => 3,
  1761. 'tag' => 'tag3',
  1762. 'created' => '2007-03-18 12:26:23',
  1763. 'updated' => '2007-03-18 12:28:31'
  1764. )));
  1765. $this->assertEquals($expected, $result);
  1766. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1767. $TestModel = new JoinA();
  1768. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1769. $data = array(
  1770. 'JoinA' => array(
  1771. 'id' => 1,
  1772. 'name' => 'Join A 1',
  1773. 'body' => 'Join A 1 Body',
  1774. ),
  1775. 'JoinC' => array(
  1776. 'JoinC' => array(
  1777. array('join_c_id' => 2, 'other' => 'new record'),
  1778. array('join_c_id' => 3, 'other' => 'new record')
  1779. )
  1780. )
  1781. );
  1782. $TestModel->save($data);
  1783. $result = $TestModel->read(null, 1);
  1784. $expected = array(4, 5);
  1785. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
  1786. $expected = array('new record', 'new record');
  1787. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
  1788. }
  1789. /**
  1790. * test that saving HABTM with an empty array will clear existing HABTM if
  1791. * unique is true
  1792. *
  1793. * @return void
  1794. */
  1795. public function testSaveHabtmEmptyData() {
  1796. $this->loadFixtures('Node', 'Dependency');
  1797. $Node = new Node();
  1798. $data = array(
  1799. 'Node' => array('name' => 'New First')
  1800. );
  1801. $Node->id = 1;
  1802. $Node->save($data);
  1803. $node = $Node->find('first', array(
  1804. 'conditions' => array('Node.id' => 1),
  1805. 'contain' => array('ParentNode')
  1806. ));
  1807. $result = Hash::extract($node, 'ParentNode.{n}.id');
  1808. $expected = array(2);
  1809. $this->assertEquals($expected, $result);
  1810. $data = array(
  1811. 'ParentNode' => array()
  1812. );
  1813. $Node->id = 1;
  1814. $Node->save($data);
  1815. $node = $Node->find('first', array(
  1816. 'conditions' => array('Node.id' => 1),
  1817. 'contain' => array('ParentNode')
  1818. ));
  1819. $result = Hash::extract($node, 'ParentNode.{n}.id');
  1820. $expected = array();
  1821. $this->assertEquals($expected, $result);
  1822. }
  1823. /**
  1824. * testSaveHabtmNoPrimaryData method
  1825. *
  1826. * @return void
  1827. */
  1828. public function testSaveHabtmNoPrimaryData() {
  1829. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1830. $TestModel = new Article();
  1831. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
  1832. $result = $TestModel->findById(2);
  1833. $expected = array(
  1834. 'Article' => array(
  1835. 'id' => '2',
  1836. 'user_id' => '3',
  1837. 'title' => 'Second Article',
  1838. 'body' => 'Second Article Body',
  1839. 'published' => 'Y',
  1840. 'created' => '2007-03-18 10:41:23',
  1841. 'updated' => '2007-03-18 10:43:31'
  1842. ),
  1843. 'Tag' => array(
  1844. array(
  1845. 'id' => '1',
  1846. 'tag' => 'tag1',
  1847. 'created' => '2007-03-18 12:22:23',
  1848. 'updated' => '2007-03-18 12:24:31'
  1849. ),
  1850. array(
  1851. 'id' => '3',
  1852. 'tag' => 'tag3',
  1853. 'created' => '2007-03-18 12:26:23',
  1854. 'updated' => '2007-03-18 12:28:31'
  1855. )
  1856. )
  1857. );
  1858. $this->assertEquals($expected, $result);
  1859. $TestModel->id = 2;
  1860. $data = array('Tag' => array('Tag' => array(2)));
  1861. $result = $TestModel->save($data);
  1862. $this->assertEquals($data['Tag'], $result['Tag']);
  1863. $result = $TestModel->findById(2);
  1864. $expected = array(
  1865. 'Article' => array(
  1866. 'id' => '2',
  1867. 'user_id' => '3',
  1868. 'title' => 'Second Article',
  1869. 'body' => 'Second Article Body',
  1870. 'published' => 'Y',
  1871. 'created' => '2007-03-18 10:41:23',
  1872. 'updated' => self::date()
  1873. ),
  1874. 'Tag' => array(
  1875. array(
  1876. 'id' => '2',
  1877. 'tag' => 'tag2',
  1878. 'created' => '2007-03-18 12:24:23',
  1879. 'updated' => '2007-03-18 12:26:31'
  1880. )
  1881. )
  1882. );
  1883. $this->assertEquals($expected, $result);
  1884. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1885. $TestModel = new Portfolio();
  1886. $result = $TestModel->findById(2);
  1887. $expected = array(
  1888. 'Portfolio' => array(
  1889. 'id' => 2,
  1890. 'seller_id' => 1,
  1891. 'name' => 'Portfolio 2'
  1892. ),
  1893. 'Item' => array(
  1894. array(
  1895. 'id' => 2,
  1896. 'syfile_id' => 2,
  1897. 'published' => '',
  1898. 'name' => 'Item 2',
  1899. 'ItemsPortfolio' => array(
  1900. 'id' => 2,
  1901. 'item_id' => 2,
  1902. 'portfolio_id' => 2
  1903. )
  1904. ),
  1905. array(
  1906. 'id' => 6,
  1907. 'syfile_id' => 6,
  1908. 'published' => '',
  1909. 'name' => 'Item 6',
  1910. 'ItemsPortfolio' => array(
  1911. 'id' => 6,
  1912. 'item_id' => 6,
  1913. 'portfolio_id' => 2
  1914. )
  1915. )
  1916. )
  1917. );
  1918. $this->assertEquals($expected, $result);
  1919. $data = array('Item' => array('Item' => array(1, 2)));
  1920. $TestModel->id = 2;
  1921. $result = $TestModel->save($data);
  1922. $this->assertTrue((bool)$result);
  1923. $result = $TestModel->findById(2);
  1924. $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
  1925. $expected = array(
  1926. 'Portfolio' => array(
  1927. 'id' => 2,
  1928. 'seller_id' => 1,
  1929. 'name' => 'Portfolio 2'
  1930. ),
  1931. 'Item' => array(
  1932. array(
  1933. 'id' => 1,
  1934. 'syfile_id' => 1,
  1935. 'published' => '',
  1936. 'name' => 'Item 1',
  1937. 'ItemsPortfolio' => array(
  1938. 'id' => 7,
  1939. 'item_id' => 1,
  1940. 'portfolio_id' => 2
  1941. )
  1942. ),
  1943. array(
  1944. 'id' => 2,
  1945. 'syfile_id' => 2,
  1946. 'published' => '',
  1947. 'name' => 'Item 2',
  1948. 'ItemsPortfolio' => array(
  1949. 'id' => 8,
  1950. 'item_id' => 2,
  1951. 'portfolio_id' => 2
  1952. )
  1953. )
  1954. )
  1955. );
  1956. $this->assertEquals($expected, $result);
  1957. }
  1958. /**
  1959. * testSaveHabtmCustomKeys method
  1960. *
  1961. * @return void
  1962. */
  1963. public function testSaveHabtmCustomKeys() {
  1964. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1965. $Story = new Story();
  1966. $data = array(
  1967. 'Story' => array('story' => '1'),
  1968. 'Tag' => array(
  1969. 'Tag' => array(2, 3)
  1970. ));
  1971. $result = $Story->set($data);
  1972. $this->assertFalse(empty($result));
  1973. $result = $Story->save();
  1974. $this->assertFalse(empty($result));
  1975. $result = $Story->find('all', array('order' => array('Story.story')));
  1976. $expected = array(
  1977. array(
  1978. 'Story' => array(
  1979. 'story' => 1,
  1980. 'title' => 'First Story'
  1981. ),
  1982. 'Tag' => array(
  1983. array(
  1984. 'id' => 2,
  1985. 'tag' => 'tag2',
  1986. 'created' => '2007-03-18 12:24:23',
  1987. 'updated' => '2007-03-18 12:26:31'
  1988. ),
  1989. array(
  1990. 'id' => 3,
  1991. 'tag' => 'tag3',
  1992. 'created' => '2007-03-18 12:26:23',
  1993. 'updated' => '2007-03-18 12:28:31'
  1994. ))),
  1995. array(
  1996. 'Story' => array(
  1997. 'story' => 2,
  1998. 'title' => 'Second Story'
  1999. ),
  2000. 'Tag' => array()
  2001. ));
  2002. $this->assertEquals($expected, $result);
  2003. }
  2004. /**
  2005. * test that saving habtm records respects conditions set in the 'conditions' key
  2006. * for the association.
  2007. *
  2008. * @return void
  2009. */
  2010. public function testHabtmSaveWithConditionsInAssociation() {
  2011. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  2012. $Something = new Something();
  2013. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  2014. $Something->bindModel(array(
  2015. 'hasAndBelongsToMany' => array(
  2016. 'DoomedSomethingElse' => array(
  2017. 'className' => 'SomethingElse',
  2018. 'joinTable' => 'join_things',
  2019. 'conditions' => array('JoinThing.doomed' => true),
  2020. 'unique' => true
  2021. ),
  2022. 'NotDoomedSomethingElse' => array(
  2023. 'className' => 'SomethingElse',
  2024. 'joinTable' => 'join_things',
  2025. 'conditions' => array('JoinThing.doomed' => 0),
  2026. 'unique' => true
  2027. )
  2028. )
  2029. ), false);
  2030. $result = $Something->read(null, 1);
  2031. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  2032. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  2033. $data = array(
  2034. 'Something' => array('id' => 1),
  2035. 'NotDoomedSomethingElse' => array(
  2036. 'NotDoomedSomethingElse' => array(
  2037. array('something_else_id' => 2, 'doomed' => 0),
  2038. array('something_else_id' => 3, 'doomed' => 0)
  2039. )
  2040. )
  2041. );
  2042. $Something->create($data);
  2043. $result = $Something->save();
  2044. $this->assertFalse(empty($result));
  2045. $result = $Something->read(null, 1);
  2046. $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
  2047. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  2048. }
  2049. /**
  2050. * testHabtmSaveKeyResolution method
  2051. *
  2052. * @return void
  2053. */
  2054. public function testHabtmSaveKeyResolution() {
  2055. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  2056. $ThePaper = new ThePaper();
  2057. $ThePaper->id = 1;
  2058. $ThePaper->save(array('Monkey' => array(2, 3)));
  2059. $result = $ThePaper->findById(1);
  2060. $expected = array(
  2061. array(
  2062. 'id' => '2',
  2063. 'device_type_id' => '1',
  2064. 'name' => 'Device 2',
  2065. 'typ' => '1'
  2066. ),
  2067. array(
  2068. 'id' => '3',
  2069. 'device_type_id' => '1',
  2070. 'name' => 'Device 3',
  2071. 'typ' => '2'
  2072. ));
  2073. $this->assertEquals($expected, $result['Monkey']);
  2074. $ThePaper->id = 2;
  2075. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  2076. $result = $ThePaper->findById(2);
  2077. $expected = array(
  2078. array(
  2079. 'id' => '1',
  2080. 'device_type_id' => '1',
  2081. 'name' => 'Device 1',
  2082. 'typ' => '1'
  2083. ),
  2084. array(
  2085. 'id' => '2',
  2086. 'device_type_id' => '1',
  2087. 'name' => 'Device 2',
  2088. 'typ' => '1'
  2089. ),
  2090. array(
  2091. 'id' => '3',
  2092. 'device_type_id' => '1',
  2093. 'name' => 'Device 3',
  2094. 'typ' => '2'
  2095. ));
  2096. $this->assertEquals($expected, $result['Monkey']);
  2097. $ThePaper->id = 2;
  2098. $ThePaper->save(array('Monkey' => array(1, 3)));
  2099. $result = $ThePaper->findById(2);
  2100. $expected = array(
  2101. array(
  2102. 'id' => '1',
  2103. 'device_type_id' => '1',
  2104. 'name' => 'Device 1',
  2105. 'typ' => '1'
  2106. ),
  2107. array(
  2108. 'id' => '3',
  2109. 'device_type_id' => '1',
  2110. 'name' => 'Device 3',
  2111. 'typ' => '2'
  2112. ));
  2113. $this->assertEquals($expected, $result['Monkey']);
  2114. $result = $ThePaper->findById(1);
  2115. $expected = array(
  2116. array(
  2117. 'id' => '2',
  2118. 'device_type_id' => '1',
  2119. 'name' => 'Device 2',
  2120. 'typ' => '1'
  2121. ),
  2122. array(
  2123. 'id' => '3',
  2124. 'device_type_id' => '1',
  2125. 'name' => 'Device 3',
  2126. 'typ' => '2'
  2127. ));
  2128. $this->assertEquals($expected, $result['Monkey']);
  2129. }
  2130. /**
  2131. * testCreationOfEmptyRecord method
  2132. *
  2133. * @return void
  2134. */
  2135. public function testCreationOfEmptyRecord() {
  2136. $this->loadFixtures('Author');
  2137. $TestModel = new Author();
  2138. $this->assertEquals(4, $TestModel->find('count'));
  2139. $TestModel->deleteAll(true, false, false);
  2140. $this->assertEquals(0, $TestModel->find('count'));
  2141. $result = $TestModel->save();
  2142. $this->assertTrue(isset($result['Author']['created']));
  2143. $this->assertTrue(isset($result['Author']['updated']));
  2144. $this->assertEquals(1, $TestModel->find('count'));
  2145. }
  2146. /**
  2147. * testCreateWithPKFiltering method
  2148. *
  2149. * @return void
  2150. */
  2151. public function testCreateWithPKFiltering() {
  2152. $TestModel = new Article();
  2153. $data = array(
  2154. 'id' => 5,
  2155. 'user_id' => 2,
  2156. 'title' => 'My article',
  2157. 'body' => 'Some text'
  2158. );
  2159. $result = $TestModel->create($data);
  2160. $expected = array(
  2161. 'Article' => array(
  2162. 'published' => 'N',
  2163. 'id' => 5,
  2164. 'user_id' => 2,
  2165. 'title' => 'My article',
  2166. 'body' => 'Some text'
  2167. ));
  2168. $this->assertEquals($expected, $result);
  2169. $this->assertEquals(5, $TestModel->id);
  2170. $result = $TestModel->create($data, true);
  2171. $expected = array(
  2172. 'Article' => array(
  2173. 'published' => 'N',
  2174. 'id' => false,
  2175. 'user_id' => 2,
  2176. 'title' => 'My article',
  2177. 'body' => 'Some text'
  2178. ));
  2179. $this->assertEquals($expected, $result);
  2180. $this->assertFalse($TestModel->id);
  2181. $result = $TestModel->create(array('Article' => $data), true);
  2182. $expected = array(
  2183. 'Article' => array(
  2184. 'published' => 'N',
  2185. 'id' => false,
  2186. 'user_id' => 2,
  2187. 'title' => 'My article',
  2188. 'body' => 'Some text'
  2189. ));
  2190. $this->assertEquals($expected, $result);
  2191. $this->assertFalse($TestModel->id);
  2192. $data = array(
  2193. 'id' => 6,
  2194. 'user_id' => 2,
  2195. 'title' => 'My article',
  2196. 'body' => 'Some text',
  2197. 'created' => '1970-01-01 00:00:00',
  2198. 'updated' => '1970-01-01 12:00:00',
  2199. 'modified' => '1970-01-01 12:00:00'
  2200. );
  2201. $result = $TestModel->create($data);
  2202. $expected = array(
  2203. 'Article' => array(
  2204. 'published' => 'N',
  2205. 'id' => 6,
  2206. 'user_id' => 2,
  2207. 'title' => 'My article',
  2208. 'body' => 'Some text',
  2209. 'created' => '1970-01-01 00:00:00',
  2210. 'updated' => '1970-01-01 12:00:00',
  2211. 'modified' => '1970-01-01 12:00:00'
  2212. ));
  2213. $this->assertEquals($expected, $result);
  2214. $this->assertEquals(6, $TestModel->id);
  2215. $result = $TestModel->create(array(
  2216. 'Article' => array_diff_key($data, array(
  2217. 'created' => true,
  2218. 'updated' => true,
  2219. 'modified' => true
  2220. ))), true);
  2221. $expected = array(
  2222. 'Article' => array(
  2223. 'published' => 'N',
  2224. 'id' => false,
  2225. 'user_id' => 2,
  2226. 'title' => 'My article',
  2227. 'body' => 'Some text'
  2228. ));
  2229. $this->assertEquals($expected, $result);
  2230. $this->assertFalse($TestModel->id);
  2231. }
  2232. /**
  2233. * testCreationWithMultipleData method
  2234. *
  2235. * @return void
  2236. */
  2237. public function testCreationWithMultipleData() {
  2238. $this->loadFixtures('Article', 'Comment');
  2239. $Article = new Article();
  2240. $Comment = new Comment();
  2241. $articles = $Article->find('all', array(
  2242. 'fields' => array('id', 'title'),
  2243. 'recursive' => -1,
  2244. 'order' => array('Article.id' => 'ASC')
  2245. ));
  2246. $expected = array(
  2247. array('Article' => array(
  2248. 'id' => 1,
  2249. 'title' => 'First Article'
  2250. )),
  2251. array('Article' => array(
  2252. 'id' => 2,
  2253. 'title' => 'Second Article'
  2254. )),
  2255. array('Article' => array(
  2256. 'id' => 3,
  2257. 'title' => 'Third Article'
  2258. )));
  2259. $this->assertEquals($expected, $articles);
  2260. $comments = $Comment->find('all', array(
  2261. 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
  2262. 'recursive' => -1,
  2263. 'order' => array('Comment.id' => 'ASC')
  2264. ));
  2265. $expected = array(
  2266. array('Comment' => array(
  2267. 'id' => 1,
  2268. 'article_id' => 1,
  2269. 'user_id' => 2,
  2270. 'comment' => 'First Comment for First Article',
  2271. 'published' => 'Y'
  2272. )),
  2273. array('Comment' => array(
  2274. 'id' => 2,
  2275. 'article_id' => 1,
  2276. 'user_id' => 4,
  2277. 'comment' => 'Second Comment for First Article',
  2278. 'published' => 'Y'
  2279. )),
  2280. array('Comment' => array(
  2281. 'id' => 3,
  2282. 'article_id' => 1,
  2283. 'user_id' => 1,
  2284. 'comment' => 'Third Comment for First Article',
  2285. 'published' => 'Y'
  2286. )),
  2287. array('Comment' => array(
  2288. 'id' => 4,
  2289. 'article_id' => 1,
  2290. 'user_id' => 1,
  2291. 'comment' => 'Fourth Comment for First Article',
  2292. 'published' => 'N'
  2293. )),
  2294. array('Comment' => array(
  2295. 'id' => 5,
  2296. 'article_id' => 2,
  2297. 'user_id' => 1,
  2298. 'comment' => 'First Comment for Second Article',
  2299. 'published' => 'Y'
  2300. )),
  2301. array('Comment' => array(
  2302. 'id' => 6,
  2303. 'article_id' => 2,
  2304. 'user_id' => 2,
  2305. 'comment' => 'Second Comment for Second Article',
  2306. 'published' => 'Y'
  2307. )));
  2308. $this->assertEquals($expected, $comments);
  2309. $data = array(
  2310. 'Comment' => array(
  2311. 'article_id' => 2,
  2312. 'user_id' => 4,
  2313. 'comment' => 'Brand New Comment',
  2314. 'published' => 'N'
  2315. ),
  2316. 'Article' => array(
  2317. 'id' => 2,
  2318. 'title' => 'Second Article Modified'
  2319. ));
  2320. $result = $Comment->create($data);
  2321. $this->assertFalse(empty($result));
  2322. $result = $Comment->save();
  2323. $this->assertFalse(empty($result));
  2324. $articles = $Article->find('all', array(
  2325. 'fields' => array('id', 'title'),
  2326. 'recursive' => -1,
  2327. 'order' => array('Article.id' => 'ASC')
  2328. ));
  2329. $expected = array(
  2330. array('Article' => array(
  2331. 'id' => 1,
  2332. 'title' => 'First Article'
  2333. )),
  2334. array('Article' => array(
  2335. 'id' => 2,
  2336. 'title' => 'Second Article'
  2337. )),
  2338. array('Article' => array(
  2339. 'id' => 3,
  2340. 'title' => 'Third Article'
  2341. )));
  2342. $this->assertEquals($expected, $articles);
  2343. $comments = $Comment->find('all', array(
  2344. 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
  2345. 'recursive' => -1,
  2346. 'order' => array('Comment.id' => 'ASC')
  2347. ));
  2348. $expected = array(
  2349. array('Comment' => array(
  2350. 'id' => 1,
  2351. 'article_id' => 1,
  2352. 'user_id' => 2,
  2353. 'comment' => 'First Comment for First Article',
  2354. 'published' => 'Y'
  2355. )),
  2356. array('Comment' => array(
  2357. 'id' => 2,
  2358. 'article_id' => 1,
  2359. 'user_id' => 4,
  2360. 'comment' => 'Second Comment for First Article',
  2361. 'published' => 'Y'
  2362. )),
  2363. array('Comment' => array(
  2364. 'id' => 3,
  2365. 'article_id' => 1,
  2366. 'user_id' => 1,
  2367. 'comment' => 'Third Comment for First Article',
  2368. 'published' => 'Y'
  2369. )),
  2370. array('Comment' => array(
  2371. 'id' => 4,
  2372. 'article_id' => 1,
  2373. 'user_id' => 1,
  2374. 'comment' => 'Fourth Comment for First Article',
  2375. 'published' => 'N'
  2376. )),
  2377. array('Comment' => array(
  2378. 'id' => 5,
  2379. 'article_id' => 2,
  2380. 'user_id' => 1,
  2381. 'comment' => 'First Comment for Second Article',
  2382. 'published' => 'Y'
  2383. )),
  2384. array('Comment' => array(
  2385. 'id' => 6,
  2386. 'article_id' => 2,
  2387. 'user_id' => 2, 'comment' =>
  2388. 'Second Comment for Second Article',
  2389. 'published' => 'Y'
  2390. )),
  2391. array('Comment' => array(
  2392. 'id' => 7,
  2393. 'article_id' => 2,
  2394. 'user_id' => 4,
  2395. 'comment' => 'Brand New Comment',
  2396. 'published' => 'N'
  2397. )));
  2398. $this->assertEquals($expected, $comments);
  2399. }
  2400. /**
  2401. * testCreationWithMultipleDataSameModel method
  2402. *
  2403. * @return void
  2404. */
  2405. public function testCreationWithMultipleDataSameModel() {
  2406. $this->loadFixtures('Article');
  2407. $Article = new Article();
  2408. $result = $Article->field('title', array('id' => 1));
  2409. $this->assertEquals('First Article', $result);
  2410. $data = array(
  2411. 'Article' => array(
  2412. 'user_id' => 2,
  2413. 'title' => 'Brand New Article',
  2414. 'body' => 'Brand New Article Body',
  2415. 'published' => 'Y'
  2416. ),
  2417. 'SecondaryArticle' => array(
  2418. 'id' => 1
  2419. ));
  2420. $Article->create();
  2421. $result = $Article->save($data);
  2422. $this->assertFalse(empty($result));
  2423. $result = $Article->getInsertID();
  2424. $this->assertTrue(!empty($result));
  2425. $result = $Article->field('title', array('id' => 1));
  2426. $this->assertEquals('First Article', $result);
  2427. $articles = $Article->find('all', array(
  2428. 'fields' => array('id', 'title'),
  2429. 'recursive' => -1,
  2430. 'order' => array('Article.id' => 'ASC')
  2431. ));
  2432. $expected = array(
  2433. array('Article' => array(
  2434. 'id' => 1,
  2435. 'title' => 'First Article'
  2436. )),
  2437. array('Article' => array(
  2438. 'id' => 2,
  2439. 'title' => 'Second Article'
  2440. )),
  2441. array('Article' => array(
  2442. 'id' => 3,
  2443. 'title' => 'Third Article'
  2444. )),
  2445. array('Article' => array(
  2446. 'id' => 4,
  2447. 'title' => 'Brand New Article'
  2448. )));
  2449. $this->assertEquals($expected, $articles);
  2450. }
  2451. /**
  2452. * testCreationWithMultipleDataSameModelManualInstances method
  2453. *
  2454. * @return void
  2455. */
  2456. public function testCreationWithMultipleDataSameModelManualInstances() {
  2457. $this->loadFixtures('PrimaryModel');
  2458. $Primary = new PrimaryModel();
  2459. $result = $Primary->field('primary_name', array('id' => 1));
  2460. $this->assertEquals('Primary Name Existing', $result);
  2461. $data = array(
  2462. 'PrimaryModel' => array(
  2463. 'primary_name' => 'Primary Name New'
  2464. ),
  2465. 'SecondaryModel' => array(
  2466. 'id' => array(1)
  2467. ));
  2468. $Primary->create();
  2469. $result = $Primary->save($data);
  2470. $this->assertFalse(empty($result));
  2471. $result = $Primary->field('primary_name', array('id' => 1));
  2472. $this->assertEquals('Primary Name Existing', $result);
  2473. $result = $Primary->getInsertID();
  2474. $this->assertTrue(!empty($result));
  2475. $result = $Primary->field('primary_name', array('id' => $result));
  2476. $this->assertEquals('Primary Name New', $result);
  2477. $result = $Primary->find('count');
  2478. $this->assertEquals(2, $result);
  2479. }
  2480. /**
  2481. * testRecordExists method
  2482. *
  2483. * @return void
  2484. */
  2485. public function testRecordExists() {
  2486. $this->loadFixtures('User');
  2487. $TestModel = new User();
  2488. $this->assertFalse($TestModel->exists());
  2489. $TestModel->read(null, 1);
  2490. $this->assertTrue($TestModel->exists());
  2491. $TestModel->create();
  2492. $this->assertFalse($TestModel->exists());
  2493. $TestModel->id = 4;
  2494. $this->assertTrue($TestModel->exists());
  2495. $TestModel = new TheVoid();
  2496. $this->assertFalse($TestModel->exists());
  2497. }
  2498. /**
  2499. * testRecordExistsMissingTable method
  2500. *
  2501. * @expectedException PDOException
  2502. * @return void
  2503. */
  2504. public function testRecordExistsMissingTable() {
  2505. $TestModel = new TheVoid();
  2506. $TestModel->id = 5;
  2507. $TestModel->exists();
  2508. }
  2509. /**
  2510. * testUpdateExisting method
  2511. *
  2512. * @return void
  2513. */
  2514. public function testUpdateExisting() {
  2515. $this->loadFixtures('User', 'Article', 'Comment');
  2516. $TestModel = new User();
  2517. $TestModel->create();
  2518. $TestModel->save(array(
  2519. 'User' => array(
  2520. 'user' => 'some user',
  2521. 'password' => 'some password'
  2522. )));
  2523. $this->assertTrue(is_int($TestModel->id) || ((int)$TestModel->id === 5));
  2524. $id = $TestModel->id;
  2525. $TestModel->save(array(
  2526. 'User' => array(
  2527. 'user' => 'updated user'
  2528. )));
  2529. $this->assertEquals($id, $TestModel->id);
  2530. $result = $TestModel->findById($id);
  2531. $this->assertEquals('updated user', $result['User']['user']);
  2532. $this->assertEquals('some password', $result['User']['password']);
  2533. $Article = new Article();
  2534. $Comment = new Comment();
  2535. $data = array(
  2536. 'Comment' => array(
  2537. 'id' => 1,
  2538. 'comment' => 'First Comment for First Article'
  2539. ),
  2540. 'Article' => array(
  2541. 'id' => 2,
  2542. 'title' => 'Second Article'
  2543. ));
  2544. $result = $Article->save($data);
  2545. $this->assertFalse(empty($result));
  2546. $result = $Comment->save($data);
  2547. $this->assertFalse(empty($result));
  2548. }
  2549. /**
  2550. * test updating records and saving blank values.
  2551. *
  2552. * @return void
  2553. */
  2554. public function testUpdateSavingBlankValues() {
  2555. $this->loadFixtures('Article');
  2556. $Article = new Article();
  2557. $Article->validate = array();
  2558. $Article->create();
  2559. $result = $Article->save(array(
  2560. 'id' => 1,
  2561. 'title' => '',
  2562. 'body' => ''
  2563. ));
  2564. $this->assertTrue((bool)$result);
  2565. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2566. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2567. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2568. }
  2569. /**
  2570. * testUpdateMultiple method
  2571. *
  2572. * @return void
  2573. */
  2574. public function testUpdateMultiple() {
  2575. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2576. $TestModel = new Comment();
  2577. $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2578. $expected = array('2', '4', '1', '1', '1', '2');
  2579. $this->assertEquals($expected, $result);
  2580. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2581. $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2582. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2583. $this->assertEquals($expected, $result);
  2584. $result = $TestModel->updateAll(
  2585. array('Comment.comment' => "'Updated today'"),
  2586. array('Comment.user_id' => 5)
  2587. );
  2588. $this->assertFalse(empty($result));
  2589. $result = Hash::extract(
  2590. $TestModel->find('all', array(
  2591. 'conditions' => array(
  2592. 'Comment.user_id' => 5
  2593. ))),
  2594. '{n}.Comment.comment'
  2595. );
  2596. $expected = array_fill(0, 2, 'Updated today');
  2597. $this->assertEquals($expected, $result);
  2598. }
  2599. /**
  2600. * testHabtmUuidWithUuidId method
  2601. *
  2602. * @return void
  2603. */
  2604. public function testHabtmUuidWithUuidId() {
  2605. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2606. $TestModel = new Uuidportfolio();
  2607. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2608. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2609. $TestModel->create($data);
  2610. $TestModel->save();
  2611. $id = $TestModel->id;
  2612. $result = $TestModel->read(null, $id);
  2613. $this->assertEquals(1, count($result['Uuiditem']));
  2614. $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
  2615. }
  2616. /**
  2617. * test HABTM saving when join table has no primary key and only 2 columns.
  2618. *
  2619. * @return void
  2620. */
  2621. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2622. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2623. $Fruit = new Fruit();
  2624. $Fruit->FruitsUuidTag->order = null;
  2625. $data = array(
  2626. 'Fruit' => array(
  2627. 'color' => 'Red',
  2628. 'shape' => 'Heart-shaped',
  2629. 'taste' => 'sweet',
  2630. 'name' => 'Strawberry',
  2631. ),
  2632. 'UuidTag' => array(
  2633. 'UuidTag' => array(
  2634. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2635. )
  2636. )
  2637. );
  2638. $result = $Fruit->save($data);
  2639. $this->assertFalse(empty($result));
  2640. }
  2641. /**
  2642. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2643. *
  2644. * @return void
  2645. */
  2646. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2647. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2648. $Fruit = new FruitNoWith();
  2649. $data = array(
  2650. 'Fruit' => array(
  2651. 'color' => 'Red',
  2652. 'shape' => 'Heart-shaped',
  2653. 'taste' => 'sweet',
  2654. 'name' => 'Strawberry',
  2655. ),
  2656. 'UuidTag' => array(
  2657. 'UuidTag' => array(
  2658. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2659. )
  2660. )
  2661. );
  2662. $result = $Fruit->save($data);
  2663. $this->assertFalse(empty($result));
  2664. }
  2665. /**
  2666. * testHabtmUuidWithNumericId method
  2667. *
  2668. * @return void
  2669. */
  2670. public function testHabtmUuidWithNumericId() {
  2671. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2672. $TestModel = new Uuiditem();
  2673. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2674. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2675. $TestModel->create($data);
  2676. $TestModel->save();
  2677. $id = $TestModel->id;
  2678. $result = $TestModel->read(null, $id);
  2679. $this->assertEquals(1, count($result['Uuidportfolio']));
  2680. }
  2681. /**
  2682. * testSaveMultipleHabtm method
  2683. *
  2684. * @return void
  2685. */
  2686. public function testSaveMultipleHabtm() {
  2687. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2688. $TestModel = new JoinA();
  2689. $result = $TestModel->findById(1);
  2690. $expected = array(
  2691. 'JoinA' => array(
  2692. 'id' => 1,
  2693. 'name' => 'Join A 1',
  2694. 'body' => 'Join A 1 Body',
  2695. 'created' => '2008-01-03 10:54:23',
  2696. 'updated' => '2008-01-03 10:54:23'
  2697. ),
  2698. 'JoinB' => array(
  2699. 0 => array(
  2700. 'id' => 2,
  2701. 'name' => 'Join B 2',
  2702. 'created' => '2008-01-03 10:55:02',
  2703. 'updated' => '2008-01-03 10:55:02',
  2704. 'JoinAsJoinB' => array(
  2705. 'id' => 1,
  2706. 'join_a_id' => 1,
  2707. 'join_b_id' => 2,
  2708. 'other' => 'Data for Join A 1 Join B 2',
  2709. 'created' => '2008-01-03 10:56:33',
  2710. 'updated' => '2008-01-03 10:56:33'
  2711. ))),
  2712. 'JoinC' => array(
  2713. 0 => array(
  2714. 'id' => 2,
  2715. 'name' => 'Join C 2',
  2716. 'created' => '2008-01-03 10:56:12',
  2717. 'updated' => '2008-01-03 10:56:12',
  2718. 'JoinAsJoinC' => array(
  2719. 'id' => 1,
  2720. 'join_a_id' => 1,
  2721. 'join_c_id' => 2,
  2722. 'other' => 'Data for Join A 1 Join C 2',
  2723. 'created' => '2008-01-03 10:57:22',
  2724. 'updated' => '2008-01-03 10:57:22'
  2725. ))));
  2726. $this->assertEquals($expected, $result);
  2727. $TestModel->id = 1;
  2728. $data = array(
  2729. 'JoinA' => array(
  2730. 'id' => '1',
  2731. 'name' => 'New name for Join A 1',
  2732. 'updated' => self::date()
  2733. ),
  2734. 'JoinB' => array(
  2735. array(
  2736. 'id' => 1,
  2737. 'join_b_id' => 2,
  2738. 'other' => 'New data for Join A 1 Join B 2',
  2739. 'created' => self::date(),
  2740. 'updated' => self::date()
  2741. )),
  2742. 'JoinC' => array(
  2743. array(
  2744. 'id' => 1,
  2745. 'join_c_id' => 2,
  2746. 'other' => 'New data for Join A 1 Join C 2',
  2747. 'created' => self::date(),
  2748. 'updated' => self::date()
  2749. )));
  2750. $TestModel->set($data);
  2751. $TestModel->save();
  2752. $result = $TestModel->findById(1);
  2753. $expected = array(
  2754. 'JoinA' => array(
  2755. 'id' => 1,
  2756. 'name' => 'New name for Join A 1',
  2757. 'body' => 'Join A 1 Body',
  2758. 'created' => '2008-01-03 10:54:23',
  2759. 'updated' => self::date()
  2760. ),
  2761. 'JoinB' => array(
  2762. 0 => array(
  2763. 'id' => 2,
  2764. 'name' => 'Join B 2',
  2765. 'created' => '2008-01-03 10:55:02',
  2766. 'updated' => '2008-01-03 10:55:02',
  2767. 'JoinAsJoinB' => array(
  2768. 'id' => 1,
  2769. 'join_a_id' => 1,
  2770. 'join_b_id' => 2,
  2771. 'other' => 'New data for Join A 1 Join B 2',
  2772. 'created' => self::date(),
  2773. 'updated' => self::date()
  2774. ))),
  2775. 'JoinC' => array(
  2776. 0 => array(
  2777. 'id' => 2,
  2778. 'name' => 'Join C 2',
  2779. 'created' => '2008-01-03 10:56:12',
  2780. 'updated' => '2008-01-03 10:56:12',
  2781. 'JoinAsJoinC' => array(
  2782. 'id' => 1,
  2783. 'join_a_id' => 1,
  2784. 'join_c_id' => 2,
  2785. 'other' => 'New data for Join A 1 Join C 2',
  2786. 'created' => self::date(),
  2787. 'updated' => self::date()
  2788. ))));
  2789. $this->assertEquals($expected, $result);
  2790. }
  2791. /**
  2792. * testSaveAll method
  2793. *
  2794. * @return void
  2795. */
  2796. public function testSaveAll() {
  2797. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2798. $TestModel = new Post();
  2799. $result = $TestModel->find('all');
  2800. $this->assertEquals(3, count($result));
  2801. $this->assertFalse(isset($result[3]));
  2802. $TestModel->saveAll(array(
  2803. 'Post' => array(
  2804. 'title' => 'Post with Author',
  2805. 'body' => 'This post will be saved with an author'
  2806. ),
  2807. 'Author' => array(
  2808. 'user' => 'bob',
  2809. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2810. )));
  2811. $result = $TestModel->find('all');
  2812. $expected = array(
  2813. 'Post' => array(
  2814. 'id' => '4',
  2815. 'author_id' => '5',
  2816. 'title' => 'Post with Author',
  2817. 'body' => 'This post will be saved with an author',
  2818. 'published' => 'N'
  2819. ),
  2820. 'Author' => array(
  2821. 'id' => '5',
  2822. 'user' => 'bob',
  2823. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2824. 'test' => 'working'
  2825. ));
  2826. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  2827. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  2828. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  2829. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  2830. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2831. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2832. $this->assertEquals($expected, $result[3]);
  2833. $this->assertEquals(4, count($result));
  2834. $TestModel->deleteAll(true);
  2835. $this->assertEquals(array(), $TestModel->find('all'));
  2836. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2837. $this->db->truncate($TestModel);
  2838. $TestModel->saveAll(array(
  2839. array(
  2840. 'title' => 'Multi-record post 1',
  2841. 'body' => 'First multi-record post',
  2842. 'author_id' => 2
  2843. ),
  2844. array(
  2845. 'title' => 'Multi-record post 2',
  2846. 'body' => 'Second multi-record post',
  2847. 'author_id' => 2
  2848. )));
  2849. $result = $TestModel->find('all', array(
  2850. 'recursive' => -1,
  2851. 'order' => 'Post.id ASC'
  2852. ));
  2853. $expected = array(
  2854. array(
  2855. 'Post' => array(
  2856. 'id' => '1',
  2857. 'author_id' => '2',
  2858. 'title' => 'Multi-record post 1',
  2859. 'body' => 'First multi-record post',
  2860. 'published' => 'N'
  2861. )),
  2862. array(
  2863. 'Post' => array(
  2864. 'id' => '2',
  2865. 'author_id' => '2',
  2866. 'title' => 'Multi-record post 2',
  2867. 'body' => 'Second multi-record post',
  2868. 'published' => 'N'
  2869. )));
  2870. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  2871. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  2872. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  2873. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  2874. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2875. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2876. $this->assertEquals($expected, $result);
  2877. $TestModel = new Comment();
  2878. $result = $TestModel->saveAll(array(
  2879. 'Comment' => array(
  2880. 'article_id' => 2,
  2881. 'user_id' => 2,
  2882. 'comment' => 'New comment with attachment',
  2883. 'published' => 'Y'
  2884. ),
  2885. 'Attachment' => array(
  2886. 'attachment' => 'some_file.tgz'
  2887. )));
  2888. $this->assertFalse(empty($result));
  2889. $result = $TestModel->find('all');
  2890. $expected = array(
  2891. 'id' => '7',
  2892. 'article_id' => '2',
  2893. 'user_id' => '2',
  2894. 'comment' => 'New comment with attachment',
  2895. 'published' => 'Y'
  2896. );
  2897. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  2898. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  2899. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2900. $this->assertEquals($expected, $result[6]['Comment']);
  2901. $expected = array(
  2902. 'id' => '2',
  2903. 'comment_id' => '7',
  2904. 'attachment' => 'some_file.tgz'
  2905. );
  2906. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  2907. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  2908. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2909. $this->assertEquals($expected, $result[6]['Attachment']);
  2910. }
  2911. /**
  2912. * Test SaveAll with Habtm relations
  2913. *
  2914. * @return void
  2915. */
  2916. public function testSaveAllHabtm() {
  2917. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2918. $data = array(
  2919. 'Article' => array(
  2920. 'user_id' => 1,
  2921. 'title' => 'Article Has and belongs to Many Tags'
  2922. ),
  2923. 'Tag' => array(
  2924. 'Tag' => array(1, 2)
  2925. ),
  2926. 'Comment' => array(
  2927. array(
  2928. 'comment' => 'Article comment',
  2929. 'user_id' => 1
  2930. )));
  2931. $Article = new Article();
  2932. $result = $Article->saveAll($data);
  2933. $this->assertFalse(empty($result));
  2934. $result = $Article->read();
  2935. $this->assertEquals(2, count($result['Tag']));
  2936. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  2937. $this->assertEquals(1, count($result['Comment']));
  2938. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  2939. }
  2940. /**
  2941. * Test SaveAll with Habtm relations and extra join table fields
  2942. *
  2943. * @return void
  2944. */
  2945. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2946. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2947. $data = array(
  2948. 'Something' => array(
  2949. 'id' => 4,
  2950. 'title' => 'Extra Fields',
  2951. 'body' => 'Extra Fields Body',
  2952. 'published' => '1'
  2953. ),
  2954. 'SomethingElse' => array(
  2955. array('something_else_id' => 1, 'doomed' => '1'),
  2956. array('something_else_id' => 2, 'doomed' => '0'),
  2957. array('something_else_id' => 3, 'doomed' => '1')
  2958. )
  2959. );
  2960. $Something = new Something();
  2961. $result = $Something->saveAll($data);
  2962. $this->assertFalse(empty($result));
  2963. $result = $Something->read();
  2964. $this->assertEquals(3, count($result['SomethingElse']));
  2965. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2966. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2967. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2968. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2969. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2970. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2971. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2972. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2973. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2974. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2975. }
  2976. /**
  2977. * testSaveAllHasOne method
  2978. *
  2979. * @return void
  2980. */
  2981. public function testSaveAllHasOne() {
  2982. $model = new Comment();
  2983. $model->deleteAll(true);
  2984. $this->assertEquals(array(), $model->find('all'));
  2985. $model->Attachment->deleteAll(true);
  2986. $this->assertEquals(array(), $model->Attachment->find('all'));
  2987. $this->assertTrue($model->saveAll(array(
  2988. 'Comment' => array(
  2989. 'comment' => 'Comment with attachment',
  2990. 'article_id' => 1,
  2991. 'user_id' => 1
  2992. ),
  2993. 'Attachment' => array(
  2994. 'attachment' => 'some_file.zip'
  2995. ))));
  2996. $result = $model->find('all', array('fields' => array(
  2997. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2998. 'Attachment.comment_id', 'Attachment.attachment'
  2999. )));
  3000. $expected = array(array(
  3001. 'Comment' => array(
  3002. 'id' => '1',
  3003. 'comment' => 'Comment with attachment'
  3004. ),
  3005. 'Attachment' => array(
  3006. 'id' => '1',
  3007. 'comment_id' => '1',
  3008. 'attachment' => 'some_file.zip'
  3009. )));
  3010. $this->assertEquals($expected, $result);
  3011. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  3012. $data = array(
  3013. 'Comment' => array(
  3014. 'comment' => 'Comment with attachment',
  3015. 'article_id' => 1,
  3016. 'user_id' => 1
  3017. ),
  3018. 'Attachment' => array(
  3019. 'attachment' => 'some_file.zip'
  3020. ));
  3021. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  3022. }
  3023. /**
  3024. * testSaveAllBelongsTo method
  3025. *
  3026. * @return void
  3027. */
  3028. public function testSaveAllBelongsTo() {
  3029. $model = new Comment();
  3030. $model->deleteAll(true);
  3031. $this->assertEquals(array(), $model->find('all'));
  3032. $model->Article->deleteAll(true);
  3033. $this->assertEquals(array(), $model->Article->find('all'));
  3034. $this->assertTrue($model->saveAll(array(
  3035. 'Comment' => array(
  3036. 'comment' => 'Article comment',
  3037. 'article_id' => 1,
  3038. 'user_id' => 1
  3039. ),
  3040. 'Article' => array(
  3041. 'title' => 'Model Associations 101',
  3042. 'user_id' => 1
  3043. ))));
  3044. $result = $model->find('all', array('fields' => array(
  3045. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  3046. )));
  3047. $expected = array(array(
  3048. 'Comment' => array(
  3049. 'id' => '1',
  3050. 'article_id' => '1',
  3051. 'comment' => 'Article comment'
  3052. ),
  3053. 'Article' => array(
  3054. 'id' => '1',
  3055. 'title' => 'Model Associations 101'
  3056. )));
  3057. $this->assertEquals($expected, $result);
  3058. }
  3059. /**
  3060. * testSaveAllHasOneValidation method
  3061. *
  3062. * @return void
  3063. */
  3064. public function testSaveAllHasOneValidation() {
  3065. $model = new Comment();
  3066. $model->deleteAll(true);
  3067. $this->assertEquals(array(), $model->find('all'));
  3068. $model->Attachment->deleteAll(true);
  3069. $this->assertEquals(array(), $model->Attachment->find('all'));
  3070. $model->validate = array('comment' => 'notEmpty');
  3071. $model->Attachment->validate = array('attachment' => 'notEmpty');
  3072. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  3073. $result = $model->saveAll(
  3074. array(
  3075. 'Comment' => array(
  3076. 'comment' => '',
  3077. 'article_id' => 1,
  3078. 'user_id' => 1
  3079. ),
  3080. 'Attachment' => array('attachment' => '')
  3081. ),
  3082. array('validate' => 'first')
  3083. );
  3084. $this->assertEquals(false, $result);
  3085. $expected = array(
  3086. 'comment' => array('This field cannot be left blank'),
  3087. 'Attachment' => array(
  3088. 'attachment' => array('This field cannot be left blank')
  3089. )
  3090. );
  3091. $this->assertEquals($expected, $model->validationErrors);
  3092. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  3093. }
  3094. /**
  3095. * testSaveAllAtomic method
  3096. *
  3097. * @return void
  3098. */
  3099. public function testSaveAllAtomic() {
  3100. $this->loadFixtures('Article', 'User', 'Comment');
  3101. $TestModel = new Article();
  3102. $result = $TestModel->saveAll(array(
  3103. 'Article' => array(
  3104. 'title' => 'Post with Author',
  3105. 'body' => 'This post will be saved with an author',
  3106. 'user_id' => 2
  3107. ),
  3108. 'Comment' => array(
  3109. array('comment' => 'First new comment', 'user_id' => 2))
  3110. ), array('atomic' => false));
  3111. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  3112. $result = $TestModel->saveAll(array(
  3113. array(
  3114. 'id' => '1',
  3115. 'title' => 'Baleeted First Post',
  3116. 'body' => 'Baleeted!',
  3117. 'published' => 'N'
  3118. ),
  3119. array(
  3120. 'id' => '2',
  3121. 'title' => 'Just update the title'
  3122. ),
  3123. array(
  3124. 'title' => 'Creating a fourth post',
  3125. 'body' => 'Fourth post body',
  3126. 'user_id' => 2
  3127. )
  3128. ), array('atomic' => false));
  3129. $this->assertSame($result, array(true, true, true));
  3130. $result = $TestModel->saveAll(array(
  3131. 'Article' => array('id' => 2),
  3132. 'Comment' => array(
  3133. array(
  3134. 'comment' => 'First new comment',
  3135. 'published' => 'Y',
  3136. 'user_id' => 1
  3137. ),
  3138. array(
  3139. 'comment' => 'Second new comment',
  3140. 'published' => 'Y',
  3141. 'user_id' => 2
  3142. ))
  3143. ), array('validate' => true, 'atomic' => false));
  3144. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  3145. $TestModel->validate = array(
  3146. 'title' => 'notEmpty',
  3147. 'author_id' => 'numeric'
  3148. );
  3149. $result = $TestModel->saveAll(array(
  3150. array(
  3151. 'id' => '1',
  3152. 'title' => 'Un-Baleeted First Post',
  3153. 'body' => 'Not Baleeted!',
  3154. 'published' => 'Y'
  3155. ),
  3156. array(
  3157. 'id' => '2',
  3158. 'title' => '',
  3159. 'body' => 'Trying to get away with an empty title'
  3160. )
  3161. ), array('validate' => true, 'atomic' => false));
  3162. $this->assertSame(array(true, false), $result);
  3163. }
  3164. /**
  3165. * testSaveAllDeepAssociated method
  3166. *
  3167. * @return void
  3168. */
  3169. public function testSaveAllDeepAssociated() {
  3170. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3171. $TestModel = new Article();
  3172. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3173. $TestModel->hasAndBelongsToMany = array();
  3174. $result = $TestModel->saveAll(array(
  3175. 'Article' => array('id' => 2),
  3176. 'Comment' => array(
  3177. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3178. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3179. )
  3180. ), array('deep' => true));
  3181. $this->assertTrue($result);
  3182. $result = $TestModel->findById(2);
  3183. $expected = array(
  3184. 'First Comment for Second Article',
  3185. 'Second Comment for Second Article',
  3186. 'First new comment',
  3187. 'Second new comment'
  3188. );
  3189. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3190. $this->assertEquals($expected, $result);
  3191. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  3192. $this->assertEquals(5, $result);
  3193. $result = $TestModel->saveAll(array(
  3194. 'Article' => array('id' => 2),
  3195. 'Comment' => array(
  3196. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3197. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3198. )
  3199. ), array('deep' => true));
  3200. $this->assertTrue($result);
  3201. $result = $TestModel->findById(2);
  3202. $expected = array(
  3203. 'First Comment for Second Article',
  3204. 'Second Comment for Second Article',
  3205. 'First new comment',
  3206. 'Second new comment',
  3207. 'Third new comment',
  3208. 'Fourth new comment'
  3209. );
  3210. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3211. $this->assertEquals($expected, $result);
  3212. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  3213. $this->assertEquals(2, $result);
  3214. $data = array(
  3215. 'Attachment' => array(
  3216. 'attachment' => 'deepsave insert',
  3217. ),
  3218. 'Comment' => array(
  3219. 'comment' => 'First comment deepsave insert',
  3220. 'published' => 'Y',
  3221. 'user_id' => 5,
  3222. 'Article' => array(
  3223. 'title' => 'First Article deepsave insert',
  3224. 'body' => 'First Article Body deepsave insert',
  3225. 'User' => array(
  3226. 'user' => '',
  3227. 'password' => 'magic'
  3228. ),
  3229. ),
  3230. )
  3231. );
  3232. $TestModel->Comment->Attachment->create();
  3233. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  3234. $this->assertFalse($result);
  3235. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  3236. $this->assertEquals($expected, $TestModel->validationErrors);
  3237. $data['Comment']['Article']['User']['user'] = 'deepsave';
  3238. $TestModel->Comment->Attachment->create();
  3239. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  3240. $this->assertTrue($result);
  3241. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3242. $expected = array(
  3243. 'Attachment' => array(
  3244. 'id' => '3',
  3245. 'comment_id' => '11',
  3246. 'attachment' => 'deepsave insert',
  3247. ),
  3248. 'Comment' => array(
  3249. 'id' => '11',
  3250. 'article_id' => '4',
  3251. 'user_id' => '5',
  3252. 'comment' => 'First comment deepsave insert',
  3253. 'published' => 'Y',
  3254. )
  3255. );
  3256. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3257. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3258. unset($result['Comment']['created'], $result['Comment']['updated']);
  3259. $this->assertEquals($expected['Comment'], $result['Comment']);
  3260. $result = $TestModel->findById($result['Comment']['article_id']);
  3261. $expected = array(
  3262. 'Article' => array(
  3263. 'id' => '4',
  3264. 'user_id' => '6',
  3265. 'title' => 'First Article deepsave insert',
  3266. 'body' => 'First Article Body deepsave insert',
  3267. 'published' => 'N',
  3268. ),
  3269. 'User' => array(
  3270. 'id' => '6',
  3271. 'user' => 'deepsave',
  3272. 'password' => 'magic',
  3273. ),
  3274. 'Comment' => array(
  3275. array(
  3276. 'id' => '11',
  3277. 'article_id' => '4',
  3278. 'user_id' => '5',
  3279. 'comment' => 'First comment deepsave insert',
  3280. 'published' => 'Y',
  3281. )
  3282. )
  3283. );
  3284. unset(
  3285. $result['Article']['created'], $result['Article']['updated'],
  3286. $result['User']['created'], $result['User']['updated'],
  3287. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  3288. );
  3289. $this->assertEquals($expected, $result);
  3290. }
  3291. /**
  3292. * testSaveAllDeepMany
  3293. * tests the validate methods with deeper recursive data
  3294. *
  3295. * @return void
  3296. */
  3297. public function testSaveAllDeepMany() {
  3298. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3299. $TestModel = new Article();
  3300. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3301. $TestModel->hasAndBelongsToMany = array();
  3302. $data = array(
  3303. array(
  3304. 'Article' => array('id' => 1),
  3305. 'Comment' => array(
  3306. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  3307. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3308. )
  3309. ),
  3310. array(
  3311. 'Article' => array('id' => 2),
  3312. 'Comment' => array(
  3313. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  3314. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  3315. )
  3316. )
  3317. );
  3318. $result = $TestModel->saveAll($data, array('deep' => true));
  3319. $this->assertTrue($result);
  3320. $data = array(
  3321. array(
  3322. 'id' => 1, 'body' => '',
  3323. 'Comment' => array(
  3324. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3325. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3326. )
  3327. ),
  3328. array(
  3329. 'Article' => array('id' => 2),
  3330. 'Comment' => array(
  3331. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3332. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3333. )
  3334. )
  3335. );
  3336. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3337. $result = $TestModel->saveAll($data, array('deep' => true));
  3338. $this->assertFalse($result);
  3339. $expected = array(
  3340. 0 => array(
  3341. 'body' => array('This field cannot be left blank'),
  3342. 'Comment' => array(
  3343. 0 => array(
  3344. 'comment' => array('This field cannot be left blank'),
  3345. 'User' => array(
  3346. 'user' => array('This field cannot be left blank')
  3347. )
  3348. )
  3349. )
  3350. ),
  3351. 1 => array(
  3352. 'Comment' => array(
  3353. 0 => array(
  3354. 'User' => array(
  3355. 'password' => array('This field cannot be left blank')
  3356. )
  3357. ),
  3358. 1 => array(
  3359. 'comment' => array('This field cannot be left blank')
  3360. )
  3361. )
  3362. )
  3363. );
  3364. $result = $TestModel->validationErrors;
  3365. $this->assertSame($expected, $result);
  3366. }
  3367. /**
  3368. * testSaveAllDeepValidateOnly
  3369. * tests the validate methods with deeper recursive data
  3370. *
  3371. * @return void
  3372. */
  3373. public function testSaveAllDeepValidateOnly() {
  3374. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3375. $TestModel = new Article();
  3376. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3377. $TestModel->hasAndBelongsToMany = array();
  3378. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3379. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3380. $result = $TestModel->saveAll(
  3381. array(
  3382. 'Article' => array('id' => 2),
  3383. 'Comment' => array(
  3384. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3385. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3386. )
  3387. ),
  3388. array('validate' => 'only', 'deep' => true)
  3389. );
  3390. $this->assertTrue($result);
  3391. $result = $TestModel->saveAll(
  3392. array(
  3393. 'Article' => array('id' => 2),
  3394. 'Comment' => array(
  3395. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3396. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3397. )
  3398. ),
  3399. array('validate' => 'only', 'deep' => true)
  3400. );
  3401. $this->assertFalse($result);
  3402. $result = $TestModel->saveAll(
  3403. array(
  3404. 'Article' => array('id' => 2),
  3405. 'Comment' => array(
  3406. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3407. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3408. )
  3409. ),
  3410. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3411. );
  3412. $expected = array(
  3413. 'Article' => true,
  3414. 'Comment' => array(
  3415. true,
  3416. true
  3417. )
  3418. );
  3419. $this->assertSame($expected, $result);
  3420. $result = $TestModel->saveAll(
  3421. array(
  3422. 'Article' => array('id' => 2),
  3423. 'Comment' => array(
  3424. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3425. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3426. )
  3427. ),
  3428. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3429. );
  3430. $expected = array(
  3431. 'Article' => true,
  3432. 'Comment' => array(
  3433. false,
  3434. true
  3435. )
  3436. );
  3437. $this->assertSame($expected, $result);
  3438. $result = $TestModel->saveAll(array(
  3439. 'Article' => array('id' => 2),
  3440. 'Comment' => array(
  3441. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3442. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3443. )
  3444. ),
  3445. array('validate' => 'only', 'deep' => true)
  3446. );
  3447. $this->assertTrue($result);
  3448. $result = $TestModel->saveAll(array(
  3449. 'Article' => array('id' => 2),
  3450. 'Comment' => array(
  3451. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3452. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3453. )
  3454. ),
  3455. array('validate' => 'only', 'deep' => true)
  3456. );
  3457. $this->assertFalse($result);
  3458. $result = $TestModel->saveAll(array(
  3459. 'Article' => array('id' => 2),
  3460. 'Comment' => array(
  3461. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3462. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  3463. )
  3464. ),
  3465. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3466. );
  3467. $expected = array(
  3468. 'Article' => true,
  3469. 'Comment' => array(
  3470. true,
  3471. true
  3472. )
  3473. );
  3474. $this->assertSame($expected, $result);
  3475. $result = $TestModel->saveAll(array(
  3476. 'Article' => array('id' => 2),
  3477. 'Comment' => array(
  3478. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3479. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3480. )
  3481. ),
  3482. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3483. );
  3484. $expected = array(
  3485. 'Article' => true,
  3486. 'Comment' => array(
  3487. true,
  3488. false
  3489. )
  3490. );
  3491. $this->assertSame($expected, $result);
  3492. $expected = array(
  3493. 'Comment' => array(
  3494. 1 => array(
  3495. 'Attachment' => array(
  3496. 'attachment' => array('This field cannot be left blank')
  3497. )
  3498. )
  3499. )
  3500. );
  3501. $result = $TestModel->validationErrors;
  3502. $this->assertSame($expected, $result);
  3503. $data = array(
  3504. 'Attachment' => array(
  3505. 'attachment' => 'deepsave insert',
  3506. ),
  3507. 'Comment' => array(
  3508. 'comment' => 'First comment deepsave insert',
  3509. 'published' => 'Y',
  3510. 'user_id' => 5,
  3511. 'Article' => array(
  3512. 'title' => 'First Article deepsave insert',
  3513. 'body' => 'First Article Body deepsave insert',
  3514. 'User' => array(
  3515. 'user' => 'deepsave',
  3516. 'password' => 'magic'
  3517. ),
  3518. ),
  3519. )
  3520. );
  3521. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3522. $this->assertTrue($result);
  3523. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3524. $expected = array(
  3525. 'Attachment' => true,
  3526. 'Comment' => true
  3527. );
  3528. $this->assertSame($expected, $result);
  3529. $data = array(
  3530. 'Attachment' => array(
  3531. 'attachment' => 'deepsave insert',
  3532. ),
  3533. 'Comment' => array(
  3534. 'comment' => 'First comment deepsave insert',
  3535. 'published' => 'Y',
  3536. 'user_id' => 5,
  3537. 'Article' => array(
  3538. 'title' => 'First Article deepsave insert',
  3539. 'body' => 'First Article Body deepsave insert',
  3540. 'User' => array(
  3541. 'user' => '',
  3542. 'password' => 'magic'
  3543. ),
  3544. ),
  3545. )
  3546. );
  3547. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3548. $this->assertFalse($result);
  3549. $result = $TestModel->Comment->Attachment->validationErrors;
  3550. $expected = array(
  3551. 'Comment' => array(
  3552. 'Article' => array(
  3553. 'User' => array(
  3554. 'user' => array('This field cannot be left blank')
  3555. )
  3556. )
  3557. )
  3558. );
  3559. $this->assertSame($expected, $result);
  3560. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3561. $expected = array(
  3562. 'Attachment' => true,
  3563. 'Comment' => false
  3564. );
  3565. $this->assertEquals($expected, $result);
  3566. $data['Comment']['Article']['body'] = '';
  3567. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3568. $this->assertFalse($result);
  3569. $result = $TestModel->Comment->Attachment->validationErrors;
  3570. $expected = array(
  3571. 'Comment' => array(
  3572. 'Article' => array(
  3573. 'body' => array('This field cannot be left blank'),
  3574. 'User' => array(
  3575. 'user' => array('This field cannot be left blank')
  3576. )
  3577. )
  3578. )
  3579. );
  3580. $this->assertSame($expected, $result);
  3581. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3582. $expected = array(
  3583. 'Attachment' => true,
  3584. 'Comment' => false
  3585. );
  3586. $this->assertEquals($expected, $result);
  3587. $data['Comment']['comment'] = '';
  3588. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3589. $this->assertFalse($result);
  3590. $result = $TestModel->Comment->Attachment->validationErrors;
  3591. $expected = array(
  3592. 'Comment' => array(
  3593. 'comment' => array('This field cannot be left blank'),
  3594. 'Article' => array(
  3595. 'body' => array('This field cannot be left blank'),
  3596. 'User' => array(
  3597. 'user' => array('This field cannot be left blank')
  3598. )
  3599. )
  3600. )
  3601. );
  3602. $this->assertSame($expected, $result);
  3603. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3604. $expected = array(
  3605. 'Attachment' => true,
  3606. 'Comment' => false
  3607. );
  3608. $this->assertEquals($expected, $result);
  3609. $data['Attachment']['attachment'] = '';
  3610. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3611. $this->assertFalse($result);
  3612. $result = $TestModel->Comment->Attachment->validationErrors;
  3613. $expected = array(
  3614. 'attachment' => array('This field cannot be left blank'),
  3615. 'Comment' => array(
  3616. 'comment' => array('This field cannot be left blank'),
  3617. 'Article' => array(
  3618. 'body' => array('This field cannot be left blank'),
  3619. 'User' => array(
  3620. 'user' => array('This field cannot be left blank')
  3621. )
  3622. )
  3623. )
  3624. );
  3625. $this->assertSame($expected, $result);
  3626. $result = $TestModel->Comment->validationErrors;
  3627. $expected = array(
  3628. 'comment' => array('This field cannot be left blank'),
  3629. 'Article' => array(
  3630. 'body' => array('This field cannot be left blank'),
  3631. 'User' => array(
  3632. 'user' => array('This field cannot be left blank')
  3633. )
  3634. )
  3635. );
  3636. $this->assertSame($expected, $result);
  3637. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3638. $expected = array(
  3639. 'Attachment' => false,
  3640. 'Comment' => false
  3641. );
  3642. $this->assertEquals($expected, $result);
  3643. }
  3644. /**
  3645. * testSaveAllNotDeepAssociated method
  3646. * test that only directly associated data gets saved
  3647. *
  3648. * @return void
  3649. */
  3650. public function testSaveAllNotDeepAssociated() {
  3651. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3652. $TestModel = new Article();
  3653. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3654. $TestModel->hasAndBelongsToMany = array();
  3655. $result = $TestModel->saveAll(array(
  3656. 'Article' => array('id' => 2),
  3657. 'Comment' => array(
  3658. array(
  3659. 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
  3660. 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
  3661. ),
  3662. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3663. )
  3664. ), array('deep' => false));
  3665. $this->assertTrue($result);
  3666. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  3667. $this->assertFalse($result);
  3668. $result = $TestModel->saveAll(array(
  3669. 'Article' => array('id' => 2),
  3670. 'Comment' => array(
  3671. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
  3672. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3673. )
  3674. ), array('deep' => false));
  3675. $this->assertTrue($result);
  3676. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  3677. $this->assertFalse($result);
  3678. $data = array(
  3679. 'Attachment' => array(
  3680. 'attachment' => 'deepsave insert',
  3681. ),
  3682. 'Comment' => array(
  3683. 'comment' => 'First comment deepsave insert',
  3684. 'published' => 'Y',
  3685. 'user_id' => 4,
  3686. 'article_id' => 1,
  3687. 'Article' => array(
  3688. 'title' => 'First Article deepsave insert',
  3689. 'body' => 'First Article Body deepsave insert',
  3690. 'User' => array(
  3691. 'user' => 'deepsave',
  3692. 'password' => 'magic'
  3693. ),
  3694. ),
  3695. )
  3696. );
  3697. $expected = $TestModel->User->find('count');
  3698. $TestModel->Comment->Attachment->create();
  3699. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
  3700. $this->assertTrue($result);
  3701. $result = $TestModel->User->find('count');
  3702. $this->assertEquals($expected, $result);
  3703. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3704. $expected = array(
  3705. 'Attachment' => array(
  3706. 'id' => '2',
  3707. 'comment_id' => '11',
  3708. 'attachment' => 'deepsave insert',
  3709. ),
  3710. 'Comment' => array(
  3711. 'id' => '11',
  3712. 'article_id' => 1,
  3713. 'user_id' => '4',
  3714. 'comment' => 'First comment deepsave insert',
  3715. 'published' => 'Y',
  3716. )
  3717. );
  3718. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3719. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3720. unset($result['Comment']['created'], $result['Comment']['updated']);
  3721. $this->assertEquals($expected['Comment'], $result['Comment']);
  3722. }
  3723. /**
  3724. * testSaveAllNotDeepMany
  3725. * tests the save methods to not save deeper recursive data
  3726. *
  3727. * @return void
  3728. */
  3729. public function testSaveAllNotDeepMany() {
  3730. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3731. $TestModel = new Article();
  3732. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3733. $TestModel->hasAndBelongsToMany = array();
  3734. $data = array(
  3735. array(
  3736. 'id' => 1,
  3737. 'body' => '',
  3738. 'Comment' => array(
  3739. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3740. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3741. )
  3742. ),
  3743. array(
  3744. 'Article' => array('id' => 2),
  3745. 'Comment' => array(
  3746. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3747. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3748. )
  3749. )
  3750. );
  3751. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3752. $result = $TestModel->saveAll($data, array('deep' => false));
  3753. $this->assertFalse($result);
  3754. $expected = array(
  3755. 0 => array(
  3756. 'body' => array('This field cannot be left blank')
  3757. )
  3758. );
  3759. $result = $TestModel->validationErrors;
  3760. $this->assertSame($expected, $result);
  3761. $data = array(
  3762. array(
  3763. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3764. 'Comment' => array(
  3765. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3766. )
  3767. ),
  3768. array(
  3769. 'Article' => array('id' => 2),
  3770. 'Comment' => array(
  3771. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3772. )
  3773. )
  3774. );
  3775. $result = $TestModel->saveAll($data, array('deep' => false));
  3776. $this->assertTrue($result);
  3777. }
  3778. /**
  3779. * testSaveAllNotDeepValidateOnly
  3780. * tests the validate methods to not validate deeper recursive data
  3781. *
  3782. * @return void
  3783. */
  3784. public function testSaveAllNotDeepValidateOnly() {
  3785. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3786. $TestModel = new Article();
  3787. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3788. $TestModel->hasAndBelongsToMany = array();
  3789. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3790. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3791. $result = $TestModel->saveAll(
  3792. array(
  3793. 'Article' => array('id' => 2, 'body' => ''),
  3794. 'Comment' => array(
  3795. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3796. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3797. )
  3798. ),
  3799. array('validate' => 'only', 'deep' => false)
  3800. );
  3801. $this->assertFalse($result);
  3802. $expected = array('body' => array('This field cannot be left blank'));
  3803. $result = $TestModel->validationErrors;
  3804. $this->assertSame($expected, $result);
  3805. $result = $TestModel->saveAll(
  3806. array(
  3807. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3808. 'Comment' => array(
  3809. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3810. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3811. )
  3812. ),
  3813. array('validate' => 'only', 'deep' => false)
  3814. );
  3815. $this->assertTrue($result);
  3816. $result = $TestModel->saveAll(
  3817. array(
  3818. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3819. 'Comment' => array(
  3820. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3821. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3822. )
  3823. ),
  3824. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3825. );
  3826. $expected = array(
  3827. 'Article' => true,
  3828. 'Comment' => array(
  3829. true,
  3830. true
  3831. )
  3832. );
  3833. $this->assertSame($expected, $result);
  3834. $result = $TestModel->saveAll(array(
  3835. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3836. 'Comment' => array(
  3837. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3838. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3839. )
  3840. ),
  3841. array('validate' => 'only', 'deep' => false)
  3842. );
  3843. $this->assertTrue($result);
  3844. $result = $TestModel->saveAll(array(
  3845. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3846. 'Comment' => array(
  3847. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3848. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3849. )
  3850. ),
  3851. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3852. );
  3853. $expected = array(
  3854. 'Article' => true,
  3855. 'Comment' => array(
  3856. true,
  3857. true
  3858. )
  3859. );
  3860. $this->assertSame($expected, $result);
  3861. $expected = array();
  3862. $result = $TestModel->validationErrors;
  3863. $this->assertSame($expected, $result);
  3864. $data = array(
  3865. 'Attachment' => array(
  3866. 'attachment' => 'deepsave insert',
  3867. ),
  3868. 'Comment' => array(
  3869. 'comment' => 'First comment deepsave insert',
  3870. 'published' => 'Y',
  3871. 'user_id' => 5,
  3872. 'Article' => array(
  3873. 'title' => 'First Article deepsave insert ignored',
  3874. 'body' => 'First Article Body deepsave insert',
  3875. 'User' => array(
  3876. 'user' => '',
  3877. 'password' => 'magic'
  3878. ),
  3879. ),
  3880. )
  3881. );
  3882. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3883. $this->assertTrue($result);
  3884. $result = $TestModel->Comment->Attachment->validationErrors;
  3885. $expected = array();
  3886. $this->assertSame($expected, $result);
  3887. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3888. $expected = array(
  3889. 'Attachment' => true,
  3890. 'Comment' => true
  3891. );
  3892. $this->assertEquals($expected, $result);
  3893. $data['Comment']['Article']['body'] = '';
  3894. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3895. $this->assertTrue($result);
  3896. $result = $TestModel->Comment->Attachment->validationErrors;
  3897. $expected = array();
  3898. $this->assertSame($expected, $result);
  3899. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3900. $expected = array(
  3901. 'Attachment' => true,
  3902. 'Comment' => true
  3903. );
  3904. $this->assertEquals($expected, $result);
  3905. }
  3906. /**
  3907. * testSaveAllHasMany method
  3908. *
  3909. * @return void
  3910. */
  3911. public function testSaveAllHasMany() {
  3912. $this->loadFixtures('Article', 'Comment');
  3913. $TestModel = new Article();
  3914. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3915. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3916. $result = $TestModel->saveAll(array(
  3917. 'Article' => array('id' => 2),
  3918. 'Comment' => array(
  3919. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3920. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3921. )
  3922. ));
  3923. $this->assertFalse(empty($result));
  3924. $result = $TestModel->findById(2);
  3925. $expected = array(
  3926. 'First Comment for Second Article',
  3927. 'Second Comment for Second Article',
  3928. 'First new comment',
  3929. 'Second new comment'
  3930. );
  3931. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3932. $this->assertEquals($expected, $result);
  3933. $result = $TestModel->saveAll(
  3934. array(
  3935. 'Article' => array('id' => 2),
  3936. 'Comment' => array(
  3937. array(
  3938. 'comment' => 'Third new comment',
  3939. 'published' => 'Y',
  3940. 'user_id' => 1
  3941. ))),
  3942. array('atomic' => false)
  3943. );
  3944. $this->assertFalse(empty($result));
  3945. $result = $TestModel->findById(2);
  3946. $expected = array(
  3947. 'First Comment for Second Article',
  3948. 'Second Comment for Second Article',
  3949. 'First new comment',
  3950. 'Second new comment',
  3951. 'Third new comment'
  3952. );
  3953. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3954. $this->assertEquals($expected, $result);
  3955. $TestModel->beforeSaveReturn = false;
  3956. $result = $TestModel->saveAll(
  3957. array(
  3958. 'Article' => array('id' => 2),
  3959. 'Comment' => array(
  3960. array(
  3961. 'comment' => 'Fourth new comment',
  3962. 'published' => 'Y',
  3963. 'user_id' => 1
  3964. ))),
  3965. array('atomic' => false)
  3966. );
  3967. $this->assertEquals(array('Article' => false), $result);
  3968. $result = $TestModel->findById(2);
  3969. $expected = array(
  3970. 'First Comment for Second Article',
  3971. 'Second Comment for Second Article',
  3972. 'First new comment',
  3973. 'Second new comment',
  3974. 'Third new comment'
  3975. );
  3976. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3977. $this->assertEquals($expected, $result);
  3978. }
  3979. /**
  3980. * testSaveAllHasManyValidation method
  3981. *
  3982. * @return void
  3983. */
  3984. public function testSaveAllHasManyValidation() {
  3985. $this->loadFixtures('Article', 'Comment');
  3986. $TestModel = new Article();
  3987. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3988. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3989. $result = $TestModel->saveAll(array(
  3990. 'Article' => array('id' => 2),
  3991. 'Comment' => array(
  3992. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3993. )
  3994. ), array('validate' => true));
  3995. $this->assertFalse($result);
  3996. $expected = array('Comment' => array(
  3997. array('comment' => array('This field cannot be left blank'))
  3998. ));
  3999. $this->assertEquals($expected, $TestModel->validationErrors);
  4000. $expected = array(
  4001. array('comment' => array('This field cannot be left blank'))
  4002. );
  4003. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4004. $result = $TestModel->saveAll(array(
  4005. 'Article' => array('id' => 2),
  4006. 'Comment' => array(
  4007. array(
  4008. 'comment' => '',
  4009. 'published' => 'Y',
  4010. 'user_id' => 1
  4011. ))
  4012. ), array('validate' => 'first'));
  4013. $this->assertFalse($result);
  4014. }
  4015. /**
  4016. * test saveAll with transactions and ensure there is no missing rollback.
  4017. *
  4018. * @return void
  4019. */
  4020. public function testSaveAllManyRowsTransactionNoRollback() {
  4021. $this->loadFixtures('Post');
  4022. $Post = new TestPost();
  4023. $Post->validate = array(
  4024. 'title' => array('rule' => array('notEmpty'))
  4025. );
  4026. // If validation error occurs, rollback() should be called.
  4027. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  4028. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  4029. $db->expects($this->never())->method('commit');
  4030. $db->expects($this->once())->method('rollback');
  4031. $Post->setDataSourceObject($db);
  4032. $data = array(
  4033. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4034. array('author_id' => 1, 'title' => '')
  4035. );
  4036. $Post->saveAll($data, array('atomic' => true, 'validate' => true));
  4037. // If exception thrown, rollback() should be called too.
  4038. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  4039. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  4040. $db->expects($this->never())->method('commit');
  4041. $db->expects($this->once())->method('rollback');
  4042. $Post->setDataSourceObject($db);
  4043. $data = array(
  4044. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4045. array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => $db->expression('PDO_EXCEPTION()'))
  4046. );
  4047. try {
  4048. $Post->saveAll($data, array('atomic' => true, 'validate' => true));
  4049. $this->fail('No exception thrown');
  4050. } catch (PDOException $e) {
  4051. }
  4052. // Otherwise, commit() should be called.
  4053. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  4054. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  4055. $db->expects($this->once())->method('commit');
  4056. $db->expects($this->never())->method('rollback');
  4057. $Post->setDataSourceObject($db);
  4058. $data = array(
  4059. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4060. array('author_id' => 1, 'title' => 'New Fifth Post')
  4061. );
  4062. $Post->saveAll($data, array('atomic' => true, 'validate' => true));
  4063. }
  4064. /**
  4065. * test saveAll with transactions and ensure there is no missing rollback.
  4066. *
  4067. * @return void
  4068. */
  4069. public function testSaveAllAssociatedTransactionNoRollback() {
  4070. $this->loadFixtures('Post', 'Author');
  4071. $Post = new TestPost();
  4072. $Post->Author->validate = array(
  4073. 'user' => array('rule' => array('notEmpty'))
  4074. );
  4075. // If validation error occurs, rollback() should be called.
  4076. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  4077. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  4078. $db->expects($this->never())->method('commit');
  4079. $db->expects($this->once())->method('rollback');
  4080. $Post->setDataSourceObject($db);
  4081. $Post->Author->setDataSourceObject($db);
  4082. $data = array(
  4083. 'Post' => array(
  4084. 'title' => 'New post',
  4085. 'body' => 'Content',
  4086. 'published' => 'Y'
  4087. ),
  4088. 'Author' => array(
  4089. 'user' => '',
  4090. 'password' => "sekret"
  4091. )
  4092. );
  4093. $Post->saveAll($data, array('validate' => true));
  4094. // If exception thrown, rollback() should be called too.
  4095. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  4096. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  4097. $db->expects($this->never())->method('commit');
  4098. $db->expects($this->once())->method('rollback');
  4099. $Post->setDataSourceObject($db);
  4100. $Post->Author->setDataSourceObject($db);
  4101. $data = array(
  4102. 'Post' => array(
  4103. 'title' => 'New post',
  4104. 'body' => $db->expression('PDO_EXCEPTION()'),
  4105. 'published' => 'Y'
  4106. ),
  4107. 'Author' => array(
  4108. 'user' => 'New user',
  4109. 'password' => "sekret"
  4110. )
  4111. );
  4112. try {
  4113. $Post->saveAll($data, array('validate' => true));
  4114. $this->fail('No exception thrown');
  4115. } catch (PDOException $e) {
  4116. }
  4117. // Otherwise, commit() should be called.
  4118. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  4119. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  4120. $db->expects($this->once())->method('commit');
  4121. $db->expects($this->never())->method('rollback');
  4122. $Post->setDataSourceObject($db);
  4123. $Post->Author->setDataSourceObject($db);
  4124. $data = array(
  4125. 'Post' => array(
  4126. 'title' => 'New post',
  4127. 'body' => 'Content',
  4128. 'published' => 'Y'
  4129. ),
  4130. 'Author' => array(
  4131. 'user' => 'New user',
  4132. 'password' => "sekret"
  4133. )
  4134. );
  4135. $Post->saveAll($data, array('validate' => true));
  4136. }
  4137. /**
  4138. * test saveAll with nested saveAll call.
  4139. *
  4140. * @return void
  4141. */
  4142. public function testSaveAllNestedSaveAll() {
  4143. $this->loadFixtures('Sample');
  4144. $TransactionTestModel = new TransactionTestModel();
  4145. $data = array(
  4146. array('apple_id' => 1, 'name' => 'sample5'),
  4147. );
  4148. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  4149. }
  4150. /**
  4151. * testSaveAllTransaction method
  4152. *
  4153. * @return void
  4154. */
  4155. public function testSaveAllTransaction() {
  4156. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4157. $TestModel = new Post();
  4158. $TestModel->validate = array('title' => 'notEmpty');
  4159. $data = array(
  4160. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4161. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4162. array('author_id' => 1, 'title' => '')
  4163. );
  4164. $this->assertFalse($TestModel->saveAll($data));
  4165. $result = $TestModel->find('all', array('recursive' => -1));
  4166. $expected = array(
  4167. array('Post' => array(
  4168. 'id' => '1',
  4169. 'author_id' => 1,
  4170. 'title' => 'First Post',
  4171. 'body' => 'First Post Body',
  4172. 'published' => 'Y',
  4173. 'created' => '2007-03-18 10:39:23',
  4174. 'updated' => '2007-03-18 10:41:31'
  4175. )),
  4176. array('Post' => array(
  4177. 'id' => '2',
  4178. 'author_id' => 3,
  4179. 'title' => 'Second Post',
  4180. 'body' => 'Second Post Body',
  4181. 'published' => 'Y',
  4182. 'created' => '2007-03-18 10:41:23',
  4183. 'updated' => '2007-03-18 10:43:31'
  4184. )),
  4185. array('Post' => array(
  4186. 'id' => '3',
  4187. 'author_id' => 1,
  4188. 'title' => 'Third Post',
  4189. 'body' => 'Third Post Body',
  4190. 'published' => 'Y',
  4191. 'created' => '2007-03-18 10:43:23',
  4192. 'updated' => '2007-03-18 10:45:31'
  4193. )));
  4194. if (count($result) != 3) {
  4195. // Database doesn't support transactions
  4196. $expected[] = array(
  4197. 'Post' => array(
  4198. 'id' => '4',
  4199. 'author_id' => 1,
  4200. 'title' => 'New Fourth Post',
  4201. 'body' => null,
  4202. 'published' => 'N',
  4203. 'created' => self::date(),
  4204. 'updated' => self::date()
  4205. ));
  4206. $expected[] = array(
  4207. 'Post' => array(
  4208. 'id' => '5',
  4209. 'author_id' => 1,
  4210. 'title' => 'New Fifth Post',
  4211. 'body' => null,
  4212. 'published' => 'N',
  4213. 'created' => self::date(),
  4214. 'updated' => self::date()
  4215. ));
  4216. $this->assertEquals($expected, $result);
  4217. // Skip the rest of the transactional tests
  4218. return;
  4219. }
  4220. $this->assertEquals($expected, $result);
  4221. $data = array(
  4222. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4223. array('author_id' => 1, 'title' => ''),
  4224. array('author_id' => 1, 'title' => 'New Sixth Post')
  4225. );
  4226. $this->assertFalse($TestModel->saveAll($data));
  4227. $result = $TestModel->find('all', array('recursive' => -1));
  4228. $expected = array(
  4229. array('Post' => array(
  4230. 'id' => '1',
  4231. 'author_id' => 1,
  4232. 'title' => 'First Post',
  4233. 'body' => 'First Post Body',
  4234. 'published' => 'Y',
  4235. 'created' => '2007-03-18 10:39:23',
  4236. 'updated' => '2007-03-18 10:41:31'
  4237. )),
  4238. array('Post' => array(
  4239. 'id' => '2',
  4240. 'author_id' => 3,
  4241. 'title' => 'Second Post',
  4242. 'body' => 'Second Post Body',
  4243. 'published' => 'Y',
  4244. 'created' => '2007-03-18 10:41:23',
  4245. 'updated' => '2007-03-18 10:43:31'
  4246. )),
  4247. array('Post' => array(
  4248. 'id' => '3',
  4249. 'author_id' => 1,
  4250. 'title' => 'Third Post',
  4251. 'body' => 'Third Post Body',
  4252. 'published' => 'Y',
  4253. 'created' => '2007-03-18 10:43:23',
  4254. 'updated' => '2007-03-18 10:45:31'
  4255. )));
  4256. if (count($result) != 3) {
  4257. // Database doesn't support transactions
  4258. $expected[] = array(
  4259. 'Post' => array(
  4260. 'id' => '4',
  4261. 'author_id' => 1,
  4262. 'title' => 'New Fourth Post',
  4263. 'body' => 'Third Post Body',
  4264. 'published' => 'N',
  4265. 'created' => self::date(),
  4266. 'updated' => self::date()
  4267. ));
  4268. $expected[] = array(
  4269. 'Post' => array(
  4270. 'id' => '5',
  4271. 'author_id' => 1,
  4272. 'title' => 'Third Post',
  4273. 'body' => 'Third Post Body',
  4274. 'published' => 'N',
  4275. 'created' => self::date(),
  4276. 'updated' => self::date()
  4277. ));
  4278. }
  4279. $this->assertEquals($expected, $result);
  4280. $TestModel->validate = array('title' => 'notEmpty');
  4281. $data = array(
  4282. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4283. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4284. array('author_id' => 1, 'title' => 'New Sixth Post')
  4285. );
  4286. $this->assertTrue($TestModel->saveAll($data));
  4287. $result = $TestModel->find('all', array(
  4288. 'recursive' => -1,
  4289. 'fields' => array('author_id', 'title', 'body', 'published'),
  4290. 'order' => array('Post.created' => 'ASC')
  4291. ));
  4292. $expected = array(
  4293. array('Post' => array(
  4294. 'author_id' => 1,
  4295. 'title' => 'First Post',
  4296. 'body' => 'First Post Body',
  4297. 'published' => 'Y'
  4298. )),
  4299. array('Post' => array(
  4300. 'author_id' => 3,
  4301. 'title' => 'Second Post',
  4302. 'body' => 'Second Post Body',
  4303. 'published' => 'Y'
  4304. )),
  4305. array('Post' => array(
  4306. 'author_id' => 1,
  4307. 'title' => 'Third Post',
  4308. 'body' => 'Third Post Body',
  4309. 'published' => 'Y'
  4310. )),
  4311. array('Post' => array(
  4312. 'author_id' => 1,
  4313. 'title' => 'New Fourth Post',
  4314. 'body' => '',
  4315. 'published' => 'N'
  4316. )),
  4317. array('Post' => array(
  4318. 'author_id' => 1,
  4319. 'title' => 'New Fifth Post',
  4320. 'body' => '',
  4321. 'published' => 'N'
  4322. )),
  4323. array('Post' => array(
  4324. 'author_id' => 1,
  4325. 'title' => 'New Sixth Post',
  4326. 'body' => '',
  4327. 'published' => 'N'
  4328. )));
  4329. $this->assertEquals($expected, $result);
  4330. }
  4331. /**
  4332. * testSaveAllValidation method
  4333. *
  4334. * @return void
  4335. */
  4336. public function testSaveAllValidation() {
  4337. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4338. $TestModel = new Post();
  4339. $data = array(
  4340. array(
  4341. 'id' => '1',
  4342. 'title' => 'Baleeted First Post',
  4343. 'body' => 'Baleeted!',
  4344. 'published' => 'N'
  4345. ),
  4346. array(
  4347. 'id' => '2',
  4348. 'title' => 'Just update the title'
  4349. ),
  4350. array(
  4351. 'title' => 'Creating a fourth post',
  4352. 'body' => 'Fourth post body',
  4353. 'author_id' => 2
  4354. ));
  4355. $this->assertTrue($TestModel->saveAll($data));
  4356. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4357. $expected = array(
  4358. array(
  4359. 'Post' => array(
  4360. 'id' => '1',
  4361. 'author_id' => '1',
  4362. 'title' => 'Baleeted First Post',
  4363. 'body' => 'Baleeted!',
  4364. 'published' => 'N',
  4365. 'created' => '2007-03-18 10:39:23'
  4366. )),
  4367. array(
  4368. 'Post' => array(
  4369. 'id' => '2',
  4370. 'author_id' => '3',
  4371. 'title' => 'Just update the title',
  4372. 'body' => 'Second Post Body',
  4373. 'published' => 'Y',
  4374. 'created' => '2007-03-18 10:41:23'
  4375. )),
  4376. array(
  4377. 'Post' => array(
  4378. 'id' => '3',
  4379. 'author_id' => '1',
  4380. 'title' => 'Third Post',
  4381. 'body' => 'Third Post Body',
  4382. 'published' => 'Y',
  4383. 'created' => '2007-03-18 10:43:23',
  4384. 'updated' => '2007-03-18 10:45:31'
  4385. )),
  4386. array(
  4387. 'Post' => array(
  4388. 'id' => '4',
  4389. 'author_id' => '2',
  4390. 'title' => 'Creating a fourth post',
  4391. 'body' => 'Fourth post body',
  4392. 'published' => 'N'
  4393. )));
  4394. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4395. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4396. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4397. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4398. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  4399. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4400. $this->assertEquals($expected, $result);
  4401. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4402. $data = array(
  4403. array(
  4404. 'id' => '1',
  4405. 'title' => 'Un-Baleeted First Post',
  4406. 'body' => 'Not Baleeted!',
  4407. 'published' => 'Y'
  4408. ),
  4409. array(
  4410. 'id' => '2',
  4411. 'title' => '',
  4412. 'body' => 'Trying to get away with an empty title'
  4413. ));
  4414. $result = $TestModel->saveAll($data);
  4415. $this->assertFalse($result);
  4416. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4417. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4418. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  4419. if (!$transactionWorked) {
  4420. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  4421. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  4422. }
  4423. $this->assertEquals($errors, $TestModel->validationErrors);
  4424. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4425. $data = array(
  4426. array(
  4427. 'id' => '1',
  4428. 'title' => 'Un-Baleeted First Post',
  4429. 'body' => 'Not Baleeted!',
  4430. 'published' => 'Y'
  4431. ),
  4432. array(
  4433. 'id' => '2',
  4434. 'title' => '',
  4435. 'body' => 'Trying to get away with an empty title'
  4436. ));
  4437. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  4438. $this->assertEquals(array(true, false), $result);
  4439. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4440. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4441. $expected = array(
  4442. array(
  4443. 'Post' => array(
  4444. 'id' => '1',
  4445. 'author_id' => '1',
  4446. 'title' => 'Un-Baleeted First Post',
  4447. 'body' => 'Not Baleeted!',
  4448. 'published' => 'Y',
  4449. 'created' => '2007-03-18 10:39:23'
  4450. )
  4451. ),
  4452. array(
  4453. 'Post' => array(
  4454. 'id' => '2',
  4455. 'author_id' => '3',
  4456. 'title' => 'Just update the title',
  4457. 'body' => 'Second Post Body',
  4458. 'published' => 'Y',
  4459. 'created' => '2007-03-18 10:41:23'
  4460. )
  4461. ),
  4462. array(
  4463. 'Post' => array(
  4464. 'id' => '3',
  4465. 'author_id' => '1',
  4466. 'title' => 'Third Post',
  4467. 'body' => 'Third Post Body',
  4468. 'published' => 'Y',
  4469. 'created' => '2007-03-18 10:43:23',
  4470. 'updated' => '2007-03-18 10:45:31'
  4471. )
  4472. ),
  4473. array(
  4474. 'Post' => array(
  4475. 'id' => '4',
  4476. 'author_id' => '2',
  4477. 'title' => 'Creating a fourth post',
  4478. 'body' => 'Fourth post body',
  4479. 'published' => 'N'
  4480. )
  4481. )
  4482. );
  4483. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4484. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4485. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4486. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4487. unset(
  4488. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4489. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4490. );
  4491. $this->assertEquals($expected, $result);
  4492. $this->assertEquals($errors, $TestModel->validationErrors);
  4493. $data = array(
  4494. array(
  4495. 'id' => '1',
  4496. 'title' => 'Re-Baleeted First Post',
  4497. 'body' => 'Baleeted!',
  4498. 'published' => 'N'
  4499. ),
  4500. array(
  4501. 'id' => '2',
  4502. 'title' => '',
  4503. 'body' => 'Trying to get away with an empty title'
  4504. ));
  4505. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  4506. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4507. unset(
  4508. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4509. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4510. );
  4511. $this->assertEquals($expected, $result);
  4512. $this->assertEquals($errors, $TestModel->validationErrors);
  4513. }
  4514. /**
  4515. * testSaveAllValidationOnly method
  4516. *
  4517. * @return void
  4518. */
  4519. public function testSaveAllValidationOnly() {
  4520. $this->loadFixtures('Comment', 'Attachment');
  4521. $TestModel = new Comment();
  4522. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4523. $data = array(
  4524. 'Comment' => array(
  4525. 'comment' => 'This is the comment'
  4526. ),
  4527. 'Attachment' => array(
  4528. 'attachment' => ''
  4529. )
  4530. );
  4531. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  4532. $this->assertFalse($result);
  4533. $TestModel = new Article();
  4534. $TestModel->validate = array('title' => 'notEmpty');
  4535. $result = $TestModel->saveAll(
  4536. array(
  4537. 0 => array('title' => ''),
  4538. 1 => array('title' => 'title 1'),
  4539. 2 => array('title' => 'title 2'),
  4540. ),
  4541. array('validate' => 'only')
  4542. );
  4543. $this->assertFalse($result);
  4544. $expected = array(
  4545. 0 => array('title' => array('This field cannot be left blank')),
  4546. );
  4547. $this->assertEquals($expected, $TestModel->validationErrors);
  4548. $result = $TestModel->saveAll(
  4549. array(
  4550. 0 => array('title' => 'title 0'),
  4551. 1 => array('title' => ''),
  4552. 2 => array('title' => 'title 2'),
  4553. ),
  4554. array('validate' => 'only')
  4555. );
  4556. $this->assertFalse($result);
  4557. $expected = array(
  4558. 1 => array('title' => array('This field cannot be left blank')),
  4559. );
  4560. $this->assertEquals($expected, $TestModel->validationErrors);
  4561. }
  4562. /**
  4563. * testSaveAllValidateFirst method
  4564. *
  4565. * @return void
  4566. */
  4567. public function testSaveAllValidateFirst() {
  4568. $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
  4569. $model = new Article();
  4570. $model->deleteAll(true);
  4571. $model->Comment->validate = array('comment' => 'notEmpty');
  4572. $result = $model->saveAll(array(
  4573. 'Article' => array(
  4574. 'title' => 'Post with Author',
  4575. 'body' => 'This post will be saved author'
  4576. ),
  4577. 'Comment' => array(
  4578. array('comment' => 'First new comment'),
  4579. array('comment' => '')
  4580. )
  4581. ), array('validate' => 'first'));
  4582. $this->assertFalse($result);
  4583. $result = $model->find('all');
  4584. $this->assertSame(array(), $result);
  4585. $expected = array('Comment' => array(
  4586. 1 => array('comment' => array('This field cannot be left blank'))
  4587. ));
  4588. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4589. $this->assertSame($model->Comment->find('count'), 0);
  4590. $result = $model->saveAll(
  4591. array(
  4592. 'Article' => array(
  4593. 'title' => 'Post with Author',
  4594. 'body' => 'This post will be saved with an author',
  4595. 'user_id' => 2
  4596. ),
  4597. 'Comment' => array(
  4598. array(
  4599. 'comment' => 'Only new comment',
  4600. 'user_id' => 2
  4601. ))),
  4602. array('validate' => 'first')
  4603. );
  4604. $this->assertTrue($result);
  4605. $result = $model->Comment->find('all');
  4606. $this->assertSame(count($result), 1);
  4607. $result = Hash::extract($result, '{n}.Comment.article_id');
  4608. $this->assertEquals(4, $result[0]);
  4609. $model->deleteAll(true);
  4610. $data = array(
  4611. 'Article' => array(
  4612. 'title' => 'Post with Author saveAlled from comment',
  4613. 'body' => 'This post will be saved with an author',
  4614. 'user_id' => 2
  4615. ),
  4616. 'Comment' => array(
  4617. 'comment' => 'Only new comment', 'user_id' => 2
  4618. ));
  4619. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  4620. $this->assertFalse(empty($result));
  4621. $result = $model->find('all');
  4622. $this->assertEquals(
  4623. $result[0]['Article']['title'],
  4624. 'Post with Author saveAlled from comment'
  4625. );
  4626. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4627. }
  4628. /**
  4629. * test saveAll()'s return is correct when using atomic = false and validate = first.
  4630. *
  4631. * @return void
  4632. */
  4633. public function testSaveAllValidateFirstAtomicFalse() {
  4634. $this->loadFixtures('Something');
  4635. $Something = new Something();
  4636. $invalidData = array(
  4637. array(
  4638. 'title' => 'foo',
  4639. 'body' => 'bar',
  4640. 'published' => 'baz',
  4641. ),
  4642. array(
  4643. 'body' => 3,
  4644. 'published' => 'sd',
  4645. ),
  4646. );
  4647. $Something->create();
  4648. $Something->validate = array(
  4649. 'title' => array(
  4650. 'rule' => 'alphaNumeric',
  4651. 'required' => true,
  4652. ),
  4653. 'body' => array(
  4654. 'rule' => 'alphaNumeric',
  4655. 'required' => true,
  4656. 'allowEmpty' => true,
  4657. ),
  4658. );
  4659. $result = $Something->saveAll($invalidData, array(
  4660. 'atomic' => false,
  4661. 'validate' => 'first',
  4662. ));
  4663. $expected = array(true, false);
  4664. $this->assertEquals($expected, $result);
  4665. $Something = new Something();
  4666. $validData = array(
  4667. array(
  4668. 'title' => 'title value',
  4669. 'body' => 'body value',
  4670. 'published' => 'baz',
  4671. ),
  4672. array(
  4673. 'title' => 'valid',
  4674. 'body' => 'this body',
  4675. 'published' => 'sd',
  4676. ),
  4677. );
  4678. $Something->create();
  4679. $result = $Something->saveAll($validData, array(
  4680. 'atomic' => false,
  4681. 'validate' => 'first',
  4682. ));
  4683. $expected = array(true, true);
  4684. $this->assertEquals($expected, $result);
  4685. }
  4686. /**
  4687. * testSaveAllHasManyValidationOnly method
  4688. *
  4689. * @return void
  4690. */
  4691. public function testSaveAllHasManyValidationOnly() {
  4692. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4693. $TestModel = new Article();
  4694. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4695. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4696. $result = $TestModel->saveAll(
  4697. array(
  4698. 'Article' => array('id' => 2),
  4699. 'Comment' => array(
  4700. array(
  4701. 'id' => 1,
  4702. 'comment' => '',
  4703. 'published' => 'Y',
  4704. 'user_id' => 1),
  4705. array(
  4706. 'id' => 2,
  4707. 'comment' =>
  4708. 'comment',
  4709. 'published' => 'Y',
  4710. 'user_id' => 1
  4711. ))),
  4712. array('validate' => 'only')
  4713. );
  4714. $this->assertFalse($result);
  4715. $result = $TestModel->saveAll(
  4716. array(
  4717. 'Article' => array('id' => 2),
  4718. 'Comment' => array(
  4719. array(
  4720. 'id' => 1,
  4721. 'comment' => '',
  4722. 'published' => 'Y',
  4723. 'user_id' => 1
  4724. ),
  4725. array(
  4726. 'id' => 2,
  4727. 'comment' => 'comment',
  4728. 'published' => 'Y',
  4729. 'user_id' => 1
  4730. ),
  4731. array(
  4732. 'id' => 3,
  4733. 'comment' => '',
  4734. 'published' => 'Y',
  4735. 'user_id' => 1
  4736. ))),
  4737. array(
  4738. 'validate' => 'only',
  4739. 'atomic' => false
  4740. ));
  4741. $expected = array(
  4742. 'Article' => true,
  4743. 'Comment' => array(false, true, false)
  4744. );
  4745. $this->assertSame($expected, $result);
  4746. $expected = array('Comment' => array(
  4747. 0 => array('comment' => array('This field cannot be left blank')),
  4748. 2 => array('comment' => array('This field cannot be left blank'))
  4749. ));
  4750. $this->assertEquals($expected, $TestModel->validationErrors);
  4751. $expected = array(
  4752. 0 => array('comment' => array('This field cannot be left blank')),
  4753. 2 => array('comment' => array('This field cannot be left blank'))
  4754. );
  4755. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4756. }
  4757. /**
  4758. * test that saveAll still behaves like previous versions (does not necessarily need a first argument)
  4759. *
  4760. * @return void
  4761. */
  4762. public function testSaveAllWithSet() {
  4763. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4764. $data = array(
  4765. 'Article' => array(
  4766. 'user_id' => 1,
  4767. 'title' => 'Article Has and belongs to Many Tags'
  4768. ),
  4769. 'Tag' => array(
  4770. 'Tag' => array(1, 2)
  4771. ),
  4772. 'Comment' => array(
  4773. array(
  4774. 'comment' => 'Article comment',
  4775. 'user_id' => 1
  4776. )));
  4777. $Article = new Article();
  4778. $Article->set($data);
  4779. $result = $Article->saveAll();
  4780. $this->assertFalse(empty($result));
  4781. }
  4782. /**
  4783. * test that saveAll behaves like plain save() when supplied empty data
  4784. *
  4785. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4786. * @return void
  4787. */
  4788. public function testSaveAllEmptyData() {
  4789. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4790. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4791. $model = new Article();
  4792. $result = $model->saveAll(array(), array('validate' => 'first'));
  4793. $this->assertFalse(empty($result));
  4794. $model = new ProductUpdateAll();
  4795. $result = $model->saveAll();
  4796. $this->assertFalse($result);
  4797. }
  4798. /**
  4799. * testSaveAssociated method
  4800. *
  4801. * @return void
  4802. */
  4803. public function testSaveAssociated() {
  4804. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  4805. $TestModel = new Post();
  4806. $result = $TestModel->find('all');
  4807. $this->assertEquals(3, count($result));
  4808. $this->assertFalse(isset($result[3]));
  4809. $TestModel->saveAssociated(array(
  4810. 'Post' => array(
  4811. 'title' => 'Post with Author',
  4812. 'body' => 'This post will be saved with an author'
  4813. ),
  4814. 'Author' => array(
  4815. 'user' => 'bob',
  4816. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  4817. )));
  4818. $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
  4819. $expected = array(
  4820. 'Post' => array(
  4821. 'id' => '4',
  4822. 'author_id' => '5',
  4823. 'title' => 'Post with Author',
  4824. 'body' => 'This post will be saved with an author',
  4825. 'published' => 'N'
  4826. ),
  4827. 'Author' => array(
  4828. 'id' => '5',
  4829. 'user' => 'bob',
  4830. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  4831. 'test' => 'working'
  4832. ));
  4833. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4834. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4835. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  4836. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  4837. unset(
  4838. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  4839. $result[3]['Author']['updated'], $result[3]['Author']['created']
  4840. );
  4841. $this->assertEquals($expected, $result[3]);
  4842. $this->assertEquals(4, count($result));
  4843. $TestModel = new Comment();
  4844. $result = $TestModel->saveAssociated(array(
  4845. 'Comment' => array(
  4846. 'article_id' => 2,
  4847. 'user_id' => 2,
  4848. 'comment' => 'New comment with attachment',
  4849. 'published' => 'Y'
  4850. ),
  4851. 'Attachment' => array(
  4852. 'attachment' => 'some_file.tgz'
  4853. )));
  4854. $this->assertFalse(empty($result));
  4855. $result = $TestModel->find('all');
  4856. $expected = array(
  4857. 'id' => '7',
  4858. 'article_id' => '2',
  4859. 'user_id' => '2',
  4860. 'comment' => 'New comment with attachment',
  4861. 'published' => 'Y'
  4862. );
  4863. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  4864. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  4865. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  4866. $this->assertEquals($expected, $result[6]['Comment']);
  4867. $expected = array(
  4868. 'id' => '2',
  4869. 'comment_id' => '7',
  4870. 'attachment' => 'some_file.tgz'
  4871. );
  4872. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  4873. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  4874. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  4875. $this->assertEquals($expected, $result[6]['Attachment']);
  4876. }
  4877. /**
  4878. * Test that validate = first, atomic = false works when associated records
  4879. * fail validation.
  4880. *
  4881. * @return void
  4882. */
  4883. public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
  4884. $this->loadFixtures('Comment', 'Article', 'User');
  4885. $Article = ClassRegistry::init('Article');
  4886. $Article->Comment->validator()->add('comment', array(
  4887. array('rule' => 'notEmpty')
  4888. ));
  4889. $data = array(
  4890. 'Article' => array(
  4891. 'user_id' => 1,
  4892. 'title' => 'Foo',
  4893. 'body' => 'text',
  4894. 'published' => 'N'
  4895. ),
  4896. 'Comment' => array(
  4897. array(
  4898. 'user_id' => 1,
  4899. 'comment' => '',
  4900. 'published' => 'N',
  4901. )
  4902. ),
  4903. );
  4904. $Article->saveAssociated(
  4905. $data,
  4906. array('validate' => 'first', 'atomic' => false)
  4907. );
  4908. $result = $Article->validationErrors;
  4909. $expected = array(
  4910. 'Comment' => array(
  4911. array(
  4912. 'comment' => array('This field cannot be left blank')
  4913. )
  4914. )
  4915. );
  4916. $this->assertEquals($expected, $result);
  4917. }
  4918. /**
  4919. * testSaveMany method
  4920. *
  4921. * @return void
  4922. */
  4923. public function testSaveMany() {
  4924. $this->loadFixtures('Post');
  4925. $TestModel = new Post();
  4926. $TestModel->deleteAll(true);
  4927. $this->assertEquals(array(), $TestModel->find('all'));
  4928. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  4929. $this->db->truncate($TestModel);
  4930. $TestModel->saveMany(array(
  4931. array(
  4932. 'title' => 'Multi-record post 1',
  4933. 'body' => 'First multi-record post',
  4934. 'author_id' => 2
  4935. ),
  4936. array(
  4937. 'title' => 'Multi-record post 2',
  4938. 'body' => 'Second multi-record post',
  4939. 'author_id' => 2
  4940. )));
  4941. $result = $TestModel->find('all', array(
  4942. 'recursive' => -1,
  4943. 'order' => 'Post.id ASC'
  4944. ));
  4945. $expected = array(
  4946. array(
  4947. 'Post' => array(
  4948. 'id' => '1',
  4949. 'author_id' => '2',
  4950. 'title' => 'Multi-record post 1',
  4951. 'body' => 'First multi-record post',
  4952. 'published' => 'N'
  4953. )
  4954. ),
  4955. array(
  4956. 'Post' => array(
  4957. 'id' => '2',
  4958. 'author_id' => '2',
  4959. 'title' => 'Multi-record post 2',
  4960. 'body' => 'Second multi-record post',
  4961. 'published' => 'N'
  4962. )
  4963. )
  4964. );
  4965. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4966. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  4967. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4968. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  4969. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  4970. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  4971. $this->assertEquals($expected, $result);
  4972. }
  4973. /**
  4974. * Test SaveMany with validate=false.
  4975. *
  4976. * @return void
  4977. */
  4978. public function testSaveManyValidateFalse() {
  4979. $this->loadFixtures('Post');
  4980. $TestModel = new Post();
  4981. $TestModel->deleteAll(true);
  4982. $data = array(
  4983. array('id' => 1, 'author_id' => 1, 'title' => 'hi'),
  4984. array('id' => 2, 'author_id' => 1, 'title' => 'bye')
  4985. );
  4986. $result = $TestModel->saveAll($data, array('validate' => false));
  4987. $this->assertTrue($result);
  4988. }
  4989. /**
  4990. * Test SaveAssociated with Habtm relations
  4991. *
  4992. * @return void
  4993. */
  4994. public function testSaveAssociatedHabtm() {
  4995. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4996. $data = array(
  4997. 'Article' => array(
  4998. 'user_id' => 1,
  4999. 'title' => 'Article Has and belongs to Many Tags'
  5000. ),
  5001. 'Tag' => array(
  5002. 'Tag' => array(1, 2)
  5003. ),
  5004. 'Comment' => array(
  5005. array(
  5006. 'comment' => 'Article comment',
  5007. 'user_id' => 1
  5008. )));
  5009. $Article = new Article();
  5010. $result = $Article->saveAssociated($data);
  5011. $this->assertFalse(empty($result));
  5012. $result = $Article->read();
  5013. $this->assertEquals(2, count($result['Tag']));
  5014. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  5015. $this->assertEquals(1, count($result['Comment']));
  5016. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  5017. }
  5018. /**
  5019. * Test SaveAssociated with Habtm relations and extra join table fields
  5020. *
  5021. * @return void
  5022. */
  5023. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  5024. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  5025. $data = array(
  5026. 'Something' => array(
  5027. 'id' => 4,
  5028. 'title' => 'Extra Fields',
  5029. 'body' => 'Extra Fields Body',
  5030. 'published' => '1'
  5031. ),
  5032. 'SomethingElse' => array(
  5033. array('something_else_id' => 1, 'doomed' => '1'),
  5034. array('something_else_id' => 2, 'doomed' => '0'),
  5035. array('something_else_id' => 3, 'doomed' => '1')
  5036. )
  5037. );
  5038. $Something = new Something();
  5039. $result = $Something->saveAssociated($data);
  5040. $this->assertFalse(empty($result));
  5041. $result = $Something->read();
  5042. $this->assertEquals(3, count($result['SomethingElse']));
  5043. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  5044. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  5045. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  5046. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  5047. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  5048. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  5049. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  5050. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  5051. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  5052. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  5053. }
  5054. /**
  5055. * testSaveAssociatedHasOne method
  5056. *
  5057. * @return void
  5058. */
  5059. public function testSaveAssociatedHasOne() {
  5060. $model = new Comment();
  5061. $model->deleteAll(true);
  5062. $this->assertEquals(array(), $model->find('all'));
  5063. $model->Attachment->deleteAll(true);
  5064. $this->assertEquals(array(), $model->Attachment->find('all'));
  5065. $this->assertTrue($model->saveAssociated(array(
  5066. 'Comment' => array(
  5067. 'comment' => 'Comment with attachment',
  5068. 'article_id' => 1,
  5069. 'user_id' => 1
  5070. ),
  5071. 'Attachment' => array(
  5072. 'attachment' => 'some_file.zip'
  5073. ))));
  5074. $result = $model->find('all', array('fields' => array(
  5075. 'Comment.id', 'Comment.comment', 'Attachment.id',
  5076. 'Attachment.comment_id', 'Attachment.attachment'
  5077. )));
  5078. $expected = array(array(
  5079. 'Comment' => array(
  5080. 'id' => '1',
  5081. 'comment' => 'Comment with attachment'
  5082. ),
  5083. 'Attachment' => array(
  5084. 'id' => '1',
  5085. 'comment_id' => '1',
  5086. 'attachment' => 'some_file.zip'
  5087. )));
  5088. $this->assertEquals($expected, $result);
  5089. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  5090. $data = array(
  5091. 'Comment' => array(
  5092. 'comment' => 'Comment with attachment',
  5093. 'article_id' => 1,
  5094. 'user_id' => 1
  5095. ),
  5096. 'Attachment' => array(
  5097. 'attachment' => 'some_file.zip'
  5098. ));
  5099. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  5100. }
  5101. /**
  5102. * testSaveAssociatedBelongsTo method
  5103. *
  5104. * @return void
  5105. */
  5106. public function testSaveAssociatedBelongsTo() {
  5107. $model = new Comment();
  5108. $model->deleteAll(true);
  5109. $this->assertEquals(array(), $model->find('all'));
  5110. $model->Article->deleteAll(true);
  5111. $this->assertEquals(array(), $model->Article->find('all'));
  5112. $this->assertTrue($model->saveAssociated(array(
  5113. 'Comment' => array(
  5114. 'comment' => 'Article comment',
  5115. 'article_id' => 1,
  5116. 'user_id' => 1
  5117. ),
  5118. 'Article' => array(
  5119. 'title' => 'Model Associations 101',
  5120. 'user_id' => 1
  5121. ))));
  5122. $result = $model->find('all', array('fields' => array(
  5123. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  5124. )));
  5125. $expected = array(array(
  5126. 'Comment' => array(
  5127. 'id' => '1',
  5128. 'article_id' => '1',
  5129. 'comment' => 'Article comment'
  5130. ),
  5131. 'Article' => array(
  5132. 'id' => '1',
  5133. 'title' => 'Model Associations 101'
  5134. )));
  5135. $this->assertEquals($expected, $result);
  5136. }
  5137. /**
  5138. * testSaveAssociatedHasOneValidation method
  5139. *
  5140. * @return void
  5141. */
  5142. public function testSaveAssociatedHasOneValidation() {
  5143. $model = new Comment();
  5144. $model->deleteAll(true);
  5145. $this->assertEquals(array(), $model->find('all'));
  5146. $model->Attachment->deleteAll(true);
  5147. $this->assertEquals(array(), $model->Attachment->find('all'));
  5148. $model->validate = array('comment' => 'notEmpty');
  5149. $model->Attachment->validate = array('attachment' => 'notEmpty');
  5150. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  5151. $result = $model->saveAssociated(
  5152. array(
  5153. 'Comment' => array(
  5154. 'comment' => '',
  5155. 'article_id' => 1,
  5156. 'user_id' => 1
  5157. ),
  5158. 'Attachment' => array('attachment' => '')
  5159. )
  5160. );
  5161. $this->assertFalse($result);
  5162. $expected = array(
  5163. 'comment' => array(
  5164. 'This field cannot be left blank'
  5165. ),
  5166. 'Attachment' => array(
  5167. 'attachment' => array(
  5168. 'This field cannot be left blank'
  5169. )
  5170. )
  5171. );
  5172. $this->assertEquals($expected, $model->validationErrors);
  5173. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  5174. }
  5175. /**
  5176. * testSaveAssociatedAtomic method
  5177. *
  5178. * @return void
  5179. */
  5180. public function testSaveAssociatedAtomic() {
  5181. $this->loadFixtures('Article', 'User');
  5182. $TestModel = new Article();
  5183. $result = $TestModel->saveAssociated(array(
  5184. 'Article' => array(
  5185. 'title' => 'Post with Author',
  5186. 'body' => 'This post will be saved with an author',
  5187. 'user_id' => 2
  5188. ),
  5189. 'Comment' => array(
  5190. array('comment' => 'First new comment', 'user_id' => 2))
  5191. ), array('atomic' => false));
  5192. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  5193. $result = $TestModel->saveAssociated(array(
  5194. 'Article' => array('id' => 2),
  5195. 'Comment' => array(
  5196. array(
  5197. 'comment' => 'First new comment',
  5198. 'published' => 'Y',
  5199. 'user_id' => 1
  5200. ),
  5201. array(
  5202. 'comment' => 'Second new comment',
  5203. 'published' => 'Y',
  5204. 'user_id' => 2
  5205. ))
  5206. ), array('validate' => true, 'atomic' => false));
  5207. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  5208. }
  5209. /**
  5210. * testSaveManyAtomic method
  5211. *
  5212. * @return void
  5213. */
  5214. public function testSaveManyAtomic() {
  5215. $this->loadFixtures('Article', 'User');
  5216. $TestModel = new Article();
  5217. $result = $TestModel->saveMany(array(
  5218. array(
  5219. 'id' => '1',
  5220. 'title' => 'Baleeted First Post',
  5221. 'body' => 'Baleeted!',
  5222. 'published' => 'N'
  5223. ),
  5224. array(
  5225. 'id' => '2',
  5226. 'title' => 'Just update the title'
  5227. ),
  5228. array(
  5229. 'title' => 'Creating a fourth post',
  5230. 'body' => 'Fourth post body',
  5231. 'user_id' => 2
  5232. )
  5233. ), array('atomic' => false));
  5234. $this->assertSame($result, array(true, true, true));
  5235. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5236. $result = $TestModel->saveMany(array(
  5237. array(
  5238. 'id' => '1',
  5239. 'title' => 'Un-Baleeted First Post',
  5240. 'body' => 'Not Baleeted!',
  5241. 'published' => 'Y'
  5242. ),
  5243. array(
  5244. 'id' => '2',
  5245. 'title' => '',
  5246. 'body' => 'Trying to get away with an empty title'
  5247. )
  5248. ), array('validate' => true, 'atomic' => false));
  5249. $this->assertSame(array(true, false), $result);
  5250. }
  5251. /**
  5252. * testSaveAssociatedHasMany method
  5253. *
  5254. * @return void
  5255. */
  5256. public function testSaveAssociatedHasMany() {
  5257. $this->loadFixtures('Article', 'Comment');
  5258. $TestModel = new Article();
  5259. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5260. $result = $TestModel->saveAssociated(array(
  5261. 'Article' => array('id' => 2),
  5262. 'Comment' => array(
  5263. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5264. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  5265. )
  5266. ));
  5267. $this->assertFalse(empty($result));
  5268. $result = $TestModel->findById(2);
  5269. $expected = array(
  5270. 'First Comment for Second Article',
  5271. 'Second Comment for Second Article',
  5272. 'First new comment',
  5273. 'Second new comment'
  5274. );
  5275. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  5276. $result = $TestModel->saveAssociated(
  5277. array(
  5278. 'Article' => array('id' => 2),
  5279. 'Comment' => array(
  5280. array(
  5281. 'comment' => 'Third new comment',
  5282. 'published' => 'Y',
  5283. 'user_id' => 1
  5284. ))),
  5285. array('atomic' => false)
  5286. );
  5287. $this->assertFalse(empty($result));
  5288. $result = $TestModel->findById(2);
  5289. $expected = array(
  5290. 'First Comment for Second Article',
  5291. 'Second Comment for Second Article',
  5292. 'First new comment',
  5293. 'Second new comment',
  5294. 'Third new comment'
  5295. );
  5296. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  5297. $TestModel->beforeSaveReturn = false;
  5298. $result = $TestModel->saveAssociated(
  5299. array(
  5300. 'Article' => array('id' => 2),
  5301. 'Comment' => array(
  5302. array(
  5303. 'comment' => 'Fourth new comment',
  5304. 'published' => 'Y',
  5305. 'user_id' => 1
  5306. ))),
  5307. array('atomic' => false)
  5308. );
  5309. $this->assertEquals(array('Article' => false), $result);
  5310. $result = $TestModel->findById(2);
  5311. $expected = array(
  5312. 'First Comment for Second Article',
  5313. 'Second Comment for Second Article',
  5314. 'First new comment',
  5315. 'Second new comment',
  5316. 'Third new comment'
  5317. );
  5318. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  5319. }
  5320. /**
  5321. * testSaveAssociatedHasManyEmpty method
  5322. *
  5323. * @return void
  5324. */
  5325. public function testSaveAssociatedHasManyEmpty() {
  5326. $this->loadFixtures('Article', 'Comment');
  5327. $TestModel = new Article();
  5328. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5329. $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  5330. //empty hasMany data is ignored in save
  5331. $result = $TestModel->saveAssociated(array(
  5332. 'Article' => array('title' => 'title', 'user_id' => 1),
  5333. 'Comment' => array()
  5334. ), array('validate' => true));
  5335. $this->assertTrue($result);
  5336. $result = $TestModel->saveAssociated(array(
  5337. 'Article' => array('title' => 'title', 'user_id' => 1),
  5338. 'Comment' => array()
  5339. ), array('validate' => true, 'atomic' => false));
  5340. $this->assertEquals(array('Article' => true), $result);
  5341. //empty primary data is not ignored
  5342. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
  5343. $this->assertFalse($result);
  5344. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
  5345. $this->assertEquals(array('Article' => false), $result);
  5346. }
  5347. /**
  5348. * testSaveAssociatedHasManyValidation method
  5349. *
  5350. * @return void
  5351. */
  5352. public function testSaveAssociatedHasManyValidation() {
  5353. $this->loadFixtures('Article', 'Comment');
  5354. $TestModel = new Article();
  5355. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5356. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5357. $result = $TestModel->saveAssociated(array(
  5358. 'Article' => array('id' => 2),
  5359. 'Comment' => array(
  5360. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  5361. )
  5362. ), array('validate' => true));
  5363. $this->assertFalse($result);
  5364. $expected = array('Comment' => array(
  5365. array('comment' => array('This field cannot be left blank'))
  5366. ));
  5367. $this->assertEquals($expected, $TestModel->validationErrors);
  5368. $expected = array(
  5369. array('comment' => array('This field cannot be left blank'))
  5370. );
  5371. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5372. $result = $TestModel->saveAssociated(array(
  5373. 'Article' => array('id' => 2),
  5374. 'Comment' => array(
  5375. array(
  5376. 'comment' => '',
  5377. 'published' => 'Y',
  5378. 'user_id' => 1
  5379. ))
  5380. ), array('validate' => 'first'));
  5381. $this->assertFalse($result);
  5382. }
  5383. /**
  5384. * test saveMany with transactions and ensure there is no missing rollback.
  5385. *
  5386. * @return void
  5387. */
  5388. public function testSaveManyTransactionNoRollback() {
  5389. $this->loadFixtures('Post');
  5390. $Post = new TestPost();
  5391. $Post->validate = array(
  5392. 'title' => array('rule' => array('notEmpty'))
  5393. );
  5394. // If validation error occurs, rollback() should be called.
  5395. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5396. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5397. $db->expects($this->never())->method('commit');
  5398. $db->expects($this->once())->method('rollback');
  5399. $Post->setDataSourceObject($db);
  5400. $data = array(
  5401. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5402. array('author_id' => 1, 'title' => '')
  5403. );
  5404. $Post->saveMany($data, array('validate' => true));
  5405. // If exception thrown, rollback() should be called too.
  5406. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5407. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5408. $db->expects($this->never())->method('commit');
  5409. $db->expects($this->once())->method('rollback');
  5410. $Post->setDataSourceObject($db);
  5411. $data = array(
  5412. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5413. array('author_id' => 1, 'title' => 'New Fifth Post', 'body' => $db->expression('PDO_EXCEPTION()'))
  5414. );
  5415. try {
  5416. $Post->saveMany($data, array('validate' => true));
  5417. $this->fail('No exception thrown');
  5418. } catch (PDOException $e) {
  5419. }
  5420. // Otherwise, commit() should be called.
  5421. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5422. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5423. $db->expects($this->once())->method('commit');
  5424. $db->expects($this->never())->method('rollback');
  5425. $Post->setDataSourceObject($db);
  5426. $data = array(
  5427. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5428. array('author_id' => 1, 'title' => 'New Fifth Post')
  5429. );
  5430. $Post->saveMany($data, array('validate' => true));
  5431. }
  5432. /**
  5433. * test saveAssociated with transactions and ensure there is no missing rollback.
  5434. *
  5435. * @return void
  5436. */
  5437. public function testSaveAssociatedTransactionNoRollback() {
  5438. $this->loadFixtures('Post', 'Author');
  5439. $Post = new TestPost();
  5440. $Post->Author->validate = array(
  5441. 'user' => array('rule' => array('notEmpty'))
  5442. );
  5443. // If validation error occurs, rollback() should be called.
  5444. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5445. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5446. $db->expects($this->never())->method('commit');
  5447. $db->expects($this->once())->method('rollback');
  5448. $Post->setDataSourceObject($db);
  5449. $Post->Author->setDataSourceObject($db);
  5450. $data = array(
  5451. 'Post' => array(
  5452. 'title' => 'New post',
  5453. 'body' => 'Content',
  5454. 'published' => 'Y'
  5455. ),
  5456. 'Author' => array(
  5457. 'user' => '',
  5458. 'password' => "sekret"
  5459. )
  5460. );
  5461. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5462. // If exception thrown, commit() should be called.
  5463. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5464. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5465. $db->expects($this->never())->method('commit');
  5466. $db->expects($this->once())->method('rollback');
  5467. $Post->setDataSourceObject($db);
  5468. $Post->Author->setDataSourceObject($db);
  5469. $data = array(
  5470. 'Post' => array(
  5471. 'title' => 'New post',
  5472. 'body' => $db->expression('PDO_EXCEPTION()'),
  5473. 'published' => 'Y'
  5474. ),
  5475. 'Author' => array(
  5476. 'user' => 'New user',
  5477. 'password' => "sekret"
  5478. )
  5479. );
  5480. try {
  5481. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5482. $this->fail('No exception thrown');
  5483. } catch (PDOException $e) {
  5484. }
  5485. // Otherwise, commit() should be called.
  5486. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  5487. $db->expects($this->once())->method('begin')->will($this->returnValue(true));
  5488. $db->expects($this->once())->method('commit');
  5489. $db->expects($this->never())->method('rollback');
  5490. $Post->setDataSourceObject($db);
  5491. $Post->Author->setDataSourceObject($db);
  5492. $data = array(
  5493. 'Post' => array(
  5494. 'title' => 'New post',
  5495. 'body' => 'Content',
  5496. 'published' => 'Y'
  5497. ),
  5498. 'Author' => array(
  5499. 'user' => 'New user',
  5500. 'password' => "sekret"
  5501. )
  5502. );
  5503. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5504. }
  5505. /**
  5506. * test saveMany with nested saveMany call.
  5507. *
  5508. * @return void
  5509. */
  5510. public function testSaveManyNestedSaveMany() {
  5511. $this->loadFixtures('Sample');
  5512. $TransactionManyTestModel = new TransactionManyTestModel();
  5513. $data = array(
  5514. array('apple_id' => 1, 'name' => 'sample5'),
  5515. );
  5516. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  5517. }
  5518. /**
  5519. * testSaveManyTransaction method
  5520. *
  5521. * @return void
  5522. */
  5523. public function testSaveManyTransaction() {
  5524. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5525. $TestModel = new Post();
  5526. $TestModel->validate = array('title' => 'notEmpty');
  5527. $data = array(
  5528. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5529. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5530. array('author_id' => 1, 'title' => '')
  5531. );
  5532. $this->assertFalse($TestModel->saveMany($data));
  5533. $result = $TestModel->find('all', array('recursive' => -1));
  5534. $expected = array(
  5535. array('Post' => array(
  5536. 'id' => '1',
  5537. 'author_id' => 1,
  5538. 'title' => 'First Post',
  5539. 'body' => 'First Post Body',
  5540. 'published' => 'Y',
  5541. 'created' => '2007-03-18 10:39:23',
  5542. 'updated' => '2007-03-18 10:41:31'
  5543. )),
  5544. array('Post' => array(
  5545. 'id' => '2',
  5546. 'author_id' => 3,
  5547. 'title' => 'Second Post',
  5548. 'body' => 'Second Post Body',
  5549. 'published' => 'Y',
  5550. 'created' => '2007-03-18 10:41:23',
  5551. 'updated' => '2007-03-18 10:43:31'
  5552. )),
  5553. array('Post' => array(
  5554. 'id' => '3',
  5555. 'author_id' => 1,
  5556. 'title' => 'Third Post',
  5557. 'body' => 'Third Post Body',
  5558. 'published' => 'Y',
  5559. 'created' => '2007-03-18 10:43:23',
  5560. 'updated' => '2007-03-18 10:45:31'
  5561. )));
  5562. if (count($result) != 3) {
  5563. // Database doesn't support transactions
  5564. $expected[] = array(
  5565. 'Post' => array(
  5566. 'id' => '4',
  5567. 'author_id' => 1,
  5568. 'title' => 'New Fourth Post',
  5569. 'body' => null,
  5570. 'published' => 'N'
  5571. ));
  5572. $expected[] = array(
  5573. 'Post' => array(
  5574. 'id' => '5',
  5575. 'author_id' => 1,
  5576. 'title' => 'New Fifth Post',
  5577. 'body' => null,
  5578. 'published' => 'N',
  5579. ));
  5580. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5581. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5582. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5583. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5584. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5585. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5586. $this->assertEquals($expected, $result);
  5587. // Skip the rest of the transactional tests
  5588. return;
  5589. }
  5590. $this->assertEquals($expected, $result);
  5591. $data = array(
  5592. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5593. array('author_id' => 1, 'title' => ''),
  5594. array('author_id' => 1, 'title' => 'New Sixth Post')
  5595. );
  5596. $this->assertFalse($TestModel->saveMany($data));
  5597. $result = $TestModel->find('all', array('recursive' => -1));
  5598. $expected = array(
  5599. array('Post' => array(
  5600. 'id' => '1',
  5601. 'author_id' => 1,
  5602. 'title' => 'First Post',
  5603. 'body' => 'First Post Body',
  5604. 'published' => 'Y',
  5605. 'created' => '2007-03-18 10:39:23',
  5606. 'updated' => '2007-03-18 10:41:31'
  5607. )),
  5608. array('Post' => array(
  5609. 'id' => '2',
  5610. 'author_id' => 3,
  5611. 'title' => 'Second Post',
  5612. 'body' => 'Second Post Body',
  5613. 'published' => 'Y',
  5614. 'created' => '2007-03-18 10:41:23',
  5615. 'updated' => '2007-03-18 10:43:31'
  5616. )),
  5617. array('Post' => array(
  5618. 'id' => '3',
  5619. 'author_id' => 1,
  5620. 'title' => 'Third Post',
  5621. 'body' => 'Third Post Body',
  5622. 'published' => 'Y',
  5623. 'created' => '2007-03-18 10:43:23',
  5624. 'updated' => '2007-03-18 10:45:31'
  5625. )));
  5626. if (count($result) != 3) {
  5627. // Database doesn't support transactions
  5628. $expected[] = array(
  5629. 'Post' => array(
  5630. 'id' => '4',
  5631. 'author_id' => 1,
  5632. 'title' => 'New Fourth Post',
  5633. 'body' => 'Third Post Body',
  5634. 'published' => 'N'
  5635. ));
  5636. $expected[] = array(
  5637. 'Post' => array(
  5638. 'id' => '5',
  5639. 'author_id' => 1,
  5640. 'title' => 'Third Post',
  5641. 'body' => 'Third Post Body',
  5642. 'published' => 'N'
  5643. ));
  5644. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5645. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5646. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5647. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5648. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5649. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5650. }
  5651. $this->assertEquals($expected, $result);
  5652. $TestModel->validate = array('title' => 'notEmpty');
  5653. $data = array(
  5654. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5655. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5656. array('author_id' => 1, 'title' => 'New Sixth Post')
  5657. );
  5658. $this->assertTrue($TestModel->saveMany($data));
  5659. $result = $TestModel->find('all', array(
  5660. 'recursive' => -1,
  5661. 'fields' => array('author_id', 'title', 'body', 'published'),
  5662. 'order' => array('Post.created' => 'ASC')
  5663. ));
  5664. $expected = array(
  5665. array('Post' => array(
  5666. 'author_id' => 1,
  5667. 'title' => 'First Post',
  5668. 'body' => 'First Post Body',
  5669. 'published' => 'Y'
  5670. )),
  5671. array('Post' => array(
  5672. 'author_id' => 3,
  5673. 'title' => 'Second Post',
  5674. 'body' => 'Second Post Body',
  5675. 'published' => 'Y'
  5676. )),
  5677. array('Post' => array(
  5678. 'author_id' => 1,
  5679. 'title' => 'Third Post',
  5680. 'body' => 'Third Post Body',
  5681. 'published' => 'Y'
  5682. )),
  5683. array('Post' => array(
  5684. 'author_id' => 1,
  5685. 'title' => 'New Fourth Post',
  5686. 'body' => '',
  5687. 'published' => 'N'
  5688. )),
  5689. array('Post' => array(
  5690. 'author_id' => 1,
  5691. 'title' => 'New Fifth Post',
  5692. 'body' => '',
  5693. 'published' => 'N'
  5694. )),
  5695. array('Post' => array(
  5696. 'author_id' => 1,
  5697. 'title' => 'New Sixth Post',
  5698. 'body' => '',
  5699. 'published' => 'N'
  5700. )));
  5701. $this->assertEquals($expected, $result);
  5702. }
  5703. /**
  5704. * testSaveManyValidation method
  5705. *
  5706. * @return void
  5707. */
  5708. public function testSaveManyValidation() {
  5709. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5710. $TestModel = new Post();
  5711. $data = array(
  5712. array(
  5713. 'id' => '1',
  5714. 'title' => 'Baleeted First Post',
  5715. 'body' => 'Baleeted!',
  5716. 'published' => 'N'
  5717. ),
  5718. array(
  5719. 'id' => '2',
  5720. 'title' => 'Just update the title'
  5721. ),
  5722. array(
  5723. 'title' => 'Creating a fourth post',
  5724. 'body' => 'Fourth post body',
  5725. 'author_id' => 2
  5726. ));
  5727. $this->assertTrue($TestModel->saveMany($data));
  5728. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5729. $expected = array(
  5730. array(
  5731. 'Post' => array(
  5732. 'id' => '1',
  5733. 'author_id' => '1',
  5734. 'title' => 'Baleeted First Post',
  5735. 'body' => 'Baleeted!',
  5736. 'published' => 'N',
  5737. 'created' => '2007-03-18 10:39:23'
  5738. )
  5739. ),
  5740. array(
  5741. 'Post' => array(
  5742. 'id' => '2',
  5743. 'author_id' => '3',
  5744. 'title' => 'Just update the title',
  5745. 'body' => 'Second Post Body',
  5746. 'published' => 'Y',
  5747. 'created' => '2007-03-18 10:41:23'
  5748. )
  5749. ),
  5750. array(
  5751. 'Post' => array(
  5752. 'id' => '3',
  5753. 'author_id' => '1',
  5754. 'title' => 'Third Post',
  5755. 'body' => 'Third Post Body',
  5756. 'published' => 'Y',
  5757. 'created' => '2007-03-18 10:43:23',
  5758. 'updated' => '2007-03-18 10:45:31'
  5759. )),
  5760. array(
  5761. 'Post' => array(
  5762. 'id' => '4',
  5763. 'author_id' => '2',
  5764. 'title' => 'Creating a fourth post',
  5765. 'body' => 'Fourth post body',
  5766. 'published' => 'N'
  5767. )
  5768. )
  5769. );
  5770. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  5771. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  5772. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5773. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5774. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  5775. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5776. $this->assertEquals($expected, $result);
  5777. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5778. $data = array(
  5779. array(
  5780. 'id' => '1',
  5781. 'title' => 'Un-Baleeted First Post',
  5782. 'body' => 'Not Baleeted!',
  5783. 'published' => 'Y'
  5784. ),
  5785. array(
  5786. 'id' => '2',
  5787. 'title' => '',
  5788. 'body' => 'Trying to get away with an empty title'
  5789. ));
  5790. $result = $TestModel->saveMany($data);
  5791. $this->assertFalse($result);
  5792. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5793. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5794. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  5795. if (!$transactionWorked) {
  5796. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  5797. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  5798. }
  5799. $this->assertEquals($errors, $TestModel->validationErrors);
  5800. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5801. $data = array(
  5802. array(
  5803. 'id' => '1',
  5804. 'title' => 'Un-Baleeted First Post',
  5805. 'body' => 'Not Baleeted!',
  5806. 'published' => 'Y'
  5807. ),
  5808. array(
  5809. 'id' => '2',
  5810. 'title' => '',
  5811. 'body' => 'Trying to get away with an empty title'
  5812. ));
  5813. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  5814. $this->assertEquals(array(true, false), $result);
  5815. $result = $TestModel->find('all', array(
  5816. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5817. 'recursive' => -1,
  5818. 'order' => 'Post.id ASC'
  5819. ));
  5820. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5821. $expected = array(
  5822. array(
  5823. 'Post' => array(
  5824. 'id' => '1',
  5825. 'author_id' => '1',
  5826. 'title' => 'Un-Baleeted First Post',
  5827. 'body' => 'Not Baleeted!',
  5828. 'published' => 'Y',
  5829. )),
  5830. array(
  5831. 'Post' => array(
  5832. 'id' => '2',
  5833. 'author_id' => '3',
  5834. 'title' => 'Just update the title',
  5835. 'body' => 'Second Post Body',
  5836. 'published' => 'Y',
  5837. )),
  5838. array(
  5839. 'Post' => array(
  5840. 'id' => '3',
  5841. 'author_id' => '1',
  5842. 'title' => 'Third Post',
  5843. 'body' => 'Third Post Body',
  5844. 'published' => 'Y',
  5845. )),
  5846. array(
  5847. 'Post' => array(
  5848. 'id' => '4',
  5849. 'author_id' => '2',
  5850. 'title' => 'Creating a fourth post',
  5851. 'body' => 'Fourth post body',
  5852. 'published' => 'N',
  5853. )));
  5854. $this->assertEquals($expected, $result);
  5855. $this->assertEquals($errors, $TestModel->validationErrors);
  5856. $data = array(
  5857. array(
  5858. 'id' => '1',
  5859. 'title' => 'Re-Baleeted First Post',
  5860. 'body' => 'Baleeted!',
  5861. 'published' => 'N'
  5862. ),
  5863. array(
  5864. 'id' => '2',
  5865. 'title' => '',
  5866. 'body' => 'Trying to get away with an empty title'
  5867. ));
  5868. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  5869. $result = $TestModel->find('all', array(
  5870. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5871. 'recursive' => -1,
  5872. 'order' => 'Post.id ASC'
  5873. ));
  5874. $this->assertEquals($expected, $result);
  5875. $this->assertEquals($errors, $TestModel->validationErrors);
  5876. }
  5877. /**
  5878. * testValidateMany method
  5879. *
  5880. * @return void
  5881. */
  5882. public function testValidateMany() {
  5883. $TestModel = new Article();
  5884. $TestModel->validate = array('title' => 'notEmpty');
  5885. $data = array(
  5886. 0 => array('title' => ''),
  5887. 1 => array('title' => 'title 1'),
  5888. 2 => array('title' => 'title 2'),
  5889. );
  5890. $result = $TestModel->validateMany($data);
  5891. $this->assertFalse($result);
  5892. $expected = array(
  5893. 0 => array('title' => array('This field cannot be left blank')),
  5894. );
  5895. $this->assertEquals($expected, $TestModel->validationErrors);
  5896. $data = array(
  5897. 0 => array('title' => 'title 0'),
  5898. 1 => array('title' => ''),
  5899. 2 => array('title' => 'title 2'),
  5900. );
  5901. $result = $TestModel->validateMany($data);
  5902. $this->assertFalse($result);
  5903. $expected = array(
  5904. 1 => array('title' => array('This field cannot be left blank')),
  5905. );
  5906. $this->assertEquals($expected, $TestModel->validationErrors);
  5907. }
  5908. /**
  5909. * testSaveAssociatedValidateFirst method
  5910. *
  5911. * @return void
  5912. */
  5913. public function testSaveAssociatedValidateFirst() {
  5914. $this->loadFixtures('Article', 'Comment', 'Attachment');
  5915. $model = new Article();
  5916. $model->deleteAll(true);
  5917. $model->Comment->validate = array('comment' => 'notEmpty');
  5918. $result = $model->saveAssociated(array(
  5919. 'Article' => array(
  5920. 'title' => 'Post with Author',
  5921. 'body' => 'This post will be saved author'
  5922. ),
  5923. 'Comment' => array(
  5924. array('comment' => 'First new comment'),
  5925. array('comment' => '')
  5926. )
  5927. ), array('validate' => 'first'));
  5928. $this->assertFalse($result);
  5929. $result = $model->find('all');
  5930. $this->assertSame(array(), $result);
  5931. $expected = array('Comment' => array(
  5932. 1 => array('comment' => array('This field cannot be left blank'))
  5933. ));
  5934. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  5935. $this->assertSame($model->Comment->find('count'), 0);
  5936. $result = $model->saveAssociated(
  5937. array(
  5938. 'Article' => array(
  5939. 'title' => 'Post with Author',
  5940. 'body' => 'This post will be saved with an author',
  5941. 'user_id' => 2
  5942. ),
  5943. 'Comment' => array(
  5944. array(
  5945. 'comment' => 'Only new comment',
  5946. 'user_id' => 2
  5947. ))),
  5948. array('validate' => 'first')
  5949. );
  5950. $this->assertTrue($result);
  5951. $result = $model->Comment->find('all');
  5952. $this->assertSame(count($result), 1);
  5953. $result = Hash::extract($result, '{n}.Comment.article_id');
  5954. $this->assertEquals(4, $result[0]);
  5955. $model->deleteAll(true);
  5956. $data = array(
  5957. 'Article' => array(
  5958. 'title' => 'Post with Author saveAlled from comment',
  5959. 'body' => 'This post will be saved with an author',
  5960. 'user_id' => 2
  5961. ),
  5962. 'Comment' => array(
  5963. 'comment' => 'Only new comment', 'user_id' => 2
  5964. ));
  5965. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  5966. $this->assertFalse(empty($result));
  5967. $result = $model->find('all');
  5968. $this->assertEquals(
  5969. 'Post with Author saveAlled from comment',
  5970. $result[0]['Article']['title']
  5971. );
  5972. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  5973. }
  5974. /**
  5975. * test saveMany()'s return is correct when using atomic = false and validate = first.
  5976. *
  5977. * @return void
  5978. */
  5979. public function testSaveManyValidateFirstAtomicFalse() {
  5980. $Something = new Something();
  5981. $invalidData = array(
  5982. array(
  5983. 'title' => 'foo',
  5984. 'body' => 'bar',
  5985. 'published' => 'baz',
  5986. ),
  5987. array(
  5988. 'body' => 3,
  5989. 'published' => 'sd',
  5990. ),
  5991. );
  5992. $Something->create();
  5993. $Something->validate = array(
  5994. 'title' => array(
  5995. 'rule' => 'alphaNumeric',
  5996. 'required' => true,
  5997. ),
  5998. 'body' => array(
  5999. 'rule' => 'alphaNumeric',
  6000. 'required' => true,
  6001. 'allowEmpty' => true,
  6002. ),
  6003. );
  6004. $result = $Something->saveMany($invalidData, array(
  6005. 'atomic' => false,
  6006. 'validate' => 'first',
  6007. ));
  6008. $expected = array(true, false);
  6009. $this->assertEquals($expected, $result);
  6010. $Something = new Something();
  6011. $validData = array(
  6012. array(
  6013. 'title' => 'title value',
  6014. 'body' => 'body value',
  6015. 'published' => 'baz',
  6016. ),
  6017. array(
  6018. 'title' => 'valid',
  6019. 'body' => 'this body',
  6020. 'published' => 'sd',
  6021. ),
  6022. );
  6023. $Something->create();
  6024. $result = $Something->saveMany($validData, array(
  6025. 'atomic' => false,
  6026. 'validate' => 'first',
  6027. ));
  6028. $expected = array(true, true);
  6029. $this->assertEquals($expected, $result);
  6030. }
  6031. /**
  6032. * testValidateAssociated method
  6033. *
  6034. * @return void
  6035. */
  6036. public function testValidateAssociated() {
  6037. $this->loadFixtures('Attachment', 'Article', 'Comment');
  6038. $TestModel = new Comment();
  6039. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  6040. $data = array(
  6041. 'Comment' => array(
  6042. 'comment' => 'This is the comment'
  6043. ),
  6044. 'Attachment' => array(
  6045. 'attachment' => ''
  6046. )
  6047. );
  6048. $result = $TestModel->validateAssociated($data);
  6049. $this->assertFalse($result);
  6050. $TestModel = new Article();
  6051. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6052. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  6053. $data = array(
  6054. 'Article' => array('id' => 2),
  6055. 'Comment' => array(
  6056. array(
  6057. 'id' => 1,
  6058. 'comment' => '',
  6059. 'published' => 'Y',
  6060. 'user_id' => 1),
  6061. array(
  6062. 'id' => 2,
  6063. 'comment' =>
  6064. 'comment',
  6065. 'published' => 'Y',
  6066. 'user_id' => 1
  6067. )));
  6068. $result = $TestModel->validateAssociated($data);
  6069. $this->assertFalse($result);
  6070. $data = array(
  6071. 'Article' => array('id' => 2),
  6072. 'Comment' => array(
  6073. array(
  6074. 'id' => 1,
  6075. 'comment' => '',
  6076. 'published' => 'Y',
  6077. 'user_id' => 1
  6078. ),
  6079. array(
  6080. 'id' => 2,
  6081. 'comment' => 'comment',
  6082. 'published' => 'Y',
  6083. 'user_id' => 1
  6084. ),
  6085. array(
  6086. 'id' => 3,
  6087. 'comment' => '',
  6088. 'published' => 'Y',
  6089. 'user_id' => 1
  6090. )));
  6091. $result = $TestModel->validateAssociated($data, array(
  6092. 'atomic' => false
  6093. ));
  6094. $expected = array(
  6095. 'Article' => true,
  6096. 'Comment' => array(false, true, false)
  6097. );
  6098. $this->assertSame($expected, $result);
  6099. $expected = array('Comment' => array(
  6100. 0 => array('comment' => array('This field cannot be left blank')),
  6101. 2 => array('comment' => array('This field cannot be left blank'))
  6102. ));
  6103. $this->assertEquals($expected, $TestModel->validationErrors);
  6104. $expected = array(
  6105. 0 => array('comment' => array('This field cannot be left blank')),
  6106. 2 => array('comment' => array('This field cannot be left blank'))
  6107. );
  6108. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  6109. }
  6110. /**
  6111. * test that saveMany behaves like plain save() when suplied empty data
  6112. *
  6113. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  6114. * @return void
  6115. */
  6116. public function testSaveManyEmptyData() {
  6117. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  6118. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  6119. $model = new Article();
  6120. $result = $model->saveMany(array(), array('validate' => true));
  6121. $this->assertFalse(empty($result));
  6122. $model = new ProductUpdateAll();
  6123. $result = $model->saveMany(array());
  6124. $this->assertFalse($result);
  6125. }
  6126. /**
  6127. * test that saveAssociated behaves like plain save() when supplied empty data
  6128. *
  6129. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  6130. * @return void
  6131. */
  6132. public function testSaveAssociatedEmptyData() {
  6133. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  6134. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  6135. $model = new Article();
  6136. $result = $model->saveAssociated(array(), array('validate' => true));
  6137. $this->assertFalse(empty($result));
  6138. $model = new ProductUpdateAll();
  6139. $result = $model->saveAssociated(array());
  6140. $this->assertFalse($result);
  6141. }
  6142. /**
  6143. * Test that saveAssociated will accept expression object values when saving.
  6144. *
  6145. * @return void
  6146. */
  6147. public function testSaveAssociatedExpressionObjects() {
  6148. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  6149. $TestModel = new Post();
  6150. $db = $TestModel->getDataSource();
  6151. $TestModel->saveAssociated(array(
  6152. 'Post' => array(
  6153. 'title' => $db->expression("(SELECT 'Post with Author')"),
  6154. 'body' => 'This post will be saved with an author'
  6155. ),
  6156. 'Author' => array(
  6157. 'user' => 'bob',
  6158. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  6159. )), array('atomic' => false));
  6160. $result = $TestModel->find('first', array(
  6161. 'order' => array('Post.id ' => 'DESC')
  6162. ));
  6163. $this->assertEquals('Post with Author', $result['Post']['title']);
  6164. }
  6165. /**
  6166. * testUpdateWithCalculation method
  6167. *
  6168. * @return void
  6169. */
  6170. public function testUpdateWithCalculation() {
  6171. $this->loadFixtures('DataTest');
  6172. $model = new DataTest();
  6173. $model->deleteAll(true);
  6174. $result = $model->saveMany(array(
  6175. array('count' => 5, 'float' => 1.1),
  6176. array('count' => 3, 'float' => 1.2),
  6177. array('count' => 4, 'float' => 1.3),
  6178. array('count' => 1, 'float' => 2.0),
  6179. ));
  6180. $this->assertFalse(empty($result));
  6181. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  6182. $this->assertEquals(array(5, 3, 4, 1), $result);
  6183. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  6184. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  6185. $this->assertEquals(array(7, 5, 6, 3), $result);
  6186. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  6187. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  6188. $this->assertEquals(array(6, 4, 5, 2), $result);
  6189. }
  6190. /**
  6191. * testToggleBoolFields method
  6192. *
  6193. * @return void
  6194. */
  6195. public function testToggleBoolFields() {
  6196. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  6197. $Post = new CounterCachePost();
  6198. $Post->unbindModel(array('belongsTo' => array('User')), true);
  6199. $true = array('Post' => array('published' => true, 'id' => 2));
  6200. $false = array('Post' => array('published' => false, 'id' => 2));
  6201. $fields = array('Post.published', 'Post.id');
  6202. $updateConditions = array('Post.id' => 2);
  6203. // check its true
  6204. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  6205. $this->assertEquals($true, $result);
  6206. // Testing without the alias
  6207. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  6208. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  6209. $this->assertEquals($false, $result);
  6210. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  6211. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  6212. $this->assertEquals($true, $result);
  6213. $db = ConnectionManager::getDataSource('test');
  6214. $alias = $db->name('Post.published');
  6215. // Testing with the alias
  6216. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  6217. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  6218. $this->assertEquals($false, $result);
  6219. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  6220. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  6221. $this->assertEquals($true, $result);
  6222. }
  6223. /**
  6224. * TestFindAllWithoutForeignKey
  6225. *
  6226. * @return void
  6227. */
  6228. public function testFindAllForeignKey() {
  6229. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  6230. $ProductUpdateAll = new ProductUpdateAll();
  6231. $conditions = array('Group.name' => 'group one');
  6232. $ProductUpdateAll->bindModel(array(
  6233. 'belongsTo' => array(
  6234. 'Group' => array('className' => 'GroupUpdateAll')
  6235. )
  6236. ));
  6237. $ProductUpdateAll->belongsTo = array(
  6238. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  6239. );
  6240. $results = $ProductUpdateAll->find('all', compact('conditions'));
  6241. $this->assertTrue(!empty($results));
  6242. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  6243. $ProductUpdateAll->belongsTo = array(
  6244. 'Group' => array(
  6245. 'className' => 'GroupUpdateAll',
  6246. 'foreignKey' => false,
  6247. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  6248. ));
  6249. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  6250. $this->assertTrue(!empty($resultsFkFalse));
  6251. $expected = array(
  6252. '0' => array(
  6253. 'ProductUpdateAll' => array(
  6254. 'id' => 1,
  6255. 'name' => 'product one',
  6256. 'groupcode' => 120,
  6257. 'group_id' => 1),
  6258. 'Group' => array(
  6259. 'id' => 1,
  6260. 'name' => 'group one',
  6261. 'code' => 120)
  6262. ),
  6263. '1' => array(
  6264. 'ProductUpdateAll' => array(
  6265. 'id' => 2,
  6266. 'name' => 'product two',
  6267. 'groupcode' => 120,
  6268. 'group_id' => 1),
  6269. 'Group' => array(
  6270. 'id' => 1,
  6271. 'name' => 'group one',
  6272. 'code' => 120)
  6273. )
  6274. );
  6275. $this->assertEquals($expected, $results);
  6276. $this->assertEquals($expected, $resultsFkFalse);
  6277. }
  6278. /**
  6279. * test updateAll with empty values.
  6280. *
  6281. * @return void
  6282. */
  6283. public function testUpdateAllEmptyValues() {
  6284. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  6285. $this->loadFixtures('Author', 'Post');
  6286. $model = new Author();
  6287. $result = $model->updateAll(array('user' => '""'));
  6288. $this->assertTrue($result);
  6289. }
  6290. /**
  6291. * testUpdateAllWithJoins
  6292. *
  6293. * @return void
  6294. */
  6295. public function testUpdateAllWithJoins() {
  6296. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  6297. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  6298. $ProductUpdateAll = new ProductUpdateAll();
  6299. $conditions = array('Group.name' => 'group one');
  6300. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  6301. 'Group' => array('className' => 'GroupUpdateAll')))
  6302. );
  6303. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  6304. $results = $ProductUpdateAll->find('all', array(
  6305. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  6306. ));
  6307. $expected = array(
  6308. '0' => array(
  6309. 'ProductUpdateAll' => array(
  6310. 'id' => 1,
  6311. 'name' => 'new product',
  6312. 'groupcode' => 120,
  6313. 'group_id' => 1),
  6314. 'Group' => array(
  6315. 'id' => 1,
  6316. 'name' => 'group one',
  6317. 'code' => 120)
  6318. ),
  6319. '1' => array(
  6320. 'ProductUpdateAll' => array(
  6321. 'id' => 2,
  6322. 'name' => 'new product',
  6323. 'groupcode' => 120,
  6324. 'group_id' => 1),
  6325. 'Group' => array(
  6326. 'id' => 1,
  6327. 'name' => 'group one',
  6328. 'code' => 120)));
  6329. $this->assertEquals($expected, $results);
  6330. }
  6331. /**
  6332. * testUpdateAllWithoutForeignKey
  6333. *
  6334. * @return void
  6335. */
  6336. public function testUpdateAllWithoutForeignKey() {
  6337. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  6338. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  6339. $ProductUpdateAll = new ProductUpdateAll();
  6340. $conditions = array('Group.name' => 'group one');
  6341. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  6342. 'Group' => array('className' => 'GroupUpdateAll')
  6343. )));
  6344. $ProductUpdateAll->belongsTo = array(
  6345. 'Group' => array(
  6346. 'className' => 'GroupUpdateAll',
  6347. 'foreignKey' => false,
  6348. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  6349. )
  6350. );
  6351. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  6352. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  6353. $expected = array(
  6354. '0' => array(
  6355. 'ProductUpdateAll' => array(
  6356. 'id' => 1,
  6357. 'name' => 'new product',
  6358. 'groupcode' => 120,
  6359. 'group_id' => 1),
  6360. 'Group' => array(
  6361. 'id' => 1,
  6362. 'name' => 'group one',
  6363. 'code' => 120)
  6364. ),
  6365. '1' => array(
  6366. 'ProductUpdateAll' => array(
  6367. 'id' => 2,
  6368. 'name' => 'new product',
  6369. 'groupcode' => 120,
  6370. 'group_id' => 1),
  6371. 'Group' => array(
  6372. 'id' => 1,
  6373. 'name' => 'group one',
  6374. 'code' => 120)));
  6375. $this->assertEquals($expected, $resultsFkFalse);
  6376. }
  6377. /**
  6378. * test writing floats in german locale.
  6379. *
  6380. * @return void
  6381. */
  6382. public function testWriteFloatAsGerman() {
  6383. $restore = setlocale(LC_NUMERIC, 0);
  6384. $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
  6385. $model = new DataTest();
  6386. $result = $model->save(array(
  6387. 'count' => 1,
  6388. 'float' => 3.14593
  6389. ));
  6390. $this->assertTrue((bool)$result);
  6391. setlocale(LC_NUMERIC, $restore);
  6392. }
  6393. /**
  6394. * Test returned array contains primary key when save creates a new record
  6395. *
  6396. * @return void
  6397. */
  6398. public function testPkInReturnArrayForCreate() {
  6399. $this->loadFixtures('Article');
  6400. $TestModel = new Article();
  6401. $data = array('Article' => array(
  6402. 'user_id' => '1',
  6403. 'title' => 'Fourth Article',
  6404. 'body' => 'Fourth Article Body',
  6405. 'published' => 'Y'
  6406. ));
  6407. $result = $TestModel->save($data);
  6408. $this->assertSame($result['Article']['id'], $TestModel->id);
  6409. }
  6410. /**
  6411. * testSaveAllFieldListValidateBelongsTo
  6412. *
  6413. * @return void
  6414. */
  6415. public function testSaveAllFieldListValidateBelongsTo() {
  6416. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  6417. $TestModel = new Post();
  6418. $result = $TestModel->find('all');
  6419. $this->assertEquals(3, count($result));
  6420. $this->assertFalse(isset($result[3]));
  6421. // test belongsTo
  6422. $fieldList = array(
  6423. 'Post' => array('title'),
  6424. 'Author' => array('user')
  6425. );
  6426. $data = array(
  6427. 'Post' => array(
  6428. 'title' => 'Post without body',
  6429. 'body' => 'This will not be saved',
  6430. ),
  6431. 'Author' => array(
  6432. 'user' => 'bob',
  6433. 'test' => 'This will not be saved',
  6434. ));
  6435. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6436. $result = $TestModel->find('all', array(
  6437. 'order' => 'Post.id ASC',
  6438. ));
  6439. $expected = array(
  6440. 'Post' => array(
  6441. 'id' => '4',
  6442. 'author_id' => '5',
  6443. 'title' => 'Post without body',
  6444. 'body' => null,
  6445. 'published' => 'N',
  6446. 'created' => self::date(),
  6447. 'updated' => self::date(),
  6448. ),
  6449. 'Author' => array(
  6450. 'id' => '5',
  6451. 'user' => 'bob',
  6452. 'password' => null,
  6453. 'created' => self::date(),
  6454. 'updated' => self::date(),
  6455. 'test' => 'working',
  6456. ),
  6457. );
  6458. $this->assertEquals($expected, $result[3]);
  6459. $this->assertEquals(4, count($result));
  6460. $this->assertEquals('', $result[3]['Post']['body']);
  6461. $this->assertEquals('working', $result[3]['Author']['test']);
  6462. $fieldList = array(
  6463. 'Post' => array('title')
  6464. );
  6465. $data = array(
  6466. 'Post' => array(
  6467. 'title' => 'Post without body 2',
  6468. 'body' => 'This will not be saved'
  6469. ),
  6470. 'Author' => array(
  6471. 'user' => 'jack'
  6472. )
  6473. );
  6474. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6475. $result = $TestModel->find('all', array(
  6476. 'order' => 'Post.id ASC',
  6477. ));
  6478. $this->assertNull($result[4]['Post']['body']);
  6479. $fieldList = array(
  6480. 'Author' => array('password')
  6481. );
  6482. $data = array(
  6483. 'Post' => array(
  6484. 'id' => '5',
  6485. 'title' => 'Post title',
  6486. 'body' => 'Post body'
  6487. ),
  6488. 'Author' => array(
  6489. 'id' => '6',
  6490. 'user' => 'will not change',
  6491. 'password' => 'foobar'
  6492. )
  6493. );
  6494. $result = $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6495. $this->assertTrue($result);
  6496. $result = $TestModel->find('all', array(
  6497. 'order' => 'Post.id ASC',
  6498. ));
  6499. $expected = array(
  6500. 'Post' => array(
  6501. 'id' => '5',
  6502. 'author_id' => '6',
  6503. 'title' => 'Post title',
  6504. 'body' => 'Post body',
  6505. 'published' => 'N',
  6506. 'created' => self::date(),
  6507. 'updated' => self::date()
  6508. ),
  6509. 'Author' => array(
  6510. 'id' => '6',
  6511. 'user' => 'jack',
  6512. 'password' => 'foobar',
  6513. 'created' => self::date(),
  6514. 'updated' => self::date(),
  6515. 'test' => 'working'
  6516. ),
  6517. );
  6518. $this->assertEquals($expected, $result[4]);
  6519. // test multirecord
  6520. $this->db->truncate($TestModel);
  6521. $fieldList = array('title', 'author_id');
  6522. $TestModel->saveAll(array(
  6523. array(
  6524. 'title' => 'Multi-record post 1',
  6525. 'body' => 'First multi-record post',
  6526. 'author_id' => 2
  6527. ),
  6528. array(
  6529. 'title' => 'Multi-record post 2',
  6530. 'body' => 'Second multi-record post',
  6531. 'author_id' => 2
  6532. )), array('fieldList' => $fieldList));
  6533. $result = $TestModel->find('all', array(
  6534. 'recursive' => -1,
  6535. 'order' => 'Post.id ASC'
  6536. ));
  6537. $expected = array(
  6538. array(
  6539. 'Post' => array(
  6540. 'id' => '1',
  6541. 'author_id' => '2',
  6542. 'title' => 'Multi-record post 1',
  6543. 'body' => '',
  6544. 'published' => 'N',
  6545. 'created' => self::date(),
  6546. 'updated' => self::date()
  6547. )
  6548. ),
  6549. array(
  6550. 'Post' => array(
  6551. 'id' => '2',
  6552. 'author_id' => '2',
  6553. 'title' => 'Multi-record post 2',
  6554. 'body' => '',
  6555. 'published' => 'N',
  6556. 'created' => self::date(),
  6557. 'updated' => self::date()
  6558. )
  6559. )
  6560. );
  6561. $this->assertEquals($expected, $result);
  6562. }
  6563. /**
  6564. * testSaveAllFieldListHasMany method
  6565. *
  6566. * @return void
  6567. */
  6568. public function testSaveAllFieldListHasMany() {
  6569. $this->loadFixtures('Article', 'Comment');
  6570. $TestModel = new Article();
  6571. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6572. $this->db->truncate($TestModel);
  6573. $this->db->truncate(new Comment());
  6574. $data = array(
  6575. 'Article' => array('title' => 'I will not save'),
  6576. 'Comment' => array(
  6577. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6578. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  6579. )
  6580. );
  6581. $fieldList = array(
  6582. 'Article' => array('id'),
  6583. 'Comment' => array('article_id', 'user_id')
  6584. );
  6585. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6586. $result = $TestModel->find('all');
  6587. $this->assertEquals('', $result[0]['Article']['title']);
  6588. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  6589. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  6590. $fieldList = array(
  6591. 'Article' => array('id'),
  6592. 'Comment' => array('user_id')
  6593. );
  6594. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6595. $result = $TestModel->find('all');
  6596. $this->assertEquals('', $result[1]['Article']['title']);
  6597. $this->assertEquals(2, count($result[1]['Comment']));
  6598. $TestModel->whitelist = array('id');
  6599. $TestModel->Comment->whitelist = array('user_id');
  6600. $TestModel->saveAll($data);
  6601. $result = $TestModel->find('all');
  6602. $this->assertEquals('', $result[2]['Article']['title']);
  6603. $this->assertEquals(2, count($result[2]['Comment']));
  6604. }
  6605. /**
  6606. * testSaveAllFieldListHasOne method
  6607. *
  6608. * @return void
  6609. */
  6610. public function testSaveAllFieldListHasOne() {
  6611. $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
  6612. $TestModel = new Comment();
  6613. $TestModel->validate = array('comment' => 'notEmpty');
  6614. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  6615. $record = array(
  6616. 'Comment' => array(
  6617. 'user_id' => 1,
  6618. 'article_id' => 1,
  6619. 'comment' => '',
  6620. ),
  6621. 'Attachment' => array(
  6622. 'attachment' => ''
  6623. )
  6624. );
  6625. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  6626. $this->assertFalse($result);
  6627. $fieldList = array(
  6628. 'Comment' => array('id', 'article_id', 'user_id'),
  6629. 'Attachment' => array('comment_id')
  6630. );
  6631. $result = $TestModel->saveAll($record, array(
  6632. 'fieldList' => $fieldList, 'validate' => 'only'
  6633. ));
  6634. $this->assertTrue($result);
  6635. $this->assertEmpty($TestModel->validationErrors);
  6636. }
  6637. /**
  6638. * testSaveAllFieldListHasOneAddFkToWhitelist method
  6639. *
  6640. * @return void
  6641. */
  6642. public function testSaveAllFieldListHasOneAddFkToWhitelist() {
  6643. $this->loadFixtures('ArticleFeatured', 'Featured');
  6644. $Article = new ArticleFeatured();
  6645. $Article->belongsTo = $Article->hasMany = array();
  6646. $Article->Featured->validate = array('end_date' => 'notEmpty');
  6647. $record = array(
  6648. 'ArticleFeatured' => array(
  6649. 'user_id' => 1,
  6650. 'title' => 'First Article',
  6651. 'body' => '',
  6652. 'published' => 'Y'
  6653. ),
  6654. 'Featured' => array(
  6655. 'category_id' => 1,
  6656. 'end_date' => ''
  6657. )
  6658. );
  6659. $result = $Article->saveAll($record, array('validate' => 'only'));
  6660. $this->assertFalse($result);
  6661. $expected = array(
  6662. 'body' => array(
  6663. 'This field cannot be left blank'
  6664. ),
  6665. 'Featured' => array(
  6666. 'end_date' => array(
  6667. 'This field cannot be left blank'
  6668. )
  6669. )
  6670. );
  6671. $this->assertEquals($expected, $Article->validationErrors);
  6672. $fieldList = array(
  6673. 'ArticleFeatured' => array('user_id', 'title'),
  6674. 'Featured' => array('category_id')
  6675. );
  6676. $result = $Article->saveAll($record, array(
  6677. 'fieldList' => $fieldList, 'validate' => 'first'
  6678. ));
  6679. $this->assertTrue($result);
  6680. $this->assertEmpty($Article->validationErrors);
  6681. $Article->recursive = 0;
  6682. $result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
  6683. $this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
  6684. }
  6685. /**
  6686. * testSaveAllDeepFieldListValidateBelongsTo
  6687. *
  6688. * @return void
  6689. */
  6690. public function testSaveAllDeepFieldListValidateBelongsTo() {
  6691. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  6692. $TestModel = new Post();
  6693. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  6694. $TestModel->recursive = 2;
  6695. $result = $TestModel->find('all');
  6696. $this->assertEquals(3, count($result));
  6697. $this->assertFalse(isset($result[3]));
  6698. // test belongsTo
  6699. $fieldList = array(
  6700. 'Post' => array('title', 'author_id'),
  6701. 'Author' => array('user'),
  6702. 'Comment' => array('comment')
  6703. );
  6704. $TestModel->saveAll(array(
  6705. 'Post' => array(
  6706. 'title' => 'Post without body',
  6707. 'body' => 'This will not be saved',
  6708. ),
  6709. 'Author' => array(
  6710. 'user' => 'bob',
  6711. 'test' => 'This will not be saved',
  6712. 'Comment' => array(
  6713. array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
  6714. )), array('fieldList' => $fieldList, 'deep' => true));
  6715. $result = $TestModel->Author->Comment->find('first', array(
  6716. 'conditions' => array('Comment.id' => 5),
  6717. 'fields' => array('comment', 'published')
  6718. ));
  6719. $expected = array(
  6720. 'Comment' => array(
  6721. 'comment' => 'I am still published',
  6722. 'published' => 'Y'
  6723. )
  6724. );
  6725. $this->assertEquals($expected, $result);
  6726. }
  6727. /**
  6728. * testSaveAllDeepFieldListHasMany method
  6729. *
  6730. * @return void
  6731. */
  6732. public function testSaveAllDeepFieldListHasMany() {
  6733. $this->loadFixtures('Article', 'Comment', 'User');
  6734. $TestModel = new Article();
  6735. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6736. $this->db->truncate($TestModel);
  6737. $this->db->truncate(new Comment());
  6738. $fieldList = array(
  6739. 'Article' => array('id'),
  6740. 'Comment' => array('article_id', 'user_id'),
  6741. 'User' => array('user')
  6742. );
  6743. $result = $TestModel->saveAll(array(
  6744. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  6745. 'Comment' => array(
  6746. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6747. array(
  6748. 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
  6749. 'User' => array('user' => 'nopassword', 'password' => 'not saved')
  6750. )
  6751. )
  6752. ), array('fieldList' => $fieldList, 'deep' => true));
  6753. $result = $TestModel->Comment->User->find('first', array(
  6754. 'conditions' => array('User.user' => 'nopassword'),
  6755. 'fields' => array('user', 'password')
  6756. ));
  6757. $expected = array(
  6758. 'User' => array(
  6759. 'user' => 'nopassword',
  6760. 'password' => ''
  6761. )
  6762. );
  6763. $this->assertEquals($expected, $result);
  6764. }
  6765. /**
  6766. * testSaveAllDeepHasManyBelongsTo method
  6767. *
  6768. * @return void
  6769. */
  6770. public function testSaveAllDeepHasManyBelongsTo() {
  6771. $this->loadFixtures('Article', 'Comment', 'User');
  6772. $TestModel = new Article();
  6773. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6774. $this->db->truncate($TestModel);
  6775. $this->db->truncate(new Comment());
  6776. $result = $TestModel->saveAll(array(
  6777. 'Article' => array('id' => 2, 'title' => 'The title'),
  6778. 'Comment' => array(
  6779. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6780. array(
  6781. 'comment' => 'belongsto', 'published' => 'Y',
  6782. 'User' => array('user' => 'findme', 'password' => 'somepass')
  6783. )
  6784. )
  6785. ), array('deep' => true));
  6786. $result = $TestModel->Comment->User->find('first', array(
  6787. 'conditions' => array('User.user' => 'findme'),
  6788. 'fields' => array('id', 'user', 'password')
  6789. ));
  6790. $expected = array(
  6791. 'User' => array(
  6792. 'id' => 5,
  6793. 'user' => 'findme',
  6794. 'password' => 'somepass',
  6795. )
  6796. );
  6797. $this->assertEquals($expected, $result);
  6798. $result = $TestModel->Comment->find('first', array(
  6799. 'conditions' => array('Comment.user_id' => 5),
  6800. 'fields' => array('id', 'comment', 'published', 'user_id')
  6801. ));
  6802. $expected = array(
  6803. 'Comment' => array(
  6804. 'id' => 2,
  6805. 'comment' => 'belongsto',
  6806. 'published' => 'Y',
  6807. 'user_id' => 5
  6808. )
  6809. );
  6810. $this->assertEquals($expected, $result);
  6811. }
  6812. /**
  6813. * testSaveAllDeepHasManyhasMany method
  6814. *
  6815. * @return void
  6816. */
  6817. public function testSaveAllDeepHasManyHasMany() {
  6818. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6819. $TestModel = new Article();
  6820. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6821. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6822. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6823. $this->db->truncate($TestModel);
  6824. $this->db->truncate(new Comment());
  6825. $this->db->truncate(new Attachment());
  6826. $result = $TestModel->saveAll(array(
  6827. 'Article' => array('id' => 2, 'title' => 'The title'),
  6828. 'Comment' => array(
  6829. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6830. array(
  6831. 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
  6832. 'Attachment' => array(
  6833. array('attachment' => 'first deep attachment'),
  6834. array('attachment' => 'second deep attachment'),
  6835. )
  6836. )
  6837. )
  6838. ), array('deep' => true));
  6839. $result = $TestModel->Comment->find('first', array(
  6840. 'conditions' => array('Comment.comment' => 'hasmany'),
  6841. 'fields' => array('id', 'comment', 'published', 'user_id'),
  6842. 'recursive' => -1
  6843. ));
  6844. $expected = array(
  6845. 'Comment' => array(
  6846. 'id' => 2,
  6847. 'comment' => 'hasmany',
  6848. 'published' => 'Y',
  6849. 'user_id' => 5
  6850. )
  6851. );
  6852. $this->assertEquals($expected, $result);
  6853. $result = $TestModel->Comment->Attachment->find('all', array(
  6854. 'fields' => array('attachment', 'comment_id'),
  6855. 'order' => array('Attachment.id' => 'ASC')
  6856. ));
  6857. $expected = array(
  6858. array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
  6859. array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
  6860. );
  6861. $this->assertEquals($expected, $result);
  6862. }
  6863. /**
  6864. * testSaveAllDeepOrderHasManyHasMany method
  6865. *
  6866. * @return void
  6867. */
  6868. public function testSaveAllDeepOrderHasManyHasMany() {
  6869. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6870. $TestModel = new Article();
  6871. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6872. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6873. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6874. $this->db->truncate($TestModel);
  6875. $this->db->truncate(new Comment());
  6876. $this->db->truncate(new Attachment());
  6877. $result = $TestModel->saveAll(array(
  6878. 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
  6879. 'Comment' => array(
  6880. array(
  6881. 'Attachment' => array(
  6882. array('attachment' => 'attachment should be created with comment_id'),
  6883. array('attachment' => 'comment should be created with article_id'),
  6884. ),
  6885. 'comment' => 'after associated data',
  6886. 'user_id' => 1
  6887. )
  6888. )
  6889. ), array('deep' => true));
  6890. $result = $TestModel->Comment->find('first', array(
  6891. 'conditions' => array('Comment.article_id' => 2),
  6892. ));
  6893. $this->assertEquals(2, $result['Comment']['article_id']);
  6894. $this->assertEquals(2, count($result['Attachment']));
  6895. }
  6896. /**
  6897. * testSaveAllDeepEmptyHasManyHasMany method
  6898. *
  6899. * @return void
  6900. */
  6901. public function testSaveAllDeepEmptyHasManyHasMany() {
  6902. $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
  6903. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6904. $TestModel = new Article();
  6905. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6906. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6907. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6908. $this->db->truncate($TestModel);
  6909. $this->db->truncate(new Comment());
  6910. $this->db->truncate(new Attachment());
  6911. $result = $TestModel->saveAll(array(
  6912. 'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
  6913. 'Comment' => array(
  6914. array(
  6915. 'user_id' => 1,
  6916. 'Attachment' => array(
  6917. array('attachment' => 'attachment should be created with comment_id'),
  6918. array('attachment' => 'comment should be created with article_id'),
  6919. ),
  6920. )
  6921. )
  6922. ), array('deep' => true));
  6923. $result = $TestModel->Comment->find('first', array(
  6924. 'conditions' => array('Comment.article_id' => 3),
  6925. ));
  6926. $this->assertEquals(3, $result['Comment']['article_id']);
  6927. $this->assertEquals(2, count($result['Attachment']));
  6928. }
  6929. /**
  6930. * testUpdateAllBoolean
  6931. *
  6932. * @return void
  6933. */
  6934. public function testUpdateAllBoolean() {
  6935. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6936. $TestModel = new Item();
  6937. $result = $TestModel->updateAll(array('published' => true));
  6938. $this->assertTrue($result);
  6939. $result = $TestModel->find('first', array('fields' => array('id', 'published')));
  6940. $this->assertEquals(true, $result['Item']['published']);
  6941. }
  6942. /**
  6943. * testUpdateAllBooleanConditions
  6944. *
  6945. * @return void
  6946. */
  6947. public function testUpdateAllBooleanConditions() {
  6948. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6949. $TestModel = new Item();
  6950. $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
  6951. $this->assertTrue($result);
  6952. $result = $TestModel->find('first', array(
  6953. 'fields' => array('id', 'published'),
  6954. 'conditions' => array('Item.id' => 1)));
  6955. $this->assertEquals(true, $result['Item']['published']);
  6956. }
  6957. /**
  6958. * testUpdateBoolean
  6959. *
  6960. * @return void
  6961. */
  6962. public function testUpdateBoolean() {
  6963. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6964. $TestModel = new Item();
  6965. $result = $TestModel->save(array('published' => true, 'id' => 1));
  6966. $this->assertTrue((bool)$result);
  6967. $result = $TestModel->find('first', array(
  6968. 'fields' => array('id', 'published'),
  6969. 'conditions' => array('Item.id' => 1)));
  6970. $this->assertEquals(true, $result['Item']['published']);
  6971. }
  6972. /**
  6973. * Test the clear() method.
  6974. *
  6975. * @return void
  6976. */
  6977. public function testClear() {
  6978. $this->loadFixtures('Bid');
  6979. $model = ClassRegistry::init('Bid');
  6980. $model->set(array('name' => 'Testing', 'message_id' => 3));
  6981. $this->assertTrue(isset($model->data['Bid']['name']));
  6982. $this->assertTrue($model->clear());
  6983. $this->assertFalse(isset($model->data['Bid']['name']));
  6984. $this->assertFalse(isset($model->data['Bid']['message_id']));
  6985. }
  6986. /**
  6987. * Test that Model::save() doesn't generate a query with WHERE 1 = 1 on race condition.
  6988. *
  6989. * @link https://github.com/cakephp/cakephp/issues/3857
  6990. * @return void
  6991. */
  6992. public function testSafeUpdateMode() {
  6993. $this->loadFixtures('User');
  6994. $User = ClassRegistry::init('User');
  6995. $this->assertFalse($User->__safeUpdateMode);
  6996. $User->getEventManager()->attach(array($this, 'deleteMe'), 'Model.beforeSave');
  6997. $User->id = 1;
  6998. $User->set(array('user' => 'nobody'));
  6999. $User->save();
  7000. $users = $User->find('list', array('fields' => 'User.user'));
  7001. $expected = array(
  7002. 2 => 'nate',
  7003. 3 => 'larry',
  7004. 4 => 'garrett',
  7005. );
  7006. $this->assertEquals($expected, $users);
  7007. $this->assertFalse($User->__safeUpdateMode);
  7008. $User->id = 2;
  7009. $User->set(array('user' => $User->getDataSource()->expression('PDO_EXCEPTION()')));
  7010. try {
  7011. $User->save(null, false);
  7012. $this->fail('No exception thrown');
  7013. } catch (PDOException $e) {
  7014. $this->assertFalse($User->__safeUpdateMode);
  7015. }
  7016. }
  7017. /**
  7018. * Emulates race condition
  7019. *
  7020. * @param CakeEvent $event containing the Model
  7021. * @return void
  7022. */
  7023. public function deleteMe($event) {
  7024. $Model = $event->subject;
  7025. $Model->getDataSource()->delete($Model, array($Model->alias . '.' . $Model->primaryKey => $Model->id));
  7026. }
  7027. /**
  7028. * Creates a convenient mock DboSource
  7029. *
  7030. * We cannot call several methods via mock DboSource, such as DboSource::value()
  7031. * because mock DboSource has no $_connection.
  7032. * This method helps us to avoid this problem.
  7033. *
  7034. * @param array $methods Configurable method names.
  7035. * @return DboSource
  7036. */
  7037. protected function _getMockDboSource($methods = array()) {
  7038. $testDb = ConnectionManager::getDataSource('test');
  7039. $passthrough = array_diff(array('value', 'begin', 'rollback', 'commit', 'describe', 'lastInsertId', 'execute'), $methods);
  7040. $methods = array_merge($methods, $passthrough);
  7041. if (!in_array('connect', $methods)) {
  7042. $methods[] = 'connect'; // This will be called by DboSource::__construct().
  7043. }
  7044. $db = $this->getMock('DboSource', $methods);
  7045. $db->columns = $testDb->columns;
  7046. $db->startQuote = $testDb->startQuote;
  7047. $db->endQuote = $testDb->endQuote;
  7048. foreach ($passthrough as $method) {
  7049. $db->expects($this->any())
  7050. ->method($method)
  7051. ->will($this->returnCallback(array($testDb, $method)));
  7052. }
  7053. return $db;
  7054. }
  7055. /**
  7056. * Test that transactions behave correctly on nested saveMany calls.
  7057. *
  7058. * @return void
  7059. */
  7060. public function testTransactionOnNestedSaveMany() {
  7061. $this->loadFixtures('Post');
  7062. $Post = new TestPost();
  7063. $Post->getEventManager()->attach(array($this, 'nestedSaveMany'), 'Model.afterSave');
  7064. // begin -> [ begin -> commit ] -> commit
  7065. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  7066. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  7067. $db->expects($this->exactly(2))->method('commit');
  7068. $db->expects($this->never())->method('rollback');
  7069. $Post->setDataSourceObject($db);
  7070. $data = array(
  7071. array('author_id' => 1, 'title' => 'Outer Post'),
  7072. );
  7073. $Post->dataForAfterSave = array(
  7074. array('author_id' => 1, 'title' => 'Inner Post'),
  7075. );
  7076. $this->assertTrue($Post->saveMany($data));
  7077. // begin -> [ begin(false) ] -> commit
  7078. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  7079. $db->expects($this->at(0))->method('begin')->will($this->returnValue(true));
  7080. $db->expects($this->at(1))->method('begin')->will($this->returnValue(false));
  7081. $db->expects($this->once())->method('commit');
  7082. $db->expects($this->never())->method('rollback');
  7083. $Post->setDataSourceObject($db);
  7084. $data = array(
  7085. array('author_id' => 1, 'title' => 'Outer Post'),
  7086. );
  7087. $Post->dataForAfterSave = array(
  7088. array('author_id' => 1, 'title' => 'Inner Post'),
  7089. );
  7090. $this->assertTrue($Post->saveMany($data));
  7091. // begin -> [ begin -> rollback ] -> rollback
  7092. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  7093. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  7094. $db->expects($this->never())->method('commit');
  7095. $db->expects($this->exactly(2))->method('rollback');
  7096. $Post->setDataSourceObject($db);
  7097. $data = array(
  7098. array('author_id' => 1, 'title' => 'Outer Post'),
  7099. );
  7100. $Post->dataForAfterSave = array(
  7101. array('author_id' => 1, 'title' => 'Inner Post', 'body' => $db->expression('PDO_EXCEPTION()')),
  7102. );
  7103. try {
  7104. $Post->saveMany($data);
  7105. $this->fail('No exception thrown');
  7106. } catch(Exception $e) {
  7107. }
  7108. }
  7109. /**
  7110. * Test that transaction behaves correctly on nested saveAssociated calls.
  7111. *
  7112. * @return void
  7113. */
  7114. public function testTransactionOnNestedSaveAssociated() {
  7115. $this->loadFixtures('Author', 'Post');
  7116. $Author = new TestAuthor();
  7117. $Author->getEventManager()->attach(array($this, 'nestedSaveAssociated'), 'Model.afterSave');
  7118. // begin -> [ begin -> commit ] -> commit
  7119. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  7120. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  7121. $db->expects($this->exactly(2))->method('commit');
  7122. $db->expects($this->never())->method('rollback');
  7123. $Author->setDataSourceObject($db);
  7124. $Author->Post->setDataSourceObject($db);
  7125. $data = array(
  7126. 'Author' => array('user' => 'outer'),
  7127. 'Post' => array(
  7128. array('title' => 'Outer Post'),
  7129. )
  7130. );
  7131. $Author->dataForAfterSave = array(
  7132. 'Author' => array('user' => 'inner'),
  7133. 'Post' => array(
  7134. array('title' => 'Inner Post'),
  7135. )
  7136. );
  7137. $this->assertTrue($Author->saveAssociated($data));
  7138. // begin -> [ begin(false) ] -> commit
  7139. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  7140. $db->expects($this->at(0))->method('begin')->will($this->returnValue(true));
  7141. $db->expects($this->at(1))->method('begin')->will($this->returnValue(false));
  7142. $db->expects($this->once())->method('commit');
  7143. $db->expects($this->never())->method('rollback');
  7144. $Author->setDataSourceObject($db);
  7145. $Author->Post->setDataSourceObject($db);
  7146. $data = array(
  7147. 'Author' => array('user' => 'outer'),
  7148. 'Post' => array(
  7149. array('title' => 'Outer Post'),
  7150. )
  7151. );
  7152. $Author->dataForAfterSave = array(
  7153. 'Author' => array('user' => 'inner'),
  7154. 'Post' => array(
  7155. array('title' => 'Inner Post'),
  7156. )
  7157. );
  7158. $this->assertTrue($Author->saveAssociated($data));
  7159. // begin -> [ begin -> rollback ] -> rollback
  7160. $db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
  7161. $db->expects($this->exactly(2))->method('begin')->will($this->returnValue(true));
  7162. $db->expects($this->never())->method('commit');
  7163. $db->expects($this->exactly(2))->method('rollback');
  7164. $Author->setDataSourceObject($db);
  7165. $Author->Post->setDataSourceObject($db);
  7166. $data = array(
  7167. 'Author' => array('user' => 'outer'),
  7168. 'Post' => array(
  7169. array('title' => 'Outer Post'),
  7170. )
  7171. );
  7172. $Author->dataForAfterSave = array(
  7173. 'Author' => array('user' => 'inner', 'password' => $db->expression('PDO_EXCEPTION()')),
  7174. 'Post' => array(
  7175. array('title' => 'Inner Post'),
  7176. )
  7177. );
  7178. try {
  7179. $Author->saveAssociated($data);
  7180. $this->fail('No exception thrown');
  7181. } catch(Exception $e) {
  7182. }
  7183. }
  7184. /**
  7185. * A callback for testing nested saveMany.
  7186. *
  7187. * @param CakeEvent $event containing the Model
  7188. * @return void
  7189. */
  7190. public function nestedSaveMany($event) {
  7191. $Model = $event->subject;
  7192. $Model->saveMany($Model->dataForAfterSave, array('callbacks' => false));
  7193. }
  7194. /**
  7195. * A callback for testing nested saveAssociated.
  7196. *
  7197. * @param CakeEvent $event containing the Model
  7198. * @return void
  7199. */
  7200. public function nestedSaveAssociated($event) {
  7201. $Model = $event->subject;
  7202. $Model->saveAssociated($Model->dataForAfterSave, array('callbacks' => false));
  7203. }
  7204. }