PageRenderTime 81ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/pyroka/hms
PHP | 6877 lines | 5613 code | 645 blank | 619 comment | 27 complexity | 948f7410bcded90ec2e386d9a4631eae MD5 | raw file
Possible License(s): LGPL-2.1
  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($data['Article']['title'], $testResult['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('id', 'title', 'count', 'created', 'updated'),
  138. array_keys($result['Uuid'])
  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('id', 'title', 'count', 'created', 'updated'),
  157. array_keys($result['Uuid'])
  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 = Hash::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'), Hash::extract($result, 'ParentNode.{n}.name'));
  554. $Node->saveField('state', 10);
  555. $result = $Node->read();
  556. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  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, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
  1414. $expected = array('new record', 'new record');
  1415. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
  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'] = Hash::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($id, $TestModel->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 = Hash::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 = Hash::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 = Hash::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. $result = $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. );
  2677. $this->assertEquals(false, $result);
  2678. $expected = array(
  2679. 'comment' => array('This field cannot be left blank'),
  2680. 'Attachment' => array(
  2681. 'attachment' => array('This field cannot be left blank')
  2682. )
  2683. );
  2684. $this->assertEquals($expected, $model->validationErrors);
  2685. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2686. }
  2687. /**
  2688. * testSaveAllAtomic method
  2689. *
  2690. * @return void
  2691. */
  2692. public function testSaveAllAtomic() {
  2693. $this->loadFixtures('Article', 'User', 'Comment');
  2694. $TestModel = new Article();
  2695. $result = $TestModel->saveAll(array(
  2696. 'Article' => array(
  2697. 'title' => 'Post with Author',
  2698. 'body' => 'This post will be saved with an author',
  2699. 'user_id' => 2
  2700. ),
  2701. 'Comment' => array(
  2702. array('comment' => 'First new comment', 'user_id' => 2))
  2703. ), array('atomic' => false));
  2704. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2705. $result = $TestModel->saveAll(array(
  2706. array(
  2707. 'id' => '1',
  2708. 'title' => 'Baleeted First Post',
  2709. 'body' => 'Baleeted!',
  2710. 'published' => 'N'
  2711. ),
  2712. array(
  2713. 'id' => '2',
  2714. 'title' => 'Just update the title'
  2715. ),
  2716. array(
  2717. 'title' => 'Creating a fourth post',
  2718. 'body' => 'Fourth post body',
  2719. 'user_id' => 2
  2720. )
  2721. ), array('atomic' => false));
  2722. $this->assertSame($result, array(true, true, true));
  2723. $result = $TestModel->saveAll(array(
  2724. 'Article' => array('id' => 2),
  2725. 'Comment' => array(
  2726. array(
  2727. 'comment' => 'First new comment',
  2728. 'published' => 'Y',
  2729. 'user_id' => 1
  2730. ),
  2731. array(
  2732. 'comment' => 'Second new comment',
  2733. 'published' => 'Y',
  2734. 'user_id' => 2
  2735. ))
  2736. ), array('validate' => true, 'atomic' => false));
  2737. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2738. $TestModel->validate = array(
  2739. 'title' => 'notEmpty',
  2740. 'author_id' => 'numeric'
  2741. );
  2742. $result = $TestModel->saveAll(array(
  2743. array(
  2744. 'id' => '1',
  2745. 'title' => 'Un-Baleeted First Post',
  2746. 'body' => 'Not Baleeted!',
  2747. 'published' => 'Y'
  2748. ),
  2749. array(
  2750. 'id' => '2',
  2751. 'title' => '',
  2752. 'body' => 'Trying to get away with an empty title'
  2753. )
  2754. ), array('validate' => true, 'atomic' => false));
  2755. $this->assertSame(array(true, false), $result);
  2756. }
  2757. /**
  2758. * testSaveAllDeepAssociated method
  2759. *
  2760. * @return void
  2761. */
  2762. public function testSaveAllDeepAssociated() {
  2763. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2764. $TestModel = new Article();
  2765. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2766. $TestModel->hasAndBelongsToMany = array();
  2767. $result = $TestModel->saveAll(array(
  2768. 'Article' => array('id' => 2),
  2769. 'Comment' => array(
  2770. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2771. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2772. )
  2773. ), array('deep' => true));
  2774. $this->assertTrue($result);
  2775. $result = $TestModel->findById(2);
  2776. $expected = array(
  2777. 'First Comment for Second Article',
  2778. 'Second Comment for Second Article',
  2779. 'First new comment',
  2780. 'Second new comment'
  2781. );
  2782. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2783. $this->assertEquals($expected, $result);
  2784. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2785. $this->assertEquals(5, $result);
  2786. $result = $TestModel->saveAll(array(
  2787. 'Article' => array('id' => 2),
  2788. 'Comment' => array(
  2789. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  2790. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2791. )
  2792. ), array('deep' => true));
  2793. $this->assertTrue($result);
  2794. $result = $TestModel->findById(2);
  2795. $expected = array(
  2796. 'First Comment for Second Article',
  2797. 'Second Comment for Second Article',
  2798. 'First new comment',
  2799. 'Second new comment',
  2800. 'Third new comment',
  2801. 'Fourth new comment'
  2802. );
  2803. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2804. $this->assertEquals($expected, $result);
  2805. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2806. $this->assertEquals(2, $result);
  2807. $data = array(
  2808. 'Attachment' => array(
  2809. 'attachment' => 'deepsave insert',
  2810. ),
  2811. 'Comment' => array(
  2812. 'comment' => 'First comment deepsave insert',
  2813. 'published' => 'Y',
  2814. 'user_id' => 5,
  2815. 'Article' => array(
  2816. 'title' => 'First Article deepsave insert',
  2817. 'body' => 'First Article Body deepsave insert',
  2818. 'User' => array(
  2819. 'user' => '',
  2820. 'password' => 'magic'
  2821. ),
  2822. ),
  2823. )
  2824. );
  2825. $TestModel->Comment->Attachment->create();
  2826. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2827. $this->assertFalse($result);
  2828. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  2829. $this->assertEquals($expected, $TestModel->validationErrors);
  2830. $data['Comment']['Article']['User']['user'] = 'deepsave';
  2831. $TestModel->Comment->Attachment->create();
  2832. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2833. $this->assertTrue($result);
  2834. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  2835. $expected = array(
  2836. 'Attachment' => array(
  2837. 'id' => '3',
  2838. 'comment_id' => '11',
  2839. 'attachment' => 'deepsave insert',
  2840. ),
  2841. 'Comment' => array(
  2842. 'id' => '11',
  2843. 'article_id' => '4',
  2844. 'user_id' => '5',
  2845. 'comment' => 'First comment deepsave insert',
  2846. 'published' => 'Y',
  2847. )
  2848. );
  2849. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  2850. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  2851. unset($result['Comment']['created'], $result['Comment']['updated']);
  2852. $this->assertEquals($expected['Comment'], $result['Comment']);
  2853. $result = $TestModel->findById($result['Comment']['article_id']);
  2854. $expected = array(
  2855. 'Article' => array(
  2856. 'id' => '4',
  2857. 'user_id' => '6',
  2858. 'title' => 'First Article deepsave insert',
  2859. 'body' => 'First Article Body deepsave insert',
  2860. 'published' => 'N',
  2861. ),
  2862. 'User' => array(
  2863. 'id' => '6',
  2864. 'user' => 'deepsave',
  2865. 'password' => 'magic',
  2866. ),
  2867. 'Comment' => array(
  2868. array(
  2869. 'id' => '11',
  2870. 'article_id' => '4',
  2871. 'user_id' => '5',
  2872. 'comment' => 'First comment deepsave insert',
  2873. 'published' => 'Y',
  2874. )
  2875. )
  2876. );
  2877. unset(
  2878. $result['Article']['created'], $result['Article']['updated'],
  2879. $result['User']['created'], $result['User']['updated'],
  2880. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  2881. );
  2882. $this->assertEquals($expected, $result);
  2883. }
  2884. /**
  2885. * testSaveAllDeepMany
  2886. * tests the validate methods with deeper recursive data
  2887. *
  2888. * @return void
  2889. */
  2890. public function testSaveAllDeepMany() {
  2891. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2892. $TestModel = new Article();
  2893. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2894. $TestModel->hasAndBelongsToMany = array();
  2895. $data = array(
  2896. array(
  2897. 'Article' => array('id' => 1),
  2898. 'Comment' => array(
  2899. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  2900. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2901. )
  2902. ),
  2903. array(
  2904. 'Article' => array('id' => 2),
  2905. 'Comment' => array(
  2906. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  2907. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  2908. )
  2909. )
  2910. );
  2911. $result = $TestModel->saveAll($data, array('deep' => true));
  2912. $this->assertTrue($result);
  2913. $data = array(
  2914. array(
  2915. 'id' => 1, 'body' => '',
  2916. 'Comment' => array(
  2917. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  2918. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2919. )
  2920. ),
  2921. array(
  2922. 'Article' => array('id' => 2),
  2923. 'Comment' => array(
  2924. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  2925. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  2926. )
  2927. )
  2928. );
  2929. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2930. $result = $TestModel->saveAll($data, array('deep' => true));
  2931. $this->assertFalse($result);
  2932. $expected = array(
  2933. 0 => array(
  2934. 'body' => array('This field cannot be left blank'),
  2935. 'Comment' => array(
  2936. 0 => array(
  2937. 'comment' => array('This field cannot be left blank'),
  2938. 'User' => array(
  2939. 'user' => array('This field cannot be left blank')
  2940. )
  2941. )
  2942. )
  2943. ),
  2944. 1 => array(
  2945. 'body' => array('This field cannot be left blank'),
  2946. 'Comment' => array(
  2947. 0 => array(
  2948. 'User' => array(
  2949. 'password' => array('This field cannot be left blank')
  2950. )
  2951. ),
  2952. 1 => array(
  2953. 'comment' => array('This field cannot be left blank')
  2954. )
  2955. )
  2956. )
  2957. );
  2958. $result = $TestModel->validationErrors;
  2959. $this->assertSame($expected, $result);
  2960. }
  2961. /**
  2962. * testSaveAllDeepValidateOnly
  2963. * tests the validate methods with deeper recursive data
  2964. *
  2965. * @return void
  2966. */
  2967. public function testSaveAllDeepValidateOnly() {
  2968. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2969. $TestModel = new Article();
  2970. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2971. $TestModel->hasAndBelongsToMany = array();
  2972. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  2973. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2974. $result = $TestModel->saveAll(
  2975. array(
  2976. 'Article' => array('id' => 2),
  2977. 'Comment' => array(
  2978. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2979. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2980. )
  2981. ),
  2982. array('validate' => 'only', 'deep' => true)
  2983. );
  2984. $this->assertTrue($result);
  2985. $result = $TestModel->saveAll(
  2986. array(
  2987. 'Article' => array('id' => 2),
  2988. 'Comment' => array(
  2989. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  2990. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2991. )
  2992. ),
  2993. array('validate' => 'only', 'deep' => true)
  2994. );
  2995. $this->assertFalse($result);
  2996. $result = $TestModel->saveAll(
  2997. array(
  2998. 'Article' => array('id' => 2),
  2999. 'Comment' => array(
  3000. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  3001. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3002. )
  3003. ),
  3004. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3005. );
  3006. $expected = array(
  3007. 'Article' => true,
  3008. 'Comment' => array(
  3009. true,
  3010. true
  3011. )
  3012. );
  3013. $this->assertSame($expected, $result);
  3014. $result = $TestModel->saveAll(
  3015. array(
  3016. 'Article' => array('id' => 2),
  3017. 'Comment' => array(
  3018. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3019. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3020. )
  3021. ),
  3022. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3023. );
  3024. $expected = array(
  3025. 'Article' => true,
  3026. 'Comment' => array(
  3027. false,
  3028. true
  3029. )
  3030. );
  3031. $this->assertSame($expected, $result);
  3032. $result = $TestModel->saveAll(array(
  3033. 'Article' => array('id' => 2),
  3034. 'Comment' => array(
  3035. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3036. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3037. )
  3038. ),
  3039. array('validate' => 'only', 'deep' => true)
  3040. );
  3041. $this->assertTrue($result);
  3042. $result = $TestModel->saveAll(array(
  3043. 'Article' => array('id' => 2),
  3044. 'Comment' => array(
  3045. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3046. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3047. )
  3048. ),
  3049. array('validate' => 'only', 'deep' => true)
  3050. );
  3051. $this->assertFalse($result);
  3052. $result = $TestModel->saveAll(array(
  3053. 'Article' => array('id' => 2),
  3054. 'Comment' => array(
  3055. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3056. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
  3057. )
  3058. ),
  3059. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3060. );
  3061. $expected = array(
  3062. 'Article' => true,
  3063. 'Comment' => array(
  3064. true,
  3065. true
  3066. )
  3067. );
  3068. $this->assertSame($expected, $result);
  3069. $result = $TestModel->saveAll(array(
  3070. 'Article' => array('id' => 2),
  3071. 'Comment' => array(
  3072. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3073. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3074. )
  3075. ),
  3076. array('validate' => 'only', 'atomic' => false, 'deep' => true)
  3077. );
  3078. $expected = array(
  3079. 'Article' => true,
  3080. 'Comment' => array(
  3081. true,
  3082. false
  3083. )
  3084. );
  3085. $this->assertSame($expected, $result);
  3086. $expected = array(
  3087. 'Comment' => array(
  3088. 1 => array(
  3089. 'Attachment' => array(
  3090. 'attachment' => array('This field cannot be left blank')
  3091. )
  3092. )
  3093. )
  3094. );
  3095. $result = $TestModel->validationErrors;
  3096. $this->assertSame($expected, $result);
  3097. $data = array(
  3098. 'Attachment' => array(
  3099. 'attachment' => 'deepsave insert',
  3100. ),
  3101. 'Comment' => array(
  3102. 'comment' => 'First comment deepsave insert',
  3103. 'published' => 'Y',
  3104. 'user_id' => 5,
  3105. 'Article' => array(
  3106. 'title' => 'First Article deepsave insert',
  3107. 'body' => 'First Article Body deepsave insert',
  3108. 'User' => array(
  3109. 'user' => 'deepsave',
  3110. 'password' => 'magic'
  3111. ),
  3112. ),
  3113. )
  3114. );
  3115. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3116. $this->assertTrue($result);
  3117. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3118. $expected = array(
  3119. 'Attachment' => true,
  3120. 'Comment' => true
  3121. );
  3122. $this->assertSame($expected, $result);
  3123. $data = array(
  3124. 'Attachment' => array(
  3125. 'attachment' => 'deepsave insert',
  3126. ),
  3127. 'Comment' => array(
  3128. 'comment' => 'First comment deepsave insert',
  3129. 'published' => 'Y',
  3130. 'user_id' => 5,
  3131. 'Article' => array(
  3132. 'title' => 'First Article deepsave insert',
  3133. 'body' => 'First Article Body deepsave insert',
  3134. 'User' => array(
  3135. 'user' => '',
  3136. 'password' => 'magic'
  3137. ),
  3138. ),
  3139. )
  3140. );
  3141. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3142. $this->assertFalse($result);
  3143. $result = $TestModel->Comment->Attachment->validationErrors;
  3144. $expected = array(
  3145. 'Comment' => array(
  3146. 'Article' => array(
  3147. 'User' => array(
  3148. 'user' => array('This field cannot be left blank')
  3149. )
  3150. )
  3151. )
  3152. );
  3153. $this->assertSame($expected, $result);
  3154. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3155. $expected = array(
  3156. 'Attachment' => true,
  3157. 'Comment' => false
  3158. );
  3159. $this->assertEquals($expected, $result);
  3160. $data['Comment']['Article']['body'] = '';
  3161. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3162. $this->assertFalse($result);
  3163. $result = $TestModel->Comment->Attachment->validationErrors;
  3164. $expected = array(
  3165. 'Comment' => array(
  3166. 'Article' => array(
  3167. 'body' => array('This field cannot be left blank'),
  3168. 'User' => array(
  3169. 'user' => array('This field cannot be left blank')
  3170. )
  3171. )
  3172. )
  3173. );
  3174. $this->assertSame($expected, $result);
  3175. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3176. $expected = array(
  3177. 'Attachment' => true,
  3178. 'Comment' => false
  3179. );
  3180. $this->assertEquals($expected, $result);
  3181. $data['Comment']['comment'] = '';
  3182. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3183. $this->assertFalse($result);
  3184. $result = $TestModel->Comment->Attachment->validationErrors;
  3185. $expected = array(
  3186. 'Comment' => array(
  3187. 'comment' => array('This field cannot be left blank'),
  3188. 'Article' => array(
  3189. 'body' => array('This field cannot be left blank'),
  3190. 'User' => array(
  3191. 'user' => array('This field cannot be left blank')
  3192. )
  3193. )
  3194. )
  3195. );
  3196. $this->assertSame($expected, $result);
  3197. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3198. $expected = array(
  3199. 'Attachment' => true,
  3200. 'Comment' => false
  3201. );
  3202. $this->assertEquals($expected, $result);
  3203. $data['Attachment']['attachment'] = '';
  3204. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
  3205. $this->assertFalse($result);
  3206. $result = $TestModel->Comment->Attachment->validationErrors;
  3207. $expected = array(
  3208. 'attachment' => array('This field cannot be left blank'),
  3209. 'Comment' => array(
  3210. 'comment' => array('This field cannot be left blank'),
  3211. 'Article' => array(
  3212. 'body' => array('This field cannot be left blank'),
  3213. 'User' => array(
  3214. 'user' => array('This field cannot be left blank')
  3215. )
  3216. )
  3217. )
  3218. );
  3219. $this->assertSame($expected, $result);
  3220. $result = $TestModel->Comment->validationErrors;
  3221. $expected = array(
  3222. 'comment' => array('This field cannot be left blank'),
  3223. 'Article' => array(
  3224. 'body' => array('This field cannot be left blank'),
  3225. 'User' => array(
  3226. 'user' => array('This field cannot be left blank')
  3227. )
  3228. )
  3229. );
  3230. $this->assertSame($expected, $result);
  3231. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
  3232. $expected = array(
  3233. 'Attachment' => false,
  3234. 'Comment' => false
  3235. );
  3236. $this->assertEquals($expected, $result);
  3237. }
  3238. /**
  3239. * testSaveAllNotDeepAssociated method
  3240. * test that only directly associated data gets saved
  3241. *
  3242. * @return void
  3243. */
  3244. public function testSaveAllNotDeepAssociated() {
  3245. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3246. $TestModel = new Article();
  3247. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3248. $TestModel->hasAndBelongsToMany = array();
  3249. $result = $TestModel->saveAll(array(
  3250. 'Article' => array('id' => 2),
  3251. 'Comment' => array(
  3252. array(
  3253. 'comment' => 'First new comment', 'published' => 'Y', 'user_id' => 2,
  3254. 'User' => array('user' => 'newuser', 'password' => 'newuserpass')
  3255. ),
  3256. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3257. )
  3258. ), array('deep' => false));
  3259. $this->assertTrue($result);
  3260. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  3261. $this->assertFalse($result);
  3262. $result = $TestModel->saveAll(array(
  3263. 'Article' => array('id' => 2),
  3264. 'Comment' => array(
  3265. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 4),
  3266. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  3267. )
  3268. ), array('deep' => false));
  3269. $this->assertTrue($result);
  3270. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  3271. $this->assertFalse($result);
  3272. $data = array(
  3273. 'Attachment' => array(
  3274. 'attachment' => 'deepsave insert',
  3275. ),
  3276. 'Comment' => array(
  3277. 'comment' => 'First comment deepsave insert',
  3278. 'published' => 'Y',
  3279. 'user_id' => 4,
  3280. 'article_id' => 1,
  3281. 'Article' => array(
  3282. 'title' => 'First Article deepsave insert',
  3283. 'body' => 'First Article Body deepsave insert',
  3284. 'User' => array(
  3285. 'user' => 'deepsave',
  3286. 'password' => 'magic'
  3287. ),
  3288. ),
  3289. )
  3290. );
  3291. $expected = $TestModel->User->find('count');
  3292. $TestModel->Comment->Attachment->create();
  3293. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => false));
  3294. $this->assertTrue($result);
  3295. $result = $TestModel->User->find('count');
  3296. $this->assertEquals($expected, $result);
  3297. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  3298. $expected = array(
  3299. 'Attachment' => array(
  3300. 'id' => '2',
  3301. 'comment_id' => '11',
  3302. 'attachment' => 'deepsave insert',
  3303. ),
  3304. 'Comment' => array(
  3305. 'id' => '11',
  3306. 'article_id' => 1,
  3307. 'user_id' => '4',
  3308. 'comment' => 'First comment deepsave insert',
  3309. 'published' => 'Y',
  3310. )
  3311. );
  3312. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  3313. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  3314. unset($result['Comment']['created'], $result['Comment']['updated']);
  3315. $this->assertEquals($expected['Comment'], $result['Comment']);
  3316. }
  3317. /**
  3318. * testSaveAllNotDeepMany
  3319. * tests the save methods to not save deeper recursive data
  3320. *
  3321. * @return void
  3322. */
  3323. public function testSaveAllNotDeepMany() {
  3324. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3325. $TestModel = new Article();
  3326. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3327. $TestModel->hasAndBelongsToMany = array();
  3328. $data = array(
  3329. array(
  3330. 'id' => 1,
  3331. 'body' => '',
  3332. 'Comment' => array(
  3333. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  3334. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  3335. )
  3336. ),
  3337. array(
  3338. 'Article' => array('id' => 2),
  3339. 'Comment' => array(
  3340. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  3341. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3342. )
  3343. )
  3344. );
  3345. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3346. $result = $TestModel->saveAll($data, array('deep' => false));
  3347. $this->assertFalse($result);
  3348. $expected = array(
  3349. 0 => array(
  3350. 'body' => array('This field cannot be left blank')
  3351. ),
  3352. 1 => array(
  3353. 'body' => array('This field cannot be left blank')
  3354. )
  3355. );
  3356. $result = $TestModel->validationErrors;
  3357. $this->assertSame($expected, $result);
  3358. $data = array(
  3359. array(
  3360. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3361. 'Comment' => array(
  3362. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3363. )
  3364. ),
  3365. array(
  3366. 'Article' => array('id' => 2, 'body' => 'Same here'),
  3367. 'Comment' => array(
  3368. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3369. )
  3370. )
  3371. );
  3372. $result = $TestModel->saveAll($data, array('deep' => false));
  3373. $this->assertTrue($result);
  3374. }
  3375. /**
  3376. * testSaveAllNotDeepValidateOnly
  3377. * tests the validate methods to not validate deeper recursive data
  3378. *
  3379. * @return void
  3380. */
  3381. public function testSaveAllNotDeepValidateOnly() {
  3382. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3383. $TestModel = new Article();
  3384. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3385. $TestModel->hasAndBelongsToMany = array();
  3386. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3387. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3388. $result = $TestModel->saveAll(
  3389. array(
  3390. 'Article' => array('id' => 2, 'body' => ''),
  3391. 'Comment' => array(
  3392. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3393. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3394. )
  3395. ),
  3396. array('validate' => 'only', 'deep' => false)
  3397. );
  3398. $this->assertFalse($result);
  3399. $expected = array('body' => array('This field cannot be left blank'));
  3400. $result = $TestModel->validationErrors;
  3401. $this->assertSame($expected, $result);
  3402. $result = $TestModel->saveAll(
  3403. array(
  3404. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3405. 'Comment' => array(
  3406. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3407. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3408. )
  3409. ),
  3410. array('validate' => 'only', 'deep' => false)
  3411. );
  3412. $this->assertTrue($result);
  3413. $result = $TestModel->saveAll(
  3414. array(
  3415. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3416. 'Comment' => array(
  3417. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3418. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3419. )
  3420. ),
  3421. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3422. );
  3423. $expected = array(
  3424. 'Article' => true,
  3425. 'Comment' => array(
  3426. true,
  3427. true
  3428. )
  3429. );
  3430. $this->assertSame($expected, $result);
  3431. $result = $TestModel->saveAll(array(
  3432. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3433. 'Comment' => array(
  3434. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3435. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3436. )
  3437. ),
  3438. array('validate' => 'only', 'deep' => false)
  3439. );
  3440. $this->assertTrue($result);
  3441. $result = $TestModel->saveAll(array(
  3442. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3443. 'Comment' => array(
  3444. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3445. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3446. )
  3447. ),
  3448. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3449. );
  3450. $expected = array(
  3451. 'Article' => true,
  3452. 'Comment' => array(
  3453. true,
  3454. true
  3455. )
  3456. );
  3457. $this->assertSame($expected, $result);
  3458. $expected = array();
  3459. $result = $TestModel->validationErrors;
  3460. $this->assertSame($expected, $result);
  3461. $data = array(
  3462. 'Attachment' => array(
  3463. 'attachment' => 'deepsave insert',
  3464. ),
  3465. 'Comment' => array(
  3466. 'comment' => 'First comment deepsave insert',
  3467. 'published' => 'Y',
  3468. 'user_id' => 5,
  3469. 'Article' => array(
  3470. 'title' => 'First Article deepsave insert ignored',
  3471. 'body' => 'First Article Body deepsave insert',
  3472. 'User' => array(
  3473. 'user' => '',
  3474. 'password' => 'magic'
  3475. ),
  3476. ),
  3477. )
  3478. );
  3479. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3480. $this->assertTrue($result);
  3481. $result = $TestModel->Comment->Attachment->validationErrors;
  3482. $expected = array();
  3483. $this->assertSame($expected, $result);
  3484. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3485. $expected = array(
  3486. 'Attachment' => true,
  3487. 'Comment' => true
  3488. );
  3489. $this->assertEquals($expected, $result);
  3490. $data['Comment']['Article']['body'] = '';
  3491. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3492. $this->assertTrue($result);
  3493. $result = $TestModel->Comment->Attachment->validationErrors;
  3494. $expected = array();
  3495. $this->assertSame($expected, $result);
  3496. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3497. $expected = array(
  3498. 'Attachment' => true,
  3499. 'Comment' => true
  3500. );
  3501. $this->assertEquals($expected, $result);
  3502. }
  3503. /**
  3504. * testSaveAllHasMany method
  3505. *
  3506. * @return void
  3507. */
  3508. public function testSaveAllHasMany() {
  3509. $this->loadFixtures('Article', 'Comment');
  3510. $TestModel = new Article();
  3511. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3512. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3513. $result = $TestModel->saveAll(array(
  3514. 'Article' => array('id' => 2),
  3515. 'Comment' => array(
  3516. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3517. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3518. )
  3519. ));
  3520. $this->assertFalse(empty($result));
  3521. $result = $TestModel->findById(2);
  3522. $expected = array(
  3523. 'First Comment for Second Article',
  3524. 'Second Comment for Second Article',
  3525. 'First new comment',
  3526. 'Second new comment'
  3527. );
  3528. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3529. $this->assertEquals($expected, $result);
  3530. $result = $TestModel->saveAll(
  3531. array(
  3532. 'Article' => array('id' => 2),
  3533. 'Comment' => array(
  3534. array(
  3535. 'comment' => 'Third new comment',
  3536. 'published' => 'Y',
  3537. 'user_id' => 1
  3538. ))),
  3539. array('atomic' => false)
  3540. );
  3541. $this->assertFalse(empty($result));
  3542. $result = $TestModel->findById(2);
  3543. $expected = array(
  3544. 'First Comment for Second Article',
  3545. 'Second Comment for Second Article',
  3546. 'First new comment',
  3547. 'Second new comment',
  3548. 'Third new comment'
  3549. );
  3550. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3551. $this->assertEquals($expected, $result);
  3552. $TestModel->beforeSaveReturn = false;
  3553. $result = $TestModel->saveAll(
  3554. array(
  3555. 'Article' => array('id' => 2),
  3556. 'Comment' => array(
  3557. array(
  3558. 'comment' => 'Fourth new comment',
  3559. 'published' => 'Y',
  3560. 'user_id' => 1
  3561. ))),
  3562. array('atomic' => false)
  3563. );
  3564. $this->assertEquals(array('Article' => false), $result);
  3565. $result = $TestModel->findById(2);
  3566. $expected = array(
  3567. 'First Comment for Second Article',
  3568. 'Second Comment for Second Article',
  3569. 'First new comment',
  3570. 'Second new comment',
  3571. 'Third new comment'
  3572. );
  3573. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3574. $this->assertEquals($expected, $result);
  3575. }
  3576. /**
  3577. * testSaveAllHasManyValidation method
  3578. *
  3579. * @return void
  3580. */
  3581. public function testSaveAllHasManyValidation() {
  3582. $this->loadFixtures('Article', 'Comment');
  3583. $TestModel = new Article();
  3584. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3585. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3586. $result = $TestModel->saveAll(array(
  3587. 'Article' => array('id' => 2),
  3588. 'Comment' => array(
  3589. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3590. )
  3591. ), array('validate' => true));
  3592. $this->assertFalse($result);
  3593. $expected = array('Comment' => array(
  3594. array('comment' => array('This field cannot be left blank'))
  3595. ));
  3596. $this->assertEquals($expected, $TestModel->validationErrors);
  3597. $expected = array(
  3598. array('comment' => array('This field cannot be left blank'))
  3599. );
  3600. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  3601. $result = $TestModel->saveAll(array(
  3602. 'Article' => array('id' => 2),
  3603. 'Comment' => array(
  3604. array(
  3605. 'comment' => '',
  3606. 'published' => 'Y',
  3607. 'user_id' => 1
  3608. ))
  3609. ), array('validate' => 'first'));
  3610. $this->assertFalse($result);
  3611. }
  3612. /**
  3613. * test saveAll with transactions and ensure there is no missing rollback.
  3614. *
  3615. * @return void
  3616. */
  3617. public function testSaveAllManyRowsTransactionNoRollback() {
  3618. $this->loadFixtures('Post');
  3619. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  3620. $db = ConnectionManager::create('mock_transaction', array(
  3621. 'datasource' => 'MockTransactionDboSource',
  3622. ));
  3623. $db->expects($this->once())
  3624. ->method('describe')
  3625. ->will($this->returnValue(array()));
  3626. $db->expects($this->once())->method('rollback');
  3627. $Post = new Post('mock_transaction');
  3628. $Post->validate = array(
  3629. 'title' => array('rule' => array('notEmpty'))
  3630. );
  3631. $data = array(
  3632. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3633. array('author_id' => 1, 'title' => '')
  3634. );
  3635. $Post->saveAll($data, array('atomic' => true));
  3636. }
  3637. /**
  3638. * test saveAll with transactions and ensure there is no missing rollback.
  3639. *
  3640. * @return void
  3641. */
  3642. public function testSaveAllAssociatedTransactionNoRollback() {
  3643. $testDb = ConnectionManager::getDataSource('test');
  3644. $mock = $this->getMock(
  3645. 'DboSource',
  3646. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  3647. array(),
  3648. 'MockTransactionAssociatedDboSource'
  3649. );
  3650. $db = ConnectionManager::create('mock_transaction_assoc', array(
  3651. 'datasource' => 'MockTransactionAssociatedDboSource',
  3652. ));
  3653. $this->mockObjects[] = $db;
  3654. $db->columns = $testDb->columns;
  3655. $db->expects($this->once())->method('rollback');
  3656. $db->expects($this->any())->method('describe')
  3657. ->will($this->returnValue(array(
  3658. 'id' => array('type' => 'integer', 'length' => 11),
  3659. 'title' => array('type' => 'string'),
  3660. 'body' => array('type' => 'text'),
  3661. 'published' => array('type' => 'string')
  3662. )));
  3663. $Post = new Post();
  3664. $Post->useDbConfig = 'mock_transaction_assoc';
  3665. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  3666. $Post->Author->validate = array(
  3667. 'user' => array('rule' => array('notEmpty'))
  3668. );
  3669. $data = array(
  3670. 'Post' => array(
  3671. 'title' => 'New post',
  3672. 'body' => 'Content',
  3673. 'published' => 'Y'
  3674. ),
  3675. 'Author' => array(
  3676. 'user' => '',
  3677. 'password' => "sekret"
  3678. )
  3679. );
  3680. $Post->saveAll($data, array('validate' => true));
  3681. }
  3682. /**
  3683. * test saveAll with nested saveAll call.
  3684. *
  3685. * @return void
  3686. */
  3687. public function testSaveAllNestedSaveAll() {
  3688. $this->loadFixtures('Sample');
  3689. $TransactionTestModel = new TransactionTestModel();
  3690. $data = array(
  3691. array('apple_id' => 1, 'name' => 'sample5'),
  3692. );
  3693. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  3694. }
  3695. /**
  3696. * testSaveAllTransaction method
  3697. *
  3698. * @return void
  3699. */
  3700. public function testSaveAllTransaction() {
  3701. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3702. $TestModel = new Post();
  3703. $TestModel->validate = array('title' => 'notEmpty');
  3704. $data = array(
  3705. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3706. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3707. array('author_id' => 1, 'title' => '')
  3708. );
  3709. $this->assertFalse($TestModel->saveAll($data));
  3710. $result = $TestModel->find('all', array('recursive' => -1));
  3711. $expected = array(
  3712. array('Post' => array(
  3713. 'id' => '1',
  3714. 'author_id' => 1,
  3715. 'title' => 'First Post',
  3716. 'body' => 'First Post Body',
  3717. 'published' => 'Y',
  3718. 'created' => '2007-03-18 10:39:23',
  3719. 'updated' => '2007-03-18 10:41:31'
  3720. )),
  3721. array('Post' => array(
  3722. 'id' => '2',
  3723. 'author_id' => 3,
  3724. 'title' => 'Second Post',
  3725. 'body' => 'Second Post Body',
  3726. 'published' => 'Y',
  3727. 'created' => '2007-03-18 10:41:23',
  3728. 'updated' => '2007-03-18 10:43:31'
  3729. )),
  3730. array('Post' => array(
  3731. 'id' => '3',
  3732. 'author_id' => 1,
  3733. 'title' => 'Third Post',
  3734. 'body' => 'Third Post Body',
  3735. 'published' => 'Y',
  3736. 'created' => '2007-03-18 10:43:23',
  3737. 'updated' => '2007-03-18 10:45:31'
  3738. )));
  3739. if (count($result) != 3) {
  3740. // Database doesn't support transactions
  3741. $expected[] = array(
  3742. 'Post' => array(
  3743. 'id' => '4',
  3744. 'author_id' => 1,
  3745. 'title' => 'New Fourth Post',
  3746. 'body' => null,
  3747. 'published' => 'N',
  3748. 'created' => self::date(),
  3749. 'updated' => self::date()
  3750. ));
  3751. $expected[] = array(
  3752. 'Post' => array(
  3753. 'id' => '5',
  3754. 'author_id' => 1,
  3755. 'title' => 'New Fifth Post',
  3756. 'body' => null,
  3757. 'published' => 'N',
  3758. 'created' => self::date(),
  3759. 'updated' => self::date()
  3760. ));
  3761. $this->assertEquals($expected, $result);
  3762. // Skip the rest of the transactional tests
  3763. return;
  3764. }
  3765. $this->assertEquals($expected, $result);
  3766. $data = array(
  3767. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3768. array('author_id' => 1, 'title' => ''),
  3769. array('author_id' => 1, 'title' => 'New Sixth Post')
  3770. );
  3771. $this->assertFalse($TestModel->saveAll($data));
  3772. $result = $TestModel->find('all', array('recursive' => -1));
  3773. $expected = array(
  3774. array('Post' => array(
  3775. 'id' => '1',
  3776. 'author_id' => 1,
  3777. 'title' => 'First Post',
  3778. 'body' => 'First Post Body',
  3779. 'published' => 'Y',
  3780. 'created' => '2007-03-18 10:39:23',
  3781. 'updated' => '2007-03-18 10:41:31'
  3782. )),
  3783. array('Post' => array(
  3784. 'id' => '2',
  3785. 'author_id' => 3,
  3786. 'title' => 'Second Post',
  3787. 'body' => 'Second Post Body',
  3788. 'published' => 'Y',
  3789. 'created' => '2007-03-18 10:41:23',
  3790. 'updated' => '2007-03-18 10:43:31'
  3791. )),
  3792. array('Post' => array(
  3793. 'id' => '3',
  3794. 'author_id' => 1,
  3795. 'title' => 'Third Post',
  3796. 'body' => 'Third Post Body',
  3797. 'published' => 'Y',
  3798. 'created' => '2007-03-18 10:43:23',
  3799. 'updated' => '2007-03-18 10:45:31'
  3800. )));
  3801. if (count($result) != 3) {
  3802. // Database doesn't support transactions
  3803. $expected[] = array(
  3804. 'Post' => array(
  3805. 'id' => '4',
  3806. 'author_id' => 1,
  3807. 'title' => 'New Fourth Post',
  3808. 'body' => 'Third Post Body',
  3809. 'published' => 'N',
  3810. 'created' => self::date(),
  3811. 'updated' => self::date()
  3812. ));
  3813. $expected[] = array(
  3814. 'Post' => array(
  3815. 'id' => '5',
  3816. 'author_id' => 1,
  3817. 'title' => 'Third Post',
  3818. 'body' => 'Third Post Body',
  3819. 'published' => 'N',
  3820. 'created' => self::date(),
  3821. 'updated' => self::date()
  3822. ));
  3823. }
  3824. $this->assertEquals($expected, $result);
  3825. $TestModel->validate = array('title' => 'notEmpty');
  3826. $data = array(
  3827. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3828. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3829. array('author_id' => 1, 'title' => 'New Sixth Post')
  3830. );
  3831. $this->assertTrue($TestModel->saveAll($data));
  3832. $result = $TestModel->find('all', array(
  3833. 'recursive' => -1,
  3834. 'fields' => array('author_id', 'title','body','published'),
  3835. 'order' => array('Post.created' => 'ASC')
  3836. ));
  3837. $expected = array(
  3838. array('Post' => array(
  3839. 'author_id' => 1,
  3840. 'title' => 'First Post',
  3841. 'body' => 'First Post Body',
  3842. 'published' => 'Y'
  3843. )),
  3844. array('Post' => array(
  3845. 'author_id' => 3,
  3846. 'title' => 'Second Post',
  3847. 'body' => 'Second Post Body',
  3848. 'published' => 'Y'
  3849. )),
  3850. array('Post' => array(
  3851. 'author_id' => 1,
  3852. 'title' => 'Third Post',
  3853. 'body' => 'Third Post Body',
  3854. 'published' => 'Y'
  3855. )),
  3856. array('Post' => array(
  3857. 'author_id' => 1,
  3858. 'title' => 'New Fourth Post',
  3859. 'body' => '',
  3860. 'published' => 'N'
  3861. )),
  3862. array('Post' => array(
  3863. 'author_id' => 1,
  3864. 'title' => 'New Fifth Post',
  3865. 'body' => '',
  3866. 'published' => 'N'
  3867. )),
  3868. array('Post' => array(
  3869. 'author_id' => 1,
  3870. 'title' => 'New Sixth Post',
  3871. 'body' => '',
  3872. 'published' => 'N'
  3873. )));
  3874. $this->assertEquals($expected, $result);
  3875. }
  3876. /**
  3877. * testSaveAllValidation method
  3878. *
  3879. * @return void
  3880. */
  3881. public function testSaveAllValidation() {
  3882. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3883. $TestModel = new Post();
  3884. $data = array(
  3885. array(
  3886. 'id' => '1',
  3887. 'title' => 'Baleeted First Post',
  3888. 'body' => 'Baleeted!',
  3889. 'published' => 'N'
  3890. ),
  3891. array(
  3892. 'id' => '2',
  3893. 'title' => 'Just update the title'
  3894. ),
  3895. array(
  3896. 'title' => 'Creating a fourth post',
  3897. 'body' => 'Fourth post body',
  3898. 'author_id' => 2
  3899. ));
  3900. $this->assertTrue($TestModel->saveAll($data));
  3901. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3902. $expected = array(
  3903. array(
  3904. 'Post' => array(
  3905. 'id' => '1',
  3906. 'author_id' => '1',
  3907. 'title' => 'Baleeted First Post',
  3908. 'body' => 'Baleeted!',
  3909. 'published' => 'N',
  3910. 'created' => '2007-03-18 10:39:23'
  3911. )),
  3912. array(
  3913. 'Post' => array(
  3914. 'id' => '2',
  3915. 'author_id' => '3',
  3916. 'title' => 'Just update the title',
  3917. 'body' => 'Second Post Body',
  3918. 'published' => 'Y',
  3919. 'created' => '2007-03-18 10:41:23'
  3920. )),
  3921. array(
  3922. 'Post' => array(
  3923. 'id' => '3',
  3924. 'author_id' => '1',
  3925. 'title' => 'Third Post',
  3926. 'body' => 'Third Post Body',
  3927. 'published' => 'Y',
  3928. 'created' => '2007-03-18 10:43:23',
  3929. 'updated' => '2007-03-18 10:45:31'
  3930. )),
  3931. array(
  3932. 'Post' => array(
  3933. 'id' => '4',
  3934. 'author_id' => '2',
  3935. 'title' => 'Creating a fourth post',
  3936. 'body' => 'Fourth post body',
  3937. 'published' => 'N'
  3938. )));
  3939. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  3940. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  3941. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  3942. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  3943. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  3944. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  3945. $this->assertEquals($expected, $result);
  3946. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3947. $data = array(
  3948. array(
  3949. 'id' => '1',
  3950. 'title' => 'Un-Baleeted First Post',
  3951. 'body' => 'Not Baleeted!',
  3952. 'published' => 'Y'
  3953. ),
  3954. array(
  3955. 'id' => '2',
  3956. 'title' => '',
  3957. 'body' => 'Trying to get away with an empty title'
  3958. ));
  3959. $result = $TestModel->saveAll($data);
  3960. $this->assertFalse($result);
  3961. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3962. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3963. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3964. if (!$transactionWorked) {
  3965. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3966. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3967. }
  3968. $this->assertEquals($errors, $TestModel->validationErrors);
  3969. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3970. $data = array(
  3971. array(
  3972. 'id' => '1',
  3973. 'title' => 'Un-Baleeted First Post',
  3974. 'body' => 'Not Baleeted!',
  3975. 'published' => 'Y'
  3976. ),
  3977. array(
  3978. 'id' => '2',
  3979. 'title' => '',
  3980. 'body' => 'Trying to get away with an empty title'
  3981. ));
  3982. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3983. $this->assertEquals(array(true, false), $result);
  3984. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3985. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3986. $expected = array(
  3987. array(
  3988. 'Post' => array(
  3989. 'id' => '1',
  3990. 'author_id' => '1',
  3991. 'title' => 'Un-Baleeted First Post',
  3992. 'body' => 'Not Baleeted!',
  3993. 'published' => 'Y',
  3994. 'created' => '2007-03-18 10:39:23'
  3995. )
  3996. ),
  3997. array(
  3998. 'Post' => array(
  3999. 'id' => '2',
  4000. 'author_id' => '3',
  4001. 'title' => 'Just update the title',
  4002. 'body' => 'Second Post Body',
  4003. 'published' => 'Y',
  4004. 'created' => '2007-03-18 10:41:23'
  4005. )
  4006. ),
  4007. array(
  4008. 'Post' => array(
  4009. 'id' => '3',
  4010. 'author_id' => '1',
  4011. 'title' => 'Third Post',
  4012. 'body' => 'Third Post Body',
  4013. 'published' => 'Y',
  4014. 'created' => '2007-03-18 10:43:23',
  4015. 'updated' => '2007-03-18 10:45:31'
  4016. )
  4017. ),
  4018. array(
  4019. 'Post' => array(
  4020. 'id' => '4',
  4021. 'author_id' => '2',
  4022. 'title' => 'Creating a fourth post',
  4023. 'body' => 'Fourth post body',
  4024. 'published' => 'N'
  4025. )
  4026. )
  4027. );
  4028. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4029. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4030. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4031. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4032. unset(
  4033. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4034. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4035. );
  4036. $this->assertEquals($expected, $result);
  4037. $this->assertEquals($errors, $TestModel->validationErrors);
  4038. $data = array(
  4039. array(
  4040. 'id' => '1',
  4041. 'title' => 'Re-Baleeted First Post',
  4042. 'body' => 'Baleeted!',
  4043. 'published' => 'N'
  4044. ),
  4045. array(
  4046. 'id' => '2',
  4047. 'title' => '',
  4048. 'body' => 'Trying to get away with an empty title'
  4049. ));
  4050. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  4051. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4052. unset(
  4053. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4054. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4055. );
  4056. $this->assertEquals($expected, $result);
  4057. $this->assertEquals($errors, $TestModel->validationErrors);
  4058. }
  4059. /**
  4060. * testSaveAllValidationOnly method
  4061. *
  4062. * @return void
  4063. */
  4064. public function testSaveAllValidationOnly() {
  4065. $this->loadFixtures('Comment', 'Attachment');
  4066. $TestModel = new Comment();
  4067. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4068. $data = array(
  4069. 'Comment' => array(
  4070. 'comment' => 'This is the comment'
  4071. ),
  4072. 'Attachment' => array(
  4073. 'attachment' => ''
  4074. )
  4075. );
  4076. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  4077. $this->assertFalse($result);
  4078. $TestModel = new Article();
  4079. $TestModel->validate = array('title' => 'notEmpty');
  4080. $result = $TestModel->saveAll(
  4081. array(
  4082. 0 => array('title' => ''),
  4083. 1 => array('title' => 'title 1'),
  4084. 2 => array('title' => 'title 2'),
  4085. ),
  4086. array('validate' => 'only')
  4087. );
  4088. $this->assertFalse($result);
  4089. $expected = array(
  4090. 0 => array('title' => array('This field cannot be left blank')),
  4091. );
  4092. $this->assertEquals($expected, $TestModel->validationErrors);
  4093. $result = $TestModel->saveAll(
  4094. array(
  4095. 0 => array('title' => 'title 0'),
  4096. 1 => array('title' => ''),
  4097. 2 => array('title' => 'title 2'),
  4098. ),
  4099. array('validate' => 'only')
  4100. );
  4101. $this->assertFalse($result);
  4102. $expected = array(
  4103. 1 => array('title' => array('This field cannot be left blank')),
  4104. );
  4105. $this->assertEquals($expected, $TestModel->validationErrors);
  4106. }
  4107. /**
  4108. * testSaveAllValidateFirst method
  4109. *
  4110. * @return void
  4111. */
  4112. public function testSaveAllValidateFirst() {
  4113. $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
  4114. $model = new Article();
  4115. $model->deleteAll(true);
  4116. $model->Comment->validate = array('comment' => 'notEmpty');
  4117. $result = $model->saveAll(array(
  4118. 'Article' => array(
  4119. 'title' => 'Post with Author',
  4120. 'body' => 'This post will be saved author'
  4121. ),
  4122. 'Comment' => array(
  4123. array('comment' => 'First new comment'),
  4124. array('comment' => '')
  4125. )
  4126. ), array('validate' => 'first'));
  4127. $this->assertFalse($result);
  4128. $result = $model->find('all');
  4129. $this->assertEquals(array(), $result);
  4130. $expected = array('Comment' => array(
  4131. 1 => array('comment' => array('This field cannot be left blank'))
  4132. ));
  4133. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4134. $this->assertSame($model->Comment->find('count'), 0);
  4135. $result = $model->saveAll(
  4136. array(
  4137. 'Article' => array(
  4138. 'title' => 'Post with Author',
  4139. 'body' => 'This post will be saved with an author',
  4140. 'user_id' => 2
  4141. ),
  4142. 'Comment' => array(
  4143. array(
  4144. 'comment' => 'Only new comment',
  4145. 'user_id' => 2
  4146. ))),
  4147. array('validate' => 'first')
  4148. );
  4149. $this->assertSame($result, true);
  4150. $result = $model->Comment->find('all');
  4151. $this->assertSame(count($result), 1);
  4152. $result = Hash::extract($result, '{n}.Comment.article_id');
  4153. $this->assertEquals(4, $result[0]);
  4154. $model->deleteAll(true);
  4155. $data = array(
  4156. 'Article' => array(
  4157. 'title' => 'Post with Author saveAlled from comment',
  4158. 'body' => 'This post will be saved with an author',
  4159. 'user_id' => 2
  4160. ),
  4161. 'Comment' => array(
  4162. 'comment' => 'Only new comment', 'user_id' => 2
  4163. ));
  4164. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  4165. $this->assertFalse(empty($result));
  4166. $result = $model->find('all');
  4167. $this->assertEquals(
  4168. $result[0]['Article']['title'],
  4169. 'Post with Author saveAlled from comment'
  4170. );
  4171. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4172. }
  4173. /**
  4174. * test saveAll()'s return is correct when using atomic = false and validate = first.
  4175. *
  4176. * @return void
  4177. */
  4178. public function testSaveAllValidateFirstAtomicFalse() {
  4179. $this->loadFixtures('Something');
  4180. $Something = new Something();
  4181. $invalidData = array(
  4182. array(
  4183. 'title' => 'foo',
  4184. 'body' => 'bar',
  4185. 'published' => 'baz',
  4186. ),
  4187. array(
  4188. 'body' => 3,
  4189. 'published' => 'sd',
  4190. ),
  4191. );
  4192. $Something->create();
  4193. $Something->validate = array(
  4194. 'title' => array(
  4195. 'rule' => 'alphaNumeric',
  4196. 'required' => true,
  4197. ),
  4198. 'body' => array(
  4199. 'rule' => 'alphaNumeric',
  4200. 'required' => true,
  4201. 'allowEmpty' => true,
  4202. ),
  4203. );
  4204. $result = $Something->saveAll($invalidData, array(
  4205. 'atomic' => false,
  4206. 'validate' => 'first',
  4207. ));
  4208. $expected = array(true, false);
  4209. $this->assertEquals($expected, $result);
  4210. $Something = new Something();
  4211. $validData = array(
  4212. array(
  4213. 'title' => 'title value',
  4214. 'body' => 'body value',
  4215. 'published' => 'baz',
  4216. ),
  4217. array(
  4218. 'title' => 'valid',
  4219. 'body' => 'this body',
  4220. 'published' => 'sd',
  4221. ),
  4222. );
  4223. $Something->create();
  4224. $result = $Something->saveAll($validData, array(
  4225. 'atomic' => false,
  4226. 'validate' => 'first',
  4227. ));
  4228. $expected = array(true, true);
  4229. $this->assertEquals($expected, $result);
  4230. }
  4231. /**
  4232. * testSaveAllHasManyValidationOnly method
  4233. *
  4234. * @return void
  4235. */
  4236. public function testSaveAllHasManyValidationOnly() {
  4237. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4238. $TestModel = new Article();
  4239. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4240. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4241. $result = $TestModel->saveAll(
  4242. array(
  4243. 'Article' => array('id' => 2),
  4244. 'Comment' => array(
  4245. array(
  4246. 'id' => 1,
  4247. 'comment' => '',
  4248. 'published' => 'Y',
  4249. 'user_id' => 1),
  4250. array(
  4251. 'id' => 2,
  4252. 'comment' =>
  4253. 'comment',
  4254. 'published' => 'Y',
  4255. 'user_id' => 1
  4256. ))),
  4257. array('validate' => 'only')
  4258. );
  4259. $this->assertFalse($result);
  4260. $result = $TestModel->saveAll(
  4261. array(
  4262. 'Article' => array('id' => 2),
  4263. 'Comment' => array(
  4264. array(
  4265. 'id' => 1,
  4266. 'comment' => '',
  4267. 'published' => 'Y',
  4268. 'user_id' => 1
  4269. ),
  4270. array(
  4271. 'id' => 2,
  4272. 'comment' => 'comment',
  4273. 'published' => 'Y',
  4274. 'user_id' => 1
  4275. ),
  4276. array(
  4277. 'id' => 3,
  4278. 'comment' => '',
  4279. 'published' => 'Y',
  4280. 'user_id' => 1
  4281. ))),
  4282. array(
  4283. 'validate' => 'only',
  4284. 'atomic' => false
  4285. ));
  4286. $expected = array(
  4287. 'Article' => true,
  4288. 'Comment' => array(false, true, false)
  4289. );
  4290. $this->assertSame($expected, $result);
  4291. $expected = array('Comment' => array(
  4292. 0 => array('comment' => array('This field cannot be left blank')),
  4293. 2 => array('comment' => array('This field cannot be left blank'))
  4294. ));
  4295. $this->assertEquals($expected, $TestModel->validationErrors);
  4296. $expected = array(
  4297. 0 => array('comment' => array('This field cannot be left blank')),
  4298. 2 => array('comment' => array('This field cannot be left blank'))
  4299. );
  4300. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4301. }
  4302. /**
  4303. * test that saveAll behaves like plain save() when supplied empty data
  4304. *
  4305. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4306. * @return void
  4307. */
  4308. public function testSaveAllEmptyData() {
  4309. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4310. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4311. $model = new Article();
  4312. $result = $model->saveAll(array(), array('validate' => 'first'));
  4313. $this->assertFalse(empty($result));
  4314. $model = new ProductUpdateAll();
  4315. $result = $model->saveAll(array());
  4316. $this->assertFalse($result);
  4317. }
  4318. /**
  4319. * testSaveAssociated method
  4320. *
  4321. * @return void
  4322. */
  4323. public function testSaveAssociated() {
  4324. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  4325. $TestModel = new Post();
  4326. $result = $TestModel->find('all');
  4327. $this->assertEquals(3, count($result));
  4328. $this->assertFalse(isset($result[3]));
  4329. $TestModel->saveAssociated(array(
  4330. 'Post' => array(
  4331. 'title' => 'Post with Author',
  4332. 'body' => 'This post will be saved with an author'
  4333. ),
  4334. 'Author' => array(
  4335. 'user' => 'bob',
  4336. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  4337. )));
  4338. $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
  4339. $expected = array(
  4340. 'Post' => array(
  4341. 'id' => '4',
  4342. 'author_id' => '5',
  4343. 'title' => 'Post with Author',
  4344. 'body' => 'This post will be saved with an author',
  4345. 'published' => 'N'
  4346. ),
  4347. 'Author' => array(
  4348. 'id' => '5',
  4349. 'user' => 'bob',
  4350. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  4351. 'test' => 'working'
  4352. ));
  4353. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4354. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4355. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  4356. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  4357. unset(
  4358. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  4359. $result[3]['Author']['updated'], $result[3]['Author']['created']
  4360. );
  4361. $this->assertEquals($expected, $result[3]);
  4362. $this->assertEquals(4, count($result));
  4363. $TestModel = new Comment();
  4364. $result = $TestModel->saveAssociated(array(
  4365. 'Comment' => array(
  4366. 'article_id' => 2,
  4367. 'user_id' => 2,
  4368. 'comment' => 'New comment with attachment',
  4369. 'published' => 'Y'
  4370. ),
  4371. 'Attachment' => array(
  4372. 'attachment' => 'some_file.tgz'
  4373. )));
  4374. $this->assertFalse(empty($result));
  4375. $result = $TestModel->find('all');
  4376. $expected = array(
  4377. 'id' => '7',
  4378. 'article_id' => '2',
  4379. 'user_id' => '2',
  4380. 'comment' => 'New comment with attachment',
  4381. 'published' => 'Y'
  4382. );
  4383. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  4384. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  4385. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  4386. $this->assertEquals($expected, $result[6]['Comment']);
  4387. $expected = array(
  4388. 'id' => '2',
  4389. 'comment_id' => '7',
  4390. 'attachment' => 'some_file.tgz'
  4391. );
  4392. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  4393. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  4394. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  4395. $this->assertEquals($expected, $result[6]['Attachment']);
  4396. }
  4397. /**
  4398. * testSaveMany method
  4399. *
  4400. * @return void
  4401. */
  4402. public function testSaveMany() {
  4403. $this->loadFixtures('Post');
  4404. $TestModel = new Post();
  4405. $TestModel->deleteAll(true);
  4406. $this->assertEquals(array(), $TestModel->find('all'));
  4407. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  4408. $this->db->truncate($TestModel);
  4409. $TestModel->saveMany(array(
  4410. array(
  4411. 'title' => 'Multi-record post 1',
  4412. 'body' => 'First multi-record post',
  4413. 'author_id' => 2
  4414. ),
  4415. array(
  4416. 'title' => 'Multi-record post 2',
  4417. 'body' => 'Second multi-record post',
  4418. 'author_id' => 2
  4419. )));
  4420. $result = $TestModel->find('all', array(
  4421. 'recursive' => -1,
  4422. 'order' => 'Post.id ASC'
  4423. ));
  4424. $expected = array(
  4425. array(
  4426. 'Post' => array(
  4427. 'id' => '1',
  4428. 'author_id' => '2',
  4429. 'title' => 'Multi-record post 1',
  4430. 'body' => 'First multi-record post',
  4431. 'published' => 'N'
  4432. )
  4433. ),
  4434. array(
  4435. 'Post' => array(
  4436. 'id' => '2',
  4437. 'author_id' => '2',
  4438. 'title' => 'Multi-record post 2',
  4439. 'body' => 'Second multi-record post',
  4440. 'published' => 'N'
  4441. )
  4442. )
  4443. );
  4444. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4445. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  4446. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4447. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  4448. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  4449. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  4450. $this->assertEquals($expected, $result);
  4451. }
  4452. /**
  4453. * Test SaveAssociated with Habtm relations
  4454. *
  4455. * @return void
  4456. */
  4457. public function testSaveAssociatedHabtm() {
  4458. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4459. $data = array(
  4460. 'Article' => array(
  4461. 'user_id' => 1,
  4462. 'title' => 'Article Has and belongs to Many Tags'
  4463. ),
  4464. 'Tag' => array(
  4465. 'Tag' => array(1, 2)
  4466. ),
  4467. 'Comment' => array(
  4468. array(
  4469. 'comment' => 'Article comment',
  4470. 'user_id' => 1
  4471. )));
  4472. $Article = new Article();
  4473. $result = $Article->saveAssociated($data);
  4474. $this->assertFalse(empty($result));
  4475. $result = $Article->read();
  4476. $this->assertEquals(2, count($result['Tag']));
  4477. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  4478. $this->assertEquals(1, count($result['Comment']));
  4479. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  4480. }
  4481. /**
  4482. * Test SaveAssociated with Habtm relations and extra join table fields
  4483. *
  4484. * @return void
  4485. */
  4486. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  4487. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  4488. $data = array(
  4489. 'Something' => array(
  4490. 'id' => 4,
  4491. 'title' => 'Extra Fields',
  4492. 'body' => 'Extra Fields Body',
  4493. 'published' => '1'
  4494. ),
  4495. 'SomethingElse' => array(
  4496. array('something_else_id' => 1, 'doomed' => '1'),
  4497. array('something_else_id' => 2, 'doomed' => '0'),
  4498. array('something_else_id' => 3, 'doomed' => '1')
  4499. )
  4500. );
  4501. $Something = new Something();
  4502. $result = $Something->saveAssociated($data);
  4503. $this->assertFalse(empty($result));
  4504. $result = $Something->read();
  4505. $this->assertEquals(3, count($result['SomethingElse']));
  4506. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  4507. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  4508. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  4509. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  4510. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  4511. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  4512. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  4513. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  4514. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  4515. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  4516. }
  4517. /**
  4518. * testSaveAssociatedHasOne method
  4519. *
  4520. * @return void
  4521. */
  4522. public function testSaveAssociatedHasOne() {
  4523. $model = new Comment();
  4524. $model->deleteAll(true);
  4525. $this->assertEquals(array(), $model->find('all'));
  4526. $model->Attachment->deleteAll(true);
  4527. $this->assertEquals(array(), $model->Attachment->find('all'));
  4528. $this->assertTrue($model->saveAssociated(array(
  4529. 'Comment' => array(
  4530. 'comment' => 'Comment with attachment',
  4531. 'article_id' => 1,
  4532. 'user_id' => 1
  4533. ),
  4534. 'Attachment' => array(
  4535. 'attachment' => 'some_file.zip'
  4536. ))));
  4537. $result = $model->find('all', array('fields' => array(
  4538. 'Comment.id', 'Comment.comment', 'Attachment.id',
  4539. 'Attachment.comment_id', 'Attachment.attachment'
  4540. )));
  4541. $expected = array(array(
  4542. 'Comment' => array(
  4543. 'id' => '1',
  4544. 'comment' => 'Comment with attachment'
  4545. ),
  4546. 'Attachment' => array(
  4547. 'id' => '1',
  4548. 'comment_id' => '1',
  4549. 'attachment' => 'some_file.zip'
  4550. )));
  4551. $this->assertEquals($expected, $result);
  4552. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  4553. $data = array(
  4554. 'Comment' => array(
  4555. 'comment' => 'Comment with attachment',
  4556. 'article_id' => 1,
  4557. 'user_id' => 1
  4558. ),
  4559. 'Attachment' => array(
  4560. 'attachment' => 'some_file.zip'
  4561. ));
  4562. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  4563. }
  4564. /**
  4565. * testSaveAssociatedBelongsTo method
  4566. *
  4567. * @return void
  4568. */
  4569. public function testSaveAssociatedBelongsTo() {
  4570. $model = new Comment();
  4571. $model->deleteAll(true);
  4572. $this->assertEquals(array(), $model->find('all'));
  4573. $model->Article->deleteAll(true);
  4574. $this->assertEquals(array(), $model->Article->find('all'));
  4575. $this->assertTrue($model->saveAssociated(array(
  4576. 'Comment' => array(
  4577. 'comment' => 'Article comment',
  4578. 'article_id' => 1,
  4579. 'user_id' => 1
  4580. ),
  4581. 'Article' => array(
  4582. 'title' => 'Model Associations 101',
  4583. 'user_id' => 1
  4584. ))));
  4585. $result = $model->find('all', array('fields' => array(
  4586. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  4587. )));
  4588. $expected = array(array(
  4589. 'Comment' => array(
  4590. 'id' => '1',
  4591. 'article_id' => '1',
  4592. 'comment' => 'Article comment'
  4593. ),
  4594. 'Article' => array(
  4595. 'id' => '1',
  4596. 'title' => 'Model Associations 101'
  4597. )));
  4598. $this->assertEquals($expected, $result);
  4599. }
  4600. /**
  4601. * testSaveAssociatedHasOneValidation method
  4602. *
  4603. * @return void
  4604. */
  4605. public function testSaveAssociatedHasOneValidation() {
  4606. $model = new Comment();
  4607. $model->deleteAll(true);
  4608. $this->assertEquals(array(), $model->find('all'));
  4609. $model->Attachment->deleteAll(true);
  4610. $this->assertEquals(array(), $model->Attachment->find('all'));
  4611. $model->validate = array('comment' => 'notEmpty');
  4612. $model->Attachment->validate = array('attachment' => 'notEmpty');
  4613. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  4614. $result = $model->saveAssociated(
  4615. array(
  4616. 'Comment' => array(
  4617. 'comment' => '',
  4618. 'article_id' => 1,
  4619. 'user_id' => 1
  4620. ),
  4621. 'Attachment' => array('attachment' => '')
  4622. )
  4623. );
  4624. $this->assertFalse($result);
  4625. $expected = array(
  4626. 'comment' => array(
  4627. 'This field cannot be left blank'
  4628. ),
  4629. 'Attachment' => array(
  4630. 'attachment' => array(
  4631. 'This field cannot be left blank'
  4632. )
  4633. )
  4634. );
  4635. $this->assertEquals($expected, $model->validationErrors);
  4636. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  4637. }
  4638. /**
  4639. * testSaveAssociatedAtomic method
  4640. *
  4641. * @return void
  4642. */
  4643. public function testSaveAssociatedAtomic() {
  4644. $this->loadFixtures('Article', 'User');
  4645. $TestModel = new Article();
  4646. $result = $TestModel->saveAssociated(array(
  4647. 'Article' => array(
  4648. 'title' => 'Post with Author',
  4649. 'body' => 'This post will be saved with an author',
  4650. 'user_id' => 2
  4651. ),
  4652. 'Comment' => array(
  4653. array('comment' => 'First new comment', 'user_id' => 2))
  4654. ), array('atomic' => false));
  4655. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  4656. $result = $TestModel->saveAssociated(array(
  4657. 'Article' => array('id' => 2),
  4658. 'Comment' => array(
  4659. array(
  4660. 'comment' => 'First new comment',
  4661. 'published' => 'Y',
  4662. 'user_id' => 1
  4663. ),
  4664. array(
  4665. 'comment' => 'Second new comment',
  4666. 'published' => 'Y',
  4667. 'user_id' => 2
  4668. ))
  4669. ), array('validate' => true, 'atomic' => false));
  4670. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  4671. }
  4672. /**
  4673. * testSaveManyAtomic method
  4674. *
  4675. * @return void
  4676. */
  4677. public function testSaveManyAtomic() {
  4678. $this->loadFixtures('Article', 'User');
  4679. $TestModel = new Article();
  4680. $result = $TestModel->saveMany(array(
  4681. array(
  4682. 'id' => '1',
  4683. 'title' => 'Baleeted First Post',
  4684. 'body' => 'Baleeted!',
  4685. 'published' => 'N'
  4686. ),
  4687. array(
  4688. 'id' => '2',
  4689. 'title' => 'Just update the title'
  4690. ),
  4691. array(
  4692. 'title' => 'Creating a fourth post',
  4693. 'body' => 'Fourth post body',
  4694. 'user_id' => 2
  4695. )
  4696. ), array('atomic' => false));
  4697. $this->assertSame($result, array(true, true, true));
  4698. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4699. $result = $TestModel->saveMany(array(
  4700. array(
  4701. 'id' => '1',
  4702. 'title' => 'Un-Baleeted First Post',
  4703. 'body' => 'Not Baleeted!',
  4704. 'published' => 'Y'
  4705. ),
  4706. array(
  4707. 'id' => '2',
  4708. 'title' => '',
  4709. 'body' => 'Trying to get away with an empty title'
  4710. )
  4711. ), array('validate' => true, 'atomic' => false));
  4712. $this->assertSame(array(true, false), $result);
  4713. }
  4714. /**
  4715. * testSaveAssociatedHasMany method
  4716. *
  4717. * @return void
  4718. */
  4719. public function testSaveAssociatedHasMany() {
  4720. $this->loadFixtures('Article', 'Comment');
  4721. $TestModel = new Article();
  4722. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4723. $result = $TestModel->saveAssociated(array(
  4724. 'Article' => array('id' => 2),
  4725. 'Comment' => array(
  4726. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  4727. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  4728. )
  4729. ));
  4730. $this->assertFalse(empty($result));
  4731. $result = $TestModel->findById(2);
  4732. $expected = array(
  4733. 'First Comment for Second Article',
  4734. 'Second Comment for Second Article',
  4735. 'First new comment',
  4736. 'Second new comment'
  4737. );
  4738. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4739. $result = $TestModel->saveAssociated(
  4740. array(
  4741. 'Article' => array('id' => 2),
  4742. 'Comment' => array(
  4743. array(
  4744. 'comment' => 'Third new comment',
  4745. 'published' => 'Y',
  4746. 'user_id' => 1
  4747. ))),
  4748. array('atomic' => false)
  4749. );
  4750. $this->assertFalse(empty($result));
  4751. $result = $TestModel->findById(2);
  4752. $expected = array(
  4753. 'First Comment for Second Article',
  4754. 'Second Comment for Second Article',
  4755. 'First new comment',
  4756. 'Second new comment',
  4757. 'Third new comment'
  4758. );
  4759. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4760. $TestModel->beforeSaveReturn = false;
  4761. $result = $TestModel->saveAssociated(
  4762. array(
  4763. 'Article' => array('id' => 2),
  4764. 'Comment' => array(
  4765. array(
  4766. 'comment' => 'Fourth new comment',
  4767. 'published' => 'Y',
  4768. 'user_id' => 1
  4769. ))),
  4770. array('atomic' => false)
  4771. );
  4772. $this->assertEquals(array('Article' => false), $result);
  4773. $result = $TestModel->findById(2);
  4774. $expected = array(
  4775. 'First Comment for Second Article',
  4776. 'Second Comment for Second Article',
  4777. 'First new comment',
  4778. 'Second new comment',
  4779. 'Third new comment'
  4780. );
  4781. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4782. }
  4783. /**
  4784. * testSaveAssociatedHasManyEmpty method
  4785. *
  4786. * @return void
  4787. */
  4788. public function testSaveAssociatedHasManyEmpty() {
  4789. $this->loadFixtures('Article', 'Comment');
  4790. $TestModel = new Article();
  4791. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4792. $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  4793. //empty hasMany data is ignored in save
  4794. $result = $TestModel->saveAssociated(array(
  4795. 'Article' => array('title' => 'title', 'user_id' => 1),
  4796. 'Comment' => array()
  4797. ), array('validate' => true));
  4798. $this->assertTrue($result);
  4799. $result = $TestModel->saveAssociated(array(
  4800. 'Article' => array('title' => 'title', 'user_id' => 1),
  4801. 'Comment' => array()
  4802. ), array('validate' => true, 'atomic' => false));
  4803. $this->assertEquals(array('Article' => true), $result);
  4804. //empty primary data is not ignored
  4805. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
  4806. $this->assertFalse($result);
  4807. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
  4808. $this->assertEquals(array('Article' => false), $result);
  4809. }
  4810. /**
  4811. * testSaveAssociatedHasManyValidation method
  4812. *
  4813. * @return void
  4814. */
  4815. public function testSaveAssociatedHasManyValidation() {
  4816. $this->loadFixtures('Article', 'Comment');
  4817. $TestModel = new Article();
  4818. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4819. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4820. $result = $TestModel->saveAssociated(array(
  4821. 'Article' => array('id' => 2),
  4822. 'Comment' => array(
  4823. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  4824. )
  4825. ), array('validate' => true));
  4826. $this->assertFalse($result);
  4827. $expected = array('Comment' => array(
  4828. array('comment' => array('This field cannot be left blank'))
  4829. ));
  4830. $this->assertEquals($expected, $TestModel->validationErrors);
  4831. $expected = array(
  4832. array('comment' => array('This field cannot be left blank'))
  4833. );
  4834. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4835. $result = $TestModel->saveAssociated(array(
  4836. 'Article' => array('id' => 2),
  4837. 'Comment' => array(
  4838. array(
  4839. 'comment' => '',
  4840. 'published' => 'Y',
  4841. 'user_id' => 1
  4842. ))
  4843. ), array('validate' => 'first'));
  4844. $this->assertFalse($result);
  4845. }
  4846. /**
  4847. * test saveMany with transactions and ensure there is no missing rollback.
  4848. *
  4849. * @return void
  4850. */
  4851. public function testSaveManyTransactionNoRollback() {
  4852. $this->loadFixtures('Post');
  4853. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  4854. $db = ConnectionManager::create('mock_many_transaction', array(
  4855. 'datasource' => 'MockManyTransactionDboSource',
  4856. ));
  4857. $db->expects($this->once())
  4858. ->method('describe')
  4859. ->will($this->returnValue(array()));
  4860. $db->expects($this->once())->method('rollback');
  4861. $Post = new Post('mock_many_transaction');
  4862. $Post->validate = array(
  4863. 'title' => array('rule' => array('notEmpty'))
  4864. );
  4865. $data = array(
  4866. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4867. array('author_id' => 1, 'title' => '')
  4868. );
  4869. $Post->saveMany($data);
  4870. }
  4871. /**
  4872. * test saveAssociated with transactions and ensure there is no missing rollback.
  4873. *
  4874. * @return void
  4875. */
  4876. public function testSaveAssociatedTransactionNoRollback() {
  4877. $testDb = ConnectionManager::getDataSource('test');
  4878. $mock = $this->getMock(
  4879. 'DboSource',
  4880. array('connect', 'rollback', 'describe', 'create', 'begin'),
  4881. array(),
  4882. 'MockAssociatedTransactionDboSource',
  4883. false
  4884. );
  4885. $db = ConnectionManager::create('mock_assoc_transaction', array(
  4886. 'datasource' => 'MockAssociatedTransactionDboSource',
  4887. ));
  4888. $this->mockObjects[] = $db;
  4889. $db->columns = $testDb->columns;
  4890. $db->expects($this->once())->method('rollback');
  4891. $db->expects($this->any())->method('describe')
  4892. ->will($this->returnValue(array(
  4893. 'id' => array('type' => 'integer', 'length' => 11),
  4894. 'title' => array('type' => 'string'),
  4895. 'body' => array('type' => 'text'),
  4896. 'published' => array('type' => 'string')
  4897. )));
  4898. $Post = new Post();
  4899. $Post->useDbConfig = 'mock_assoc_transaction';
  4900. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  4901. $Post->Author->validate = array(
  4902. 'user' => array('rule' => array('notEmpty'))
  4903. );
  4904. $data = array(
  4905. 'Post' => array(
  4906. 'title' => 'New post',
  4907. 'body' => 'Content',
  4908. 'published' => 'Y'
  4909. ),
  4910. 'Author' => array(
  4911. 'user' => '',
  4912. 'password' => "sekret"
  4913. )
  4914. );
  4915. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  4916. }
  4917. /**
  4918. * test saveMany with nested saveMany call.
  4919. *
  4920. * @return void
  4921. */
  4922. public function testSaveManyNestedSaveMany() {
  4923. $this->loadFixtures('Sample');
  4924. $TransactionManyTestModel = new TransactionManyTestModel();
  4925. $data = array(
  4926. array('apple_id' => 1, 'name' => 'sample5'),
  4927. );
  4928. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  4929. }
  4930. /**
  4931. * testSaveManyTransaction method
  4932. *
  4933. * @return void
  4934. */
  4935. public function testSaveManyTransaction() {
  4936. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4937. $TestModel = new Post();
  4938. $TestModel->validate = array('title' => 'notEmpty');
  4939. $data = array(
  4940. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4941. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4942. array('author_id' => 1, 'title' => '')
  4943. );
  4944. $this->assertFalse($TestModel->saveMany($data));
  4945. $result = $TestModel->find('all', array('recursive' => -1));
  4946. $expected = array(
  4947. array('Post' => array(
  4948. 'id' => '1',
  4949. 'author_id' => 1,
  4950. 'title' => 'First Post',
  4951. 'body' => 'First Post Body',
  4952. 'published' => 'Y',
  4953. 'created' => '2007-03-18 10:39:23',
  4954. 'updated' => '2007-03-18 10:41:31'
  4955. )),
  4956. array('Post' => array(
  4957. 'id' => '2',
  4958. 'author_id' => 3,
  4959. 'title' => 'Second Post',
  4960. 'body' => 'Second Post Body',
  4961. 'published' => 'Y',
  4962. 'created' => '2007-03-18 10:41:23',
  4963. 'updated' => '2007-03-18 10:43:31'
  4964. )),
  4965. array('Post' => array(
  4966. 'id' => '3',
  4967. 'author_id' => 1,
  4968. 'title' => 'Third Post',
  4969. 'body' => 'Third Post Body',
  4970. 'published' => 'Y',
  4971. 'created' => '2007-03-18 10:43:23',
  4972. 'updated' => '2007-03-18 10:45:31'
  4973. )));
  4974. if (count($result) != 3) {
  4975. // Database doesn't support transactions
  4976. $expected[] = array(
  4977. 'Post' => array(
  4978. 'id' => '4',
  4979. 'author_id' => 1,
  4980. 'title' => 'New Fourth Post',
  4981. 'body' => null,
  4982. 'published' => 'N'
  4983. ));
  4984. $expected[] = array(
  4985. 'Post' => array(
  4986. 'id' => '5',
  4987. 'author_id' => 1,
  4988. 'title' => 'New Fifth Post',
  4989. 'body' => null,
  4990. 'published' => 'N',
  4991. ));
  4992. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4993. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4994. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  4995. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  4996. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4997. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4998. $this->assertEquals($expected, $result);
  4999. // Skip the rest of the transactional tests
  5000. return;
  5001. }
  5002. $this->assertEquals($expected, $result);
  5003. $data = array(
  5004. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5005. array('author_id' => 1, 'title' => ''),
  5006. array('author_id' => 1, 'title' => 'New Sixth Post')
  5007. );
  5008. $this->assertFalse($TestModel->saveMany($data));
  5009. $result = $TestModel->find('all', array('recursive' => -1));
  5010. $expected = array(
  5011. array('Post' => array(
  5012. 'id' => '1',
  5013. 'author_id' => 1,
  5014. 'title' => 'First Post',
  5015. 'body' => 'First Post Body',
  5016. 'published' => 'Y',
  5017. 'created' => '2007-03-18 10:39:23',
  5018. 'updated' => '2007-03-18 10:41:31'
  5019. )),
  5020. array('Post' => array(
  5021. 'id' => '2',
  5022. 'author_id' => 3,
  5023. 'title' => 'Second Post',
  5024. 'body' => 'Second Post Body',
  5025. 'published' => 'Y',
  5026. 'created' => '2007-03-18 10:41:23',
  5027. 'updated' => '2007-03-18 10:43:31'
  5028. )),
  5029. array('Post' => array(
  5030. 'id' => '3',
  5031. 'author_id' => 1,
  5032. 'title' => 'Third Post',
  5033. 'body' => 'Third Post Body',
  5034. 'published' => 'Y',
  5035. 'created' => '2007-03-18 10:43:23',
  5036. 'updated' => '2007-03-18 10:45:31'
  5037. )));
  5038. if (count($result) != 3) {
  5039. // Database doesn't support transactions
  5040. $expected[] = array(
  5041. 'Post' => array(
  5042. 'id' => '4',
  5043. 'author_id' => 1,
  5044. 'title' => 'New Fourth Post',
  5045. 'body' => 'Third Post Body',
  5046. 'published' => 'N'
  5047. ));
  5048. $expected[] = array(
  5049. 'Post' => array(
  5050. 'id' => '5',
  5051. 'author_id' => 1,
  5052. 'title' => 'Third Post',
  5053. 'body' => 'Third Post Body',
  5054. 'published' => 'N'
  5055. ));
  5056. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5057. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5058. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5059. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5060. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5061. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5062. }
  5063. $this->assertEquals($expected, $result);
  5064. $TestModel->validate = array('title' => 'notEmpty');
  5065. $data = array(
  5066. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5067. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5068. array('author_id' => 1, 'title' => 'New Sixth Post')
  5069. );
  5070. $this->assertTrue($TestModel->saveMany($data));
  5071. $result = $TestModel->find('all', array(
  5072. 'recursive' => -1,
  5073. 'fields' => array('author_id', 'title','body','published'),
  5074. 'order' => array('Post.created' => 'ASC')
  5075. ));
  5076. $expected = array(
  5077. array('Post' => array(
  5078. 'author_id' => 1,
  5079. 'title' => 'First Post',
  5080. 'body' => 'First Post Body',
  5081. 'published' => 'Y'
  5082. )),
  5083. array('Post' => array(
  5084. 'author_id' => 3,
  5085. 'title' => 'Second Post',
  5086. 'body' => 'Second Post Body',
  5087. 'published' => 'Y'
  5088. )),
  5089. array('Post' => array(
  5090. 'author_id' => 1,
  5091. 'title' => 'Third Post',
  5092. 'body' => 'Third Post Body',
  5093. 'published' => 'Y'
  5094. )),
  5095. array('Post' => array(
  5096. 'author_id' => 1,
  5097. 'title' => 'New Fourth Post',
  5098. 'body' => '',
  5099. 'published' => 'N'
  5100. )),
  5101. array('Post' => array(
  5102. 'author_id' => 1,
  5103. 'title' => 'New Fifth Post',
  5104. 'body' => '',
  5105. 'published' => 'N'
  5106. )),
  5107. array('Post' => array(
  5108. 'author_id' => 1,
  5109. 'title' => 'New Sixth Post',
  5110. 'body' => '',
  5111. 'published' => 'N'
  5112. )));
  5113. $this->assertEquals($expected, $result);
  5114. }
  5115. /**
  5116. * testSaveManyValidation method
  5117. *
  5118. * @return void
  5119. */
  5120. public function testSaveManyValidation() {
  5121. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5122. $TestModel = new Post();
  5123. $data = array(
  5124. array(
  5125. 'id' => '1',
  5126. 'title' => 'Baleeted First Post',
  5127. 'body' => 'Baleeted!',
  5128. 'published' => 'N'
  5129. ),
  5130. array(
  5131. 'id' => '2',
  5132. 'title' => 'Just update the title'
  5133. ),
  5134. array(
  5135. 'title' => 'Creating a fourth post',
  5136. 'body' => 'Fourth post body',
  5137. 'author_id' => 2
  5138. ));
  5139. $this->assertTrue($TestModel->saveMany($data));
  5140. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5141. $expected = array(
  5142. array(
  5143. 'Post' => array(
  5144. 'id' => '1',
  5145. 'author_id' => '1',
  5146. 'title' => 'Baleeted First Post',
  5147. 'body' => 'Baleeted!',
  5148. 'published' => 'N',
  5149. 'created' => '2007-03-18 10:39:23'
  5150. )
  5151. ),
  5152. array(
  5153. 'Post' => array(
  5154. 'id' => '2',
  5155. 'author_id' => '3',
  5156. 'title' => 'Just update the title',
  5157. 'body' => 'Second Post Body',
  5158. 'published' => 'Y',
  5159. 'created' => '2007-03-18 10:41:23'
  5160. )
  5161. ),
  5162. array(
  5163. 'Post' => array(
  5164. 'id' => '3',
  5165. 'author_id' => '1',
  5166. 'title' => 'Third Post',
  5167. 'body' => 'Third Post Body',
  5168. 'published' => 'Y',
  5169. 'created' => '2007-03-18 10:43:23',
  5170. 'updated' => '2007-03-18 10:45:31'
  5171. )),
  5172. array(
  5173. 'Post' => array(
  5174. 'id' => '4',
  5175. 'author_id' => '2',
  5176. 'title' => 'Creating a fourth post',
  5177. 'body' => 'Fourth post body',
  5178. 'published' => 'N'
  5179. )
  5180. )
  5181. );
  5182. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  5183. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  5184. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5185. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5186. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  5187. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5188. $this->assertEquals($expected, $result);
  5189. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5190. $data = array(
  5191. array(
  5192. 'id' => '1',
  5193. 'title' => 'Un-Baleeted First Post',
  5194. 'body' => 'Not Baleeted!',
  5195. 'published' => 'Y'
  5196. ),
  5197. array(
  5198. 'id' => '2',
  5199. 'title' => '',
  5200. 'body' => 'Trying to get away with an empty title'
  5201. ));
  5202. $result = $TestModel->saveMany($data);
  5203. $this->assertFalse($result);
  5204. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5205. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5206. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  5207. if (!$transactionWorked) {
  5208. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  5209. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  5210. }
  5211. $this->assertEquals($errors, $TestModel->validationErrors);
  5212. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5213. $data = array(
  5214. array(
  5215. 'id' => '1',
  5216. 'title' => 'Un-Baleeted First Post',
  5217. 'body' => 'Not Baleeted!',
  5218. 'published' => 'Y'
  5219. ),
  5220. array(
  5221. 'id' => '2',
  5222. 'title' => '',
  5223. 'body' => 'Trying to get away with an empty title'
  5224. ));
  5225. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  5226. $this->assertEquals(array(true, false), $result);
  5227. $result = $TestModel->find('all', array(
  5228. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5229. 'recursive' => -1,
  5230. 'order' => 'Post.id ASC'
  5231. ));
  5232. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5233. $expected = array(
  5234. array(
  5235. 'Post' => array(
  5236. 'id' => '1',
  5237. 'author_id' => '1',
  5238. 'title' => 'Un-Baleeted First Post',
  5239. 'body' => 'Not Baleeted!',
  5240. 'published' => 'Y',
  5241. )),
  5242. array(
  5243. 'Post' => array(
  5244. 'id' => '2',
  5245. 'author_id' => '3',
  5246. 'title' => 'Just update the title',
  5247. 'body' => 'Second Post Body',
  5248. 'published' => 'Y',
  5249. )),
  5250. array(
  5251. 'Post' => array(
  5252. 'id' => '3',
  5253. 'author_id' => '1',
  5254. 'title' => 'Third Post',
  5255. 'body' => 'Third Post Body',
  5256. 'published' => 'Y',
  5257. )),
  5258. array(
  5259. 'Post' => array(
  5260. 'id' => '4',
  5261. 'author_id' => '2',
  5262. 'title' => 'Creating a fourth post',
  5263. 'body' => 'Fourth post body',
  5264. 'published' => 'N',
  5265. )));
  5266. $this->assertEquals($expected, $result);
  5267. $this->assertEquals($errors, $TestModel->validationErrors);
  5268. $data = array(
  5269. array(
  5270. 'id' => '1',
  5271. 'title' => 'Re-Baleeted First Post',
  5272. 'body' => 'Baleeted!',
  5273. 'published' => 'N'
  5274. ),
  5275. array(
  5276. 'id' => '2',
  5277. 'title' => '',
  5278. 'body' => 'Trying to get away with an empty title'
  5279. ));
  5280. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  5281. $result = $TestModel->find('all', array(
  5282. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5283. 'recursive' => -1,
  5284. 'order' => 'Post.id ASC'
  5285. ));
  5286. $this->assertEquals($expected, $result);
  5287. $this->assertEquals($errors, $TestModel->validationErrors);
  5288. }
  5289. /**
  5290. * testValidateMany method
  5291. *
  5292. * @return void
  5293. */
  5294. public function testValidateMany() {
  5295. $TestModel = new Article();
  5296. $TestModel->validate = array('title' => 'notEmpty');
  5297. $data = array(
  5298. 0 => array('title' => ''),
  5299. 1 => array('title' => 'title 1'),
  5300. 2 => array('title' => 'title 2'),
  5301. );
  5302. $result = $TestModel->validateMany($data);
  5303. $this->assertFalse($result);
  5304. $expected = array(
  5305. 0 => array('title' => array('This field cannot be left blank')),
  5306. );
  5307. $this->assertEquals($expected, $TestModel->validationErrors);
  5308. $data = array(
  5309. 0 => array('title' => 'title 0'),
  5310. 1 => array('title' => ''),
  5311. 2 => array('title' => 'title 2'),
  5312. );
  5313. $result = $TestModel->validateMany($data);
  5314. $this->assertFalse($result);
  5315. $expected = array(
  5316. 1 => array('title' => array('This field cannot be left blank')),
  5317. );
  5318. $this->assertEquals($expected, $TestModel->validationErrors);
  5319. }
  5320. /**
  5321. * testSaveAssociatedValidateFirst method
  5322. *
  5323. * @return void
  5324. */
  5325. public function testSaveAssociatedValidateFirst() {
  5326. $this->loadFixtures('Article', 'Comment', 'Attachment');
  5327. $model = new Article();
  5328. $model->deleteAll(true);
  5329. $model->Comment->validate = array('comment' => 'notEmpty');
  5330. $result = $model->saveAssociated(array(
  5331. 'Article' => array(
  5332. 'title' => 'Post with Author',
  5333. 'body' => 'This post will be saved author'
  5334. ),
  5335. 'Comment' => array(
  5336. array('comment' => 'First new comment'),
  5337. array('comment' => '')
  5338. )
  5339. ), array('validate' => 'first'));
  5340. $this->assertFalse($result);
  5341. $result = $model->find('all');
  5342. $this->assertEquals(array(), $result);
  5343. $expected = array('Comment' => array(
  5344. 1 => array('comment' => array('This field cannot be left blank'))
  5345. ));
  5346. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  5347. $this->assertSame($model->Comment->find('count'), 0);
  5348. $result = $model->saveAssociated(
  5349. array(
  5350. 'Article' => array(
  5351. 'title' => 'Post with Author',
  5352. 'body' => 'This post will be saved with an author',
  5353. 'user_id' => 2
  5354. ),
  5355. 'Comment' => array(
  5356. array(
  5357. 'comment' => 'Only new comment',
  5358. 'user_id' => 2
  5359. ))),
  5360. array('validate' => 'first')
  5361. );
  5362. $this->assertSame($result, true);
  5363. $result = $model->Comment->find('all');
  5364. $this->assertSame(count($result), 1);
  5365. $result = Hash::extract($result, '{n}.Comment.article_id');
  5366. $this->assertEquals(4, $result[0]);
  5367. $model->deleteAll(true);
  5368. $data = array(
  5369. 'Article' => array(
  5370. 'title' => 'Post with Author saveAlled from comment',
  5371. 'body' => 'This post will be saved with an author',
  5372. 'user_id' => 2
  5373. ),
  5374. 'Comment' => array(
  5375. 'comment' => 'Only new comment', 'user_id' => 2
  5376. ));
  5377. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  5378. $this->assertFalse(empty($result));
  5379. $result = $model->find('all');
  5380. $this->assertEquals(
  5381. 'Post with Author saveAlled from comment',
  5382. $result[0]['Article']['title']
  5383. );
  5384. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  5385. }
  5386. /**
  5387. * test saveMany()'s return is correct when using atomic = false and validate = first.
  5388. *
  5389. * @return void
  5390. */
  5391. public function testSaveManyValidateFirstAtomicFalse() {
  5392. $Something = new Something();
  5393. $invalidData = array(
  5394. array(
  5395. 'title' => 'foo',
  5396. 'body' => 'bar',
  5397. 'published' => 'baz',
  5398. ),
  5399. array(
  5400. 'body' => 3,
  5401. 'published' => 'sd',
  5402. ),
  5403. );
  5404. $Something->create();
  5405. $Something->validate = array(
  5406. 'title' => array(
  5407. 'rule' => 'alphaNumeric',
  5408. 'required' => true,
  5409. ),
  5410. 'body' => array(
  5411. 'rule' => 'alphaNumeric',
  5412. 'required' => true,
  5413. 'allowEmpty' => true,
  5414. ),
  5415. );
  5416. $result = $Something->saveMany($invalidData, array(
  5417. 'atomic' => false,
  5418. 'validate' => 'first',
  5419. ));
  5420. $expected = array(true, false);
  5421. $this->assertEquals($expected, $result);
  5422. $Something = new Something();
  5423. $validData = array(
  5424. array(
  5425. 'title' => 'title value',
  5426. 'body' => 'body value',
  5427. 'published' => 'baz',
  5428. ),
  5429. array(
  5430. 'title' => 'valid',
  5431. 'body' => 'this body',
  5432. 'published' => 'sd',
  5433. ),
  5434. );
  5435. $Something->create();
  5436. $result = $Something->saveMany($validData, array(
  5437. 'atomic' => false,
  5438. 'validate' => 'first',
  5439. ));
  5440. $expected = array(true, true);
  5441. $this->assertEquals($expected, $result);
  5442. }
  5443. /**
  5444. * testValidateAssociated method
  5445. *
  5446. * @return void
  5447. */
  5448. public function testValidateAssociated() {
  5449. $this->loadFixtures('Attachment', 'Article', 'Comment');
  5450. $TestModel = new Comment();
  5451. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5452. $data = array(
  5453. 'Comment' => array(
  5454. 'comment' => 'This is the comment'
  5455. ),
  5456. 'Attachment' => array(
  5457. 'attachment' => ''
  5458. )
  5459. );
  5460. $result = $TestModel->validateAssociated($data);
  5461. $this->assertFalse($result);
  5462. $TestModel = new Article();
  5463. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5464. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5465. $data = array(
  5466. 'Article' => array('id' => 2),
  5467. 'Comment' => array(
  5468. array(
  5469. 'id' => 1,
  5470. 'comment' => '',
  5471. 'published' => 'Y',
  5472. 'user_id' => 1),
  5473. array(
  5474. 'id' => 2,
  5475. 'comment' =>
  5476. 'comment',
  5477. 'published' => 'Y',
  5478. 'user_id' => 1
  5479. )));
  5480. $result = $TestModel->validateAssociated($data);
  5481. $this->assertFalse($result);
  5482. $data = array(
  5483. 'Article' => array('id' => 2),
  5484. 'Comment' => array(
  5485. array(
  5486. 'id' => 1,
  5487. 'comment' => '',
  5488. 'published' => 'Y',
  5489. 'user_id' => 1
  5490. ),
  5491. array(
  5492. 'id' => 2,
  5493. 'comment' => 'comment',
  5494. 'published' => 'Y',
  5495. 'user_id' => 1
  5496. ),
  5497. array(
  5498. 'id' => 3,
  5499. 'comment' => '',
  5500. 'published' => 'Y',
  5501. 'user_id' => 1
  5502. )));
  5503. $result = $TestModel->validateAssociated($data, array(
  5504. 'atomic' => false
  5505. ));
  5506. $expected = array(
  5507. 'Article' => true,
  5508. 'Comment' => array(false, true, false)
  5509. );
  5510. $this->assertSame($expected, $result);
  5511. $expected = array('Comment' => array(
  5512. 0 => array('comment' => array('This field cannot be left blank')),
  5513. 2 => array('comment' => array('This field cannot be left blank'))
  5514. ));
  5515. $this->assertEquals($expected, $TestModel->validationErrors);
  5516. $expected = array(
  5517. 0 => array('comment' => array('This field cannot be left blank')),
  5518. 2 => array('comment' => array('This field cannot be left blank'))
  5519. );
  5520. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5521. }
  5522. /**
  5523. * test that saveMany behaves like plain save() when suplied empty data
  5524. *
  5525. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5526. * @return void
  5527. */
  5528. public function testSaveManyEmptyData() {
  5529. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5530. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5531. $model = new Article();
  5532. $result = $model->saveMany(array(), array('validate' => true));
  5533. $this->assertFalse(empty($result));
  5534. $model = new ProductUpdateAll();
  5535. $result = $model->saveMany(array());
  5536. $this->assertFalse($result);
  5537. }
  5538. /**
  5539. * test that saveAssociated behaves like plain save() when supplied empty data
  5540. *
  5541. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5542. * @return void
  5543. */
  5544. public function testSaveAssociatedEmptyData() {
  5545. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5546. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5547. $model = new Article();
  5548. $result = $model->saveAssociated(array(), array('validate' => true));
  5549. $this->assertFalse(empty($result));
  5550. $model = new ProductUpdateAll();
  5551. $result = $model->saveAssociated(array());
  5552. $this->assertFalse($result);
  5553. }
  5554. /**
  5555. * testUpdateWithCalculation method
  5556. *
  5557. * @return void
  5558. */
  5559. public function testUpdateWithCalculation() {
  5560. $this->loadFixtures('DataTest');
  5561. $model = new DataTest();
  5562. $model->deleteAll(true);
  5563. $result = $model->saveMany(array(
  5564. array('count' => 5, 'float' => 1.1),
  5565. array('count' => 3, 'float' => 1.2),
  5566. array('count' => 4, 'float' => 1.3),
  5567. array('count' => 1, 'float' => 2.0),
  5568. ));
  5569. $this->assertFalse(empty($result));
  5570. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5571. $this->assertEquals(array(5, 3, 4, 1), $result);
  5572. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  5573. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5574. $this->assertEquals(array(7, 5, 6, 3), $result);
  5575. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  5576. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5577. $this->assertEquals(array(6, 4, 5, 2), $result);
  5578. }
  5579. public function testToggleBoolFields() {
  5580. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  5581. $Post = new CounterCachePost();
  5582. $Post->unbindModel(array('belongsTo' => array('User')), true);
  5583. $true = array('Post' => array('published' => true, 'id' => 2));
  5584. $false = array('Post' => array('published' => false, 'id' => 2));
  5585. $fields = array('Post.published', 'Post.id');
  5586. $updateConditions = array('Post.id' => 2);
  5587. // check its true
  5588. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5589. $this->assertEquals($true, $result);
  5590. // Testing without the alias
  5591. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5592. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5593. $this->assertEquals($false, $result);
  5594. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5595. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5596. $this->assertEquals($true, $result);
  5597. $db = ConnectionManager::getDataSource('test');
  5598. $alias = $db->name('Post.published');
  5599. // Testing with the alias
  5600. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5601. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5602. $this->assertEquals($false, $result);
  5603. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5604. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5605. $this->assertEquals($true, $result);
  5606. }
  5607. /**
  5608. * TestFindAllWithoutForeignKey
  5609. *
  5610. * @return void
  5611. */
  5612. public function testFindAllForeignKey() {
  5613. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5614. $ProductUpdateAll = new ProductUpdateAll();
  5615. $conditions = array('Group.name' => 'group one');
  5616. $ProductUpdateAll->bindModel(array(
  5617. 'belongsTo' => array(
  5618. 'Group' => array('className' => 'GroupUpdateAll')
  5619. )
  5620. ));
  5621. $ProductUpdateAll->belongsTo = array(
  5622. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  5623. );
  5624. $results = $ProductUpdateAll->find('all', compact('conditions'));
  5625. $this->assertTrue(!empty($results));
  5626. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  5627. $ProductUpdateAll->belongsTo = array(
  5628. 'Group' => array(
  5629. 'className' => 'GroupUpdateAll',
  5630. 'foreignKey' => false,
  5631. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5632. ));
  5633. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  5634. $this->assertTrue(!empty($resultsFkFalse));
  5635. $expected = array(
  5636. '0' => array(
  5637. 'ProductUpdateAll' => array(
  5638. 'id' => 1,
  5639. 'name' => 'product one',
  5640. 'groupcode' => 120,
  5641. 'group_id' => 1),
  5642. 'Group' => array(
  5643. 'id' => 1,
  5644. 'name' => 'group one',
  5645. 'code' => 120)
  5646. ),
  5647. '1' => array(
  5648. 'ProductUpdateAll' => array(
  5649. 'id' => 2,
  5650. 'name' => 'product two',
  5651. 'groupcode' => 120,
  5652. 'group_id' => 1),
  5653. 'Group' => array(
  5654. 'id' => 1,
  5655. 'name' => 'group one',
  5656. 'code' => 120)
  5657. )
  5658. );
  5659. $this->assertEquals($expected, $results);
  5660. $this->assertEquals($expected, $resultsFkFalse);
  5661. }
  5662. /**
  5663. * test updateAll with empty values.
  5664. *
  5665. * @return void
  5666. */
  5667. public function testUpdateAllEmptyValues() {
  5668. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  5669. $this->loadFixtures('Author', 'Post');
  5670. $model = new Author();
  5671. $result = $model->updateAll(array('user' => '""'));
  5672. $this->assertTrue($result);
  5673. }
  5674. /**
  5675. * testUpdateAllWithJoins
  5676. *
  5677. * @return void
  5678. */
  5679. public function testUpdateAllWithJoins() {
  5680. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  5681. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5682. $ProductUpdateAll = new ProductUpdateAll();
  5683. $conditions = array('Group.name' => 'group one');
  5684. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5685. 'Group' => array('className' => 'GroupUpdateAll')))
  5686. );
  5687. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5688. $results = $ProductUpdateAll->find('all', array(
  5689. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  5690. ));
  5691. $expected = array(
  5692. '0' => array(
  5693. 'ProductUpdateAll' => array(
  5694. 'id' => 1,
  5695. 'name' => 'new product',
  5696. 'groupcode' => 120,
  5697. 'group_id' => 1),
  5698. 'Group' => array(
  5699. 'id' => 1,
  5700. 'name' => 'group one',
  5701. 'code' => 120)
  5702. ),
  5703. '1' => array(
  5704. 'ProductUpdateAll' => array(
  5705. 'id' => 2,
  5706. 'name' => 'new product',
  5707. 'groupcode' => 120,
  5708. 'group_id' => 1),
  5709. 'Group' => array(
  5710. 'id' => 1,
  5711. 'name' => 'group one',
  5712. 'code' => 120)));
  5713. $this->assertEquals($expected, $results);
  5714. }
  5715. /**
  5716. * testUpdateAllWithoutForeignKey
  5717. *
  5718. * @return void
  5719. */
  5720. public function testUpdateAllWithoutForeignKey() {
  5721. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  5722. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5723. $ProductUpdateAll = new ProductUpdateAll();
  5724. $conditions = array('Group.name' => 'group one');
  5725. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5726. 'Group' => array('className' => 'GroupUpdateAll')
  5727. )));
  5728. $ProductUpdateAll->belongsTo = array(
  5729. 'Group' => array(
  5730. 'className' => 'GroupUpdateAll',
  5731. 'foreignKey' => false,
  5732. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5733. )
  5734. );
  5735. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5736. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  5737. $expected = array(
  5738. '0' => array(
  5739. 'ProductUpdateAll' => array(
  5740. 'id' => 1,
  5741. 'name' => 'new product',
  5742. 'groupcode' => 120,
  5743. 'group_id' => 1),
  5744. 'Group' => array(
  5745. 'id' => 1,
  5746. 'name' => 'group one',
  5747. 'code' => 120)
  5748. ),
  5749. '1' => array(
  5750. 'ProductUpdateAll' => array(
  5751. 'id' => 2,
  5752. 'name' => 'new product',
  5753. 'groupcode' => 120,
  5754. 'group_id' => 1),
  5755. 'Group' => array(
  5756. 'id' => 1,
  5757. 'name' => 'group one',
  5758. 'code' => 120)));
  5759. $this->assertEquals($expected, $resultsFkFalse);
  5760. }
  5761. /**
  5762. * test writing floats in german locale.
  5763. *
  5764. * @return void
  5765. */
  5766. public function testWriteFloatAsGerman() {
  5767. $restore = setlocale(LC_NUMERIC, 0);
  5768. setlocale(LC_NUMERIC, 'de_DE');
  5769. $model = new DataTest();
  5770. $result = $model->save(array(
  5771. 'count' => 1,
  5772. 'float' => 3.14593
  5773. ));
  5774. $this->assertTrue((bool)$result);
  5775. setlocale(LC_NUMERIC, $restore);
  5776. }
  5777. /**
  5778. * Test returned array contains primary key when save creates a new record
  5779. *
  5780. * @return void
  5781. */
  5782. public function testPkInReturnArrayForCreate() {
  5783. $this->loadFixtures('Article');
  5784. $TestModel = new Article();
  5785. $data = array('Article' => array(
  5786. 'user_id' => '1',
  5787. 'title' => 'Fourth Article',
  5788. 'body' => 'Fourth Article Body',
  5789. 'published' => 'Y'
  5790. ));
  5791. $result = $TestModel->save($data);
  5792. $this->assertSame($result['Article']['id'], $TestModel->id);
  5793. }
  5794. /**
  5795. * testSaveAllFieldListValidateBelongsTo
  5796. *
  5797. * @return void
  5798. */
  5799. public function testSaveAllFieldListValidateBelongsTo() {
  5800. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5801. $TestModel = new Post();
  5802. $result = $TestModel->find('all');
  5803. $this->assertEquals(3, count($result));
  5804. $this->assertFalse(isset($result[3]));
  5805. // test belongsTo
  5806. $fieldList = array(
  5807. 'Post' => array('title', 'author_id'),
  5808. 'Author' => array('user')
  5809. );
  5810. $TestModel->saveAll(array(
  5811. 'Post' => array(
  5812. 'title' => 'Post without body',
  5813. 'body' => 'This will not be saved',
  5814. ),
  5815. 'Author' => array(
  5816. 'user' => 'bob',
  5817. 'test' => 'This will not be saved',
  5818. )), array('fieldList' => $fieldList));
  5819. $result = $TestModel->find('all');
  5820. $expected = array(
  5821. 'Post' => array (
  5822. 'id' => '4',
  5823. 'author_id' => '5',
  5824. 'title' => 'Post without body',
  5825. 'body' => null,
  5826. 'published' => 'N',
  5827. 'created' => self::date(),
  5828. 'updated' => self::date(),
  5829. ),
  5830. 'Author' => array (
  5831. 'id' => '5',
  5832. 'user' => 'bob',
  5833. 'password' => null,
  5834. 'created' => self::date(),
  5835. 'updated' => self::date(),
  5836. 'test' => 'working',
  5837. ),
  5838. );
  5839. $this->assertEquals($expected, $result[3]);
  5840. $this->assertEquals(4, count($result));
  5841. $this->assertEquals('', $result[3]['Post']['body']);
  5842. $this->assertEquals('working', $result[3]['Author']['test']);
  5843. // test multirecord
  5844. $this->db->truncate($TestModel);
  5845. $fieldList = array('title', 'author_id');
  5846. $TestModel->saveAll(array(
  5847. array(
  5848. 'title' => 'Multi-record post 1',
  5849. 'body' => 'First multi-record post',
  5850. 'author_id' => 2
  5851. ),
  5852. array(
  5853. 'title' => 'Multi-record post 2',
  5854. 'body' => 'Second multi-record post',
  5855. 'author_id' => 2
  5856. )), array('fieldList' => $fieldList));
  5857. $result = $TestModel->find('all', array(
  5858. 'recursive' => -1,
  5859. 'order' => 'Post.id ASC'
  5860. ));
  5861. $expected = array(
  5862. array(
  5863. 'Post' => array(
  5864. 'id' => '1',
  5865. 'author_id' => '2',
  5866. 'title' => 'Multi-record post 1',
  5867. 'body' => '',
  5868. 'published' => 'N',
  5869. 'created' => self::date(),
  5870. 'updated' => self::date()
  5871. )
  5872. ),
  5873. array(
  5874. 'Post' => array(
  5875. 'id' => '2',
  5876. 'author_id' => '2',
  5877. 'title' => 'Multi-record post 2',
  5878. 'body' => '',
  5879. 'published' => 'N',
  5880. 'created' => self::date(),
  5881. 'updated' => self::date()
  5882. )
  5883. )
  5884. );
  5885. $this->assertEquals($expected, $result);
  5886. }
  5887. /**
  5888. * testSaveAllFieldListHasMany method
  5889. *
  5890. * return @void
  5891. */
  5892. public function testSaveAllFieldListHasMany() {
  5893. $this->loadFixtures('Article', 'Comment');
  5894. $TestModel = new Article();
  5895. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5896. $this->db->truncate($TestModel);
  5897. $this->db->truncate(new Comment());
  5898. $fieldList = array(
  5899. 'Article' => array('id'),
  5900. 'Comment' => array('article_id', 'user_id')
  5901. );
  5902. $result = $TestModel->saveAll(array(
  5903. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  5904. 'Comment' => array(
  5905. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5906. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  5907. )
  5908. ), array('fieldList' => $fieldList));
  5909. $result = $TestModel->find('all');
  5910. $this->assertEquals('', $result[0]['Article']['title']);
  5911. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  5912. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  5913. }
  5914. /**
  5915. * testSaveAllFieldListHasOne method
  5916. *
  5917. * @return void
  5918. */
  5919. public function testSaveAllFieldListHasOne() {
  5920. $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
  5921. $TestModel = new Comment();
  5922. $TestModel->validate = array('comment' => 'notEmpty');
  5923. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5924. $record = array(
  5925. 'Comment' => array(
  5926. 'user_id' => 1,
  5927. 'article_id' => 1,
  5928. 'comment' => '',
  5929. ),
  5930. 'Attachment' => array(
  5931. 'attachment' => ''
  5932. )
  5933. );
  5934. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  5935. $this->assertFalse($result);
  5936. $fieldList = array(
  5937. 'Comment' => array('id', 'article_id', 'user_id'),
  5938. 'Attachment' => array('comment_id')
  5939. );
  5940. $result = $TestModel->saveAll($record, array(
  5941. 'fieldList' => $fieldList, 'validate' => 'only'
  5942. ));
  5943. $this->assertTrue($result);
  5944. $this->assertEmpty($TestModel->validationErrors);
  5945. }
  5946. /**
  5947. * testSaveAllDeepFieldListValidateBelongsTo
  5948. *
  5949. * @return void
  5950. */
  5951. public function testSaveAllDeepFieldListValidateBelongsTo() {
  5952. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  5953. $TestModel = new Post();
  5954. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  5955. $TestModel->recursive = 2;
  5956. $result = $TestModel->find('all');
  5957. $this->assertEquals(3, count($result));
  5958. $this->assertFalse(isset($result[3]));
  5959. // test belongsTo
  5960. $fieldList = array(
  5961. 'Post' => array('title', 'author_id'),
  5962. 'Author' => array('user'),
  5963. 'Comment' => array('comment')
  5964. );
  5965. $TestModel->saveAll(array(
  5966. 'Post' => array(
  5967. 'title' => 'Post without body',
  5968. 'body' => 'This will not be saved',
  5969. ),
  5970. 'Author' => array(
  5971. 'user' => 'bob',
  5972. 'test' => 'This will not be saved',
  5973. 'Comment' => array(
  5974. array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
  5975. )), array('fieldList' => $fieldList, 'deep' => true));
  5976. $result = $TestModel->Author->Comment->find('first', array(
  5977. 'conditions' => array('Comment.id' => 5),
  5978. 'fields' => array('comment', 'published')
  5979. ));
  5980. $expected = array(
  5981. 'Comment' => array(
  5982. 'comment' => 'I am still published',
  5983. 'published' => 'Y'
  5984. )
  5985. );
  5986. $this->assertEquals($expected, $result);
  5987. }
  5988. /**
  5989. * testSaveAllDeepFieldListHasMany method
  5990. *
  5991. * return @void
  5992. */
  5993. public function testSaveAllDeepFieldListHasMany() {
  5994. $this->loadFixtures('Article', 'Comment', 'User');
  5995. $TestModel = new Article();
  5996. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5997. $this->db->truncate($TestModel);
  5998. $this->db->truncate(new Comment());
  5999. $fieldList = array(
  6000. 'Article' => array('id'),
  6001. 'Comment' => array('article_id', 'user_id'),
  6002. 'User' => array('user')
  6003. );
  6004. $result = $TestModel->saveAll(array(
  6005. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  6006. 'Comment' => array(
  6007. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6008. array(
  6009. 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
  6010. 'User' => array('user' => 'nopassword', 'password' => 'not saved')
  6011. )
  6012. )
  6013. ), array('fieldList' => $fieldList, 'deep' => true));
  6014. $result = $TestModel->Comment->User->find('first', array(
  6015. 'conditions' => array('User.user' => 'nopassword'),
  6016. 'fields' => array('user', 'password')
  6017. ));
  6018. $expected = array(
  6019. 'User' => array(
  6020. 'user' => 'nopassword',
  6021. 'password' => ''
  6022. )
  6023. );
  6024. $this->assertEquals($expected, $result);
  6025. }
  6026. /**
  6027. * testSaveAllDeepHasManyBelongsTo method
  6028. *
  6029. * return @void
  6030. */
  6031. public function testSaveAllDeepHasManyBelongsTo() {
  6032. $this->loadFixtures('Article', 'Comment', 'User');
  6033. $TestModel = new Article();
  6034. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6035. $this->db->truncate($TestModel);
  6036. $this->db->truncate(new Comment());
  6037. $result = $TestModel->saveAll(array(
  6038. 'Article' => array('id' => 2, 'title' => 'The title'),
  6039. 'Comment' => array(
  6040. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6041. array(
  6042. 'comment' => 'belongsto', 'published' => 'Y',
  6043. 'User' => array('user' => 'findme', 'password' => 'somepass')
  6044. )
  6045. )
  6046. ), array('deep' => true));
  6047. $result = $TestModel->Comment->User->find('first', array(
  6048. 'conditions' => array('User.user' => 'findme'),
  6049. 'fields' => array('id', 'user', 'password')
  6050. ));
  6051. $expected = array(
  6052. 'User' => array(
  6053. 'id' => 5,
  6054. 'user' => 'findme',
  6055. 'password' => 'somepass',
  6056. )
  6057. );
  6058. $this->assertEquals($expected, $result);
  6059. $result = $TestModel->Comment->find('first', array(
  6060. 'conditions' => array('Comment.user_id' => 5),
  6061. 'fields' => array('id', 'comment', 'published', 'user_id')
  6062. ));
  6063. $expected = array(
  6064. 'Comment' => array(
  6065. 'id' => 2,
  6066. 'comment' => 'belongsto',
  6067. 'published' => 'Y',
  6068. 'user_id' => 5
  6069. )
  6070. );
  6071. $this->assertEquals($expected, $result);
  6072. }
  6073. /**
  6074. * testSaveAllDeepHasManyhasMany method
  6075. *
  6076. * return @void
  6077. */
  6078. public function testSaveAllDeepHasManyHasMany() {
  6079. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6080. $TestModel = new Article();
  6081. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6082. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6083. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6084. $this->db->truncate($TestModel);
  6085. $this->db->truncate(new Comment());
  6086. $this->db->truncate(new Attachment());
  6087. $result = $TestModel->saveAll(array(
  6088. 'Article' => array('id' => 2, 'title' => 'The title'),
  6089. 'Comment' => array(
  6090. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6091. array(
  6092. 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
  6093. 'Attachment' => array(
  6094. array('attachment' => 'first deep attachment'),
  6095. array('attachment' => 'second deep attachment'),
  6096. )
  6097. )
  6098. )
  6099. ), array('deep' => true));
  6100. $result = $TestModel->Comment->find('first', array(
  6101. 'conditions' => array('Comment.comment' => 'hasmany'),
  6102. 'fields' => array('id', 'comment', 'published', 'user_id'),
  6103. 'recursive' => -1
  6104. ));
  6105. $expected = array(
  6106. 'Comment' => array(
  6107. 'id' => 2,
  6108. 'comment' => 'hasmany',
  6109. 'published' => 'Y',
  6110. 'user_id' => 5
  6111. )
  6112. );
  6113. $this->assertEquals($expected, $result);
  6114. $result = $TestModel->Comment->Attachment->find('all', array(
  6115. 'fields' => array('attachment', 'comment_id'),
  6116. 'order' => array('Attachment.id' => 'ASC')
  6117. ));
  6118. $expected = array(
  6119. array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
  6120. array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
  6121. );
  6122. $this->assertEquals($expected, $result);
  6123. }
  6124. /**
  6125. * testSaveAllDeepOrderHasManyHasMany method
  6126. *
  6127. * return @void
  6128. */
  6129. public function testSaveAllDeepOrderHasManyHasMany() {
  6130. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6131. $TestModel = new Article();
  6132. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6133. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6134. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6135. $this->db->truncate($TestModel);
  6136. $this->db->truncate(new Comment());
  6137. $this->db->truncate(new Attachment());
  6138. $result = $TestModel->saveAll(array(
  6139. 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
  6140. 'Comment' => array(
  6141. array(
  6142. 'Attachment' => array(
  6143. array('attachment' => 'attachment should be created with comment_id'),
  6144. array('attachment' => 'comment should be created with article_id'),
  6145. ),
  6146. 'comment' => 'after associated data',
  6147. 'user_id' => 1
  6148. )
  6149. )
  6150. ), array('deep' => true));
  6151. $result = $TestModel->Comment->find('first', array(
  6152. 'conditions' => array('Comment.article_id' => 2),
  6153. ));
  6154. $this->assertEquals(2, $result['Comment']['article_id']);
  6155. $this->assertEquals(2, count($result['Attachment']));
  6156. }
  6157. /**
  6158. * testSaveAllDeepEmptyHasManyHasMany method
  6159. *
  6160. * return @void
  6161. */
  6162. public function testSaveAllDeepEmptyHasManyHasMany() {
  6163. $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
  6164. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6165. $TestModel = new Article();
  6166. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6167. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6168. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6169. $this->db->truncate($TestModel);
  6170. $this->db->truncate(new Comment());
  6171. $this->db->truncate(new Attachment());
  6172. $result = $TestModel->saveAll(array(
  6173. 'Article' => array('id' => 3, 'title' => 'Comment has no data'),
  6174. 'Comment' => array(
  6175. array(
  6176. 'Attachment' => array(
  6177. array('attachment' => 'attachment should be created with comment_id'),
  6178. array('attachment' => 'comment should be created with article_id'),
  6179. ),
  6180. )
  6181. )
  6182. ), array('deep' => true));
  6183. $result = $TestModel->Comment->find('first', array(
  6184. 'conditions' => array('Comment.article_id' => 3),
  6185. ));
  6186. $this->assertEquals(3, $result['Comment']['article_id']);
  6187. $this->assertEquals(2, count($result['Attachment']));
  6188. }
  6189. /**
  6190. * testUpdateAllBoolean
  6191. *
  6192. * return @void
  6193. */
  6194. public function testUpdateAllBoolean() {
  6195. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6196. $TestModel = new Item();
  6197. $result = $TestModel->updateAll(array('published' => true));
  6198. $this->assertTrue($result);
  6199. $result = $TestModel->find('first', array('fields' => array('id', 'published')));
  6200. $this->assertEquals(true, $result['Item']['published']);
  6201. }
  6202. /**
  6203. * testUpdateAllBooleanConditions
  6204. *
  6205. * return @void
  6206. */
  6207. public function testUpdateAllBooleanConditions() {
  6208. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6209. $TestModel = new Item();
  6210. $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
  6211. $this->assertTrue($result);
  6212. $result = $TestModel->find('first', array(
  6213. 'fields' => array('id', 'published'),
  6214. 'conditions' => array('Item.id' => 1)));
  6215. $this->assertEquals(true, $result['Item']['published']);
  6216. }
  6217. /**
  6218. * testUpdateBoolean
  6219. *
  6220. * return @void
  6221. */
  6222. public function testUpdateBoolean() {
  6223. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6224. $TestModel = new Item();
  6225. $result = $TestModel->save(array('published' => true, 'id' => 1));
  6226. $this->assertTrue((boolean)$result);
  6227. $result = $TestModel->find('first', array(
  6228. 'fields' => array('id', 'published'),
  6229. 'conditions' => array('Item.id' => 1)));
  6230. $this->assertEquals(true, $result['Item']['published']);
  6231. }
  6232. }