PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/floresj/notetime
PHP | 6874 lines | 5610 code | 645 blank | 619 comment | 27 complexity | fee70b9771afc91eec47c5496d3a6be3 MD5 | raw file
  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. $this->assertEquals($data['Tag'], $result['Tag']);
  1043. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1044. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1045. $expected = array(
  1046. 'Article' => array(
  1047. 'id' => '2',
  1048. 'user_id' => '3',
  1049. 'title' => 'New Second Article',
  1050. 'body' => 'Second Article Body'
  1051. ),
  1052. 'Tag' => array(
  1053. array(
  1054. 'id' => '1',
  1055. 'tag' => 'tag1',
  1056. 'created' => '2007-03-18 12:22:23',
  1057. 'updated' => '2007-03-18 12:24:31'
  1058. ),
  1059. array(
  1060. 'id' => '2',
  1061. 'tag' => 'tag2',
  1062. 'created' => '2007-03-18 12:24:23',
  1063. 'updated' => '2007-03-18 12:26:31'
  1064. )));
  1065. $this->assertEquals($expected, $result);
  1066. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1067. $result = $TestModel->set($data);
  1068. $this->assertFalse(empty($result));
  1069. $result = $TestModel->save();
  1070. $this->assertFalse(empty($result));
  1071. $TestModel->unbindModel(array(
  1072. 'belongsTo' => array('User'),
  1073. 'hasMany' => array('Comment')
  1074. ));
  1075. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1076. $expected = array(
  1077. 'Article' => array(
  1078. 'id' => '2',
  1079. 'user_id' => '3',
  1080. 'title' => 'New Second Article',
  1081. 'body' => 'Second Article Body'
  1082. ),
  1083. 'Tag' => array(
  1084. array(
  1085. 'id' => '2',
  1086. 'tag' => 'tag2',
  1087. 'created' => '2007-03-18 12:24:23',
  1088. 'updated' => '2007-03-18 12:26:31'
  1089. ),
  1090. array(
  1091. 'id' => '3',
  1092. 'tag' => 'tag3',
  1093. 'created' => '2007-03-18 12:26:23',
  1094. 'updated' => '2007-03-18 12:28:31'
  1095. )));
  1096. $this->assertEquals($expected, $result);
  1097. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1098. $result = $TestModel->set($data);
  1099. $this->assertFalse(empty($result));
  1100. $result = $TestModel->save();
  1101. $this->assertFalse(empty($result));
  1102. $TestModel->unbindModel(array(
  1103. 'belongsTo' => array('User'),
  1104. 'hasMany' => array('Comment')
  1105. ));
  1106. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1107. $expected = array(
  1108. 'Article' => array(
  1109. 'id' => '2',
  1110. 'user_id' => '3',
  1111. 'title' => 'New Second Article',
  1112. 'body' => 'Second Article Body'
  1113. ),
  1114. 'Tag' => array(
  1115. array(
  1116. 'id' => '1',
  1117. 'tag' => 'tag1',
  1118. 'created' => '2007-03-18 12:22:23',
  1119. 'updated' => '2007-03-18 12:24:31'
  1120. ),
  1121. array(
  1122. 'id' => '2',
  1123. 'tag' => 'tag2',
  1124. 'created' => '2007-03-18 12:24:23',
  1125. 'updated' => '2007-03-18 12:26:31'
  1126. ),
  1127. array(
  1128. 'id' => '3',
  1129. 'tag' => 'tag3',
  1130. 'created' => '2007-03-18 12:26:23',
  1131. 'updated' => '2007-03-18 12:28:31'
  1132. )));
  1133. $this->assertEquals($expected, $result);
  1134. $data = array('Tag' => array('Tag' => array()));
  1135. $result = $TestModel->set($data);
  1136. $this->assertFalse(empty($result));
  1137. $result = $TestModel->save();
  1138. $this->assertFalse(empty($result));
  1139. $data = array('Tag' => array('Tag' => ''));
  1140. $result = $TestModel->set($data);
  1141. $this->assertFalse(empty($result));
  1142. $result = $TestModel->save();
  1143. $this->assertFalse(empty($result));
  1144. $TestModel->unbindModel(array(
  1145. 'belongsTo' => array('User'),
  1146. 'hasMany' => array('Comment')
  1147. ));
  1148. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1149. $expected = array(
  1150. 'Article' => array(
  1151. 'id' => '2',
  1152. 'user_id' => '3',
  1153. 'title' => 'New Second Article',
  1154. 'body' => 'Second Article Body'
  1155. ),
  1156. 'Tag' => array()
  1157. );
  1158. $this->assertEquals($expected, $result);
  1159. $data = array('Tag' => array('Tag' => array(2, 3)));
  1160. $result = $TestModel->set($data);
  1161. $this->assertFalse(empty($result));
  1162. $result = $TestModel->save();
  1163. $this->assertFalse(empty($result));
  1164. $TestModel->unbindModel(array(
  1165. 'belongsTo' => array('User'),
  1166. 'hasMany' => array('Comment')
  1167. ));
  1168. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1169. $expected = array(
  1170. 'Article' => array(
  1171. 'id' => '2',
  1172. 'user_id' => '3',
  1173. 'title' => 'New Second Article',
  1174. 'body' => 'Second Article Body'
  1175. ),
  1176. 'Tag' => array(
  1177. array(
  1178. 'id' => '2',
  1179. 'tag' => 'tag2',
  1180. 'created' => '2007-03-18 12:24:23',
  1181. 'updated' => '2007-03-18 12:26:31'
  1182. ),
  1183. array(
  1184. 'id' => '3',
  1185. 'tag' => 'tag3',
  1186. 'created' => '2007-03-18 12:26:23',
  1187. 'updated' => '2007-03-18 12:28:31'
  1188. )));
  1189. $this->assertEquals($expected, $result);
  1190. $data = array(
  1191. 'Tag' => array(
  1192. 'Tag' => array(1, 2)
  1193. ),
  1194. 'Article' => array(
  1195. 'id' => '2',
  1196. 'title' => 'New Second Article'
  1197. ));
  1198. $result = $TestModel->set($data);
  1199. $this->assertFalse(empty($result));
  1200. $result = $TestModel->save();
  1201. $this->assertFalse(empty($result));
  1202. $TestModel->unbindModel(array(
  1203. 'belongsTo' => array('User'),
  1204. 'hasMany' => array('Comment')
  1205. ));
  1206. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1207. $expected = array(
  1208. 'Article' => array(
  1209. 'id' => '2',
  1210. 'user_id' => '3',
  1211. 'title' => 'New Second Article',
  1212. 'body' => 'Second Article Body'
  1213. ),
  1214. 'Tag' => array(
  1215. array(
  1216. 'id' => '1',
  1217. 'tag' => 'tag1',
  1218. 'created' => '2007-03-18 12:22:23',
  1219. 'updated' => '2007-03-18 12:24:31'
  1220. ),
  1221. array(
  1222. 'id' => '2',
  1223. 'tag' => 'tag2',
  1224. 'created' => '2007-03-18 12:24:23',
  1225. 'updated' => '2007-03-18 12:26:31'
  1226. )));
  1227. $this->assertEquals($expected, $result);
  1228. $data = array(
  1229. 'Tag' => array(
  1230. 'Tag' => array(1, 2)
  1231. ),
  1232. 'Article' => array(
  1233. 'id' => '2',
  1234. 'title' => 'New Second Article Title'
  1235. ));
  1236. $result = $TestModel->set($data);
  1237. $this->assertFalse(empty($result));
  1238. $result = $TestModel->save();
  1239. $this->assertFalse(empty($result));
  1240. $TestModel->unbindModel(array(
  1241. 'belongsTo' => array('User'),
  1242. 'hasMany' => array('Comment')
  1243. ));
  1244. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1245. $expected = array(
  1246. 'Article' => array(
  1247. 'id' => '2',
  1248. 'user_id' => '3',
  1249. 'title' => 'New Second Article Title',
  1250. 'body' => 'Second Article Body'
  1251. ),
  1252. 'Tag' => array(
  1253. array(
  1254. 'id' => '1',
  1255. 'tag' => 'tag1',
  1256. 'created' => '2007-03-18 12:22:23',
  1257. 'updated' => '2007-03-18 12:24:31'
  1258. ),
  1259. array(
  1260. 'id' => '2',
  1261. 'tag' => 'tag2',
  1262. 'created' => '2007-03-18 12:24:23',
  1263. 'updated' => '2007-03-18 12:26:31'
  1264. )
  1265. )
  1266. );
  1267. $this->assertEquals($expected, $result);
  1268. $data = array(
  1269. 'Tag' => array(
  1270. 'Tag' => array(2, 3)
  1271. ),
  1272. 'Article' => array(
  1273. 'id' => '2',
  1274. 'title' => 'Changed Second Article'
  1275. ));
  1276. $result = $TestModel->set($data);
  1277. $this->assertFalse(empty($result));
  1278. $result = $TestModel->save();
  1279. $this->assertFalse(empty($result));
  1280. $TestModel->unbindModel(array(
  1281. 'belongsTo' => array('User'),
  1282. 'hasMany' => array('Comment')
  1283. ));
  1284. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1285. $expected = array(
  1286. 'Article' => array(
  1287. 'id' => '2',
  1288. 'user_id' => '3',
  1289. 'title' => 'Changed Second Article',
  1290. 'body' => 'Second Article Body'
  1291. ),
  1292. 'Tag' => array(
  1293. array(
  1294. 'id' => '2',
  1295. 'tag' => 'tag2',
  1296. 'created' => '2007-03-18 12:24:23',
  1297. 'updated' => '2007-03-18 12:26:31'
  1298. ),
  1299. array(
  1300. 'id' => '3',
  1301. 'tag' => 'tag3',
  1302. 'created' => '2007-03-18 12:26:23',
  1303. 'updated' => '2007-03-18 12:28:31'
  1304. )
  1305. )
  1306. );
  1307. $this->assertEquals($expected, $result);
  1308. $data = array(
  1309. 'Tag' => array(
  1310. 'Tag' => array(1, 3)
  1311. ),
  1312. 'Article' => array('id' => '2'),
  1313. );
  1314. $result = $TestModel->set($data);
  1315. $this->assertFalse(empty($result));
  1316. $result = $TestModel->save();
  1317. $this->assertFalse(empty($result));
  1318. $TestModel->unbindModel(array(
  1319. 'belongsTo' => array('User'),
  1320. 'hasMany' => array('Comment')
  1321. ));
  1322. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1323. $expected = array(
  1324. 'Article' => array(
  1325. 'id' => '2',
  1326. 'user_id' => '3',
  1327. 'title' => 'Changed Second Article',
  1328. 'body' => 'Second Article Body'
  1329. ),
  1330. 'Tag' => array(
  1331. array(
  1332. 'id' => '1',
  1333. 'tag' => 'tag1',
  1334. 'created' => '2007-03-18 12:22:23',
  1335. 'updated' => '2007-03-18 12:24:31'
  1336. ),
  1337. array(
  1338. 'id' => '3',
  1339. 'tag' => 'tag3',
  1340. 'created' => '2007-03-18 12:26:23',
  1341. 'updated' => '2007-03-18 12:28:31'
  1342. )));
  1343. $this->assertEquals($expected, $result);
  1344. $data = array(
  1345. 'Article' => array(
  1346. 'id' => 10,
  1347. 'user_id' => '2',
  1348. 'title' => 'New Article With Tags and fieldList',
  1349. 'body' => 'New Article Body with Tags and fieldList',
  1350. 'created' => '2007-03-18 14:55:23',
  1351. 'updated' => '2007-03-18 14:57:31'
  1352. ),
  1353. 'Tag' => array(
  1354. 'Tag' => array(1, 2, 3)
  1355. )
  1356. );
  1357. $result = $TestModel->create()
  1358. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1359. $this->assertFalse(empty($result));
  1360. $TestModel->unbindModel(array(
  1361. 'belongsTo' => array('User'),
  1362. 'hasMany' => array('Comment')
  1363. ));
  1364. $result = $TestModel->read();
  1365. $expected = array(
  1366. 'Article' => array(
  1367. 'id' => 4,
  1368. 'user_id' => 2,
  1369. 'title' => 'New Article With Tags and fieldList',
  1370. 'body' => '',
  1371. 'published' => 'N',
  1372. 'created' => '',
  1373. 'updated' => ''
  1374. ),
  1375. 'Tag' => array(
  1376. 0 => array(
  1377. 'id' => 1,
  1378. 'tag' => 'tag1',
  1379. 'created' => '2007-03-18 12:22:23',
  1380. 'updated' => '2007-03-18 12:24:31'
  1381. ),
  1382. 1 => array(
  1383. 'id' => 2,
  1384. 'tag' => 'tag2',
  1385. 'created' => '2007-03-18 12:24:23',
  1386. 'updated' => '2007-03-18 12:26:31'
  1387. ),
  1388. 2 => array(
  1389. 'id' => 3,
  1390. 'tag' => 'tag3',
  1391. 'created' => '2007-03-18 12:26:23',
  1392. 'updated' => '2007-03-18 12:28:31'
  1393. )));
  1394. $this->assertEquals($expected, $result);
  1395. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1396. $TestModel = new JoinA();
  1397. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1398. $data = array(
  1399. 'JoinA' => array(
  1400. 'id' => 1,
  1401. 'name' => 'Join A 1',
  1402. 'body' => 'Join A 1 Body',
  1403. ),
  1404. 'JoinC' => array(
  1405. 'JoinC' => array(
  1406. array('join_c_id' => 2, 'other' => 'new record'),
  1407. array('join_c_id' => 3, 'other' => 'new record')
  1408. )
  1409. )
  1410. );
  1411. $TestModel->save($data);
  1412. $result = $TestModel->read(null, 1);
  1413. $expected = array(4, 5);
  1414. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
  1415. $expected = array('new record', 'new record');
  1416. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
  1417. }
  1418. /**
  1419. * testSaveHabtmNoPrimaryData method
  1420. *
  1421. * @return void
  1422. */
  1423. public function testSaveHabtmNoPrimaryData() {
  1424. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1425. $TestModel = new Article();
  1426. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
  1427. $result = $TestModel->findById(2);
  1428. $expected = array(
  1429. 'Article' => array(
  1430. 'id' => '2',
  1431. 'user_id' => '3',
  1432. 'title' => 'Second Article',
  1433. 'body' => 'Second Article Body',
  1434. 'published' => 'Y',
  1435. 'created' => '2007-03-18 10:41:23',
  1436. 'updated' => '2007-03-18 10:43:31'
  1437. ),
  1438. 'Tag' => array(
  1439. array(
  1440. 'id' => '1',
  1441. 'tag' => 'tag1',
  1442. 'created' => '2007-03-18 12:22:23',
  1443. 'updated' => '2007-03-18 12:24:31'
  1444. ),
  1445. array(
  1446. 'id' => '3',
  1447. 'tag' => 'tag3',
  1448. 'created' => '2007-03-18 12:26:23',
  1449. 'updated' => '2007-03-18 12:28:31'
  1450. )
  1451. )
  1452. );
  1453. $this->assertEquals($expected, $result);
  1454. $TestModel->id = 2;
  1455. $data = array('Tag' => array('Tag' => array(2)));
  1456. $TestModel->save($data);
  1457. $result = $TestModel->findById(2);
  1458. $expected = array(
  1459. 'Article' => array(
  1460. 'id' => '2',
  1461. 'user_id' => '3',
  1462. 'title' => 'Second Article',
  1463. 'body' => 'Second Article Body',
  1464. 'published' => 'Y',
  1465. 'created' => '2007-03-18 10:41:23',
  1466. 'updated' => self::date()
  1467. ),
  1468. 'Tag' => array(
  1469. array(
  1470. 'id' => '2',
  1471. 'tag' => 'tag2',
  1472. 'created' => '2007-03-18 12:24:23',
  1473. 'updated' => '2007-03-18 12:26:31'
  1474. )
  1475. )
  1476. );
  1477. $this->assertEquals($expected, $result);
  1478. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1479. $TestModel = new Portfolio();
  1480. $result = $TestModel->findById(2);
  1481. $expected = array(
  1482. 'Portfolio' => array(
  1483. 'id' => 2,
  1484. 'seller_id' => 1,
  1485. 'name' => 'Portfolio 2'
  1486. ),
  1487. 'Item' => array(
  1488. array(
  1489. 'id' => 2,
  1490. 'syfile_id' => 2,
  1491. 'published' => '',
  1492. 'name' => 'Item 2',
  1493. 'ItemsPortfolio' => array(
  1494. 'id' => 2,
  1495. 'item_id' => 2,
  1496. 'portfolio_id' => 2
  1497. )
  1498. ),
  1499. array(
  1500. 'id' => 6,
  1501. 'syfile_id' => 6,
  1502. 'published' => '',
  1503. 'name' => 'Item 6',
  1504. 'ItemsPortfolio' => array(
  1505. 'id' => 6,
  1506. 'item_id' => 6,
  1507. 'portfolio_id' => 2
  1508. )
  1509. )
  1510. )
  1511. );
  1512. $this->assertEquals($expected, $result);
  1513. $data = array('Item' => array('Item' => array(1, 2)));
  1514. $TestModel->id = 2;
  1515. $TestModel->save($data);
  1516. $result = $TestModel->findById(2);
  1517. $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
  1518. $expected = array(
  1519. 'Portfolio' => array(
  1520. 'id' => 2,
  1521. 'seller_id' => 1,
  1522. 'name' => 'Portfolio 2'
  1523. ),
  1524. 'Item' => array(
  1525. array(
  1526. 'id' => 1,
  1527. 'syfile_id' => 1,
  1528. 'published' => '',
  1529. 'name' => 'Item 1',
  1530. 'ItemsPortfolio' => array(
  1531. 'id' => 7,
  1532. 'item_id' => 1,
  1533. 'portfolio_id' => 2
  1534. )
  1535. ),
  1536. array(
  1537. 'id' => 2,
  1538. 'syfile_id' => 2,
  1539. 'published' => '',
  1540. 'name' => 'Item 2',
  1541. 'ItemsPortfolio' => array(
  1542. 'id' => 8,
  1543. 'item_id' => 2,
  1544. 'portfolio_id' => 2
  1545. )
  1546. )
  1547. )
  1548. );
  1549. $this->assertEquals($expected, $result);
  1550. }
  1551. /**
  1552. * testSaveHabtmCustomKeys method
  1553. *
  1554. * @return void
  1555. */
  1556. public function testSaveHabtmCustomKeys() {
  1557. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1558. $Story = new Story();
  1559. $data = array(
  1560. 'Story' => array('story' => '1'),
  1561. 'Tag' => array(
  1562. 'Tag' => array(2, 3)
  1563. ));
  1564. $result = $Story->set($data);
  1565. $this->assertFalse(empty($result));
  1566. $result = $Story->save();
  1567. $this->assertFalse(empty($result));
  1568. $result = $Story->find('all', array('order' => array('Story.story')));
  1569. $expected = array(
  1570. array(
  1571. 'Story' => array(
  1572. 'story' => 1,
  1573. 'title' => 'First Story'
  1574. ),
  1575. 'Tag' => array(
  1576. array(
  1577. 'id' => 2,
  1578. 'tag' => 'tag2',
  1579. 'created' => '2007-03-18 12:24:23',
  1580. 'updated' => '2007-03-18 12:26:31'
  1581. ),
  1582. array(
  1583. 'id' => 3,
  1584. 'tag' => 'tag3',
  1585. 'created' => '2007-03-18 12:26:23',
  1586. 'updated' => '2007-03-18 12:28:31'
  1587. ))),
  1588. array(
  1589. 'Story' => array(
  1590. 'story' => 2,
  1591. 'title' => 'Second Story'
  1592. ),
  1593. 'Tag' => array()
  1594. ));
  1595. $this->assertEquals($expected, $result);
  1596. }
  1597. /**
  1598. * test that saving habtm records respects conditions set in the 'conditions' key
  1599. * for the association.
  1600. *
  1601. * @return void
  1602. */
  1603. public function testHabtmSaveWithConditionsInAssociation() {
  1604. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1605. $Something = new Something();
  1606. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1607. $Something->bindModel(array(
  1608. 'hasAndBelongsToMany' => array(
  1609. 'DoomedSomethingElse' => array(
  1610. 'className' => 'SomethingElse',
  1611. 'joinTable' => 'join_things',
  1612. 'conditions' => array('JoinThing.doomed' => true),
  1613. 'unique' => true
  1614. ),
  1615. 'NotDoomedSomethingElse' => array(
  1616. 'className' => 'SomethingElse',
  1617. 'joinTable' => 'join_things',
  1618. 'conditions' => array('JoinThing.doomed' => 0),
  1619. 'unique' => true
  1620. )
  1621. )
  1622. ), false);
  1623. $result = $Something->read(null, 1);
  1624. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1625. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1626. $data = array(
  1627. 'Something' => array('id' => 1),
  1628. 'NotDoomedSomethingElse' => array(
  1629. 'NotDoomedSomethingElse' => array(
  1630. array('something_else_id' => 2, 'doomed' => 0),
  1631. array('something_else_id' => 3, 'doomed' => 0)
  1632. )
  1633. )
  1634. );
  1635. $Something->create($data);
  1636. $result = $Something->save();
  1637. $this->assertFalse(empty($result));
  1638. $result = $Something->read(null, 1);
  1639. $this->assertEquals(2, count($result['NotDoomedSomethingElse']));
  1640. $this->assertEquals(1, count($result['DoomedSomethingElse']));
  1641. }
  1642. /**
  1643. * testHabtmSaveKeyResolution method
  1644. *
  1645. * @return void
  1646. */
  1647. public function testHabtmSaveKeyResolution() {
  1648. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1649. $ThePaper = new ThePaper();
  1650. $ThePaper->id = 1;
  1651. $ThePaper->save(array('Monkey' => array(2, 3)));
  1652. $result = $ThePaper->findById(1);
  1653. $expected = array(
  1654. array(
  1655. 'id' => '2',
  1656. 'device_type_id' => '1',
  1657. 'name' => 'Device 2',
  1658. 'typ' => '1'
  1659. ),
  1660. array(
  1661. 'id' => '3',
  1662. 'device_type_id' => '1',
  1663. 'name' => 'Device 3',
  1664. 'typ' => '2'
  1665. ));
  1666. $this->assertEquals($expected, $result['Monkey']);
  1667. $ThePaper->id = 2;
  1668. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1669. $result = $ThePaper->findById(2);
  1670. $expected = array(
  1671. array(
  1672. 'id' => '1',
  1673. 'device_type_id' => '1',
  1674. 'name' => 'Device 1',
  1675. 'typ' => '1'
  1676. ),
  1677. array(
  1678. 'id' => '2',
  1679. 'device_type_id' => '1',
  1680. 'name' => 'Device 2',
  1681. 'typ' => '1'
  1682. ),
  1683. array(
  1684. 'id' => '3',
  1685. 'device_type_id' => '1',
  1686. 'name' => 'Device 3',
  1687. 'typ' => '2'
  1688. ));
  1689. $this->assertEquals($expected, $result['Monkey']);
  1690. $ThePaper->id = 2;
  1691. $ThePaper->save(array('Monkey' => array(1, 3)));
  1692. $result = $ThePaper->findById(2);
  1693. $expected = array(
  1694. array(
  1695. 'id' => '1',
  1696. 'device_type_id' => '1',
  1697. 'name' => 'Device 1',
  1698. 'typ' => '1'
  1699. ),
  1700. array(
  1701. 'id' => '3',
  1702. 'device_type_id' => '1',
  1703. 'name' => 'Device 3',
  1704. 'typ' => '2'
  1705. ));
  1706. $this->assertEquals($expected, $result['Monkey']);
  1707. $result = $ThePaper->findById(1);
  1708. $expected = array(
  1709. array(
  1710. 'id' => '2',
  1711. 'device_type_id' => '1',
  1712. 'name' => 'Device 2',
  1713. 'typ' => '1'
  1714. ),
  1715. array(
  1716. 'id' => '3',
  1717. 'device_type_id' => '1',
  1718. 'name' => 'Device 3',
  1719. 'typ' => '2'
  1720. ));
  1721. $this->assertEquals($expected, $result['Monkey']);
  1722. }
  1723. /**
  1724. * testCreationOfEmptyRecord method
  1725. *
  1726. * @return void
  1727. */
  1728. public function testCreationOfEmptyRecord() {
  1729. $this->loadFixtures('Author');
  1730. $TestModel = new Author();
  1731. $this->assertEquals(4, $TestModel->find('count'));
  1732. $TestModel->deleteAll(true, false, false);
  1733. $this->assertEquals(0, $TestModel->find('count'));
  1734. $result = $TestModel->save();
  1735. $this->assertTrue(isset($result['Author']['created']));
  1736. $this->assertTrue(isset($result['Author']['updated']));
  1737. $this->assertEquals(1, $TestModel->find('count'));
  1738. }
  1739. /**
  1740. * testCreateWithPKFiltering method
  1741. *
  1742. * @return void
  1743. */
  1744. public function testCreateWithPKFiltering() {
  1745. $TestModel = new Article();
  1746. $data = array(
  1747. 'id' => 5,
  1748. 'user_id' => 2,
  1749. 'title' => 'My article',
  1750. 'body' => 'Some text'
  1751. );
  1752. $result = $TestModel->create($data);
  1753. $expected = array(
  1754. 'Article' => array(
  1755. 'published' => 'N',
  1756. 'id' => 5,
  1757. 'user_id' => 2,
  1758. 'title' => 'My article',
  1759. 'body' => 'Some text'
  1760. ));
  1761. $this->assertEquals($expected, $result);
  1762. $this->assertEquals(5, $TestModel->id);
  1763. $result = $TestModel->create($data, true);
  1764. $expected = array(
  1765. 'Article' => array(
  1766. 'published' => 'N',
  1767. 'id' => false,
  1768. 'user_id' => 2,
  1769. 'title' => 'My article',
  1770. 'body' => 'Some text'
  1771. ));
  1772. $this->assertEquals($expected, $result);
  1773. $this->assertFalse($TestModel->id);
  1774. $result = $TestModel->create(array('Article' => $data), true);
  1775. $expected = array(
  1776. 'Article' => array(
  1777. 'published' => 'N',
  1778. 'id' => false,
  1779. 'user_id' => 2,
  1780. 'title' => 'My article',
  1781. 'body' => 'Some text'
  1782. ));
  1783. $this->assertEquals($expected, $result);
  1784. $this->assertFalse($TestModel->id);
  1785. $data = array(
  1786. 'id' => 6,
  1787. 'user_id' => 2,
  1788. 'title' => 'My article',
  1789. 'body' => 'Some text',
  1790. 'created' => '1970-01-01 00:00:00',
  1791. 'updated' => '1970-01-01 12:00:00',
  1792. 'modified' => '1970-01-01 12:00:00'
  1793. );
  1794. $result = $TestModel->create($data);
  1795. $expected = array(
  1796. 'Article' => array(
  1797. 'published' => 'N',
  1798. 'id' => 6,
  1799. 'user_id' => 2,
  1800. 'title' => 'My article',
  1801. 'body' => 'Some text',
  1802. 'created' => '1970-01-01 00:00:00',
  1803. 'updated' => '1970-01-01 12:00:00',
  1804. 'modified' => '1970-01-01 12:00:00'
  1805. ));
  1806. $this->assertEquals($expected, $result);
  1807. $this->assertEquals(6, $TestModel->id);
  1808. $result = $TestModel->create(array(
  1809. 'Article' => array_diff_key($data, array(
  1810. 'created' => true,
  1811. 'updated' => true,
  1812. 'modified' => true
  1813. ))), true);
  1814. $expected = array(
  1815. 'Article' => array(
  1816. 'published' => 'N',
  1817. 'id' => false,
  1818. 'user_id' => 2,
  1819. 'title' => 'My article',
  1820. 'body' => 'Some text'
  1821. ));
  1822. $this->assertEquals($expected, $result);
  1823. $this->assertFalse($TestModel->id);
  1824. }
  1825. /**
  1826. * testCreationWithMultipleData method
  1827. *
  1828. * @return void
  1829. */
  1830. public function testCreationWithMultipleData() {
  1831. $this->loadFixtures('Article', 'Comment');
  1832. $Article = new Article();
  1833. $Comment = new Comment();
  1834. $articles = $Article->find('all', array(
  1835. 'fields' => array('id','title'),
  1836. 'recursive' => -1,
  1837. 'order' => array('Article.id' => 'ASC')
  1838. ));
  1839. $expected = array(
  1840. array('Article' => array(
  1841. 'id' => 1,
  1842. 'title' => 'First Article'
  1843. )),
  1844. array('Article' => array(
  1845. 'id' => 2,
  1846. 'title' => 'Second Article'
  1847. )),
  1848. array('Article' => array(
  1849. 'id' => 3,
  1850. 'title' => 'Third Article'
  1851. )));
  1852. $this->assertEquals($expected, $articles);
  1853. $comments = $Comment->find('all', array(
  1854. 'fields' => array('id','article_id','user_id','comment','published'),
  1855. 'recursive' => -1,
  1856. 'order' => array('Comment.id' => 'ASC')
  1857. ));
  1858. $expected = array(
  1859. array('Comment' => array(
  1860. 'id' => 1,
  1861. 'article_id' => 1,
  1862. 'user_id' => 2,
  1863. 'comment' => 'First Comment for First Article',
  1864. 'published' => 'Y'
  1865. )),
  1866. array('Comment' => array(
  1867. 'id' => 2,
  1868. 'article_id' => 1,
  1869. 'user_id' => 4,
  1870. 'comment' => 'Second Comment for First Article',
  1871. 'published' => 'Y'
  1872. )),
  1873. array('Comment' => array(
  1874. 'id' => 3,
  1875. 'article_id' => 1,
  1876. 'user_id' => 1,
  1877. 'comment' => 'Third Comment for First Article',
  1878. 'published' => 'Y'
  1879. )),
  1880. array('Comment' => array(
  1881. 'id' => 4,
  1882. 'article_id' => 1,
  1883. 'user_id' => 1,
  1884. 'comment' => 'Fourth Comment for First Article',
  1885. 'published' => 'N'
  1886. )),
  1887. array('Comment' => array(
  1888. 'id' => 5,
  1889. 'article_id' => 2,
  1890. 'user_id' => 1,
  1891. 'comment' => 'First Comment for Second Article',
  1892. 'published' => 'Y'
  1893. )),
  1894. array('Comment' => array(
  1895. 'id' => 6,
  1896. 'article_id' => 2,
  1897. 'user_id' => 2,
  1898. 'comment' => 'Second Comment for Second Article',
  1899. 'published' => 'Y'
  1900. )));
  1901. $this->assertEquals($expected, $comments);
  1902. $data = array(
  1903. 'Comment' => array(
  1904. 'article_id' => 2,
  1905. 'user_id' => 4,
  1906. 'comment' => 'Brand New Comment',
  1907. 'published' => 'N'
  1908. ),
  1909. 'Article' => array(
  1910. 'id' => 2,
  1911. 'title' => 'Second Article Modified'
  1912. ));
  1913. $result = $Comment->create($data);
  1914. $this->assertFalse(empty($result));
  1915. $result = $Comment->save();
  1916. $this->assertFalse(empty($result));
  1917. $articles = $Article->find('all', array(
  1918. 'fields' => array('id','title'),
  1919. 'recursive' => -1,
  1920. 'order' => array('Article.id' => 'ASC')
  1921. ));
  1922. $expected = array(
  1923. array('Article' => array(
  1924. 'id' => 1,
  1925. 'title' => 'First Article'
  1926. )),
  1927. array('Article' => array(
  1928. 'id' => 2,
  1929. 'title' => 'Second Article'
  1930. )),
  1931. array('Article' => array(
  1932. 'id' => 3,
  1933. 'title' => 'Third Article'
  1934. )));
  1935. $this->assertEquals($expected, $articles);
  1936. $comments = $Comment->find('all', array(
  1937. 'fields' => array('id','article_id','user_id','comment','published'),
  1938. 'recursive' => -1,
  1939. 'order' => array('Comment.id' => 'ASC')
  1940. ));
  1941. $expected = array(
  1942. array('Comment' => array(
  1943. 'id' => 1,
  1944. 'article_id' => 1,
  1945. 'user_id' => 2,
  1946. 'comment' => 'First Comment for First Article',
  1947. 'published' => 'Y'
  1948. )),
  1949. array('Comment' => array(
  1950. 'id' => 2,
  1951. 'article_id' => 1,
  1952. 'user_id' => 4,
  1953. 'comment' => 'Second Comment for First Article',
  1954. 'published' => 'Y'
  1955. )),
  1956. array('Comment' => array(
  1957. 'id' => 3,
  1958. 'article_id' => 1,
  1959. 'user_id' => 1,
  1960. 'comment' => 'Third Comment for First Article',
  1961. 'published' => 'Y'
  1962. )),
  1963. array('Comment' => array(
  1964. 'id' => 4,
  1965. 'article_id' => 1,
  1966. 'user_id' => 1,
  1967. 'comment' => 'Fourth Comment for First Article',
  1968. 'published' => 'N'
  1969. )),
  1970. array('Comment' => array(
  1971. 'id' => 5,
  1972. 'article_id' => 2,
  1973. 'user_id' => 1,
  1974. 'comment' => 'First Comment for Second Article',
  1975. 'published' => 'Y'
  1976. )),
  1977. array('Comment' => array(
  1978. 'id' => 6,
  1979. 'article_id' => 2,
  1980. 'user_id' => 2, 'comment' =>
  1981. 'Second Comment for Second Article',
  1982. 'published' => 'Y'
  1983. )),
  1984. array('Comment' => array(
  1985. 'id' => 7,
  1986. 'article_id' => 2,
  1987. 'user_id' => 4,
  1988. 'comment' => 'Brand New Comment',
  1989. 'published' => 'N'
  1990. )));
  1991. $this->assertEquals($expected, $comments);
  1992. }
  1993. /**
  1994. * testCreationWithMultipleDataSameModel method
  1995. *
  1996. * @return void
  1997. */
  1998. public function testCreationWithMultipleDataSameModel() {
  1999. $this->loadFixtures('Article');
  2000. $Article = new Article();
  2001. $SecondaryArticle = new Article();
  2002. $result = $Article->field('title', array('id' => 1));
  2003. $this->assertEquals('First Article', $result);
  2004. $data = array(
  2005. 'Article' => array(
  2006. 'user_id' => 2,
  2007. 'title' => 'Brand New Article',
  2008. 'body' => 'Brand New Article Body',
  2009. 'published' => 'Y'
  2010. ),
  2011. 'SecondaryArticle' => array(
  2012. 'id' => 1
  2013. ));
  2014. $Article->create();
  2015. $result = $Article->save($data);
  2016. $this->assertFalse(empty($result));
  2017. $result = $Article->getInsertID();
  2018. $this->assertTrue(!empty($result));
  2019. $result = $Article->field('title', array('id' => 1));
  2020. $this->assertEquals('First Article', $result);
  2021. $articles = $Article->find('all', array(
  2022. 'fields' => array('id','title'),
  2023. 'recursive' => -1,
  2024. 'order' => array('Article.id' => 'ASC')
  2025. ));
  2026. $expected = array(
  2027. array('Article' => array(
  2028. 'id' => 1,
  2029. 'title' => 'First Article'
  2030. )),
  2031. array('Article' => array(
  2032. 'id' => 2,
  2033. 'title' => 'Second Article'
  2034. )),
  2035. array('Article' => array(
  2036. 'id' => 3,
  2037. 'title' => 'Third Article'
  2038. )),
  2039. array('Article' => array(
  2040. 'id' => 4,
  2041. 'title' => 'Brand New Article'
  2042. )));
  2043. $this->assertEquals($expected, $articles);
  2044. }
  2045. /**
  2046. * testCreationWithMultipleDataSameModelManualInstances method
  2047. *
  2048. * @return void
  2049. */
  2050. public function testCreationWithMultipleDataSameModelManualInstances() {
  2051. $this->loadFixtures('PrimaryModel');
  2052. $Primary = new PrimaryModel();
  2053. $Secondary = new PrimaryModel();
  2054. $result = $Primary->field('primary_name', array('id' => 1));
  2055. $this->assertEquals('Primary Name Existing', $result);
  2056. $data = array(
  2057. 'PrimaryModel' => array(
  2058. 'primary_name' => 'Primary Name New'
  2059. ),
  2060. 'SecondaryModel' => array(
  2061. 'id' => array(1)
  2062. ));
  2063. $Primary->create();
  2064. $result = $Primary->save($data);
  2065. $this->assertFalse(empty($result));
  2066. $result = $Primary->field('primary_name', array('id' => 1));
  2067. $this->assertEquals('Primary Name Existing', $result);
  2068. $result = $Primary->getInsertID();
  2069. $this->assertTrue(!empty($result));
  2070. $result = $Primary->field('primary_name', array('id' => $result));
  2071. $this->assertEquals('Primary Name New', $result);
  2072. $result = $Primary->find('count');
  2073. $this->assertEquals(2, $result);
  2074. }
  2075. /**
  2076. * testRecordExists method
  2077. *
  2078. * @return void
  2079. */
  2080. public function testRecordExists() {
  2081. $this->loadFixtures('User');
  2082. $TestModel = new User();
  2083. $this->assertFalse($TestModel->exists());
  2084. $TestModel->read(null, 1);
  2085. $this->assertTrue($TestModel->exists());
  2086. $TestModel->create();
  2087. $this->assertFalse($TestModel->exists());
  2088. $TestModel->id = 4;
  2089. $this->assertTrue($TestModel->exists());
  2090. $TestModel = new TheVoid();
  2091. $this->assertFalse($TestModel->exists());
  2092. }
  2093. /**
  2094. * testRecordExistsMissingTable method
  2095. *
  2096. * @expectedException PDOException
  2097. * @return void
  2098. */
  2099. public function testRecordExistsMissingTable() {
  2100. $TestModel = new TheVoid();
  2101. $TestModel->id = 5;
  2102. $TestModel->exists();
  2103. }
  2104. /**
  2105. * testUpdateExisting method
  2106. *
  2107. * @return void
  2108. */
  2109. public function testUpdateExisting() {
  2110. $this->loadFixtures('User', 'Article', 'Comment');
  2111. $TestModel = new User();
  2112. $TestModel->create();
  2113. $TestModel->save(array(
  2114. 'User' => array(
  2115. 'user' => 'some user',
  2116. 'password' => 'some password'
  2117. )));
  2118. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  2119. $id = $TestModel->id;
  2120. $TestModel->save(array(
  2121. 'User' => array(
  2122. 'user' => 'updated user'
  2123. )));
  2124. $this->assertEquals($id, $TestModel->id);
  2125. $result = $TestModel->findById($id);
  2126. $this->assertEquals('updated user', $result['User']['user']);
  2127. $this->assertEquals('some password', $result['User']['password']);
  2128. $Article = new Article();
  2129. $Comment = new Comment();
  2130. $data = array(
  2131. 'Comment' => array(
  2132. 'id' => 1,
  2133. 'comment' => 'First Comment for First Article'
  2134. ),
  2135. 'Article' => array(
  2136. 'id' => 2,
  2137. 'title' => 'Second Article'
  2138. ));
  2139. $result = $Article->save($data);
  2140. $this->assertFalse(empty($result));
  2141. $result = $Comment->save($data);
  2142. $this->assertFalse(empty($result));
  2143. }
  2144. /**
  2145. * test updating records and saving blank values.
  2146. *
  2147. * @return void
  2148. */
  2149. public function testUpdateSavingBlankValues() {
  2150. $this->loadFixtures('Article');
  2151. $Article = new Article();
  2152. $Article->validate = array();
  2153. $Article->create();
  2154. $result = $Article->save(array(
  2155. 'id' => 1,
  2156. 'title' => '',
  2157. 'body' => ''
  2158. ));
  2159. $this->assertTrue((bool)$result);
  2160. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2161. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2162. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2163. }
  2164. /**
  2165. * testUpdateMultiple method
  2166. *
  2167. * @return void
  2168. */
  2169. public function testUpdateMultiple() {
  2170. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2171. $TestModel = new Comment();
  2172. $result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2173. $expected = array('2', '4', '1', '1', '1', '2');
  2174. $this->assertEquals($expected, $result);
  2175. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2176. $result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2177. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2178. $this->assertEquals($expected, $result);
  2179. $result = $TestModel->updateAll(
  2180. array('Comment.comment' => "'Updated today'"),
  2181. array('Comment.user_id' => 5)
  2182. );
  2183. $this->assertFalse(empty($result));
  2184. $result = Hash::extract(
  2185. $TestModel->find('all', array(
  2186. 'conditions' => array(
  2187. 'Comment.user_id' => 5
  2188. ))),
  2189. '{n}.Comment.comment'
  2190. );
  2191. $expected = array_fill(0, 2, 'Updated today');
  2192. $this->assertEquals($expected, $result);
  2193. }
  2194. /**
  2195. * testHabtmUuidWithUuidId method
  2196. *
  2197. * @return void
  2198. */
  2199. public function testHabtmUuidWithUuidId() {
  2200. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2201. $TestModel = new Uuidportfolio();
  2202. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2203. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2204. $TestModel->create($data);
  2205. $TestModel->save();
  2206. $id = $TestModel->id;
  2207. $result = $TestModel->read(null, $id);
  2208. $this->assertEquals(1, count($result['Uuiditem']));
  2209. $this->assertEquals(36, strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']));
  2210. }
  2211. /**
  2212. * test HABTM saving when join table has no primary key and only 2 columns.
  2213. *
  2214. * @return void
  2215. */
  2216. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2217. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2218. $Fruit = new Fruit();
  2219. $data = array(
  2220. 'Fruit' => array(
  2221. 'color' => 'Red',
  2222. 'shape' => 'Heart-shaped',
  2223. 'taste' => 'sweet',
  2224. 'name' => 'Strawberry',
  2225. ),
  2226. 'UuidTag' => array(
  2227. 'UuidTag' => array(
  2228. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2229. )
  2230. )
  2231. );
  2232. $result = $Fruit->save($data);
  2233. $this->assertFalse(empty($result));
  2234. }
  2235. /**
  2236. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2237. *
  2238. * @return void
  2239. */
  2240. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2241. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2242. $Fruit = new FruitNoWith();
  2243. $data = array(
  2244. 'Fruit' => array(
  2245. 'color' => 'Red',
  2246. 'shape' => 'Heart-shaped',
  2247. 'taste' => 'sweet',
  2248. 'name' => 'Strawberry',
  2249. ),
  2250. 'UuidTag' => array(
  2251. 'UuidTag' => array(
  2252. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2253. )
  2254. )
  2255. );
  2256. $result = $Fruit->save($data);
  2257. $this->assertFalse(empty($result));
  2258. }
  2259. /**
  2260. * testHabtmUuidWithNumericId method
  2261. *
  2262. * @return void
  2263. */
  2264. public function testHabtmUuidWithNumericId() {
  2265. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2266. $TestModel = new Uuiditem();
  2267. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2268. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2269. $TestModel->create($data);
  2270. $TestModel->save();
  2271. $id = $TestModel->id;
  2272. $result = $TestModel->read(null, $id);
  2273. $this->assertEquals(1, count($result['Uuidportfolio']));
  2274. }
  2275. /**
  2276. * testSaveMultipleHabtm method
  2277. *
  2278. * @return void
  2279. */
  2280. public function testSaveMultipleHabtm() {
  2281. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2282. $TestModel = new JoinA();
  2283. $result = $TestModel->findById(1);
  2284. $expected = array(
  2285. 'JoinA' => array(
  2286. 'id' => 1,
  2287. 'name' => 'Join A 1',
  2288. 'body' => 'Join A 1 Body',
  2289. 'created' => '2008-01-03 10:54:23',
  2290. 'updated' => '2008-01-03 10:54:23'
  2291. ),
  2292. 'JoinB' => array(
  2293. 0 => array(
  2294. 'id' => 2,
  2295. 'name' => 'Join B 2',
  2296. 'created' => '2008-01-03 10:55:02',
  2297. 'updated' => '2008-01-03 10:55:02',
  2298. 'JoinAsJoinB' => array(
  2299. 'id' => 1,
  2300. 'join_a_id' => 1,
  2301. 'join_b_id' => 2,
  2302. 'other' => 'Data for Join A 1 Join B 2',
  2303. 'created' => '2008-01-03 10:56:33',
  2304. 'updated' => '2008-01-03 10:56:33'
  2305. ))),
  2306. 'JoinC' => array(
  2307. 0 => array(
  2308. 'id' => 2,
  2309. 'name' => 'Join C 2',
  2310. 'created' => '2008-01-03 10:56:12',
  2311. 'updated' => '2008-01-03 10:56:12',
  2312. 'JoinAsJoinC' => array(
  2313. 'id' => 1,
  2314. 'join_a_id' => 1,
  2315. 'join_c_id' => 2,
  2316. 'other' => 'Data for Join A 1 Join C 2',
  2317. 'created' => '2008-01-03 10:57:22',
  2318. 'updated' => '2008-01-03 10:57:22'
  2319. ))));
  2320. $this->assertEquals($expected, $result);
  2321. $TestModel->id = 1;
  2322. $data = array(
  2323. 'JoinA' => array(
  2324. 'id' => '1',
  2325. 'name' => 'New name for Join A 1',
  2326. 'updated' => self::date()
  2327. ),
  2328. 'JoinB' => array(
  2329. array(
  2330. 'id' => 1,
  2331. 'join_b_id' => 2,
  2332. 'other' => 'New data for Join A 1 Join B 2',
  2333. 'created' => self::date(),
  2334. 'updated' => self::date()
  2335. )),
  2336. 'JoinC' => array(
  2337. array(
  2338. 'id' => 1,
  2339. 'join_c_id' => 2,
  2340. 'other' => 'New data for Join A 1 Join C 2',
  2341. 'created' => self::date(),
  2342. 'updated' => self::date()
  2343. )));
  2344. $TestModel->set($data);
  2345. $TestModel->save();
  2346. $result = $TestModel->findById(1);
  2347. $expected = array(
  2348. 'JoinA' => array(
  2349. 'id' => 1,
  2350. 'name' => 'New name for Join A 1',
  2351. 'body' => 'Join A 1 Body',
  2352. 'created' => '2008-01-03 10:54:23',
  2353. 'updated' => self::date()
  2354. ),
  2355. 'JoinB' => array(
  2356. 0 => array(
  2357. 'id' => 2,
  2358. 'name' => 'Join B 2',
  2359. 'created' => '2008-01-03 10:55:02',
  2360. 'updated' => '2008-01-03 10:55:02',
  2361. 'JoinAsJoinB' => array(
  2362. 'id' => 1,
  2363. 'join_a_id' => 1,
  2364. 'join_b_id' => 2,
  2365. 'other' => 'New data for Join A 1 Join B 2',
  2366. 'created' => self::date(),
  2367. 'updated' => self::date()
  2368. ))),
  2369. 'JoinC' => array(
  2370. 0 => array(
  2371. 'id' => 2,
  2372. 'name' => 'Join C 2',
  2373. 'created' => '2008-01-03 10:56:12',
  2374. 'updated' => '2008-01-03 10:56:12',
  2375. 'JoinAsJoinC' => array(
  2376. 'id' => 1,
  2377. 'join_a_id' => 1,
  2378. 'join_c_id' => 2,
  2379. 'other' => 'New data for Join A 1 Join C 2',
  2380. 'created' => self::date(),
  2381. 'updated' => self::date()
  2382. ))));
  2383. $this->assertEquals($expected, $result);
  2384. }
  2385. /**
  2386. * testSaveAll method
  2387. *
  2388. * @return void
  2389. */
  2390. public function testSaveAll() {
  2391. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2392. $TestModel = new Post();
  2393. $result = $TestModel->find('all');
  2394. $this->assertEquals(3, count($result));
  2395. $this->assertFalse(isset($result[3]));
  2396. $TestModel->saveAll(array(
  2397. 'Post' => array(
  2398. 'title' => 'Post with Author',
  2399. 'body' => 'This post will be saved with an author'
  2400. ),
  2401. 'Author' => array(
  2402. 'user' => 'bob',
  2403. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2404. )));
  2405. $result = $TestModel->find('all');
  2406. $expected = array(
  2407. 'Post' => array(
  2408. 'id' => '4',
  2409. 'author_id' => '5',
  2410. 'title' => 'Post with Author',
  2411. 'body' => 'This post will be saved with an author',
  2412. 'published' => 'N'
  2413. ),
  2414. 'Author' => array(
  2415. 'id' => '5',
  2416. 'user' => 'bob',
  2417. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2418. 'test' => 'working'
  2419. ));
  2420. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  2421. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  2422. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  2423. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  2424. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2425. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2426. $this->assertEquals($expected, $result[3]);
  2427. $this->assertEquals(4, count($result));
  2428. $TestModel->deleteAll(true);
  2429. $this->assertEquals(array(), $TestModel->find('all'));
  2430. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2431. $this->db->truncate($TestModel);
  2432. $TestModel->saveAll(array(
  2433. array(
  2434. 'title' => 'Multi-record post 1',
  2435. 'body' => 'First multi-record post',
  2436. 'author_id' => 2
  2437. ),
  2438. array(
  2439. 'title' => 'Multi-record post 2',
  2440. 'body' => 'Second multi-record post',
  2441. 'author_id' => 2
  2442. )));
  2443. $result = $TestModel->find('all', array(
  2444. 'recursive' => -1,
  2445. 'order' => 'Post.id ASC'
  2446. ));
  2447. $expected = array(
  2448. array(
  2449. 'Post' => array(
  2450. 'id' => '1',
  2451. 'author_id' => '2',
  2452. 'title' => 'Multi-record post 1',
  2453. 'body' => 'First multi-record post',
  2454. 'published' => 'N'
  2455. )),
  2456. array(
  2457. 'Post' => array(
  2458. 'id' => '2',
  2459. 'author_id' => '2',
  2460. 'title' => 'Multi-record post 2',
  2461. 'body' => 'Second multi-record post',
  2462. 'published' => 'N'
  2463. )));
  2464. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  2465. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  2466. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  2467. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  2468. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2469. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2470. $this->assertEquals($expected, $result);
  2471. $TestModel = new Comment();
  2472. $result = $TestModel->saveAll(array(
  2473. 'Comment' => array(
  2474. 'article_id' => 2,
  2475. 'user_id' => 2,
  2476. 'comment' => 'New comment with attachment',
  2477. 'published' => 'Y'
  2478. ),
  2479. 'Attachment' => array(
  2480. 'attachment' => 'some_file.tgz'
  2481. )));
  2482. $this->assertFalse(empty($result));
  2483. $result = $TestModel->find('all');
  2484. $expected = array(
  2485. 'id' => '7',
  2486. 'article_id' => '2',
  2487. 'user_id' => '2',
  2488. 'comment' => 'New comment with attachment',
  2489. 'published' => 'Y'
  2490. );
  2491. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  2492. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  2493. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2494. $this->assertEquals($expected, $result[6]['Comment']);
  2495. $expected = array(
  2496. 'id' => '2',
  2497. 'comment_id' => '7',
  2498. 'attachment' => 'some_file.tgz'
  2499. );
  2500. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  2501. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  2502. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2503. $this->assertEquals($expected, $result[6]['Attachment']);
  2504. }
  2505. /**
  2506. * Test SaveAll with Habtm relations
  2507. *
  2508. * @return void
  2509. */
  2510. public function testSaveAllHabtm() {
  2511. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2512. $data = array(
  2513. 'Article' => array(
  2514. 'user_id' => 1,
  2515. 'title' => 'Article Has and belongs to Many Tags'
  2516. ),
  2517. 'Tag' => array(
  2518. 'Tag' => array(1, 2)
  2519. ),
  2520. 'Comment' => array(
  2521. array(
  2522. 'comment' => 'Article comment',
  2523. 'user_id' => 1
  2524. )));
  2525. $Article = new Article();
  2526. $result = $Article->saveAll($data);
  2527. $this->assertFalse(empty($result));
  2528. $result = $Article->read();
  2529. $this->assertEquals(2, count($result['Tag']));
  2530. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  2531. $this->assertEquals(1, count($result['Comment']));
  2532. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  2533. }
  2534. /**
  2535. * Test SaveAll with Habtm relations and extra join table fields
  2536. *
  2537. * @return void
  2538. */
  2539. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2540. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2541. $data = array(
  2542. 'Something' => array(
  2543. 'id' => 4,
  2544. 'title' => 'Extra Fields',
  2545. 'body' => 'Extra Fields Body',
  2546. 'published' => '1'
  2547. ),
  2548. 'SomethingElse' => array(
  2549. array('something_else_id' => 1, 'doomed' => '1'),
  2550. array('something_else_id' => 2, 'doomed' => '0'),
  2551. array('something_else_id' => 3, 'doomed' => '1')
  2552. )
  2553. );
  2554. $Something = new Something();
  2555. $result = $Something->saveAll($data);
  2556. $this->assertFalse(empty($result));
  2557. $result = $Something->read();
  2558. $this->assertEquals(3, count($result['SomethingElse']));
  2559. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2560. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2561. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2562. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2563. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2564. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2565. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2566. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2567. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2568. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2569. }
  2570. /**
  2571. * testSaveAllHasOne method
  2572. *
  2573. * @return void
  2574. */
  2575. public function testSaveAllHasOne() {
  2576. $model = new Comment();
  2577. $model->deleteAll(true);
  2578. $this->assertEquals(array(), $model->find('all'));
  2579. $model->Attachment->deleteAll(true);
  2580. $this->assertEquals(array(), $model->Attachment->find('all'));
  2581. $this->assertTrue($model->saveAll(array(
  2582. 'Comment' => array(
  2583. 'comment' => 'Comment with attachment',
  2584. 'article_id' => 1,
  2585. 'user_id' => 1
  2586. ),
  2587. 'Attachment' => array(
  2588. 'attachment' => 'some_file.zip'
  2589. ))));
  2590. $result = $model->find('all', array('fields' => array(
  2591. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2592. 'Attachment.comment_id', 'Attachment.attachment'
  2593. )));
  2594. $expected = array(array(
  2595. 'Comment' => array(
  2596. 'id' => '1',
  2597. 'comment' => 'Comment with attachment'
  2598. ),
  2599. 'Attachment' => array(
  2600. 'id' => '1',
  2601. 'comment_id' => '1',
  2602. 'attachment' => 'some_file.zip'
  2603. )));
  2604. $this->assertEquals($expected, $result);
  2605. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2606. $data = array(
  2607. 'Comment' => array(
  2608. 'comment' => 'Comment with attachment',
  2609. 'article_id' => 1,
  2610. 'user_id' => 1
  2611. ),
  2612. 'Attachment' => array(
  2613. 'attachment' => 'some_file.zip'
  2614. ));
  2615. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2616. }
  2617. /**
  2618. * testSaveAllBelongsTo method
  2619. *
  2620. * @return void
  2621. */
  2622. public function testSaveAllBelongsTo() {
  2623. $model = new Comment();
  2624. $model->deleteAll(true);
  2625. $this->assertEquals(array(), $model->find('all'));
  2626. $model->Article->deleteAll(true);
  2627. $this->assertEquals(array(), $model->Article->find('all'));
  2628. $this->assertTrue($model->saveAll(array(
  2629. 'Comment' => array(
  2630. 'comment' => 'Article comment',
  2631. 'article_id' => 1,
  2632. 'user_id' => 1
  2633. ),
  2634. 'Article' => array(
  2635. 'title' => 'Model Associations 101',
  2636. 'user_id' => 1
  2637. ))));
  2638. $result = $model->find('all', array('fields' => array(
  2639. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2640. )));
  2641. $expected = array(array(
  2642. 'Comment' => array(
  2643. 'id' => '1',
  2644. 'article_id' => '1',
  2645. 'comment' => 'Article comment'
  2646. ),
  2647. 'Article' => array(
  2648. 'id' => '1',
  2649. 'title' => 'Model Associations 101'
  2650. )));
  2651. $this->assertEquals($expected, $result);
  2652. }
  2653. /**
  2654. * testSaveAllHasOneValidation method
  2655. *
  2656. * @return void
  2657. */
  2658. public function testSaveAllHasOneValidation() {
  2659. $model = new Comment();
  2660. $model->deleteAll(true);
  2661. $this->assertEquals(array(), $model->find('all'));
  2662. $model->Attachment->deleteAll(true);
  2663. $this->assertEquals(array(), $model->Attachment->find('all'));
  2664. $model->validate = array('comment' => 'notEmpty');
  2665. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2666. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2667. $result = $model->saveAll(
  2668. array(
  2669. 'Comment' => array(
  2670. 'comment' => '',
  2671. 'article_id' => 1,
  2672. 'user_id' => 1
  2673. ),
  2674. 'Attachment' => array('attachment' => '')
  2675. ),
  2676. array('validate' => 'first')
  2677. );
  2678. $this->assertEquals(false, $result);
  2679. $expected = array(
  2680. 'comment' => array('This field cannot be left blank'),
  2681. 'Attachment' => array(
  2682. 'attachment' => array('This field cannot be left blank')
  2683. )
  2684. );
  2685. $this->assertEquals($expected, $model->validationErrors);
  2686. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  2687. }
  2688. /**
  2689. * testSaveAllAtomic method
  2690. *
  2691. * @return void
  2692. */
  2693. public function testSaveAllAtomic() {
  2694. $this->loadFixtures('Article', 'User', 'Comment');
  2695. $TestModel = new Article();
  2696. $result = $TestModel->saveAll(array(
  2697. 'Article' => array(
  2698. 'title' => 'Post with Author',
  2699. 'body' => 'This post will be saved with an author',
  2700. 'user_id' => 2
  2701. ),
  2702. 'Comment' => array(
  2703. array('comment' => 'First new comment', 'user_id' => 2))
  2704. ), array('atomic' => false));
  2705. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2706. $result = $TestModel->saveAll(array(
  2707. array(
  2708. 'id' => '1',
  2709. 'title' => 'Baleeted First Post',
  2710. 'body' => 'Baleeted!',
  2711. 'published' => 'N'
  2712. ),
  2713. array(
  2714. 'id' => '2',
  2715. 'title' => 'Just update the title'
  2716. ),
  2717. array(
  2718. 'title' => 'Creating a fourth post',
  2719. 'body' => 'Fourth post body',
  2720. 'user_id' => 2
  2721. )
  2722. ), array('atomic' => false));
  2723. $this->assertSame($result, array(true, true, true));
  2724. $result = $TestModel->saveAll(array(
  2725. 'Article' => array('id' => 2),
  2726. 'Comment' => array(
  2727. array(
  2728. 'comment' => 'First new comment',
  2729. 'published' => 'Y',
  2730. 'user_id' => 1
  2731. ),
  2732. array(
  2733. 'comment' => 'Second new comment',
  2734. 'published' => 'Y',
  2735. 'user_id' => 2
  2736. ))
  2737. ), array('validate' => true, 'atomic' => false));
  2738. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2739. $TestModel->validate = array(
  2740. 'title' => 'notEmpty',
  2741. 'author_id' => 'numeric'
  2742. );
  2743. $result = $TestModel->saveAll(array(
  2744. array(
  2745. 'id' => '1',
  2746. 'title' => 'Un-Baleeted First Post',
  2747. 'body' => 'Not Baleeted!',
  2748. 'published' => 'Y'
  2749. ),
  2750. array(
  2751. 'id' => '2',
  2752. 'title' => '',
  2753. 'body' => 'Trying to get away with an empty title'
  2754. )
  2755. ), array('validate' => true, 'atomic' => false));
  2756. $this->assertSame(array(true, false), $result);
  2757. }
  2758. /**
  2759. * testSaveAllDeepAssociated method
  2760. *
  2761. * @return void
  2762. */
  2763. public function testSaveAllDeepAssociated() {
  2764. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2765. $TestModel = new Article();
  2766. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2767. $TestModel->hasAndBelongsToMany = array();
  2768. $result = $TestModel->saveAll(array(
  2769. 'Article' => array('id' => 2),
  2770. 'Comment' => array(
  2771. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
  2772. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2773. )
  2774. ), array('deep' => true));
  2775. $this->assertTrue($result);
  2776. $result = $TestModel->findById(2);
  2777. $expected = array(
  2778. 'First Comment for Second Article',
  2779. 'Second Comment for Second Article',
  2780. 'First new comment',
  2781. 'Second new comment'
  2782. );
  2783. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2784. $this->assertEquals($expected, $result);
  2785. $result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
  2786. $this->assertEquals(5, $result);
  2787. $result = $TestModel->saveAll(array(
  2788. 'Article' => array('id' => 2),
  2789. 'Comment' => array(
  2790. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  2791. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
  2792. )
  2793. ), array('deep' => true));
  2794. $this->assertTrue($result);
  2795. $result = $TestModel->findById(2);
  2796. $expected = array(
  2797. 'First Comment for Second Article',
  2798. 'Second Comment for Second Article',
  2799. 'First new comment',
  2800. 'Second new comment',
  2801. 'Third new comment',
  2802. 'Fourth new comment'
  2803. );
  2804. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  2805. $this->assertEquals($expected, $result);
  2806. $result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
  2807. $this->assertEquals(2, $result);
  2808. $data = array(
  2809. 'Attachment' => array(
  2810. 'attachment' => 'deepsave insert',
  2811. ),
  2812. 'Comment' => array(
  2813. 'comment' => 'First comment deepsave insert',
  2814. 'published' => 'Y',
  2815. 'user_id' => 5,
  2816. 'Article' => array(
  2817. 'title' => 'First Article deepsave insert',
  2818. 'body' => 'First Article Body deepsave insert',
  2819. 'User' => array(
  2820. 'user' => '',
  2821. 'password' => 'magic'
  2822. ),
  2823. ),
  2824. )
  2825. );
  2826. $TestModel->Comment->Attachment->create();
  2827. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2828. $this->assertFalse($result);
  2829. $expected = array('User' => array('user' => array('This field cannot be left blank')));
  2830. $this->assertEquals($expected, $TestModel->validationErrors);
  2831. $data['Comment']['Article']['User']['user'] = 'deepsave';
  2832. $TestModel->Comment->Attachment->create();
  2833. $result = $TestModel->Comment->Attachment->saveAll($data, array('deep' => true));
  2834. $this->assertTrue($result);
  2835. $result = $TestModel->Comment->Attachment->findById($TestModel->Comment->Attachment->id);
  2836. $expected = array(
  2837. 'Attachment' => array(
  2838. 'id' => '3',
  2839. 'comment_id' => '11',
  2840. 'attachment' => 'deepsave insert',
  2841. ),
  2842. 'Comment' => array(
  2843. 'id' => '11',
  2844. 'article_id' => '4',
  2845. 'user_id' => '5',
  2846. 'comment' => 'First comment deepsave insert',
  2847. 'published' => 'Y',
  2848. )
  2849. );
  2850. unset($result['Attachment']['created'], $result['Attachment']['updated']);
  2851. $this->assertEquals($expected['Attachment'], $result['Attachment']);
  2852. unset($result['Comment']['created'], $result['Comment']['updated']);
  2853. $this->assertEquals($expected['Comment'], $result['Comment']);
  2854. $result = $TestModel->findById($result['Comment']['article_id']);
  2855. $expected = array(
  2856. 'Article' => array(
  2857. 'id' => '4',
  2858. 'user_id' => '6',
  2859. 'title' => 'First Article deepsave insert',
  2860. 'body' => 'First Article Body deepsave insert',
  2861. 'published' => 'N',
  2862. ),
  2863. 'User' => array(
  2864. 'id' => '6',
  2865. 'user' => 'deepsave',
  2866. 'password' => 'magic',
  2867. ),
  2868. 'Comment' => array(
  2869. array(
  2870. 'id' => '11',
  2871. 'article_id' => '4',
  2872. 'user_id' => '5',
  2873. 'comment' => 'First comment deepsave insert',
  2874. 'published' => 'Y',
  2875. )
  2876. )
  2877. );
  2878. unset(
  2879. $result['Article']['created'], $result['Article']['updated'],
  2880. $result['User']['created'], $result['User']['updated'],
  2881. $result['Comment'][0]['created'], $result['Comment'][0]['updated']
  2882. );
  2883. $this->assertEquals($expected, $result);
  2884. }
  2885. /**
  2886. * testSaveAllDeepMany
  2887. * tests the validate methods with deeper recursive data
  2888. *
  2889. * @return void
  2890. */
  2891. public function testSaveAllDeepMany() {
  2892. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  2893. $TestModel = new Article();
  2894. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2895. $TestModel->hasAndBelongsToMany = array();
  2896. $data = array(
  2897. array(
  2898. 'Article' => array('id' => 1),
  2899. 'Comment' => array(
  2900. array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
  2901. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2902. )
  2903. ),
  2904. array(
  2905. 'Article' => array('id' => 2),
  2906. 'Comment' => array(
  2907. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
  2908. array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
  2909. )
  2910. )
  2911. );
  2912. $result = $TestModel->saveAll($data, array('deep' => true));
  2913. $this->assertTrue($result);
  2914. $data = array(
  2915. array(
  2916. 'id' => 1, 'body' => '',
  2917. 'Comment' => array(
  2918. array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
  2919. array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
  2920. )
  2921. ),
  2922. array(
  2923. 'Article' => array('id' => 2),
  2924. 'Comment' => array(
  2925. array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => '')),
  2926. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  2927. )
  2928. )
  2929. );
  2930. $TestModel->Comment->validate['comment'] = 'notEmpty';
  2931. $result = $TestModel->saveAll($data, array('deep' => true));
  2932. $this->assertFalse($result);
  2933. $expected = array(
  2934. 0 => array(
  2935. 'body' => array('This field cannot be left blank'),
  2936. 'Comment' => array(
  2937. 0 => array(
  2938. 'comment' => array('This field cannot be left blank'),
  2939. 'User' => array(
  2940. 'user' => array('This field cannot be left blank')
  2941. )
  2942. )
  2943. )
  2944. ),
  2945. 1 => array(
  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. );
  3353. $result = $TestModel->validationErrors;
  3354. $this->assertSame($expected, $result);
  3355. $data = array(
  3356. array(
  3357. 'Article' => array('id' => 1, 'body' => 'Ignore invalid comment'),
  3358. 'Comment' => array(
  3359. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3360. )
  3361. ),
  3362. array(
  3363. 'Article' => array('id' => 2),
  3364. 'Comment' => array(
  3365. array('comment' => '', 'published' => 'Y', 'user_id' => 2)
  3366. )
  3367. )
  3368. );
  3369. $result = $TestModel->saveAll($data, array('deep' => false));
  3370. $this->assertTrue($result);
  3371. }
  3372. /**
  3373. * testSaveAllNotDeepValidateOnly
  3374. * tests the validate methods to not validate deeper recursive data
  3375. *
  3376. * @return void
  3377. */
  3378. public function testSaveAllNotDeepValidateOnly() {
  3379. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  3380. $TestModel = new Article();
  3381. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3382. $TestModel->hasAndBelongsToMany = array();
  3383. $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
  3384. $TestModel->Comment->validate['comment'] = 'notEmpty';
  3385. $result = $TestModel->saveAll(
  3386. array(
  3387. 'Article' => array('id' => 2, 'body' => ''),
  3388. 'Comment' => array(
  3389. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3390. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3391. )
  3392. ),
  3393. array('validate' => 'only', 'deep' => false)
  3394. );
  3395. $this->assertFalse($result);
  3396. $expected = array('body' => array('This field cannot be left blank'));
  3397. $result = $TestModel->validationErrors;
  3398. $this->assertSame($expected, $result);
  3399. $result = $TestModel->saveAll(
  3400. array(
  3401. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3402. 'Comment' => array(
  3403. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3404. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3405. )
  3406. ),
  3407. array('validate' => 'only', 'deep' => false)
  3408. );
  3409. $this->assertTrue($result);
  3410. $result = $TestModel->saveAll(
  3411. array(
  3412. 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
  3413. 'Comment' => array(
  3414. array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
  3415. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3416. )
  3417. ),
  3418. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3419. );
  3420. $expected = array(
  3421. 'Article' => true,
  3422. 'Comment' => array(
  3423. true,
  3424. true
  3425. )
  3426. );
  3427. $this->assertSame($expected, $result);
  3428. $result = $TestModel->saveAll(array(
  3429. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3430. 'Comment' => array(
  3431. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3432. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3433. )
  3434. ),
  3435. array('validate' => 'only', 'deep' => false)
  3436. );
  3437. $this->assertTrue($result);
  3438. $result = $TestModel->saveAll(array(
  3439. 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
  3440. 'Comment' => array(
  3441. array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
  3442. array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
  3443. )
  3444. ),
  3445. array('validate' => 'only', 'atomic' => false, 'deep' => false)
  3446. );
  3447. $expected = array(
  3448. 'Article' => true,
  3449. 'Comment' => array(
  3450. true,
  3451. true
  3452. )
  3453. );
  3454. $this->assertSame($expected, $result);
  3455. $expected = array();
  3456. $result = $TestModel->validationErrors;
  3457. $this->assertSame($expected, $result);
  3458. $data = array(
  3459. 'Attachment' => array(
  3460. 'attachment' => 'deepsave insert',
  3461. ),
  3462. 'Comment' => array(
  3463. 'comment' => 'First comment deepsave insert',
  3464. 'published' => 'Y',
  3465. 'user_id' => 5,
  3466. 'Article' => array(
  3467. 'title' => 'First Article deepsave insert ignored',
  3468. 'body' => 'First Article Body deepsave insert',
  3469. 'User' => array(
  3470. 'user' => '',
  3471. 'password' => 'magic'
  3472. ),
  3473. ),
  3474. )
  3475. );
  3476. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3477. $this->assertTrue($result);
  3478. $result = $TestModel->Comment->Attachment->validationErrors;
  3479. $expected = array();
  3480. $this->assertSame($expected, $result);
  3481. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3482. $expected = array(
  3483. 'Attachment' => true,
  3484. 'Comment' => true
  3485. );
  3486. $this->assertEquals($expected, $result);
  3487. $data['Comment']['Article']['body'] = '';
  3488. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
  3489. $this->assertTrue($result);
  3490. $result = $TestModel->Comment->Attachment->validationErrors;
  3491. $expected = array();
  3492. $this->assertSame($expected, $result);
  3493. $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
  3494. $expected = array(
  3495. 'Attachment' => true,
  3496. 'Comment' => true
  3497. );
  3498. $this->assertEquals($expected, $result);
  3499. }
  3500. /**
  3501. * testSaveAllHasMany method
  3502. *
  3503. * @return void
  3504. */
  3505. public function testSaveAllHasMany() {
  3506. $this->loadFixtures('Article', 'Comment');
  3507. $TestModel = new Article();
  3508. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  3509. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3510. $result = $TestModel->saveAll(array(
  3511. 'Article' => array('id' => 2),
  3512. 'Comment' => array(
  3513. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3514. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3515. )
  3516. ));
  3517. $this->assertFalse(empty($result));
  3518. $result = $TestModel->findById(2);
  3519. $expected = array(
  3520. 'First Comment for Second Article',
  3521. 'Second Comment for Second Article',
  3522. 'First new comment',
  3523. 'Second new comment'
  3524. );
  3525. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3526. $this->assertEquals($expected, $result);
  3527. $result = $TestModel->saveAll(
  3528. array(
  3529. 'Article' => array('id' => 2),
  3530. 'Comment' => array(
  3531. array(
  3532. 'comment' => 'Third new comment',
  3533. 'published' => 'Y',
  3534. 'user_id' => 1
  3535. ))),
  3536. array('atomic' => false)
  3537. );
  3538. $this->assertFalse(empty($result));
  3539. $result = $TestModel->findById(2);
  3540. $expected = array(
  3541. 'First Comment for Second Article',
  3542. 'Second Comment for Second Article',
  3543. 'First new comment',
  3544. 'Second new comment',
  3545. 'Third new comment'
  3546. );
  3547. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3548. $this->assertEquals($expected, $result);
  3549. $TestModel->beforeSaveReturn = false;
  3550. $result = $TestModel->saveAll(
  3551. array(
  3552. 'Article' => array('id' => 2),
  3553. 'Comment' => array(
  3554. array(
  3555. 'comment' => 'Fourth new comment',
  3556. 'published' => 'Y',
  3557. 'user_id' => 1
  3558. ))),
  3559. array('atomic' => false)
  3560. );
  3561. $this->assertEquals(array('Article' => false), $result);
  3562. $result = $TestModel->findById(2);
  3563. $expected = array(
  3564. 'First Comment for Second Article',
  3565. 'Second Comment for Second Article',
  3566. 'First new comment',
  3567. 'Second new comment',
  3568. 'Third new comment'
  3569. );
  3570. $result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
  3571. $this->assertEquals($expected, $result);
  3572. }
  3573. /**
  3574. * testSaveAllHasManyValidation method
  3575. *
  3576. * @return void
  3577. */
  3578. public function testSaveAllHasManyValidation() {
  3579. $this->loadFixtures('Article', 'Comment');
  3580. $TestModel = new Article();
  3581. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3582. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3583. $result = $TestModel->saveAll(array(
  3584. 'Article' => array('id' => 2),
  3585. 'Comment' => array(
  3586. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  3587. )
  3588. ), array('validate' => true));
  3589. $this->assertFalse($result);
  3590. $expected = array('Comment' => array(
  3591. array('comment' => array('This field cannot be left blank'))
  3592. ));
  3593. $this->assertEquals($expected, $TestModel->validationErrors);
  3594. $expected = array(
  3595. array('comment' => array('This field cannot be left blank'))
  3596. );
  3597. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  3598. $result = $TestModel->saveAll(array(
  3599. 'Article' => array('id' => 2),
  3600. 'Comment' => array(
  3601. array(
  3602. 'comment' => '',
  3603. 'published' => 'Y',
  3604. 'user_id' => 1
  3605. ))
  3606. ), array('validate' => 'first'));
  3607. $this->assertFalse($result);
  3608. }
  3609. /**
  3610. * test saveAll with transactions and ensure there is no missing rollback.
  3611. *
  3612. * @return void
  3613. */
  3614. public function testSaveAllManyRowsTransactionNoRollback() {
  3615. $this->loadFixtures('Post');
  3616. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  3617. $db = ConnectionManager::create('mock_transaction', array(
  3618. 'datasource' => 'MockTransactionDboSource',
  3619. ));
  3620. $db->expects($this->once())
  3621. ->method('describe')
  3622. ->will($this->returnValue(array()));
  3623. $db->expects($this->once())->method('rollback');
  3624. $Post = new Post('mock_transaction');
  3625. $Post->validate = array(
  3626. 'title' => array('rule' => array('notEmpty'))
  3627. );
  3628. $data = array(
  3629. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3630. array('author_id' => 1, 'title' => '')
  3631. );
  3632. $Post->saveAll($data, array('atomic' => true));
  3633. }
  3634. /**
  3635. * test saveAll with transactions and ensure there is no missing rollback.
  3636. *
  3637. * @return void
  3638. */
  3639. public function testSaveAllAssociatedTransactionNoRollback() {
  3640. $testDb = ConnectionManager::getDataSource('test');
  3641. $mock = $this->getMock(
  3642. 'DboSource',
  3643. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  3644. array(),
  3645. 'MockTransactionAssociatedDboSource'
  3646. );
  3647. $db = ConnectionManager::create('mock_transaction_assoc', array(
  3648. 'datasource' => 'MockTransactionAssociatedDboSource',
  3649. ));
  3650. $this->mockObjects[] = $db;
  3651. $db->columns = $testDb->columns;
  3652. $db->expects($this->once())->method('rollback');
  3653. $db->expects($this->any())->method('describe')
  3654. ->will($this->returnValue(array(
  3655. 'id' => array('type' => 'integer', 'length' => 11),
  3656. 'title' => array('type' => 'string'),
  3657. 'body' => array('type' => 'text'),
  3658. 'published' => array('type' => 'string')
  3659. )));
  3660. $Post = new Post();
  3661. $Post->useDbConfig = 'mock_transaction_assoc';
  3662. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  3663. $Post->Author->validate = array(
  3664. 'user' => array('rule' => array('notEmpty'))
  3665. );
  3666. $data = array(
  3667. 'Post' => array(
  3668. 'title' => 'New post',
  3669. 'body' => 'Content',
  3670. 'published' => 'Y'
  3671. ),
  3672. 'Author' => array(
  3673. 'user' => '',
  3674. 'password' => "sekret"
  3675. )
  3676. );
  3677. $Post->saveAll($data, array('validate' => true));
  3678. }
  3679. /**
  3680. * test saveAll with nested saveAll call.
  3681. *
  3682. * @return void
  3683. */
  3684. public function testSaveAllNestedSaveAll() {
  3685. $this->loadFixtures('Sample');
  3686. $TransactionTestModel = new TransactionTestModel();
  3687. $data = array(
  3688. array('apple_id' => 1, 'name' => 'sample5'),
  3689. );
  3690. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  3691. }
  3692. /**
  3693. * testSaveAllTransaction method
  3694. *
  3695. * @return void
  3696. */
  3697. public function testSaveAllTransaction() {
  3698. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3699. $TestModel = new Post();
  3700. $TestModel->validate = array('title' => 'notEmpty');
  3701. $data = array(
  3702. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3703. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3704. array('author_id' => 1, 'title' => '')
  3705. );
  3706. $this->assertFalse($TestModel->saveAll($data));
  3707. $result = $TestModel->find('all', array('recursive' => -1));
  3708. $expected = array(
  3709. array('Post' => array(
  3710. 'id' => '1',
  3711. 'author_id' => 1,
  3712. 'title' => 'First Post',
  3713. 'body' => 'First Post Body',
  3714. 'published' => 'Y',
  3715. 'created' => '2007-03-18 10:39:23',
  3716. 'updated' => '2007-03-18 10:41:31'
  3717. )),
  3718. array('Post' => array(
  3719. 'id' => '2',
  3720. 'author_id' => 3,
  3721. 'title' => 'Second Post',
  3722. 'body' => 'Second Post Body',
  3723. 'published' => 'Y',
  3724. 'created' => '2007-03-18 10:41:23',
  3725. 'updated' => '2007-03-18 10:43:31'
  3726. )),
  3727. array('Post' => array(
  3728. 'id' => '3',
  3729. 'author_id' => 1,
  3730. 'title' => 'Third Post',
  3731. 'body' => 'Third Post Body',
  3732. 'published' => 'Y',
  3733. 'created' => '2007-03-18 10:43:23',
  3734. 'updated' => '2007-03-18 10:45:31'
  3735. )));
  3736. if (count($result) != 3) {
  3737. // Database doesn't support transactions
  3738. $expected[] = array(
  3739. 'Post' => array(
  3740. 'id' => '4',
  3741. 'author_id' => 1,
  3742. 'title' => 'New Fourth Post',
  3743. 'body' => null,
  3744. 'published' => 'N',
  3745. 'created' => self::date(),
  3746. 'updated' => self::date()
  3747. ));
  3748. $expected[] = array(
  3749. 'Post' => array(
  3750. 'id' => '5',
  3751. 'author_id' => 1,
  3752. 'title' => 'New Fifth Post',
  3753. 'body' => null,
  3754. 'published' => 'N',
  3755. 'created' => self::date(),
  3756. 'updated' => self::date()
  3757. ));
  3758. $this->assertEquals($expected, $result);
  3759. // Skip the rest of the transactional tests
  3760. return;
  3761. }
  3762. $this->assertEquals($expected, $result);
  3763. $data = array(
  3764. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3765. array('author_id' => 1, 'title' => ''),
  3766. array('author_id' => 1, 'title' => 'New Sixth Post')
  3767. );
  3768. $this->assertFalse($TestModel->saveAll($data));
  3769. $result = $TestModel->find('all', array('recursive' => -1));
  3770. $expected = array(
  3771. array('Post' => array(
  3772. 'id' => '1',
  3773. 'author_id' => 1,
  3774. 'title' => 'First Post',
  3775. 'body' => 'First Post Body',
  3776. 'published' => 'Y',
  3777. 'created' => '2007-03-18 10:39:23',
  3778. 'updated' => '2007-03-18 10:41:31'
  3779. )),
  3780. array('Post' => array(
  3781. 'id' => '2',
  3782. 'author_id' => 3,
  3783. 'title' => 'Second Post',
  3784. 'body' => 'Second Post Body',
  3785. 'published' => 'Y',
  3786. 'created' => '2007-03-18 10:41:23',
  3787. 'updated' => '2007-03-18 10:43:31'
  3788. )),
  3789. array('Post' => array(
  3790. 'id' => '3',
  3791. 'author_id' => 1,
  3792. 'title' => 'Third Post',
  3793. 'body' => 'Third Post Body',
  3794. 'published' => 'Y',
  3795. 'created' => '2007-03-18 10:43:23',
  3796. 'updated' => '2007-03-18 10:45:31'
  3797. )));
  3798. if (count($result) != 3) {
  3799. // Database doesn't support transactions
  3800. $expected[] = array(
  3801. 'Post' => array(
  3802. 'id' => '4',
  3803. 'author_id' => 1,
  3804. 'title' => 'New Fourth Post',
  3805. 'body' => 'Third Post Body',
  3806. 'published' => 'N',
  3807. 'created' => self::date(),
  3808. 'updated' => self::date()
  3809. ));
  3810. $expected[] = array(
  3811. 'Post' => array(
  3812. 'id' => '5',
  3813. 'author_id' => 1,
  3814. 'title' => 'Third Post',
  3815. 'body' => 'Third Post Body',
  3816. 'published' => 'N',
  3817. 'created' => self::date(),
  3818. 'updated' => self::date()
  3819. ));
  3820. }
  3821. $this->assertEquals($expected, $result);
  3822. $TestModel->validate = array('title' => 'notEmpty');
  3823. $data = array(
  3824. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3825. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3826. array('author_id' => 1, 'title' => 'New Sixth Post')
  3827. );
  3828. $this->assertTrue($TestModel->saveAll($data));
  3829. $result = $TestModel->find('all', array(
  3830. 'recursive' => -1,
  3831. 'fields' => array('author_id', 'title','body','published'),
  3832. 'order' => array('Post.created' => 'ASC')
  3833. ));
  3834. $expected = array(
  3835. array('Post' => array(
  3836. 'author_id' => 1,
  3837. 'title' => 'First Post',
  3838. 'body' => 'First Post Body',
  3839. 'published' => 'Y'
  3840. )),
  3841. array('Post' => array(
  3842. 'author_id' => 3,
  3843. 'title' => 'Second Post',
  3844. 'body' => 'Second Post Body',
  3845. 'published' => 'Y'
  3846. )),
  3847. array('Post' => array(
  3848. 'author_id' => 1,
  3849. 'title' => 'Third Post',
  3850. 'body' => 'Third Post Body',
  3851. 'published' => 'Y'
  3852. )),
  3853. array('Post' => array(
  3854. 'author_id' => 1,
  3855. 'title' => 'New Fourth Post',
  3856. 'body' => '',
  3857. 'published' => 'N'
  3858. )),
  3859. array('Post' => array(
  3860. 'author_id' => 1,
  3861. 'title' => 'New Fifth Post',
  3862. 'body' => '',
  3863. 'published' => 'N'
  3864. )),
  3865. array('Post' => array(
  3866. 'author_id' => 1,
  3867. 'title' => 'New Sixth Post',
  3868. 'body' => '',
  3869. 'published' => 'N'
  3870. )));
  3871. $this->assertEquals($expected, $result);
  3872. }
  3873. /**
  3874. * testSaveAllValidation method
  3875. *
  3876. * @return void
  3877. */
  3878. public function testSaveAllValidation() {
  3879. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3880. $TestModel = new Post();
  3881. $data = array(
  3882. array(
  3883. 'id' => '1',
  3884. 'title' => 'Baleeted First Post',
  3885. 'body' => 'Baleeted!',
  3886. 'published' => 'N'
  3887. ),
  3888. array(
  3889. 'id' => '2',
  3890. 'title' => 'Just update the title'
  3891. ),
  3892. array(
  3893. 'title' => 'Creating a fourth post',
  3894. 'body' => 'Fourth post body',
  3895. 'author_id' => 2
  3896. ));
  3897. $this->assertTrue($TestModel->saveAll($data));
  3898. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3899. $expected = array(
  3900. array(
  3901. 'Post' => array(
  3902. 'id' => '1',
  3903. 'author_id' => '1',
  3904. 'title' => 'Baleeted First Post',
  3905. 'body' => 'Baleeted!',
  3906. 'published' => 'N',
  3907. 'created' => '2007-03-18 10:39:23'
  3908. )),
  3909. array(
  3910. 'Post' => array(
  3911. 'id' => '2',
  3912. 'author_id' => '3',
  3913. 'title' => 'Just update the title',
  3914. 'body' => 'Second Post Body',
  3915. 'published' => 'Y',
  3916. 'created' => '2007-03-18 10:41:23'
  3917. )),
  3918. array(
  3919. 'Post' => array(
  3920. 'id' => '3',
  3921. 'author_id' => '1',
  3922. 'title' => 'Third Post',
  3923. 'body' => 'Third Post Body',
  3924. 'published' => 'Y',
  3925. 'created' => '2007-03-18 10:43:23',
  3926. 'updated' => '2007-03-18 10:45:31'
  3927. )),
  3928. array(
  3929. 'Post' => array(
  3930. 'id' => '4',
  3931. 'author_id' => '2',
  3932. 'title' => 'Creating a fourth post',
  3933. 'body' => 'Fourth post body',
  3934. 'published' => 'N'
  3935. )));
  3936. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  3937. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  3938. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  3939. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  3940. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  3941. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  3942. $this->assertEquals($expected, $result);
  3943. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3944. $data = array(
  3945. array(
  3946. 'id' => '1',
  3947. 'title' => 'Un-Baleeted First Post',
  3948. 'body' => 'Not Baleeted!',
  3949. 'published' => 'Y'
  3950. ),
  3951. array(
  3952. 'id' => '2',
  3953. 'title' => '',
  3954. 'body' => 'Trying to get away with an empty title'
  3955. ));
  3956. $result = $TestModel->saveAll($data);
  3957. $this->assertFalse($result);
  3958. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3959. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3960. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3961. if (!$transactionWorked) {
  3962. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3963. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3964. }
  3965. $this->assertEquals($errors, $TestModel->validationErrors);
  3966. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3967. $data = array(
  3968. array(
  3969. 'id' => '1',
  3970. 'title' => 'Un-Baleeted First Post',
  3971. 'body' => 'Not Baleeted!',
  3972. 'published' => 'Y'
  3973. ),
  3974. array(
  3975. 'id' => '2',
  3976. 'title' => '',
  3977. 'body' => 'Trying to get away with an empty title'
  3978. ));
  3979. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3980. $this->assertEquals(array(true, false), $result);
  3981. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3982. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3983. $expected = array(
  3984. array(
  3985. 'Post' => array(
  3986. 'id' => '1',
  3987. 'author_id' => '1',
  3988. 'title' => 'Un-Baleeted First Post',
  3989. 'body' => 'Not Baleeted!',
  3990. 'published' => 'Y',
  3991. 'created' => '2007-03-18 10:39:23'
  3992. )
  3993. ),
  3994. array(
  3995. 'Post' => array(
  3996. 'id' => '2',
  3997. 'author_id' => '3',
  3998. 'title' => 'Just update the title',
  3999. 'body' => 'Second Post Body',
  4000. 'published' => 'Y',
  4001. 'created' => '2007-03-18 10:41:23'
  4002. )
  4003. ),
  4004. array(
  4005. 'Post' => array(
  4006. 'id' => '3',
  4007. 'author_id' => '1',
  4008. 'title' => 'Third Post',
  4009. 'body' => 'Third Post Body',
  4010. 'published' => 'Y',
  4011. 'created' => '2007-03-18 10:43:23',
  4012. 'updated' => '2007-03-18 10:45:31'
  4013. )
  4014. ),
  4015. array(
  4016. 'Post' => array(
  4017. 'id' => '4',
  4018. 'author_id' => '2',
  4019. 'title' => 'Creating a fourth post',
  4020. 'body' => 'Fourth post body',
  4021. 'published' => 'N'
  4022. )
  4023. )
  4024. );
  4025. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4026. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4027. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4028. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4029. unset(
  4030. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4031. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4032. );
  4033. $this->assertEquals($expected, $result);
  4034. $this->assertEquals($errors, $TestModel->validationErrors);
  4035. $data = array(
  4036. array(
  4037. 'id' => '1',
  4038. 'title' => 'Re-Baleeted First Post',
  4039. 'body' => 'Baleeted!',
  4040. 'published' => 'N'
  4041. ),
  4042. array(
  4043. 'id' => '2',
  4044. 'title' => '',
  4045. 'body' => 'Trying to get away with an empty title'
  4046. ));
  4047. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  4048. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4049. unset(
  4050. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  4051. $result[3]['Post']['updated'], $result[3]['Post']['created']
  4052. );
  4053. $this->assertEquals($expected, $result);
  4054. $this->assertEquals($errors, $TestModel->validationErrors);
  4055. }
  4056. /**
  4057. * testSaveAllValidationOnly method
  4058. *
  4059. * @return void
  4060. */
  4061. public function testSaveAllValidationOnly() {
  4062. $this->loadFixtures('Comment', 'Attachment');
  4063. $TestModel = new Comment();
  4064. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4065. $data = array(
  4066. 'Comment' => array(
  4067. 'comment' => 'This is the comment'
  4068. ),
  4069. 'Attachment' => array(
  4070. 'attachment' => ''
  4071. )
  4072. );
  4073. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  4074. $this->assertFalse($result);
  4075. $TestModel = new Article();
  4076. $TestModel->validate = array('title' => 'notEmpty');
  4077. $result = $TestModel->saveAll(
  4078. array(
  4079. 0 => array('title' => ''),
  4080. 1 => array('title' => 'title 1'),
  4081. 2 => array('title' => 'title 2'),
  4082. ),
  4083. array('validate' => 'only')
  4084. );
  4085. $this->assertFalse($result);
  4086. $expected = array(
  4087. 0 => array('title' => array('This field cannot be left blank')),
  4088. );
  4089. $this->assertEquals($expected, $TestModel->validationErrors);
  4090. $result = $TestModel->saveAll(
  4091. array(
  4092. 0 => array('title' => 'title 0'),
  4093. 1 => array('title' => ''),
  4094. 2 => array('title' => 'title 2'),
  4095. ),
  4096. array('validate' => 'only')
  4097. );
  4098. $this->assertFalse($result);
  4099. $expected = array(
  4100. 1 => array('title' => array('This field cannot be left blank')),
  4101. );
  4102. $this->assertEquals($expected, $TestModel->validationErrors);
  4103. }
  4104. /**
  4105. * testSaveAllValidateFirst method
  4106. *
  4107. * @return void
  4108. */
  4109. public function testSaveAllValidateFirst() {
  4110. $this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
  4111. $model = new Article();
  4112. $model->deleteAll(true);
  4113. $model->Comment->validate = array('comment' => 'notEmpty');
  4114. $result = $model->saveAll(array(
  4115. 'Article' => array(
  4116. 'title' => 'Post with Author',
  4117. 'body' => 'This post will be saved author'
  4118. ),
  4119. 'Comment' => array(
  4120. array('comment' => 'First new comment'),
  4121. array('comment' => '')
  4122. )
  4123. ), array('validate' => 'first'));
  4124. $this->assertFalse($result);
  4125. $result = $model->find('all');
  4126. $this->assertEquals(array(), $result);
  4127. $expected = array('Comment' => array(
  4128. 1 => array('comment' => array('This field cannot be left blank'))
  4129. ));
  4130. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  4131. $this->assertSame($model->Comment->find('count'), 0);
  4132. $result = $model->saveAll(
  4133. array(
  4134. 'Article' => array(
  4135. 'title' => 'Post with Author',
  4136. 'body' => 'This post will be saved with an author',
  4137. 'user_id' => 2
  4138. ),
  4139. 'Comment' => array(
  4140. array(
  4141. 'comment' => 'Only new comment',
  4142. 'user_id' => 2
  4143. ))),
  4144. array('validate' => 'first')
  4145. );
  4146. $this->assertSame($result, true);
  4147. $result = $model->Comment->find('all');
  4148. $this->assertSame(count($result), 1);
  4149. $result = Hash::extract($result, '{n}.Comment.article_id');
  4150. $this->assertEquals(4, $result[0]);
  4151. $model->deleteAll(true);
  4152. $data = array(
  4153. 'Article' => array(
  4154. 'title' => 'Post with Author saveAlled from comment',
  4155. 'body' => 'This post will be saved with an author',
  4156. 'user_id' => 2
  4157. ),
  4158. 'Comment' => array(
  4159. 'comment' => 'Only new comment', 'user_id' => 2
  4160. ));
  4161. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  4162. $this->assertFalse(empty($result));
  4163. $result = $model->find('all');
  4164. $this->assertEquals(
  4165. $result[0]['Article']['title'],
  4166. 'Post with Author saveAlled from comment'
  4167. );
  4168. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  4169. }
  4170. /**
  4171. * test saveAll()'s return is correct when using atomic = false and validate = first.
  4172. *
  4173. * @return void
  4174. */
  4175. public function testSaveAllValidateFirstAtomicFalse() {
  4176. $this->loadFixtures('Something');
  4177. $Something = new Something();
  4178. $invalidData = array(
  4179. array(
  4180. 'title' => 'foo',
  4181. 'body' => 'bar',
  4182. 'published' => 'baz',
  4183. ),
  4184. array(
  4185. 'body' => 3,
  4186. 'published' => 'sd',
  4187. ),
  4188. );
  4189. $Something->create();
  4190. $Something->validate = array(
  4191. 'title' => array(
  4192. 'rule' => 'alphaNumeric',
  4193. 'required' => true,
  4194. ),
  4195. 'body' => array(
  4196. 'rule' => 'alphaNumeric',
  4197. 'required' => true,
  4198. 'allowEmpty' => true,
  4199. ),
  4200. );
  4201. $result = $Something->saveAll($invalidData, array(
  4202. 'atomic' => false,
  4203. 'validate' => 'first',
  4204. ));
  4205. $expected = array(true, false);
  4206. $this->assertEquals($expected, $result);
  4207. $Something = new Something();
  4208. $validData = array(
  4209. array(
  4210. 'title' => 'title value',
  4211. 'body' => 'body value',
  4212. 'published' => 'baz',
  4213. ),
  4214. array(
  4215. 'title' => 'valid',
  4216. 'body' => 'this body',
  4217. 'published' => 'sd',
  4218. ),
  4219. );
  4220. $Something->create();
  4221. $result = $Something->saveAll($validData, array(
  4222. 'atomic' => false,
  4223. 'validate' => 'first',
  4224. ));
  4225. $expected = array(true, true);
  4226. $this->assertEquals($expected, $result);
  4227. }
  4228. /**
  4229. * testSaveAllHasManyValidationOnly method
  4230. *
  4231. * @return void
  4232. */
  4233. public function testSaveAllHasManyValidationOnly() {
  4234. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4235. $TestModel = new Article();
  4236. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4237. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4238. $result = $TestModel->saveAll(
  4239. array(
  4240. 'Article' => array('id' => 2),
  4241. 'Comment' => array(
  4242. array(
  4243. 'id' => 1,
  4244. 'comment' => '',
  4245. 'published' => 'Y',
  4246. 'user_id' => 1),
  4247. array(
  4248. 'id' => 2,
  4249. 'comment' =>
  4250. 'comment',
  4251. 'published' => 'Y',
  4252. 'user_id' => 1
  4253. ))),
  4254. array('validate' => 'only')
  4255. );
  4256. $this->assertFalse($result);
  4257. $result = $TestModel->saveAll(
  4258. array(
  4259. 'Article' => array('id' => 2),
  4260. 'Comment' => array(
  4261. array(
  4262. 'id' => 1,
  4263. 'comment' => '',
  4264. 'published' => 'Y',
  4265. 'user_id' => 1
  4266. ),
  4267. array(
  4268. 'id' => 2,
  4269. 'comment' => 'comment',
  4270. 'published' => 'Y',
  4271. 'user_id' => 1
  4272. ),
  4273. array(
  4274. 'id' => 3,
  4275. 'comment' => '',
  4276. 'published' => 'Y',
  4277. 'user_id' => 1
  4278. ))),
  4279. array(
  4280. 'validate' => 'only',
  4281. 'atomic' => false
  4282. ));
  4283. $expected = array(
  4284. 'Article' => true,
  4285. 'Comment' => array(false, true, false)
  4286. );
  4287. $this->assertSame($expected, $result);
  4288. $expected = array('Comment' => array(
  4289. 0 => array('comment' => array('This field cannot be left blank')),
  4290. 2 => array('comment' => array('This field cannot be left blank'))
  4291. ));
  4292. $this->assertEquals($expected, $TestModel->validationErrors);
  4293. $expected = array(
  4294. 0 => array('comment' => array('This field cannot be left blank')),
  4295. 2 => array('comment' => array('This field cannot be left blank'))
  4296. );
  4297. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4298. }
  4299. /**
  4300. * test that saveAll behaves like plain save() when supplied empty data
  4301. *
  4302. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4303. * @return void
  4304. */
  4305. public function testSaveAllEmptyData() {
  4306. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4307. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4308. $model = new Article();
  4309. $result = $model->saveAll(array(), array('validate' => 'first'));
  4310. $this->assertFalse(empty($result));
  4311. $model = new ProductUpdateAll();
  4312. $result = $model->saveAll(array());
  4313. $this->assertFalse($result);
  4314. }
  4315. /**
  4316. * testSaveAssociated method
  4317. *
  4318. * @return void
  4319. */
  4320. public function testSaveAssociated() {
  4321. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  4322. $TestModel = new Post();
  4323. $result = $TestModel->find('all');
  4324. $this->assertEquals(3, count($result));
  4325. $this->assertFalse(isset($result[3]));
  4326. $TestModel->saveAssociated(array(
  4327. 'Post' => array(
  4328. 'title' => 'Post with Author',
  4329. 'body' => 'This post will be saved with an author'
  4330. ),
  4331. 'Author' => array(
  4332. 'user' => 'bob',
  4333. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  4334. )));
  4335. $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
  4336. $expected = array(
  4337. 'Post' => array(
  4338. 'id' => '4',
  4339. 'author_id' => '5',
  4340. 'title' => 'Post with Author',
  4341. 'body' => 'This post will be saved with an author',
  4342. 'published' => 'N'
  4343. ),
  4344. 'Author' => array(
  4345. 'id' => '5',
  4346. 'user' => 'bob',
  4347. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  4348. 'test' => 'working'
  4349. ));
  4350. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4351. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4352. $this->assertEquals(self::date(), $result[3]['Author']['created']);
  4353. $this->assertEquals(self::date(), $result[3]['Author']['updated']);
  4354. unset(
  4355. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  4356. $result[3]['Author']['updated'], $result[3]['Author']['created']
  4357. );
  4358. $this->assertEquals($expected, $result[3]);
  4359. $this->assertEquals(4, count($result));
  4360. $TestModel = new Comment();
  4361. $result = $TestModel->saveAssociated(array(
  4362. 'Comment' => array(
  4363. 'article_id' => 2,
  4364. 'user_id' => 2,
  4365. 'comment' => 'New comment with attachment',
  4366. 'published' => 'Y'
  4367. ),
  4368. 'Attachment' => array(
  4369. 'attachment' => 'some_file.tgz'
  4370. )));
  4371. $this->assertFalse(empty($result));
  4372. $result = $TestModel->find('all');
  4373. $expected = array(
  4374. 'id' => '7',
  4375. 'article_id' => '2',
  4376. 'user_id' => '2',
  4377. 'comment' => 'New comment with attachment',
  4378. 'published' => 'Y'
  4379. );
  4380. $this->assertEquals(self::date(), $result[6]['Comment']['updated']);
  4381. $this->assertEquals(self::date(), $result[6]['Comment']['created']);
  4382. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  4383. $this->assertEquals($expected, $result[6]['Comment']);
  4384. $expected = array(
  4385. 'id' => '2',
  4386. 'comment_id' => '7',
  4387. 'attachment' => 'some_file.tgz'
  4388. );
  4389. $this->assertEquals(self::date(), $result[6]['Attachment']['updated']);
  4390. $this->assertEquals(self::date(), $result[6]['Attachment']['created']);
  4391. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  4392. $this->assertEquals($expected, $result[6]['Attachment']);
  4393. }
  4394. /**
  4395. * testSaveMany method
  4396. *
  4397. * @return void
  4398. */
  4399. public function testSaveMany() {
  4400. $this->loadFixtures('Post');
  4401. $TestModel = new Post();
  4402. $TestModel->deleteAll(true);
  4403. $this->assertEquals(array(), $TestModel->find('all'));
  4404. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  4405. $this->db->truncate($TestModel);
  4406. $TestModel->saveMany(array(
  4407. array(
  4408. 'title' => 'Multi-record post 1',
  4409. 'body' => 'First multi-record post',
  4410. 'author_id' => 2
  4411. ),
  4412. array(
  4413. 'title' => 'Multi-record post 2',
  4414. 'body' => 'Second multi-record post',
  4415. 'author_id' => 2
  4416. )));
  4417. $result = $TestModel->find('all', array(
  4418. 'recursive' => -1,
  4419. 'order' => 'Post.id ASC'
  4420. ));
  4421. $expected = array(
  4422. array(
  4423. 'Post' => array(
  4424. 'id' => '1',
  4425. 'author_id' => '2',
  4426. 'title' => 'Multi-record post 1',
  4427. 'body' => 'First multi-record post',
  4428. 'published' => 'N'
  4429. )
  4430. ),
  4431. array(
  4432. 'Post' => array(
  4433. 'id' => '2',
  4434. 'author_id' => '2',
  4435. 'title' => 'Multi-record post 2',
  4436. 'body' => 'Second multi-record post',
  4437. 'published' => 'N'
  4438. )
  4439. )
  4440. );
  4441. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  4442. $this->assertEquals(self::date(), $result[0]['Post']['created']);
  4443. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  4444. $this->assertEquals(self::date(), $result[1]['Post']['created']);
  4445. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  4446. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  4447. $this->assertEquals($expected, $result);
  4448. }
  4449. /**
  4450. * Test SaveAssociated with Habtm relations
  4451. *
  4452. * @return void
  4453. */
  4454. public function testSaveAssociatedHabtm() {
  4455. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  4456. $data = array(
  4457. 'Article' => array(
  4458. 'user_id' => 1,
  4459. 'title' => 'Article Has and belongs to Many Tags'
  4460. ),
  4461. 'Tag' => array(
  4462. 'Tag' => array(1, 2)
  4463. ),
  4464. 'Comment' => array(
  4465. array(
  4466. 'comment' => 'Article comment',
  4467. 'user_id' => 1
  4468. )));
  4469. $Article = new Article();
  4470. $result = $Article->saveAssociated($data);
  4471. $this->assertFalse(empty($result));
  4472. $result = $Article->read();
  4473. $this->assertEquals(2, count($result['Tag']));
  4474. $this->assertEquals('tag1', $result['Tag'][0]['tag']);
  4475. $this->assertEquals(1, count($result['Comment']));
  4476. $this->assertEquals(1, count($result['Comment'][0]['comment']));
  4477. }
  4478. /**
  4479. * Test SaveAssociated with Habtm relations and extra join table fields
  4480. *
  4481. * @return void
  4482. */
  4483. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  4484. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  4485. $data = array(
  4486. 'Something' => array(
  4487. 'id' => 4,
  4488. 'title' => 'Extra Fields',
  4489. 'body' => 'Extra Fields Body',
  4490. 'published' => '1'
  4491. ),
  4492. 'SomethingElse' => array(
  4493. array('something_else_id' => 1, 'doomed' => '1'),
  4494. array('something_else_id' => 2, 'doomed' => '0'),
  4495. array('something_else_id' => 3, 'doomed' => '1')
  4496. )
  4497. );
  4498. $Something = new Something();
  4499. $result = $Something->saveAssociated($data);
  4500. $this->assertFalse(empty($result));
  4501. $result = $Something->read();
  4502. $this->assertEquals(3, count($result['SomethingElse']));
  4503. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  4504. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  4505. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  4506. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  4507. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  4508. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  4509. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  4510. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  4511. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  4512. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  4513. }
  4514. /**
  4515. * testSaveAssociatedHasOne method
  4516. *
  4517. * @return void
  4518. */
  4519. public function testSaveAssociatedHasOne() {
  4520. $model = new Comment();
  4521. $model->deleteAll(true);
  4522. $this->assertEquals(array(), $model->find('all'));
  4523. $model->Attachment->deleteAll(true);
  4524. $this->assertEquals(array(), $model->Attachment->find('all'));
  4525. $this->assertTrue($model->saveAssociated(array(
  4526. 'Comment' => array(
  4527. 'comment' => 'Comment with attachment',
  4528. 'article_id' => 1,
  4529. 'user_id' => 1
  4530. ),
  4531. 'Attachment' => array(
  4532. 'attachment' => 'some_file.zip'
  4533. ))));
  4534. $result = $model->find('all', array('fields' => array(
  4535. 'Comment.id', 'Comment.comment', 'Attachment.id',
  4536. 'Attachment.comment_id', 'Attachment.attachment'
  4537. )));
  4538. $expected = array(array(
  4539. 'Comment' => array(
  4540. 'id' => '1',
  4541. 'comment' => 'Comment with attachment'
  4542. ),
  4543. 'Attachment' => array(
  4544. 'id' => '1',
  4545. 'comment_id' => '1',
  4546. 'attachment' => 'some_file.zip'
  4547. )));
  4548. $this->assertEquals($expected, $result);
  4549. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  4550. $data = array(
  4551. 'Comment' => array(
  4552. 'comment' => 'Comment with attachment',
  4553. 'article_id' => 1,
  4554. 'user_id' => 1
  4555. ),
  4556. 'Attachment' => array(
  4557. 'attachment' => 'some_file.zip'
  4558. ));
  4559. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  4560. }
  4561. /**
  4562. * testSaveAssociatedBelongsTo method
  4563. *
  4564. * @return void
  4565. */
  4566. public function testSaveAssociatedBelongsTo() {
  4567. $model = new Comment();
  4568. $model->deleteAll(true);
  4569. $this->assertEquals(array(), $model->find('all'));
  4570. $model->Article->deleteAll(true);
  4571. $this->assertEquals(array(), $model->Article->find('all'));
  4572. $this->assertTrue($model->saveAssociated(array(
  4573. 'Comment' => array(
  4574. 'comment' => 'Article comment',
  4575. 'article_id' => 1,
  4576. 'user_id' => 1
  4577. ),
  4578. 'Article' => array(
  4579. 'title' => 'Model Associations 101',
  4580. 'user_id' => 1
  4581. ))));
  4582. $result = $model->find('all', array('fields' => array(
  4583. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  4584. )));
  4585. $expected = array(array(
  4586. 'Comment' => array(
  4587. 'id' => '1',
  4588. 'article_id' => '1',
  4589. 'comment' => 'Article comment'
  4590. ),
  4591. 'Article' => array(
  4592. 'id' => '1',
  4593. 'title' => 'Model Associations 101'
  4594. )));
  4595. $this->assertEquals($expected, $result);
  4596. }
  4597. /**
  4598. * testSaveAssociatedHasOneValidation method
  4599. *
  4600. * @return void
  4601. */
  4602. public function testSaveAssociatedHasOneValidation() {
  4603. $model = new Comment();
  4604. $model->deleteAll(true);
  4605. $this->assertEquals(array(), $model->find('all'));
  4606. $model->Attachment->deleteAll(true);
  4607. $this->assertEquals(array(), $model->Attachment->find('all'));
  4608. $model->validate = array('comment' => 'notEmpty');
  4609. $model->Attachment->validate = array('attachment' => 'notEmpty');
  4610. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  4611. $result = $model->saveAssociated(
  4612. array(
  4613. 'Comment' => array(
  4614. 'comment' => '',
  4615. 'article_id' => 1,
  4616. 'user_id' => 1
  4617. ),
  4618. 'Attachment' => array('attachment' => '')
  4619. )
  4620. );
  4621. $this->assertFalse($result);
  4622. $expected = array(
  4623. 'comment' => array(
  4624. 'This field cannot be left blank'
  4625. ),
  4626. 'Attachment' => array(
  4627. 'attachment' => array(
  4628. 'This field cannot be left blank'
  4629. )
  4630. )
  4631. );
  4632. $this->assertEquals($expected, $model->validationErrors);
  4633. $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
  4634. }
  4635. /**
  4636. * testSaveAssociatedAtomic method
  4637. *
  4638. * @return void
  4639. */
  4640. public function testSaveAssociatedAtomic() {
  4641. $this->loadFixtures('Article', 'User');
  4642. $TestModel = new Article();
  4643. $result = $TestModel->saveAssociated(array(
  4644. 'Article' => array(
  4645. 'title' => 'Post with Author',
  4646. 'body' => 'This post will be saved with an author',
  4647. 'user_id' => 2
  4648. ),
  4649. 'Comment' => array(
  4650. array('comment' => 'First new comment', 'user_id' => 2))
  4651. ), array('atomic' => false));
  4652. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  4653. $result = $TestModel->saveAssociated(array(
  4654. 'Article' => array('id' => 2),
  4655. 'Comment' => array(
  4656. array(
  4657. 'comment' => 'First new comment',
  4658. 'published' => 'Y',
  4659. 'user_id' => 1
  4660. ),
  4661. array(
  4662. 'comment' => 'Second new comment',
  4663. 'published' => 'Y',
  4664. 'user_id' => 2
  4665. ))
  4666. ), array('validate' => true, 'atomic' => false));
  4667. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  4668. }
  4669. /**
  4670. * testSaveManyAtomic method
  4671. *
  4672. * @return void
  4673. */
  4674. public function testSaveManyAtomic() {
  4675. $this->loadFixtures('Article', 'User');
  4676. $TestModel = new Article();
  4677. $result = $TestModel->saveMany(array(
  4678. array(
  4679. 'id' => '1',
  4680. 'title' => 'Baleeted First Post',
  4681. 'body' => 'Baleeted!',
  4682. 'published' => 'N'
  4683. ),
  4684. array(
  4685. 'id' => '2',
  4686. 'title' => 'Just update the title'
  4687. ),
  4688. array(
  4689. 'title' => 'Creating a fourth post',
  4690. 'body' => 'Fourth post body',
  4691. 'user_id' => 2
  4692. )
  4693. ), array('atomic' => false));
  4694. $this->assertSame($result, array(true, true, true));
  4695. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4696. $result = $TestModel->saveMany(array(
  4697. array(
  4698. 'id' => '1',
  4699. 'title' => 'Un-Baleeted First Post',
  4700. 'body' => 'Not Baleeted!',
  4701. 'published' => 'Y'
  4702. ),
  4703. array(
  4704. 'id' => '2',
  4705. 'title' => '',
  4706. 'body' => 'Trying to get away with an empty title'
  4707. )
  4708. ), array('validate' => true, 'atomic' => false));
  4709. $this->assertSame(array(true, false), $result);
  4710. }
  4711. /**
  4712. * testSaveAssociatedHasMany method
  4713. *
  4714. * @return void
  4715. */
  4716. public function testSaveAssociatedHasMany() {
  4717. $this->loadFixtures('Article', 'Comment');
  4718. $TestModel = new Article();
  4719. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4720. $result = $TestModel->saveAssociated(array(
  4721. 'Article' => array('id' => 2),
  4722. 'Comment' => array(
  4723. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  4724. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  4725. )
  4726. ));
  4727. $this->assertFalse(empty($result));
  4728. $result = $TestModel->findById(2);
  4729. $expected = array(
  4730. 'First Comment for Second Article',
  4731. 'Second Comment for Second Article',
  4732. 'First new comment',
  4733. 'Second new comment'
  4734. );
  4735. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4736. $result = $TestModel->saveAssociated(
  4737. array(
  4738. 'Article' => array('id' => 2),
  4739. 'Comment' => array(
  4740. array(
  4741. 'comment' => 'Third new comment',
  4742. 'published' => 'Y',
  4743. 'user_id' => 1
  4744. ))),
  4745. array('atomic' => false)
  4746. );
  4747. $this->assertFalse(empty($result));
  4748. $result = $TestModel->findById(2);
  4749. $expected = array(
  4750. 'First Comment for Second Article',
  4751. 'Second Comment for Second Article',
  4752. 'First new comment',
  4753. 'Second new comment',
  4754. 'Third new comment'
  4755. );
  4756. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4757. $TestModel->beforeSaveReturn = false;
  4758. $result = $TestModel->saveAssociated(
  4759. array(
  4760. 'Article' => array('id' => 2),
  4761. 'Comment' => array(
  4762. array(
  4763. 'comment' => 'Fourth new comment',
  4764. 'published' => 'Y',
  4765. 'user_id' => 1
  4766. ))),
  4767. array('atomic' => false)
  4768. );
  4769. $this->assertEquals(array('Article' => false), $result);
  4770. $result = $TestModel->findById(2);
  4771. $expected = array(
  4772. 'First Comment for Second Article',
  4773. 'Second Comment for Second Article',
  4774. 'First new comment',
  4775. 'Second new comment',
  4776. 'Third new comment'
  4777. );
  4778. $this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
  4779. }
  4780. /**
  4781. * testSaveAssociatedHasManyEmpty method
  4782. *
  4783. * @return void
  4784. */
  4785. public function testSaveAssociatedHasManyEmpty() {
  4786. $this->loadFixtures('Article', 'Comment');
  4787. $TestModel = new Article();
  4788. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4789. $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  4790. //empty hasMany data is ignored in save
  4791. $result = $TestModel->saveAssociated(array(
  4792. 'Article' => array('title' => 'title', 'user_id' => 1),
  4793. 'Comment' => array()
  4794. ), array('validate' => true));
  4795. $this->assertTrue($result);
  4796. $result = $TestModel->saveAssociated(array(
  4797. 'Article' => array('title' => 'title', 'user_id' => 1),
  4798. 'Comment' => array()
  4799. ), array('validate' => true, 'atomic' => false));
  4800. $this->assertEquals(array('Article' => true), $result);
  4801. //empty primary data is not ignored
  4802. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
  4803. $this->assertFalse($result);
  4804. $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
  4805. $this->assertEquals(array('Article' => false), $result);
  4806. }
  4807. /**
  4808. * testSaveAssociatedHasManyValidation method
  4809. *
  4810. * @return void
  4811. */
  4812. public function testSaveAssociatedHasManyValidation() {
  4813. $this->loadFixtures('Article', 'Comment');
  4814. $TestModel = new Article();
  4815. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4816. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4817. $result = $TestModel->saveAssociated(array(
  4818. 'Article' => array('id' => 2),
  4819. 'Comment' => array(
  4820. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  4821. )
  4822. ), array('validate' => true));
  4823. $this->assertFalse($result);
  4824. $expected = array('Comment' => array(
  4825. array('comment' => array('This field cannot be left blank'))
  4826. ));
  4827. $this->assertEquals($expected, $TestModel->validationErrors);
  4828. $expected = array(
  4829. array('comment' => array('This field cannot be left blank'))
  4830. );
  4831. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  4832. $result = $TestModel->saveAssociated(array(
  4833. 'Article' => array('id' => 2),
  4834. 'Comment' => array(
  4835. array(
  4836. 'comment' => '',
  4837. 'published' => 'Y',
  4838. 'user_id' => 1
  4839. ))
  4840. ), array('validate' => 'first'));
  4841. $this->assertFalse($result);
  4842. }
  4843. /**
  4844. * test saveMany with transactions and ensure there is no missing rollback.
  4845. *
  4846. * @return void
  4847. */
  4848. public function testSaveManyTransactionNoRollback() {
  4849. $this->loadFixtures('Post');
  4850. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  4851. $db = ConnectionManager::create('mock_many_transaction', array(
  4852. 'datasource' => 'MockManyTransactionDboSource',
  4853. ));
  4854. $db->expects($this->once())
  4855. ->method('describe')
  4856. ->will($this->returnValue(array()));
  4857. $db->expects($this->once())->method('rollback');
  4858. $Post = new Post('mock_many_transaction');
  4859. $Post->validate = array(
  4860. 'title' => array('rule' => array('notEmpty'))
  4861. );
  4862. $data = array(
  4863. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4864. array('author_id' => 1, 'title' => '')
  4865. );
  4866. $Post->saveMany($data);
  4867. }
  4868. /**
  4869. * test saveAssociated with transactions and ensure there is no missing rollback.
  4870. *
  4871. * @return void
  4872. */
  4873. public function testSaveAssociatedTransactionNoRollback() {
  4874. $testDb = ConnectionManager::getDataSource('test');
  4875. $mock = $this->getMock(
  4876. 'DboSource',
  4877. array('connect', 'rollback', 'describe', 'create', 'begin'),
  4878. array(),
  4879. 'MockAssociatedTransactionDboSource',
  4880. false
  4881. );
  4882. $db = ConnectionManager::create('mock_assoc_transaction', array(
  4883. 'datasource' => 'MockAssociatedTransactionDboSource',
  4884. ));
  4885. $this->mockObjects[] = $db;
  4886. $db->columns = $testDb->columns;
  4887. $db->expects($this->once())->method('rollback');
  4888. $db->expects($this->any())->method('describe')
  4889. ->will($this->returnValue(array(
  4890. 'id' => array('type' => 'integer', 'length' => 11),
  4891. 'title' => array('type' => 'string'),
  4892. 'body' => array('type' => 'text'),
  4893. 'published' => array('type' => 'string')
  4894. )));
  4895. $Post = new Post();
  4896. $Post->useDbConfig = 'mock_assoc_transaction';
  4897. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  4898. $Post->Author->validate = array(
  4899. 'user' => array('rule' => array('notEmpty'))
  4900. );
  4901. $data = array(
  4902. 'Post' => array(
  4903. 'title' => 'New post',
  4904. 'body' => 'Content',
  4905. 'published' => 'Y'
  4906. ),
  4907. 'Author' => array(
  4908. 'user' => '',
  4909. 'password' => "sekret"
  4910. )
  4911. );
  4912. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  4913. }
  4914. /**
  4915. * test saveMany with nested saveMany call.
  4916. *
  4917. * @return void
  4918. */
  4919. public function testSaveManyNestedSaveMany() {
  4920. $this->loadFixtures('Sample');
  4921. $TransactionManyTestModel = new TransactionManyTestModel();
  4922. $data = array(
  4923. array('apple_id' => 1, 'name' => 'sample5'),
  4924. );
  4925. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  4926. }
  4927. /**
  4928. * testSaveManyTransaction method
  4929. *
  4930. * @return void
  4931. */
  4932. public function testSaveManyTransaction() {
  4933. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4934. $TestModel = new Post();
  4935. $TestModel->validate = array('title' => 'notEmpty');
  4936. $data = array(
  4937. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4938. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4939. array('author_id' => 1, 'title' => '')
  4940. );
  4941. $this->assertFalse($TestModel->saveMany($data));
  4942. $result = $TestModel->find('all', array('recursive' => -1));
  4943. $expected = array(
  4944. array('Post' => array(
  4945. 'id' => '1',
  4946. 'author_id' => 1,
  4947. 'title' => 'First Post',
  4948. 'body' => 'First Post Body',
  4949. 'published' => 'Y',
  4950. 'created' => '2007-03-18 10:39:23',
  4951. 'updated' => '2007-03-18 10:41:31'
  4952. )),
  4953. array('Post' => array(
  4954. 'id' => '2',
  4955. 'author_id' => 3,
  4956. 'title' => 'Second Post',
  4957. 'body' => 'Second Post Body',
  4958. 'published' => 'Y',
  4959. 'created' => '2007-03-18 10:41:23',
  4960. 'updated' => '2007-03-18 10:43:31'
  4961. )),
  4962. array('Post' => array(
  4963. 'id' => '3',
  4964. 'author_id' => 1,
  4965. 'title' => 'Third Post',
  4966. 'body' => 'Third Post Body',
  4967. 'published' => 'Y',
  4968. 'created' => '2007-03-18 10:43:23',
  4969. 'updated' => '2007-03-18 10:45:31'
  4970. )));
  4971. if (count($result) != 3) {
  4972. // Database doesn't support transactions
  4973. $expected[] = array(
  4974. 'Post' => array(
  4975. 'id' => '4',
  4976. 'author_id' => 1,
  4977. 'title' => 'New Fourth Post',
  4978. 'body' => null,
  4979. 'published' => 'N'
  4980. ));
  4981. $expected[] = array(
  4982. 'Post' => array(
  4983. 'id' => '5',
  4984. 'author_id' => 1,
  4985. 'title' => 'New Fifth Post',
  4986. 'body' => null,
  4987. 'published' => 'N',
  4988. ));
  4989. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  4990. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  4991. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  4992. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  4993. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4994. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4995. $this->assertEquals($expected, $result);
  4996. // Skip the rest of the transactional tests
  4997. return;
  4998. }
  4999. $this->assertEquals($expected, $result);
  5000. $data = array(
  5001. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5002. array('author_id' => 1, 'title' => ''),
  5003. array('author_id' => 1, 'title' => 'New Sixth Post')
  5004. );
  5005. $this->assertFalse($TestModel->saveMany($data));
  5006. $result = $TestModel->find('all', array('recursive' => -1));
  5007. $expected = array(
  5008. array('Post' => array(
  5009. 'id' => '1',
  5010. 'author_id' => 1,
  5011. 'title' => 'First Post',
  5012. 'body' => 'First Post Body',
  5013. 'published' => 'Y',
  5014. 'created' => '2007-03-18 10:39:23',
  5015. 'updated' => '2007-03-18 10:41:31'
  5016. )),
  5017. array('Post' => array(
  5018. 'id' => '2',
  5019. 'author_id' => 3,
  5020. 'title' => 'Second Post',
  5021. 'body' => 'Second Post Body',
  5022. 'published' => 'Y',
  5023. 'created' => '2007-03-18 10:41:23',
  5024. 'updated' => '2007-03-18 10:43:31'
  5025. )),
  5026. array('Post' => array(
  5027. 'id' => '3',
  5028. 'author_id' => 1,
  5029. 'title' => 'Third Post',
  5030. 'body' => 'Third Post Body',
  5031. 'published' => 'Y',
  5032. 'created' => '2007-03-18 10:43:23',
  5033. 'updated' => '2007-03-18 10:45:31'
  5034. )));
  5035. if (count($result) != 3) {
  5036. // Database doesn't support transactions
  5037. $expected[] = array(
  5038. 'Post' => array(
  5039. 'id' => '4',
  5040. 'author_id' => 1,
  5041. 'title' => 'New Fourth Post',
  5042. 'body' => 'Third Post Body',
  5043. 'published' => 'N'
  5044. ));
  5045. $expected[] = array(
  5046. 'Post' => array(
  5047. 'id' => '5',
  5048. 'author_id' => 1,
  5049. 'title' => 'Third Post',
  5050. 'body' => 'Third Post Body',
  5051. 'published' => 'N'
  5052. ));
  5053. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5054. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5055. $this->assertEquals(self::date(), $result[4]['Post']['created']);
  5056. $this->assertEquals(self::date(), $result[4]['Post']['updated']);
  5057. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5058. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  5059. }
  5060. $this->assertEquals($expected, $result);
  5061. $TestModel->validate = array('title' => 'notEmpty');
  5062. $data = array(
  5063. array('author_id' => 1, 'title' => 'New Fourth Post'),
  5064. array('author_id' => 1, 'title' => 'New Fifth Post'),
  5065. array('author_id' => 1, 'title' => 'New Sixth Post')
  5066. );
  5067. $this->assertTrue($TestModel->saveMany($data));
  5068. $result = $TestModel->find('all', array(
  5069. 'recursive' => -1,
  5070. 'fields' => array('author_id', 'title','body','published'),
  5071. 'order' => array('Post.created' => 'ASC')
  5072. ));
  5073. $expected = array(
  5074. array('Post' => array(
  5075. 'author_id' => 1,
  5076. 'title' => 'First Post',
  5077. 'body' => 'First Post Body',
  5078. 'published' => 'Y'
  5079. )),
  5080. array('Post' => array(
  5081. 'author_id' => 3,
  5082. 'title' => 'Second Post',
  5083. 'body' => 'Second Post Body',
  5084. 'published' => 'Y'
  5085. )),
  5086. array('Post' => array(
  5087. 'author_id' => 1,
  5088. 'title' => 'Third Post',
  5089. 'body' => 'Third Post Body',
  5090. 'published' => 'Y'
  5091. )),
  5092. array('Post' => array(
  5093. 'author_id' => 1,
  5094. 'title' => 'New Fourth Post',
  5095. 'body' => '',
  5096. 'published' => 'N'
  5097. )),
  5098. array('Post' => array(
  5099. 'author_id' => 1,
  5100. 'title' => 'New Fifth Post',
  5101. 'body' => '',
  5102. 'published' => 'N'
  5103. )),
  5104. array('Post' => array(
  5105. 'author_id' => 1,
  5106. 'title' => 'New Sixth Post',
  5107. 'body' => '',
  5108. 'published' => 'N'
  5109. )));
  5110. $this->assertEquals($expected, $result);
  5111. }
  5112. /**
  5113. * testSaveManyValidation method
  5114. *
  5115. * @return void
  5116. */
  5117. public function testSaveManyValidation() {
  5118. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5119. $TestModel = new Post();
  5120. $data = array(
  5121. array(
  5122. 'id' => '1',
  5123. 'title' => 'Baleeted First Post',
  5124. 'body' => 'Baleeted!',
  5125. 'published' => 'N'
  5126. ),
  5127. array(
  5128. 'id' => '2',
  5129. 'title' => 'Just update the title'
  5130. ),
  5131. array(
  5132. 'title' => 'Creating a fourth post',
  5133. 'body' => 'Fourth post body',
  5134. 'author_id' => 2
  5135. ));
  5136. $this->assertTrue($TestModel->saveMany($data));
  5137. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5138. $expected = array(
  5139. array(
  5140. 'Post' => array(
  5141. 'id' => '1',
  5142. 'author_id' => '1',
  5143. 'title' => 'Baleeted First Post',
  5144. 'body' => 'Baleeted!',
  5145. 'published' => 'N',
  5146. 'created' => '2007-03-18 10:39:23'
  5147. )
  5148. ),
  5149. array(
  5150. 'Post' => array(
  5151. 'id' => '2',
  5152. 'author_id' => '3',
  5153. 'title' => 'Just update the title',
  5154. 'body' => 'Second Post Body',
  5155. 'published' => 'Y',
  5156. 'created' => '2007-03-18 10:41:23'
  5157. )
  5158. ),
  5159. array(
  5160. 'Post' => array(
  5161. 'id' => '3',
  5162. 'author_id' => '1',
  5163. 'title' => 'Third Post',
  5164. 'body' => 'Third Post Body',
  5165. 'published' => 'Y',
  5166. 'created' => '2007-03-18 10:43:23',
  5167. 'updated' => '2007-03-18 10:45:31'
  5168. )),
  5169. array(
  5170. 'Post' => array(
  5171. 'id' => '4',
  5172. 'author_id' => '2',
  5173. 'title' => 'Creating a fourth post',
  5174. 'body' => 'Fourth post body',
  5175. 'published' => 'N'
  5176. )
  5177. )
  5178. );
  5179. $this->assertEquals(self::date(), $result[0]['Post']['updated']);
  5180. $this->assertEquals(self::date(), $result[1]['Post']['updated']);
  5181. $this->assertEquals(self::date(), $result[3]['Post']['created']);
  5182. $this->assertEquals(self::date(), $result[3]['Post']['updated']);
  5183. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  5184. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  5185. $this->assertEquals($expected, $result);
  5186. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5187. $data = array(
  5188. array(
  5189. 'id' => '1',
  5190. 'title' => 'Un-Baleeted First Post',
  5191. 'body' => 'Not Baleeted!',
  5192. 'published' => 'Y'
  5193. ),
  5194. array(
  5195. 'id' => '2',
  5196. 'title' => '',
  5197. 'body' => 'Trying to get away with an empty title'
  5198. ));
  5199. $result = $TestModel->saveMany($data);
  5200. $this->assertFalse($result);
  5201. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  5202. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5203. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  5204. if (!$transactionWorked) {
  5205. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  5206. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  5207. }
  5208. $this->assertEquals($errors, $TestModel->validationErrors);
  5209. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  5210. $data = array(
  5211. array(
  5212. 'id' => '1',
  5213. 'title' => 'Un-Baleeted First Post',
  5214. 'body' => 'Not Baleeted!',
  5215. 'published' => 'Y'
  5216. ),
  5217. array(
  5218. 'id' => '2',
  5219. 'title' => '',
  5220. 'body' => 'Trying to get away with an empty title'
  5221. ));
  5222. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  5223. $this->assertEquals(array(true, false), $result);
  5224. $result = $TestModel->find('all', array(
  5225. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5226. 'recursive' => -1,
  5227. 'order' => 'Post.id ASC'
  5228. ));
  5229. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  5230. $expected = array(
  5231. array(
  5232. 'Post' => array(
  5233. 'id' => '1',
  5234. 'author_id' => '1',
  5235. 'title' => 'Un-Baleeted First Post',
  5236. 'body' => 'Not Baleeted!',
  5237. 'published' => 'Y',
  5238. )),
  5239. array(
  5240. 'Post' => array(
  5241. 'id' => '2',
  5242. 'author_id' => '3',
  5243. 'title' => 'Just update the title',
  5244. 'body' => 'Second Post Body',
  5245. 'published' => 'Y',
  5246. )),
  5247. array(
  5248. 'Post' => array(
  5249. 'id' => '3',
  5250. 'author_id' => '1',
  5251. 'title' => 'Third Post',
  5252. 'body' => 'Third Post Body',
  5253. 'published' => 'Y',
  5254. )),
  5255. array(
  5256. 'Post' => array(
  5257. 'id' => '4',
  5258. 'author_id' => '2',
  5259. 'title' => 'Creating a fourth post',
  5260. 'body' => 'Fourth post body',
  5261. 'published' => 'N',
  5262. )));
  5263. $this->assertEquals($expected, $result);
  5264. $this->assertEquals($errors, $TestModel->validationErrors);
  5265. $data = array(
  5266. array(
  5267. 'id' => '1',
  5268. 'title' => 'Re-Baleeted First Post',
  5269. 'body' => 'Baleeted!',
  5270. 'published' => 'N'
  5271. ),
  5272. array(
  5273. 'id' => '2',
  5274. 'title' => '',
  5275. 'body' => 'Trying to get away with an empty title'
  5276. ));
  5277. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  5278. $result = $TestModel->find('all', array(
  5279. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  5280. 'recursive' => -1,
  5281. 'order' => 'Post.id ASC'
  5282. ));
  5283. $this->assertEquals($expected, $result);
  5284. $this->assertEquals($errors, $TestModel->validationErrors);
  5285. }
  5286. /**
  5287. * testValidateMany method
  5288. *
  5289. * @return void
  5290. */
  5291. public function testValidateMany() {
  5292. $TestModel = new Article();
  5293. $TestModel->validate = array('title' => 'notEmpty');
  5294. $data = array(
  5295. 0 => array('title' => ''),
  5296. 1 => array('title' => 'title 1'),
  5297. 2 => array('title' => 'title 2'),
  5298. );
  5299. $result = $TestModel->validateMany($data);
  5300. $this->assertFalse($result);
  5301. $expected = array(
  5302. 0 => array('title' => array('This field cannot be left blank')),
  5303. );
  5304. $this->assertEquals($expected, $TestModel->validationErrors);
  5305. $data = array(
  5306. 0 => array('title' => 'title 0'),
  5307. 1 => array('title' => ''),
  5308. 2 => array('title' => 'title 2'),
  5309. );
  5310. $result = $TestModel->validateMany($data);
  5311. $this->assertFalse($result);
  5312. $expected = array(
  5313. 1 => array('title' => array('This field cannot be left blank')),
  5314. );
  5315. $this->assertEquals($expected, $TestModel->validationErrors);
  5316. }
  5317. /**
  5318. * testSaveAssociatedValidateFirst method
  5319. *
  5320. * @return void
  5321. */
  5322. public function testSaveAssociatedValidateFirst() {
  5323. $this->loadFixtures('Article', 'Comment', 'Attachment');
  5324. $model = new Article();
  5325. $model->deleteAll(true);
  5326. $model->Comment->validate = array('comment' => 'notEmpty');
  5327. $result = $model->saveAssociated(array(
  5328. 'Article' => array(
  5329. 'title' => 'Post with Author',
  5330. 'body' => 'This post will be saved author'
  5331. ),
  5332. 'Comment' => array(
  5333. array('comment' => 'First new comment'),
  5334. array('comment' => '')
  5335. )
  5336. ), array('validate' => 'first'));
  5337. $this->assertFalse($result);
  5338. $result = $model->find('all');
  5339. $this->assertEquals(array(), $result);
  5340. $expected = array('Comment' => array(
  5341. 1 => array('comment' => array('This field cannot be left blank'))
  5342. ));
  5343. $this->assertEquals($expected['Comment'], $model->Comment->validationErrors);
  5344. $this->assertSame($model->Comment->find('count'), 0);
  5345. $result = $model->saveAssociated(
  5346. array(
  5347. 'Article' => array(
  5348. 'title' => 'Post with Author',
  5349. 'body' => 'This post will be saved with an author',
  5350. 'user_id' => 2
  5351. ),
  5352. 'Comment' => array(
  5353. array(
  5354. 'comment' => 'Only new comment',
  5355. 'user_id' => 2
  5356. ))),
  5357. array('validate' => 'first')
  5358. );
  5359. $this->assertSame($result, true);
  5360. $result = $model->Comment->find('all');
  5361. $this->assertSame(count($result), 1);
  5362. $result = Hash::extract($result, '{n}.Comment.article_id');
  5363. $this->assertEquals(4, $result[0]);
  5364. $model->deleteAll(true);
  5365. $data = array(
  5366. 'Article' => array(
  5367. 'title' => 'Post with Author saveAlled from comment',
  5368. 'body' => 'This post will be saved with an author',
  5369. 'user_id' => 2
  5370. ),
  5371. 'Comment' => array(
  5372. 'comment' => 'Only new comment', 'user_id' => 2
  5373. ));
  5374. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  5375. $this->assertFalse(empty($result));
  5376. $result = $model->find('all');
  5377. $this->assertEquals(
  5378. 'Post with Author saveAlled from comment',
  5379. $result[0]['Article']['title']
  5380. );
  5381. $this->assertEquals('Only new comment', $result[0]['Comment'][0]['comment']);
  5382. }
  5383. /**
  5384. * test saveMany()'s return is correct when using atomic = false and validate = first.
  5385. *
  5386. * @return void
  5387. */
  5388. public function testSaveManyValidateFirstAtomicFalse() {
  5389. $Something = new Something();
  5390. $invalidData = array(
  5391. array(
  5392. 'title' => 'foo',
  5393. 'body' => 'bar',
  5394. 'published' => 'baz',
  5395. ),
  5396. array(
  5397. 'body' => 3,
  5398. 'published' => 'sd',
  5399. ),
  5400. );
  5401. $Something->create();
  5402. $Something->validate = array(
  5403. 'title' => array(
  5404. 'rule' => 'alphaNumeric',
  5405. 'required' => true,
  5406. ),
  5407. 'body' => array(
  5408. 'rule' => 'alphaNumeric',
  5409. 'required' => true,
  5410. 'allowEmpty' => true,
  5411. ),
  5412. );
  5413. $result = $Something->saveMany($invalidData, array(
  5414. 'atomic' => false,
  5415. 'validate' => 'first',
  5416. ));
  5417. $expected = array(true, false);
  5418. $this->assertEquals($expected, $result);
  5419. $Something = new Something();
  5420. $validData = array(
  5421. array(
  5422. 'title' => 'title value',
  5423. 'body' => 'body value',
  5424. 'published' => 'baz',
  5425. ),
  5426. array(
  5427. 'title' => 'valid',
  5428. 'body' => 'this body',
  5429. 'published' => 'sd',
  5430. ),
  5431. );
  5432. $Something->create();
  5433. $result = $Something->saveMany($validData, array(
  5434. 'atomic' => false,
  5435. 'validate' => 'first',
  5436. ));
  5437. $expected = array(true, true);
  5438. $this->assertEquals($expected, $result);
  5439. }
  5440. /**
  5441. * testValidateAssociated method
  5442. *
  5443. * @return void
  5444. */
  5445. public function testValidateAssociated() {
  5446. $this->loadFixtures('Attachment', 'Article', 'Comment');
  5447. $TestModel = new Comment();
  5448. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5449. $data = array(
  5450. 'Comment' => array(
  5451. 'comment' => 'This is the comment'
  5452. ),
  5453. 'Attachment' => array(
  5454. 'attachment' => ''
  5455. )
  5456. );
  5457. $result = $TestModel->validateAssociated($data);
  5458. $this->assertFalse($result);
  5459. $TestModel = new Article();
  5460. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5461. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  5462. $data = array(
  5463. 'Article' => array('id' => 2),
  5464. 'Comment' => array(
  5465. array(
  5466. 'id' => 1,
  5467. 'comment' => '',
  5468. 'published' => 'Y',
  5469. 'user_id' => 1),
  5470. array(
  5471. 'id' => 2,
  5472. 'comment' =>
  5473. 'comment',
  5474. 'published' => 'Y',
  5475. 'user_id' => 1
  5476. )));
  5477. $result = $TestModel->validateAssociated($data);
  5478. $this->assertFalse($result);
  5479. $data = array(
  5480. 'Article' => array('id' => 2),
  5481. 'Comment' => array(
  5482. array(
  5483. 'id' => 1,
  5484. 'comment' => '',
  5485. 'published' => 'Y',
  5486. 'user_id' => 1
  5487. ),
  5488. array(
  5489. 'id' => 2,
  5490. 'comment' => 'comment',
  5491. 'published' => 'Y',
  5492. 'user_id' => 1
  5493. ),
  5494. array(
  5495. 'id' => 3,
  5496. 'comment' => '',
  5497. 'published' => 'Y',
  5498. 'user_id' => 1
  5499. )));
  5500. $result = $TestModel->validateAssociated($data, array(
  5501. 'atomic' => false
  5502. ));
  5503. $expected = array(
  5504. 'Article' => true,
  5505. 'Comment' => array(false, true, false)
  5506. );
  5507. $this->assertSame($expected, $result);
  5508. $expected = array('Comment' => array(
  5509. 0 => array('comment' => array('This field cannot be left blank')),
  5510. 2 => array('comment' => array('This field cannot be left blank'))
  5511. ));
  5512. $this->assertEquals($expected, $TestModel->validationErrors);
  5513. $expected = array(
  5514. 0 => array('comment' => array('This field cannot be left blank')),
  5515. 2 => array('comment' => array('This field cannot be left blank'))
  5516. );
  5517. $this->assertEquals($expected, $TestModel->Comment->validationErrors);
  5518. }
  5519. /**
  5520. * test that saveMany behaves like plain save() when suplied empty data
  5521. *
  5522. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5523. * @return void
  5524. */
  5525. public function testSaveManyEmptyData() {
  5526. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5527. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5528. $model = new Article();
  5529. $result = $model->saveMany(array(), array('validate' => true));
  5530. $this->assertFalse(empty($result));
  5531. $model = new ProductUpdateAll();
  5532. $result = $model->saveMany(array());
  5533. $this->assertFalse($result);
  5534. }
  5535. /**
  5536. * test that saveAssociated behaves like plain save() when supplied empty data
  5537. *
  5538. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  5539. * @return void
  5540. */
  5541. public function testSaveAssociatedEmptyData() {
  5542. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  5543. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  5544. $model = new Article();
  5545. $result = $model->saveAssociated(array(), array('validate' => true));
  5546. $this->assertFalse(empty($result));
  5547. $model = new ProductUpdateAll();
  5548. $result = $model->saveAssociated(array());
  5549. $this->assertFalse($result);
  5550. }
  5551. /**
  5552. * testUpdateWithCalculation method
  5553. *
  5554. * @return void
  5555. */
  5556. public function testUpdateWithCalculation() {
  5557. $this->loadFixtures('DataTest');
  5558. $model = new DataTest();
  5559. $model->deleteAll(true);
  5560. $result = $model->saveMany(array(
  5561. array('count' => 5, 'float' => 1.1),
  5562. array('count' => 3, 'float' => 1.2),
  5563. array('count' => 4, 'float' => 1.3),
  5564. array('count' => 1, 'float' => 2.0),
  5565. ));
  5566. $this->assertFalse(empty($result));
  5567. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5568. $this->assertEquals(array(5, 3, 4, 1), $result);
  5569. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  5570. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5571. $this->assertEquals(array(7, 5, 6, 3), $result);
  5572. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  5573. $result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
  5574. $this->assertEquals(array(6, 4, 5, 2), $result);
  5575. }
  5576. public function testToggleBoolFields() {
  5577. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  5578. $Post = new CounterCachePost();
  5579. $Post->unbindModel(array('belongsTo' => array('User')), true);
  5580. $true = array('Post' => array('published' => true, 'id' => 2));
  5581. $false = array('Post' => array('published' => false, 'id' => 2));
  5582. $fields = array('Post.published', 'Post.id');
  5583. $updateConditions = array('Post.id' => 2);
  5584. // check its true
  5585. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5586. $this->assertEquals($true, $result);
  5587. // Testing without the alias
  5588. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5589. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5590. $this->assertEquals($false, $result);
  5591. $this->assertTrue($Post->updateAll(array('published' => 'NOT published'), $updateConditions));
  5592. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5593. $this->assertEquals($true, $result);
  5594. $db = ConnectionManager::getDataSource('test');
  5595. $alias = $db->name('Post.published');
  5596. // Testing with the alias
  5597. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5598. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5599. $this->assertEquals($false, $result);
  5600. $this->assertTrue($Post->updateAll(array('Post.published' => "NOT $alias"), $updateConditions));
  5601. $result = $Post->find('first', array('conditions' => $updateConditions, 'fields' => $fields));
  5602. $this->assertEquals($true, $result);
  5603. }
  5604. /**
  5605. * TestFindAllWithoutForeignKey
  5606. *
  5607. * @return void
  5608. */
  5609. public function testFindAllForeignKey() {
  5610. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5611. $ProductUpdateAll = new ProductUpdateAll();
  5612. $conditions = array('Group.name' => 'group one');
  5613. $ProductUpdateAll->bindModel(array(
  5614. 'belongsTo' => array(
  5615. 'Group' => array('className' => 'GroupUpdateAll')
  5616. )
  5617. ));
  5618. $ProductUpdateAll->belongsTo = array(
  5619. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  5620. );
  5621. $results = $ProductUpdateAll->find('all', compact('conditions'));
  5622. $this->assertTrue(!empty($results));
  5623. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  5624. $ProductUpdateAll->belongsTo = array(
  5625. 'Group' => array(
  5626. 'className' => 'GroupUpdateAll',
  5627. 'foreignKey' => false,
  5628. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5629. ));
  5630. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  5631. $this->assertTrue(!empty($resultsFkFalse));
  5632. $expected = array(
  5633. '0' => array(
  5634. 'ProductUpdateAll' => array(
  5635. 'id' => 1,
  5636. 'name' => 'product one',
  5637. 'groupcode' => 120,
  5638. 'group_id' => 1),
  5639. 'Group' => array(
  5640. 'id' => 1,
  5641. 'name' => 'group one',
  5642. 'code' => 120)
  5643. ),
  5644. '1' => array(
  5645. 'ProductUpdateAll' => array(
  5646. 'id' => 2,
  5647. 'name' => 'product two',
  5648. 'groupcode' => 120,
  5649. 'group_id' => 1),
  5650. 'Group' => array(
  5651. 'id' => 1,
  5652. 'name' => 'group one',
  5653. 'code' => 120)
  5654. )
  5655. );
  5656. $this->assertEquals($expected, $results);
  5657. $this->assertEquals($expected, $resultsFkFalse);
  5658. }
  5659. /**
  5660. * test updateAll with empty values.
  5661. *
  5662. * @return void
  5663. */
  5664. public function testUpdateAllEmptyValues() {
  5665. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  5666. $this->loadFixtures('Author', 'Post');
  5667. $model = new Author();
  5668. $result = $model->updateAll(array('user' => '""'));
  5669. $this->assertTrue($result);
  5670. }
  5671. /**
  5672. * testUpdateAllWithJoins
  5673. *
  5674. * @return void
  5675. */
  5676. public function testUpdateAllWithJoins() {
  5677. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  5678. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5679. $ProductUpdateAll = new ProductUpdateAll();
  5680. $conditions = array('Group.name' => 'group one');
  5681. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5682. 'Group' => array('className' => 'GroupUpdateAll')))
  5683. );
  5684. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5685. $results = $ProductUpdateAll->find('all', array(
  5686. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  5687. ));
  5688. $expected = array(
  5689. '0' => array(
  5690. 'ProductUpdateAll' => array(
  5691. 'id' => 1,
  5692. 'name' => 'new product',
  5693. 'groupcode' => 120,
  5694. 'group_id' => 1),
  5695. 'Group' => array(
  5696. 'id' => 1,
  5697. 'name' => 'group one',
  5698. 'code' => 120)
  5699. ),
  5700. '1' => array(
  5701. 'ProductUpdateAll' => array(
  5702. 'id' => 2,
  5703. 'name' => 'new product',
  5704. 'groupcode' => 120,
  5705. 'group_id' => 1),
  5706. 'Group' => array(
  5707. 'id' => 1,
  5708. 'name' => 'group one',
  5709. 'code' => 120)));
  5710. $this->assertEquals($expected, $results);
  5711. }
  5712. /**
  5713. * testUpdateAllWithoutForeignKey
  5714. *
  5715. * @return void
  5716. */
  5717. public function testUpdateAllWithoutForeignKey() {
  5718. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  5719. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  5720. $ProductUpdateAll = new ProductUpdateAll();
  5721. $conditions = array('Group.name' => 'group one');
  5722. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  5723. 'Group' => array('className' => 'GroupUpdateAll')
  5724. )));
  5725. $ProductUpdateAll->belongsTo = array(
  5726. 'Group' => array(
  5727. 'className' => 'GroupUpdateAll',
  5728. 'foreignKey' => false,
  5729. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  5730. )
  5731. );
  5732. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  5733. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  5734. $expected = array(
  5735. '0' => array(
  5736. 'ProductUpdateAll' => array(
  5737. 'id' => 1,
  5738. 'name' => 'new product',
  5739. 'groupcode' => 120,
  5740. 'group_id' => 1),
  5741. 'Group' => array(
  5742. 'id' => 1,
  5743. 'name' => 'group one',
  5744. 'code' => 120)
  5745. ),
  5746. '1' => array(
  5747. 'ProductUpdateAll' => array(
  5748. 'id' => 2,
  5749. 'name' => 'new product',
  5750. 'groupcode' => 120,
  5751. 'group_id' => 1),
  5752. 'Group' => array(
  5753. 'id' => 1,
  5754. 'name' => 'group one',
  5755. 'code' => 120)));
  5756. $this->assertEquals($expected, $resultsFkFalse);
  5757. }
  5758. /**
  5759. * test writing floats in german locale.
  5760. *
  5761. * @return void
  5762. */
  5763. public function testWriteFloatAsGerman() {
  5764. $restore = setlocale(LC_NUMERIC, 0);
  5765. setlocale(LC_NUMERIC, 'de_DE');
  5766. $model = new DataTest();
  5767. $result = $model->save(array(
  5768. 'count' => 1,
  5769. 'float' => 3.14593
  5770. ));
  5771. $this->assertTrue((bool)$result);
  5772. setlocale(LC_NUMERIC, $restore);
  5773. }
  5774. /**
  5775. * Test returned array contains primary key when save creates a new record
  5776. *
  5777. * @return void
  5778. */
  5779. public function testPkInReturnArrayForCreate() {
  5780. $this->loadFixtures('Article');
  5781. $TestModel = new Article();
  5782. $data = array('Article' => array(
  5783. 'user_id' => '1',
  5784. 'title' => 'Fourth Article',
  5785. 'body' => 'Fourth Article Body',
  5786. 'published' => 'Y'
  5787. ));
  5788. $result = $TestModel->save($data);
  5789. $this->assertSame($result['Article']['id'], $TestModel->id);
  5790. }
  5791. /**
  5792. * testSaveAllFieldListValidateBelongsTo
  5793. *
  5794. * @return void
  5795. */
  5796. public function testSaveAllFieldListValidateBelongsTo() {
  5797. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  5798. $TestModel = new Post();
  5799. $result = $TestModel->find('all');
  5800. $this->assertEquals(3, count($result));
  5801. $this->assertFalse(isset($result[3]));
  5802. // test belongsTo
  5803. $fieldList = array(
  5804. 'Post' => array('title', 'author_id'),
  5805. 'Author' => array('user')
  5806. );
  5807. $TestModel->saveAll(array(
  5808. 'Post' => array(
  5809. 'title' => 'Post without body',
  5810. 'body' => 'This will not be saved',
  5811. ),
  5812. 'Author' => array(
  5813. 'user' => 'bob',
  5814. 'test' => 'This will not be saved',
  5815. )), array('fieldList' => $fieldList));
  5816. $result = $TestModel->find('all');
  5817. $expected = array(
  5818. 'Post' => array (
  5819. 'id' => '4',
  5820. 'author_id' => '5',
  5821. 'title' => 'Post without body',
  5822. 'body' => null,
  5823. 'published' => 'N',
  5824. 'created' => self::date(),
  5825. 'updated' => self::date(),
  5826. ),
  5827. 'Author' => array (
  5828. 'id' => '5',
  5829. 'user' => 'bob',
  5830. 'password' => null,
  5831. 'created' => self::date(),
  5832. 'updated' => self::date(),
  5833. 'test' => 'working',
  5834. ),
  5835. );
  5836. $this->assertEquals($expected, $result[3]);
  5837. $this->assertEquals(4, count($result));
  5838. $this->assertEquals('', $result[3]['Post']['body']);
  5839. $this->assertEquals('working', $result[3]['Author']['test']);
  5840. // test multirecord
  5841. $this->db->truncate($TestModel);
  5842. $fieldList = array('title', 'author_id');
  5843. $TestModel->saveAll(array(
  5844. array(
  5845. 'title' => 'Multi-record post 1',
  5846. 'body' => 'First multi-record post',
  5847. 'author_id' => 2
  5848. ),
  5849. array(
  5850. 'title' => 'Multi-record post 2',
  5851. 'body' => 'Second multi-record post',
  5852. 'author_id' => 2
  5853. )), array('fieldList' => $fieldList));
  5854. $result = $TestModel->find('all', array(
  5855. 'recursive' => -1,
  5856. 'order' => 'Post.id ASC'
  5857. ));
  5858. $expected = array(
  5859. array(
  5860. 'Post' => array(
  5861. 'id' => '1',
  5862. 'author_id' => '2',
  5863. 'title' => 'Multi-record post 1',
  5864. 'body' => '',
  5865. 'published' => 'N',
  5866. 'created' => self::date(),
  5867. 'updated' => self::date()
  5868. )
  5869. ),
  5870. array(
  5871. 'Post' => array(
  5872. 'id' => '2',
  5873. 'author_id' => '2',
  5874. 'title' => 'Multi-record post 2',
  5875. 'body' => '',
  5876. 'published' => 'N',
  5877. 'created' => self::date(),
  5878. 'updated' => self::date()
  5879. )
  5880. )
  5881. );
  5882. $this->assertEquals($expected, $result);
  5883. }
  5884. /**
  5885. * testSaveAllFieldListHasMany method
  5886. *
  5887. * return @void
  5888. */
  5889. public function testSaveAllFieldListHasMany() {
  5890. $this->loadFixtures('Article', 'Comment');
  5891. $TestModel = new Article();
  5892. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5893. $this->db->truncate($TestModel);
  5894. $this->db->truncate(new Comment());
  5895. $fieldList = array(
  5896. 'Article' => array('id'),
  5897. 'Comment' => array('article_id', 'user_id')
  5898. );
  5899. $result = $TestModel->saveAll(array(
  5900. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  5901. 'Comment' => array(
  5902. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  5903. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  5904. )
  5905. ), array('fieldList' => $fieldList));
  5906. $result = $TestModel->find('all');
  5907. $this->assertEquals('', $result[0]['Article']['title']);
  5908. $this->assertEquals('', $result[0]['Comment'][0]['comment']);
  5909. $this->assertEquals('', $result[0]['Comment'][1]['comment']);
  5910. }
  5911. /**
  5912. * testSaveAllFieldListHasOne method
  5913. *
  5914. * @return void
  5915. */
  5916. public function testSaveAllFieldListHasOne() {
  5917. $this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
  5918. $TestModel = new Comment();
  5919. $TestModel->validate = array('comment' => 'notEmpty');
  5920. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  5921. $record = array(
  5922. 'Comment' => array(
  5923. 'user_id' => 1,
  5924. 'article_id' => 1,
  5925. 'comment' => '',
  5926. ),
  5927. 'Attachment' => array(
  5928. 'attachment' => ''
  5929. )
  5930. );
  5931. $result = $TestModel->saveAll($record, array('validate' => 'only'));
  5932. $this->assertFalse($result);
  5933. $fieldList = array(
  5934. 'Comment' => array('id', 'article_id', 'user_id'),
  5935. 'Attachment' => array('comment_id')
  5936. );
  5937. $result = $TestModel->saveAll($record, array(
  5938. 'fieldList' => $fieldList, 'validate' => 'only'
  5939. ));
  5940. $this->assertTrue($result);
  5941. $this->assertEmpty($TestModel->validationErrors);
  5942. }
  5943. /**
  5944. * testSaveAllDeepFieldListValidateBelongsTo
  5945. *
  5946. * @return void
  5947. */
  5948. public function testSaveAllDeepFieldListValidateBelongsTo() {
  5949. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  5950. $TestModel = new Post();
  5951. $TestModel->Author->bindModel(array('hasMany' => array('Comment' => array('foreignKey' => 'user_id'))), false);
  5952. $TestModel->recursive = 2;
  5953. $result = $TestModel->find('all');
  5954. $this->assertEquals(3, count($result));
  5955. $this->assertFalse(isset($result[3]));
  5956. // test belongsTo
  5957. $fieldList = array(
  5958. 'Post' => array('title', 'author_id'),
  5959. 'Author' => array('user'),
  5960. 'Comment' => array('comment')
  5961. );
  5962. $TestModel->saveAll(array(
  5963. 'Post' => array(
  5964. 'title' => 'Post without body',
  5965. 'body' => 'This will not be saved',
  5966. ),
  5967. 'Author' => array(
  5968. 'user' => 'bob',
  5969. 'test' => 'This will not be saved',
  5970. 'Comment' => array(
  5971. array('id' => 5, 'comment' => 'I am still published', 'published' => 'N'))
  5972. )), array('fieldList' => $fieldList, 'deep' => true));
  5973. $result = $TestModel->Author->Comment->find('first', array(
  5974. 'conditions' => array('Comment.id' => 5),
  5975. 'fields' => array('comment', 'published')
  5976. ));
  5977. $expected = array(
  5978. 'Comment' => array(
  5979. 'comment' => 'I am still published',
  5980. 'published' => 'Y'
  5981. )
  5982. );
  5983. $this->assertEquals($expected, $result);
  5984. }
  5985. /**
  5986. * testSaveAllDeepFieldListHasMany method
  5987. *
  5988. * return @void
  5989. */
  5990. public function testSaveAllDeepFieldListHasMany() {
  5991. $this->loadFixtures('Article', 'Comment', 'User');
  5992. $TestModel = new Article();
  5993. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  5994. $this->db->truncate($TestModel);
  5995. $this->db->truncate(new Comment());
  5996. $fieldList = array(
  5997. 'Article' => array('id'),
  5998. 'Comment' => array('article_id', 'user_id'),
  5999. 'User' => array('user')
  6000. );
  6001. $result = $TestModel->saveAll(array(
  6002. 'Article' => array('id' => 2, 'title' => 'I will not save'),
  6003. 'Comment' => array(
  6004. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6005. array(
  6006. 'comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2,
  6007. 'User' => array('user' => 'nopassword', 'password' => 'not saved')
  6008. )
  6009. )
  6010. ), array('fieldList' => $fieldList, 'deep' => true));
  6011. $result = $TestModel->Comment->User->find('first', array(
  6012. 'conditions' => array('User.user' => 'nopassword'),
  6013. 'fields' => array('user', 'password')
  6014. ));
  6015. $expected = array(
  6016. 'User' => array(
  6017. 'user' => 'nopassword',
  6018. 'password' => ''
  6019. )
  6020. );
  6021. $this->assertEquals($expected, $result);
  6022. }
  6023. /**
  6024. * testSaveAllDeepHasManyBelongsTo method
  6025. *
  6026. * return @void
  6027. */
  6028. public function testSaveAllDeepHasManyBelongsTo() {
  6029. $this->loadFixtures('Article', 'Comment', 'User');
  6030. $TestModel = new Article();
  6031. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  6032. $this->db->truncate($TestModel);
  6033. $this->db->truncate(new Comment());
  6034. $result = $TestModel->saveAll(array(
  6035. 'Article' => array('id' => 2, 'title' => 'The title'),
  6036. 'Comment' => array(
  6037. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6038. array(
  6039. 'comment' => 'belongsto', 'published' => 'Y',
  6040. 'User' => array('user' => 'findme', 'password' => 'somepass')
  6041. )
  6042. )
  6043. ), array('deep' => true));
  6044. $result = $TestModel->Comment->User->find('first', array(
  6045. 'conditions' => array('User.user' => 'findme'),
  6046. 'fields' => array('id', 'user', 'password')
  6047. ));
  6048. $expected = array(
  6049. 'User' => array(
  6050. 'id' => 5,
  6051. 'user' => 'findme',
  6052. 'password' => 'somepass',
  6053. )
  6054. );
  6055. $this->assertEquals($expected, $result);
  6056. $result = $TestModel->Comment->find('first', array(
  6057. 'conditions' => array('Comment.user_id' => 5),
  6058. 'fields' => array('id', 'comment', 'published', 'user_id')
  6059. ));
  6060. $expected = array(
  6061. 'Comment' => array(
  6062. 'id' => 2,
  6063. 'comment' => 'belongsto',
  6064. 'published' => 'Y',
  6065. 'user_id' => 5
  6066. )
  6067. );
  6068. $this->assertEquals($expected, $result);
  6069. }
  6070. /**
  6071. * testSaveAllDeepHasManyhasMany method
  6072. *
  6073. * return @void
  6074. */
  6075. public function testSaveAllDeepHasManyHasMany() {
  6076. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6077. $TestModel = new Article();
  6078. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6079. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6080. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6081. $this->db->truncate($TestModel);
  6082. $this->db->truncate(new Comment());
  6083. $this->db->truncate(new Attachment());
  6084. $result = $TestModel->saveAll(array(
  6085. 'Article' => array('id' => 2, 'title' => 'The title'),
  6086. 'Comment' => array(
  6087. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  6088. array(
  6089. 'comment' => 'hasmany', 'published' => 'Y', 'user_id' => 5,
  6090. 'Attachment' => array(
  6091. array('attachment' => 'first deep attachment'),
  6092. array('attachment' => 'second deep attachment'),
  6093. )
  6094. )
  6095. )
  6096. ), array('deep' => true));
  6097. $result = $TestModel->Comment->find('first', array(
  6098. 'conditions' => array('Comment.comment' => 'hasmany'),
  6099. 'fields' => array('id', 'comment', 'published', 'user_id'),
  6100. 'recursive' => -1
  6101. ));
  6102. $expected = array(
  6103. 'Comment' => array(
  6104. 'id' => 2,
  6105. 'comment' => 'hasmany',
  6106. 'published' => 'Y',
  6107. 'user_id' => 5
  6108. )
  6109. );
  6110. $this->assertEquals($expected, $result);
  6111. $result = $TestModel->Comment->Attachment->find('all', array(
  6112. 'fields' => array('attachment', 'comment_id'),
  6113. 'order' => array('Attachment.id' => 'ASC')
  6114. ));
  6115. $expected = array(
  6116. array('Attachment' => array('attachment' => 'first deep attachment', 'comment_id' => 2)),
  6117. array('Attachment' => array('attachment' => 'second deep attachment', 'comment_id' => 2)),
  6118. );
  6119. $this->assertEquals($expected, $result);
  6120. }
  6121. /**
  6122. * testSaveAllDeepOrderHasManyHasMany method
  6123. *
  6124. * return @void
  6125. */
  6126. public function testSaveAllDeepOrderHasManyHasMany() {
  6127. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6128. $TestModel = new Article();
  6129. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6130. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6131. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6132. $this->db->truncate($TestModel);
  6133. $this->db->truncate(new Comment());
  6134. $this->db->truncate(new Attachment());
  6135. $result = $TestModel->saveAll(array(
  6136. 'Article' => array('id' => 2, 'title' => 'Comment has its data after Attachment'),
  6137. 'Comment' => array(
  6138. array(
  6139. 'Attachment' => array(
  6140. array('attachment' => 'attachment should be created with comment_id'),
  6141. array('attachment' => 'comment should be created with article_id'),
  6142. ),
  6143. 'comment' => 'after associated data',
  6144. 'user_id' => 1
  6145. )
  6146. )
  6147. ), array('deep' => true));
  6148. $result = $TestModel->Comment->find('first', array(
  6149. 'conditions' => array('Comment.article_id' => 2),
  6150. ));
  6151. $this->assertEquals(2, $result['Comment']['article_id']);
  6152. $this->assertEquals(2, count($result['Attachment']));
  6153. }
  6154. /**
  6155. * testSaveAllDeepEmptyHasManyHasMany method
  6156. *
  6157. * return @void
  6158. */
  6159. public function testSaveAllDeepEmptyHasManyHasMany() {
  6160. $this->skipIf(!$this->db instanceof Mysql, 'This test is only compatible with Mysql.');
  6161. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
  6162. $TestModel = new Article();
  6163. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->Comment->belongsTo = array();
  6164. $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')), false);
  6165. $TestModel->Comment->bindModel(array('hasMany' => array('Attachment')), false);
  6166. $this->db->truncate($TestModel);
  6167. $this->db->truncate(new Comment());
  6168. $this->db->truncate(new Attachment());
  6169. $result = $TestModel->saveAll(array(
  6170. 'Article' => array('id' => 3, 'title' => 'Comment has no data'),
  6171. 'Comment' => array(
  6172. array(
  6173. 'Attachment' => array(
  6174. array('attachment' => 'attachment should be created with comment_id'),
  6175. array('attachment' => 'comment should be created with article_id'),
  6176. ),
  6177. )
  6178. )
  6179. ), array('deep' => true));
  6180. $result = $TestModel->Comment->find('first', array(
  6181. 'conditions' => array('Comment.article_id' => 3),
  6182. ));
  6183. $this->assertEquals(3, $result['Comment']['article_id']);
  6184. $this->assertEquals(2, count($result['Attachment']));
  6185. }
  6186. /**
  6187. * testUpdateAllBoolean
  6188. *
  6189. * return @void
  6190. */
  6191. public function testUpdateAllBoolean() {
  6192. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6193. $TestModel = new Item();
  6194. $result = $TestModel->updateAll(array('published' => true));
  6195. $this->assertTrue($result);
  6196. $result = $TestModel->find('first', array('fields' => array('id', 'published')));
  6197. $this->assertEquals(true, $result['Item']['published']);
  6198. }
  6199. /**
  6200. * testUpdateAllBooleanConditions
  6201. *
  6202. * return @void
  6203. */
  6204. public function testUpdateAllBooleanConditions() {
  6205. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6206. $TestModel = new Item();
  6207. $result = $TestModel->updateAll(array('published' => true), array('Item.id' => 1));
  6208. $this->assertTrue($result);
  6209. $result = $TestModel->find('first', array(
  6210. 'fields' => array('id', 'published'),
  6211. 'conditions' => array('Item.id' => 1)));
  6212. $this->assertEquals(true, $result['Item']['published']);
  6213. }
  6214. /**
  6215. * testUpdateBoolean
  6216. *
  6217. * return @void
  6218. */
  6219. public function testUpdateBoolean() {
  6220. $this->loadFixtures('Item', 'Syfile', 'Portfolio', 'Image', 'ItemsPortfolio');
  6221. $TestModel = new Item();
  6222. $result = $TestModel->save(array('published' => true, 'id' => 1));
  6223. $this->assertTrue((boolean)$result);
  6224. $result = $TestModel->find('first', array(
  6225. 'fields' => array('id', 'published'),
  6226. 'conditions' => array('Item.id' => 1)));
  6227. $this->assertEquals(true, $result['Item']['published']);
  6228. }
  6229. }