PageRenderTime 89ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Test/Case/Model/ModelWriteTest.php

https://bitbucket.org/daveschwan/ronin-group
PHP | 7209 lines | 6007 code | 615 blank | 587 comment | 26 complexity | e57f7fb416a3ffb68e2cc4cd450e30a6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, Apache-2.0
  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. * ModelWriteTest
  25. *
  26. * @package Cake.Test.Case.Model
  27. */
  28. class ModelWriteTest extends BaseModelTest {
  29. /**
  30. * override locale to the default (eng).
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. Configure::write('Config.language', 'eng');
  37. }
  38. /**
  39. * Test save() failing when there is no data.
  40. *
  41. * @return void
  42. */
  43. public function testInsertNoData() {
  44. $this->loadFixtures('Bid');
  45. $Bid = ClassRegistry::init('Bid');
  46. $this->assertFalse($Bid->save());
  47. $result = $Bid->save(array('Bid' => array()));
  48. $this->assertFalse($result);
  49. $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
  50. $this->assertFalse($result);
  51. }
  52. /**
  53. * testInsertAnotherHabtmRecordWithSameForeignKey method
  54. *
  55. * @return void
  56. */
  57. public function testInsertAnotherHabtmRecordWithSameForeignKey() {
  58. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  59. $TestModel = new JoinA();
  60. $result = $TestModel->JoinAsJoinB->findById(1);
  61. $expected = array(
  62. 'JoinAsJoinB' => array(
  63. 'id' => 1,
  64. 'join_a_id' => 1,
  65. 'join_b_id' => 2,
  66. 'other' => 'Data for Join A 1 Join B 2',
  67. 'created' => '2008-01-03 10:56:33',
  68. 'updated' => '2008-01-03 10:56:33'
  69. ));
  70. $this->assertEquals($expected, $result);
  71. $TestModel->JoinAsJoinB->create();
  72. $data = array(
  73. 'join_a_id' => 1,
  74. 'join_b_id' => 1,
  75. 'other' => 'Data for Join A 1 Join B 1',
  76. 'created' => '2008-01-03 10:56:44',
  77. 'updated' => '2008-01-03 10:56:44'
  78. );
  79. $result = $TestModel->JoinAsJoinB->save($data);
  80. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  81. $data['id'] = $lastInsertId;
  82. $this->assertEquals(array('JoinAsJoinB' => $data), $result);
  83. $this->assertTrue($lastInsertId > 0);
  84. $result = $TestModel->JoinAsJoinB->findById(1);
  85. $expected = array(
  86. 'JoinAsJoinB' => array(
  87. 'id' => 1,
  88. 'join_a_id' => 1,
  89. 'join_b_id' => 2,
  90. 'other' => 'Data for Join A 1 Join B 2',
  91. 'created' => '2008-01-03 10:56:33',
  92. 'updated' => '2008-01-03 10:56:33'
  93. ));
  94. $this->assertEquals($expected, $result);
  95. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  96. $TestModel->JoinAsJoinB->id = 1;
  97. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  98. $this->assertFalse(empty($result));
  99. $result = $TestModel->JoinAsJoinB->findById(1);
  100. $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
  101. }
  102. /**
  103. * testSaveDateAsFirstEntry method
  104. *
  105. * @return void
  106. */
  107. public function testSaveDateAsFirstEntry() {
  108. $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
  109. $Article = new Article();
  110. $data = array(
  111. 'Article' => array(
  112. 'created' => array(
  113. 'day' => '1',
  114. 'month' => '1',
  115. 'year' => '2008'
  116. ),
  117. 'title' => 'Test Title',
  118. 'user_id' => 1
  119. ));
  120. $Article->create();
  121. $result = $Article->save($data);
  122. $this->assertFalse(empty($result));
  123. $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
  124. $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
  125. $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
  126. }
  127. /**
  128. * testUnderscoreFieldSave method
  129. *
  130. * @return void
  131. */
  132. public function testUnderscoreFieldSave() {
  133. $this->loadFixtures('UnderscoreField');
  134. $UnderscoreField = new UnderscoreField();
  135. $currentCount = $UnderscoreField->find('count');
  136. $this->assertEquals(3, $currentCount);
  137. $data = array('UnderscoreField' => array(
  138. 'user_id' => '1',
  139. 'my_model_has_a_field' => 'Content here',
  140. 'body' => 'Body',
  141. 'published' => 'Y',
  142. 'another_field' => 4
  143. ));
  144. $ret = $UnderscoreField->save($data);
  145. $this->assertFalse(empty($ret));
  146. $currentCount = $UnderscoreField->find('count');
  147. $this->assertEquals(4, $currentCount);
  148. }
  149. /**
  150. * testAutoSaveUuid method
  151. *
  152. * @return void
  153. */
  154. public function testAutoSaveUuid() {
  155. // SQLite does not support non-integer primary keys
  156. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  157. $this->loadFixtures('Uuid');
  158. $TestModel = new Uuid();
  159. $TestModel->save(array('title' => 'Test record'));
  160. $result = $TestModel->findByTitle('Test record');
  161. $this->assertEquals(
  162. array('id', 'title', 'count', 'created', 'updated'),
  163. array_keys($result['Uuid'])
  164. );
  165. $this->assertEquals(36, strlen($result['Uuid']['id']));
  166. }
  167. /**
  168. * Ensure that if the id key is null but present the save doesn't fail (with an
  169. * x sql error: "Column id specified twice")
  170. *
  171. * @return void
  172. */
  173. public function testSaveUuidNull() {
  174. // SQLite does not support non-integer primary keys
  175. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  176. $this->loadFixtures('Uuid');
  177. $TestModel = new Uuid();
  178. $TestModel->save(array('title' => 'Test record', 'id' => null));
  179. $result = $TestModel->findByTitle('Test record');
  180. $this->assertEquals(
  181. array('id', 'title', 'count', 'created', 'updated'),
  182. array_keys($result['Uuid'])
  183. );
  184. $this->assertEquals(36, strlen($result['Uuid']['id']));
  185. }
  186. /**
  187. * testZeroDefaultFieldValue method
  188. *
  189. * @return void
  190. */
  191. public function testZeroDefaultFieldValue() {
  192. $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
  193. $this->loadFixtures('DataTest');
  194. $TestModel = new DataTest();
  195. $TestModel->create(array());
  196. $TestModel->save();
  197. $result = $TestModel->findById($TestModel->id);
  198. $this->assertEquals(0, $result['DataTest']['count']);
  199. $this->assertEquals(0, $result['DataTest']['float']);
  200. }
  201. /**
  202. * Tests validation parameter order in custom validation methods
  203. *
  204. * @return void
  205. */
  206. public function testAllowSimulatedFields() {
  207. $TestModel = new ValidationTest1();
  208. $TestModel->create(array(
  209. 'title' => 'foo',
  210. 'bar' => 'baz'
  211. ));
  212. $expected = array(
  213. 'ValidationTest1' => array(
  214. 'title' => 'foo',
  215. 'bar' => 'baz'
  216. ));
  217. $this->assertEquals($expected, $TestModel->data);
  218. }
  219. /**
  220. * test that Caches are getting cleared on save().
  221. * ensure that both inflections of controller names are getting cleared
  222. * as URL for controller could be either overallFavorites/index or overall_favorites/index
  223. *
  224. * @return void
  225. */
  226. public function testCacheClearOnSave() {
  227. $_back = array(
  228. 'check' => Configure::read('Cache.check'),
  229. 'disable' => Configure::read('Cache.disable'),
  230. );
  231. Configure::write('Cache.check', true);
  232. Configure::write('Cache.disable', false);
  233. $this->loadFixtures('OverallFavorite');
  234. $OverallFavorite = new OverallFavorite();
  235. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  236. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  237. $data = array(
  238. 'OverallFavorite' => array(
  239. 'id' => 22,
  240. 'model_type' => '8-track',
  241. 'model_id' => '3',
  242. 'priority' => '1'
  243. )
  244. );
  245. $OverallFavorite->create($data);
  246. $OverallFavorite->save();
  247. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  248. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  249. Configure::write('Cache.check', $_back['check']);
  250. Configure::write('Cache.disable', $_back['disable']);
  251. }
  252. /**
  253. * test that save() resets whitelist on failed save
  254. */
  255. public function testSaveFieldListResetsWhitelistOnFailedSave() {
  256. $this->loadFixtures('Bidding');
  257. $model = new Bidding();
  258. $whitelist = array('title');
  259. $model->whitelist = $whitelist;
  260. $result = $model->save(
  261. array(),
  262. array('fieldList' => array('body'))
  263. );
  264. $this->assertFalse($result);
  265. $this->assertEquals($whitelist, $model->whitelist);
  266. }
  267. /**
  268. * testSaveWithCounterCache method
  269. *
  270. * @return void
  271. */
  272. public function testSaveWithCounterCache() {
  273. $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
  274. $TestModel = new Syfile();
  275. $TestModel2 = new Item();
  276. $result = $TestModel->findById(1);
  277. $this->assertNull($result['Syfile']['item_count']);
  278. $TestModel2->save(array(
  279. 'name' => 'Item 7',
  280. 'syfile_id' => 1,
  281. 'published' => false
  282. ));
  283. $result = $TestModel->findById(1);
  284. $this->assertEquals(2, $result['Syfile']['item_count']);
  285. $TestModel2->delete(1);
  286. $result = $TestModel->findById(1);
  287. $this->assertEquals(1, $result['Syfile']['item_count']);
  288. $TestModel2->id = 2;
  289. $TestModel2->saveField('syfile_id', 1);
  290. $result = $TestModel->findById(1);
  291. $this->assertEquals(2, $result['Syfile']['item_count']);
  292. $result = $TestModel->findById(2);
  293. $this->assertEquals(0, $result['Syfile']['item_count']);
  294. }
  295. /**
  296. * Tests that counter caches are updated when records are added
  297. *
  298. * @return void
  299. */
  300. public function testCounterCacheIncrease() {
  301. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  302. $User = new CounterCacheUser();
  303. $Post = new CounterCachePost();
  304. $data = array('Post' => array(
  305. 'id' => 22,
  306. 'title' => 'New Post',
  307. 'user_id' => 66
  308. ));
  309. $Post->save($data);
  310. $user = $User->find('first', array(
  311. 'conditions' => array('id' => 66),
  312. 'recursive' => -1
  313. ));
  314. $result = $user[$User->alias]['post_count'];
  315. $expected = 3;
  316. $this->assertEquals($expected, $result);
  317. }
  318. /**
  319. * Tests that counter caches are updated when records are deleted
  320. *
  321. * @return void
  322. */
  323. public function testCounterCacheDecrease() {
  324. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  325. $User = new CounterCacheUser();
  326. $Post = new CounterCachePost();
  327. $Post->delete(2);
  328. $user = $User->find('first', array(
  329. 'conditions' => array('id' => 66),
  330. 'recursive' => -1
  331. ));
  332. $result = $user[$User->alias]['post_count'];
  333. $expected = 1;
  334. $this->assertEquals($expected, $result);
  335. }
  336. /**
  337. * Tests that counter caches are updated when foreign keys of counted records change
  338. *
  339. * @return void
  340. */
  341. public function testCounterCacheUpdated() {
  342. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  343. $User = new CounterCacheUser();
  344. $Post = new CounterCachePost();
  345. $data = $Post->find('first', array(
  346. 'conditions' => array('id' => 1),
  347. 'recursive' => -1
  348. ));
  349. $data[$Post->alias]['user_id'] = 301;
  350. $Post->save($data);
  351. $users = $User->find('all', array('order' => 'User.id'));
  352. $this->assertEquals(1, $users[0]['User']['post_count']);
  353. $this->assertEquals(2, $users[1]['User']['post_count']);
  354. }
  355. /**
  356. * Test counter cache with models that use a non-standard (i.e. not using 'id')
  357. * as their primary key.
  358. *
  359. * @return void
  360. */
  361. public function testCounterCacheWithNonstandardPrimaryKey() {
  362. $this->loadFixtures(
  363. 'CounterCacheUserNonstandardPrimaryKey',
  364. 'CounterCachePostNonstandardPrimaryKey'
  365. );
  366. $User = new CounterCacheUserNonstandardPrimaryKey();
  367. $Post = new CounterCachePostNonstandardPrimaryKey();
  368. $data = $Post->find('first', array(
  369. 'conditions' => array('pid' => 1),
  370. 'recursive' => -1
  371. ));
  372. $data[$Post->alias]['uid'] = 301;
  373. $Post->save($data);
  374. $users = $User->find('all', array('order' => 'User.uid'));
  375. $this->assertEquals(1, $users[0]['User']['post_count']);
  376. $this->assertEquals(2, $users[1]['User']['post_count']);
  377. }
  378. /**
  379. * test Counter Cache With Self Joining table
  380. *
  381. * @return void
  382. */
  383. public function testCounterCacheWithSelfJoin() {
  384. $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
  385. $this->loadFixtures('CategoryThread');
  386. $column = 'COLUMN ';
  387. if ($this->db instanceof Sqlserver) {
  388. $column = '';
  389. }
  390. $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
  391. $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
  392. $this->db->flushMethodCache();
  393. $Category = new CategoryThread();
  394. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  395. $this->assertFalse(empty($result));
  396. $Category = new CategoryThread();
  397. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  398. $Category->updateCounterCache(array('parent_id' => 5));
  399. $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  400. $expected = array(1);
  401. $this->assertEquals($expected, $result);
  402. }
  403. /**
  404. * testSaveWithCounterCacheScope method
  405. *
  406. * @return void
  407. */
  408. public function testSaveWithCounterCacheScope() {
  409. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  410. $TestModel = new Syfile();
  411. $TestModel2 = new Item();
  412. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  413. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  414. $result = $TestModel->findById(1);
  415. $this->assertNull($result['Syfile']['item_count']);
  416. $TestModel2->save(array(
  417. 'name' => 'Item 7',
  418. 'syfile_id' => 1,
  419. 'published' => true
  420. ));
  421. $result = $TestModel->findById(1);
  422. $this->assertEquals(1, $result['Syfile']['item_count']);
  423. $TestModel2->id = 1;
  424. $TestModel2->saveField('published', true);
  425. $result = $TestModel->findById(1);
  426. $this->assertEquals(2, $result['Syfile']['item_count']);
  427. $TestModel2->save(array(
  428. 'id' => 1,
  429. 'syfile_id' => 1,
  430. 'published' => false
  431. ));
  432. $result = $TestModel->findById(1);
  433. $this->assertEquals(1, $result['Syfile']['item_count']);
  434. }
  435. /**
  436. * Tests having multiple counter caches for an associated model
  437. *
  438. * @return void
  439. */
  440. public function testCounterCacheMultipleCaches() {
  441. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  442. $User = new CounterCacheUser();
  443. $Post = new CounterCachePost();
  444. $Post->unbindModel(array('belongsTo' => array('User')), false);
  445. $Post->bindModel(array(
  446. 'belongsTo' => array(
  447. 'User' => array(
  448. 'className' => 'CounterCacheUser',
  449. 'foreignKey' => 'user_id',
  450. 'counterCache' => array(
  451. true,
  452. 'posts_published' => array('Post.published' => true)
  453. )
  454. )
  455. )
  456. ), false);
  457. // Count Increase
  458. $data = array('Post' => array(
  459. 'id' => 22,
  460. 'title' => 'New Post',
  461. 'user_id' => 66,
  462. 'published' => true
  463. ));
  464. $Post->save($data);
  465. $result = $User->find('first', array(
  466. 'conditions' => array('id' => 66),
  467. 'recursive' => -1
  468. ));
  469. $this->assertEquals(3, $result[$User->alias]['post_count']);
  470. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  471. // Count decrease
  472. $Post->delete(1);
  473. $result = $User->find('first', array(
  474. 'conditions' => array('id' => 66),
  475. 'recursive' => -1
  476. ));
  477. $this->assertEquals(2, $result[$User->alias]['post_count']);
  478. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  479. // Count update
  480. $data = $Post->find('first', array(
  481. 'conditions' => array('id' => 1),
  482. 'recursive' => -1
  483. ));
  484. $data[$Post->alias]['user_id'] = 301;
  485. $Post->save($data);
  486. $result = $User->find('all', array('order' => 'User.id'));
  487. $this->assertEquals(2, $result[0]['User']['post_count']);
  488. $this->assertEquals(1, $result[1]['User']['posts_published']);
  489. }
  490. /**
  491. * Tests that counter caches are unchanged when using 'counterCache' => false
  492. *
  493. * @return void
  494. */
  495. public function testCounterCacheSkip() {
  496. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  497. $User = new CounterCacheUser();
  498. $Post = new CounterCachePost();
  499. $data = $Post->find('first', array(
  500. 'conditions' => array('id' => 1),
  501. 'recursive' => -1
  502. ));
  503. $data[$Post->alias]['user_id'] = 301;
  504. $Post->save($data, array('counterCache' => false));
  505. $users = $User->find('all', array('order' => 'User.id'));
  506. $this->assertEquals(2, $users[0]['User']['post_count']);
  507. $this->assertEquals(1, $users[1]['User']['post_count']);
  508. }
  509. /**
  510. * test that beforeValidate returning false can abort saves.
  511. *
  512. * @return void
  513. */
  514. public function testBeforeValidateSaveAbortion() {
  515. $this->loadFixtures('Post');
  516. $Model = new CallbackPostTestModel();
  517. $Model->beforeValidateReturn = false;
  518. $data = array(
  519. 'title' => 'new article',
  520. 'body' => 'this is some text.'
  521. );
  522. $Model->create();
  523. $result = $Model->save($data);
  524. $this->assertFalse($result);
  525. }
  526. /**
  527. * test that beforeSave returning false can abort saves.
  528. *
  529. * @return void
  530. */
  531. public function testBeforeSaveSaveAbortion() {
  532. $this->loadFixtures('Post');
  533. $Model = new CallbackPostTestModel();
  534. $Model->beforeSaveReturn = false;
  535. $data = array(
  536. 'title' => 'new article',
  537. 'body' => 'this is some text.'
  538. );
  539. $Model->create();
  540. $result = $Model->save($data);
  541. $this->assertFalse($result);
  542. }
  543. /**
  544. * testSaveField method
  545. *
  546. * @return void
  547. */
  548. public function testSaveField() {
  549. $this->loadFixtures('Article');
  550. $TestModel = new Article();
  551. $TestModel->id = 1;
  552. $result = $TestModel->saveField('title', 'New First Article');
  553. $this->assertFalse(empty($result));
  554. $TestModel->recursive = -1;
  555. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  556. $expected = array('Article' => array(
  557. 'id' => '1',
  558. 'user_id' => '1',
  559. 'title' => 'New First Article',
  560. 'body' => 'First Article Body'
  561. ));
  562. $this->assertEquals($expected, $result);
  563. $TestModel->id = 1;
  564. $result = $TestModel->saveField('title', '');
  565. $this->assertFalse(empty($result));
  566. $TestModel->recursive = -1;
  567. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  568. $expected = array('Article' => array(
  569. 'id' => '1',
  570. 'user_id' => '1',
  571. 'title' => '',
  572. 'body' => 'First Article Body'
  573. ));
  574. $result['Article']['title'] = trim($result['Article']['title']);
  575. $this->assertEquals($expected, $result);
  576. $TestModel->id = 1;
  577. $TestModel->set('body', 'Messed up data');
  578. $result = $TestModel->saveField('title', 'First Article');
  579. $this->assertFalse(empty($result));
  580. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  581. $expected = array('Article' => array(
  582. 'id' => '1',
  583. 'user_id' => '1',
  584. 'title' => 'First Article',
  585. 'body' => 'First Article Body'
  586. ));
  587. $this->assertEquals($expected, $result);
  588. $TestModel->recursive = -1;
  589. $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  590. $TestModel->id = 1;
  591. $result = $TestModel->saveField('title', '', true);
  592. $this->assertFalse($result);
  593. $TestModel->recursive = -1;
  594. $TestModel->id = 1;
  595. $result = $TestModel->saveField('user_id', 9999);
  596. $this->assertTrue((bool)$result);
  597. $result = $TestModel->read(array('id', 'user_id'), 1);
  598. $expected = array('Article' => array(
  599. 'id' => '1',
  600. 'user_id' => '9999',
  601. ));
  602. $this->assertEquals($expected, $result);
  603. $this->loadFixtures('Node', 'Dependency');
  604. $Node = new Node();
  605. $Node->set('id', 1);
  606. $result = $Node->read();
  607. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  608. $Node->saveField('state', 10);
  609. $result = $Node->read();
  610. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  611. }
  612. /**
  613. * testSaveWithCreate method
  614. *
  615. * @return void
  616. */
  617. public function testSaveWithCreate() {
  618. $this->loadFixtures(
  619. 'User',
  620. 'Article',
  621. 'User',
  622. 'Comment',
  623. 'Tag',
  624. 'ArticlesTag',
  625. 'Attachment'
  626. );
  627. $TestModel = new User();
  628. $data = array('User' => array(
  629. 'user' => 'user',
  630. 'password' => ''
  631. ));
  632. $result = $TestModel->save($data);
  633. $this->assertFalse($result);
  634. $this->assertTrue(!empty($TestModel->validationErrors));
  635. $TestModel = new Article();
  636. $data = array('Article' => array(
  637. 'user_id' => '',
  638. 'title' => '',
  639. 'body' => ''
  640. ));
  641. $result = $TestModel->create($data) && $TestModel->save();
  642. $this->assertFalse($result);
  643. $this->assertTrue(!empty($TestModel->validationErrors));
  644. $data = array('Article' => array(
  645. 'id' => 1,
  646. 'user_id' => '1',
  647. 'title' => 'New First Article',
  648. 'body' => ''
  649. ));
  650. $result = $TestModel->create($data) && $TestModel->save();
  651. $this->assertFalse($result);
  652. $data = array('Article' => array(
  653. 'id' => 1,
  654. 'title' => 'New First Article'
  655. ));
  656. $result = $TestModel->create() && $TestModel->save($data, false);
  657. $this->assertFalse(empty($result));
  658. $TestModel->recursive = -1;
  659. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  660. $expected = array('Article' => array(
  661. 'id' => '1',
  662. 'user_id' => '1',
  663. 'title' => 'New First Article',
  664. 'body' => 'First Article Body',
  665. 'published' => 'N'
  666. ));
  667. $this->assertEquals($expected, $result);
  668. $data = array('Article' => array(
  669. 'id' => 1,
  670. 'user_id' => '2',
  671. 'title' => 'First Article',
  672. 'body' => 'New First Article Body',
  673. 'published' => 'Y'
  674. ));
  675. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  676. $this->assertFalse(empty($result));
  677. $TestModel->recursive = -1;
  678. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  679. $expected = array('Article' => array(
  680. 'id' => '1',
  681. 'user_id' => '1',
  682. 'title' => 'First Article',
  683. 'body' => 'First Article Body',
  684. 'published' => 'Y'
  685. ));
  686. $this->assertEquals($expected, $result);
  687. $data = array(
  688. 'Article' => array(
  689. 'user_id' => '2',
  690. 'title' => 'New Article',
  691. 'body' => 'New Article Body',
  692. 'created' => '2007-03-18 14:55:23',
  693. 'updated' => '2007-03-18 14:57:31'
  694. ),
  695. 'Tag' => array('Tag' => array(1, 3))
  696. );
  697. $TestModel->create();
  698. $result = $TestModel->create() && $TestModel->save($data);
  699. $this->assertFalse(empty($result));
  700. $TestModel->recursive = 2;
  701. $result = $TestModel->read(null, 4);
  702. $expected = array(
  703. 'Article' => array(
  704. 'id' => '4',
  705. 'user_id' => '2',
  706. 'title' => 'New Article',
  707. 'body' => 'New Article Body',
  708. 'published' => 'N',
  709. 'created' => '2007-03-18 14:55:23',
  710. 'updated' => '2007-03-18 14:57:31'
  711. ),
  712. 'User' => array(
  713. 'id' => '2',
  714. 'user' => 'nate',
  715. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  716. 'created' => '2007-03-17 01:18:23',
  717. 'updated' => '2007-03-17 01:20:31'
  718. ),
  719. 'Comment' => array(),
  720. 'Tag' => array(
  721. array(
  722. 'id' => '1',
  723. 'tag' => 'tag1',
  724. 'created' => '2007-03-18 12:22:23',
  725. 'updated' => '2007-03-18 12:24:31'
  726. ),
  727. array(
  728. 'id' => '3',
  729. 'tag' => 'tag3',
  730. 'created' => '2007-03-18 12:26:23',
  731. 'updated' => '2007-03-18 12:28:31'
  732. )));
  733. $this->assertEquals($expected, $result);
  734. $data = array('Comment' => array(
  735. 'article_id' => '4',
  736. 'user_id' => '1',
  737. 'comment' => 'Comment New Article',
  738. 'published' => 'Y',
  739. 'created' => '2007-03-18 14:57:23',
  740. 'updated' => '2007-03-18 14:59:31'
  741. ));
  742. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  743. $this->assertFalse(empty($result));
  744. $data = array('Attachment' => array(
  745. 'comment_id' => '7',
  746. 'attachment' => 'newattachment.zip',
  747. 'created' => '2007-03-18 15:02:23',
  748. 'updated' => '2007-03-18 15:04:31'
  749. ));
  750. $result = $TestModel->Comment->Attachment->save($data);
  751. $this->assertFalse(empty($result));
  752. $TestModel->recursive = 2;
  753. $result = $TestModel->read(null, 4);
  754. $expected = array(
  755. 'Article' => array(
  756. 'id' => '4',
  757. 'user_id' => '2',
  758. 'title' => 'New Article',
  759. 'body' => 'New Article Body',
  760. 'published' => 'N',
  761. 'created' => '2007-03-18 14:55:23',
  762. 'updated' => '2007-03-18 14:57:31'
  763. ),
  764. 'User' => array(
  765. 'id' => '2',
  766. 'user' => 'nate',
  767. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  768. 'created' => '2007-03-17 01:18:23',
  769. 'updated' => '2007-03-17 01:20:31'
  770. ),
  771. 'Comment' => array(
  772. array(
  773. 'id' => '7',
  774. 'article_id' => '4',
  775. 'user_id' => '1',
  776. 'comment' => 'Comment New Article',
  777. 'published' => 'Y',
  778. 'created' => '2007-03-18 14:57:23',
  779. 'updated' => '2007-03-18 14:59:31',
  780. 'Article' => array(
  781. 'id' => '4',
  782. 'user_id' => '2',
  783. 'title' => 'New Article',
  784. 'body' => 'New Article Body',
  785. 'published' => 'N',
  786. 'created' => '2007-03-18 14:55:23',
  787. 'updated' => '2007-03-18 14:57:31'
  788. ),
  789. 'User' => array(
  790. 'id' => '1',
  791. 'user' => 'mariano',
  792. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  793. 'created' => '2007-03-17 01:16:23',
  794. 'updated' => '2007-03-17 01:18:31'
  795. ),
  796. 'Attachment' => array(
  797. 'id' => '2',
  798. 'comment_id' => '7',
  799. 'attachment' => 'newattachment.zip',
  800. 'created' => '2007-03-18 15:02:23',
  801. 'updated' => '2007-03-18 15:04:31'
  802. ))),
  803. 'Tag' => array(
  804. array(
  805. 'id' => '1',
  806. 'tag' => 'tag1',
  807. 'created' => '2007-03-18 12:22:23',
  808. 'updated' => '2007-03-18 12:24:31'
  809. ),
  810. array(
  811. 'id' => '3',
  812. 'tag' => 'tag3',
  813. 'created' => '2007-03-18 12:26:23',
  814. 'updated' => '2007-03-18 12:28:31'
  815. )));
  816. $this->assertEquals($expected, $result);
  817. }
  818. /**
  819. * test that a null Id doesn't cause errors
  820. *
  821. * @return void
  822. */
  823. public function testSaveWithNullId() {
  824. $this->loadFixtures('User');
  825. $User = new User();
  826. $User->read(null, 1);
  827. $User->data['User']['id'] = null;
  828. $result = $User->save(array('password' => 'test'));
  829. $this->assertFalse(empty($result));
  830. $this->assertTrue($User->id > 0);
  831. $User->read(null, 2);
  832. $User->data['User']['id'] = null;
  833. $result = $User->save(array('password' => 'test'));
  834. $this->assertFalse(empty($result));
  835. $this->assertTrue($User->id > 0);
  836. $User->data['User'] = array('password' => 'something');
  837. $result = $User->save();
  838. $this->assertFalse(empty($result));
  839. $result = $User->read();
  840. $this->assertEquals('something', $User->data['User']['password']);
  841. }
  842. /**
  843. * testSaveWithSet method
  844. *
  845. * @return void
  846. */
  847. public function testSaveWithSet() {
  848. $this->loadFixtures('Article');
  849. $TestModel = new Article();
  850. // Create record we will be updating later
  851. $data = array('Article' => array(
  852. 'user_id' => '1',
  853. 'title' => 'Fourth Article',
  854. 'body' => 'Fourth Article Body',
  855. 'published' => 'Y'
  856. ));
  857. $result = $TestModel->create() && $TestModel->save($data);
  858. $this->assertFalse(empty($result));
  859. // Check record we created
  860. $TestModel->recursive = -1;
  861. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  862. $expected = array('Article' => array(
  863. 'id' => '4',
  864. 'user_id' => '1',
  865. 'title' => 'Fourth Article',
  866. 'body' => 'Fourth Article Body',
  867. 'published' => 'Y'
  868. ));
  869. $this->assertEquals($expected, $result);
  870. // Create new record just to overlap Model->id on previously created record
  871. $data = array('Article' => array(
  872. 'user_id' => '4',
  873. 'title' => 'Fifth Article',
  874. 'body' => 'Fifth Article Body',
  875. 'published' => 'Y'
  876. ));
  877. $result = $TestModel->create() && $TestModel->save($data);
  878. $this->assertFalse(empty($result));
  879. $TestModel->recursive = -1;
  880. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  881. $expected = array('Article' => array(
  882. 'id' => '5',
  883. 'user_id' => '4',
  884. 'title' => 'Fifth Article',
  885. 'body' => 'Fifth Article Body',
  886. 'published' => 'Y'
  887. ));
  888. $this->assertEquals($expected, $result);
  889. // Go back and edit the first article we created, starting by checking it's still there
  890. $TestModel->recursive = -1;
  891. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  892. $expected = array('Article' => array(
  893. 'id' => '4',
  894. 'user_id' => '1',
  895. 'title' => 'Fourth Article',
  896. 'body' => 'Fourth Article Body',
  897. 'published' => 'Y'
  898. ));
  899. $this->assertEquals($expected, $result);
  900. // And now do the update with set()
  901. $data = array('Article' => array(
  902. 'id' => '4',
  903. 'title' => 'Fourth Article - New Title',
  904. 'published' => 'N'
  905. ));
  906. $result = $TestModel->set($data) && $TestModel->save();
  907. $this->assertFalse(empty($result));
  908. $TestModel->recursive = -1;
  909. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  910. $expected = array('Article' => array(
  911. 'id' => '4',
  912. 'user_id' => '1',
  913. 'title' => 'Fourth Article - New Title',
  914. 'body' => 'Fourth Article Body',
  915. 'published' => 'N'
  916. ));
  917. $this->assertEquals($expected, $result);
  918. $TestModel->recursive = -1;
  919. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  920. $expected = array('Article' => array(
  921. 'id' => '5',
  922. 'user_id' => '4',
  923. 'title' => 'Fifth Article',
  924. 'body' => 'Fifth Article Body',
  925. 'published' => 'Y'
  926. ));
  927. $this->assertEquals($expected, $result);
  928. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  929. $result = ($TestModel->set($data) && $TestModel->save());
  930. $this->assertFalse(empty($result));
  931. $TestModel->recursive = -1;
  932. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  933. $expected = array('Article' => array(
  934. 'id' => '5',
  935. 'user_id' => '4',
  936. 'title' => 'Fifth Article - New Title 5',
  937. 'body' => 'Fifth Article Body',
  938. 'published' => 'Y'
  939. ));
  940. $this->assertEquals($expected, $result);
  941. $TestModel->recursive = -1;
  942. $result = $TestModel->find('all', array(
  943. 'fields' => array('id', 'title'),
  944. 'order' => array('Article.id' => 'ASC')
  945. ));
  946. $expected = array(
  947. array('Article' => array('id' => 1, 'title' => 'First Article')),
  948. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  949. array('Article' => array('id' => 3, 'title' => 'Third Article')),
  950. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
  951. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
  952. );
  953. $this->assertEquals($expected, $result);
  954. }
  955. /**
  956. * testSaveWithNonExistentFields method
  957. *
  958. * @return void
  959. */
  960. public function testSaveWithNonExistentFields() {
  961. $this->loadFixtures('Article');
  962. $TestModel = new Article();
  963. $TestModel->recursive = -1;
  964. $data = array(
  965. 'non_existent' => 'This field does not exist',
  966. 'user_id' => '1',
  967. 'title' => 'Fourth Article - New Title',
  968. 'body' => 'Fourth Article Body',
  969. 'published' => 'N'
  970. );
  971. $result = $TestModel->create() && $TestModel->save($data);
  972. $this->assertFalse(empty($result));
  973. $expected = array('Article' => array(
  974. 'id' => '4',
  975. 'user_id' => '1',
  976. 'title' => 'Fourth Article - New Title',
  977. 'body' => 'Fourth Article Body',
  978. 'published' => 'N'
  979. ));
  980. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  981. $this->assertEquals($expected, $result);
  982. $data = array(
  983. 'user_id' => '1',
  984. 'non_existent' => 'This field does not exist',
  985. 'title' => 'Fifth Article - New Title',
  986. 'body' => 'Fifth Article Body',
  987. 'published' => 'N'
  988. );
  989. $result = $TestModel->create() && $TestModel->save($data);
  990. $this->assertFalse(empty($result));
  991. $expected = array('Article' => array(
  992. 'id' => '5',
  993. 'user_id' => '1',
  994. 'title' => 'Fifth Article - New Title',
  995. 'body' => 'Fifth Article Body',
  996. 'published' => 'N'
  997. ));
  998. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  999. $this->assertEquals($expected, $result);
  1000. }
  1001. /**
  1002. * testSaveFromXml method
  1003. *
  1004. * @return void
  1005. */
  1006. public function testSaveFromXml() {
  1007. $this->markTestSkipped('This feature needs to be fixed or dropped');
  1008. $this->loadFixtures('Article');
  1009. App::uses('Xml', 'Utility');
  1010. $Article = new Article();
  1011. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  1012. $this->assertFalse(empty($result));
  1013. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  1014. $this->assertFalse(empty($results));
  1015. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  1016. $this->assertFalse(empty($result));
  1017. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  1018. $this->assertFalse(empty($results));
  1019. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  1020. $this->assertFalse(empty($result));
  1021. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  1022. $this->assertFalse(empty($results));
  1023. }
  1024. /**
  1025. * testSaveHabtm method
  1026. *
  1027. * @return void
  1028. */
  1029. public function testSaveHabtm() {
  1030. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1031. $TestModel = new Article();
  1032. $result = $TestModel->findById(2);
  1033. $expected = array(
  1034. 'Article' => array(
  1035. 'id' => '2',
  1036. 'user_id' => '3',
  1037. 'title' => 'Second Article',
  1038. 'body' => 'Second Article Body',
  1039. 'published' => 'Y',
  1040. 'created' => '2007-03-18 10:41:23',
  1041. 'updated' => '2007-03-18 10:43:31'
  1042. ),
  1043. 'User' => array(
  1044. 'id' => '3',
  1045. 'user' => 'larry',
  1046. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1047. 'created' => '2007-03-17 01:20:23',
  1048. 'updated' => '2007-03-17 01:22:31'
  1049. ),
  1050. 'Comment' => array(
  1051. array(
  1052. 'id' => '5',
  1053. 'article_id' => '2',
  1054. 'user_id' => '1',
  1055. 'comment' => 'First Comment for Second Article',
  1056. 'published' => 'Y',
  1057. 'created' => '2007-03-18 10:53:23',
  1058. 'updated' => '2007-03-18 10:55:31'
  1059. ),
  1060. array(
  1061. 'id' => '6',
  1062. 'article_id' => '2',
  1063. 'user_id' => '2',
  1064. 'comment' => 'Second Comment for Second Article',
  1065. 'published' => 'Y',
  1066. 'created' => '2007-03-18 10:55:23',
  1067. 'updated' => '2007-03-18 10:57:31'
  1068. )),
  1069. 'Tag' => array(
  1070. array(
  1071. 'id' => '1',
  1072. 'tag' => 'tag1',
  1073. 'created' => '2007-03-18 12:22:23',
  1074. 'updated' => '2007-03-18 12:24:31'
  1075. ),
  1076. array(
  1077. 'id' => '3',
  1078. 'tag' => 'tag3',
  1079. 'created' => '2007-03-18 12:26:23',
  1080. 'updated' => '2007-03-18 12:28:31'
  1081. )
  1082. )
  1083. );
  1084. $this->assertEquals($expected, $result);
  1085. $data = array(
  1086. 'Article' => array(
  1087. 'id' => '2',
  1088. 'title' => 'New Second Article'
  1089. ),
  1090. 'Tag' => array('Tag' => array(1, 2))
  1091. );
  1092. $result = $TestModel->set($data);
  1093. $this->assertFalse(empty($result));
  1094. $result = $TestModel->save();
  1095. $this->assertFalse(empty($result));
  1096. $this->assertEquals($data['Tag'], $result['Tag']);
  1097. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1098. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1099. $expected = array(
  1100. 'Article' => array(
  1101. 'id' => '2',
  1102. 'user_id' => '3',
  1103. 'title' => 'New Second Article',
  1104. 'body' => 'Second Article Body'
  1105. ),
  1106. 'Tag' => array(
  1107. array(
  1108. 'id' => '1',
  1109. 'tag' => 'tag1',
  1110. 'created' => '2007-03-18 12:22:23',
  1111. 'updated' => '2007-03-18 12:24:31'
  1112. ),
  1113. array(
  1114. 'id' => '2',
  1115. 'tag' => 'tag2',
  1116. 'created' => '2007-03-18 12:24:23',
  1117. 'updated' => '2007-03-18 12:26:31'
  1118. )));
  1119. $this->assertEquals($expected, $result);
  1120. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1121. $result = $TestModel->set($data);
  1122. $this->assertFalse(empty($result));
  1123. $result = $TestModel->save();
  1124. $this->assertFalse(empty($result));
  1125. $TestModel->unbindModel(array(
  1126. 'belongsTo' => array('User'),
  1127. 'hasMany' => array('Comment')
  1128. ));
  1129. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1130. $expected = array(
  1131. 'Article' => array(
  1132. 'id' => '2',
  1133. 'user_id' => '3',
  1134. 'title' => 'New Second Article',
  1135. 'body' => 'Second Article Body'
  1136. ),
  1137. 'Tag' => array(
  1138. array(
  1139. 'id' => '2',
  1140. 'tag' => 'tag2',
  1141. 'created' => '2007-03-18 12:24:23',
  1142. 'updated' => '2007-03-18 12:26:31'
  1143. ),
  1144. array(
  1145. 'id' => '3',
  1146. 'tag' => 'tag3',
  1147. 'created' => '2007-03-18 12:26:23',
  1148. 'updated' => '2007-03-18 12:28:31'
  1149. )));
  1150. $this->assertEquals($expected, $result);
  1151. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1152. $result = $TestModel->set($data);
  1153. $this->assertFalse(empty($result));
  1154. $result = $TestModel->save();
  1155. $this->assertFalse(empty($result));
  1156. $TestModel->unbindModel(array(
  1157. 'belongsTo' => array('User'),
  1158. 'hasMany' => array('Comment')
  1159. ));
  1160. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1161. $expected = array(
  1162. 'Article' => array(
  1163. 'id' => '2',
  1164. 'user_id' => '3',
  1165. 'title' => 'New Second Article',
  1166. 'body' => 'Second Article Body'
  1167. ),
  1168. 'Tag' => array(
  1169. array(
  1170. 'id' => '1',
  1171. 'tag' => 'tag1',
  1172. 'created' => '2007-03-18 12:22:23',
  1173. 'updated' => '2007-03-18 12:24:31'
  1174. ),
  1175. array(
  1176. 'id' => '2',
  1177. 'tag' => 'tag2',
  1178. 'created' => '2007-03-18 12:24:23',
  1179. 'updated' => '2007-03-18 12:26:31'
  1180. ),
  1181. array(
  1182. 'id' => '3',
  1183. 'tag' => 'tag3',
  1184. 'created' => '2007-03-18 12:26:23',
  1185. 'updated' => '2007-03-18 12:28:31'
  1186. )));
  1187. $this->assertEquals($expected, $result);
  1188. $data = array('Tag' => array('Tag' => array()));
  1189. $result = $TestModel->set($data);
  1190. $this->assertFalse(empty($result));
  1191. $result = $TestModel->save();
  1192. $this->assertFalse(empty($result));
  1193. $data = array('Tag' => array('Tag' => ''));
  1194. $result = $TestModel->set($data);
  1195. $this->assertFalse(empty($result));
  1196. $result = $TestModel->save();
  1197. $this->assertFalse(empty($result));
  1198. $TestModel->unbindModel(array(
  1199. 'belongsTo' => array('User'),
  1200. 'hasMany' => array('Comment')
  1201. ));
  1202. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1203. $expected = array(
  1204. 'Article' => array(
  1205. 'id' => '2',
  1206. 'user_id' => '3',
  1207. 'title' => 'New Second Article',
  1208. 'body' => 'Second Article Body'
  1209. ),
  1210. 'Tag' => array()
  1211. );
  1212. $this->assertEquals($expected, $result);
  1213. $data = array('Tag' => array('Tag' => array(2, 3)));
  1214. $result = $TestModel->set($data);
  1215. $this->assertFalse(empty($result));
  1216. $result = $TestModel->save();
  1217. $this->assertFalse(empty($result));
  1218. $TestModel->unbindModel(array(
  1219. 'belongsTo' => array('User'),
  1220. 'hasMany' => array('Comment')
  1221. ));
  1222. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1223. $expected = array(
  1224. 'Article' => array(
  1225. 'id' => '2',
  1226. 'user_id' => '3',
  1227. 'title' => 'New Second Article',
  1228. 'body' => 'Second Article Body'
  1229. ),
  1230. 'Tag' => array(
  1231. array(
  1232. 'id' => '2',
  1233. 'tag' => 'tag2',
  1234. 'created' => '2007-03-18 12:24:23',
  1235. 'updated' => '2007-03-18 12:26:31'
  1236. ),
  1237. array(
  1238. 'id' => '3',
  1239. 'tag' => 'tag3',
  1240. 'created' => '2007-03-18 12:26:23',
  1241. 'updated' => '2007-03-18 12:28:31'
  1242. )));
  1243. $this->assertEquals($expected, $result);
  1244. $data = array(
  1245. 'Tag' => array(
  1246. 'Tag' => array(1, 2)
  1247. ),
  1248. 'Article' => array(
  1249. 'id' => '2',
  1250. 'title' => 'New Second Article'
  1251. ));
  1252. $result = $TestModel->set($data);
  1253. $this->assertFalse(empty($result));
  1254. $result = $TestModel->save();
  1255. $this->assertFalse(empty($result));
  1256. $TestModel->unbindModel(array(
  1257. 'belongsTo' => array('User'),
  1258. 'hasMany' => array('Comment')
  1259. ));
  1260. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1261. $expected = array(
  1262. 'Article' => array(
  1263. 'id' => '2',
  1264. 'user_id' => '3',
  1265. 'title' => 'New Second Article',
  1266. 'body' => 'Second Article Body'
  1267. ),
  1268. 'Tag' => array(
  1269. array(
  1270. 'id' => '1',
  1271. 'tag' => 'tag1',
  1272. 'created' => '2007-03-18 12:22:23',
  1273. 'updated' => '2007-03-18 12:24:31'
  1274. ),
  1275. array(
  1276. 'id' => '2',
  1277. 'tag' => 'tag2',
  1278. 'created' => '2007-03-18 12:24:23',
  1279. 'updated' => '2007-03-18 12:26:31'
  1280. )));
  1281. $this->assertEquals($expected, $result);
  1282. $data = array(
  1283. 'Tag' => array(
  1284. 'Tag' => array(1, 2)
  1285. ),
  1286. 'Article' => array(
  1287. 'id' => '2',
  1288. 'title' => 'New Second Article Title'
  1289. ));
  1290. $result = $TestModel->set($data);
  1291. $this->assertFalse(empty($result));
  1292. $result = $TestModel->save();
  1293. $this->assertFalse(empty($result));
  1294. $TestModel->unbindModel(array(
  1295. 'belongsTo' => array('User'),
  1296. 'hasMany' => array('Comment')
  1297. ));
  1298. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1299. $expected = array(
  1300. 'Article' => array(
  1301. 'id' => '2',
  1302. 'user_id' => '3',
  1303. 'title' => 'New Second Article Title',
  1304. 'body' => 'Second Article Body'
  1305. ),
  1306. 'Tag' => array(
  1307. array(
  1308. 'id' => '1',
  1309. 'tag' => 'tag1',
  1310. 'created' => '2007-03-18 12:22:23',
  1311. 'updated' => '2007-03-18 12:24:31'
  1312. ),
  1313. array(
  1314. 'id' => '2',
  1315. 'tag' => 'tag2',
  1316. 'created' => '2007-03-18 12:24:23',
  1317. 'updated' => '2007-03-18 12:26:31'
  1318. )
  1319. )
  1320. );
  1321. $this->assertEquals($expected, $result);
  1322. $data = array(
  1323. 'Tag' => array(
  1324. 'Tag' => array(2, 3)
  1325. ),
  1326. 'Article' => array(
  1327. 'id' => '2',
  1328. 'title' => 'Changed Second Article'
  1329. ));
  1330. $result = $TestModel->set($data);
  1331. $this->assertFalse(empty($result));
  1332. $result = $TestModel->save();
  1333. $this->assertFalse(empty($result));
  1334. $TestModel->unbindModel(array(
  1335. 'belongsTo' => array('User'),
  1336. 'hasMany' => array('Comment')
  1337. ));
  1338. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1339. $expected = array(
  1340. 'Article' => array(
  1341. 'id' => '2',
  1342. 'user_id' => '3',
  1343. 'title' => 'Changed Second Article',
  1344. 'body' => 'Second Article Body'
  1345. ),
  1346. 'Tag' => array(
  1347. array(
  1348. 'id' => '2',
  1349. 'tag' => 'tag2',
  1350. 'created' => '2007-03-18 12:24:23',
  1351. 'updated' => '2007-03-18 12:26:31'
  1352. ),
  1353. array(
  1354. 'id' => '3',
  1355. 'tag' => 'tag3',
  1356. 'created' => '2007-03-18 12:26:23',
  1357. 'updated' => '2007-03-18 12:28:31'
  1358. )
  1359. )
  1360. );
  1361. $this->assertEquals($expected, $result);
  1362. $data = array(
  1363. 'Tag' => array(
  1364. 'Tag' => array(1, 3)
  1365. ),
  1366. 'Article' => array('id' => '2'),
  1367. );
  1368. $result = $TestModel->set($data);
  1369. $this->assertFalse(empty($result));
  1370. $result = $TestModel->save();
  1371. $this->assertFalse(empty($result));
  1372. $TestModel->unbindModel(array(
  1373. 'belongsTo' => array('User'),
  1374. 'hasMany' => array('Comment')
  1375. ));
  1376. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1377. $expected = array(
  1378. 'Article' => array(
  1379. 'id' => '2',
  1380. 'user_id' => '3',
  1381. 'title' => 'Changed Second Article',
  1382. 'body' => 'Second Article Body'
  1383. ),
  1384. 'Tag' => array(
  1385. array(
  1386. 'id' => '1',
  1387. 'tag' => 'tag1',
  1388. 'created' => '2007-03-18 12:22:23',
  1389. 'updated' => '2007-03-18 12:24:31'
  1390. ),
  1391. array(
  1392. 'id' => '3',
  1393. 'tag' => 'tag3',
  1394. 'created' => '2007-03-18 12:26:23',
  1395. 'updated' => '2007-03-18 12:28:31'
  1396. )));
  1397. $this->assertEquals($expected, $result);
  1398. $data = array(
  1399. 'Article' => array(
  1400. 'id' => 10,
  1401. 'user_id' => '2',
  1402. 'title' => 'New Article With Tags and fieldList',
  1403. 'body' => 'New Article Body with Tags and fieldList',
  1404. 'created' => '2007-03-18 14:55:23',
  1405. 'updated' => '2007-03-18 14:57:31'
  1406. ),
  1407. 'Tag' => array(
  1408. 'Tag' => array(1, 2, 3)
  1409. )
  1410. );
  1411. $result = $TestModel->create()
  1412. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1413. $this->assertFalse(empty($result));
  1414. $TestModel->unbindModel(array(
  1415. 'belongsTo' => array('User'),
  1416. 'hasMany' => array('Comment')
  1417. ));
  1418. $result = $TestModel->read();
  1419. $expected = array(
  1420. 'Article' => array(
  1421. 'id' => 4,
  1422. 'user_id' => 2,
  1423. 'title' => 'New Article With Tags and fieldList',
  1424. 'body' => '',
  1425. 'published' => 'N',
  1426. 'created' => '',
  1427. 'updated' => ''
  1428. ),
  1429. 'Tag' => array(
  1430. 0 => array(
  1431. 'id' => 1,
  1432. 'tag' => 'tag1',
  1433. 'created' => '2007-03-18 12:22:23',
  1434. 'updated' => '2007-03-18 12:24:31'
  1435. ),
  1436. 1 => array(
  1437. 'id' => 2,
  1438. 'tag' => 'tag2',
  1439. 'created' => '2007-03-18 12:24:23',
  1440. 'updated' => '2007-03-18 12:26:31'
  1441. ),
  1442. 2 => array(
  1443. 'id' => 3,
  1444. 'tag' => 'tag3',
  1445. 'created' => '2007-03-18 12:26:23',
  1446. 'updated' => '2007-03-18 12:28:31'
  1447. )));
  1448. $this->assertEquals($expected, $result);
  1449. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1450. $TestModel = new JoinA();
  1451. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1452. $data = array(
  1453. 'JoinA' => array(
  1454. 'id' => 1,
  1455. 'name' => 'Join A 1',
  1456. 'body' => 'Join A 1 Body',
  1457. ),
  1458. 'JoinC' => array(
  1459. 'JoinC' => array(
  1460. array('join_c_id' => 2, 'other' => 'new record'),
  1461. array('join_c_id' => 3, 'other' => 'new record')
  1462. )
  1463. )
  1464. );
  1465. $TestModel->save($data);
  1466. $result = $TestModel->read(null, 1);
  1467. $expected = array(4, 5);
  1468. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
  1469. $expected = array('new record', 'new record');
  1470. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
  1471. }
  1472. /**
  1473. * testSaveHabtmNoPrimaryData method
  1474. *
  1475. * @return void
  1476. */
  1477. public function testSaveHabtmNoPrimaryData() {
  1478. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1479. $TestModel = new Article();
  1480. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
  1481. $result = $TestModel->findById(2);
  1482. $expected = array(
  1483. 'Article' => array(
  1484. 'id' => '2',
  1485. 'user_id' => '3',
  1486. 'title' => 'Second Article',
  1487. 'body' => 'Second Article Body',
  1488. 'published' => 'Y',
  1489. 'created' => '2007-03-18 10:41:23',
  1490. 'updated' => '2007-03-18 10:43:31'
  1491. ),
  1492. 'Tag' => array(
  1493. array(
  1494. 'id' => '1',
  1495. 'tag' => 'tag1',
  1496. 'created' => '2007-03-18 12:22:23',
  1497. 'updated' => '2007-03-18 12:24:31'
  1498. ),
  1499. array(
  1500. 'id' => '3',
  1501. 'tag' => 'tag3',
  1502. 'created' => '2007-03-18 12:26:23',
  1503. 'updated' => '2007-03-18 12:28:31'
  1504. )
  1505. )
  1506. );
  1507. $this->assertEquals($expected, $result);
  1508. $TestModel->id = 2;
  1509. $data = array('Tag' => array('Tag' => array(2)));
  1510. $result = $TestModel->save($data);
  1511. $this->assertEquals($data['Tag'], $result['Tag']);
  1512. $result = $TestModel->findById(2);
  1513. $expected = array(
  1514. 'Article' => array(
  1515. 'id' => '2',
  1516. 'user_id' => '3',
  1517. 'title' => 'Second Article',
  1518. 'body' => 'Second Article Body',
  1519. 'published' => 'Y',
  1520. 'created' => '2007-03-18 10:41:23',
  1521. 'updated' => self::date()
  1522. ),
  1523. 'Tag' => array(
  1524. array(
  1525. 'id' => '2',
  1526. 'tag' => 'tag2',
  1527. 'created' => '2007-03-18 12:24:23',
  1528. 'updated' => '2007-03-18 12:26:31'
  1529. )
  1530. )
  1531. );
  1532. $this->assertEquals($expected, $result);
  1533. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1534. $TestModel = new Portfolio();
  1535. $result = $TestModel->findById(2);
  1536. $expected = array(
  1537. 'Portfolio' => array(
  1538. 'id' => 2,
  1539. 'seller_id' => 1,
  1540. 'name' => 'Portfolio 2'
  1541. ),
  1542. 'Item' => array(
  1543. array(
  1544. 'id' => 2,
  1545. 'syfile_id' => 2,
  1546. 'published' => '',
  1547. 'name' => 'Item 2',
  1548. 'ItemsPortfolio' => array(
  1549. 'id' => 2,
  1550. 'item_id' => 2,
  1551. 'portfolio_id' => 2
  1552. )
  1553. ),
  1554. array(
  1555. 'id' => 6,
  1556. 'syfile_id' => 6,
  1557. 'published' => '',
  1558. 'name' => 'Item 6',
  1559. 'ItemsPortfolio' => array(
  1560. 'id' => 6,
  1561. 'item_id' => 6,
  1562. 'portfolio_id' => 2
  1563. )
  1564. )
  1565. )
  1566. );
  1567. $this->assertEquals($expected, $result);
  1568. $data = array('Item' => array('Item' => array(1, 2)));
  1569. $TestModel->id = 2;
  1570. $TestModel->save($data);
  1571. $result = $TestModel->findById(2);
  1572. $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
  1573. $expected = array(
  1574. 'Portfolio' => array(
  1575. 'id' => 2,
  1576. 'seller_id' => 1,
  1577. 'name' => 'Portfolio 2'
  1578. ),
  1579. 'Item' => array(
  1580. array(
  1581. 'id' => 1,
  1582. 'syfile_id' => 1,
  1583. 'published' => '',
  1584. 'name' => 'Item 1',
  1585. 'ItemsPortfolio' => array(
  1586. 'id' => 7,
  1587. 'item_id' => 1,
  1588. 'portfolio_id' => 2
  1589. )
  1590. ),
  1591. array(
  1592. 'id' => 2,
  1593. 'syfile_id' => 2,
  1594. 'published' => '',
  1595. 'name' => 'Item 2',
  1596. 'ItemsPortfolio' => array(
  1597. 'id' => 8,
  1598. 'item_id' => 2,
  1599. 'portfolio_id' => 2
  1600. )
  1601. )
  1602. )
  1603. );
  1604. $this->assertEquals($expected, $result);
  1605. }
  1606. /**
  1607. * testSaveHabtmCustomKeys method
  1608. *
  1609. * @return void
  1610. */
  1611. public function testSaveHabtmCustomKeys() {
  1612. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1613. $Story = new Story();
  1614. $data = array(
  1615. 'Story' => array('story' => '1'),
  1616. 'Tag' => array(
  1617. 'Tag' => array(2, 3)
  1618. ));
  1619. $result = $Story->set($data);
  1620. $this->assertFalse(empty($result));
  1621. $result = $Story->save();
  1622. $this->assertFalse(empty($result));
  1623. $result = $Story->find('all', array('order' => array('Story.story')));
  1624. $expected = array(
  1625. array(
  1626. 'Story' => array(
  1627. 'story' => 1,
  1628. 'title' => 'First Story'
  1629. ),
  1630. 'Tag' => array(
  1631. array(
  1632. 'id' => 2,
  1633. 'tag' => 'tag2',
  1634. 'created' => '2007-03-18 12:24:23',
  1635. 'updated' => '2007-03-18 12:26:31'
  1636. ),
  1637. array(
  1638. 'id' => 3,
  1639. 'tag' => 'tag3',
  1640. 'created' => '2007-03-18 12:26:23',
  1641. 'updated' => '2007-03-18 12:28:31'
  1642. ))),
  1643. array(
  1644. 'Story' => array(
  1645. 'story' => 2,
  1646. 'title' => 'Second Story'
  1647. ),
  1648. 'Tag' => array()
  1649. ));
  1650. $this->assertEquals($expected, $result);
  1651. }
  1652. /**
  1653. * test that saving habtm records respects conditions set in the 'conditions' key
  1654. * for the association.
  1655. *
  1656. * @return void
  1657. */
  1658. public function testHabtmSaveWithConditionsInAssociation() {
  1659. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1660. $Something = new Something();
  1661. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1662. $Something->bindModel(array(
  1663. 'hasAndBelongsToMany' => array(
  1664. 'DoomedSomethingElse' => array(
  1665. 'className' => 'SomethingElse',
  1666. 'joinTable' => 'join_things',
  1667. 'conditions' => array('JoinThing.doomed' => true),
  1668. 'unique' => true
  1669. ),
  1670. 'NotDoomedSomethingElse' => array(
  1671. 'className' => 'SomethingElse',
  1672. 'joinTable' => 'join_things',
  1673. 'conditions' => array('JoinThing.doomed' => 0),
  1674. 'unique' => true
  1675. )
  1676. )
  1677. ), false);
  1678. $result = $Something->read(null, 1);
  1679. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1680. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1681. $data = array(
  1682. 'Something' => array('id' => 1),
  1683. 'NotDoomedSomethingElse' => array(
  1684. 'NotDoomedSomethingElse' => array(
  1685. array('something_else_id' => 2, 'doomed' => 0),
  1686. array('something_else_id' => 3, 'doomed' => 0)
  1687. )
  1688. )
  1689. );
  1690. $Something->create($data);
  1691. $result = $Something->save();
  1692. $this->assertFalse(empty($result));
  1693. $result = $Something->read(null, 1);
  1694. $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
  1695. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1696. }
  1697. /**
  1698. * testHabtmSaveKeyResolution method
  1699. *
  1700. * @return void
  1701. */
  1702. public function testHabtmSaveKeyResolution() {
  1703. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1704. $ThePaper = new ThePaper();
  1705. $ThePaper->id = 1;
  1706. $ThePaper->save(array('Monkey' => array(2, 3)));
  1707. $result = $ThePaper->findById(1);
  1708. $expected = array(
  1709. array(
  1710. 'id' => '2',
  1711. 'device_type_id' => '1',
  1712. 'name' => 'Device 2',
  1713. 'typ' => '1'
  1714. ),
  1715. array(
  1716. 'id' => '3',
  1717. 'device_type_id' => '1',
  1718. 'name' => 'Device 3',
  1719. 'typ' => '2'
  1720. ));
  1721. $this->assertEquals($expected, $result['Monkey']);
  1722. $ThePaper->id = 2;
  1723. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1724. $result = $ThePaper->findById(2);
  1725. $expected = array(
  1726. array(
  1727. 'id' => '1',
  1728. 'device_type_id' => '1',
  1729. 'name' => 'Device 1',
  1730. 'typ' => '1'
  1731. ),
  1732. array(
  1733. 'id' => '2',
  1734. 'device_type_id' => '1',
  1735. 'name' => 'Device 2',
  1736. 'typ' => '1'
  1737. ),
  1738. array(
  1739. 'id' => '3',
  1740. 'device_type_id' => '1',
  1741. 'name' => 'Device 3',
  1742. 'typ' => '2'
  1743. ));
  1744. $this->assertEquals($expected, $result['Monkey']);
  1745. $ThePaper->id = 2;
  1746. $ThePaper->save(array('Monkey' => array(1, 3)));
  1747. $result = $ThePaper->findById(2);
  1748. $expected = array(
  1749. array(
  1750. 'id' => '1',
  1751. 'device_type_id' => '1',
  1752. 'name' => 'Device 1',
  1753. 'typ' => '1'
  1754. ),
  1755. array(
  1756. 'id' => '3',
  1757. 'device_type_id' => '1',
  1758. 'name' => 'Device 3',
  1759. 'typ' => '2'
  1760. ));
  1761. $this->assertEquals($expected, $result['Monkey']);
  1762. $result = $ThePaper->findById(1);
  1763. $expected = array(
  1764. array(
  1765. 'id' => '2',
  1766. 'device_type_id' => '1',
  1767. 'name' => 'Device 2',
  1768. 'typ' => '1'
  1769. ),
  1770. array(
  1771. 'id' => '3',
  1772. 'device_type_id' => '1',
  1773. 'name' => 'Device 3',
  1774. 'typ' => '2'
  1775. ));
  1776. $this->assertEquals($expected, $result['Monkey']);
  1777. }
  1778. /**
  1779. * testCreationOfEmptyRecord method
  1780. *
  1781. * @return void
  1782. */
  1783. public function testCreationOfEmptyRecord() {
  1784. $this->loadFixtures('Author');
  1785. $TestModel = new Author();
  1786. $this->assertEquals(4, $TestModel->find('count'));
  1787. $TestModel->deleteAll(true, false, false);
  1788. $this->assertEquals(0, $TestModel->find('count'));
  1789. $result = $TestModel->save();
  1790. $this->assertTrue(isset($result['Author']['created']));
  1791. $this->assertTrue(isset($result['Author']['updated']));
  1792. $this->assertEquals(1, $TestModel->find('count'));
  1793. }
  1794. /**
  1795. * testCreateWithPKFiltering method
  1796. *
  1797. * @return void
  1798. */
  1799. public function testCreateWithPKFiltering() {
  1800. $TestModel = new Article();
  1801. $data = array(
  1802. 'id' => 5,
  1803. 'user_id' => 2,
  1804. 'title' => 'My article',
  1805. 'body' => 'Some text'
  1806. );
  1807. $result = $TestModel->create($data);
  1808. $expected = array(
  1809. 'Article' => array(
  1810. 'published' => 'N',
  1811. 'id' => 5,
  1812. 'user_id' => 2,
  1813. 'title' => 'My article',
  1814. 'body' => 'Some text'
  1815. ));
  1816. $this->assertEquals($expected, $result);
  1817. $this->assertEquals(5, $TestModel->id);
  1818. $result = $TestModel->create($data, true);
  1819. $expected = array(
  1820. 'Article' => array(
  1821. 'published' => 'N',
  1822. 'id' => false,
  1823. 'user_id' => 2,
  1824. 'title' => 'My article',
  1825. 'body' => 'Some text'
  1826. ));
  1827. $this->assertEquals($expected, $result);
  1828. $this->assertFalse($TestModel->id);
  1829. $result = $TestModel->create(array('Article' => $data), true);
  1830. $expected = array(
  1831. 'Article' => array(
  1832. 'published' => 'N',
  1833. 'id' => false,
  1834. 'user_id' => 2,
  1835. 'title' => 'My article',
  1836. 'body' => 'Some text'
  1837. ));
  1838. $this->assertEquals($expected, $result);
  1839. $this->assertFalse($TestModel->id);
  1840. $data = array(
  1841. 'id' => 6,
  1842. 'user_id' => 2,
  1843. 'title' => 'My article',
  1844. 'body' => 'Some text',
  1845. 'created' => '1970-01-01 00:00:00',
  1846. 'updated' => '1970-01-01 12:00:00',
  1847. 'modified' => '1970-01-01 12:00:00'
  1848. );
  1849. $result = $TestModel->create($data);
  1850. $expected = array(
  1851. 'Article' => array(
  1852. 'published' => 'N',
  1853. 'id' => 6,
  1854. 'user_id' => 2,
  1855. 'title' => 'My article',
  1856. 'body' => 'Some text',
  1857. 'created' => '1970-01-01 00:00:00',
  1858. 'updated' => '1970-01-01 12:00:00',
  1859. 'modified' => '1970-01-01 12:00:00'
  1860. ));
  1861. $this->assertEquals($expected, $result);
  1862. $this->assertEquals(6, $TestModel->id);
  1863. $result = $TestModel->create(array(
  1864. 'Article' => array_diff_key($data, array(
  1865. 'created' => true,
  1866. 'updated' => true,
  1867. 'modified' => true
  1868. ))), true);
  1869. $expected = array(
  1870. 'Article' => array(
  1871. 'published' => 'N',
  1872. 'id' => false,
  1873. 'user_id' => 2,
  1874. 'title' => 'My article',
  1875. 'body' => 'Some text'
  1876. ));
  1877. $this->assertEquals($expected, $result);
  1878. $this->assertFalse($TestModel->id);
  1879. }
  1880. /**
  1881. * testCreationWithMultipleData method
  1882. *
  1883. * @return void
  1884. */
  1885. public function testCreationWithMultipleData() {
  1886. $this->loadFixtures('Article', 'Comment');
  1887. $Article = new Article();
  1888. $Comment = new Comment();
  1889. $articles = $Article->find('all', array(
  1890. 'fields' => array('id', 'title'),
  1891. 'recursive' => -1,
  1892. 'order' => array('Article.id' => 'ASC')
  1893. ));
  1894. $expected = array(
  1895. array('Article' => array(
  1896. 'id' => 1,
  1897. 'title' => 'First Article'
  1898. )),
  1899. array('Article' => array(
  1900. 'id' => 2,
  1901. 'title' => 'Second Article'
  1902. )),
  1903. array('Article' => array(
  1904. 'id' => 3,
  1905. 'title' => 'Third Article'
  1906. )));
  1907. $this->assertEquals($expected, $articles);
  1908. $comments = $Comment->find('all', array(
  1909. 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
  1910. 'recursive' => -1,
  1911. 'order' => array('Comment.id' => 'ASC')
  1912. ));
  1913. $expected = array(
  1914. array('Comment' => array(
  1915. 'id' => 1,
  1916. 'article_id' => 1,
  1917. 'user_id' => 2,
  1918. 'comment' => 'First Comment for First Article',
  1919. 'published' => 'Y'
  1920. )),
  1921. array('Comment' => array(
  1922. 'id' => 2,
  1923. 'article_id' => 1,
  1924. 'user_id' => 4,
  1925. 'comment' => 'Second Comment for First Article',
  1926. 'published' => 'Y'
  1927. )),
  1928. array('Comment' => array(
  1929. 'id' => 3,
  1930. 'article_id' => 1,
  1931. 'user_id' => 1,
  1932. 'comment' => 'Third Comment for First Article',
  1933. 'published' => 'Y'
  1934. )),
  1935. array('Comment' => array(
  1936. 'id' => 4,
  1937. 'article_id' => 1,
  1938. 'user_id' => 1,
  1939. 'comment' => 'Fourth Comment for First Article',
  1940. 'published' => 'N'
  1941. )),
  1942. array('Comment' => array(
  1943. 'id' => 5,
  1944. 'article_id' => 2,
  1945. 'user_id' => 1,
  1946. 'comment' => 'First Comment for Second Article',
  1947. 'published' => 'Y'
  1948. )),
  1949. array('Comment' => array(
  1950. 'id' => 6,
  1951. 'article_id' => 2,
  1952. 'user_id' => 2,
  1953. 'comment' => 'Second Comment for Second Article',
  1954. 'published' => 'Y'
  1955. )));
  1956. $this->assertEquals($expected, $comments);
  1957. $data = array(
  1958. 'Comment' => array(
  1959. 'article_id' => 2,
  1960. 'user_id' => 4,
  1961. 'comment' => 'Brand New Comment',
  1962. 'published' => 'N'
  1963. ),
  1964. 'Article' => array(
  1965. 'id' => 2,
  1966. 'title' => 'Second Article Modified'
  1967. ));
  1968. $result = $Comment->create($data);
  1969. $this->assertFalse(empty($result));
  1970. $result = $Comment->save();
  1971. $this->assertFalse(empty($result));
  1972. $articles = $Article->find('all', array(
  1973. 'fields' => array('id', 'title'),
  1974. 'recursive' => -1,
  1975. 'order' => array('Article.id' => 'ASC')
  1976. ));
  1977. $expected = array(
  1978. array('Article' => array(
  1979. 'id' => 1,
  1980. 'title' => 'First Article'
  1981. )),
  1982. array('Article' => array(
  1983. 'id' => 2,
  1984. 'title' => 'Second Article'
  1985. )),
  1986. array('Article' => array(
  1987. 'id' => 3,
  1988. 'title' => 'Third Article'
  1989. )));
  1990. $this->assertEquals($expected, $articles);
  1991. $comments = $Comment->find('all', array(
  1992. 'fields' => array('id', 'article_id', 'user_id', 'comment', 'published'),
  1993. 'recursive' => -1,
  1994. 'order' => array('Comment.id' => 'ASC')
  1995. ));
  1996. $expected = array(
  1997. array('Comment' => array(
  1998. 'id' => 1,
  1999. 'article_id' => 1,
  2000. 'user_id' => 2,
  2001. 'comment' => 'First Comment for First Article',
  2002. 'published' => 'Y'
  2003. )),
  2004. array('Comment' => array(
  2005. 'id' => 2,
  2006. 'article_id' => 1,
  2007. 'user_id' => 4,
  2008. 'comment' => 'Second Comment for First Article',
  2009. 'published' => 'Y'
  2010. )),
  2011. array('Comment' => array(
  2012. 'id' => 3,
  2013. 'article_id' => 1,
  2014. 'user_id' => 1,
  2015. 'comment' => 'Third Comment for First Article',
  2016. 'published' => 'Y'
  2017. )),
  2018. array('Comment' => array(
  2019. 'id' => 4,
  2020. 'article_id' => 1,
  2021. 'user_id' => 1,
  2022. 'comment' => 'Fourth Comment for First Article',
  2023. 'published' => 'N'
  2024. )),
  2025. array('Comment' => array(
  2026. 'id' => 5,
  2027. 'article_id' => 2,
  2028. 'user_id' => 1,
  2029. 'comment' => 'First Comment for Second Article',
  2030. 'published' => 'Y'
  2031. )),
  2032. array('Comment' => array(
  2033. 'id' => 6,
  2034. 'article_id' => 2,
  2035. 'user_id' => 2, 'comment' =>
  2036. 'Second Comment for Second Article',
  2037. 'published' => 'Y'
  2038. )),
  2039. array('Comment' => array(
  2040. 'id' => 7,
  2041. 'article_id' => 2,
  2042. 'user_id' => 4,
  2043. 'comment' => 'Brand New Comment',
  2044. 'published' => 'N'
  2045. )));
  2046. $this->assertEquals($expected, $comments);
  2047. }
  2048. /**
  2049. * testCreationWithMultipleDataSameModel method
  2050. *
  2051. * @return void
  2052. */
  2053. public function testCreationWithMultipleDataSameModel() {
  2054. $this->loadFixtures('Article');
  2055. $Article = new Article();
  2056. $result = $Article->field('title', array('id' => 1));
  2057. $this->assertEquals('First Article', $result);
  2058. $data = array(
  2059. 'Article' => array(
  2060. 'user_id' => 2,
  2061. 'title' => 'Brand New Article',
  2062. 'body' => 'Brand New Article Body',
  2063. 'published' => 'Y'
  2064. ),
  2065. 'SecondaryArticle' => array(
  2066. 'id' => 1
  2067. ));
  2068. $Article->create();
  2069. $result = $Article->save($data);
  2070. $this->assertFalse(empty($result));
  2071. $result = $Article->getInsertID();
  2072. $this->assertTrue(!empty($result));
  2073. $result = $Article->field('title', array('id' => 1));
  2074. $this->assertEquals('First Article', $result);
  2075. $articles = $Article->find('all', array(
  2076. 'fields' => array('id', 'title'),
  2077. 'recursive' => -1,
  2078. 'order' => array('Article.id' => 'ASC')
  2079. ));
  2080. $expected = array(
  2081. array('Article' => array(
  2082. 'id' => 1,
  2083. 'title' => 'First Article'
  2084. )),
  2085. array('Article' => array(
  2086. 'id' => 2,
  2087. 'title' => 'Second Article'
  2088. )),
  2089. array('Article' => array(
  2090. 'id' => 3,
  2091. 'title' => 'Third Article'
  2092. )),
  2093. array('Article' => array(
  2094. 'id' => 4,
  2095. 'title' => 'Brand New Article'
  2096. )));
  2097. $this->assertEquals($expected, $articles);
  2098. }
  2099. /**
  2100. * testCreationWithMultipleDataSameModelManualInstances method
  2101. *
  2102. * @return void
  2103. */
  2104. public function testCreationWithMultipleDataSameModelManualInstances() {
  2105. $this->loadFixtures('PrimaryModel');
  2106. $Primary = new PrimaryModel();
  2107. $result = $Primary->field('primary_name', array('id' => 1));
  2108. $this->assertEquals('Primary Name Existing', $result);
  2109. $data = array(
  2110. 'PrimaryModel' => array(
  2111. 'primary_name' => 'Primary Name New'
  2112. ),
  2113. 'SecondaryModel' => array(
  2114. 'id' => array(1)
  2115. ));
  2116. $Primary->create();
  2117. $result = $Primary->save($data);
  2118. $this->assertFalse(empty($result));
  2119. $result = $Primary->field('primary_name', array('id' => 1));
  2120. $this->assertEquals('Primary Name Existing', $result);
  2121. $result = $Primary->getInsertID();
  2122. $this->assertTrue(!empty($result));
  2123. $result = $Primary->field('primary_name', array('id' => $result));
  2124. $this->assertEquals('Primary Name New', $result);
  2125. $result = $Primary->find('count');
  2126. $this->assertEquals(2, $result);
  2127. }
  2128. /**
  2129. * testRecordExists method
  2130. *
  2131. * @return void
  2132. */
  2133. public function testRecordExists() {
  2134. $this->loadFixtures('User');
  2135. $TestModel = new User();
  2136. $this->assertFalse($TestModel->exists());
  2137. $TestModel->read(null, 1);
  2138. $this->assertTrue($TestModel->exists());
  2139. $TestModel->create();
  2140. $this->assertFalse($TestModel->exists());
  2141. $TestModel->id = 4;
  2142. $this->assertTrue($TestModel->exists());
  2143. $TestModel = new TheVoid();
  2144. $this->assertFalse($TestModel->exists());
  2145. }
  2146. /**
  2147. * testRecordExistsMissingTable method
  2148. *
  2149. * @expectedException PDOException
  2150. * @return void
  2151. */
  2152. public function testRecordExistsMissingTable() {
  2153. $TestModel = new TheVoid();
  2154. $TestModel->id = 5;
  2155. $TestModel->exists();
  2156. }
  2157. /**
  2158. * testUpdateExisting method
  2159. *
  2160. * @return void
  2161. */
  2162. public function testUpdateExisting() {
  2163. $this->loadFixtures('User', 'Article', 'Comment');
  2164. $TestModel = new User();
  2165. $TestModel->create();
  2166. $TestModel->save(array(
  2167. 'User' => array(
  2168. 'user' => 'some user',
  2169. 'password' => 'some password'
  2170. )));
  2171. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  2172. $id = $TestModel->id;
  2173. $TestModel->save(array(
  2174. 'User' => array(
  2175. 'user' => 'updated user'
  2176. )));
  2177. $this->assertEquals($id, $TestModel->id);
  2178. $result = $TestModel->findById($id);
  2179. $this->assertEquals('updated user', $result['User']['user']);
  2180. $this->assertEquals('some password', $result['User']['password']);
  2181. $Article = new Article();
  2182. $Comment = new Comment();
  2183. $data = array(
  2184. 'Comment' => array(
  2185. 'id' => 1,
  2186. 'comment' => 'First Comment for First Article'
  2187. ),
  2188. 'Article' => array(
  2189. 'id' => 2,
  2190. 'title' => 'Second Article'
  2191. ));
  2192. $result = $Article->save($data);
  2193. $this->assertFalse(empty($result));
  2194. $result = $Comment->save($data);
  2195. $this->assertFalse(empty($result));
  2196. }
  2197. /**
  2198. * test updating records and saving blank values.
  2199. *
  2200. * @return void
  2201. */
  2202. public function testUpdateSavingBlankValues() {
  2203. $this->loadFixtures('Article');
  2204. $Article = new Article();
  2205. $Article->validate = array();
  2206. $Article->create();
  2207. $result = $Article->save(array(
  2208. 'id' => 1,
  2209. 'title' => '',
  2210. 'body' => ''
  2211. ));
  2212. $this->assertTrue((bool)$result);
  2213. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2214. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2215. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2216. }
  2217. /**
  2218. * testUpdateMultiple method
  2219. *
  2220. * @return void
  2221. */
  2222. public function testUpdateMultiple() {
  2223. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2224. $TestModel = new Comment();
  2225. $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2226. $expected = array('2', '4', '1', '1', '1', '2');
  2227. $this->assertEquals($expected, $result);
  2228. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2229. $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2230. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2231. $this->assertEquals($expected, $result);
  2232. $result = $TestModel->updateAll(
  2233. array('Comment.comment' => "'Updated today'"),
  2234. array('Comment.user_id' => 5)
  2235. );
  2236. $this->assertFalse(empty($result));
  2237. $result = Hash::extract(
  2238. $TestModel->find('all', array(
  2239. 'conditions' => array(
  2240. 'Comment.user_id' => 5
  2241. ))),
  2242. '{n}.Comment.comment'
  2243. );
  2244. $expected = array_fill(0, 2, 'Updated today');
  2245. $this->assertEquals($expected, $result);
  2246. }
  2247. /**
  2248. * testHabtmUuidWithUuidId method
  2249. *
  2250. * @return void
  2251. */
  2252. public function testHabtmUuidWithUuidId() {
  2253. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2254. $TestModel = new Uuidportfolio();
  2255. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2256. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2257. $TestModel->create($data);
  2258. $TestModel->save();
  2259. $id = $TestModel->id;
  2260. $result = $TestModel->read(null, $id);
  2261. $this->assertEquals(1, count($result['Uuiditem']));
  2262. $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
  2263. }
  2264. /**
  2265. * test HABTM saving when join table has no primary key and only 2 columns.
  2266. *
  2267. * @return void
  2268. */
  2269. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2270. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2271. $Fruit = new Fruit();
  2272. $Fruit->FruitsUuidTag->order = null;
  2273. $data = array(
  2274. 'Fruit' => array(
  2275. 'color' => 'Red',
  2276. 'shape' => 'Heart-shaped',
  2277. 'taste' => 'sweet',
  2278. 'name' => 'Strawberry',
  2279. ),
  2280. 'UuidTag' => array(
  2281. 'UuidTag' => array(
  2282. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2283. )
  2284. )
  2285. );
  2286. $result = $Fruit->save($data);
  2287. $this->assertFalse(empty($result));
  2288. }
  2289. /**
  2290. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2295. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2296. $Fruit = new FruitNoWith();
  2297. $data = array(
  2298. 'Fruit' => array(
  2299. 'color' => 'Red',
  2300. 'shape' => 'Heart-shaped',
  2301. 'taste' => 'sweet',
  2302. 'name' => 'Strawberry',
  2303. ),
  2304. 'UuidTag' => array(
  2305. 'UuidTag' => array(
  2306. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2307. )
  2308. )
  2309. );
  2310. $result = $Fruit->save($data);
  2311. $this->assertFalse(empty($result));
  2312. }
  2313. /**
  2314. * testHabtmUuidWithNumericId method
  2315. *
  2316. * @return void
  2317. */
  2318. public function testHabtmUuidWithNumericId() {
  2319. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2320. $TestModel = new Uuiditem();
  2321. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2322. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2323. $TestModel->create($data);
  2324. $TestModel->save();
  2325. $id = $TestModel->id;
  2326. $result = $TestModel->read(null, $id);
  2327. $this->assertEquals(1, count($result['Uuidportfolio']));
  2328. }
  2329. /**
  2330. * testSaveMultipleHabtm method
  2331. *
  2332. * @return void
  2333. */
  2334. public function testSaveMultipleHabtm() {
  2335. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2336. $TestModel = new JoinA();
  2337. $result = $TestModel->findById(1);
  2338. $expected = array(
  2339. 'JoinA' => array(
  2340. 'id' => 1,
  2341. 'name' => 'Join A 1',
  2342. 'body' => 'Join A 1 Body',
  2343. 'created' => '2008-01-03 10:54:23',
  2344. 'updated' => '2008-01-03 10:54:23'
  2345. ),
  2346. 'JoinB' => array(
  2347. 0 => array(
  2348. 'id' => 2,
  2349. 'name' => 'Join B 2',
  2350. 'created' => '2008-01-03 10:55:02',
  2351. 'updated' => '2008-01-03 10:55:02',
  2352. 'JoinAsJoinB' => array(
  2353. 'id' => 1,
  2354. 'join_a_id' => 1,
  2355. 'join_b_id' => 2,
  2356. 'other' => 'Data for Join A 1 Join B 2',
  2357. 'created' => '2008-01-03 10:56:33',
  2358. 'updated' => '2008-01-03 10:56:33'
  2359. ))),
  2360. 'JoinC' => array(
  2361. 0 => array(
  2362. 'id' => 2,
  2363. 'name' => 'Join C 2',
  2364. 'created' => '2008-01-03 10:56:12',
  2365. 'updated' => '2008-01-03 10:56:12',
  2366. 'JoinAsJoinC' => array(
  2367. 'id' => 1,
  2368. 'join_a_id' => 1,
  2369. 'join_c_id' => 2,
  2370. 'other' => 'Data for Join A 1 Join C 2',
  2371. 'created' => '2008-01-03 10:57:22',
  2372. 'updated' => '2008-01-03 10:57:22'
  2373. ))));
  2374. $this->assertEquals($expected, $result);
  2375. $TestModel->id = 1;
  2376. $data = array(
  2377. 'JoinA' => array(
  2378. 'id' => '1',
  2379. 'name' => 'New name for Join A 1',
  2380. 'updated' => self::date()
  2381. ),
  2382. 'JoinB' => array(
  2383. array(
  2384. 'id' => 1,
  2385. 'join_b_id' => 2,
  2386. 'other' => 'New data for Join A 1 Join B 2',
  2387. 'created' => self::date(),
  2388. 'updated' => self::date()
  2389. )),
  2390. 'JoinC' => array(
  2391. array(
  2392. 'id' => 1,
  2393. 'join_c_id' => 2,
  2394. 'other' => 'New data for Join A 1 Join C 2',
  2395. 'created' => self::date(),
  2396. 'updated' => self::date()
  2397. )));
  2398. $TestModel->set($data);
  2399. $TestModel->save();
  2400. $result = $TestModel->findById(1);
  2401. $expected = array(
  2402. 'JoinA' => array(
  2403. 'id' => 1,
  2404. 'name' => 'New name for Join A 1',
  2405. 'body' => 'Join A 1 Body',
  2406. 'created' => '2008-01-03 10:54:23',
  2407. 'updated' => self::date()
  2408. ),
  2409. 'JoinB' => array(
  2410. 0 => array(
  2411. 'id' => 2,
  2412. 'name' => 'Join B 2',
  2413. 'created' => '2008-01-03 10:55:02',
  2414. 'updated' => '2008-01-03 10:55:02',
  2415. 'JoinAsJoinB' => array(
  2416. 'id' => 1,
  2417. 'join_a_id' => 1,
  2418. 'join_b_id' => 2,
  2419. 'other' => 'New data for Join A 1 Join B 2',
  2420. 'created' => self::date(),
  2421. 'updated' => self::date()
  2422. ))),
  2423. 'JoinC' => array(
  2424. 0 => array(
  2425. 'id' => 2,
  2426. 'name' => 'Join C 2',
  2427. 'created' => '2008-01-03 10:56:12',
  2428. 'updated' => '2008-01-03 10:56:12',
  2429. 'JoinAsJoinC' => array(
  2430. 'id' => 1,
  2431. 'join_a_id' => 1,
  2432. 'join_c_id' => 2,
  2433. 'other' => 'New data for Join A 1 Join C 2',
  2434. 'created' => self::date(),
  2435. 'updated' => self::date()
  2436. ))));
  2437. $this->assertEquals($expected, $result);
  2438. }
  2439. /**
  2440. * testSaveAll method
  2441. *
  2442. * @return void
  2443. */
  2444. public function testSaveAll() {
  2445. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2446. $TestModel = new Post();
  2447. $result = $TestModel->find('all');
  2448. $this->assertEquals(3, count($result));
  2449. $this->assertFalse(isset($result[3]));
  2450. $TestModel->saveAll(array(
  2451. 'Post' => array(
  2452. 'title' => 'Post with Author',
  2453. 'body' => 'This post will be saved with an author'
  2454. ),
  2455. 'Author' => array(
  2456. 'user' => 'bob',
  2457. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2458. )));
  2459. $result = $TestModel->find('all');
  2460. $expected = array(
  2461. 'Post' => array(
  2462. 'id' => '4',
  2463. 'author_id' => '5',
  2464. 'title' => 'Post with Author',
  2465. 'body' => 'This post will be saved with an author',
  2466. 'published' => 'N'
  2467. ),
  2468. 'Author' => array(
  2469. 'id' => '5',
  2470. 'user' => 'bob',
  2471. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2472. 'test' => 'working'
  2473. ));
  2474. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  2475. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  2476. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  2477. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  2478. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2479. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2480. $this->assertEquals($expected, $result[3]);
  2481. $this->assertEquals(4, count($result));
  2482. $TestModel->deleteAll(true);
  2483. $this->assertEquals(array(), $TestModel->find('all'));
  2484. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2485. $this->db->truncate($TestModel);
  2486. $TestModel->saveAll(array(
  2487. array(
  2488. 'title' => 'Multi-record post 1',
  2489. 'body' => 'First multi-record post',
  2490. 'author_id' => 2
  2491. ),
  2492. array(
  2493. 'title' => 'Multi-record post 2',
  2494. 'body' => 'Second multi-record post',
  2495. 'author_id' => 2
  2496. )));
  2497. $result = $TestModel->find('all', array(
  2498. 'recursive' => -1,
  2499. 'order' => 'Post.id ASC'
  2500. ));
  2501. $expected = array(
  2502. array(
  2503. 'Post' => array(
  2504. 'id' => '1',
  2505. 'author_id' => '2',
  2506. 'title' => 'Multi-record post 1',
  2507. 'body' => 'First multi-record post',
  2508. 'published' => 'N'
  2509. )),
  2510. array(
  2511. 'Post' => array(
  2512. 'id' => '2',
  2513. 'author_id' => '2',
  2514. 'title' => 'Multi-record post 2',
  2515. 'body' => 'Second multi-record post',
  2516. 'published' => 'N'
  2517. )));
  2518. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  2519. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  2520. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  2521. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  2522. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2523. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2524. $this->assertEquals($expected, $result);
  2525. $TestModel = new Comment();
  2526. $result = $TestModel->saveAll(array(
  2527. 'Comment' => array(
  2528. 'article_id' => 2,
  2529. 'user_id' => 2,
  2530. 'comment' => 'New comment with attachment',
  2531. 'published' => 'Y'
  2532. ),
  2533. 'Attachment' => array(
  2534. 'attachment' => 'some_file.tgz'
  2535. )));
  2536. $this->assertFalse(empty($result));
  2537. $result = $TestModel->find('all');
  2538. $expected = array(
  2539. 'id' => '7',
  2540. 'article_id' => '2',
  2541. 'user_id' => '2',
  2542. 'comment' => 'New comment with attachment',
  2543. 'published' => 'Y'
  2544. );
  2545. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  2546. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  2547. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2548. $this->assertEquals($expected, $result[6]['Comment']);
  2549. $expected = array(
  2550. 'id' => '2',
  2551. 'comment_id' => '7',
  2552. 'attachment' => 'some_file.tgz'
  2553. );
  2554. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  2555. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  2556. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2557. $this->assertEquals($expected, $result[6]['Attachment']);
  2558. }
  2559. /**
  2560. * Test SaveAll with Habtm relations
  2561. *
  2562. * @return void
  2563. */
  2564. public function testSaveAllHabtm() {
  2565. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2566. $data = array(
  2567. 'Article' => array(
  2568. 'user_id' => 1,
  2569. 'title' => 'Article Has and belongs to Many Tags'
  2570. ),
  2571. 'Tag' => array(
  2572. 'Tag' => array(1, 2)
  2573. ),
  2574. 'Comment' => array(
  2575. array(
  2576. 'comment' => 'Article comment',
  2577. 'user_id' => 1
  2578. )));
  2579. $Article = new Article();
  2580. $result = $Article->saveAll($data);
  2581. $this->assertFalse(empty($result));
  2582. $result = $Article->read();
  2583. $this->assertEquals(2, count($result['Tag']));
  2584. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  2585. $this->assertEquals(1, count($result['Comment']));
  2586. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  2587. }
  2588. /**
  2589. * Test SaveAll with Habtm relations and extra join table fields
  2590. *
  2591. * @return void
  2592. */
  2593. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2594. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2595. $data = array(
  2596. 'Something' => array(
  2597. 'id' => 4,
  2598. 'title' => 'Extra Fields',
  2599. 'body' => 'Extra Fields Body',
  2600. 'published' => '1'
  2601. ),
  2602. 'SomethingElse' => array(
  2603. array('something_else_id' => 1, 'doomed' => '1'),
  2604. array('something_else_id' => 2, 'doomed' => '0'),
  2605. array('something_else_id' => 3, 'doomed' => '1')
  2606. )
  2607. );
  2608. $Something = new Something();
  2609. $result = $Something->saveAll($data);
  2610. $this->assertFalse(empty($result));
  2611. $result = $Something->read();
  2612. $this->assertEquals(3, count($result['SomethingElse']));
  2613. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2614. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2615. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2616. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2617. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2618. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2619. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2620. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2621. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2622. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2623. }
  2624. /**
  2625. * testSaveAllHasOne method
  2626. *
  2627. * @return void
  2628. */
  2629. public function testSaveAllHasOne() {
  2630. $model = new Comment();
  2631. $model->deleteAll(true);
  2632. $this->assertEquals(array(), $model->find('all'));
  2633. $model->Attachment->deleteAll(true);
  2634. $this->assertEquals(array(), $model->Attachment->find('all'));
  2635. $this->assertTrue($model->saveAll(array(
  2636. 'Comment' => array(
  2637. 'comment' => 'Comment with attachment',
  2638. 'article_id' => 1,
  2639. 'user_id' => 1
  2640. ),
  2641. 'Attachment' => array(
  2642. 'attachment' => 'some_file.zip'
  2643. ))));
  2644. $result = $model->find('all', array('fields' => array(
  2645. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2646. 'Attachment.comment_id', 'Attachment.attachment'
  2647. )));
  2648. $expected = array(array(
  2649. 'Comment' => array(
  2650. 'id' => '1',
  2651. 'comment' => 'Comment with attachment'
  2652. ),
  2653. 'Attachment' => array(
  2654. 'id' => '1',
  2655. 'comment_id' => '1',
  2656. 'attachment' => 'some_file.zip'
  2657. )));
  2658. $this->assertEquals($expected, $result);
  2659. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2660. $data = array(
  2661. 'Comment' => array(
  2662. 'comment' => 'Comment with attachment',
  2663. 'article_id' => 1,
  2664. 'user_id' => 1
  2665. ),
  2666. 'Attachment' => array(
  2667. 'attachment' => 'some_file.zip'
  2668. ));
  2669. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2670. }
  2671. /**
  2672. * testSaveAllBelongsTo method
  2673. *
  2674. * @return void
  2675. */
  2676. public function testSaveAllBelongsTo() {
  2677. $model = new Comment();
  2678. $model->deleteAll(true);
  2679. $this->assertEquals(array(), $model->find('all'));
  2680. $model->Article->deleteAll(true);
  2681. $this->assertEquals(array(), $model->Article->find('all'));
  2682. $this->assertTrue($model->saveAll(array(
  2683. 'Comment' => array(
  2684. 'comment' => 'Article comment',
  2685. 'article_id' => 1,
  2686. 'user_id' => 1
  2687. ),
  2688. 'Article' => array(
  2689. 'title' => 'Model Associations 101',
  2690. 'user_id' => 1
  2691. ))));
  2692. $result = $model->find('all', array('fields' => array(
  2693. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2694. )));
  2695. $expected = array(array(
  2696. 'Comment' => array(
  2697. 'id' => '1',
  2698. 'article_id' => '1',
  2699. 'comment' => 'Article comment'
  2700. ),
  2701. 'Article' => array(
  2702. 'id' => '1',
  2703. 'title' => 'Model Associations 101'
  2704. )));
  2705. $this->assertEquals($expected, $result);
  2706. }
  2707. /**
  2708. * testSaveAllHasOneValidation method
  2709. *
  2710. * @return void
  2711. */
  2712. public function testSaveAllHasOneValidation() {
  2713. $model = new Comment();
  2714. $model->deleteAll(true);
  2715. $this->assertEquals(array(), $model->find('all'));
  2716. $model->Attachment->deleteAll(true);
  2717. $this->assertEquals(array(), $model->Attachment->find('all'));
  2718. $model->validate = array('comment' => 'notEmpty');
  2719. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2720. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2721. $result = $model->saveAll(
  2722. array(
  2723. 'Comment' => array(
  2724. 'comment' => '',
  2725. 'article_id' => 1,
  2726. 'user_id' => 1
  2727. ),
  2728. 'Attachment' => array('attachment' => '')
  2729. ),
  2730. array('validate' => 'first')
  2731. );
  2732. $this->assertEquals(false, $result);
  2733. $expected = array(
  2734. 'comment' => array('This field cannot be left blank'),
  2735. 'Attachment' => array(
  2736. 'attachment' => array('This field cannot be left blank')
  2737. )
  2738. );
  2739. $this->assertEquals($expected, $model->validationErrors);
  2740. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2741. }
  2742. /**
  2743. * testSaveAllAtomic method
  2744. *
  2745. * @return void
  2746. */
  2747. public function testSaveAllAtomic() {
  2748. $this->loadFixtures('Article', 'User', 'Comment');
  2749. $TestModel = new Article();
  2750. $result = $TestModel->saveAll(array(
  2751. 'Article' => array(
  2752. 'title' => 'Post with Author',
  2753. 'body' => 'This post will be saved with an author',
  2754. 'user_id' => 2
  2755. ),
  2756. 'Comment' => array(
  2757. array('comment' => 'First new comment', 'user_id' => 2))
  2758. ), array('atomic' => false));
  2759. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2760. $result = $TestModel->saveAll(array(
  2761. array(
  2762. 'id' => '1',
  2763. 'title' => 'Baleeted First Post',
  2764. 'body' => 'Baleeted!',
  2765. 'published' => 'N'
  2766. ),
  2767. array(
  2768. 'id' => '2',
  2769. 'title' => 'Just update the title'
  2770. ),
  2771. array(
  2772. 'title' => 'Creating a fourth post',
  2773. 'body' => 'Fourth post body',
  2774. 'user_id' => 2
  2775. )
  2776. ), array('atomic' => false));
  2777. $this->assertSame($result, array(true, true, true));
  2778. $result = $TestModel->saveAll(array(
  2779. 'Article' => array('id' => 2),
  2780. 'Comment' => array(
  2781. array(
  2782. 'comment' => 'First new comment',
  2783. 'published' => 'Y',
  2784. 'user_id' => 1
  2785. ),
  2786. array(
  2787. 'comment' => 'Second new comment',
  2788. 'published' => 'Y',
  2789. 'user_id' => 2
  2790. ))
  2791. ), array('validate' => true, 'atomic' => false));
  2792. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2793. $TestModel->validate = array(
  2794. 'title' => 'notEmpty',
  2795. 'author_id' => 'numeric'
  2796. );
  2797. $result = $TestModel->saveAll(array(
  2798. array(
  2799. 'id' => '1',
  2800. 'title' => 'Un-Baleeted First Post',
  2801. 'body' => 'Not Baleeted!',
  2802. 'published' => 'Y'
  2803. ),
  2804. array(
  2805. 'id' => '2',
  2806. 'title' => '',
  2807. 'body' => 'Trying to get away with an empty title'
  2808. )
  2809. ), array('validate' => true, 'atomic' => false));
  2810. $this->assertSame(array(true, false), $result);
  2811. }
  2812. /**
  2813. * testSaveAllDeepAssociated method
  2814. *
  2815. * @return void
  2816. */
  2817. public function testSaveAllDeepAssociated() {
  2818. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2819. $TestModel = new Article();
  2820. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2821. $TestModel->hasAndBelongsToMany = array();
  2822. $result = $TestModel->saveAll(array(
  2823. 'Article' => array('id' => 2),
  2824. 'Comment' => array(
  2825. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2826. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2827. )
  2828. ), array('deep' => true));
  2829. $this->assertTrue($result);
  2830. $result = $TestModel->findById(2);
  2831. $expected = array(
  2832. 'First Comment for Second Article',
  2833. 'Second Comment for Second Article',
  2834. 'First new comment',
  2835. 'Second new comment'
  2836. );
  2837. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2838. $this->assertEquals($expected, $result);
  2839. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2840. $this->assertEquals(5, $result);
  2841. $result = $TestModel->saveAll(array(
  2842. 'Article' => array('id' => 2),
  2843. 'Comment' => array(
  2844. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  2845. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2846. )
  2847. ), array('deep' => true));
  2848. $this->assertTrue($result);
  2849. $result = $TestModel->findById(2);
  2850. $expected = array(
  2851. 'First Comment for Second Article',
  2852. 'Second Comment for Second Article',
  2853. 'First new comment',
  2854. 'Second new comment',
  2855. 'Third new comment',
  2856. 'Fourth new comment'
  2857. );
  2858. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2859. $this->assertEquals($expected, $result);
  2860. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2861. $this->assertEquals(2, $result);
  2862. $data = array(
  2863. 'Attachment' => array(
  2864. 'attachment' => 'deepsave insert',
  2865. ),
  2866. 'Comment' => array(
  2867. 'comment' => 'First comment deepsave insert',
  2868. 'published' => 'Y',
  2869. 'user_id' => 5,
  2870. 'Article' => array(
  2871. 'title' => 'First Article deepsave insert',
  2872. 'body' => 'First Article Body deepsave insert',
  2873. 'User' => array(
  2874. 'user' => '',
  2875. 'password' => 'magic'
  2876. ),
  2877. ),
  2878. )
  2879. );
  2880. $TestModel->Comment->Attachment->create();
  2881. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2882. $this->assertFalse($result);
  2883. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  2884. $this->assertEquals($expected, $TestModel->validationErrors);
  2885. $data['Comment']['Article']['User']['user'] = 'deepsave';
  2886. $TestModel->Comment->Attachment->create();
  2887. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2888. $this->assertTrue($result);
  2889. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  2890. $expected = array(
  2891. 'Attachment' => array(
  2892. 'id' => '3',
  2893. 'comment_id' => '11',
  2894. 'attachment' => 'deepsave insert',
  2895. ),
  2896. 'Comment' => array(
  2897. 'id' => '11',
  2898. 'article_id' => '4',
  2899. 'user_id' => '5',
  2900. 'comment' => 'First comment deepsave insert',
  2901. 'published' => 'Y',
  2902. )
  2903. );
  2904. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  2905. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  2906. unset($result['Comment']['created'], $result['Comment']['updated']);
  2907. $this->assertEquals($expected['Comment'], $result['Comment']);
  2908. $result = $TestModel->findById($result['Comment']['article_id']);
  2909. $expected = array(
  2910. 'Article' => array(
  2911. 'id' => '4',
  2912. 'user_id' => '6',
  2913. 'title' => 'First Article deepsave insert',
  2914. 'body' => 'First Article Body deepsave insert',
  2915. 'published' => 'N',
  2916. ),
  2917. 'User' => array(
  2918. 'id' => '6',
  2919. 'user' => 'deepsave',
  2920. 'password' => 'magic',
  2921. ),
  2922. 'Comment' => array(
  2923. array(
  2924. 'id' => '11',
  2925. 'article_id' => '4',
  2926. 'user_id' => '5',
  2927. 'comment' => 'First comment deepsave insert',
  2928. 'published' => 'Y',
  2929. )
  2930. )
  2931. );
  2932. unset(
  2933. $result['Article']['created'], $result['Article']['updated'],
  2934. $result['User']['created'], $result['User']['updated'],
  2935. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  2936. );
  2937. $this->assertEquals($expected, $result);
  2938. }
  2939. /**
  2940. * testSaveAllDeepMany
  2941. * tests the validate methods with deeper recursive data
  2942. *
  2943. * @return void
  2944. */
  2945. public function testSaveAllDeepMany() {
  2946. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2947. $TestModel = new Article();
  2948. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2949. $TestModel->hasAndBelongsToMany = array();
  2950. $data = array(
  2951. array(
  2952. 'Article' => array('id' => 1),
  2953. 'Comment' => array(
  2954. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  2955. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2956. )
  2957. ),
  2958. array(
  2959. 'Article' => array('id' => 2),
  2960. 'Comment' => array(
  2961. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  2962. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  2963. )
  2964. )
  2965. );
  2966. $result = $TestModel->saveAll($data, array('deep' => true));
  2967. $this->assertTrue($result);
  2968. $data = array(
  2969. array(
  2970. 'id' => 1, 'body' => '',
  2971. 'Comment' => array(
  2972. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  2973. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2974. )
  2975. ),
  2976. array(
  2977. 'Article' => array('id' => 2),
  2978. 'Comment' => array(
  2979. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  2980. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  2981. )
  2982. )
  2983. );
  2984. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2985. $result = $TestModel->saveAll($data, array('deep' => true));
  2986. $this->assertFalse($result);
  2987. $expected = array(
  2988. 0 => array(
  2989. 'body' => array('This field cannot be left blank'),
  2990. 'Comment' => array(
  2991. 0 => array(
  2992. 'comment' => array('This field cannot be left blank'),
  2993. 'User' => array(
  2994. 'user' => array('This field cannot be left blank')
  2995. )
  2996. )
  2997. )
  2998. ),
  2999. 1 => array(
  3000. 'Comment' => array(
  3001. 0 => array(
  3002. 'User' => array(
  3003. 'password' => array('This field cannot be left blank')
  3004. )
  3005. ),
  3006. 1 => array(
  3007. 'comment' => array('This field cannot be left blank')
  3008. )
  3009. )
  3010. )
  3011. );
  3012. $result = $TestModel->validationErrors;
  3013. $this->assertSame($expected, $result);
  3014. }
  3015. /**
  3016. * testSaveAllDeepValidateOnly
  3017. * tests the validate methods with deeper recursive data
  3018. *
  3019. * @return void
  3020. */
  3021. public function testSaveAllDeepValidateOnly() {
  3022. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3023. $TestModel = new Article();
  3024. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3025. $TestModel->hasAndBelongsToMany = array();
  3026. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3027. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3028. $result = $TestModel->saveAll(
  3029. array(
  3030. 'Article' => array('id' => 2),
  3031. 'Comment' => array(
  3032. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3033. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3034. )
  3035. ),
  3036. array('validate' => 'only', 'deep' => true)
  3037. );
  3038. $this->assertTrue($result);
  3039. $result = $TestModel->saveAll(
  3040. array(
  3041. 'Article' => array('id' => 2),
  3042. 'Comment' => array(
  3043. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3044. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3045. )
  3046. ),
  3047. array('validate' => 'only', 'deep' => true)
  3048. );
  3049. $this->assertFalse($result);
  3050. $result = $TestModel->saveAll(
  3051. array(
  3052. 'Article' => array('id' => 2),
  3053. 'Comment' => array(
  3054. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3055. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3056. )
  3057. ),
  3058. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3059. );
  3060. $expected = array(
  3061. 'Article' => true,
  3062. 'Comment' => array(
  3063. true,
  3064. true
  3065. )
  3066. );
  3067. $this->assertSame($expected, $result);
  3068. $result = $TestModel->saveAll(
  3069. array(
  3070. 'Article' => array('id' => 2),
  3071. 'Comment' => array(
  3072. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3073. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3074. )
  3075. ),
  3076. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3077. );
  3078. $expected = array(
  3079. 'Article' => true,
  3080. 'Comment' => array(
  3081. false,
  3082. true
  3083. )
  3084. );
  3085. $this->assertSame($expected, $result);
  3086. $result = $TestModel->saveAll(array(
  3087. 'Article' => array('id' => 2),
  3088. 'Comment' => array(
  3089. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3090. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3091. )
  3092. ),
  3093. array('validate' => 'only', 'deep' => true)
  3094. );
  3095. $this->assertTrue($result);
  3096. $result = $TestModel->saveAll(array(
  3097. 'Article' => array('id' => 2),
  3098. 'Comment' => array(
  3099. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3100. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3101. )
  3102. ),
  3103. array('validate' => 'only', 'deep' => true)
  3104. );
  3105. $this->assertFalse($result);
  3106. $result = $TestModel->saveAll(array(
  3107. 'Article' => array('id' => 2),
  3108. 'Comment' => array(
  3109. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3110. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  3111. )
  3112. ),
  3113. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3114. );
  3115. $expected = array(
  3116. 'Article' => true,
  3117. 'Comment' => array(
  3118. true,
  3119. true
  3120. )
  3121. );
  3122. $this->assertSame($expected, $result);
  3123. $result = $TestModel->saveAll(array(
  3124. 'Article' => array('id' => 2),
  3125. 'Comment' => array(
  3126. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3127. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3128. )
  3129. ),
  3130. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3131. );
  3132. $expected = array(
  3133. 'Article' => true,
  3134. 'Comment' => array(
  3135. true,
  3136. false
  3137. )
  3138. );
  3139. $this->assertSame($expected, $result);
  3140. $expected = array(
  3141. 'Comment' => array(
  3142. 1 => array(
  3143. 'Attachment' => array(
  3144. 'attachment' => array('This field cannot be left blank')
  3145. )
  3146. )
  3147. )
  3148. );
  3149. $result = $TestModel->validationErrors;
  3150. $this->assertSame($expected, $result);
  3151. $data = array(
  3152. 'Attachment' => array(
  3153. 'attachment' => 'deepsave insert',
  3154. ),
  3155. 'Comment' => array(
  3156. 'comment' => 'First comment deepsave insert',
  3157. 'published' => 'Y',
  3158. 'user_id' => 5,
  3159. 'Article' => array(
  3160. 'title' => 'First Article deepsave insert',
  3161. 'body' => 'First Article Body deepsave insert',
  3162. 'User' => array(
  3163. 'user' => 'deepsave',
  3164. 'password' => 'magic'
  3165. ),
  3166. ),
  3167. )
  3168. );
  3169. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3170. $this->assertTrue($result);
  3171. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3172. $expected = array(
  3173. 'Attachment' => true,
  3174. 'Comment' => true
  3175. );
  3176. $this->assertSame($expected, $result);
  3177. $data = array(
  3178. 'Attachment' => array(
  3179. 'attachment' => 'deepsave insert',
  3180. ),
  3181. 'Comment' => array(
  3182. 'comment' => 'First comment deepsave insert',
  3183. 'published' => 'Y',
  3184. 'user_id' => 5,
  3185. 'Article' => array(
  3186. 'title' => 'First Article deepsave insert',
  3187. 'body' => 'First Article Body deepsave insert',
  3188. 'User' => array(
  3189. 'user' => '',
  3190. 'password' => 'magic'
  3191. ),
  3192. ),
  3193. )
  3194. );
  3195. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3196. $this->assertFalse($result);
  3197. $result = $TestModel->Comment->Attachment->validationErrors;
  3198. $expected = array(
  3199. 'Comment' => array(
  3200. 'Article' => array(
  3201. 'User' => array(
  3202. 'user' => array('This field cannot be left blank')
  3203. )
  3204. )
  3205. )
  3206. );
  3207. $this->assertSame($expected, $result);
  3208. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3209. $expected = array(
  3210. 'Attachment' => true,
  3211. 'Comment' => false
  3212. );
  3213. $this->assertEquals($expected, $result);
  3214. $data['Comment']['Article']['body'] = '';
  3215. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3216. $this->assertFalse($result);
  3217. $result = $TestModel->Comment->Attachment->validationErrors;
  3218. $expected = array(
  3219. 'Comment' => array(
  3220. 'Article' => array(
  3221. 'body' => array('This field cannot be left blank'),
  3222. 'User' => array(
  3223. 'user' => array('This field cannot be left blank')
  3224. )
  3225. )
  3226. )
  3227. );
  3228. $this->assertSame($expected, $result);
  3229. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3230. $expected = array(
  3231. 'Attachment' => true,
  3232. 'Comment' => false
  3233. );
  3234. $this->assertEquals($expected, $result);
  3235. $data['Comment']['comment'] = '';
  3236. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3237. $this->assertFalse($result);
  3238. $result = $TestModel->Comment->Attachment->validationErrors;
  3239. $expected = array(
  3240. 'Comment' => array(
  3241. 'comment' => array('This field cannot be left blank'),
  3242. 'Article' => array(
  3243. 'body' => array('This field cannot be left blank'),
  3244. 'User' => array(
  3245. 'user' => array('This field cannot be left blank')
  3246. )
  3247. )
  3248. )
  3249. );
  3250. $this->assertSame($expected, $result);
  3251. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3252. $expected = array(
  3253. 'Attachment' => true,
  3254. 'Comment' => false
  3255. );
  3256. $this->assertEquals($expected, $result);
  3257. $data['Attachment']['attachment'] = '';
  3258. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3259. $this->assertFalse($result);
  3260. $result = $TestModel->Comment->Attachment->validationErrors;
  3261. $expected = array(
  3262. 'attachment' => array('This field cannot be left blank'),
  3263. 'Comment' => array(
  3264. 'comment' => array('This field cannot be left blank'),
  3265. 'Article' => array(
  3266. 'body' => array('This field cannot be left blank'),
  3267. 'User' => array(
  3268. 'user' => array('This field cannot be left blank')
  3269. )
  3270. )
  3271. )
  3272. );
  3273. $this->assertSame($expected, $result);
  3274. $result = $TestModel->Comment->validationErrors;
  3275. $expected = array(
  3276. 'comment' => array('This field cannot be left blank'),
  3277. 'Article' => array(
  3278. 'body' => array('This field cannot be left blank'),
  3279. 'User' => array(
  3280. 'user' => array('This field cannot be left blank')
  3281. )
  3282. )
  3283. );
  3284. $this->assertSame($expected, $result);
  3285. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3286. $expected = array(
  3287. 'Attachment' => false,
  3288. 'Comment' => false
  3289. );
  3290. $this->assertEquals($expected, $result);
  3291. }
  3292. /**
  3293. * testSaveAllNotDeepAssociated method
  3294. * test that only directly associated data gets saved
  3295. *
  3296. * @return void
  3297. */
  3298. public function testSaveAllNotDeepAssociated() {
  3299. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3300. $TestModel = new Article();
  3301. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3302. $TestModel->hasAndBelongsToMany = array();
  3303. $result = $TestModel->saveAll(array(
  3304. 'Article' => array('id' => 2),
  3305. 'Comment' => array(
  3306. array(
  3307. 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
  3308. 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
  3309. ),
  3310. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3311. )
  3312. ), array('deep' => false));
  3313. $this->assertTrue($result);
  3314. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  3315. $this->assertFalse($result);
  3316. $result = $TestModel->saveAll(array(
  3317. 'Article' => array('id' => 2),
  3318. 'Comment' => array(
  3319. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
  3320. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3321. )
  3322. ), array('deep' => false));
  3323. $this->assertTrue($result);
  3324. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  3325. $this->assertFalse($result);
  3326. $data = array(
  3327. 'Attachment' => array(
  3328. 'attachment' => 'deepsave insert',
  3329. ),
  3330. 'Comment' => array(
  3331. 'comment' => 'First comment deepsave insert',
  3332. 'published' => 'Y',
  3333. 'user_id' => 4,
  3334. 'article_id' => 1,
  3335. 'Article' => array(
  3336. 'title' => 'First Article deepsave insert',
  3337. 'body' => 'First Article Body deepsave insert',
  3338. 'User' => array(
  3339. 'user' => 'deepsave',
  3340. 'password' => 'magic'
  3341. ),
  3342. ),
  3343. )
  3344. );
  3345. $expected = $TestModel->User->find('count');
  3346. $TestModel->Comment->Attachment->create();
  3347. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
  3348. $this->assertTrue($result);
  3349. $result = $TestModel->User->find('count');
  3350. $this->assertEquals($expected, $result);
  3351. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3352. $expected = array(
  3353. 'Attachment' => array(
  3354. 'id' => '2',
  3355. 'comment_id' => '11',
  3356. 'attachment' => 'deepsave insert',
  3357. ),
  3358. 'Comment' => array(
  3359. 'id' => '11',
  3360. 'article_id' => 1,
  3361. 'user_id' => '4',
  3362. 'comment' => 'First comment deepsave insert',
  3363. 'published' => 'Y',
  3364. )
  3365. );
  3366. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3367. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3368. unset($result['Comment']['created'], $result['Comment']['updated']);
  3369. $this->assertEquals($expected['Comment'], $result['Comment']);
  3370. }
  3371. /**
  3372. * testSaveAllNotDeepMany
  3373. * tests the save methods to not save deeper recursive data
  3374. *
  3375. * @return void
  3376. */
  3377. public function testSaveAllNotDeepMany() {
  3378. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3379. $TestModel = new Article();
  3380. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3381. $TestModel->hasAndBelongsToMany = array();
  3382. $data = array(
  3383. array(
  3384. 'id' => 1,
  3385. 'body' => '',
  3386. 'Comment' => array(
  3387. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3388. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3389. )
  3390. ),
  3391. array(
  3392. 'Article' => array('id' => 2),
  3393. 'Comment' => array(
  3394. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3395. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3396. )
  3397. )
  3398. );
  3399. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3400. $result = $TestModel->saveAll($data, array('deep' => false));
  3401. $this->assertFalse($result);
  3402. $expected = array(
  3403. 0 => array(
  3404. 'body' => array('This field cannot be left blank')
  3405. )
  3406. );
  3407. $result = $TestModel->validationErrors;
  3408. $this->assertSame($expected, $result);
  3409. $data = array(
  3410. array(
  3411. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3412. 'Comment' => array(
  3413. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3414. )
  3415. ),
  3416. array(
  3417. 'Article' => array('id' => 2),
  3418. 'Comment' => array(
  3419. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3420. )
  3421. )
  3422. );
  3423. $result = $TestModel->saveAll($data, array('deep' => false));
  3424. $this->assertTrue($result);
  3425. }
  3426. /**
  3427. * testSaveAllNotDeepValidateOnly
  3428. * tests the validate methods to not validate deeper recursive data
  3429. *
  3430. * @return void
  3431. */
  3432. public function testSaveAllNotDeepValidateOnly() {
  3433. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3434. $TestModel = new Article();
  3435. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3436. $TestModel->hasAndBelongsToMany = array();
  3437. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3438. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3439. $result = $TestModel->saveAll(
  3440. array(
  3441. 'Article' => array('id' => 2, 'body' => ''),
  3442. 'Comment' => array(
  3443. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3444. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3445. )
  3446. ),
  3447. array('validate' => 'only', 'deep' => false)
  3448. );
  3449. $this->assertFalse($result);
  3450. $expected = array('body' => array('This field cannot be left blank'));
  3451. $result = $TestModel->validationErrors;
  3452. $this->assertSame($expected, $result);
  3453. $result = $TestModel->saveAll(
  3454. array(
  3455. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3456. 'Comment' => array(
  3457. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3458. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3459. )
  3460. ),
  3461. array('validate' => 'only', 'deep' => false)
  3462. );
  3463. $this->assertTrue($result);
  3464. $result = $TestModel->saveAll(
  3465. array(
  3466. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3467. 'Comment' => array(
  3468. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3469. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3470. )
  3471. ),
  3472. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3473. );
  3474. $expected = array(
  3475. 'Article' => true,
  3476. 'Comment' => array(
  3477. true,
  3478. true
  3479. )
  3480. );
  3481. $this->assertSame($expected, $result);
  3482. $result = $TestModel->saveAll(array(
  3483. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3484. 'Comment' => array(
  3485. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3486. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3487. )
  3488. ),
  3489. array('validate' => 'only', 'deep' => false)
  3490. );
  3491. $this->assertTrue($result);
  3492. $result = $TestModel->saveAll(array(
  3493. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3494. 'Comment' => array(
  3495. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3496. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3497. )
  3498. ),
  3499. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3500. );
  3501. $expected = array(
  3502. 'Article' => true,
  3503. 'Comment' => array(
  3504. true,
  3505. true
  3506. )
  3507. );
  3508. $this->assertSame($expected, $result);
  3509. $expected = array();
  3510. $result = $TestModel->validationErrors;
  3511. $this->assertSame($expected, $result);
  3512. $data = array(
  3513. 'Attachment' => array(
  3514. 'attachment' => 'deepsave insert',
  3515. ),
  3516. 'Comment' => array(
  3517. 'comment' => 'First comment deepsave insert',
  3518. 'published' => 'Y',
  3519. 'user_id' => 5,
  3520. 'Article' => array(
  3521. 'title' => 'First Article deepsave insert ignored',
  3522. 'body' => 'First Article Body deepsave insert',
  3523. 'User' => array(
  3524. 'user' => '',
  3525. 'password' => 'magic'
  3526. ),
  3527. ),
  3528. )
  3529. );
  3530. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3531. $this->assertTrue($result);
  3532. $result = $TestModel->Comment->Attachment->validationErrors;
  3533. $expected = array();
  3534. $this->assertSame($expected, $result);
  3535. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3536. $expected = array(
  3537. 'Attachment' => true,
  3538. 'Comment' => true
  3539. );
  3540. $this->assertEquals($expected, $result);
  3541. $data['Comment']['Article']['body'] = '';
  3542. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3543. $this->assertTrue($result);
  3544. $result = $TestModel->Comment->Attachment->validationErrors;
  3545. $expected = array();
  3546. $this->assertSame($expected, $result);
  3547. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3548. $expected = array(
  3549. 'Attachment' => true,
  3550. 'Comment' => true
  3551. );
  3552. $this->assertEquals($expected, $result);
  3553. }
  3554. /**
  3555. * testSaveAllHasMany method
  3556. *
  3557. * @return void
  3558. */
  3559. public function testSaveAllHasMany() {
  3560. $this->loadFixtures('Article', 'Comment');
  3561. $TestModel = new Article();
  3562. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3563. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3564. $result = $TestModel->saveAll(array(
  3565. 'Article' => array('id' => 2),
  3566. 'Comment' => array(
  3567. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3568. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3569. )
  3570. ));
  3571. $this->assertFalse(empty($result));
  3572. $result = $TestModel->findById(2);
  3573. $expected = array(
  3574. 'First Comment for Second Article',
  3575. 'Second Comment for Second Article',
  3576. 'First new comment',
  3577. 'Second new comment'
  3578. );
  3579. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3580. $this->assertEquals($expected, $result);
  3581. $result = $TestModel->saveAll(
  3582. array(
  3583. 'Article' => array('id' => 2),
  3584. 'Comment' => array(
  3585. array(
  3586. 'comment' => 'Third new comment',
  3587. 'published' => 'Y',
  3588. 'user_id' => 1
  3589. ))),
  3590. array('atomic' => false)
  3591. );
  3592. $this->assertFalse(empty($result));
  3593. $result = $TestModel->findById(2);
  3594. $expected = array(
  3595. 'First Comment for Second Article',
  3596. 'Second Comment for Second Article',
  3597. 'First new comment',
  3598. 'Second new comment',
  3599. 'Third new comment'
  3600. );
  3601. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3602. $this->assertEquals($expected, $result);
  3603. $TestModel->beforeSaveReturn = false;
  3604. $result = $TestModel->saveAll(
  3605. array(
  3606. 'Article' => array('id' => 2),
  3607. 'Comment' => array(
  3608. array(
  3609. 'comment' => 'Fourth new comment',
  3610. 'published' => 'Y',
  3611. 'user_id' => 1
  3612. ))),
  3613. array('atomic' => false)
  3614. );
  3615. $this->assertEquals(array('Article' => false), $result);
  3616. $result = $TestModel->findById(2);
  3617. $expected = array(
  3618. 'First Comment for Second Article',
  3619. 'Second Comment for Second Article',
  3620. 'First new comment',
  3621. 'Second new comment',
  3622. 'Third new comment'
  3623. );
  3624. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3625. $this->assertEquals($expected, $result);
  3626. }
  3627. /**
  3628. * testSaveAllHasManyValidation method
  3629. *
  3630. * @return void
  3631. */
  3632. public function testSaveAllHasManyValidation() {
  3633. $this->loadFixtures('Article', 'Comment');
  3634. $TestModel = new Article();
  3635. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3636. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3637. $result = $TestModel->saveAll(array(
  3638. 'Article' => array('id' => 2),
  3639. 'Comment' => array(
  3640. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3641. )
  3642. ), array('validate' => true));
  3643. $this->assertFalse($result);
  3644. $expected = array('Comment' => array(
  3645. array('comment' => array('This field cannot be left blank'))
  3646. ));
  3647. $this->assertEquals($expected, $TestModel->validationErrors);
  3648. $expected = array(
  3649. array('comment' => array('This field cannot be left blank'))
  3650. );
  3651. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  3652. $result = $TestModel->saveAll(array(
  3653. 'Article' => array('id' => 2),
  3654. 'Comment' => array(
  3655. array(
  3656. 'comment' => '',
  3657. 'published' => 'Y',
  3658. 'user_id' => 1
  3659. ))
  3660. ), array('validate' => 'first'));
  3661. $this->assertFalse($result);
  3662. }
  3663. /**
  3664. * test saveAll with transactions and ensure there is no missing rollback.
  3665. *
  3666. * @return void
  3667. */
  3668. public function testSaveAllManyRowsTransactionNoRollback() {
  3669. $this->loadFixtures('Post');
  3670. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  3671. $db = ConnectionManager::create('mock_transaction', array(
  3672. 'datasource' => 'MockTransactionDboSource',
  3673. ));
  3674. $db->expects($this->once())
  3675. ->method('describe')
  3676. ->will($this->returnValue(array()));
  3677. $db->expects($this->once())->method('rollback');
  3678. $Post = new Post('mock_transaction');
  3679. $Post->validate = array(
  3680. 'title' => array('rule' => array('notEmpty'))
  3681. );
  3682. $data = array(
  3683. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3684. array('author_id' => 1, 'title' => '')
  3685. );
  3686. $Post->saveAll($data, array('atomic' => true));
  3687. }
  3688. /**
  3689. * test saveAll with transactions and ensure there is no missing rollback.
  3690. *
  3691. * @return void
  3692. */
  3693. public function testSaveAllAssociatedTransactionNoRollback() {
  3694. $testDb = ConnectionManager::getDataSource('test');
  3695. $this->getMock(
  3696. 'DboSource',
  3697. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  3698. array(),
  3699. 'MockTransactionAssociatedDboSource'
  3700. );
  3701. $db = ConnectionManager::create('mock_transaction_assoc', array(
  3702. 'datasource' => 'MockTransactionAssociatedDboSource',
  3703. ));
  3704. $this->mockObjects[] = $db;
  3705. $db->columns = $testDb->columns;
  3706. $db->expects($this->once())->method('rollback');
  3707. $db->expects($this->any())->method('describe')
  3708. ->will($this->returnValue(array(
  3709. 'id' => array('type' => 'integer', 'length' => 11),
  3710. 'title' => array('type' => 'string'),
  3711. 'body' => array('type' => 'text'),
  3712. 'published' => array('type' => 'string')
  3713. )));
  3714. $Post = new Post();
  3715. $Post->useDbConfig = 'mock_transaction_assoc';
  3716. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  3717. $Post->Author->validate = array(
  3718. 'user' => array('rule' => array('notEmpty'))
  3719. );
  3720. $data = array(
  3721. 'Post' => array(
  3722. 'title' => 'New post',
  3723. 'body' => 'Content',
  3724. 'published' => 'Y'
  3725. ),
  3726. 'Author' => array(
  3727. 'user' => '',
  3728. 'password' => "sekret"
  3729. )
  3730. );
  3731. $Post->saveAll($data, array('validate' => true));
  3732. }
  3733. /**
  3734. * test saveAll with nested saveAll call.
  3735. *
  3736. * @return void
  3737. */
  3738. public function testSaveAllNestedSaveAll() {
  3739. $this->loadFixtures('Sample');
  3740. $TransactionTestModel = new TransactionTestModel();
  3741. $data = array(
  3742. array('apple_id' => 1, 'name' => 'sample5'),
  3743. );
  3744. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  3745. }
  3746. /**
  3747. * testSaveAllTransaction method
  3748. *
  3749. * @return void
  3750. */
  3751. public function testSaveAllTransaction() {
  3752. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3753. $TestModel = new Post();
  3754. $TestModel->validate = array('title' => 'notEmpty');
  3755. $data = array(
  3756. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3757. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3758. array('author_id' => 1, 'title' => '')
  3759. );
  3760. $this->assertFalse($TestModel->saveAll($data));
  3761. $result = $TestModel->find('all', array('recursive' => -1));
  3762. $expected = array(
  3763. array('Post' => array(
  3764. 'id' => '1',
  3765. 'author_id' => 1,
  3766. 'title' => 'First Post',
  3767. 'body' => 'First Post Body',
  3768. 'published' => 'Y',
  3769. 'created' => '2007-03-18 10:39:23',
  3770. 'updated' => '2007-03-18 10:41:31'
  3771. )),
  3772. array('Post' => array(
  3773. 'id' => '2',
  3774. 'author_id' => 3,
  3775. 'title' => 'Second Post',
  3776. 'body' => 'Second Post Body',
  3777. 'published' => 'Y',
  3778. 'created' => '2007-03-18 10:41:23',
  3779. 'updated' => '2007-03-18 10:43:31'
  3780. )),
  3781. array('Post' => array(
  3782. 'id' => '3',
  3783. 'author_id' => 1,
  3784. 'title' => 'Third Post',
  3785. 'body' => 'Third Post Body',
  3786. 'published' => 'Y',
  3787. 'created' => '2007-03-18 10:43:23',
  3788. 'updated' => '2007-03-18 10:45:31'
  3789. )));
  3790. if (count($result) != 3) {
  3791. // Database doesn't support transactions
  3792. $expected[] = array(
  3793. 'Post' => array(
  3794. 'id' => '4',
  3795. 'author_id' => 1,
  3796. 'title' => 'New Fourth Post',
  3797. 'body' => null,
  3798. 'published' => 'N',
  3799. 'created' => self::date(),
  3800. 'updated' => self::date()
  3801. ));
  3802. $expected[] = array(
  3803. 'Post' => array(
  3804. 'id' => '5',
  3805. 'author_id' => 1,
  3806. 'title' => 'New Fifth Post',
  3807. 'body' => null,
  3808. 'published' => 'N',
  3809. 'created' => self::date(),
  3810. 'updated' => self::date()
  3811. ));
  3812. $this->assertEquals($expected, $result);
  3813. // Skip the rest of the transactional tests
  3814. return;
  3815. }
  3816. $this->assertEquals($expected, $result);
  3817. $data = array(
  3818. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3819. array('author_id' => 1, 'title' => ''),
  3820. array('author_id' => 1, 'title' => 'New Sixth Post')
  3821. );
  3822. $this->assertFalse($TestModel->saveAll($data));
  3823. $result = $TestModel->find('all', array('recursive' => -1));
  3824. $expected = array(
  3825. array('Post' => array(
  3826. 'id' => '1',
  3827. 'author_id' => 1,
  3828. 'title' => 'First Post',
  3829. 'body' => 'First Post Body',
  3830. 'published' => 'Y',
  3831. 'created' => '2007-03-18 10:39:23',
  3832. 'updated' => '2007-03-18 10:41:31'
  3833. )),
  3834. array('Post' => array(
  3835. 'id' => '2',
  3836. 'author_id' => 3,
  3837. 'title' => 'Second Post',
  3838. 'body' => 'Second Post Body',
  3839. 'published' => 'Y',
  3840. 'created' => '2007-03-18 10:41:23',
  3841. 'updated' => '2007-03-18 10:43:31'
  3842. )),
  3843. array('Post' => array(
  3844. 'id' => '3',
  3845. 'author_id' => 1,
  3846. 'title' => 'Third Post',
  3847. 'body' => 'Third Post Body',
  3848. 'published' => 'Y',
  3849. 'created' => '2007-03-18 10:43:23',
  3850. 'updated' => '2007-03-18 10:45:31'
  3851. )));
  3852. if (count($result) != 3) {
  3853. // Database doesn't support transactions
  3854. $expected[] = array(
  3855. 'Post' => array(
  3856. 'id' => '4',
  3857. 'author_id' => 1,
  3858. 'title' => 'New Fourth Post',
  3859. 'body' => 'Third Post Body',
  3860. 'published' => 'N',
  3861. 'created' => self::date(),
  3862. 'updated' => self::date()
  3863. ));
  3864. $expected[] = array(
  3865. 'Post' => array(
  3866. 'id' => '5',
  3867. 'author_id' => 1,
  3868. 'title' => 'Third Post',
  3869. 'body' => 'Third Post Body',
  3870. 'published' => 'N',
  3871. 'created' => self::date(),
  3872. 'updated' => self::date()
  3873. ));
  3874. }
  3875. $this->assertEquals($expected, $result);
  3876. $TestModel->validate = array('title' => 'notEmpty');
  3877. $data = array(
  3878. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3879. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3880. array('author_id' => 1, 'title' => 'New Sixth Post')
  3881. );
  3882. $this->assertTrue($TestModel->saveAll($data));
  3883. $result = $TestModel->find('all', array(
  3884. 'recursive' => -1,
  3885. 'fields' => array('author_id', 'title', 'body', 'published'),
  3886. 'order' => array('Post.created' => 'ASC')
  3887. ));
  3888. $expected = array(
  3889. array('Post' => array(
  3890. 'author_id' => 1,
  3891. 'title' => 'First Post',
  3892. 'body' => 'First Post Body',
  3893. 'published' => 'Y'
  3894. )),
  3895. array('Post' => array(
  3896. 'author_id' => 3,
  3897. 'title' => 'Second Post',
  3898. 'body' => 'Second Post Body',
  3899. 'published' => 'Y'
  3900. )),
  3901. array('Post' => array(
  3902. 'author_id' => 1,
  3903. 'title' => 'Third Post',
  3904. 'body' => 'Third Post Body',
  3905. 'published' => 'Y'
  3906. )),
  3907. array('Post' => array(
  3908. 'author_id' => 1,
  3909. 'title' => 'New Fourth Post',
  3910. 'body' => '',
  3911. 'published' => 'N'
  3912. )),
  3913. array('Post' => array(
  3914. 'author_id' => 1,
  3915. 'title' => 'New Fifth Post',
  3916. 'body' => '',
  3917. 'published' => 'N'
  3918. )),
  3919. array('Post' => array(
  3920. 'author_id' => 1,
  3921. 'title' => 'New Sixth Post',
  3922. 'body' => '',
  3923. 'published' => 'N'
  3924. )));
  3925. $this->assertEquals($expected, $result);
  3926. }
  3927. /**
  3928. * testSaveAllValidation method
  3929. *
  3930. * @return void
  3931. */
  3932. public function testSaveAllValidation() {
  3933. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3934. $TestModel = new Post();
  3935. $data = array(
  3936. array(
  3937. 'id' => '1',
  3938. 'title' => 'Baleeted First Post',
  3939. 'body' => 'Baleeted!',
  3940. 'published' => 'N'
  3941. ),
  3942. array(
  3943. 'id' => '2',
  3944. 'title' => 'Just update the title'
  3945. ),
  3946. array(
  3947. 'title' => 'Creating a fourth post',
  3948. 'body' => 'Fourth post body',
  3949. 'author_id' => 2
  3950. ));
  3951. $this->assertTrue($TestModel->saveAll($data));
  3952. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3953. $expected = array(
  3954. array(
  3955. 'Post' => array(
  3956. 'id' => '1',
  3957. 'author_id' => '1',
  3958. 'title' => 'Baleeted First Post',
  3959. 'body' => 'Baleeted!',
  3960. 'published' => 'N',
  3961. 'created' => '2007-03-18 10:39:23'
  3962. )),
  3963. array(
  3964. 'Post' => array(
  3965. 'id' => '2',
  3966. 'author_id' => '3',
  3967. 'title' => 'Just update the title',
  3968. 'body' => 'Second Post Body',
  3969. 'published' => 'Y',
  3970. 'created' => '2007-03-18 10:41:23'
  3971. )),
  3972. array(
  3973. 'Post' => array(
  3974. 'id' => '3',
  3975. 'author_id' => '1',
  3976. 'title' => 'Third Post',
  3977. 'body' => 'Third Post Body',
  3978. 'published' => 'Y',
  3979. 'created' => '2007-03-18 10:43:23',
  3980. 'updated' => '2007-03-18 10:45:31'
  3981. )),
  3982. array(
  3983. 'Post' => array(
  3984. 'id' => '4',
  3985. 'author_id' => '2',
  3986. 'title' => 'Creating a fourth post',
  3987. 'body' => 'Fourth post body',
  3988. 'published' => 'N'
  3989. )));
  3990. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  3991. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  3992. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  3993. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  3994. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  3995. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  3996. $this->assertEquals($expected, $result);
  3997. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3998. $data = array(
  3999. array(
  4000. 'id' => '1',
  4001. 'title' => 'Un-Baleeted First Post',
  4002. 'body' => 'Not Baleeted!',
  4003. 'published' => 'Y'
  4004. ),
  4005. array(
  4006. 'id' => '2',
  4007. 'title' => '',
  4008. 'body' => 'Trying to get away with an empty title'
  4009. ));
  4010. $result = $TestModel->saveAll($data);
  4011. $this->assertFalse($result);
  4012. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4013. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4014. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  4015. if (!$transactionWorked) {
  4016. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  4017. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  4018. }
  4019. $this->assertEquals($errors, $TestModel->validationErrors);
  4020. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4021. $data = array(
  4022. array(
  4023. 'id' => '1',
  4024. 'title' => 'Un-Baleeted First Post',
  4025. 'body' => 'Not Baleeted!',
  4026. 'published' => 'Y'
  4027. ),
  4028. array(
  4029. 'id' => '2',
  4030. 'title' => '',
  4031. 'body' => 'Trying to get away with an empty title'
  4032. ));
  4033. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  4034. $this->assertEquals(array(true, false), $result);
  4035. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4036. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4037. $expected = array(
  4038. array(
  4039. 'Post' => array(
  4040. 'id' => '1',
  4041. 'author_id' => '1',
  4042. 'title' => 'Un-Baleeted First Post',
  4043. 'body' => 'Not Baleeted!',
  4044. 'published' => 'Y',
  4045. 'created' => '2007-03-18 10:39:23'
  4046. )
  4047. ),
  4048. array(
  4049. 'Post' => array(
  4050. 'id' => '2',
  4051. 'author_id' => '3',
  4052. 'title' => 'Just update the title',
  4053. 'body' => 'Second Post Body',
  4054. 'published' => 'Y',
  4055. 'created' => '2007-03-18 10:41:23'
  4056. )
  4057. ),
  4058. array(
  4059. 'Post' => array(
  4060. 'id' => '3',
  4061. 'author_id' => '1',
  4062. 'title' => 'Third Post',
  4063. 'body' => 'Third Post Body',
  4064. 'published' => 'Y',
  4065. 'created' => '2007-03-18 10:43:23',
  4066. 'updated' => '2007-03-18 10:45:31'
  4067. )
  4068. ),
  4069. array(
  4070. 'Post' => array(
  4071. 'id' => '4',
  4072. 'author_id' => '2',
  4073. 'title' => 'Creating a fourth post',
  4074. 'body' => 'Fourth post body',
  4075. 'published' => 'N'
  4076. )
  4077. )
  4078. );
  4079. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4080. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4081. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4082. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4083. unset(
  4084. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4085. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4086. );
  4087. $this->assertEquals($expected, $result);
  4088. $this->assertEquals($errors, $TestModel->validationErrors);
  4089. $data = array(
  4090. array(
  4091. 'id' => '1',
  4092. 'title' => 'Re-Baleeted First Post',
  4093. 'body' => 'Baleeted!',
  4094. 'published' => 'N'
  4095. ),
  4096. array(
  4097. 'id' => '2',
  4098. 'title' => '',
  4099. 'body' => 'Trying to get away with an empty title'
  4100. ));
  4101. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  4102. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4103. unset(
  4104. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4105. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4106. );
  4107. $this->assertEquals($expected, $result);
  4108. $this->assertEquals($errors, $TestModel->validationErrors);
  4109. }
  4110. /**
  4111. * testSaveAllValidationOnly method
  4112. *
  4113. * @return void
  4114. */
  4115. public function testSaveAllValidationOnly() {
  4116. $this->loadFixtures('Comment', 'Attachment');
  4117. $TestModel = new Comment();
  4118. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4119. $data = array(
  4120. 'Comment' => array(
  4121. 'comment' => 'This is the comment'
  4122. ),
  4123. 'Attachment' => array(
  4124. 'attachment' => ''
  4125. )
  4126. );
  4127. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  4128. $this->assertFalse($result);
  4129. $TestModel = new Article();
  4130. $TestModel->validate = array('title' => 'notEmpty');
  4131. $result = $TestModel->saveAll(
  4132. array(
  4133. 0 => array('title' => ''),
  4134. 1 => array('title' => 'title 1'),
  4135. 2 => array('title' => 'title 2'),
  4136. ),
  4137. array('validate' => 'only')
  4138. );
  4139. $this->assertFalse($result);
  4140. $expected = array(
  4141. 0 => array('title' => array('This field cannot be left blank')),
  4142. );
  4143. $this->assertEquals($expected, $TestModel->validationErrors);
  4144. $result = $TestModel->saveAll(
  4145. array(
  4146. 0 => array('title' => 'title 0'),
  4147. 1 => array('title' => ''),
  4148. 2 => array('title' => 'title 2'),
  4149. ),
  4150. array('validate' => 'only')
  4151. );
  4152. $this->assertFalse($result);
  4153. $expected = array(
  4154. 1 => array('title' => array('This field cannot be left blank')),
  4155. );
  4156. $this->assertEquals($expected, $TestModel->validationErrors);
  4157. }
  4158. /**
  4159. * testSaveAllValidateFirst method
  4160. *
  4161. * @return void
  4162. */
  4163. public function testSaveAllValidateFirst() {
  4164. $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
  4165. $model = new Article();
  4166. $model->deleteAll(true);
  4167. $model->Comment->validate = array('comment' => 'notEmpty');
  4168. $result = $model->saveAll(array(
  4169. 'Article' => array(
  4170. 'title' => 'Post with Author',
  4171. 'body' => 'This post will be saved author'
  4172. ),
  4173. 'Comment' => array(
  4174. array('comment' => 'First new comment'),
  4175. array('comment' => '')
  4176. )
  4177. ), array('validate' => 'first'));
  4178. $this->assertFalse($result);
  4179. $result = $model->find('all');
  4180. $this->assertSame(array(), $result);
  4181. $expected = array('Comment' => array(
  4182. 1 => array('comment' => array('This field cannot be left blank'))
  4183. ));
  4184. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4185. $this->assertSame($model->Comment->find('count'), 0);
  4186. $result = $model->saveAll(
  4187. array(
  4188. 'Article' => array(
  4189. 'title' => 'Post with Author',
  4190. 'body' => 'This post will be saved with an author',
  4191. 'user_id' => 2
  4192. ),
  4193. 'Comment' => array(
  4194. array(
  4195. 'comment' => 'Only new comment',
  4196. 'user_id' => 2
  4197. ))),
  4198. array('validate' => 'first')
  4199. );
  4200. $this->assertTrue($result);
  4201. $result = $model->Comment->find('all');
  4202. $this->assertSame(count($result), 1);
  4203. $result = Hash::extract($result, '{n}.Comment.article_id');
  4204. $this->assertEquals(4, $result[0]);
  4205. $model->deleteAll(true);
  4206. $data = array(
  4207. 'Article' => array(
  4208. 'title' => 'Post with Author saveAlled from comment',
  4209. 'body' => 'This post will be saved with an author',
  4210. 'user_id' => 2
  4211. ),
  4212. 'Comment' => array(
  4213. 'comment' => 'Only new comment', 'user_id' => 2
  4214. ));
  4215. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  4216. $this->assertFalse(empty($result));
  4217. $result = $model->find('all');
  4218. $this->assertEquals(
  4219. $result[0]['Article']['title'],
  4220. 'Post with Author saveAlled from comment'
  4221. );
  4222. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4223. }
  4224. /**
  4225. * test saveAll()'s return is correct when using atomic = false and validate = first.
  4226. *
  4227. * @return void
  4228. */
  4229. public function testSaveAllValidateFirstAtomicFalse() {
  4230. $this->loadFixtures('Something');
  4231. $Something = new Something();
  4232. $invalidData = array(
  4233. array(
  4234. 'title' => 'foo',
  4235. 'body' => 'bar',
  4236. 'published' => 'baz',
  4237. ),
  4238. array(
  4239. 'body' => 3,
  4240. 'published' => 'sd',
  4241. ),
  4242. );
  4243. $Something->create();
  4244. $Something->validate = array(
  4245. 'title' => array(
  4246. 'rule' => 'alphaNumeric',
  4247. 'required' => true,
  4248. ),
  4249. 'body' => array(
  4250. 'rule' => 'alphaNumeric',
  4251. 'required' => true,
  4252. 'allowEmpty' => true,
  4253. ),
  4254. );
  4255. $result = $Something->saveAll($invalidData, array(
  4256. 'atomic' => false,
  4257. 'validate' => 'first',
  4258. ));
  4259. $expected = array(true, false);
  4260. $this->assertEquals($expected, $result);
  4261. $Something = new Something();
  4262. $validData = array(
  4263. array(
  4264. 'title' => 'title value',
  4265. 'body' => 'body value',
  4266. 'published' => 'baz',
  4267. ),
  4268. array(
  4269. 'title' => 'valid',
  4270. 'body' => 'this body',
  4271. 'published' => 'sd',
  4272. ),
  4273. );
  4274. $Something->create();
  4275. $result = $Something->saveAll($validData, array(
  4276. 'atomic' => false,
  4277. 'validate' => 'first',
  4278. ));
  4279. $expected = array(true, true);
  4280. $this->assertEquals($expected, $result);
  4281. }
  4282. /**
  4283. * testSaveAllHasManyValidationOnly method
  4284. *
  4285. * @return void
  4286. */
  4287. public function testSaveAllHasManyValidationOnly() {
  4288. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4289. $TestModel = new Article();
  4290. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4291. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4292. $result = $TestModel->saveAll(
  4293. array(
  4294. 'Article' => array('id' => 2),
  4295. 'Comment' => array(
  4296. array(
  4297. 'id' => 1,
  4298. 'comment' => '',
  4299. 'published' => 'Y',
  4300. 'user_id' => 1),
  4301. array(
  4302. 'id' => 2,
  4303. 'comment' =>
  4304. 'comment',
  4305. 'published' => 'Y',
  4306. 'user_id' => 1
  4307. ))),
  4308. array('validate' => 'only')
  4309. );
  4310. $this->assertFalse($result);
  4311. $result = $TestModel->saveAll(
  4312. array(
  4313. 'Article' => array('id' => 2),
  4314. 'Comment' => array(
  4315. array(
  4316. 'id' => 1,
  4317. 'comment' => '',
  4318. 'published' => 'Y',
  4319. 'user_id' => 1
  4320. ),
  4321. array(
  4322. 'id' => 2,
  4323. 'comment' => 'comment',
  4324. 'published' => 'Y',
  4325. 'user_id' => 1
  4326. ),
  4327. array(
  4328. 'id' => 3,
  4329. 'comment' => '',
  4330. 'published' => 'Y',
  4331. 'user_id' => 1
  4332. ))),
  4333. array(
  4334. 'validate' => 'only',
  4335. 'atomic' => false
  4336. ));
  4337. $expected = array(
  4338. 'Article' => true,
  4339. 'Comment' => array(false, true, false)
  4340. );
  4341. $this->assertSame($expected, $result);
  4342. $expected = array('Comment' => array(
  4343. 0 => array('comment' => array('This field cannot be left blank')),
  4344. 2 => array('comment' => array('This field cannot be left blank'))
  4345. ));
  4346. $this->assertEquals($expected, $TestModel->validationErrors);
  4347. $expected = array(
  4348. 0 => array('comment' => array('This field cannot be left blank')),
  4349. 2 => array('comment' => array('This field cannot be left blank'))
  4350. );
  4351. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4352. }
  4353. /**
  4354. * test that saveAll still behaves like previous versions (does not necessarily need a first argument)
  4355. *
  4356. * @return void
  4357. */
  4358. public function testSaveAllWithSet() {
  4359. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4360. $data = array(
  4361. 'Article' => array(
  4362. 'user_id' => 1,
  4363. 'title' => 'Article Has and belongs to Many Tags'
  4364. ),
  4365. 'Tag' => array(
  4366. 'Tag' => array(1, 2)
  4367. ),
  4368. 'Comment' => array(
  4369. array(
  4370. 'comment' => 'Article comment',
  4371. 'user_id' => 1
  4372. )));
  4373. $Article = new Article();
  4374. $Article->set($data);
  4375. $result = $Article->saveAll();
  4376. $this->assertFalse(empty($result));
  4377. }
  4378. /**
  4379. * test that saveAll behaves like plain save() when supplied empty data
  4380. *
  4381. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4382. * @return void
  4383. */
  4384. public function testSaveAllEmptyData() {
  4385. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4386. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4387. $model = new Article();
  4388. $result = $model->saveAll(array(), array('validate' => 'first'));
  4389. $this->assertFalse(empty($result));
  4390. $model = new ProductUpdateAll();
  4391. $result = $model->saveAll();
  4392. $this->assertFalse($result);
  4393. }
  4394. /**
  4395. * testSaveAssociated method
  4396. *
  4397. * @return void
  4398. */
  4399. public function testSaveAssociated() {
  4400. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  4401. $TestModel = new Post();
  4402. $result = $TestModel->find('all');
  4403. $this->assertEquals(3, count($result));
  4404. $this->assertFalse(isset($result[3]));
  4405. $TestModel->saveAssociated(array(
  4406. 'Post' => array(
  4407. 'title' => 'Post with Author',
  4408. 'body' => 'This post will be saved with an author'
  4409. ),
  4410. 'Author' => array(
  4411. 'user' => 'bob',
  4412. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  4413. )));
  4414. $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
  4415. $expected = array(
  4416. 'Post' => array(
  4417. 'id' => '4',
  4418. 'author_id' => '5',
  4419. 'title' => 'Post with Author',
  4420. 'body' => 'This post will be saved with an author',
  4421. 'published' => 'N'
  4422. ),
  4423. 'Author' => array(
  4424. 'id' => '5',
  4425. 'user' => 'bob',
  4426. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  4427. 'test' => 'working'
  4428. ));
  4429. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4430. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4431. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  4432. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  4433. unset(
  4434. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  4435. $result[3]['Author']['updated'], $result[3]['Author']['created']
  4436. );
  4437. $this->assertEquals($expected, $result[3]);
  4438. $this->assertEquals(4, count($result));
  4439. $TestModel = new Comment();
  4440. $result = $TestModel->saveAssociated(array(
  4441. 'Comment' => array(
  4442. 'article_id' => 2,
  4443. 'user_id' => 2,
  4444. 'comment' => 'New comment with attachment',
  4445. 'published' => 'Y'
  4446. ),
  4447. 'Attachment' => array(
  4448. 'attachment' => 'some_file.tgz'
  4449. )));
  4450. $this->assertFalse(empty($result));
  4451. $result = $TestModel->find('all');
  4452. $expected = array(
  4453. 'id' => '7',
  4454. 'article_id' => '2',
  4455. 'user_id' => '2',
  4456. 'comment' => 'New comment with attachment',
  4457. 'published' => 'Y'
  4458. );
  4459. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  4460. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  4461. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  4462. $this->assertEquals($expected, $result[6]['Comment']);
  4463. $expected = array(
  4464. 'id' => '2',
  4465. 'comment_id' => '7',
  4466. 'attachment' => 'some_file.tgz'
  4467. );
  4468. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  4469. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  4470. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  4471. $this->assertEquals($expected, $result[6]['Attachment']);
  4472. }
  4473. /**
  4474. * Test that validate = first, atomic = false works when associated records
  4475. * fail validation.
  4476. *
  4477. * @return void
  4478. */
  4479. public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
  4480. $this->loadFixtures('Comment', 'Article', 'User');
  4481. $Article = ClassRegistry::init('Article');
  4482. $Article->Comment->validator()->add('comment', array(
  4483. array('rule' => 'notEmpty')
  4484. ));
  4485. $data = array(
  4486. 'Article' => array(
  4487. 'user_id' => 1,
  4488. 'title' => 'Foo',
  4489. 'body' => 'text',
  4490. 'published' => 'N'
  4491. ),
  4492. 'Comment' => array(
  4493. array(
  4494. 'user_id' => 1,
  4495. 'comment' => '',
  4496. 'published' => 'N',
  4497. )
  4498. ),
  4499. );
  4500. $Article->saveAssociated(
  4501. $data,
  4502. array('validate' => 'first', 'atomic' => false)
  4503. );
  4504. $result = $Article->validationErrors;
  4505. $expected = array(
  4506. 'Comment' => array(
  4507. array(
  4508. 'comment' => array('This field cannot be left blank')
  4509. )
  4510. )
  4511. );
  4512. $this->assertEquals($expected, $result);
  4513. }
  4514. /**
  4515. * testSaveMany method
  4516. *
  4517. * @return void
  4518. */
  4519. public function testSaveMany() {
  4520. $this->loadFixtures('Post');
  4521. $TestModel = new Post();
  4522. $TestModel->deleteAll(true);
  4523. $this->assertEquals(array(), $TestModel->find('all'));
  4524. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  4525. $this->db->truncate($TestModel);
  4526. $TestModel->saveMany(array(
  4527. array(
  4528. 'title' => 'Multi-record post 1',
  4529. 'body' => 'First multi-record post',
  4530. 'author_id' => 2
  4531. ),
  4532. array(
  4533. 'title' => 'Multi-record post 2',
  4534. 'body' => 'Second multi-record post',
  4535. 'author_id' => 2
  4536. )));
  4537. $result = $TestModel->find('all', array(
  4538. 'recursive' => -1,
  4539. 'order' => 'Post.id ASC'
  4540. ));
  4541. $expected = array(
  4542. array(
  4543. 'Post' => array(
  4544. 'id' => '1',
  4545. 'author_id' => '2',
  4546. 'title' => 'Multi-record post 1',
  4547. 'body' => 'First multi-record post',
  4548. 'published' => 'N'
  4549. )
  4550. ),
  4551. array(
  4552. 'Post' => array(
  4553. 'id' => '2',
  4554. 'author_id' => '2',
  4555. 'title' => 'Multi-record post 2',
  4556. 'body' => 'Second multi-record post',
  4557. 'published' => 'N'
  4558. )
  4559. )
  4560. );
  4561. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4562. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  4563. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4564. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  4565. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  4566. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  4567. $this->assertEquals($expected, $result);
  4568. }
  4569. /**
  4570. * Test SaveMany with validate=false.
  4571. *
  4572. * @return void
  4573. */
  4574. public function testSaveManyValidateFalse() {
  4575. $this->loadFixtures('Post');
  4576. $TestModel = new Post();
  4577. $TestModel->deleteAll(true);
  4578. $data = array(
  4579. array('id' => 1, 'author_id' => 1, 'title' => 'hi'),
  4580. array('id' => 2, 'author_id' => 1, 'title' => 'bye')
  4581. );
  4582. $result = $TestModel->saveAll($data, array('validate' => false));
  4583. $this->assertTrue($result);
  4584. }
  4585. /**
  4586. * Test SaveAssociated with Habtm relations
  4587. *
  4588. * @return void
  4589. */
  4590. public function testSaveAssociatedHabtm() {
  4591. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4592. $data = array(
  4593. 'Article' => array(
  4594. 'user_id' => 1,
  4595. 'title' => 'Article Has and belongs to Many Tags'
  4596. ),
  4597. 'Tag' => array(
  4598. 'Tag' => array(1, 2)
  4599. ),
  4600. 'Comment' => array(
  4601. array(
  4602. 'comment' => 'Article comment',
  4603. 'user_id' => 1
  4604. )));
  4605. $Article = new Article();
  4606. $result = $Article->saveAssociated($data);
  4607. $this->assertFalse(empty($result));
  4608. $result = $Article->read();
  4609. $this->assertEquals(2, count($result['Tag']));
  4610. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  4611. $this->assertEquals(1, count($result['Comment']));
  4612. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  4613. }
  4614. /**
  4615. * Test SaveAssociated with Habtm relations and extra join table fields
  4616. *
  4617. * @return void
  4618. */
  4619. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  4620. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  4621. $data = array(
  4622. 'Something' => array(
  4623. 'id' => 4,
  4624. 'title' => 'Extra Fields',
  4625. 'body' => 'Extra Fields Body',
  4626. 'published' => '1'
  4627. ),
  4628. 'SomethingElse' => array(
  4629. array('something_else_id' => 1, 'doomed' => '1'),
  4630. array('something_else_id' => 2, 'doomed' => '0'),
  4631. array('something_else_id' => 3, 'doomed' => '1')
  4632. )
  4633. );
  4634. $Something = new Something();
  4635. $result = $Something->saveAssociated($data);
  4636. $this->assertFalse(empty($result));
  4637. $result = $Something->read();
  4638. $this->assertEquals(3, count($result['SomethingElse']));
  4639. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  4640. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  4641. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  4642. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  4643. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  4644. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  4645. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  4646. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  4647. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  4648. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  4649. }
  4650. /**
  4651. * testSaveAssociatedHasOne method
  4652. *
  4653. * @return void
  4654. */
  4655. public function testSaveAssociatedHasOne() {
  4656. $model = new Comment();
  4657. $model->deleteAll(true);
  4658. $this->assertEquals(array(), $model->find('all'));
  4659. $model->Attachment->deleteAll(true);
  4660. $this->assertEquals(array(), $model->Attachment->find('all'));
  4661. $this->assertTrue($model->saveAssociated(array(
  4662. 'Comment' => array(
  4663. 'comment' => 'Comment with attachment',
  4664. 'article_id' => 1,
  4665. 'user_id' => 1
  4666. ),
  4667. 'Attachment' => array(
  4668. 'attachment' => 'some_file.zip'
  4669. ))));
  4670. $result = $model->find('all', array('fields' => array(
  4671. 'Comment.id', 'Comment.comment', 'Attachment.id',
  4672. 'Attachment.comment_id', 'Attachment.attachment'
  4673. )));
  4674. $expected = array(array(
  4675. 'Comment' => array(
  4676. 'id' => '1',
  4677. 'comment' => 'Comment with attachment'
  4678. ),
  4679. 'Attachment' => array(
  4680. 'id' => '1',
  4681. 'comment_id' => '1',
  4682. 'attachment' => 'some_file.zip'
  4683. )));
  4684. $this->assertEquals($expected, $result);
  4685. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  4686. $data = array(
  4687. 'Comment' => array(
  4688. 'comment' => 'Comment with attachment',
  4689. 'article_id' => 1,
  4690. 'user_id' => 1
  4691. ),
  4692. 'Attachment' => array(
  4693. 'attachment' => 'some_file.zip'
  4694. ));
  4695. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  4696. }
  4697. /**
  4698. * testSaveAssociatedBelongsTo method
  4699. *
  4700. * @return void
  4701. */
  4702. public function testSaveAssociatedBelongsTo() {
  4703. $model = new Comment();
  4704. $model->deleteAll(true);
  4705. $this->assertEquals(array(), $model->find('all'));
  4706. $model->Article->deleteAll(true);
  4707. $this->assertEquals(array(), $model->Article->find('all'));
  4708. $this->assertTrue($model->saveAssociated(array(
  4709. 'Comment' => array(
  4710. 'comment' => 'Article comment',
  4711. 'article_id' => 1,
  4712. 'user_id' => 1
  4713. ),
  4714. 'Article' => array(
  4715. 'title' => 'Model Associations 101',
  4716. 'user_id' => 1
  4717. ))));
  4718. $result = $model->find('all', array('fields' => array(
  4719. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  4720. )));
  4721. $expected = array(array(
  4722. 'Comment' => array(
  4723. 'id' => '1',
  4724. 'article_id' => '1',
  4725. 'comment' => 'Article comment'
  4726. ),
  4727. 'Article' => array(
  4728. 'id' => '1',
  4729. 'title' => 'Model Associations 101'
  4730. )));
  4731. $this->assertEquals($expected, $result);
  4732. }
  4733. /**
  4734. * testSaveAssociatedHasOneValidation method
  4735. *
  4736. * @return void
  4737. */
  4738. public function testSaveAssociatedHasOneValidation() {
  4739. $model = new Comment();
  4740. $model->deleteAll(true);
  4741. $this->assertEquals(array(), $model->find('all'));
  4742. $model->Attachment->deleteAll(true);
  4743. $this->assertEquals(array(), $model->Attachment->find('all'));
  4744. $model->validate = array('comment' => 'notEmpty');
  4745. $model->Attachment->validate = array('attachment' => 'notEmpty');
  4746. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  4747. $result = $model->saveAssociated(
  4748. array(
  4749. 'Comment' => array(
  4750. 'comment' => '',
  4751. 'article_id' => 1,
  4752. 'user_id' => 1
  4753. ),
  4754. 'Attachment' => array('attachment' => '')
  4755. )
  4756. );
  4757. $this->assertFalse($result);
  4758. $expected = array(
  4759. 'comment' => array(
  4760. 'This field cannot be left blank'
  4761. ),
  4762. 'Attachment' => array(
  4763. 'attachment' => array(
  4764. 'This field cannot be left blank'
  4765. )
  4766. )
  4767. );
  4768. $this->assertEquals($expected, $model->validationErrors);
  4769. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  4770. }
  4771. /**
  4772. * testSaveAssociatedAtomic method
  4773. *
  4774. * @return void
  4775. */
  4776. public function testSaveAssociatedAtomic() {
  4777. $this->loadFixtures('Article', 'User');
  4778. $TestModel = new Article();
  4779. $result = $TestModel->saveAssociated(array(
  4780. 'Article' => array(
  4781. 'title' => 'Post with Author',
  4782. 'body' => 'This post will be saved with an author',
  4783. 'user_id' => 2
  4784. ),
  4785. 'Comment' => array(
  4786. array('comment' => 'First new comment', 'user_id' => 2))
  4787. ), array('atomic' => false));
  4788. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  4789. $result = $TestModel->saveAssociated(array(
  4790. 'Article' => array('id' => 2),
  4791. 'Comment' => array(
  4792. array(
  4793. 'comment' => 'First new comment',
  4794. 'published' => 'Y',
  4795. 'user_id' => 1
  4796. ),
  4797. array(
  4798. 'comment' => 'Second new comment',
  4799. 'published' => 'Y',
  4800. 'user_id' => 2
  4801. ))
  4802. ), array('validate' => true, 'atomic' => false));
  4803. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  4804. }
  4805. /**
  4806. * testSaveManyAtomic method
  4807. *
  4808. * @return void
  4809. */
  4810. public function testSaveManyAtomic() {
  4811. $this->loadFixtures('Article', 'User');
  4812. $TestModel = new Article();
  4813. $result = $TestModel->saveMany(array(
  4814. array(
  4815. 'id' => '1',
  4816. 'title' => 'Baleeted First Post',
  4817. 'body' => 'Baleeted!',
  4818. 'published' => 'N'
  4819. ),
  4820. array(
  4821. 'id' => '2',
  4822. 'title' => 'Just update the title'
  4823. ),
  4824. array(
  4825. 'title' => 'Creating a fourth post',
  4826. 'body' => 'Fourth post body',
  4827. 'user_id' => 2
  4828. )
  4829. ), array('atomic' => false));
  4830. $this->assertSame($result, array(true, true, true));
  4831. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4832. $result = $TestModel->saveMany(array(
  4833. array(
  4834. 'id' => '1',
  4835. 'title' => 'Un-Baleeted First Post',
  4836. 'body' => 'Not Baleeted!',
  4837. 'published' => 'Y'
  4838. ),
  4839. array(
  4840. 'id' => '2',
  4841. 'title' => '',
  4842. 'body' => 'Trying to get away with an empty title'
  4843. )
  4844. ), array('validate' => true, 'atomic' => false));
  4845. $this->assertSame(array(true, false), $result);
  4846. }
  4847. /**
  4848. * testSaveAssociatedHasMany method
  4849. *
  4850. * @return void
  4851. */
  4852. public function testSaveAssociatedHasMany() {
  4853. $this->loadFixtures('Article', 'Comment');
  4854. $TestModel = new Article();
  4855. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4856. $result = $TestModel->saveAssociated(array(
  4857. 'Article' => array('id' => 2),
  4858. 'Comment' => array(
  4859. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  4860. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  4861. )
  4862. ));
  4863. $this->assertFalse(empty($result));
  4864. $result = $TestModel->findById(2);
  4865. $expected = array(
  4866. 'First Comment for Second Article',
  4867. 'Second Comment for Second Article',
  4868. 'First new comment',
  4869. 'Second new comment'
  4870. );
  4871. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4872. $result = $TestModel->saveAssociated(
  4873. array(
  4874. 'Article' => array('id' => 2),
  4875. 'Comment' => array(
  4876. array(
  4877. 'comment' => 'Third new comment',
  4878. 'published' => 'Y',
  4879. 'user_id' => 1
  4880. ))),
  4881. array('atomic' => false)
  4882. );
  4883. $this->assertFalse(empty($result));
  4884. $result = $TestModel->findById(2);
  4885. $expected = array(
  4886. 'First Comment for Second Article',
  4887. 'Second Comment for Second Article',
  4888. 'First new comment',
  4889. 'Second new comment',
  4890. 'Third new comment'
  4891. );
  4892. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4893. $TestModel->beforeSaveReturn = false;
  4894. $result = $TestModel->saveAssociated(
  4895. array(
  4896. 'Article' => array('id' => 2),
  4897. 'Comment' => array(
  4898. array(
  4899. 'comment' => 'Fourth new comment',
  4900. 'published' => 'Y',
  4901. 'user_id' => 1
  4902. ))),
  4903. array('atomic' => false)
  4904. );
  4905. $this->assertEquals(array('Article' => false), $result);
  4906. $result = $TestModel->findById(2);
  4907. $expected = array(
  4908. 'First Comment for Second Article',
  4909. 'Second Comment for Second Article',
  4910. 'First new comment',
  4911. 'Second new comment',
  4912. 'Third new comment'
  4913. );
  4914. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4915. }
  4916. /**
  4917. * testSaveAssociatedHasManyEmpty method
  4918. *
  4919. * @return void
  4920. */
  4921. public function testSaveAssociatedHasManyEmpty() {
  4922. $this->loadFixtures('Article', 'Comment');
  4923. $TestModel = new Article();
  4924. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4925. $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  4926. //empty hasMany data is ignored in save
  4927. $result = $TestModel->saveAssociated(array(
  4928. 'Article' => array('title' => 'title', 'user_id' => 1),
  4929. 'Comment' => array()
  4930. ), array('validate' => true));
  4931. $this->assertTrue($result);
  4932. $result = $TestModel->saveAssociated(array(
  4933. 'Article' => array('title' => 'title', 'user_id' => 1),
  4934. 'Comment' => array()
  4935. ), array('validate' => true, 'atomic' => false));
  4936. $this->assertEquals(array('Article' => true), $result);
  4937. //empty primary data is not ignored
  4938. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
  4939. $this->assertFalse($result);
  4940. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
  4941. $this->assertEquals(array('Article' => false), $result);
  4942. }
  4943. /**
  4944. * testSaveAssociatedHasManyValidation method
  4945. *
  4946. * @return void
  4947. */
  4948. public function testSaveAssociatedHasManyValidation() {
  4949. $this->loadFixtures('Article', 'Comment');
  4950. $TestModel = new Article();
  4951. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4952. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4953. $result = $TestModel->saveAssociated(array(
  4954. 'Article' => array('id' => 2),
  4955. 'Comment' => array(
  4956. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  4957. )
  4958. ), array('validate' => true));
  4959. $this->assertFalse($result);
  4960. $expected = array('Comment' => array(
  4961. array('comment' => array('This field cannot be left blank'))
  4962. ));
  4963. $this->assertEquals($expected, $TestModel->validationErrors);
  4964. $expected = array(
  4965. array('comment' => array('This field cannot be left blank'))
  4966. );
  4967. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4968. $result = $TestModel->saveAssociated(array(
  4969. 'Article' => array('id' => 2),
  4970. 'Comment' => array(
  4971. array(
  4972. 'comment' => '',
  4973. 'published' => 'Y',
  4974. 'user_id' => 1
  4975. ))
  4976. ), array('validate' => 'first'));
  4977. $this->assertFalse($result);
  4978. }
  4979. /**
  4980. * test saveMany with transactions and ensure there is no missing rollback.
  4981. *
  4982. * @return void
  4983. */
  4984. public function testSaveManyTransactionNoRollback() {
  4985. $this->loadFixtures('Post');
  4986. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  4987. $db = ConnectionManager::create('mock_many_transaction', array(
  4988. 'datasource' => 'MockManyTransactionDboSource',
  4989. ));
  4990. $db->expects($this->once())
  4991. ->method('describe')
  4992. ->will($this->returnValue(array()));
  4993. $db->expects($this->once())->method('rollback');
  4994. $Post = new Post('mock_many_transaction');
  4995. $Post->validate = array(
  4996. 'title' => array('rule' => array('notEmpty'))
  4997. );
  4998. $data = array(
  4999. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5000. array('author_id' => 1, 'title' => '')
  5001. );
  5002. $Post->saveMany($data);
  5003. }
  5004. /**
  5005. * test saveAssociated with transactions and ensure there is no missing rollback.
  5006. *
  5007. * @return void
  5008. */
  5009. public function testSaveAssociatedTransactionNoRollback() {
  5010. $testDb = ConnectionManager::getDataSource('test');
  5011. $this->getMock(
  5012. 'DboSource',
  5013. array('connect', 'rollback', 'describe', 'create', 'begin'),
  5014. array(),
  5015. 'MockAssociatedTransactionDboSource',
  5016. false
  5017. );
  5018. $db = ConnectionManager::create('mock_assoc_transaction', array(
  5019. 'datasource' => 'MockAssociatedTransactionDboSource',
  5020. ));
  5021. $this->mockObjects[] = $db;
  5022. $db->columns = $testDb->columns;
  5023. $db->expects($this->once())->method('rollback');
  5024. $db->expects($this->any())->method('describe')
  5025. ->will($this->returnValue(array(
  5026. 'id' => array('type' => 'integer', 'length' => 11),
  5027. 'title' => array('type' => 'string'),
  5028. 'body' => array('type' => 'text'),
  5029. 'published' => array('type' => 'string')
  5030. )));
  5031. $Post = new Post();
  5032. $Post->useDbConfig = 'mock_assoc_transaction';
  5033. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  5034. $Post->Author->validate = array(
  5035. 'user' => array('rule' => array('notEmpty'))
  5036. );
  5037. $data = array(
  5038. 'Post' => array(
  5039. 'title' => 'New post',
  5040. 'body' => 'Content',
  5041. 'published' => 'Y'
  5042. ),
  5043. 'Author' => array(
  5044. 'user' => '',
  5045. 'password' => "sekret"
  5046. )
  5047. );
  5048. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  5049. }
  5050. /**
  5051. * test saveMany with nested saveMany call.
  5052. *
  5053. * @return void
  5054. */
  5055. public function testSaveManyNestedSaveMany() {
  5056. $this->loadFixtures('Sample');
  5057. $TransactionManyTestModel = new TransactionManyTestModel();
  5058. $data = array(
  5059. array('apple_id' => 1, 'name' => 'sample5'),
  5060. );
  5061. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  5062. }
  5063. /**
  5064. * testSaveManyTransaction method
  5065. *
  5066. * @return void
  5067. */
  5068. public function testSaveManyTransaction() {
  5069. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5070. $TestModel = new Post();
  5071. $TestModel->validate = array('title' => 'notEmpty');
  5072. $data = array(
  5073. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5074. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5075. array('author_id' => 1, 'title' => '')
  5076. );
  5077. $this->assertFalse($TestModel->saveMany($data));
  5078. $result = $TestModel->find('all', array('recursive' => -1));
  5079. $expected = array(
  5080. array('Post' => array(
  5081. 'id' => '1',
  5082. 'author_id' => 1,
  5083. 'title' => 'First Post',
  5084. 'body' => 'First Post Body',
  5085. 'published' => 'Y',
  5086. 'created' => '2007-03-18 10:39:23',
  5087. 'updated' => '2007-03-18 10:41:31'
  5088. )),
  5089. array('Post' => array(
  5090. 'id' => '2',
  5091. 'author_id' => 3,
  5092. 'title' => 'Second Post',
  5093. 'body' => 'Second Post Body',
  5094. 'published' => 'Y',
  5095. 'created' => '2007-03-18 10:41:23',
  5096. 'updated' => '2007-03-18 10:43:31'
  5097. )),
  5098. array('Post' => array(
  5099. 'id' => '3',
  5100. 'author_id' => 1,
  5101. 'title' => 'Third Post',
  5102. 'body' => 'Third Post Body',
  5103. 'published' => 'Y',
  5104. 'created' => '2007-03-18 10:43:23',
  5105. 'updated' => '2007-03-18 10:45:31'
  5106. )));
  5107. if (count($result) != 3) {
  5108. // Database doesn't support transactions
  5109. $expected[] = array(
  5110. 'Post' => array(
  5111. 'id' => '4',
  5112. 'author_id' => 1,
  5113. 'title' => 'New Fourth Post',
  5114. 'body' => null,
  5115. 'published' => 'N'
  5116. ));
  5117. $expected[] = array(
  5118. 'Post' => array(
  5119. 'id' => '5',
  5120. 'author_id' => 1,
  5121. 'title' => 'New Fifth Post',
  5122. 'body' => null,
  5123. 'published' => 'N',
  5124. ));
  5125. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5126. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5127. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5128. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5129. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5130. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5131. $this->assertEquals($expected, $result);
  5132. // Skip the rest of the transactional tests
  5133. return;
  5134. }
  5135. $this->assertEquals($expected, $result);
  5136. $data = array(
  5137. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5138. array('author_id' => 1, 'title' => ''),
  5139. array('author_id' => 1, 'title' => 'New Sixth Post')
  5140. );
  5141. $this->assertFalse($TestModel->saveMany($data));
  5142. $result = $TestModel->find('all', array('recursive' => -1));
  5143. $expected = array(
  5144. array('Post' => array(
  5145. 'id' => '1',
  5146. 'author_id' => 1,
  5147. 'title' => 'First Post',
  5148. 'body' => 'First Post Body',
  5149. 'published' => 'Y',
  5150. 'created' => '2007-03-18 10:39:23',
  5151. 'updated' => '2007-03-18 10:41:31'
  5152. )),
  5153. array('Post' => array(
  5154. 'id' => '2',
  5155. 'author_id' => 3,
  5156. 'title' => 'Second Post',
  5157. 'body' => 'Second Post Body',
  5158. 'published' => 'Y',
  5159. 'created' => '2007-03-18 10:41:23',
  5160. 'updated' => '2007-03-18 10:43:31'
  5161. )),
  5162. array('Post' => array(
  5163. 'id' => '3',
  5164. 'author_id' => 1,
  5165. 'title' => 'Third Post',
  5166. 'body' => 'Third Post Body',
  5167. 'published' => 'Y',
  5168. 'created' => '2007-03-18 10:43:23',
  5169. 'updated' => '2007-03-18 10:45:31'
  5170. )));
  5171. if (count($result) != 3) {
  5172. // Database doesn't support transactions
  5173. $expected[] = array(
  5174. 'Post' => array(
  5175. 'id' => '4',
  5176. 'author_id' => 1,
  5177. 'title' => 'New Fourth Post',
  5178. 'body' => 'Third Post Body',
  5179. 'published' => 'N'
  5180. ));
  5181. $expected[] = array(
  5182. 'Post' => array(
  5183. 'id' => '5',
  5184. 'author_id' => 1,
  5185. 'title' => 'Third Post',
  5186. 'body' => 'Third Post Body',
  5187. 'published' => 'N'
  5188. ));
  5189. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5190. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5191. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5192. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5193. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5194. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5195. }
  5196. $this->assertEquals($expected, $result);
  5197. $TestModel->validate = array('title' => 'notEmpty');
  5198. $data = array(
  5199. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5200. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5201. array('author_id' => 1, 'title' => 'New Sixth Post')
  5202. );
  5203. $this->assertTrue($TestModel->saveMany($data));
  5204. $result = $TestModel->find('all', array(
  5205. 'recursive' => -1,
  5206. 'fields' => array('author_id', 'title', 'body', 'published'),
  5207. 'order' => array('Post.created' => 'ASC')
  5208. ));
  5209. $expected = array(
  5210. array('Post' => array(
  5211. 'author_id' => 1,
  5212. 'title' => 'First Post',
  5213. 'body' => 'First Post Body',
  5214. 'published' => 'Y'
  5215. )),
  5216. array('Post' => array(
  5217. 'author_id' => 3,
  5218. 'title' => 'Second Post',
  5219. 'body' => 'Second Post Body',
  5220. 'published' => 'Y'
  5221. )),
  5222. array('Post' => array(
  5223. 'author_id' => 1,
  5224. 'title' => 'Third Post',
  5225. 'body' => 'Third Post Body',
  5226. 'published' => 'Y'
  5227. )),
  5228. array('Post' => array(
  5229. 'author_id' => 1,
  5230. 'title' => 'New Fourth Post',
  5231. 'body' => '',
  5232. 'published' => 'N'
  5233. )),
  5234. array('Post' => array(
  5235. 'author_id' => 1,
  5236. 'title' => 'New Fifth Post',
  5237. 'body' => '',
  5238. 'published' => 'N'
  5239. )),
  5240. array('Post' => array(
  5241. 'author_id' => 1,
  5242. 'title' => 'New Sixth Post',
  5243. 'body' => '',
  5244. 'published' => 'N'
  5245. )));
  5246. $this->assertEquals($expected, $result);
  5247. }
  5248. /**
  5249. * testSaveManyValidation method
  5250. *
  5251. * @return void
  5252. */
  5253. public function testSaveManyValidation() {
  5254. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5255. $TestModel = new Post();
  5256. $data = array(
  5257. array(
  5258. 'id' => '1',
  5259. 'title' => 'Baleeted First Post',
  5260. 'body' => 'Baleeted!',
  5261. 'published' => 'N'
  5262. ),
  5263. array(
  5264. 'id' => '2',
  5265. 'title' => 'Just update the title'
  5266. ),
  5267. array(
  5268. 'title' => 'Creating a fourth post',
  5269. 'body' => 'Fourth post body',
  5270. 'author_id' => 2
  5271. ));
  5272. $this->assertTrue($TestModel->saveMany($data));
  5273. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5274. $expected = array(
  5275. array(
  5276. 'Post' => array(
  5277. 'id' => '1',
  5278. 'author_id' => '1',
  5279. 'title' => 'Baleeted First Post',
  5280. 'body' => 'Baleeted!',
  5281. 'published' => 'N',
  5282. 'created' => '2007-03-18 10:39:23'
  5283. )
  5284. ),
  5285. array(
  5286. 'Post' => array(
  5287. 'id' => '2',
  5288. 'author_id' => '3',
  5289. 'title' => 'Just update the title',
  5290. 'body' => 'Second Post Body',
  5291. 'published' => 'Y',
  5292. 'created' => '2007-03-18 10:41:23'
  5293. )
  5294. ),
  5295. array(
  5296. 'Post' => array(
  5297. 'id' => '3',
  5298. 'author_id' => '1',
  5299. 'title' => 'Third Post',
  5300. 'body' => 'Third Post Body',
  5301. 'published' => 'Y',
  5302. 'created' => '2007-03-18 10:43:23',
  5303. 'updated' => '2007-03-18 10:45:31'
  5304. )),
  5305. array(
  5306. 'Post' => array(
  5307. 'id' => '4',
  5308. 'author_id' => '2',
  5309. 'title' => 'Creating a fourth post',
  5310. 'body' => 'Fourth post body',
  5311. 'published' => 'N'
  5312. )
  5313. )
  5314. );
  5315. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  5316. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  5317. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5318. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5319. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  5320. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5321. $this->assertEquals($expected, $result);
  5322. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5323. $data = array(
  5324. array(
  5325. 'id' => '1',
  5326. 'title' => 'Un-Baleeted First Post',
  5327. 'body' => 'Not Baleeted!',
  5328. 'published' => 'Y'
  5329. ),
  5330. array(
  5331. 'id' => '2',
  5332. 'title' => '',
  5333. 'body' => 'Trying to get away with an empty title'
  5334. ));
  5335. $result = $TestModel->saveMany($data);
  5336. $this->assertFalse($result);
  5337. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5338. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5339. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  5340. if (!$transactionWorked) {
  5341. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  5342. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  5343. }
  5344. $this->assertEquals($errors, $TestModel->validationErrors);
  5345. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5346. $data = array(
  5347. array(
  5348. 'id' => '1',
  5349. 'title' => 'Un-Baleeted First Post',
  5350. 'body' => 'Not Baleeted!',
  5351. 'published' => 'Y'
  5352. ),
  5353. array(
  5354. 'id' => '2',
  5355. 'title' => '',
  5356. 'body' => 'Trying to get away with an empty title'
  5357. ));
  5358. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  5359. $this->assertEquals(array(true, false), $result);
  5360. $result = $TestModel->find('all', array(
  5361. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5362. 'recursive' => -1,
  5363. 'order' => 'Post.id ASC'
  5364. ));
  5365. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5366. $expected = array(
  5367. array(
  5368. 'Post' => array(
  5369. 'id' => '1',
  5370. 'author_id' => '1',
  5371. 'title' => 'Un-Baleeted First Post',
  5372. 'body' => 'Not Baleeted!',
  5373. 'published' => 'Y',
  5374. )),
  5375. array(
  5376. 'Post' => array(
  5377. 'id' => '2',
  5378. 'author_id' => '3',
  5379. 'title' => 'Just update the title',
  5380. 'body' => 'Second Post Body',
  5381. 'published' => 'Y',
  5382. )),
  5383. array(
  5384. 'Post' => array(
  5385. 'id' => '3',
  5386. 'author_id' => '1',
  5387. 'title' => 'Third Post',
  5388. 'body' => 'Third Post Body',
  5389. 'published' => 'Y',
  5390. )),
  5391. array(
  5392. 'Post' => array(
  5393. 'id' => '4',
  5394. 'author_id' => '2',
  5395. 'title' => 'Creating a fourth post',
  5396. 'body' => 'Fourth post body',
  5397. 'published' => 'N',
  5398. )));
  5399. $this->assertEquals($expected, $result);
  5400. $this->assertEquals($errors, $TestModel->validationErrors);
  5401. $data = array(
  5402. array(
  5403. 'id' => '1',
  5404. 'title' => 'Re-Baleeted First Post',
  5405. 'body' => 'Baleeted!',
  5406. 'published' => 'N'
  5407. ),
  5408. array(
  5409. 'id' => '2',
  5410. 'title' => '',
  5411. 'body' => 'Trying to get away with an empty title'
  5412. ));
  5413. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  5414. $result = $TestModel->find('all', array(
  5415. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5416. 'recursive' => -1,
  5417. 'order' => 'Post.id ASC'
  5418. ));
  5419. $this->assertEquals($expected, $result);
  5420. $this->assertEquals($errors, $TestModel->validationErrors);
  5421. }
  5422. /**
  5423. * testValidateMany method
  5424. *
  5425. * @return void
  5426. */
  5427. public function testValidateMany() {
  5428. $TestModel = new Article();
  5429. $TestModel->validate = array('title' => 'notEmpty');
  5430. $data = array(
  5431. 0 => array('title' => ''),
  5432. 1 => array('title' => 'title 1'),
  5433. 2 => array('title' => 'title 2'),
  5434. );
  5435. $result = $TestModel->validateMany($data);
  5436. $this->assertFalse($result);
  5437. $expected = array(
  5438. 0 => array('title' => array('This field cannot be left blank')),
  5439. );
  5440. $this->assertEquals($expected, $TestModel->validationErrors);
  5441. $data = array(
  5442. 0 => array('title' => 'title 0'),
  5443. 1 => array('title' => ''),
  5444. 2 => array('title' => 'title 2'),
  5445. );
  5446. $result = $TestModel->validateMany($data);
  5447. $this->assertFalse($result);
  5448. $expected = array(
  5449. 1 => array('title' => array('This field cannot be left blank')),
  5450. );
  5451. $this->assertEquals($expected, $TestModel->validationErrors);
  5452. }
  5453. /**
  5454. * testSaveAssociatedValidateFirst method
  5455. *
  5456. * @return void
  5457. */
  5458. public function testSaveAssociatedValidateFirst() {
  5459. $this->loadFixtures('Article', 'Comment', 'Attachment');
  5460. $model = new Article();
  5461. $model->deleteAll(true);
  5462. $model->Comment->validate = array('comment' => 'notEmpty');
  5463. $result = $model->saveAssociated(array(
  5464. 'Article' => array(
  5465. 'title' => 'Post with Author',
  5466. 'body' => 'This post will be saved author'
  5467. ),
  5468. 'Comment' => array(
  5469. array('comment' => 'First new comment'),
  5470. array('comment' => '')
  5471. )
  5472. ), array('validate' => 'first'));
  5473. $this->assertFalse($result);
  5474. $result = $model->find('all');
  5475. $this->assertSame(array(), $result);
  5476. $expected = array('Comment' => array(
  5477. 1 => array('comment' => array('This field cannot be left blank'))
  5478. ));
  5479. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  5480. $this->assertSame($model->Comment->find('count'), 0);
  5481. $result = $model->saveAssociated(
  5482. array(
  5483. 'Article' => array(
  5484. 'title' => 'Post with Author',
  5485. 'body' => 'This post will be saved with an author',
  5486. 'user_id' => 2
  5487. ),
  5488. 'Comment' => array(
  5489. array(
  5490. 'comment' => 'Only new comment',
  5491. 'user_id' => 2
  5492. ))),
  5493. array('validate' => 'first')
  5494. );
  5495. $this->assertTrue($result);
  5496. $result = $model->Comment->find('all');
  5497. $this->assertSame(count($result), 1);
  5498. $result = Hash::extract($result, '{n}.Comment.article_id');
  5499. $this->assertEquals(4, $result[0]);
  5500. $model->deleteAll(true);
  5501. $data = array(
  5502. 'Article' => array(
  5503. 'title' => 'Post with Author saveAlled from comment',
  5504. 'body' => 'This post will be saved with an author',
  5505. 'user_id' => 2
  5506. ),
  5507. 'Comment' => array(
  5508. 'comment' => 'Only new comment', 'user_id' => 2
  5509. ));
  5510. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  5511. $this->assertFalse(empty($result));
  5512. $result = $model->find('all');
  5513. $this->assertEquals(
  5514. 'Post with Author saveAlled from comment',
  5515. $result[0]['Article']['title']
  5516. );
  5517. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  5518. }
  5519. /**
  5520. * test saveMany()'s return is correct when using atomic = false and validate = first.
  5521. *
  5522. * @return void
  5523. */
  5524. public function testSaveManyValidateFirstAtomicFalse() {
  5525. $Something = new Something();
  5526. $invalidData = array(
  5527. array(
  5528. 'title' => 'foo',
  5529. 'body' => 'bar',
  5530. 'published' => 'baz',
  5531. ),
  5532. array(
  5533. 'body' => 3,
  5534. 'published' => 'sd',
  5535. ),
  5536. );
  5537. $Something->create();
  5538. $Something->validate = array(
  5539. 'title' => array(
  5540. 'rule' => 'alphaNumeric',
  5541. 'required' => true,
  5542. ),
  5543. 'body' => array(
  5544. 'rule' => 'alphaNumeric',
  5545. 'required' => true,
  5546. 'allowEmpty' => true,
  5547. ),
  5548. );
  5549. $result = $Something->saveMany($invalidData, array(
  5550. 'atomic' => false,
  5551. 'validate' => 'first',
  5552. ));
  5553. $expected = array(true, false);
  5554. $this->assertEquals($expected, $result);
  5555. $Something = new Something();
  5556. $validData = array(
  5557. array(
  5558. 'title' => 'title value',
  5559. 'body' => 'body value',
  5560. 'published' => 'baz',
  5561. ),
  5562. array(
  5563. 'title' => 'valid',
  5564. 'body' => 'this body',
  5565. 'published' => 'sd',
  5566. ),
  5567. );
  5568. $Something->create();
  5569. $result = $Something->saveMany($validData, array(
  5570. 'atomic' => false,
  5571. 'validate' => 'first',
  5572. ));
  5573. $expected = array(true, true);
  5574. $this->assertEquals($expected, $result);
  5575. }
  5576. /**
  5577. * testValidateAssociated method
  5578. *
  5579. * @return void
  5580. */
  5581. public function testValidateAssociated() {
  5582. $this->loadFixtures('Attachment', 'Article', 'Comment');
  5583. $TestModel = new Comment();
  5584. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5585. $data = array(
  5586. 'Comment' => array(
  5587. 'comment' => 'This is the comment'
  5588. ),
  5589. 'Attachment' => array(
  5590. 'attachment' => ''
  5591. )
  5592. );
  5593. $result = $TestModel->validateAssociated($data);
  5594. $this->assertFalse($result);
  5595. $TestModel = new Article();
  5596. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5597. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5598. $data = array(
  5599. 'Article' => array('id' => 2),
  5600. 'Comment' => array(
  5601. array(
  5602. 'id' => 1,
  5603. 'comment' => '',
  5604. 'published' => 'Y',
  5605. 'user_id' => 1),
  5606. array(
  5607. 'id' => 2,
  5608. 'comment' =>
  5609. 'comment',
  5610. 'published' => 'Y',
  5611. 'user_id' => 1
  5612. )));
  5613. $result = $TestModel->validateAssociated($data);
  5614. $this->assertFalse($result);
  5615. $data = array(
  5616. 'Article' => array('id' => 2),
  5617. 'Comment' => array(
  5618. array(
  5619. 'id' => 1,
  5620. 'comment' => '',
  5621. 'published' => 'Y',
  5622. 'user_id' => 1
  5623. ),
  5624. array(
  5625. 'id' => 2,
  5626. 'comment' => 'comment',
  5627. 'published' => 'Y',
  5628. 'user_id' => 1
  5629. ),
  5630. array(
  5631. 'id' => 3,
  5632. 'comment' => '',
  5633. 'published' => 'Y',
  5634. 'user_id' => 1
  5635. )));
  5636. $result = $TestModel->validateAssociated($data, array(
  5637. 'atomic' => false
  5638. ));
  5639. $expected = array(
  5640. 'Article' => true,
  5641. 'Comment' => array(false, true, false)
  5642. );
  5643. $this->assertSame($expected, $result);
  5644. $expected = array('Comment' => array(
  5645. 0 => array('comment' => array('This field cannot be left blank')),
  5646. 2 => array('comment' => array('This field cannot be left blank'))
  5647. ));
  5648. $this->assertEquals($expected, $TestModel->validationErrors);
  5649. $expected = array(
  5650. 0 => array('comment' => array('This field cannot be left blank')),
  5651. 2 => array('comment' => array('This field cannot be left blank'))
  5652. );
  5653. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5654. }
  5655. /**
  5656. * test that saveMany behaves like plain save() when suplied empty data
  5657. *
  5658. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5659. * @return void
  5660. */
  5661. public function testSaveManyEmptyData() {
  5662. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5663. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5664. $model = new Article();
  5665. $result = $model->saveMany(array(), array('validate' => true));
  5666. $this->assertFalse(empty($result));
  5667. $model = new ProductUpdateAll();
  5668. $result = $model->saveMany(array());
  5669. $this->assertFalse($result);
  5670. }
  5671. /**
  5672. * test that saveAssociated behaves like plain save() when supplied empty data
  5673. *
  5674. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5675. * @return void
  5676. */
  5677. public function testSaveAssociatedEmptyData() {
  5678. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5679. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5680. $model = new Article();
  5681. $result = $model->saveAssociated(array(), array('validate' => true));
  5682. $this->assertFalse(empty($result));
  5683. $model = new ProductUpdateAll();
  5684. $result = $model->saveAssociated(array());
  5685. $this->assertFalse($result);
  5686. }
  5687. /**
  5688. * Test that saveAssociated will accept expression object values when saving.
  5689. *
  5690. * @return void
  5691. */
  5692. public function testSaveAssociatedExpressionObjects() {
  5693. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  5694. $TestModel = new Post();
  5695. $db = $TestModel->getDataSource();
  5696. $TestModel->saveAssociated(array(
  5697. 'Post' => array(
  5698. 'title' => $db->expression("(SELECT 'Post with Author')"),
  5699. 'body' => 'This post will be saved with an author'
  5700. ),
  5701. 'Author' => array(
  5702. 'user' => 'bob',
  5703. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  5704. )), array('atomic' => false));
  5705. $result = $TestModel->find('first', array(
  5706. 'order' => array('Post.id ' => 'DESC')
  5707. ));
  5708. $this->assertEquals('Post with Author', $result['Post']['title']);
  5709. }
  5710. /**
  5711. * testUpdateWithCalculation method
  5712. *
  5713. * @return void
  5714. */
  5715. public function testUpdateWithCalculation() {
  5716. $this->loadFixtures('DataTest');
  5717. $model = new DataTest();
  5718. $model->deleteAll(true);
  5719. $result = $model->saveMany(array(
  5720. array('count' => 5, 'float' => 1.1),
  5721. array('count' => 3, 'float' => 1.2),
  5722. array('count' => 4, 'float' => 1.3),
  5723. array('count' => 1, 'float' => 2.0),
  5724. ));
  5725. $this->assertFalse(empty($result));
  5726. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5727. $this->assertEquals(array(5, 3, 4, 1), $result);
  5728. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  5729. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5730. $this->assertEquals(array(7, 5, 6, 3), $result);
  5731. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  5732. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5733. $this->assertEquals(array(6, 4, 5, 2), $result);
  5734. }
  5735. public function testToggleBoolFields() {
  5736. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  5737. $Post = new CounterCachePost();
  5738. $Post->unbindModel(array('belongsTo' => array('User')), true);
  5739. $true = array('Post' => array('published' => true, 'id' => 2));
  5740. $false = array('Post' => array('published' => false, 'id' => 2));
  5741. $fields = array('Post.published', 'Post.id');
  5742. $updateConditions = array('Post.id' => 2);
  5743. // check its true
  5744. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5745. $this->assertEquals($true, $result);
  5746. // Testing without the alias
  5747. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5748. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5749. $this->assertEquals($false, $result);
  5750. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5751. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5752. $this->assertEquals($true, $result);
  5753. $db = ConnectionManager::getDataSource('test');
  5754. $alias = $db->name('Post.published');
  5755. // Testing with the alias
  5756. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5757. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5758. $this->assertEquals($false, $result);
  5759. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5760. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5761. $this->assertEquals($true, $result);
  5762. }
  5763. /**
  5764. * TestFindAllWithoutForeignKey
  5765. *
  5766. * @return void
  5767. */
  5768. public function testFindAllForeignKey() {
  5769. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5770. $ProductUpdateAll = new ProductUpdateAll();
  5771. $conditions = array('Group.name' => 'group one');
  5772. $ProductUpdateAll->bindModel(array(
  5773. 'belongsTo' => array(
  5774. 'Group' => array('className' => 'GroupUpdateAll')
  5775. )
  5776. ));
  5777. $ProductUpdateAll->belongsTo = array(
  5778. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  5779. );
  5780. $results = $ProductUpdateAll->find('all', compact('conditions'));
  5781. $this->assertTrue(!empty($results));
  5782. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  5783. $ProductUpdateAll->belongsTo = array(
  5784. 'Group' => array(
  5785. 'className' => 'GroupUpdateAll',
  5786. 'foreignKey' => false,
  5787. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5788. ));
  5789. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  5790. $this->assertTrue(!empty($resultsFkFalse));
  5791. $expected = array(
  5792. '0' => array(
  5793. 'ProductUpdateAll' => array(
  5794. 'id' => 1,
  5795. 'name' => 'product one',
  5796. 'groupcode' => 120,
  5797. 'group_id' => 1),
  5798. 'Group' => array(
  5799. 'id' => 1,
  5800. 'name' => 'group one',
  5801. 'code' => 120)
  5802. ),
  5803. '1' => array(
  5804. 'ProductUpdateAll' => array(
  5805. 'id' => 2,
  5806. 'name' => 'product two',
  5807. 'groupcode' => 120,
  5808. 'group_id' => 1),
  5809. 'Group' => array(
  5810. 'id' => 1,
  5811. 'name' => 'group one',
  5812. 'code' => 120)
  5813. )
  5814. );
  5815. $this->assertEquals($expected, $results);
  5816. $this->assertEquals($expected, $resultsFkFalse);
  5817. }
  5818. /**
  5819. * test updateAll with empty values.
  5820. *
  5821. * @return void
  5822. */
  5823. public function testUpdateAllEmptyValues() {
  5824. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  5825. $this->loadFixtures('Author', 'Post');
  5826. $model = new Author();
  5827. $result = $model->updateAll(array('user' => '""'));
  5828. $this->assertTrue($result);
  5829. }
  5830. /**
  5831. * testUpdateAllWithJoins
  5832. *
  5833. * @return void
  5834. */
  5835. public function testUpdateAllWithJoins() {
  5836. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  5837. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5838. $ProductUpdateAll = new ProductUpdateAll();
  5839. $conditions = array('Group.name' => 'group one');
  5840. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5841. 'Group' => array('className' => 'GroupUpdateAll')))
  5842. );
  5843. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5844. $results = $ProductUpdateAll->find('all', array(
  5845. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  5846. ));
  5847. $expected = array(
  5848. '0' => array(
  5849. 'ProductUpdateAll' => array(
  5850. 'id' => 1,
  5851. 'name' => 'new product',
  5852. 'groupcode' => 120,
  5853. 'group_id' => 1),
  5854. 'Group' => array(
  5855. 'id' => 1,
  5856. 'name' => 'group one',
  5857. 'code' => 120)
  5858. ),
  5859. '1' => array(
  5860. 'ProductUpdateAll' => array(
  5861. 'id' => 2,
  5862. 'name' => 'new product',
  5863. 'groupcode' => 120,
  5864. 'group_id' => 1),
  5865. 'Group' => array(
  5866. 'id' => 1,
  5867. 'name' => 'group one',
  5868. 'code' => 120)));
  5869. $this->assertEquals($expected, $results);
  5870. }
  5871. /**
  5872. * testUpdateAllWithoutForeignKey
  5873. *
  5874. * @return void
  5875. */
  5876. public function testUpdateAllWithoutForeignKey() {
  5877. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  5878. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5879. $ProductUpdateAll = new ProductUpdateAll();
  5880. $conditions = array('Group.name' => 'group one');
  5881. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5882. 'Group' => array('className' => 'GroupUpdateAll')
  5883. )));
  5884. $ProductUpdateAll->belongsTo = array(
  5885. 'Group' => array(
  5886. 'className' => 'GroupUpdateAll',
  5887. 'foreignKey' => false,
  5888. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5889. )
  5890. );
  5891. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5892. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  5893. $expected = array(
  5894. '0' => array(
  5895. 'ProductUpdateAll' => array(
  5896. 'id' => 1,
  5897. 'name' => 'new product',
  5898. 'groupcode' => 120,
  5899. 'group_id' => 1),
  5900. 'Group' => array(
  5901. 'id' => 1,
  5902. 'name' => 'group one',
  5903. 'code' => 120)
  5904. ),
  5905. '1' => array(
  5906. 'ProductUpdateAll' => array(
  5907. 'id' => 2,
  5908. 'name' => 'new product',
  5909. 'groupcode' => 120,
  5910. 'group_id' => 1),
  5911. 'Group' => array(
  5912. 'id' => 1,
  5913. 'name' => 'group one',
  5914. 'code' => 120)));
  5915. $this->assertEquals($expected, $resultsFkFalse);
  5916. }
  5917. /**
  5918. * test writing floats in german locale.
  5919. *
  5920. * @return void
  5921. */
  5922. public function testWriteFloatAsGerman() {
  5923. $restore = setlocale(LC_NUMERIC, 0);
  5924. $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
  5925. $model = new DataTest();
  5926. $result = $model->save(array(
  5927. 'count' => 1,
  5928. 'float' => 3.14593
  5929. ));
  5930. $this->assertTrue((bool)$result);
  5931. setlocale(LC_NUMERIC, $restore);
  5932. }
  5933. /**
  5934. * Test returned array contains primary key when save creates a new record
  5935. *
  5936. * @return void
  5937. */
  5938. public function testPkInReturnArrayForCreate() {
  5939. $this->loadFixtures('Article');
  5940. $TestModel = new Article();
  5941. $data = array('Article' => array(
  5942. 'user_id' => '1',
  5943. 'title' => 'Fourth Article',
  5944. 'body' => 'Fourth Article Body',
  5945. 'published' => 'Y'
  5946. ));
  5947. $result = $TestModel->save($data);
  5948. $this->assertSame($result['Article']['id'], $TestModel->id);
  5949. }
  5950. /**
  5951. * testSaveAllFieldListValidateBelongsTo
  5952. *
  5953. * @return void
  5954. */
  5955. public function testSaveAllFieldListValidateBelongsTo() {
  5956. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5957. $TestModel = new Post();
  5958. $result = $TestModel->find('all');
  5959. $this->assertEquals(3, count($result));
  5960. $this->assertFalse(isset($result[3]));
  5961. // test belongsTo
  5962. $fieldList = array(
  5963. 'Post' => array('title'),
  5964. 'Author' => array('user')
  5965. );
  5966. $data = array(
  5967. 'Post' => array(
  5968. 'title' => 'Post without body',
  5969. 'body' => 'This will not be saved',
  5970. ),
  5971. 'Author' => array(
  5972. 'user' => 'bob',
  5973. 'test' => 'This will not be saved',
  5974. ));
  5975. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  5976. $result = $TestModel->find('all', array(
  5977. 'order' => 'Post.id ASC',
  5978. ));
  5979. $expected = array(
  5980. 'Post' => array(
  5981. 'id' => '4',
  5982. 'author_id' => '5',
  5983. 'title' => 'Post without body',
  5984. 'body' => null,
  5985. 'published' => 'N',
  5986. 'created' => self::date(),
  5987. 'updated' => self::date(),
  5988. ),
  5989. 'Author' => array(
  5990. 'id' => '5',
  5991. 'user' => 'bob',
  5992. 'password' => null,
  5993. 'created' => self::date(),
  5994. 'updated' => self::date(),
  5995. 'test' => 'working',
  5996. ),
  5997. );
  5998. $this->assertEquals($expected, $result[3]);
  5999. $this->assertEquals(4, count($result));
  6000. $this->assertEquals('', $result[3]['Post']['body']);
  6001. $this->assertEquals('working', $result[3]['Author']['test']);
  6002. $fieldList = array(
  6003. 'Post' => array('title')
  6004. );
  6005. $data = array(
  6006. 'Post' => array(
  6007. 'title' => 'Post without body 2',
  6008. 'body' => 'This will not be saved'
  6009. ),
  6010. 'Author' => array(
  6011. 'user' => 'jack'
  6012. )
  6013. );
  6014. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6015. $result = $TestModel->find('all', array(
  6016. 'order' => 'Post.id ASC',
  6017. ));
  6018. $this->assertNull($result[4]['Post']['body']);
  6019. $fieldList = array(
  6020. 'Author' => array('password')
  6021. );
  6022. $data = array(
  6023. 'Post' => array(
  6024. 'id' => '5',
  6025. 'title' => 'Post title',
  6026. 'body' => 'Post body'
  6027. ),
  6028. 'Author' => array(
  6029. 'id' => '6',
  6030. 'user' => 'will not change',
  6031. 'password' => 'foobar'
  6032. )
  6033. );
  6034. $result = $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6035. $this->assertTrue($result);
  6036. $result = $TestModel->find('all', array(
  6037. 'order' => 'Post.id ASC',
  6038. ));
  6039. $expected = array(
  6040. 'Post' => array(
  6041. 'id' => '5',
  6042. 'author_id' => '6',
  6043. 'title' => 'Post title',
  6044. 'body' => 'Post body',
  6045. 'published' => 'N',
  6046. 'created' => self::date(),
  6047. 'updated' => self::date()
  6048. ),
  6049. 'Author' => array(
  6050. 'id' => '6',
  6051. 'user' => 'jack',
  6052. 'password' => 'foobar',
  6053. 'created' => self::date(),
  6054. 'updated' => self::date(),
  6055. 'test' => 'working'
  6056. ),
  6057. );
  6058. $this->assertEquals($expected, $result[4]);
  6059. // test multirecord
  6060. $this->db->truncate($TestModel);
  6061. $fieldList = array('title', 'author_id');
  6062. $TestModel->saveAll(array(
  6063. array(
  6064. 'title' => 'Multi-record post 1',
  6065. 'body' => 'First multi-record post',
  6066. 'author_id' => 2
  6067. ),
  6068. array(
  6069. 'title' => 'Multi-record post 2',
  6070. 'body' => 'Second multi-record post',
  6071. 'author_id' => 2
  6072. )), array('fieldList' => $fieldList));
  6073. $result = $TestModel->find('all', array(
  6074. 'recursive' => -1,
  6075. 'order' => 'Post.id ASC'
  6076. ));
  6077. $expected = array(
  6078. array(
  6079. 'Post' => array(
  6080. 'id' => '1',
  6081. 'author_id' => '2',
  6082. 'title' => 'Multi-record post 1',
  6083. 'body' => '',
  6084. 'published' => 'N',
  6085. 'created' => self::date(),
  6086. 'updated' => self::date()
  6087. )
  6088. ),
  6089. array(
  6090. 'Post' => array(
  6091. 'id' => '2',
  6092. 'author_id' => '2',
  6093. 'title' => 'Multi-record post 2',
  6094. 'body' => '',
  6095. 'published' => 'N',
  6096. 'created' => self::date(),
  6097. 'updated' => self::date()
  6098. )
  6099. )
  6100. );
  6101. $this->assertEquals($expected, $result);
  6102. }
  6103. /**
  6104. * testSaveAllFieldListHasMany method
  6105. *
  6106. * return @void
  6107. */
  6108. public function testSaveAllFieldListHasMany() {
  6109. $this->loadFixtures('Article', 'Comment');
  6110. $TestModel = new Article();
  6111. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6112. $this->db->truncate($TestModel);
  6113. $this->db->truncate(new Comment());
  6114. $data = array(
  6115. 'Article' => array('title' => 'I will not save'),
  6116. 'Comment' => array(
  6117. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6118. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  6119. )
  6120. );
  6121. $fieldList = array(
  6122. 'Article' => array('id'),
  6123. 'Comment' => array('article_id', 'user_id')
  6124. );
  6125. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6126. $result = $TestModel->find('all');
  6127. $this->assertEquals('', $result[0]['Article']['title']);
  6128. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  6129. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  6130. $fieldList = array(
  6131. 'Article' => array('id'),
  6132. 'Comment' => array('user_id')
  6133. );
  6134. $TestModel->saveAll($data, array('fieldList' => $fieldList));
  6135. $result = $TestModel->find('all');
  6136. $this->assertEquals('', $result[1]['Article']['title']);
  6137. $this->assertEquals(2, count($result[1]['Comment']));
  6138. $TestModel->whitelist = array('id');
  6139. $TestModel->Comment->whitelist = array('user_id');
  6140. $TestModel->saveAll($data);
  6141. $result = $TestModel->find('all');
  6142. $this->assertEquals('', $result[2]['Article']['title']);
  6143. $this->assertEquals(2, count($result[2]['Comment']));
  6144. }
  6145. /**
  6146. * testSaveAllFieldListHasOne method
  6147. *
  6148. * @return void
  6149. */
  6150. public function testSaveAllFieldListHasOne() {
  6151. $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
  6152. $TestModel = new Comment();
  6153. $TestModel->validate = array('comment' => 'notEmpty');
  6154. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  6155. $record = array(
  6156. 'Comment' => array(
  6157. 'user_id' => 1,
  6158. 'article_id' => 1,
  6159. 'comment' => '',
  6160. ),
  6161. 'Attachment' => array(
  6162. 'attachment' => ''
  6163. )
  6164. );
  6165. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  6166. $this->assertFalse($result);
  6167. $fieldList = array(
  6168. 'Comment' => array('id', 'article_id', 'user_id'),
  6169. 'Attachment' => array('comment_id')
  6170. );
  6171. $result = $TestModel->saveAll($record, array(
  6172. 'fieldList' => $fieldList, 'validate' => 'only'
  6173. ));
  6174. $this->assertTrue($result);
  6175. $this->assertEmpty($TestModel->validationErrors);
  6176. }
  6177. /**
  6178. * testSaveAllFieldListHasOneAddFkToWhitelist method
  6179. *
  6180. * @return void
  6181. */
  6182. public function testSaveAllFieldListHasOneAddFkToWhitelist() {
  6183. $this->loadFixtures('ArticleFeatured', 'Featured');
  6184. $Article = new ArticleFeatured();
  6185. $Article->belongsTo = $Article->hasMany = array();
  6186. $Article->Featured->validate = array('end_date' => 'notEmpty');
  6187. $record = array(
  6188. 'ArticleFeatured' => array(
  6189. 'user_id' => 1,
  6190. 'title' => 'First Article',
  6191. 'body' => '',
  6192. 'published' => 'Y'
  6193. ),
  6194. 'Featured' => array(
  6195. 'category_id' => 1,
  6196. 'end_date' => ''
  6197. )
  6198. );
  6199. $result = $Article->saveAll($record, array('validate' => 'only'));
  6200. $this->assertFalse($result);
  6201. $expected = array(
  6202. 'body' => array(
  6203. 'This field cannot be left blank'
  6204. ),
  6205. 'Featured' => array(
  6206. 'end_date' => array(
  6207. 'This field cannot be left blank'
  6208. )
  6209. )
  6210. );
  6211. $this->assertEquals($expected, $Article->validationErrors);
  6212. $fieldList = array(
  6213. 'ArticleFeatured' => array('user_id', 'title'),
  6214. 'Featured' => array('category_id')
  6215. );
  6216. $result = $Article->saveAll($record, array(
  6217. 'fieldList' => $fieldList, 'validate' => 'first'
  6218. ));
  6219. $this->assertTrue($result);
  6220. $this->assertEmpty($Article->validationErrors);
  6221. $Article->recursive = 0;
  6222. $result = $Article->find('first', array('order' => array('ArticleFeatured.created' => 'DESC')));
  6223. $this->assertSame($result['ArticleFeatured']['id'], $result['Featured']['article_featured_id']);
  6224. }
  6225. /**
  6226. * testSaveAllDeepFieldListValidateBelongsTo
  6227. *
  6228. * @return void
  6229. */
  6230. public function testSaveAllDeepFieldListValidateBelongsTo() {
  6231. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  6232. $TestModel = new Post();
  6233. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  6234. $TestModel->recursive = 2;
  6235. $result = $TestModel->find('all');
  6236. $this->assertEquals(3, count($result));
  6237. $this->assertFalse(isset($result[3]));
  6238. // test belongsTo
  6239. $fieldList = array(
  6240. 'Post' => array('title', 'author_id'),
  6241. 'Author' => array('user'),
  6242. 'Comment' => array('comment')
  6243. );
  6244. $TestModel->saveAll(array(
  6245. 'Post' => array(
  6246. 'title' => 'Post without body',
  6247. 'body' => 'This will not be saved',
  6248. ),
  6249. 'Author' => array(
  6250. 'user' => 'bob',
  6251. 'test' => 'This will not be saved',
  6252. 'Comment' => array(
  6253. array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
  6254. )), array('fieldList' => $fieldList, 'deep' => true));
  6255. $result = $TestModel->Author->Comment->find('first', array(
  6256. 'conditions' => array('Comment.id' => 5),
  6257. 'fields' => array('comment', 'published')
  6258. ));
  6259. $expected = array(
  6260. 'Comment' => array(
  6261. 'comment' => 'I am still published',
  6262. 'published' => 'Y'
  6263. )
  6264. );
  6265. $this->assertEquals($expected, $result);
  6266. }
  6267. /**
  6268. * testSaveAllDeepFieldListHasMany method
  6269. *
  6270. * return @void
  6271. */
  6272. public function testSaveAllDeepFieldListHasMany() {
  6273. $this->loadFixtures('Article', 'Comment', 'User');
  6274. $TestModel = new Article();
  6275. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6276. $this->db->truncate($TestModel);
  6277. $this->db->truncate(new Comment());
  6278. $fieldList = array(
  6279. 'Article' => array('id'),
  6280. 'Comment' => array('article_id', 'user_id'),
  6281. 'User' => array('user')
  6282. );
  6283. $result = $TestModel->saveAll(array(
  6284. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  6285. 'Comment' => array(
  6286. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6287. array(
  6288. 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
  6289. 'User' => array('user' => 'nopassword', 'password' => 'not saved')
  6290. )
  6291. )
  6292. ), array('fieldList' => $fieldList, 'deep' => true));
  6293. $result = $TestModel->Comment->User->find('first', array(
  6294. 'conditions' => array('User.user' => 'nopassword'),
  6295. 'fields' => array('user', 'password')
  6296. ));
  6297. $expected = array(
  6298. 'User' => array(
  6299. 'user' => 'nopassword',
  6300. 'password' => ''
  6301. )
  6302. );
  6303. $this->assertEquals($expected, $result);
  6304. }
  6305. /**
  6306. * testSaveAllDeepHasManyBelongsTo method
  6307. *
  6308. * return @void
  6309. */
  6310. public function testSaveAllDeepHasManyBelongsTo() {
  6311. $this->loadFixtures('Article', 'Comment', 'User');
  6312. $TestModel = new Article();
  6313. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6314. $this->db->truncate($TestModel);
  6315. $this->db->truncate(new Comment());
  6316. $result = $TestModel->saveAll(array(
  6317. 'Article' => array('id' => 2, 'title' => 'The title'),
  6318. 'Comment' => array(
  6319. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6320. array(
  6321. 'comment' => 'belongsto', 'published' => 'Y',
  6322. 'User' => array('user' => 'findme', 'password' => 'somepass')
  6323. )
  6324. )
  6325. ), array('deep' => true));
  6326. $result = $TestModel->Comment->User->find('first', array(
  6327. 'conditions' => array('User.user' => 'findme'),
  6328. 'fields' => array('id', 'user', 'password')
  6329. ));
  6330. $expected = array(
  6331. 'User' => array(
  6332. 'id' => 5,
  6333. 'user' => 'findme',
  6334. 'password' => 'somepass',
  6335. )
  6336. );
  6337. $this->assertEquals($expected, $result);
  6338. $result = $TestModel->Comment->find('first', array(
  6339. 'conditions' => array('Comment.user_id' => 5),
  6340. 'fields' => array('id', 'comment', 'published', 'user_id')
  6341. ));
  6342. $expected = array(
  6343. 'Comment' => array(
  6344. 'id' => 2,
  6345. 'comment' => 'belongsto',
  6346. 'published' => 'Y',
  6347. 'user_id' => 5
  6348. )
  6349. );
  6350. $this->assertEquals($expected, $result);
  6351. }
  6352. /**
  6353. * testSaveAllDeepHasManyhasMany method
  6354. *
  6355. * return @void
  6356. */
  6357. public function testSaveAllDeepHasManyHasMany() {
  6358. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6359. $TestModel = new Article();
  6360. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6361. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6362. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6363. $this->db->truncate($TestModel);
  6364. $this->db->truncate(new Comment());
  6365. $this->db->truncate(new Attachment());
  6366. $result = $TestModel->saveAll(array(
  6367. 'Article' => array('id' => 2, 'title' => 'The title'),
  6368. 'Comment' => array(
  6369. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6370. array(
  6371. 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
  6372. 'Attachment' => array(
  6373. array('attachment' => 'first deep attachment'),
  6374. array('attachment' => 'second deep attachment'),
  6375. )
  6376. )
  6377. )
  6378. ), array('deep' => true));
  6379. $result = $TestModel->Comment->find('first', array(
  6380. 'conditions' => array('Comment.comment' => 'hasmany'),
  6381. 'fields' => array('id', 'comment', 'published', 'user_id'),
  6382. 'recursive' => -1
  6383. ));
  6384. $expected = array(
  6385. 'Comment' => array(
  6386. 'id' => 2,
  6387. 'comment' => 'hasmany',
  6388. 'published' => 'Y',
  6389. 'user_id' => 5
  6390. )
  6391. );
  6392. $this->assertEquals($expected, $result);
  6393. $result = $TestModel->Comment->Attachment->find('all', array(
  6394. 'fields' => array('attachment', 'comment_id'),
  6395. 'order' => array('Attachment.id' => 'ASC')
  6396. ));
  6397. $expected = array(
  6398. array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
  6399. array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
  6400. );
  6401. $this->assertEquals($expected, $result);
  6402. }
  6403. /**
  6404. * testSaveAllDeepOrderHasManyHasMany method
  6405. *
  6406. * return @void
  6407. */
  6408. public function testSaveAllDeepOrderHasManyHasMany() {
  6409. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6410. $TestModel = new Article();
  6411. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6412. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6413. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6414. $this->db->truncate($TestModel);
  6415. $this->db->truncate(new Comment());
  6416. $this->db->truncate(new Attachment());
  6417. $result = $TestModel->saveAll(array(
  6418. 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
  6419. 'Comment' => array(
  6420. array(
  6421. 'Attachment' => array(
  6422. array('attachment' => 'attachment should be created with comment_id'),
  6423. array('attachment' => 'comment should be created with article_id'),
  6424. ),
  6425. 'comment' => 'after associated data',
  6426. 'user_id' => 1
  6427. )
  6428. )
  6429. ), array('deep' => true));
  6430. $result = $TestModel->Comment->find('first', array(
  6431. 'conditions' => array('Comment.article_id' => 2),
  6432. ));
  6433. $this->assertEquals(2, $result['Comment']['article_id']);
  6434. $this->assertEquals(2, count($result['Attachment']));
  6435. }
  6436. /**
  6437. * testSaveAllDeepEmptyHasManyHasMany method
  6438. *
  6439. * return @void
  6440. */
  6441. public function testSaveAllDeepEmptyHasManyHasMany() {
  6442. $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
  6443. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6444. $TestModel = new Article();
  6445. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6446. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6447. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6448. $this->db->truncate($TestModel);
  6449. $this->db->truncate(new Comment());
  6450. $this->db->truncate(new Attachment());
  6451. $result = $TestModel->saveAll(array(
  6452. 'Article' => array('id' => 3, 'user_id' => 1, 'title' => 'Comment has no data'),
  6453. 'Comment' => array(
  6454. array(
  6455. 'user_id' => 1,
  6456. 'Attachment' => array(
  6457. array('attachment' => 'attachment should be created with comment_id'),
  6458. array('attachment' => 'comment should be created with article_id'),
  6459. ),
  6460. )
  6461. )
  6462. ), array('deep' => true));
  6463. $result = $TestModel->Comment->find('first', array(
  6464. 'conditions' => array('Comment.article_id' => 3),
  6465. ));
  6466. $this->assertEquals(3, $result['Comment']['article_id']);
  6467. $this->assertEquals(2, count($result['Attachment']));
  6468. }
  6469. /**
  6470. * testUpdateAllBoolean
  6471. *
  6472. * return @void
  6473. */
  6474. public function testUpdateAllBoolean() {
  6475. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6476. $TestModel = new Item();
  6477. $result = $TestModel->updateAll(array('published' => true));
  6478. $this->assertTrue($result);
  6479. $result = $TestModel->find('first', array('fields' => array('id', 'published')));
  6480. $this->assertEquals(true, $result['Item']['published']);
  6481. }
  6482. /**
  6483. * testUpdateAllBooleanConditions
  6484. *
  6485. * return @void
  6486. */
  6487. public function testUpdateAllBooleanConditions() {
  6488. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6489. $TestModel = new Item();
  6490. $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
  6491. $this->assertTrue($result);
  6492. $result = $TestModel->find('first', array(
  6493. 'fields' => array('id', 'published'),
  6494. 'conditions' => array('Item.id' => 1)));
  6495. $this->assertEquals(true, $result['Item']['published']);
  6496. }
  6497. /**
  6498. * testUpdateBoolean
  6499. *
  6500. * return @void
  6501. */
  6502. public function testUpdateBoolean() {
  6503. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6504. $TestModel = new Item();
  6505. $result = $TestModel->save(array('published' => true, 'id' => 1));
  6506. $this->assertTrue((bool)$result);
  6507. $result = $TestModel->find('first', array(
  6508. 'fields' => array('id', 'published'),
  6509. 'conditions' => array('Item.id' => 1)));
  6510. $this->assertEquals(true, $result['Item']['published']);
  6511. }
  6512. /**
  6513. * Test the clear() method.
  6514. *
  6515. * @return void
  6516. */
  6517. public function testClear() {
  6518. $this->loadFixtures('Bid');
  6519. $model = ClassRegistry::init('Bid');
  6520. $model->set(array('name' => 'Testing', 'message_id' => 3));
  6521. $this->assertTrue(isset($model->data['Bid']['name']));
  6522. $this->assertTrue($model->clear());
  6523. $this->assertFalse(isset($model->data['Bid']['name']));
  6524. $this->assertFalse(isset($model->data['Bid']['message_id']));
  6525. }
  6526. }