PageRenderTime 63ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 1ms

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

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