PageRenderTime 74ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 5499 lines | 4459 code | 529 blank | 511 comment | 27 complexity | fc6cf55090f458839ee9496a05a687bf MD5 | raw file
  1. <?php
  2. /**
  3. * ModelWriteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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($result, array('JoinAsJoinB' => $data));
  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($result['JoinAsJoinB']['other'], $updatedValue);
  76. }
  77. /**
  78. * testSaveDateAsFirstEntry method
  79. *
  80. * @return void
  81. */
  82. public function testSaveDateAsFirstEntry() {
  83. $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
  84. $Article = new Article();
  85. $data = array(
  86. 'Article' => array(
  87. 'created' => array(
  88. 'day' => '1',
  89. 'month' => '1',
  90. 'year' => '2008'
  91. ),
  92. 'title' => 'Test Title',
  93. 'user_id' => 1
  94. ));
  95. $Article->create();
  96. $result = $Article->save($data);
  97. $this->assertFalse(empty($result));
  98. $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
  99. $this->assertEquals($testResult['Article']['title'], $data['Article']['title']);
  100. $this->assertEquals($testResult['Article']['created'], '2008-01-01 00:00:00');
  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($currentCount, 3);
  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($currentCount, 4);
  123. }
  124. /**
  125. * testAutoSaveUuid method
  126. *
  127. * @return void
  128. */
  129. public function testAutoSaveUuid() {
  130. // SQLite does not support non-integer primary keys
  131. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  132. $this->loadFixtures('Uuid');
  133. $TestModel = new Uuid();
  134. $TestModel->save(array('title' => 'Test record'));
  135. $result = $TestModel->findByTitle('Test record');
  136. $this->assertEquals(
  137. array_keys($result['Uuid']),
  138. array('id', 'title', 'count', 'created', 'updated')
  139. );
  140. $this->assertEquals(strlen($result['Uuid']['id']), 36);
  141. }
  142. /**
  143. * Ensure that if the id key is null but present the save doesn't fail (with an
  144. * x sql error: "Column id specified twice")
  145. *
  146. * @return void
  147. */
  148. public function testSaveUuidNull() {
  149. // SQLite does not support non-integer primary keys
  150. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  151. $this->loadFixtures('Uuid');
  152. $TestModel = new Uuid();
  153. $TestModel->save(array('title' => 'Test record', 'id' => null));
  154. $result = $TestModel->findByTitle('Test record');
  155. $this->assertEquals(
  156. array_keys($result['Uuid']),
  157. array('id', 'title', 'count', 'created', 'updated')
  158. );
  159. $this->assertEquals(strlen($result['Uuid']['id']), 36);
  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($result['DataTest']['count'], 0);
  174. $this->assertEquals($result['DataTest']['float'], 0);
  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($TestModel->data, $expected);
  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($result['Syfile']['item_count'], 2);
  245. $TestModel2->delete(1);
  246. $result = $TestModel->findById(1);
  247. $this->assertEquals($result['Syfile']['item_count'], 1);
  248. $TestModel2->id = 2;
  249. $TestModel2->saveField('syfile_id', 1);
  250. $result = $TestModel->findById(1);
  251. $this->assertEquals($result['Syfile']['item_count'], 2);
  252. $result = $TestModel->findById(2);
  253. $this->assertEquals($result['Syfile']['item_count'], 0);
  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($users[0]['User']['post_count'], 1);
  313. $this->assertEquals($users[1]['User']['post_count'], 2);
  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($users[0]['User']['post_count'], 1);
  336. $this->assertEquals($users[1]['User']['post_count'], 2);
  337. }
  338. /**
  339. * test Counter Cache With Self Joining table
  340. *
  341. * @return void
  342. */
  343. public function testCounterCacheWithSelfJoin() {
  344. $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
  345. $this->loadFixtures('CategoryThread');
  346. $column = 'COLUMN ';
  347. if ($this->db instanceof Sqlserver) {
  348. $column = '';
  349. }
  350. $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
  351. $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . ' ADD ' . $column);
  352. $this->db->flushMethodCache();
  353. $Category = new CategoryThread();
  354. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  355. $this->assertFalse(empty($result));
  356. $Category = new CategoryThread();
  357. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  358. $Category->updateCounterCache(array('parent_id' => 5));
  359. $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  360. $expected = array(1);
  361. $this->assertEquals($expected, $result);
  362. }
  363. /**
  364. * testSaveWithCounterCacheScope method
  365. *
  366. * @return void
  367. */
  368. public function testSaveWithCounterCacheScope() {
  369. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  370. $TestModel = new Syfile();
  371. $TestModel2 = new Item();
  372. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  373. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  374. $result = $TestModel->findById(1);
  375. $this->assertSame($result['Syfile']['item_count'], null);
  376. $TestModel2->save(array(
  377. 'name' => 'Item 7',
  378. 'syfile_id' => 1,
  379. 'published' => true
  380. ));
  381. $result = $TestModel->findById(1);
  382. $this->assertEquals($result['Syfile']['item_count'], 1);
  383. $TestModel2->id = 1;
  384. $TestModel2->saveField('published', true);
  385. $result = $TestModel->findById(1);
  386. $this->assertEquals($result['Syfile']['item_count'], 2);
  387. $TestModel2->save(array(
  388. 'id' => 1,
  389. 'syfile_id' => 1,
  390. 'published' => false
  391. ));
  392. $result = $TestModel->findById(1);
  393. $this->assertEquals($result['Syfile']['item_count'], 1);
  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. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  536. $TestModel->id = 1;
  537. $result = $TestModel->saveField('title', '', true);
  538. $this->assertFalse($result);
  539. $this->loadFixtures('Node', 'Dependency');
  540. $Node = new Node();
  541. $Node->set('id', 1);
  542. $result = $Node->read();
  543. $this->assertEquals(Set::extract('/ParentNode/name', $result), array('Second'));
  544. $Node->saveField('state', 10);
  545. $result = $Node->read();
  546. $this->assertEquals(Set::extract('/ParentNode/name', $result), array('Second'));
  547. }
  548. /**
  549. * testSaveWithCreate method
  550. *
  551. * @return void
  552. */
  553. public function testSaveWithCreate() {
  554. $this->loadFixtures(
  555. 'User',
  556. 'Article',
  557. 'User',
  558. 'Comment',
  559. 'Tag',
  560. 'ArticlesTag',
  561. 'Attachment'
  562. );
  563. $TestModel = new User();
  564. $data = array('User' => array(
  565. 'user' => 'user',
  566. 'password' => ''
  567. ));
  568. $result = $TestModel->save($data);
  569. $this->assertFalse($result);
  570. $this->assertTrue(!empty($TestModel->validationErrors));
  571. $TestModel = new Article();
  572. $data = array('Article' => array(
  573. 'user_id' => '',
  574. 'title' => '',
  575. 'body' => ''
  576. ));
  577. $result = $TestModel->create($data) && $TestModel->save();
  578. $this->assertFalse($result);
  579. $this->assertTrue(!empty($TestModel->validationErrors));
  580. $data = array('Article' => array(
  581. 'id' => 1,
  582. 'user_id' => '1',
  583. 'title' => 'New First Article',
  584. 'body' => ''
  585. ));
  586. $result = $TestModel->create($data) && $TestModel->save();
  587. $this->assertFalse($result);
  588. $data = array('Article' => array(
  589. 'id' => 1,
  590. 'title' => 'New First Article'
  591. ));
  592. $result = $TestModel->create() && $TestModel->save($data, false);
  593. $this->assertFalse(empty($result));
  594. $TestModel->recursive = -1;
  595. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  596. $expected = array('Article' => array(
  597. 'id' => '1',
  598. 'user_id' => '1',
  599. 'title' => 'New First Article',
  600. 'body' => 'First Article Body',
  601. 'published' => 'N'
  602. ));
  603. $this->assertEquals($expected, $result);
  604. $data = array('Article' => array(
  605. 'id' => 1,
  606. 'user_id' => '2',
  607. 'title' => 'First Article',
  608. 'body' => 'New First Article Body',
  609. 'published' => 'Y'
  610. ));
  611. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  612. $this->assertFalse(empty($result));
  613. $TestModel->recursive = -1;
  614. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  615. $expected = array('Article' => array(
  616. 'id' => '1',
  617. 'user_id' => '1',
  618. 'title' => 'First Article',
  619. 'body' => 'First Article Body',
  620. 'published' => 'Y'
  621. ));
  622. $this->assertEquals($expected, $result);
  623. $data = array(
  624. 'Article' => array(
  625. 'user_id' => '2',
  626. 'title' => 'New Article',
  627. 'body' => 'New Article Body',
  628. 'created' => '2007-03-18 14:55:23',
  629. 'updated' => '2007-03-18 14:57:31'
  630. ),
  631. 'Tag' => array('Tag' => array(1, 3))
  632. );
  633. $TestModel->create();
  634. $result = $TestModel->create() && $TestModel->save($data);
  635. $this->assertFalse(empty($result));
  636. $TestModel->recursive = 2;
  637. $result = $TestModel->read(null, 4);
  638. $expected = array(
  639. 'Article' => array(
  640. 'id' => '4',
  641. 'user_id' => '2',
  642. 'title' => 'New Article',
  643. 'body' => 'New Article Body',
  644. 'published' => 'N',
  645. 'created' => '2007-03-18 14:55:23',
  646. 'updated' => '2007-03-18 14:57:31'
  647. ),
  648. 'User' => array(
  649. 'id' => '2',
  650. 'user' => 'nate',
  651. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  652. 'created' => '2007-03-17 01:18:23',
  653. 'updated' => '2007-03-17 01:20:31'
  654. ),
  655. 'Comment' => array(),
  656. 'Tag' => array(
  657. array(
  658. 'id' => '1',
  659. 'tag' => 'tag1',
  660. 'created' => '2007-03-18 12:22:23',
  661. 'updated' => '2007-03-18 12:24:31'
  662. ),
  663. array(
  664. 'id' => '3',
  665. 'tag' => 'tag3',
  666. 'created' => '2007-03-18 12:26:23',
  667. 'updated' => '2007-03-18 12:28:31'
  668. )));
  669. $this->assertEquals($expected, $result);
  670. $data = array('Comment' => array(
  671. 'article_id' => '4',
  672. 'user_id' => '1',
  673. 'comment' => 'Comment New Article',
  674. 'published' => 'Y',
  675. 'created' => '2007-03-18 14:57:23',
  676. 'updated' => '2007-03-18 14:59:31'
  677. ));
  678. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  679. $this->assertFalse(empty($result));
  680. $data = array('Attachment' => array(
  681. 'comment_id' => '7',
  682. 'attachment' => 'newattachment.zip',
  683. 'created' => '2007-03-18 15:02:23',
  684. 'updated' => '2007-03-18 15:04:31'
  685. ));
  686. $result = $TestModel->Comment->Attachment->save($data);
  687. $this->assertFalse(empty($result));
  688. $TestModel->recursive = 2;
  689. $result = $TestModel->read(null, 4);
  690. $expected = array(
  691. 'Article' => array(
  692. 'id' => '4',
  693. 'user_id' => '2',
  694. 'title' => 'New Article',
  695. 'body' => 'New Article Body',
  696. 'published' => 'N',
  697. 'created' => '2007-03-18 14:55:23',
  698. 'updated' => '2007-03-18 14:57:31'
  699. ),
  700. 'User' => array(
  701. 'id' => '2',
  702. 'user' => 'nate',
  703. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  704. 'created' => '2007-03-17 01:18:23',
  705. 'updated' => '2007-03-17 01:20:31'
  706. ),
  707. 'Comment' => array(
  708. array(
  709. 'id' => '7',
  710. 'article_id' => '4',
  711. 'user_id' => '1',
  712. 'comment' => 'Comment New Article',
  713. 'published' => 'Y',
  714. 'created' => '2007-03-18 14:57:23',
  715. 'updated' => '2007-03-18 14:59:31',
  716. 'Article' => array(
  717. 'id' => '4',
  718. 'user_id' => '2',
  719. 'title' => 'New Article',
  720. 'body' => 'New Article Body',
  721. 'published' => 'N',
  722. 'created' => '2007-03-18 14:55:23',
  723. 'updated' => '2007-03-18 14:57:31'
  724. ),
  725. 'User' => array(
  726. 'id' => '1',
  727. 'user' => 'mariano',
  728. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  729. 'created' => '2007-03-17 01:16:23',
  730. 'updated' => '2007-03-17 01:18:31'
  731. ),
  732. 'Attachment' => array(
  733. 'id' => '2',
  734. 'comment_id' => '7',
  735. 'attachment' => 'newattachment.zip',
  736. 'created' => '2007-03-18 15:02:23',
  737. 'updated' => '2007-03-18 15:04:31'
  738. ))),
  739. 'Tag' => array(
  740. array(
  741. 'id' => '1',
  742. 'tag' => 'tag1',
  743. 'created' => '2007-03-18 12:22:23',
  744. 'updated' => '2007-03-18 12:24:31'
  745. ),
  746. array(
  747. 'id' => '3',
  748. 'tag' => 'tag3',
  749. 'created' => '2007-03-18 12:26:23',
  750. 'updated' => '2007-03-18 12:28:31'
  751. )));
  752. $this->assertEquals($expected, $result);
  753. }
  754. /**
  755. * test that a null Id doesn't cause errors
  756. *
  757. * @return void
  758. */
  759. public function testSaveWithNullId() {
  760. $this->loadFixtures('User');
  761. $User = new User();
  762. $User->read(null, 1);
  763. $User->data['User']['id'] = null;
  764. $result = $User->save(array('password' => 'test'));
  765. $this->assertFalse(empty($result));
  766. $this->assertTrue($User->id > 0);
  767. $User->read(null, 2);
  768. $User->data['User']['id'] = null;
  769. $result = $User->save(array('password' => 'test'));
  770. $this->assertFalse(empty($result));
  771. $this->assertTrue($User->id > 0);
  772. $User->data['User'] = array('password' => 'something');
  773. $result = $User->save();
  774. $this->assertFalse(empty($result));
  775. $result = $User->read();
  776. $this->assertEquals($User->data['User']['password'], 'something');
  777. }
  778. /**
  779. * testSaveWithSet method
  780. *
  781. * @return void
  782. */
  783. public function testSaveWithSet() {
  784. $this->loadFixtures('Article');
  785. $TestModel = new Article();
  786. // Create record we will be updating later
  787. $data = array('Article' => array(
  788. 'user_id' => '1',
  789. 'title' => 'Fourth Article',
  790. 'body' => 'Fourth Article Body',
  791. 'published' => 'Y'
  792. ));
  793. $result = $TestModel->create() && $TestModel->save($data);
  794. $this->assertFalse(empty($result));
  795. // Check record we created
  796. $TestModel->recursive = -1;
  797. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  798. $expected = array('Article' => array(
  799. 'id' => '4',
  800. 'user_id' => '1',
  801. 'title' => 'Fourth Article',
  802. 'body' => 'Fourth Article Body',
  803. 'published' => 'Y'
  804. ));
  805. $this->assertEquals($expected, $result);
  806. // Create new record just to overlap Model->id on previously created record
  807. $data = array('Article' => array(
  808. 'user_id' => '4',
  809. 'title' => 'Fifth Article',
  810. 'body' => 'Fifth Article Body',
  811. 'published' => 'Y'
  812. ));
  813. $result = $TestModel->create() && $TestModel->save($data);
  814. $this->assertFalse(empty($result));
  815. $TestModel->recursive = -1;
  816. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  817. $expected = array('Article' => array(
  818. 'id' => '5',
  819. 'user_id' => '4',
  820. 'title' => 'Fifth Article',
  821. 'body' => 'Fifth Article Body',
  822. 'published' => 'Y'
  823. ));
  824. $this->assertEquals($expected, $result);
  825. // Go back and edit the first article we created, starting by checking it's still there
  826. $TestModel->recursive = -1;
  827. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  828. $expected = array('Article' => array(
  829. 'id' => '4',
  830. 'user_id' => '1',
  831. 'title' => 'Fourth Article',
  832. 'body' => 'Fourth Article Body',
  833. 'published' => 'Y'
  834. ));
  835. $this->assertEquals($expected, $result);
  836. // And now do the update with set()
  837. $data = array('Article' => array(
  838. 'id' => '4',
  839. 'title' => 'Fourth Article - New Title',
  840. 'published' => 'N'
  841. ));
  842. $result = $TestModel->set($data) && $TestModel->save();
  843. $this->assertFalse(empty($result));
  844. $TestModel->recursive = -1;
  845. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  846. $expected = array('Article' => array(
  847. 'id' => '4',
  848. 'user_id' => '1',
  849. 'title' => 'Fourth Article - New Title',
  850. 'body' => 'Fourth Article Body',
  851. 'published' => 'N'
  852. ));
  853. $this->assertEquals($expected, $result);
  854. $TestModel->recursive = -1;
  855. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  856. $expected = array('Article' => array(
  857. 'id' => '5',
  858. 'user_id' => '4',
  859. 'title' => 'Fifth Article',
  860. 'body' => 'Fifth Article Body',
  861. 'published' => 'Y'
  862. ));
  863. $this->assertEquals($expected, $result);
  864. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  865. $result = ($TestModel->set($data) && $TestModel->save());
  866. $this->assertFalse(empty($result));
  867. $TestModel->recursive = -1;
  868. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  869. $expected = array('Article' => array(
  870. 'id' => '5',
  871. 'user_id' => '4',
  872. 'title' => 'Fifth Article - New Title 5',
  873. 'body' => 'Fifth Article Body',
  874. 'published' => 'Y'
  875. ));
  876. $this->assertEquals($expected, $result);
  877. $TestModel->recursive = -1;
  878. $result = $TestModel->find('all', array('fields' => array('id', 'title')));
  879. $expected = array(
  880. array('Article' => array('id' => 1, 'title' => 'First Article')),
  881. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  882. array('Article' => array('id' => 3, 'title' => 'Third Article')),
  883. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
  884. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
  885. );
  886. $this->assertEquals($expected, $result);
  887. }
  888. /**
  889. * testSaveWithNonExistentFields method
  890. *
  891. * @return void
  892. */
  893. public function testSaveWithNonExistentFields() {
  894. $this->loadFixtures('Article');
  895. $TestModel = new Article();
  896. $TestModel->recursive = -1;
  897. $data = array(
  898. 'non_existent' => 'This field does not exist',
  899. 'user_id' => '1',
  900. 'title' => 'Fourth Article - New Title',
  901. 'body' => 'Fourth Article Body',
  902. 'published' => 'N'
  903. );
  904. $result = $TestModel->create() && $TestModel->save($data);
  905. $this->assertFalse(empty($result));
  906. $expected = array('Article' => array(
  907. 'id' => '4',
  908. 'user_id' => '1',
  909. 'title' => 'Fourth Article - New Title',
  910. 'body' => 'Fourth Article Body',
  911. 'published' => 'N'
  912. ));
  913. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  914. $this->assertEquals($expected, $result);
  915. $data = array(
  916. 'user_id' => '1',
  917. 'non_existent' => 'This field does not exist',
  918. 'title' => 'Fiveth Article - New Title',
  919. 'body' => 'Fiveth Article Body',
  920. 'published' => 'N'
  921. );
  922. $result = $TestModel->create() && $TestModel->save($data);
  923. $this->assertFalse(empty($result));
  924. $expected = array('Article' => array(
  925. 'id' => '5',
  926. 'user_id' => '1',
  927. 'title' => 'Fiveth Article - New Title',
  928. 'body' => 'Fiveth Article Body',
  929. 'published' => 'N'
  930. ));
  931. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  932. $this->assertEquals($expected, $result);
  933. }
  934. /**
  935. * testSaveFromXml method
  936. *
  937. * @return void
  938. */
  939. public function testSaveFromXml() {
  940. $this->markTestSkipped('This feature needs to be fixed or dropped');
  941. $this->loadFixtures('Article');
  942. App::uses('Xml', 'Utility');
  943. $Article = new Article();
  944. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  945. $this->assertFalse(empty($result));
  946. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  947. $this->assertFalse(empty($results));
  948. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  949. $this->assertFalse(empty($result));
  950. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  951. $this->assertFalse(empty($results));
  952. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  953. $this->assertFalse(empty($result));
  954. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  955. $this->assertFalse(empty($results));
  956. }
  957. /**
  958. * testSaveHabtm method
  959. *
  960. * @return void
  961. */
  962. public function testSaveHabtm() {
  963. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  964. $TestModel = new Article();
  965. $result = $TestModel->findById(2);
  966. $expected = array(
  967. 'Article' => array(
  968. 'id' => '2',
  969. 'user_id' => '3',
  970. 'title' => 'Second Article',
  971. 'body' => 'Second Article Body',
  972. 'published' => 'Y',
  973. 'created' => '2007-03-18 10:41:23',
  974. 'updated' => '2007-03-18 10:43:31'
  975. ),
  976. 'User' => array(
  977. 'id' => '3',
  978. 'user' => 'larry',
  979. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  980. 'created' => '2007-03-17 01:20:23',
  981. 'updated' => '2007-03-17 01:22:31'
  982. ),
  983. 'Comment' => array(
  984. array(
  985. 'id' => '5',
  986. 'article_id' => '2',
  987. 'user_id' => '1',
  988. 'comment' => 'First Comment for Second Article',
  989. 'published' => 'Y',
  990. 'created' => '2007-03-18 10:53:23',
  991. 'updated' => '2007-03-18 10:55:31'
  992. ),
  993. array(
  994. 'id' => '6',
  995. 'article_id' => '2',
  996. 'user_id' => '2',
  997. 'comment' => 'Second Comment for Second Article',
  998. 'published' => 'Y',
  999. 'created' => '2007-03-18 10:55:23',
  1000. 'updated' => '2007-03-18 10:57:31'
  1001. )),
  1002. 'Tag' => array(
  1003. array(
  1004. 'id' => '1',
  1005. 'tag' => 'tag1',
  1006. 'created' => '2007-03-18 12:22:23',
  1007. 'updated' => '2007-03-18 12:24:31'
  1008. ),
  1009. array(
  1010. 'id' => '3',
  1011. 'tag' => 'tag3',
  1012. 'created' => '2007-03-18 12:26:23',
  1013. 'updated' => '2007-03-18 12:28:31'
  1014. )
  1015. )
  1016. );
  1017. $this->assertEquals($expected, $result);
  1018. $data = array(
  1019. 'Article' => array(
  1020. 'id' => '2',
  1021. 'title' => 'New Second Article'
  1022. ),
  1023. 'Tag' => array('Tag' => array(1, 2))
  1024. );
  1025. $result = $TestModel->set($data);
  1026. $this->assertFalse(empty($result));
  1027. $result = $TestModel->save();
  1028. $this->assertFalse(empty($result));
  1029. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1030. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1031. $expected = array(
  1032. 'Article' => array(
  1033. 'id' => '2',
  1034. 'user_id' => '3',
  1035. 'title' => 'New Second Article',
  1036. 'body' => 'Second Article Body'
  1037. ),
  1038. 'Tag' => array(
  1039. array(
  1040. 'id' => '1',
  1041. 'tag' => 'tag1',
  1042. 'created' => '2007-03-18 12:22:23',
  1043. 'updated' => '2007-03-18 12:24:31'
  1044. ),
  1045. array(
  1046. 'id' => '2',
  1047. 'tag' => 'tag2',
  1048. 'created' => '2007-03-18 12:24:23',
  1049. 'updated' => '2007-03-18 12:26:31'
  1050. )));
  1051. $this->assertEquals($expected, $result);
  1052. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1053. $result = $TestModel->set($data);
  1054. $this->assertFalse(empty($result));
  1055. $result = $TestModel->save();
  1056. $this->assertFalse(empty($result));
  1057. $TestModel->unbindModel(array(
  1058. 'belongsTo' => array('User'),
  1059. 'hasMany' => array('Comment')
  1060. ));
  1061. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1062. $expected = array(
  1063. 'Article' => array(
  1064. 'id' => '2',
  1065. 'user_id' => '3',
  1066. 'title' => 'New Second Article',
  1067. 'body' => 'Second Article Body'
  1068. ),
  1069. 'Tag' => array(
  1070. array(
  1071. 'id' => '2',
  1072. 'tag' => 'tag2',
  1073. 'created' => '2007-03-18 12:24:23',
  1074. 'updated' => '2007-03-18 12:26:31'
  1075. ),
  1076. array(
  1077. 'id' => '3',
  1078. 'tag' => 'tag3',
  1079. 'created' => '2007-03-18 12:26:23',
  1080. 'updated' => '2007-03-18 12:28:31'
  1081. )));
  1082. $this->assertEquals($expected, $result);
  1083. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1084. $result = $TestModel->set($data);
  1085. $this->assertFalse(empty($result));
  1086. $result = $TestModel->save();
  1087. $this->assertFalse(empty($result));
  1088. $TestModel->unbindModel(array(
  1089. 'belongsTo' => array('User'),
  1090. 'hasMany' => array('Comment')
  1091. ));
  1092. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1093. $expected = array(
  1094. 'Article' => array(
  1095. 'id' => '2',
  1096. 'user_id' => '3',
  1097. 'title' => 'New Second Article',
  1098. 'body' => 'Second Article Body'
  1099. ),
  1100. 'Tag' => array(
  1101. array(
  1102. 'id' => '1',
  1103. 'tag' => 'tag1',
  1104. 'created' => '2007-03-18 12:22:23',
  1105. 'updated' => '2007-03-18 12:24:31'
  1106. ),
  1107. array(
  1108. 'id' => '2',
  1109. 'tag' => 'tag2',
  1110. 'created' => '2007-03-18 12:24:23',
  1111. 'updated' => '2007-03-18 12:26:31'
  1112. ),
  1113. array(
  1114. 'id' => '3',
  1115. 'tag' => 'tag3',
  1116. 'created' => '2007-03-18 12:26:23',
  1117. 'updated' => '2007-03-18 12:28:31'
  1118. )));
  1119. $this->assertEquals($expected, $result);
  1120. $data = array('Tag' => array('Tag' => array()));
  1121. $result = $TestModel->set($data);
  1122. $this->assertFalse(empty($result));
  1123. $result = $TestModel->save();
  1124. $this->assertFalse(empty($result));
  1125. $data = array('Tag' => array('Tag' => ''));
  1126. $result = $TestModel->set($data);
  1127. $this->assertFalse(empty($result));
  1128. $result = $TestModel->save();
  1129. $this->assertFalse(empty($result));
  1130. $TestModel->unbindModel(array(
  1131. 'belongsTo' => array('User'),
  1132. 'hasMany' => array('Comment')
  1133. ));
  1134. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1135. $expected = array(
  1136. 'Article' => array(
  1137. 'id' => '2',
  1138. 'user_id' => '3',
  1139. 'title' => 'New Second Article',
  1140. 'body' => 'Second Article Body'
  1141. ),
  1142. 'Tag' => array()
  1143. );
  1144. $this->assertEquals($expected, $result);
  1145. $data = array('Tag' => array('Tag' => array(2, 3)));
  1146. $result = $TestModel->set($data);
  1147. $this->assertFalse(empty($result));
  1148. $result = $TestModel->save();
  1149. $this->assertFalse(empty($result));
  1150. $TestModel->unbindModel(array(
  1151. 'belongsTo' => array('User'),
  1152. 'hasMany' => array('Comment')
  1153. ));
  1154. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1155. $expected = array(
  1156. 'Article' => array(
  1157. 'id' => '2',
  1158. 'user_id' => '3',
  1159. 'title' => 'New Second Article',
  1160. 'body' => 'Second Article Body'
  1161. ),
  1162. 'Tag' => array(
  1163. array(
  1164. 'id' => '2',
  1165. 'tag' => 'tag2',
  1166. 'created' => '2007-03-18 12:24:23',
  1167. 'updated' => '2007-03-18 12:26:31'
  1168. ),
  1169. array(
  1170. 'id' => '3',
  1171. 'tag' => 'tag3',
  1172. 'created' => '2007-03-18 12:26:23',
  1173. 'updated' => '2007-03-18 12:28:31'
  1174. )));
  1175. $this->assertEquals($expected, $result);
  1176. $data = array(
  1177. 'Tag' => array(
  1178. 'Tag' => array(1, 2)
  1179. ),
  1180. 'Article' => array(
  1181. 'id' => '2',
  1182. 'title' => 'New Second Article'
  1183. ));
  1184. $result = $TestModel->set($data);
  1185. $this->assertFalse(empty($result));
  1186. $result = $TestModel->save();
  1187. $this->assertFalse(empty($result));
  1188. $TestModel->unbindModel(array(
  1189. 'belongsTo' => array('User'),
  1190. 'hasMany' => array('Comment')
  1191. ));
  1192. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1193. $expected = array(
  1194. 'Article' => array(
  1195. 'id' => '2',
  1196. 'user_id' => '3',
  1197. 'title' => 'New Second Article',
  1198. 'body' => 'Second Article Body'
  1199. ),
  1200. 'Tag' => array(
  1201. array(
  1202. 'id' => '1',
  1203. 'tag' => 'tag1',
  1204. 'created' => '2007-03-18 12:22:23',
  1205. 'updated' => '2007-03-18 12:24:31'
  1206. ),
  1207. array(
  1208. 'id' => '2',
  1209. 'tag' => 'tag2',
  1210. 'created' => '2007-03-18 12:24:23',
  1211. 'updated' => '2007-03-18 12:26:31'
  1212. )));
  1213. $this->assertEquals($expected, $result);
  1214. $data = array(
  1215. 'Tag' => array(
  1216. 'Tag' => array(1, 2)
  1217. ),
  1218. 'Article' => array(
  1219. 'id' => '2',
  1220. 'title' => 'New Second Article Title'
  1221. ));
  1222. $result = $TestModel->set($data);
  1223. $this->assertFalse(empty($result));
  1224. $result = $TestModel->save();
  1225. $this->assertFalse(empty($result));
  1226. $TestModel->unbindModel(array(
  1227. 'belongsTo' => array('User'),
  1228. 'hasMany' => array('Comment')
  1229. ));
  1230. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1231. $expected = array(
  1232. 'Article' => array(
  1233. 'id' => '2',
  1234. 'user_id' => '3',
  1235. 'title' => 'New Second Article Title',
  1236. 'body' => 'Second Article Body'
  1237. ),
  1238. 'Tag' => array(
  1239. array(
  1240. 'id' => '1',
  1241. 'tag' => 'tag1',
  1242. 'created' => '2007-03-18 12:22:23',
  1243. 'updated' => '2007-03-18 12:24:31'
  1244. ),
  1245. array(
  1246. 'id' => '2',
  1247. 'tag' => 'tag2',
  1248. 'created' => '2007-03-18 12:24:23',
  1249. 'updated' => '2007-03-18 12:26:31'
  1250. )
  1251. )
  1252. );
  1253. $this->assertEquals($expected, $result);
  1254. $data = array(
  1255. 'Tag' => array(
  1256. 'Tag' => array(2, 3)
  1257. ),
  1258. 'Article' => array(
  1259. 'id' => '2',
  1260. 'title' => 'Changed Second Article'
  1261. ));
  1262. $result = $TestModel->set($data);
  1263. $this->assertFalse(empty($result));
  1264. $result = $TestModel->save();
  1265. $this->assertFalse(empty($result));
  1266. $TestModel->unbindModel(array(
  1267. 'belongsTo' => array('User'),
  1268. 'hasMany' => array('Comment')
  1269. ));
  1270. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1271. $expected = array(
  1272. 'Article' => array(
  1273. 'id' => '2',
  1274. 'user_id' => '3',
  1275. 'title' => 'Changed Second Article',
  1276. 'body' => 'Second Article Body'
  1277. ),
  1278. 'Tag' => array(
  1279. array(
  1280. 'id' => '2',
  1281. 'tag' => 'tag2',
  1282. 'created' => '2007-03-18 12:24:23',
  1283. 'updated' => '2007-03-18 12:26:31'
  1284. ),
  1285. array(
  1286. 'id' => '3',
  1287. 'tag' => 'tag3',
  1288. 'created' => '2007-03-18 12:26:23',
  1289. 'updated' => '2007-03-18 12:28:31'
  1290. )
  1291. )
  1292. );
  1293. $this->assertEquals($expected, $result);
  1294. $data = array(
  1295. 'Tag' => array(
  1296. 'Tag' => array(1, 3)
  1297. ),
  1298. 'Article' => array('id' => '2'),
  1299. );
  1300. $result = $TestModel->set($data);
  1301. $this->assertFalse(empty($result));
  1302. $result = $TestModel->save();
  1303. $this->assertFalse(empty($result));
  1304. $TestModel->unbindModel(array(
  1305. 'belongsTo' => array('User'),
  1306. 'hasMany' => array('Comment')
  1307. ));
  1308. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1309. $expected = array(
  1310. 'Article' => array(
  1311. 'id' => '2',
  1312. 'user_id' => '3',
  1313. 'title' => 'Changed Second Article',
  1314. 'body' => 'Second Article Body'
  1315. ),
  1316. 'Tag' => array(
  1317. array(
  1318. 'id' => '1',
  1319. 'tag' => 'tag1',
  1320. 'created' => '2007-03-18 12:22:23',
  1321. 'updated' => '2007-03-18 12:24:31'
  1322. ),
  1323. array(
  1324. 'id' => '3',
  1325. 'tag' => 'tag3',
  1326. 'created' => '2007-03-18 12:26:23',
  1327. 'updated' => '2007-03-18 12:28:31'
  1328. )));
  1329. $this->assertEquals($expected, $result);
  1330. $data = array(
  1331. 'Article' => array(
  1332. 'id' => 10,
  1333. 'user_id' => '2',
  1334. 'title' => 'New Article With Tags and fieldList',
  1335. 'body' => 'New Article Body with Tags and fieldList',
  1336. 'created' => '2007-03-18 14:55:23',
  1337. 'updated' => '2007-03-18 14:57:31'
  1338. ),
  1339. 'Tag' => array(
  1340. 'Tag' => array(1, 2, 3)
  1341. ));
  1342. $result = $TestModel->create()
  1343. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1344. $this->assertFalse(empty($result));
  1345. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1346. $result = $TestModel->read();
  1347. $expected = array(
  1348. 'Article' => array(
  1349. 'id' => 4,
  1350. 'user_id' => 2,
  1351. 'title' => 'New Article With Tags and fieldList',
  1352. 'body' => '',
  1353. 'published' => 'N',
  1354. 'created' => '',
  1355. 'updated' => ''
  1356. ),
  1357. 'Tag' => array(
  1358. 0 => array(
  1359. 'id' => 1,
  1360. 'tag' => 'tag1',
  1361. 'created' => '2007-03-18 12:22:23',
  1362. 'updated' => '2007-03-18 12:24:31'
  1363. ),
  1364. 1 => array(
  1365. 'id' => 2,
  1366. 'tag' => 'tag2',
  1367. 'created' => '2007-03-18 12:24:23',
  1368. 'updated' => '2007-03-18 12:26:31'
  1369. ),
  1370. 2 => array(
  1371. 'id' => 3,
  1372. 'tag' => 'tag3',
  1373. 'created' => '2007-03-18 12:26:23',
  1374. 'updated' => '2007-03-18 12:28:31'
  1375. )));
  1376. $this->assertEquals($expected, $result);
  1377. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1378. $TestModel = new JoinA();
  1379. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1380. $data = array(
  1381. 'JoinA' => array(
  1382. 'id' => 1,
  1383. 'name' => 'Join A 1',
  1384. 'body' => 'Join A 1 Body',
  1385. ),
  1386. 'JoinC' => array(
  1387. 'JoinC' => array(
  1388. array('join_c_id' => 2, 'other' => 'new record'),
  1389. array('join_c_id' => 3, 'other' => 'new record')
  1390. )
  1391. )
  1392. );
  1393. $TestModel->save($data);
  1394. $result = $TestModel->read(null, 1);
  1395. $expected = array(4, 5);
  1396. $this->assertEquals(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected);
  1397. $expected = array('new record', 'new record');
  1398. $this->assertEquals(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected);
  1399. }
  1400. /**
  1401. * testSaveHabtmNoPrimaryData method
  1402. *
  1403. * @return void
  1404. */
  1405. public function testSaveHabtmNoPrimaryData() {
  1406. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1407. $TestModel = new Article();
  1408. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
  1409. $result = $TestModel->findById(2);
  1410. $expected = array(
  1411. 'Article' => array(
  1412. 'id' => '2',
  1413. 'user_id' => '3',
  1414. 'title' => 'Second Article',
  1415. 'body' => 'Second Article Body',
  1416. 'published' => 'Y',
  1417. 'created' => '2007-03-18 10:41:23',
  1418. 'updated' => '2007-03-18 10:43:31'
  1419. ),
  1420. 'Tag' => array(
  1421. array(
  1422. 'id' => '1',
  1423. 'tag' => 'tag1',
  1424. 'created' => '2007-03-18 12:22:23',
  1425. 'updated' => '2007-03-18 12:24:31'
  1426. ),
  1427. array(
  1428. 'id' => '3',
  1429. 'tag' => 'tag3',
  1430. 'created' => '2007-03-18 12:26:23',
  1431. 'updated' => '2007-03-18 12:28:31'
  1432. )
  1433. )
  1434. );
  1435. $this->assertEquals($expected, $result);
  1436. $ts = date('Y-m-d H:i:s');
  1437. $TestModel->id = 2;
  1438. $data = array('Tag' => array('Tag' => array(2)));
  1439. $TestModel->save($data);
  1440. $result = $TestModel->findById(2);
  1441. $expected = array(
  1442. 'Article' => array(
  1443. 'id' => '2',
  1444. 'user_id' => '3',
  1445. 'title' => 'Second Article',
  1446. 'body' => 'Second Article Body',
  1447. 'published' => 'Y',
  1448. 'created' => '2007-03-18 10:41:23',
  1449. 'updated' => $ts
  1450. ),
  1451. 'Tag' => array(
  1452. array(
  1453. 'id' => '2',
  1454. 'tag' => 'tag2',
  1455. 'created' => '2007-03-18 12:24:23',
  1456. 'updated' => '2007-03-18 12:26:31'
  1457. )
  1458. )
  1459. );
  1460. $this->assertEquals($expected, $result);
  1461. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1462. $TestModel = new Portfolio();
  1463. $result = $TestModel->findById(2);
  1464. $expected = array(
  1465. 'Portfolio' => array(
  1466. 'id' => 2,
  1467. 'seller_id' => 1,
  1468. 'name' => 'Portfolio 2'
  1469. ),
  1470. 'Item' => array(
  1471. array(
  1472. 'id' => 2,
  1473. 'syfile_id' => 2,
  1474. 'published' => '',
  1475. 'name' => 'Item 2',
  1476. 'ItemsPortfolio' => array(
  1477. 'id' => 2,
  1478. 'item_id' => 2,
  1479. 'portfolio_id' => 2
  1480. )
  1481. ),
  1482. array(
  1483. 'id' => 6,
  1484. 'syfile_id' => 6,
  1485. 'published' => '',
  1486. 'name' => 'Item 6',
  1487. 'ItemsPortfolio' => array(
  1488. 'id' => 6,
  1489. 'item_id' => 6,
  1490. 'portfolio_id' => 2
  1491. )
  1492. )
  1493. )
  1494. );
  1495. $this->assertEquals($expected, $result);
  1496. $data = array('Item' => array('Item' => array(1, 2)));
  1497. $TestModel->id = 2;
  1498. $TestModel->save($data);
  1499. $result = $TestModel->findById(2);
  1500. $result['Item'] = Set::sort($result['Item'], '{n}.id', 'asc');
  1501. $expected = array(
  1502. 'Portfolio' => array(
  1503. 'id' => 2,
  1504. 'seller_id' => 1,
  1505. 'name' => 'Portfolio 2'
  1506. ),
  1507. 'Item' => array(
  1508. array(
  1509. 'id' => 1,
  1510. 'syfile_id' => 1,
  1511. 'published' => '',
  1512. 'name' => 'Item 1',
  1513. 'ItemsPortfolio' => array(
  1514. 'id' => 7,
  1515. 'item_id' => 1,
  1516. 'portfolio_id' => 2
  1517. )
  1518. ),
  1519. array(
  1520. 'id' => 2,
  1521. 'syfile_id' => 2,
  1522. 'published' => '',
  1523. 'name' => 'Item 2',
  1524. 'ItemsPortfolio' => array(
  1525. 'id' => 8,
  1526. 'item_id' => 2,
  1527. 'portfolio_id' => 2
  1528. )
  1529. )
  1530. )
  1531. );
  1532. $this->assertEquals($expected, $result);
  1533. }
  1534. /**
  1535. * testSaveHabtmCustomKeys method
  1536. *
  1537. * @return void
  1538. */
  1539. public function testSaveHabtmCustomKeys() {
  1540. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1541. $Story = new Story();
  1542. $data = array(
  1543. 'Story' => array('story' => '1'),
  1544. 'Tag' => array(
  1545. 'Tag' => array(2, 3)
  1546. ));
  1547. $result = $Story->set($data);
  1548. $this->assertFalse(empty($result));
  1549. $result = $Story->save();
  1550. $this->assertFalse(empty($result));
  1551. $result = $Story->find('all', array('order' => array('Story.story')));
  1552. $expected = array(
  1553. array(
  1554. 'Story' => array(
  1555. 'story' => 1,
  1556. 'title' => 'First Story'
  1557. ),
  1558. 'Tag' => array(
  1559. array(
  1560. 'id' => 2,
  1561. 'tag' => 'tag2',
  1562. 'created' => '2007-03-18 12:24:23',
  1563. 'updated' => '2007-03-18 12:26:31'
  1564. ),
  1565. array(
  1566. 'id' => 3,
  1567. 'tag' => 'tag3',
  1568. 'created' => '2007-03-18 12:26:23',
  1569. 'updated' => '2007-03-18 12:28:31'
  1570. ))),
  1571. array(
  1572. 'Story' => array(
  1573. 'story' => 2,
  1574. 'title' => 'Second Story'
  1575. ),
  1576. 'Tag' => array()
  1577. ));
  1578. $this->assertEquals($expected, $result);
  1579. }
  1580. /**
  1581. * test that saving habtm records respects conditions set in the 'conditions' key
  1582. * for the association.
  1583. *
  1584. * @return void
  1585. */
  1586. public function testHabtmSaveWithConditionsInAssociation() {
  1587. $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
  1588. $Something = new Something();
  1589. $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
  1590. $Something->bindModel(array(
  1591. 'hasAndBelongsToMany' => array(
  1592. 'DoomedSomethingElse' => array(
  1593. 'className' => 'SomethingElse',
  1594. 'joinTable' => 'join_things',
  1595. 'conditions' => array('JoinThing.doomed' => true),
  1596. 'unique' => true
  1597. ),
  1598. 'NotDoomedSomethingElse' => array(
  1599. 'className' => 'SomethingElse',
  1600. 'joinTable' => 'join_things',
  1601. 'conditions' => array('JoinThing.doomed' => 0),
  1602. 'unique' => true
  1603. )
  1604. )
  1605. ), false);
  1606. $result = $Something->read(null, 1);
  1607. $this->assertTrue(empty($result['NotDoomedSomethingElse']));
  1608. $this->assertEquals(count($result['DoomedSomethingElse']), 1);
  1609. $data = array(
  1610. 'Something' => array('id' => 1),
  1611. 'NotDoomedSomethingElse' => array(
  1612. 'NotDoomedSomethingElse' => array(
  1613. array('something_else_id' => 2, 'doomed' => 0),
  1614. array('something_else_id' => 3, 'doomed' => 0)
  1615. )
  1616. )
  1617. );
  1618. $Something->create($data);
  1619. $result = $Something->save();
  1620. $this->assertFalse(empty($result));
  1621. $result = $Something->read(null, 1);
  1622. $this->assertEquals(count($result['NotDoomedSomethingElse']), 2);
  1623. $this->assertEquals(count($result['DoomedSomethingElse']), 1);
  1624. }
  1625. /**
  1626. * testHabtmSaveKeyResolution method
  1627. *
  1628. * @return void
  1629. */
  1630. public function testHabtmSaveKeyResolution() {
  1631. $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
  1632. $ThePaper = new ThePaper();
  1633. $ThePaper->id = 1;
  1634. $ThePaper->save(array('Monkey' => array(2, 3)));
  1635. $result = $ThePaper->findById(1);
  1636. $expected = array(
  1637. array(
  1638. 'id' => '2',
  1639. 'device_type_id' => '1',
  1640. 'name' => 'Device 2',
  1641. 'typ' => '1'
  1642. ),
  1643. array(
  1644. 'id' => '3',
  1645. 'device_type_id' => '1',
  1646. 'name' => 'Device 3',
  1647. 'typ' => '2'
  1648. ));
  1649. $this->assertEquals($result['Monkey'], $expected);
  1650. $ThePaper->id = 2;
  1651. $ThePaper->save(array('Monkey' => array(1, 2, 3)));
  1652. $result = $ThePaper->findById(2);
  1653. $expected = array(
  1654. array(
  1655. 'id' => '1',
  1656. 'device_type_id' => '1',
  1657. 'name' => 'Device 1',
  1658. 'typ' => '1'
  1659. ),
  1660. array(
  1661. 'id' => '2',
  1662. 'device_type_id' => '1',
  1663. 'name' => 'Device 2',
  1664. 'typ' => '1'
  1665. ),
  1666. array(
  1667. 'id' => '3',
  1668. 'device_type_id' => '1',
  1669. 'name' => 'Device 3',
  1670. 'typ' => '2'
  1671. ));
  1672. $this->assertEquals($result['Monkey'], $expected);
  1673. $ThePaper->id = 2;
  1674. $ThePaper->save(array('Monkey' => array(1, 3)));
  1675. $result = $ThePaper->findById(2);
  1676. $expected = array(
  1677. array(
  1678. 'id' => '1',
  1679. 'device_type_id' => '1',
  1680. 'name' => 'Device 1',
  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($result['Monkey'], $expected);
  1690. $result = $ThePaper->findById(1);
  1691. $expected = array(
  1692. array(
  1693. 'id' => '2',
  1694. 'device_type_id' => '1',
  1695. 'name' => 'Device 2',
  1696. 'typ' => '1'
  1697. ),
  1698. array(
  1699. 'id' => '3',
  1700. 'device_type_id' => '1',
  1701. 'name' => 'Device 3',
  1702. 'typ' => '2'
  1703. ));
  1704. $this->assertEquals($result['Monkey'], $expected);
  1705. }
  1706. /**
  1707. * testCreationOfEmptyRecord method
  1708. *
  1709. * @return void
  1710. */
  1711. public function testCreationOfEmptyRecord() {
  1712. $this->loadFixtures('Author');
  1713. $TestModel = new Author();
  1714. $this->assertEquals($TestModel->find('count'), 4);
  1715. $TestModel->deleteAll(true, false, false);
  1716. $this->assertEquals($TestModel->find('count'), 0);
  1717. $result = $TestModel->save();
  1718. $this->assertTrue(isset($result['Author']['created']));
  1719. $this->assertTrue(isset($result['Author']['updated']));
  1720. $this->assertEquals($TestModel->find('count'), 1);
  1721. }
  1722. /**
  1723. * testCreateWithPKFiltering method
  1724. *
  1725. * @return void
  1726. */
  1727. public function testCreateWithPKFiltering() {
  1728. $TestModel = new Article();
  1729. $data = array(
  1730. 'id' => 5,
  1731. 'user_id' => 2,
  1732. 'title' => 'My article',
  1733. 'body' => 'Some text'
  1734. );
  1735. $result = $TestModel->create($data);
  1736. $expected = array(
  1737. 'Article' => array(
  1738. 'published' => 'N',
  1739. 'id' => 5,
  1740. 'user_id' => 2,
  1741. 'title' => 'My article',
  1742. 'body' => 'Some text'
  1743. ));
  1744. $this->assertEquals($expected, $result);
  1745. $this->assertEquals($TestModel->id, 5);
  1746. $result = $TestModel->create($data, true);
  1747. $expected = array(
  1748. 'Article' => array(
  1749. 'published' => 'N',
  1750. 'id' => false,
  1751. 'user_id' => 2,
  1752. 'title' => 'My article',
  1753. 'body' => 'Some text'
  1754. ));
  1755. $this->assertEquals($expected, $result);
  1756. $this->assertFalse($TestModel->id);
  1757. $result = $TestModel->create(array('Article' => $data), true);
  1758. $expected = array(
  1759. 'Article' => array(
  1760. 'published' => 'N',
  1761. 'id' => false,
  1762. 'user_id' => 2,
  1763. 'title' => 'My article',
  1764. 'body' => 'Some text'
  1765. ));
  1766. $this->assertEquals($expected, $result);
  1767. $this->assertFalse($TestModel->id);
  1768. $data = array(
  1769. 'id' => 6,
  1770. 'user_id' => 2,
  1771. 'title' => 'My article',
  1772. 'body' => 'Some text',
  1773. 'created' => '1970-01-01 00:00:00',
  1774. 'updated' => '1970-01-01 12:00:00',
  1775. 'modified' => '1970-01-01 12:00:00'
  1776. );
  1777. $result = $TestModel->create($data);
  1778. $expected = array(
  1779. 'Article' => array(
  1780. 'published' => 'N',
  1781. 'id' => 6,
  1782. 'user_id' => 2,
  1783. 'title' => 'My article',
  1784. 'body' => 'Some text',
  1785. 'created' => '1970-01-01 00:00:00',
  1786. 'updated' => '1970-01-01 12:00:00',
  1787. 'modified' => '1970-01-01 12:00:00'
  1788. ));
  1789. $this->assertEquals($expected, $result);
  1790. $this->assertEquals($TestModel->id, 6);
  1791. $result = $TestModel->create(array(
  1792. 'Article' => array_diff_key($data, array(
  1793. 'created' => true,
  1794. 'updated' => true,
  1795. 'modified' => true
  1796. ))), true);
  1797. $expected = array(
  1798. 'Article' => array(
  1799. 'published' => 'N',
  1800. 'id' => false,
  1801. 'user_id' => 2,
  1802. 'title' => 'My article',
  1803. 'body' => 'Some text'
  1804. ));
  1805. $this->assertEquals($expected, $result);
  1806. $this->assertFalse($TestModel->id);
  1807. }
  1808. /**
  1809. * testCreationWithMultipleData method
  1810. *
  1811. * @return void
  1812. */
  1813. public function testCreationWithMultipleData() {
  1814. $this->loadFixtures('Article', 'Comment');
  1815. $Article = new Article();
  1816. $Comment = new Comment();
  1817. $articles = $Article->find('all', array(
  1818. 'fields' => array('id','title'),
  1819. 'recursive' => -1
  1820. ));
  1821. $comments = $Comment->find('all', array(
  1822. 'fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1));
  1823. $this->assertEquals($articles, array(
  1824. array('Article' => array(
  1825. 'id' => 1,
  1826. 'title' => 'First Article'
  1827. )),
  1828. array('Article' => array(
  1829. 'id' => 2,
  1830. 'title' => 'Second Article'
  1831. )),
  1832. array('Article' => array(
  1833. 'id' => 3,
  1834. 'title' => 'Third Article'
  1835. ))));
  1836. $this->assertEquals($comments, array(
  1837. array('Comment' => array(
  1838. 'id' => 1,
  1839. 'article_id' => 1,
  1840. 'user_id' => 2,
  1841. 'comment' => 'First Comment for First Article',
  1842. 'published' => 'Y'
  1843. )),
  1844. array('Comment' => array(
  1845. 'id' => 2,
  1846. 'article_id' => 1,
  1847. 'user_id' => 4,
  1848. 'comment' => 'Second Comment for First Article',
  1849. 'published' => 'Y'
  1850. )),
  1851. array('Comment' => array(
  1852. 'id' => 3,
  1853. 'article_id' => 1,
  1854. 'user_id' => 1,
  1855. 'comment' => 'Third Comment for First Article',
  1856. 'published' => 'Y'
  1857. )),
  1858. array('Comment' => array(
  1859. 'id' => 4,
  1860. 'article_id' => 1,
  1861. 'user_id' => 1,
  1862. 'comment' => 'Fourth Comment for First Article',
  1863. 'published' => 'N'
  1864. )),
  1865. array('Comment' => array(
  1866. 'id' => 5,
  1867. 'article_id' => 2,
  1868. 'user_id' => 1,
  1869. 'comment' => 'First Comment for Second Article',
  1870. 'published' => 'Y'
  1871. )),
  1872. array('Comment' => array(
  1873. 'id' => 6,
  1874. 'article_id' => 2,
  1875. 'user_id' => 2,
  1876. 'comment' => 'Second Comment for Second Article',
  1877. 'published' => 'Y'
  1878. ))));
  1879. $data = array(
  1880. 'Comment' => array(
  1881. 'article_id' => 2,
  1882. 'user_id' => 4,
  1883. 'comment' => 'Brand New Comment',
  1884. 'published' => 'N'
  1885. ),
  1886. 'Article' => array(
  1887. 'id' => 2,
  1888. 'title' => 'Second Article Modified'
  1889. ));
  1890. $result = $Comment->create($data);
  1891. $this->assertFalse(empty($result));
  1892. $result = $Comment->save();
  1893. $this->assertFalse(empty($result));
  1894. $articles = $Article->find('all', array(
  1895. 'fields' => array('id','title'),
  1896. 'recursive' => -1
  1897. ));
  1898. $comments = $Comment->find('all', array(
  1899. 'fields' => array('id','article_id','user_id','comment','published'),
  1900. 'recursive' => -1
  1901. ));
  1902. $this->assertEquals($articles, array(
  1903. array('Article' => array(
  1904. 'id' => 1,
  1905. 'title' => 'First Article'
  1906. )),
  1907. array('Article' => array(
  1908. 'id' => 2,
  1909. 'title' => 'Second Article'
  1910. )),
  1911. array('Article' => array(
  1912. 'id' => 3,
  1913. 'title' => 'Third Article'
  1914. ))));
  1915. $this->assertEquals($comments, array(
  1916. array('Comment' => array(
  1917. 'id' => 1,
  1918. 'article_id' => 1,
  1919. 'user_id' => 2,
  1920. 'comment' => 'First Comment for First Article',
  1921. 'published' => 'Y'
  1922. )),
  1923. array('Comment' => array(
  1924. 'id' => 2,
  1925. 'article_id' => 1,
  1926. 'user_id' => 4,
  1927. 'comment' => 'Second Comment for First Article',
  1928. 'published' => 'Y'
  1929. )),
  1930. array('Comment' => array(
  1931. 'id' => 3,
  1932. 'article_id' => 1,
  1933. 'user_id' => 1,
  1934. 'comment' => 'Third Comment for First Article',
  1935. 'published' => 'Y'
  1936. )),
  1937. array('Comment' => array(
  1938. 'id' => 4,
  1939. 'article_id' => 1,
  1940. 'user_id' => 1,
  1941. 'comment' => 'Fourth Comment for First Article',
  1942. 'published' => 'N'
  1943. )),
  1944. array('Comment' => array(
  1945. 'id' => 5,
  1946. 'article_id' => 2,
  1947. 'user_id' => 1,
  1948. 'comment' => 'First Comment for Second Article',
  1949. 'published' => 'Y'
  1950. )),
  1951. array('Comment' => array(
  1952. 'id' => 6,
  1953. 'article_id' => 2,
  1954. 'user_id' => 2, 'comment' =>
  1955. 'Second Comment for Second Article',
  1956. 'published' => 'Y'
  1957. )),
  1958. array('Comment' => array(
  1959. 'id' => 7,
  1960. 'article_id' => 2,
  1961. 'user_id' => 4,
  1962. 'comment' => 'Brand New Comment',
  1963. 'published' => 'N'
  1964. ))));
  1965. }
  1966. /**
  1967. * testCreationWithMultipleDataSameModel method
  1968. *
  1969. * @return void
  1970. */
  1971. public function testCreationWithMultipleDataSameModel() {
  1972. $this->loadFixtures('Article');
  1973. $Article = new Article();
  1974. $SecondaryArticle = new Article();
  1975. $result = $Article->field('title', array('id' => 1));
  1976. $this->assertEquals($result, 'First Article');
  1977. $data = array(
  1978. 'Article' => array(
  1979. 'user_id' => 2,
  1980. 'title' => 'Brand New Article',
  1981. 'body' => 'Brand New Article Body',
  1982. 'published' => 'Y'
  1983. ),
  1984. 'SecondaryArticle' => array(
  1985. 'id' => 1
  1986. ));
  1987. $Article->create();
  1988. $result = $Article->save($data);
  1989. $this->assertFalse(empty($result));
  1990. $result = $Article->getInsertID();
  1991. $this->assertTrue(!empty($result));
  1992. $result = $Article->field('title', array('id' => 1));
  1993. $this->assertEquals($result, 'First Article');
  1994. $articles = $Article->find('all', array(
  1995. 'fields' => array('id','title'),
  1996. 'recursive' => -1
  1997. ));
  1998. $this->assertEquals($articles, array(
  1999. array('Article' => array(
  2000. 'id' => 1,
  2001. 'title' => 'First Article'
  2002. )),
  2003. array('Article' => array(
  2004. 'id' => 2,
  2005. 'title' => 'Second Article'
  2006. )),
  2007. array('Article' => array(
  2008. 'id' => 3,
  2009. 'title' => 'Third Article'
  2010. )),
  2011. array('Article' => array(
  2012. 'id' => 4,
  2013. 'title' => 'Brand New Article'
  2014. ))));
  2015. }
  2016. /**
  2017. * testCreationWithMultipleDataSameModelManualInstances method
  2018. *
  2019. * @return void
  2020. */
  2021. public function testCreationWithMultipleDataSameModelManualInstances() {
  2022. $this->loadFixtures('PrimaryModel');
  2023. $Primary = new PrimaryModel();
  2024. $Secondary = new PrimaryModel();
  2025. $result = $Primary->field('primary_name', array('id' => 1));
  2026. $this->assertEquals($result, 'Primary Name Existing');
  2027. $data = array(
  2028. 'PrimaryModel' => array(
  2029. 'primary_name' => 'Primary Name New'
  2030. ),
  2031. 'SecondaryModel' => array(
  2032. 'id' => array(1)
  2033. ));
  2034. $Primary->create();
  2035. $result = $Primary->save($data);
  2036. $this->assertFalse(empty($result));
  2037. $result = $Primary->field('primary_name', array('id' => 1));
  2038. $this->assertEquals($result, 'Primary Name Existing');
  2039. $result = $Primary->getInsertID();
  2040. $this->assertTrue(!empty($result));
  2041. $result = $Primary->field('primary_name', array('id' => $result));
  2042. $this->assertEquals($result, 'Primary Name New');
  2043. $result = $Primary->find('count');
  2044. $this->assertEquals($result, 2);
  2045. }
  2046. /**
  2047. * testRecordExists method
  2048. *
  2049. * @return void
  2050. */
  2051. public function testRecordExists() {
  2052. $this->loadFixtures('User');
  2053. $TestModel = new User();
  2054. $this->assertFalse($TestModel->exists());
  2055. $TestModel->read(null, 1);
  2056. $this->assertTrue($TestModel->exists());
  2057. $TestModel->create();
  2058. $this->assertFalse($TestModel->exists());
  2059. $TestModel->id = 4;
  2060. $this->assertTrue($TestModel->exists());
  2061. $TestModel = new TheVoid();
  2062. $this->assertFalse($TestModel->exists());
  2063. }
  2064. /**
  2065. * testRecordExistsMissingTable method
  2066. *
  2067. * @expectedException PDOException
  2068. * @return void
  2069. */
  2070. public function testRecordExistsMissingTable() {
  2071. $TestModel = new TheVoid();
  2072. $TestModel->id = 5;
  2073. $TestModel->exists();
  2074. }
  2075. /**
  2076. * testUpdateExisting method
  2077. *
  2078. * @return void
  2079. */
  2080. public function testUpdateExisting() {
  2081. $this->loadFixtures('User', 'Article', 'Comment');
  2082. $TestModel = new User();
  2083. $TestModel->create();
  2084. $TestModel->save(array(
  2085. 'User' => array(
  2086. 'user' => 'some user',
  2087. 'password' => 'some password'
  2088. )));
  2089. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  2090. $id = $TestModel->id;
  2091. $TestModel->save(array(
  2092. 'User' => array(
  2093. 'user' => 'updated user'
  2094. )));
  2095. $this->assertEquals($TestModel->id, $id);
  2096. $result = $TestModel->findById($id);
  2097. $this->assertEquals($result['User']['user'], 'updated user');
  2098. $this->assertEquals($result['User']['password'], 'some password');
  2099. $Article = new Article();
  2100. $Comment = new Comment();
  2101. $data = array(
  2102. 'Comment' => array(
  2103. 'id' => 1,
  2104. 'comment' => 'First Comment for First Article'
  2105. ),
  2106. 'Article' => array(
  2107. 'id' => 2,
  2108. 'title' => 'Second Article'
  2109. ));
  2110. $result = $Article->save($data);
  2111. $this->assertFalse(empty($result));
  2112. $result = $Comment->save($data);
  2113. $this->assertFalse(empty($result));
  2114. }
  2115. /**
  2116. * test updating records and saving blank values.
  2117. *
  2118. * @return void
  2119. */
  2120. public function testUpdateSavingBlankValues() {
  2121. $this->loadFixtures('Article');
  2122. $Article = new Article();
  2123. $Article->validate = array();
  2124. $Article->create();
  2125. $result = $Article->save(array(
  2126. 'id' => 1,
  2127. 'title' => '',
  2128. 'body' => ''
  2129. ));
  2130. $this->assertTrue((bool)$result);
  2131. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2132. $this->assertEquals('', $result['Article']['title'], 'Title is not blank');
  2133. $this->assertEquals('', $result['Article']['body'], 'Body is not blank');
  2134. }
  2135. /**
  2136. * testUpdateMultiple method
  2137. *
  2138. * @return void
  2139. */
  2140. public function testUpdateMultiple() {
  2141. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2142. $TestModel = new Comment();
  2143. $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2144. $expected = array('2', '4', '1', '1', '1', '2');
  2145. $this->assertEquals($expected, $result);
  2146. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2147. $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2148. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2149. $this->assertEquals($expected, $result);
  2150. $result = $TestModel->updateAll(
  2151. array('Comment.comment' => "'Updated today'"),
  2152. array('Comment.user_id' => 5)
  2153. );
  2154. $this->assertFalse(empty($result));
  2155. $result = Set::extract(
  2156. $TestModel->find('all', array(
  2157. 'conditions' => array(
  2158. 'Comment.user_id' => 5
  2159. ))),
  2160. '{n}.Comment.comment'
  2161. );
  2162. $expected = array_fill(0, 2, 'Updated today');
  2163. $this->assertEquals($expected, $result);
  2164. }
  2165. /**
  2166. * testHabtmUuidWithUuidId method
  2167. *
  2168. * @return void
  2169. */
  2170. public function testHabtmUuidWithUuidId() {
  2171. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio', 'UuiditemsUuidportfolioNumericid');
  2172. $TestModel = new Uuidportfolio();
  2173. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2174. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2175. $TestModel->create($data);
  2176. $TestModel->save();
  2177. $id = $TestModel->id;
  2178. $result = $TestModel->read(null, $id);
  2179. $this->assertEquals(1, count($result['Uuiditem']));
  2180. $this->assertEquals(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
  2181. }
  2182. /**
  2183. * test HABTM saving when join table has no primary key and only 2 columns.
  2184. *
  2185. * @return void
  2186. */
  2187. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2188. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2189. $Fruit = new Fruit();
  2190. $data = array(
  2191. 'Fruit' => array(
  2192. 'color' => 'Red',
  2193. 'shape' => 'Heart-shaped',
  2194. 'taste' => 'sweet',
  2195. 'name' => 'Strawberry',
  2196. ),
  2197. 'UuidTag' => array(
  2198. 'UuidTag' => array(
  2199. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2200. )
  2201. )
  2202. );
  2203. $result = $Fruit->save($data);
  2204. $this->assertFalse(empty($result));
  2205. }
  2206. /**
  2207. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2212. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2213. $Fruit = new FruitNoWith();
  2214. $data = array(
  2215. 'Fruit' => array(
  2216. 'color' => 'Red',
  2217. 'shape' => 'Heart-shaped',
  2218. 'taste' => 'sweet',
  2219. 'name' => 'Strawberry',
  2220. ),
  2221. 'UuidTag' => array(
  2222. 'UuidTag' => array(
  2223. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2224. )
  2225. )
  2226. );
  2227. $result = $Fruit->save($data);
  2228. $this->assertFalse(empty($result));
  2229. }
  2230. /**
  2231. * testHabtmUuidWithNumericId method
  2232. *
  2233. * @return void
  2234. */
  2235. public function testHabtmUuidWithNumericId() {
  2236. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2237. $TestModel = new Uuiditem();
  2238. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2239. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2240. $TestModel->create($data);
  2241. $TestModel->save();
  2242. $id = $TestModel->id;
  2243. $result = $TestModel->read(null, $id);
  2244. $this->assertEquals(1, count($result['Uuidportfolio']));
  2245. }
  2246. /**
  2247. * testSaveMultipleHabtm method
  2248. *
  2249. * @return void
  2250. */
  2251. public function testSaveMultipleHabtm() {
  2252. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2253. $TestModel = new JoinA();
  2254. $result = $TestModel->findById(1);
  2255. $expected = array(
  2256. 'JoinA' => array(
  2257. 'id' => 1,
  2258. 'name' => 'Join A 1',
  2259. 'body' => 'Join A 1 Body',
  2260. 'created' => '2008-01-03 10:54:23',
  2261. 'updated' => '2008-01-03 10:54:23'
  2262. ),
  2263. 'JoinB' => array(
  2264. 0 => array(
  2265. 'id' => 2,
  2266. 'name' => 'Join B 2',
  2267. 'created' => '2008-01-03 10:55:02',
  2268. 'updated' => '2008-01-03 10:55:02',
  2269. 'JoinAsJoinB' => array(
  2270. 'id' => 1,
  2271. 'join_a_id' => 1,
  2272. 'join_b_id' => 2,
  2273. 'other' => 'Data for Join A 1 Join B 2',
  2274. 'created' => '2008-01-03 10:56:33',
  2275. 'updated' => '2008-01-03 10:56:33'
  2276. ))),
  2277. 'JoinC' => array(
  2278. 0 => array(
  2279. 'id' => 2,
  2280. 'name' => 'Join C 2',
  2281. 'created' => '2008-01-03 10:56:12',
  2282. 'updated' => '2008-01-03 10:56:12',
  2283. 'JoinAsJoinC' => array(
  2284. 'id' => 1,
  2285. 'join_a_id' => 1,
  2286. 'join_c_id' => 2,
  2287. 'other' => 'Data for Join A 1 Join C 2',
  2288. 'created' => '2008-01-03 10:57:22',
  2289. 'updated' => '2008-01-03 10:57:22'
  2290. ))));
  2291. $this->assertEquals($expected, $result);
  2292. $ts = date('Y-m-d H:i:s');
  2293. $TestModel->id = 1;
  2294. $data = array(
  2295. 'JoinA' => array(
  2296. 'id' => '1',
  2297. 'name' => 'New name for Join A 1',
  2298. 'updated' => $ts
  2299. ),
  2300. 'JoinB' => array(
  2301. array(
  2302. 'id' => 1,
  2303. 'join_b_id' => 2,
  2304. 'other' => 'New data for Join A 1 Join B 2',
  2305. 'created' => $ts,
  2306. 'updated' => $ts
  2307. )),
  2308. 'JoinC' => array(
  2309. array(
  2310. 'id' => 1,
  2311. 'join_c_id' => 2,
  2312. 'other' => 'New data for Join A 1 Join C 2',
  2313. 'created' => $ts,
  2314. 'updated' => $ts
  2315. )));
  2316. $TestModel->set($data);
  2317. $TestModel->save();
  2318. $result = $TestModel->findById(1);
  2319. $expected = array(
  2320. 'JoinA' => array(
  2321. 'id' => 1,
  2322. 'name' => 'New name for Join A 1',
  2323. 'body' => 'Join A 1 Body',
  2324. 'created' => '2008-01-03 10:54:23',
  2325. 'updated' => $ts
  2326. ),
  2327. 'JoinB' => array(
  2328. 0 => array(
  2329. 'id' => 2,
  2330. 'name' => 'Join B 2',
  2331. 'created' => '2008-01-03 10:55:02',
  2332. 'updated' => '2008-01-03 10:55:02',
  2333. 'JoinAsJoinB' => array(
  2334. 'id' => 1,
  2335. 'join_a_id' => 1,
  2336. 'join_b_id' => 2,
  2337. 'other' => 'New data for Join A 1 Join B 2',
  2338. 'created' => $ts,
  2339. 'updated' => $ts
  2340. ))),
  2341. 'JoinC' => array(
  2342. 0 => array(
  2343. 'id' => 2,
  2344. 'name' => 'Join C 2',
  2345. 'created' => '2008-01-03 10:56:12',
  2346. 'updated' => '2008-01-03 10:56:12',
  2347. 'JoinAsJoinC' => array(
  2348. 'id' => 1,
  2349. 'join_a_id' => 1,
  2350. 'join_c_id' => 2,
  2351. 'other' => 'New data for Join A 1 Join C 2',
  2352. 'created' => $ts,
  2353. 'updated' => $ts
  2354. ))));
  2355. $this->assertEquals($expected, $result);
  2356. }
  2357. /**
  2358. * testSaveAll method
  2359. *
  2360. * @return void
  2361. */
  2362. public function testSaveAll() {
  2363. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  2364. $TestModel = new Post();
  2365. $result = $TestModel->find('all');
  2366. $this->assertEquals(count($result), 3);
  2367. $this->assertFalse(isset($result[3]));
  2368. $ts = date('Y-m-d H:i:s');
  2369. $TestModel->saveAll(array(
  2370. 'Post' => array(
  2371. 'title' => 'Post with Author',
  2372. 'body' => 'This post will be saved with an author'
  2373. ),
  2374. 'Author' => array(
  2375. 'user' => 'bob',
  2376. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2377. )));
  2378. $result = $TestModel->find('all');
  2379. $expected = array(
  2380. 'Post' => array(
  2381. 'id' => '4',
  2382. 'author_id' => '5',
  2383. 'title' => 'Post with Author',
  2384. 'body' => 'This post will be saved with an author',
  2385. 'published' => 'N'
  2386. ),
  2387. 'Author' => array(
  2388. 'id' => '5',
  2389. 'user' => 'bob',
  2390. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2391. 'test' => 'working'
  2392. ));
  2393. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  2394. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  2395. $this->assertTrue($result[3]['Author']['created'] >= $ts);
  2396. $this->assertTrue($result[3]['Author']['updated'] >= $ts);
  2397. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  2398. unset($result[3]['Author']['created'], $result[3]['Author']['updated']);
  2399. $this->assertEquals($result[3], $expected);
  2400. $this->assertEquals(count($result), 4);
  2401. $TestModel->deleteAll(true);
  2402. $this->assertEquals($TestModel->find('all'), array());
  2403. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2404. $this->db->truncate($TestModel);
  2405. $ts = date('Y-m-d H:i:s');
  2406. $TestModel->saveAll(array(
  2407. array(
  2408. 'title' => 'Multi-record post 1',
  2409. 'body' => 'First multi-record post',
  2410. 'author_id' => 2
  2411. ),
  2412. array(
  2413. 'title' => 'Multi-record post 2',
  2414. 'body' => 'Second multi-record post',
  2415. 'author_id' => 2
  2416. )));
  2417. $result = $TestModel->find('all', array(
  2418. 'recursive' => -1,
  2419. 'order' => 'Post.id ASC'
  2420. ));
  2421. $expected = array(
  2422. array(
  2423. 'Post' => array(
  2424. 'id' => '1',
  2425. 'author_id' => '2',
  2426. 'title' => 'Multi-record post 1',
  2427. 'body' => 'First multi-record post',
  2428. 'published' => 'N'
  2429. )),
  2430. array(
  2431. 'Post' => array(
  2432. 'id' => '2',
  2433. 'author_id' => '2',
  2434. 'title' => 'Multi-record post 2',
  2435. 'body' => 'Second multi-record post',
  2436. 'published' => 'N'
  2437. )));
  2438. $this->assertTrue($result[0]['Post']['created'] >= $ts);
  2439. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  2440. $this->assertTrue($result[1]['Post']['created'] >= $ts);
  2441. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  2442. unset($result[0]['Post']['created'], $result[0]['Post']['updated']);
  2443. unset($result[1]['Post']['created'], $result[1]['Post']['updated']);
  2444. $this->assertEquals($expected, $result);
  2445. $TestModel = new Comment();
  2446. $ts = date('Y-m-d H:i:s');
  2447. $result = $TestModel->saveAll(array(
  2448. 'Comment' => array(
  2449. 'article_id' => 2,
  2450. 'user_id' => 2,
  2451. 'comment' => 'New comment with attachment',
  2452. 'published' => 'Y'
  2453. ),
  2454. 'Attachment' => array(
  2455. 'attachment' => 'some_file.tgz'
  2456. )));
  2457. $this->assertFalse(empty($result));
  2458. $result = $TestModel->find('all');
  2459. $expected = array(
  2460. 'id' => '7',
  2461. 'article_id' => '2',
  2462. 'user_id' => '2',
  2463. 'comment' => 'New comment with attachment',
  2464. 'published' => 'Y'
  2465. );
  2466. $this->assertTrue($result[6]['Comment']['created'] >= $ts);
  2467. $this->assertTrue($result[6]['Comment']['updated'] >= $ts);
  2468. unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']);
  2469. $this->assertEquals($result[6]['Comment'], $expected);
  2470. $expected = array(
  2471. 'id' => '2',
  2472. 'comment_id' => '7',
  2473. 'attachment' => 'some_file.tgz'
  2474. );
  2475. $this->assertTrue($result[6]['Attachment']['created'] >= $ts);
  2476. $this->assertTrue($result[6]['Attachment']['updated'] >= $ts);
  2477. unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']);
  2478. $this->assertEquals($result[6]['Attachment'], $expected);
  2479. }
  2480. /**
  2481. * Test SaveAll with Habtm relations
  2482. *
  2483. * @return void
  2484. */
  2485. public function testSaveAllHabtm() {
  2486. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  2487. $data = array(
  2488. 'Article' => array(
  2489. 'user_id' => 1,
  2490. 'title' => 'Article Has and belongs to Many Tags'
  2491. ),
  2492. 'Tag' => array(
  2493. 'Tag' => array(1, 2)
  2494. ),
  2495. 'Comment' => array(
  2496. array(
  2497. 'comment' => 'Article comment',
  2498. 'user_id' => 1
  2499. )));
  2500. $Article = new Article();
  2501. $result = $Article->saveAll($data);
  2502. $this->assertFalse(empty($result));
  2503. $result = $Article->read();
  2504. $this->assertEquals(count($result['Tag']), 2);
  2505. $this->assertEquals($result['Tag'][0]['tag'], 'tag1');
  2506. $this->assertEquals(count($result['Comment']), 1);
  2507. $this->assertEquals(count($result['Comment'][0]['comment']), 1);
  2508. }
  2509. /**
  2510. * Test SaveAll with Habtm relations and extra join table fields
  2511. *
  2512. * @return void
  2513. */
  2514. public function testSaveAllHabtmWithExtraJoinTableFields() {
  2515. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2516. $data = array(
  2517. 'Something' => array(
  2518. 'id' => 4,
  2519. 'title' => 'Extra Fields',
  2520. 'body' => 'Extra Fields Body',
  2521. 'published' => '1'
  2522. ),
  2523. 'SomethingElse' => array(
  2524. array('something_else_id' => 1, 'doomed' => '1'),
  2525. array('something_else_id' => 2, 'doomed' => '0'),
  2526. array('something_else_id' => 3, 'doomed' => '1')
  2527. )
  2528. );
  2529. $Something = new Something();
  2530. $result = $Something->saveAll($data);
  2531. $this->assertFalse(empty($result));
  2532. $result = $Something->read();
  2533. $this->assertEquals(count($result['SomethingElse']), 3);
  2534. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2535. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2536. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2537. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2538. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2539. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2540. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2541. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2542. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2543. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2544. }
  2545. /**
  2546. * testSaveAllHasOne method
  2547. *
  2548. * @return void
  2549. */
  2550. public function testSaveAllHasOne() {
  2551. $model = new Comment();
  2552. $model->deleteAll(true);
  2553. $this->assertEquals($model->find('all'), array());
  2554. $model->Attachment->deleteAll(true);
  2555. $this->assertEquals($model->Attachment->find('all'), array());
  2556. $this->assertTrue($model->saveAll(array(
  2557. 'Comment' => array(
  2558. 'comment' => 'Comment with attachment',
  2559. 'article_id' => 1,
  2560. 'user_id' => 1
  2561. ),
  2562. 'Attachment' => array(
  2563. 'attachment' => 'some_file.zip'
  2564. ))));
  2565. $result = $model->find('all', array('fields' => array(
  2566. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2567. 'Attachment.comment_id', 'Attachment.attachment'
  2568. )));
  2569. $expected = array(array(
  2570. 'Comment' => array(
  2571. 'id' => '1',
  2572. 'comment' => 'Comment with attachment'
  2573. ),
  2574. 'Attachment' => array(
  2575. 'id' => '1',
  2576. 'comment_id' => '1',
  2577. 'attachment' => 'some_file.zip'
  2578. )));
  2579. $this->assertEquals($expected, $result);
  2580. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2581. $data = 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. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2591. }
  2592. /**
  2593. * testSaveAllBelongsTo method
  2594. *
  2595. * @return void
  2596. */
  2597. public function testSaveAllBelongsTo() {
  2598. $model = new Comment();
  2599. $model->deleteAll(true);
  2600. $this->assertEquals($model->find('all'), array());
  2601. $model->Article->deleteAll(true);
  2602. $this->assertEquals($model->Article->find('all'), array());
  2603. $this->assertTrue($model->saveAll(array(
  2604. 'Comment' => array(
  2605. 'comment' => 'Article comment',
  2606. 'article_id' => 1,
  2607. 'user_id' => 1
  2608. ),
  2609. 'Article' => array(
  2610. 'title' => 'Model Associations 101',
  2611. 'user_id' => 1
  2612. ))));
  2613. $result = $model->find('all', array('fields' => array(
  2614. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2615. )));
  2616. $expected = array(array(
  2617. 'Comment' => array(
  2618. 'id' => '1',
  2619. 'article_id' => '1',
  2620. 'comment' => 'Article comment'
  2621. ),
  2622. 'Article' => array(
  2623. 'id' => '1',
  2624. 'title' => 'Model Associations 101'
  2625. )));
  2626. $this->assertEquals($expected, $result);
  2627. }
  2628. /**
  2629. * testSaveAllHasOneValidation method
  2630. *
  2631. * @return void
  2632. */
  2633. public function testSaveAllHasOneValidation() {
  2634. $model = new Comment();
  2635. $model->deleteAll(true);
  2636. $this->assertEquals($model->find('all'), array());
  2637. $model->Attachment->deleteAll(true);
  2638. $this->assertEquals($model->Attachment->find('all'), array());
  2639. $model->validate = array('comment' => 'notEmpty');
  2640. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2641. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2642. $this->assertEquals($model->saveAll(
  2643. array(
  2644. 'Comment' => array(
  2645. 'comment' => '',
  2646. 'article_id' => 1,
  2647. 'user_id' => 1
  2648. ),
  2649. 'Attachment' => array('attachment' => '')
  2650. ),
  2651. array('validate' => 'first')
  2652. ), false);
  2653. $expected = array(
  2654. 'Comment' => array('comment' => array('This field cannot be left blank')),
  2655. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  2656. );
  2657. $this->assertEquals($model->validationErrors, $expected['Comment']);
  2658. $this->assertEquals($model->Attachment->validationErrors, $expected['Attachment']);
  2659. $this->assertFalse($model->saveAll(
  2660. array(
  2661. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  2662. 'Attachment' => array('attachment' => '')
  2663. ),
  2664. array('validate' => 'only')
  2665. ));
  2666. $this->assertEquals($model->validationErrors, $expected['Comment']);
  2667. $this->assertEquals($model->Attachment->validationErrors, $expected['Attachment']);
  2668. }
  2669. /**
  2670. * testSaveAllAtomic method
  2671. *
  2672. * @return void
  2673. */
  2674. public function testSaveAllAtomic() {
  2675. $this->loadFixtures('Article', 'User');
  2676. $TestModel = new Article();
  2677. $result = $TestModel->saveAll(array(
  2678. 'Article' => array(
  2679. 'title' => 'Post with Author',
  2680. 'body' => 'This post will be saved with an author',
  2681. 'user_id' => 2
  2682. ),
  2683. 'Comment' => array(
  2684. array('comment' => 'First new comment', 'user_id' => 2))
  2685. ), array('atomic' => false));
  2686. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  2687. $result = $TestModel->saveAll(array(
  2688. array(
  2689. 'id' => '1',
  2690. 'title' => 'Baleeted First Post',
  2691. 'body' => 'Baleeted!',
  2692. 'published' => 'N'
  2693. ),
  2694. array(
  2695. 'id' => '2',
  2696. 'title' => 'Just update the title'
  2697. ),
  2698. array(
  2699. 'title' => 'Creating a fourth post',
  2700. 'body' => 'Fourth post body',
  2701. 'user_id' => 2
  2702. )
  2703. ), array('atomic' => false));
  2704. $this->assertSame($result, array(true, true, true));
  2705. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  2706. $result = $TestModel->saveAll(array(
  2707. array(
  2708. 'id' => '1',
  2709. 'title' => 'Un-Baleeted First Post',
  2710. 'body' => 'Not Baleeted!',
  2711. 'published' => 'Y'
  2712. ),
  2713. array(
  2714. 'id' => '2',
  2715. 'title' => '',
  2716. 'body' => 'Trying to get away with an empty title'
  2717. )
  2718. ), array('validate' => true, 'atomic' => false));
  2719. $this->assertSame($result, array(true, false));
  2720. $result = $TestModel->saveAll(array(
  2721. 'Article' => array('id' => 2),
  2722. 'Comment' => array(
  2723. array(
  2724. 'comment' => 'First new comment',
  2725. 'published' => 'Y',
  2726. 'user_id' => 1
  2727. ),
  2728. array(
  2729. 'comment' => 'Second new comment',
  2730. 'published' => 'Y',
  2731. 'user_id' => 2
  2732. ))
  2733. ), array('validate' => true, 'atomic' => false));
  2734. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  2735. }
  2736. /**
  2737. * testSaveAllHasMany method
  2738. *
  2739. * @return void
  2740. */
  2741. public function testSaveAllHasMany() {
  2742. $this->loadFixtures('Article', 'Comment');
  2743. $TestModel = new Article();
  2744. $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
  2745. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2746. $result = $TestModel->saveAll(array(
  2747. 'Article' => array('id' => 2),
  2748. 'Comment' => array(
  2749. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  2750. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2751. )
  2752. ));
  2753. $this->assertFalse(empty($result));
  2754. $result = $TestModel->findById(2);
  2755. $expected = array(
  2756. 'First Comment for Second Article',
  2757. 'Second Comment for Second Article',
  2758. 'First new comment',
  2759. 'Second new comment'
  2760. );
  2761. $this->assertEquals(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2762. $result = $TestModel->saveAll(
  2763. array(
  2764. 'Article' => array('id' => 2),
  2765. 'Comment' => array(
  2766. array(
  2767. 'comment' => 'Third new comment',
  2768. 'published' => 'Y',
  2769. 'user_id' => 1
  2770. ))),
  2771. array('atomic' => false)
  2772. );
  2773. $this->assertFalse(empty($result));
  2774. $result = $TestModel->findById(2);
  2775. $expected = array(
  2776. 'First Comment for Second Article',
  2777. 'Second Comment for Second Article',
  2778. 'First new comment',
  2779. 'Second new comment',
  2780. 'Third new comment'
  2781. );
  2782. $this->assertEquals(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2783. $TestModel->beforeSaveReturn = false;
  2784. $result = $TestModel->saveAll(
  2785. array(
  2786. 'Article' => array('id' => 2),
  2787. 'Comment' => array(
  2788. array(
  2789. 'comment' => 'Fourth new comment',
  2790. 'published' => 'Y',
  2791. 'user_id' => 1
  2792. ))),
  2793. array('atomic' => false)
  2794. );
  2795. $this->assertEquals($result, array('Article' => false));
  2796. $result = $TestModel->findById(2);
  2797. $expected = array(
  2798. 'First Comment for Second Article',
  2799. 'Second Comment for Second Article',
  2800. 'First new comment',
  2801. 'Second new comment',
  2802. 'Third new comment'
  2803. );
  2804. $this->assertEquals(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2805. }
  2806. /**
  2807. * testSaveAllHasManyValidation method
  2808. *
  2809. * @return void
  2810. */
  2811. public function testSaveAllHasManyValidation() {
  2812. $this->loadFixtures('Article', 'Comment');
  2813. $TestModel = new Article();
  2814. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2815. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  2816. $result = $TestModel->saveAll(array(
  2817. 'Article' => array('id' => 2),
  2818. 'Comment' => array(
  2819. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  2820. )
  2821. ), array('validate' => true));
  2822. $this->assertFalse($result);
  2823. $expected = array('Comment' => array(
  2824. array('comment' => array('This field cannot be left blank'))
  2825. ));
  2826. $this->assertEquals($TestModel->validationErrors, $expected);
  2827. $expected = array(
  2828. array('comment' => array('This field cannot be left blank'))
  2829. );
  2830. $this->assertEquals($TestModel->Comment->validationErrors, $expected);
  2831. $result = $TestModel->saveAll(array(
  2832. 'Article' => array('id' => 2),
  2833. 'Comment' => array(
  2834. array(
  2835. 'comment' => '',
  2836. 'published' => 'Y',
  2837. 'user_id' => 1
  2838. ))
  2839. ), array('validate' => 'first'));
  2840. $this->assertFalse($result);
  2841. }
  2842. /**
  2843. * test saveAll with transactions and ensure there is no missing rollback.
  2844. *
  2845. * @return void
  2846. */
  2847. public function testSaveAllManyRowsTransactionNoRollback() {
  2848. $this->loadFixtures('Post');
  2849. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockTransactionDboSource');
  2850. $db = ConnectionManager::create('mock_transaction', array(
  2851. 'datasource' => 'MockTransactionDboSource',
  2852. ));
  2853. $db->expects($this->once())
  2854. ->method('describe')
  2855. ->will($this->returnValue(array()));
  2856. $db->expects($this->once())->method('rollback');
  2857. $Post = new Post('mock_transaction');
  2858. $Post->validate = array(
  2859. 'title' => array('rule' => array('notEmpty'))
  2860. );
  2861. $data = array(
  2862. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2863. array('author_id' => 1, 'title' => '')
  2864. );
  2865. $Post->saveAll($data, array('atomic' => true));
  2866. }
  2867. /**
  2868. * test saveAll with transactions and ensure there is no missing rollback.
  2869. *
  2870. * @return void
  2871. */
  2872. public function testSaveAllAssociatedTransactionNoRollback() {
  2873. $testDb = ConnectionManager::getDataSource('test');
  2874. $mock = $this->getMock(
  2875. 'DboSource',
  2876. array('connect', 'rollback', 'describe', 'create', 'update', 'begin'),
  2877. array(),
  2878. 'MockTransactionAssociatedDboSource'
  2879. );
  2880. $db = ConnectionManager::create('mock_transaction_assoc', array(
  2881. 'datasource' => 'MockTransactionAssociatedDboSource',
  2882. ));
  2883. $this->mockObjects[] = $db;
  2884. $db->columns = $testDb->columns;
  2885. $db->expects($this->once())->method('rollback');
  2886. $db->expects($this->any())->method('describe')
  2887. ->will($this->returnValue(array(
  2888. 'id' => array('type' => 'integer'),
  2889. 'title' => array('type' => 'string'),
  2890. 'body' => array('type' => 'text'),
  2891. 'published' => array('type' => 'string')
  2892. )));
  2893. $Post = new Post();
  2894. $Post->useDbConfig = 'mock_transaction_assoc';
  2895. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  2896. $Post->Author->validate = array(
  2897. 'user' => array('rule' => array('notEmpty'))
  2898. );
  2899. $data = array(
  2900. 'Post' => array(
  2901. 'title' => 'New post',
  2902. 'body' => 'Content',
  2903. 'published' => 'Y'
  2904. ),
  2905. 'Author' => array(
  2906. 'user' => '',
  2907. 'password' => "sekret"
  2908. )
  2909. );
  2910. $Post->saveAll($data, array('validate' => true));
  2911. }
  2912. /**
  2913. * test saveAll with nested saveAll call.
  2914. *
  2915. * @return void
  2916. */
  2917. public function testSaveAllNestedSaveAll() {
  2918. $this->loadFixtures('Sample');
  2919. $TransactionTestModel = new TransactionTestModel();
  2920. $data = array(
  2921. array('apple_id' => 1, 'name' => 'sample5'),
  2922. );
  2923. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  2924. }
  2925. /**
  2926. * testSaveAllTransaction method
  2927. *
  2928. * @return void
  2929. */
  2930. public function testSaveAllTransaction() {
  2931. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2932. $TestModel = new Post();
  2933. $TestModel->validate = array('title' => 'notEmpty');
  2934. $data = array(
  2935. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2936. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2937. array('author_id' => 1, 'title' => '')
  2938. );
  2939. $ts = date('Y-m-d H:i:s');
  2940. $this->assertFalse($TestModel->saveAll($data));
  2941. $result = $TestModel->find('all', array('recursive' => -1));
  2942. $expected = array(
  2943. array('Post' => array(
  2944. 'id' => '1',
  2945. 'author_id' => 1,
  2946. 'title' => 'First Post',
  2947. 'body' => 'First Post Body',
  2948. 'published' => 'Y',
  2949. 'created' => '2007-03-18 10:39:23',
  2950. 'updated' => '2007-03-18 10:41:31'
  2951. )),
  2952. array('Post' => array(
  2953. 'id' => '2',
  2954. 'author_id' => 3,
  2955. 'title' => 'Second Post',
  2956. 'body' => 'Second Post Body',
  2957. 'published' => 'Y',
  2958. 'created' => '2007-03-18 10:41:23',
  2959. 'updated' => '2007-03-18 10:43:31'
  2960. )),
  2961. array('Post' => array(
  2962. 'id' => '3',
  2963. 'author_id' => 1,
  2964. 'title' => 'Third Post',
  2965. 'body' => 'Third Post Body',
  2966. 'published' => 'Y',
  2967. 'created' => '2007-03-18 10:43:23',
  2968. 'updated' => '2007-03-18 10:45:31'
  2969. )));
  2970. if (count($result) != 3) {
  2971. // Database doesn't support transactions
  2972. $expected[] = array(
  2973. 'Post' => array(
  2974. 'id' => '4',
  2975. 'author_id' => 1,
  2976. 'title' => 'New Fourth Post',
  2977. 'body' => null,
  2978. 'published' => 'N',
  2979. 'created' => $ts,
  2980. 'updated' => $ts
  2981. ));
  2982. $expected[] = array(
  2983. 'Post' => array(
  2984. 'id' => '5',
  2985. 'author_id' => 1,
  2986. 'title' => 'New Fifth Post',
  2987. 'body' => null,
  2988. 'published' => 'N',
  2989. 'created' => $ts,
  2990. 'updated' => $ts
  2991. ));
  2992. $this->assertEquals($expected, $result);
  2993. // Skip the rest of the transactional tests
  2994. return;
  2995. }
  2996. $this->assertEquals($expected, $result);
  2997. $data = array(
  2998. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2999. array('author_id' => 1, 'title' => ''),
  3000. array('author_id' => 1, 'title' => 'New Sixth Post')
  3001. );
  3002. $ts = date('Y-m-d H:i:s');
  3003. $this->assertFalse($TestModel->saveAll($data));
  3004. $result = $TestModel->find('all', array('recursive' => -1));
  3005. $expected = array(
  3006. array('Post' => array(
  3007. 'id' => '1',
  3008. 'author_id' => 1,
  3009. 'title' => 'First Post',
  3010. 'body' => 'First Post Body',
  3011. 'published' => 'Y',
  3012. 'created' => '2007-03-18 10:39:23',
  3013. 'updated' => '2007-03-18 10:41:31'
  3014. )),
  3015. array('Post' => array(
  3016. 'id' => '2',
  3017. 'author_id' => 3,
  3018. 'title' => 'Second Post',
  3019. 'body' => 'Second Post Body',
  3020. 'published' => 'Y',
  3021. 'created' => '2007-03-18 10:41:23',
  3022. 'updated' => '2007-03-18 10:43:31'
  3023. )),
  3024. array('Post' => array(
  3025. 'id' => '3',
  3026. 'author_id' => 1,
  3027. 'title' => 'Third Post',
  3028. 'body' => 'Third Post Body',
  3029. 'published' => 'Y',
  3030. 'created' => '2007-03-18 10:43:23',
  3031. 'updated' => '2007-03-18 10:45:31'
  3032. )));
  3033. if (count($result) != 3) {
  3034. // Database doesn't support transactions
  3035. $expected[] = array(
  3036. 'Post' => array(
  3037. 'id' => '4',
  3038. 'author_id' => 1,
  3039. 'title' => 'New Fourth Post',
  3040. 'body' => 'Third Post Body',
  3041. 'published' => 'N',
  3042. 'created' => $ts,
  3043. 'updated' => $ts
  3044. ));
  3045. $expected[] = array(
  3046. 'Post' => array(
  3047. 'id' => '5',
  3048. 'author_id' => 1,
  3049. 'title' => 'Third Post',
  3050. 'body' => 'Third Post Body',
  3051. 'published' => 'N',
  3052. 'created' => $ts,
  3053. 'updated' => $ts
  3054. ));
  3055. }
  3056. $this->assertEquals($expected, $result);
  3057. $TestModel->validate = array('title' => 'notEmpty');
  3058. $data = array(
  3059. array('author_id' => 1, 'title' => 'New Fourth Post'),
  3060. array('author_id' => 1, 'title' => 'New Fifth Post'),
  3061. array('author_id' => 1, 'title' => 'New Sixth Post')
  3062. );
  3063. $this->assertTrue($TestModel->saveAll($data));
  3064. $result = $TestModel->find('all', array(
  3065. 'recursive' => -1,
  3066. 'fields' => array('author_id', 'title','body','published')
  3067. ));
  3068. $expected = array(
  3069. array('Post' => array(
  3070. 'author_id' => 1,
  3071. 'title' => 'First Post',
  3072. 'body' => 'First Post Body',
  3073. 'published' => 'Y'
  3074. )),
  3075. array('Post' => array(
  3076. 'author_id' => 3,
  3077. 'title' => 'Second Post',
  3078. 'body' => 'Second Post Body',
  3079. 'published' => 'Y'
  3080. )),
  3081. array('Post' => array(
  3082. 'author_id' => 1,
  3083. 'title' => 'Third Post',
  3084. 'body' => 'Third Post Body',
  3085. 'published' => 'Y'
  3086. )),
  3087. array('Post' => array(
  3088. 'author_id' => 1,
  3089. 'title' => 'New Fourth Post',
  3090. 'body' => '',
  3091. 'published' => 'N'
  3092. )),
  3093. array('Post' => array(
  3094. 'author_id' => 1,
  3095. 'title' => 'New Fifth Post',
  3096. 'body' => '',
  3097. 'published' => 'N'
  3098. )),
  3099. array('Post' => array(
  3100. 'author_id' => 1,
  3101. 'title' => 'New Sixth Post',
  3102. 'body' => '',
  3103. 'published' => 'N'
  3104. )));
  3105. $this->assertEquals($expected, $result);
  3106. }
  3107. /**
  3108. * testSaveAllValidation method
  3109. *
  3110. * @return void
  3111. */
  3112. public function testSaveAllValidation() {
  3113. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3114. $TestModel = new Post();
  3115. $data = array(
  3116. array(
  3117. 'id' => '1',
  3118. 'title' => 'Baleeted First Post',
  3119. 'body' => 'Baleeted!',
  3120. 'published' => 'N'
  3121. ),
  3122. array(
  3123. 'id' => '2',
  3124. 'title' => 'Just update the title'
  3125. ),
  3126. array(
  3127. 'title' => 'Creating a fourth post',
  3128. 'body' => 'Fourth post body',
  3129. 'author_id' => 2
  3130. ));
  3131. $ts = date('Y-m-d H:i:s');
  3132. $this->assertTrue($TestModel->saveAll($data));
  3133. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3134. $expected = array(
  3135. array(
  3136. 'Post' => array(
  3137. 'id' => '1',
  3138. 'author_id' => '1',
  3139. 'title' => 'Baleeted First Post',
  3140. 'body' => 'Baleeted!',
  3141. 'published' => 'N',
  3142. 'created' => '2007-03-18 10:39:23'
  3143. )),
  3144. array(
  3145. 'Post' => array(
  3146. 'id' => '2',
  3147. 'author_id' => '3',
  3148. 'title' => 'Just update the title',
  3149. 'body' => 'Second Post Body',
  3150. 'published' => 'Y',
  3151. 'created' => '2007-03-18 10:41:23'
  3152. )),
  3153. array(
  3154. 'Post' => array(
  3155. 'id' => '3',
  3156. 'author_id' => '1',
  3157. 'title' => 'Third Post',
  3158. 'body' => 'Third Post Body',
  3159. 'published' => 'Y',
  3160. 'created' => '2007-03-18 10:43:23',
  3161. 'updated' => '2007-03-18 10:45:31'
  3162. )),
  3163. array(
  3164. 'Post' => array(
  3165. 'id' => '4',
  3166. 'author_id' => '2',
  3167. 'title' => 'Creating a fourth post',
  3168. 'body' => 'Fourth post body',
  3169. 'published' => 'N'
  3170. )));
  3171. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  3172. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  3173. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  3174. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  3175. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  3176. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  3177. $this->assertEquals($expected, $result);
  3178. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3179. $data = array(
  3180. array(
  3181. 'id' => '1',
  3182. 'title' => 'Un-Baleeted First Post',
  3183. 'body' => 'Not Baleeted!',
  3184. 'published' => 'Y'
  3185. ),
  3186. array(
  3187. 'id' => '2',
  3188. 'title' => '',
  3189. 'body' => 'Trying to get away with an empty title'
  3190. ));
  3191. $result = $TestModel->saveAll($data);
  3192. $this->assertFalse($result);
  3193. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3194. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3195. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3196. if (!$transactionWorked) {
  3197. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3198. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3199. }
  3200. $this->assertEquals($TestModel->validationErrors, $errors);
  3201. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3202. $data = array(
  3203. array(
  3204. 'id' => '1',
  3205. 'title' => 'Un-Baleeted First Post',
  3206. 'body' => 'Not Baleeted!',
  3207. 'published' => 'Y'
  3208. ),
  3209. array(
  3210. 'id' => '2',
  3211. 'title' => '',
  3212. 'body' => 'Trying to get away with an empty title'
  3213. ));
  3214. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3215. $this->assertEquals($result, array(true, false));
  3216. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3217. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  3218. $expected = array(
  3219. array(
  3220. 'Post' => array(
  3221. 'id' => '1',
  3222. 'author_id' => '1',
  3223. 'title' => 'Un-Baleeted First Post',
  3224. 'body' => 'Not Baleeted!',
  3225. 'published' => 'Y',
  3226. 'created' => '2007-03-18 10:39:23'
  3227. )
  3228. ),
  3229. array(
  3230. 'Post' => array(
  3231. 'id' => '2',
  3232. 'author_id' => '3',
  3233. 'title' => 'Just update the title',
  3234. 'body' => 'Second Post Body',
  3235. 'published' => 'Y',
  3236. 'created' => '2007-03-18 10:41:23'
  3237. )
  3238. ),
  3239. array(
  3240. 'Post' => array(
  3241. 'id' => '3',
  3242. 'author_id' => '1',
  3243. 'title' => 'Third Post',
  3244. 'body' => 'Third Post Body',
  3245. 'published' => 'Y',
  3246. 'created' => '2007-03-18 10:43:23',
  3247. 'updated' => '2007-03-18 10:45:31'
  3248. )
  3249. ),
  3250. array(
  3251. 'Post' => array(
  3252. 'id' => '4',
  3253. 'author_id' => '2',
  3254. 'title' => 'Creating a fourth post',
  3255. 'body' => 'Fourth post body',
  3256. 'published' => 'N'
  3257. )
  3258. )
  3259. );
  3260. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  3261. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  3262. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  3263. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  3264. unset(
  3265. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  3266. $result[3]['Post']['updated'], $result[3]['Post']['created']
  3267. );
  3268. $this->assertEquals($expected, $result);
  3269. $this->assertEquals($TestModel->validationErrors, $errors);
  3270. $data = array(
  3271. array(
  3272. 'id' => '1',
  3273. 'title' => 'Re-Baleeted First Post',
  3274. 'body' => 'Baleeted!',
  3275. 'published' => 'N'
  3276. ),
  3277. array(
  3278. 'id' => '2',
  3279. 'title' => '',
  3280. 'body' => 'Trying to get away with an empty title'
  3281. ));
  3282. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  3283. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3284. unset(
  3285. $result[0]['Post']['updated'], $result[1]['Post']['updated'],
  3286. $result[3]['Post']['updated'], $result[3]['Post']['created']
  3287. );
  3288. $this->assertEquals($expected, $result);
  3289. $this->assertEquals($TestModel->validationErrors, $errors);
  3290. }
  3291. /**
  3292. * testSaveAllValidationOnly method
  3293. *
  3294. * @return void
  3295. */
  3296. public function testSaveAllValidationOnly() {
  3297. $this->loadFixtures('Comment', 'Attachment');
  3298. $TestModel = new Comment();
  3299. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  3300. $data = array(
  3301. 'Comment' => array(
  3302. 'comment' => 'This is the comment'
  3303. ),
  3304. 'Attachment' => array(
  3305. 'attachment' => ''
  3306. )
  3307. );
  3308. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  3309. $this->assertFalse($result);
  3310. $TestModel = new Article();
  3311. $TestModel->validate = array('title' => 'notEmpty');
  3312. $result = $TestModel->saveAll(
  3313. array(
  3314. 0 => array('title' => ''),
  3315. 1 => array('title' => 'title 1'),
  3316. 2 => array('title' => 'title 2'),
  3317. ),
  3318. array('validate' => 'only')
  3319. );
  3320. $this->assertFalse($result);
  3321. $expected = array(
  3322. 0 => array('title' => array('This field cannot be left blank')),
  3323. );
  3324. $this->assertEquals($TestModel->validationErrors, $expected);
  3325. $result = $TestModel->saveAll(
  3326. array(
  3327. 0 => array('title' => 'title 0'),
  3328. 1 => array('title' => ''),
  3329. 2 => array('title' => 'title 2'),
  3330. ),
  3331. array('validate' => 'only')
  3332. );
  3333. $this->assertFalse($result);
  3334. $expected = array(
  3335. 1 => array('title' => array('This field cannot be left blank')),
  3336. );
  3337. $this->assertEquals($TestModel->validationErrors, $expected);
  3338. }
  3339. /**
  3340. * testSaveAllValidateFirst method
  3341. *
  3342. * @return void
  3343. */
  3344. public function testSaveAllValidateFirst() {
  3345. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3346. $model = new Article();
  3347. $model->deleteAll(true);
  3348. $model->Comment->validate = array('comment' => 'notEmpty');
  3349. $result = $model->saveAll(array(
  3350. 'Article' => array(
  3351. 'title' => 'Post with Author',
  3352. 'body' => 'This post will be saved author'
  3353. ),
  3354. 'Comment' => array(
  3355. array('comment' => 'First new comment'),
  3356. array('comment' => '')
  3357. )
  3358. ), array('validate' => 'first'));
  3359. $this->assertFalse($result);
  3360. $result = $model->find('all');
  3361. $this->assertEquals($result, array());
  3362. $expected = array('Comment' => array(
  3363. 1 => array('comment' => array('This field cannot be left blank'))
  3364. ));
  3365. $this->assertEquals($model->Comment->validationErrors, $expected['Comment']);
  3366. $this->assertSame($model->Comment->find('count'), 0);
  3367. $result = $model->saveAll(
  3368. array(
  3369. 'Article' => array(
  3370. 'title' => 'Post with Author',
  3371. 'body' => 'This post will be saved with an author',
  3372. 'user_id' => 2
  3373. ),
  3374. 'Comment' => array(
  3375. array(
  3376. 'comment' => 'Only new comment',
  3377. 'user_id' => 2
  3378. ))),
  3379. array('validate' => 'first')
  3380. );
  3381. $this->assertSame($result, true);
  3382. $result = $model->Comment->find('all');
  3383. $this->assertSame(count($result), 1);
  3384. $result = Set::extract('/Comment/article_id', $result);
  3385. $this->assertEquals($result[0], 4);
  3386. $model->deleteAll(true);
  3387. $data = array(
  3388. 'Article' => array(
  3389. 'title' => 'Post with Author saveAlled from comment',
  3390. 'body' => 'This post will be saved with an author',
  3391. 'user_id' => 2
  3392. ),
  3393. 'Comment' => array(
  3394. 'comment' => 'Only new comment', 'user_id' => 2
  3395. ));
  3396. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  3397. $this->assertFalse(empty($result));
  3398. $result = $model->find('all');
  3399. $this->assertEquals(
  3400. $result[0]['Article']['title'],
  3401. 'Post with Author saveAlled from comment'
  3402. );
  3403. $this->assertEquals($result[0]['Comment'][0]['comment'], 'Only new comment');
  3404. }
  3405. /**
  3406. * test saveAll()'s return is correct when using atomic = false and validate = first.
  3407. *
  3408. * @return void
  3409. */
  3410. public function testSaveAllValidateFirstAtomicFalse() {
  3411. $Something = new Something();
  3412. $invalidData = array(
  3413. array(
  3414. 'title' => 'foo',
  3415. 'body' => 'bar',
  3416. 'published' => 'baz',
  3417. ),
  3418. array(
  3419. 'body' => 3,
  3420. 'published' => 'sd',
  3421. ),
  3422. );
  3423. $Something->create();
  3424. $Something->validate = array(
  3425. 'title' => array(
  3426. 'rule' => 'alphaNumeric',
  3427. 'required' => true,
  3428. ),
  3429. 'body' => array(
  3430. 'rule' => 'alphaNumeric',
  3431. 'required' => true,
  3432. 'allowEmpty' => true,
  3433. ),
  3434. );
  3435. $result = $Something->saveAll($invalidData, array(
  3436. 'atomic' => false,
  3437. 'validate' => 'first',
  3438. ));
  3439. $expected = array(true, false);
  3440. $this->assertEquals($expected, $result);
  3441. $Something = new Something();
  3442. $validData = array(
  3443. array(
  3444. 'title' => 'title value',
  3445. 'body' => 'body value',
  3446. 'published' => 'baz',
  3447. ),
  3448. array(
  3449. 'title' => 'valid',
  3450. 'body' => 'this body',
  3451. 'published' => 'sd',
  3452. ),
  3453. );
  3454. $Something->create();
  3455. $result = $Something->saveAll($validData, array(
  3456. 'atomic' => false,
  3457. 'validate' => 'first',
  3458. ));
  3459. $expected = array(true, true);
  3460. $this->assertEquals($expected, $result);
  3461. }
  3462. /**
  3463. * testSaveAllHasManyValidationOnly method
  3464. *
  3465. * @return void
  3466. */
  3467. public function testSaveAllHasManyValidationOnly() {
  3468. $this->loadFixtures('Article', 'Comment', 'Attachment');
  3469. $TestModel = new Article();
  3470. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3471. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3472. $result = $TestModel->saveAll(
  3473. array(
  3474. 'Article' => array('id' => 2),
  3475. 'Comment' => array(
  3476. array(
  3477. 'id' => 1,
  3478. 'comment' => '',
  3479. 'published' => 'Y',
  3480. 'user_id' => 1),
  3481. array(
  3482. 'id' => 2,
  3483. 'comment' =>
  3484. 'comment',
  3485. 'published' => 'Y',
  3486. 'user_id' => 1
  3487. ))),
  3488. array('validate' => 'only')
  3489. );
  3490. $this->assertFalse($result);
  3491. $result = $TestModel->saveAll(
  3492. array(
  3493. 'Article' => array('id' => 2),
  3494. 'Comment' => array(
  3495. array(
  3496. 'id' => 1,
  3497. 'comment' => '',
  3498. 'published' => 'Y',
  3499. 'user_id' => 1
  3500. ),
  3501. array(
  3502. 'id' => 2,
  3503. 'comment' => 'comment',
  3504. 'published' => 'Y',
  3505. 'user_id' => 1
  3506. ),
  3507. array(
  3508. 'id' => 3,
  3509. 'comment' => '',
  3510. 'published' => 'Y',
  3511. 'user_id' => 1
  3512. ))),
  3513. array(
  3514. 'validate' => 'only',
  3515. 'atomic' => false
  3516. ));
  3517. $expected = array(
  3518. 'Article' => true,
  3519. 'Comment' => array(false, true, false)
  3520. );
  3521. $this->assertSame($expected, $result);
  3522. $expected = array('Comment' => array(
  3523. 0 => array('comment' => array('This field cannot be left blank')),
  3524. 2 => array('comment' => array('This field cannot be left blank'))
  3525. ));
  3526. $this->assertEquals($TestModel->validationErrors, $expected);
  3527. $expected = array(
  3528. 0 => array('comment' => array('This field cannot be left blank')),
  3529. 2 => array('comment' => array('This field cannot be left blank'))
  3530. );
  3531. $this->assertEquals($TestModel->Comment->validationErrors, $expected);
  3532. }
  3533. /**
  3534. * test that saveAll behaves like plain save() when suplied empty data
  3535. *
  3536. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  3537. * @return void
  3538. */
  3539. public function testSaveAllEmptyData() {
  3540. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  3541. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  3542. $model = new Article();
  3543. $result = $model->saveAll(array(), array('validate' => 'first'));
  3544. $this->assertFalse(empty($result));
  3545. $model = new ProductUpdateAll();
  3546. $result = $model->saveAll(array());
  3547. $this->assertFalse($result);
  3548. }
  3549. /**
  3550. * testSaveAssociated method
  3551. *
  3552. * @return void
  3553. */
  3554. public function testSaveAssociated() {
  3555. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User');
  3556. $TestModel = new Post();
  3557. $result = $TestModel->find('all');
  3558. $this->assertEquals(count($result), 3);
  3559. $this->assertFalse(isset($result[3]));
  3560. $ts = date('Y-m-d H:i:s');
  3561. $TestModel->saveAssociated(array(
  3562. 'Post' => array(
  3563. 'title' => 'Post with Author',
  3564. 'body' => 'This post will be saved with an author'
  3565. ),
  3566. 'Author' => array(
  3567. 'user' => 'bob',
  3568. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  3569. )));
  3570. $result = $TestModel->find('all');
  3571. $expected = array(
  3572. 'Post' => array(
  3573. 'id' => '4',
  3574. 'author_id' => '5',
  3575. 'title' => 'Post with Author',
  3576. 'body' => 'This post will be saved with an author',
  3577. 'published' => 'N'
  3578. ),
  3579. 'Author' => array(
  3580. 'id' => '5',
  3581. 'user' => 'bob',
  3582. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  3583. 'test' => 'working'
  3584. ));
  3585. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  3586. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  3587. $this->assertTrue($result[3]['Author']['created'] >= $ts);
  3588. $this->assertTrue($result[3]['Author']['updated'] >= $ts);
  3589. unset(
  3590. $result[3]['Post']['updated'], $result[3]['Post']['created'],
  3591. $result[3]['Author']['updated'], $result[3]['Author']['created']
  3592. );
  3593. $this->assertEquals($result[3], $expected);
  3594. $this->assertEquals(count($result), 4);
  3595. $ts = date('Y-m-d H:i:s');
  3596. $TestModel = new Comment();
  3597. $ts = date('Y-m-d H:i:s');
  3598. $result = $TestModel->saveAssociated(array(
  3599. 'Comment' => array(
  3600. 'article_id' => 2,
  3601. 'user_id' => 2,
  3602. 'comment' => 'New comment with attachment',
  3603. 'published' => 'Y'
  3604. ),
  3605. 'Attachment' => array(
  3606. 'attachment' => 'some_file.tgz'
  3607. )));
  3608. $this->assertFalse(empty($result));
  3609. $result = $TestModel->find('all');
  3610. $expected = array(
  3611. 'id' => '7',
  3612. 'article_id' => '2',
  3613. 'user_id' => '2',
  3614. 'comment' => 'New comment with attachment',
  3615. 'published' => 'Y'
  3616. );
  3617. $this->assertTrue($result[6]['Comment']['updated'] >= $ts);
  3618. $this->assertTrue($result[6]['Comment']['created'] >= $ts);
  3619. unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']);
  3620. $this->assertEquals($result[6]['Comment'], $expected);
  3621. $expected = array(
  3622. 'id' => '2',
  3623. 'comment_id' => '7',
  3624. 'attachment' => 'some_file.tgz'
  3625. );
  3626. $this->assertTrue($result[6]['Attachment']['updated'] >= $ts);
  3627. $this->assertTrue($result[6]['Attachment']['created'] >= $ts);
  3628. unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']);
  3629. $this->assertEquals($result[6]['Attachment'], $expected);
  3630. }
  3631. /**
  3632. * testSaveMany method
  3633. *
  3634. * @return void
  3635. */
  3636. public function testSaveMany() {
  3637. $this->loadFixtures('Post');
  3638. $TestModel = new Post();
  3639. $TestModel->deleteAll(true);
  3640. $this->assertEquals($TestModel->find('all'), array());
  3641. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  3642. $this->db->truncate($TestModel);
  3643. $ts = date('Y-m-d H:i:s');
  3644. $TestModel->saveMany(array(
  3645. array(
  3646. 'title' => 'Multi-record post 1',
  3647. 'body' => 'First multi-record post',
  3648. 'author_id' => 2
  3649. ),
  3650. array(
  3651. 'title' => 'Multi-record post 2',
  3652. 'body' => 'Second multi-record post',
  3653. 'author_id' => 2
  3654. )));
  3655. $result = $TestModel->find('all', array(
  3656. 'recursive' => -1,
  3657. 'order' => 'Post.id ASC'
  3658. ));
  3659. $expected = array(
  3660. array(
  3661. 'Post' => array(
  3662. 'id' => '1',
  3663. 'author_id' => '2',
  3664. 'title' => 'Multi-record post 1',
  3665. 'body' => 'First multi-record post',
  3666. 'published' => 'N'
  3667. )
  3668. ),
  3669. array(
  3670. 'Post' => array(
  3671. 'id' => '2',
  3672. 'author_id' => '2',
  3673. 'title' => 'Multi-record post 2',
  3674. 'body' => 'Second multi-record post',
  3675. 'published' => 'N'
  3676. )
  3677. )
  3678. );
  3679. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  3680. $this->assertTrue($result[0]['Post']['created'] >= $ts);
  3681. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  3682. $this->assertTrue($result[1]['Post']['created'] >= $ts);
  3683. unset($result[0]['Post']['updated'], $result[0]['Post']['created']);
  3684. unset($result[1]['Post']['updated'], $result[1]['Post']['created']);
  3685. $this->assertEquals($expected, $result);
  3686. }
  3687. /**
  3688. * Test SaveAssociated with Habtm relations
  3689. *
  3690. * @return void
  3691. */
  3692. public function testSaveAssociatedHabtm() {
  3693. $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  3694. $data = array(
  3695. 'Article' => array(
  3696. 'user_id' => 1,
  3697. 'title' => 'Article Has and belongs to Many Tags'
  3698. ),
  3699. 'Tag' => array(
  3700. 'Tag' => array(1, 2)
  3701. ),
  3702. 'Comment' => array(
  3703. array(
  3704. 'comment' => 'Article comment',
  3705. 'user_id' => 1
  3706. )));
  3707. $Article = new Article();
  3708. $result = $Article->saveAssociated($data);
  3709. $this->assertFalse(empty($result));
  3710. $result = $Article->read();
  3711. $this->assertEquals(count($result['Tag']), 2);
  3712. $this->assertEquals($result['Tag'][0]['tag'], 'tag1');
  3713. $this->assertEquals(count($result['Comment']), 1);
  3714. $this->assertEquals(count($result['Comment'][0]['comment']), 1);
  3715. }
  3716. /**
  3717. * Test SaveAssociated with Habtm relations and extra join table fields
  3718. *
  3719. * @return void
  3720. */
  3721. public function testSaveAssociatedHabtmWithExtraJoinTableFields() {
  3722. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  3723. $data = array(
  3724. 'Something' => array(
  3725. 'id' => 4,
  3726. 'title' => 'Extra Fields',
  3727. 'body' => 'Extra Fields Body',
  3728. 'published' => '1'
  3729. ),
  3730. 'SomethingElse' => array(
  3731. array('something_else_id' => 1, 'doomed' => '1'),
  3732. array('something_else_id' => 2, 'doomed' => '0'),
  3733. array('something_else_id' => 3, 'doomed' => '1')
  3734. )
  3735. );
  3736. $Something = new Something();
  3737. $result = $Something->saveAssociated($data);
  3738. $this->assertFalse(empty($result));
  3739. $result = $Something->read();
  3740. $this->assertEquals(count($result['SomethingElse']), 3);
  3741. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  3742. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  3743. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  3744. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  3745. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  3746. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  3747. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  3748. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  3749. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  3750. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  3751. }
  3752. /**
  3753. * testSaveAssociatedHasOne method
  3754. *
  3755. * @return void
  3756. */
  3757. public function testSaveAssociatedHasOne() {
  3758. $model = new Comment();
  3759. $model->deleteAll(true);
  3760. $this->assertEquals($model->find('all'), array());
  3761. $model->Attachment->deleteAll(true);
  3762. $this->assertEquals($model->Attachment->find('all'), array());
  3763. $this->assertTrue($model->saveAssociated(array(
  3764. 'Comment' => array(
  3765. 'comment' => 'Comment with attachment',
  3766. 'article_id' => 1,
  3767. 'user_id' => 1
  3768. ),
  3769. 'Attachment' => array(
  3770. 'attachment' => 'some_file.zip'
  3771. ))));
  3772. $result = $model->find('all', array('fields' => array(
  3773. 'Comment.id', 'Comment.comment', 'Attachment.id',
  3774. 'Attachment.comment_id', 'Attachment.attachment'
  3775. )));
  3776. $expected = array(array(
  3777. 'Comment' => array(
  3778. 'id' => '1',
  3779. 'comment' => 'Comment with attachment'
  3780. ),
  3781. 'Attachment' => array(
  3782. 'id' => '1',
  3783. 'comment_id' => '1',
  3784. 'attachment' => 'some_file.zip'
  3785. )));
  3786. $this->assertEquals($expected, $result);
  3787. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  3788. $data = array(
  3789. 'Comment' => array(
  3790. 'comment' => 'Comment with attachment',
  3791. 'article_id' => 1,
  3792. 'user_id' => 1
  3793. ),
  3794. 'Attachment' => array(
  3795. 'attachment' => 'some_file.zip'
  3796. ));
  3797. $this->assertTrue($model->saveAssociated($data, array('validate' => 'first')));
  3798. }
  3799. /**
  3800. * testSaveAssociatedBelongsTo method
  3801. *
  3802. * @return void
  3803. */
  3804. public function testSaveAssociatedBelongsTo() {
  3805. $model = new Comment();
  3806. $model->deleteAll(true);
  3807. $this->assertEquals($model->find('all'), array());
  3808. $model->Article->deleteAll(true);
  3809. $this->assertEquals($model->Article->find('all'), array());
  3810. $this->assertTrue($model->saveAssociated(array(
  3811. 'Comment' => array(
  3812. 'comment' => 'Article comment',
  3813. 'article_id' => 1,
  3814. 'user_id' => 1
  3815. ),
  3816. 'Article' => array(
  3817. 'title' => 'Model Associations 101',
  3818. 'user_id' => 1
  3819. ))));
  3820. $result = $model->find('all', array('fields' => array(
  3821. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  3822. )));
  3823. $expected = array(array(
  3824. 'Comment' => array(
  3825. 'id' => '1',
  3826. 'article_id' => '1',
  3827. 'comment' => 'Article comment'
  3828. ),
  3829. 'Article' => array(
  3830. 'id' => '1',
  3831. 'title' => 'Model Associations 101'
  3832. )));
  3833. $this->assertEquals($expected, $result);
  3834. }
  3835. /**
  3836. * testSaveAssociatedHasOneValidation method
  3837. *
  3838. * @return void
  3839. */
  3840. public function testSaveAssociatedHasOneValidation() {
  3841. $model = new Comment();
  3842. $model->deleteAll(true);
  3843. $this->assertEquals($model->find('all'), array());
  3844. $model->Attachment->deleteAll(true);
  3845. $this->assertEquals($model->Attachment->find('all'), array());
  3846. $model->validate = array('comment' => 'notEmpty');
  3847. $model->Attachment->validate = array('attachment' => 'notEmpty');
  3848. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  3849. $this->assertEquals($model->saveAssociated(
  3850. array(
  3851. 'Comment' => array(
  3852. 'comment' => '',
  3853. 'article_id' => 1,
  3854. 'user_id' => 1
  3855. ),
  3856. 'Attachment' => array('attachment' => '')
  3857. ),
  3858. array('validate' => 'first')
  3859. ), false);
  3860. $expected = array(
  3861. 'Comment' => array('comment' => array('This field cannot be left blank')),
  3862. 'Attachment' => array('attachment' => array('This field cannot be left blank'))
  3863. );
  3864. $this->assertEquals($model->validationErrors, $expected['Comment']);
  3865. $this->assertEquals($model->Attachment->validationErrors, $expected['Attachment']);
  3866. }
  3867. /**
  3868. * testSaveAssociatedAtomic method
  3869. *
  3870. * @return void
  3871. */
  3872. public function testSaveAssociatedAtomic() {
  3873. $this->loadFixtures('Article', 'User');
  3874. $TestModel = new Article();
  3875. $result = $TestModel->saveAssociated(array(
  3876. 'Article' => array(
  3877. 'title' => 'Post with Author',
  3878. 'body' => 'This post will be saved with an author',
  3879. 'user_id' => 2
  3880. ),
  3881. 'Comment' => array(
  3882. array('comment' => 'First new comment', 'user_id' => 2))
  3883. ), array('atomic' => false));
  3884. $this->assertSame($result, array('Article' => true, 'Comment' => array(true)));
  3885. $result = $TestModel->saveAssociated(array(
  3886. 'Article' => array('id' => 2),
  3887. 'Comment' => array(
  3888. array(
  3889. 'comment' => 'First new comment',
  3890. 'published' => 'Y',
  3891. 'user_id' => 1
  3892. ),
  3893. array(
  3894. 'comment' => 'Second new comment',
  3895. 'published' => 'Y',
  3896. 'user_id' => 2
  3897. ))
  3898. ), array('validate' => true, 'atomic' => false));
  3899. $this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
  3900. }
  3901. /**
  3902. * testSaveManyAtomic method
  3903. *
  3904. * @return void
  3905. */
  3906. public function testSaveManyAtomic() {
  3907. $this->loadFixtures('Article', 'User');
  3908. $TestModel = new Article();
  3909. $result = $TestModel->saveMany(array(
  3910. array(
  3911. 'id' => '1',
  3912. 'title' => 'Baleeted First Post',
  3913. 'body' => 'Baleeted!',
  3914. 'published' => 'N'
  3915. ),
  3916. array(
  3917. 'id' => '2',
  3918. 'title' => 'Just update the title'
  3919. ),
  3920. array(
  3921. 'title' => 'Creating a fourth post',
  3922. 'body' => 'Fourth post body',
  3923. 'user_id' => 2
  3924. )
  3925. ), array('atomic' => false));
  3926. $this->assertSame($result, array(true, true, true));
  3927. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3928. $result = $TestModel->saveMany(array(
  3929. array(
  3930. 'id' => '1',
  3931. 'title' => 'Un-Baleeted First Post',
  3932. 'body' => 'Not Baleeted!',
  3933. 'published' => 'Y'
  3934. ),
  3935. array(
  3936. 'id' => '2',
  3937. 'title' => '',
  3938. 'body' => 'Trying to get away with an empty title'
  3939. )
  3940. ), array('validate' => true, 'atomic' => false));
  3941. $this->assertSame($result, array(true, false));
  3942. }
  3943. /**
  3944. * testSaveAssociatedHasMany method
  3945. *
  3946. * @return void
  3947. */
  3948. public function testSaveAssociatedHasMany() {
  3949. $this->loadFixtures('Article', 'Comment');
  3950. $TestModel = new Article();
  3951. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3952. $result = $TestModel->saveAssociated(array(
  3953. 'Article' => array('id' => 2),
  3954. 'Comment' => array(
  3955. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  3956. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  3957. )
  3958. ));
  3959. $this->assertFalse(empty($result));
  3960. $result = $TestModel->findById(2);
  3961. $expected = array(
  3962. 'First Comment for Second Article',
  3963. 'Second Comment for Second Article',
  3964. 'First new comment',
  3965. 'Second new comment'
  3966. );
  3967. $this->assertEquals(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3968. $result = $TestModel->saveAssociated(
  3969. array(
  3970. 'Article' => array('id' => 2),
  3971. 'Comment' => array(
  3972. array(
  3973. 'comment' => 'Third new comment',
  3974. 'published' => 'Y',
  3975. 'user_id' => 1
  3976. ))),
  3977. array('atomic' => false)
  3978. );
  3979. $this->assertFalse(empty($result));
  3980. $result = $TestModel->findById(2);
  3981. $expected = array(
  3982. 'First Comment for Second Article',
  3983. 'Second Comment for Second Article',
  3984. 'First new comment',
  3985. 'Second new comment',
  3986. 'Third new comment'
  3987. );
  3988. $this->assertEquals(Set::extract($result['Comment'], '{n}.comment'), $expected);
  3989. $TestModel->beforeSaveReturn = false;
  3990. $result = $TestModel->saveAssociated(
  3991. array(
  3992. 'Article' => array('id' => 2),
  3993. 'Comment' => array(
  3994. array(
  3995. 'comment' => 'Fourth new comment',
  3996. 'published' => 'Y',
  3997. 'user_id' => 1
  3998. ))),
  3999. array('atomic' => false)
  4000. );
  4001. $this->assertEquals($result, array('Article' => false));
  4002. $result = $TestModel->findById(2);
  4003. $expected = array(
  4004. 'First Comment for Second Article',
  4005. 'Second Comment for Second Article',
  4006. 'First new comment',
  4007. 'Second new comment',
  4008. 'Third new comment'
  4009. );
  4010. $this->assertEquals(Set::extract($result['Comment'], '{n}.comment'), $expected);
  4011. }
  4012. /**
  4013. * testSaveAssociatedHasManyValidation method
  4014. *
  4015. * @return void
  4016. */
  4017. public function testSaveAssociatedHasManyValidation() {
  4018. $this->loadFixtures('Article', 'Comment');
  4019. $TestModel = new Article();
  4020. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4021. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4022. $result = $TestModel->saveAssociated(array(
  4023. 'Article' => array('id' => 2),
  4024. 'Comment' => array(
  4025. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  4026. )
  4027. ), array('validate' => true));
  4028. $this->assertFalse($result);
  4029. $expected = array('Comment' => array(
  4030. array('comment' => array('This field cannot be left blank'))
  4031. ));
  4032. $this->assertEquals($TestModel->validationErrors, $expected);
  4033. $expected = array(
  4034. array('comment' => array('This field cannot be left blank'))
  4035. );
  4036. $this->assertEquals($TestModel->Comment->validationErrors, $expected);
  4037. $result = $TestModel->saveAssociated(array(
  4038. 'Article' => array('id' => 2),
  4039. 'Comment' => array(
  4040. array(
  4041. 'comment' => '',
  4042. 'published' => 'Y',
  4043. 'user_id' => 1
  4044. ))
  4045. ), array('validate' => 'first'));
  4046. $this->assertFalse($result);
  4047. }
  4048. /**
  4049. * test saveMany with transactions and ensure there is no missing rollback.
  4050. *
  4051. * @return void
  4052. */
  4053. public function testSaveManyTransactionNoRollback() {
  4054. $this->loadFixtures('Post');
  4055. $this->getMock('DboSource', array('connect', 'rollback', 'describe'), array(), 'MockManyTransactionDboSource');
  4056. $db = ConnectionManager::create('mock_many_transaction', array(
  4057. 'datasource' => 'MockManyTransactionDboSource',
  4058. ));
  4059. $db->expects($this->once())
  4060. ->method('describe')
  4061. ->will($this->returnValue(array()));
  4062. $db->expects($this->once())->method('rollback');
  4063. $Post = new Post('mock_many_transaction');
  4064. $Post->validate = array(
  4065. 'title' => array('rule' => array('notEmpty'))
  4066. );
  4067. $data = array(
  4068. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4069. array('author_id' => 1, 'title' => '')
  4070. );
  4071. $Post->saveMany($data);
  4072. }
  4073. /**
  4074. * test saveAssociated with transactions and ensure there is no missing rollback.
  4075. *
  4076. * @return void
  4077. */
  4078. public function testSaveAssociatedTransactionNoRollback() {
  4079. $testDb = ConnectionManager::getDataSource('test');
  4080. $mock = $this->getMock(
  4081. 'DboSource',
  4082. array('connect', 'rollback', 'describe', 'create', 'begin'),
  4083. array(),
  4084. 'MockAssociatedTransactionDboSource',
  4085. false
  4086. );
  4087. $db = ConnectionManager::create('mock_assoc_transaction', array(
  4088. 'datasource' => 'MockAssociatedTransactionDboSource',
  4089. ));
  4090. $this->mockObjects[] = $db;
  4091. $db->columns = $testDb->columns;
  4092. $db->expects($this->once())->method('rollback');
  4093. $db->expects($this->any())->method('describe')
  4094. ->will($this->returnValue(array(
  4095. 'id' => array('type' => 'integer'),
  4096. 'title' => array('type' => 'string'),
  4097. 'body' => array('type' => 'text'),
  4098. 'published' => array('type' => 'string')
  4099. )));
  4100. $Post = new Post();
  4101. $Post->useDbConfig = 'mock_assoc_transaction';
  4102. $Post->Author->useDbConfig = 'mock_assoc_transaction';
  4103. $Post->Author->validate = array(
  4104. 'user' => array('rule' => array('notEmpty'))
  4105. );
  4106. $data = array(
  4107. 'Post' => array(
  4108. 'title' => 'New post',
  4109. 'body' => 'Content',
  4110. 'published' => 'Y'
  4111. ),
  4112. 'Author' => array(
  4113. 'user' => '',
  4114. 'password' => "sekret"
  4115. )
  4116. );
  4117. $Post->saveAssociated($data, array('validate' => true, 'atomic' => true));
  4118. }
  4119. /**
  4120. * test saveMany with nested saveMany call.
  4121. *
  4122. * @return void
  4123. */
  4124. public function testSaveManyNestedSaveMany() {
  4125. $this->loadFixtures('Sample');
  4126. $TransactionManyTestModel = new TransactionManyTestModel();
  4127. $data = array(
  4128. array('apple_id' => 1, 'name' => 'sample5'),
  4129. );
  4130. $this->assertTrue($TransactionManyTestModel->saveMany($data, array('atomic' => true)));
  4131. }
  4132. /**
  4133. * testSaveManyTransaction method
  4134. *
  4135. * @return void
  4136. */
  4137. public function testSaveManyTransaction() {
  4138. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4139. $TestModel = new Post();
  4140. $TestModel->validate = array('title' => 'notEmpty');
  4141. $data = array(
  4142. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4143. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4144. array('author_id' => 1, 'title' => '')
  4145. );
  4146. $ts = date('Y-m-d H:i:s');
  4147. $this->assertFalse($TestModel->saveMany($data));
  4148. $result = $TestModel->find('all', array('recursive' => -1));
  4149. $expected = array(
  4150. array('Post' => array(
  4151. 'id' => '1',
  4152. 'author_id' => 1,
  4153. 'title' => 'First Post',
  4154. 'body' => 'First Post Body',
  4155. 'published' => 'Y',
  4156. 'created' => '2007-03-18 10:39:23',
  4157. 'updated' => '2007-03-18 10:41:31'
  4158. )),
  4159. array('Post' => array(
  4160. 'id' => '2',
  4161. 'author_id' => 3,
  4162. 'title' => 'Second Post',
  4163. 'body' => 'Second Post Body',
  4164. 'published' => 'Y',
  4165. 'created' => '2007-03-18 10:41:23',
  4166. 'updated' => '2007-03-18 10:43:31'
  4167. )),
  4168. array('Post' => array(
  4169. 'id' => '3',
  4170. 'author_id' => 1,
  4171. 'title' => 'Third Post',
  4172. 'body' => 'Third Post Body',
  4173. 'published' => 'Y',
  4174. 'created' => '2007-03-18 10:43:23',
  4175. 'updated' => '2007-03-18 10:45:31'
  4176. )));
  4177. if (count($result) != 3) {
  4178. // Database doesn't support transactions
  4179. $expected[] = array(
  4180. 'Post' => array(
  4181. 'id' => '4',
  4182. 'author_id' => 1,
  4183. 'title' => 'New Fourth Post',
  4184. 'body' => null,
  4185. 'published' => 'N'
  4186. ));
  4187. $expected[] = array(
  4188. 'Post' => array(
  4189. 'id' => '5',
  4190. 'author_id' => 1,
  4191. 'title' => 'New Fifth Post',
  4192. 'body' => null,
  4193. 'published' => 'N',
  4194. ));
  4195. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4196. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4197. $this->assertTrue($result[4]['Post']['created'] >= $ts);
  4198. $this->assertTrue($result[4]['Post']['updated'] >= $ts);
  4199. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4200. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4201. $this->assertEquals($expected, $result);
  4202. // Skip the rest of the transactional tests
  4203. return;
  4204. }
  4205. $this->assertEquals($expected, $result);
  4206. $data = array(
  4207. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4208. array('author_id' => 1, 'title' => ''),
  4209. array('author_id' => 1, 'title' => 'New Sixth Post')
  4210. );
  4211. $ts = date('Y-m-d H:i:s');
  4212. $this->assertFalse($TestModel->saveMany($data));
  4213. $result = $TestModel->find('all', array('recursive' => -1));
  4214. $expected = array(
  4215. array('Post' => array(
  4216. 'id' => '1',
  4217. 'author_id' => 1,
  4218. 'title' => 'First Post',
  4219. 'body' => 'First Post Body',
  4220. 'published' => 'Y',
  4221. 'created' => '2007-03-18 10:39:23',
  4222. 'updated' => '2007-03-18 10:41:31'
  4223. )),
  4224. array('Post' => array(
  4225. 'id' => '2',
  4226. 'author_id' => 3,
  4227. 'title' => 'Second Post',
  4228. 'body' => 'Second Post Body',
  4229. 'published' => 'Y',
  4230. 'created' => '2007-03-18 10:41:23',
  4231. 'updated' => '2007-03-18 10:43:31'
  4232. )),
  4233. array('Post' => array(
  4234. 'id' => '3',
  4235. 'author_id' => 1,
  4236. 'title' => 'Third Post',
  4237. 'body' => 'Third Post Body',
  4238. 'published' => 'Y',
  4239. 'created' => '2007-03-18 10:43:23',
  4240. 'updated' => '2007-03-18 10:45:31'
  4241. )));
  4242. if (count($result) != 3) {
  4243. // Database doesn't support transactions
  4244. $expected[] = array(
  4245. 'Post' => array(
  4246. 'id' => '4',
  4247. 'author_id' => 1,
  4248. 'title' => 'New Fourth Post',
  4249. 'body' => 'Third Post Body',
  4250. 'published' => 'N'
  4251. ));
  4252. $expected[] = array(
  4253. 'Post' => array(
  4254. 'id' => '5',
  4255. 'author_id' => 1,
  4256. 'title' => 'Third Post',
  4257. 'body' => 'Third Post Body',
  4258. 'published' => 'N'
  4259. ));
  4260. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4261. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4262. $this->assertTrue($result[4]['Post']['created'] >= $ts);
  4263. $this->assertTrue($result[4]['Post']['updated'] >= $ts);
  4264. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4265. unset($result[4]['Post']['created'], $result[4]['Post']['updated']);
  4266. }
  4267. $this->assertEquals($expected, $result);
  4268. $TestModel->validate = array('title' => 'notEmpty');
  4269. $data = array(
  4270. array('author_id' => 1, 'title' => 'New Fourth Post'),
  4271. array('author_id' => 1, 'title' => 'New Fifth Post'),
  4272. array('author_id' => 1, 'title' => 'New Sixth Post')
  4273. );
  4274. $this->assertTrue($TestModel->saveMany($data));
  4275. $result = $TestModel->find('all', array(
  4276. 'recursive' => -1,
  4277. 'fields' => array('author_id', 'title','body','published')
  4278. ));
  4279. $expected = array(
  4280. array('Post' => array(
  4281. 'author_id' => 1,
  4282. 'title' => 'First Post',
  4283. 'body' => 'First Post Body',
  4284. 'published' => 'Y'
  4285. )),
  4286. array('Post' => array(
  4287. 'author_id' => 3,
  4288. 'title' => 'Second Post',
  4289. 'body' => 'Second Post Body',
  4290. 'published' => 'Y'
  4291. )),
  4292. array('Post' => array(
  4293. 'author_id' => 1,
  4294. 'title' => 'Third Post',
  4295. 'body' => 'Third Post Body',
  4296. 'published' => 'Y'
  4297. )),
  4298. array('Post' => array(
  4299. 'author_id' => 1,
  4300. 'title' => 'New Fourth Post',
  4301. 'body' => '',
  4302. 'published' => 'N'
  4303. )),
  4304. array('Post' => array(
  4305. 'author_id' => 1,
  4306. 'title' => 'New Fifth Post',
  4307. 'body' => '',
  4308. 'published' => 'N'
  4309. )),
  4310. array('Post' => array(
  4311. 'author_id' => 1,
  4312. 'title' => 'New Sixth Post',
  4313. 'body' => '',
  4314. 'published' => 'N'
  4315. )));
  4316. $this->assertEquals($expected, $result);
  4317. }
  4318. /**
  4319. * testSaveManyValidation method
  4320. *
  4321. * @return void
  4322. */
  4323. public function testSaveManyValidation() {
  4324. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  4325. $TestModel = new Post();
  4326. $ts = date('Y-m-d H:i:s');
  4327. $data = array(
  4328. array(
  4329. 'id' => '1',
  4330. 'title' => 'Baleeted First Post',
  4331. 'body' => 'Baleeted!',
  4332. 'published' => 'N'
  4333. ),
  4334. array(
  4335. 'id' => '2',
  4336. 'title' => 'Just update the title'
  4337. ),
  4338. array(
  4339. 'title' => 'Creating a fourth post',
  4340. 'body' => 'Fourth post body',
  4341. 'author_id' => 2
  4342. ));
  4343. $this->assertTrue($TestModel->saveMany($data));
  4344. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4345. $expected = array(
  4346. array(
  4347. 'Post' => array(
  4348. 'id' => '1',
  4349. 'author_id' => '1',
  4350. 'title' => 'Baleeted First Post',
  4351. 'body' => 'Baleeted!',
  4352. 'published' => 'N',
  4353. 'created' => '2007-03-18 10:39:23'
  4354. )
  4355. ),
  4356. array(
  4357. 'Post' => array(
  4358. 'id' => '2',
  4359. 'author_id' => '3',
  4360. 'title' => 'Just update the title',
  4361. 'body' => 'Second Post Body',
  4362. 'published' => 'Y',
  4363. 'created' => '2007-03-18 10:41:23'
  4364. )
  4365. ),
  4366. array(
  4367. 'Post' => array(
  4368. 'id' => '3',
  4369. 'author_id' => '1',
  4370. 'title' => 'Third Post',
  4371. 'body' => 'Third Post Body',
  4372. 'published' => 'Y',
  4373. 'created' => '2007-03-18 10:43:23',
  4374. 'updated' => '2007-03-18 10:45:31'
  4375. )),
  4376. array(
  4377. 'Post' => array(
  4378. 'id' => '4',
  4379. 'author_id' => '2',
  4380. 'title' => 'Creating a fourth post',
  4381. 'body' => 'Fourth post body',
  4382. 'published' => 'N'
  4383. )
  4384. )
  4385. );
  4386. $this->assertTrue($result[0]['Post']['updated'] >= $ts);
  4387. $this->assertTrue($result[1]['Post']['updated'] >= $ts);
  4388. $this->assertTrue($result[3]['Post']['created'] >= $ts);
  4389. $this->assertTrue($result[3]['Post']['updated'] >= $ts);
  4390. unset($result[0]['Post']['updated'], $result[1]['Post']['updated']);
  4391. unset($result[3]['Post']['created'], $result[3]['Post']['updated']);
  4392. $this->assertEquals($expected, $result);
  4393. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4394. $data = array(
  4395. array(
  4396. 'id' => '1',
  4397. 'title' => 'Un-Baleeted First Post',
  4398. 'body' => 'Not Baleeted!',
  4399. 'published' => 'Y'
  4400. ),
  4401. array(
  4402. 'id' => '2',
  4403. 'title' => '',
  4404. 'body' => 'Trying to get away with an empty title'
  4405. ));
  4406. $result = $TestModel->saveMany($data);
  4407. $this->assertFalse($result);
  4408. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  4409. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4410. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  4411. if (!$transactionWorked) {
  4412. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  4413. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  4414. }
  4415. $this->assertEquals($TestModel->validationErrors, $errors);
  4416. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  4417. $data = array(
  4418. array(
  4419. 'id' => '1',
  4420. 'title' => 'Un-Baleeted First Post',
  4421. 'body' => 'Not Baleeted!',
  4422. 'published' => 'Y'
  4423. ),
  4424. array(
  4425. 'id' => '2',
  4426. 'title' => '',
  4427. 'body' => 'Trying to get away with an empty title'
  4428. ));
  4429. $result = $TestModel->saveMany($data, array('validate' => true, 'atomic' => false));
  4430. $this->assertEquals($result, array(true, false));
  4431. $result = $TestModel->find('all', array(
  4432. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  4433. 'recursive' => -1,
  4434. 'order' => 'Post.id ASC'
  4435. ));
  4436. $errors = array(1 => array('title' => array('This field cannot be left blank')));
  4437. $expected = array(
  4438. array(
  4439. 'Post' => array(
  4440. 'id' => '1',
  4441. 'author_id' => '1',
  4442. 'title' => 'Un-Baleeted First Post',
  4443. 'body' => 'Not Baleeted!',
  4444. 'published' => 'Y',
  4445. )),
  4446. array(
  4447. 'Post' => array(
  4448. 'id' => '2',
  4449. 'author_id' => '3',
  4450. 'title' => 'Just update the title',
  4451. 'body' => 'Second Post Body',
  4452. 'published' => 'Y',
  4453. )),
  4454. array(
  4455. 'Post' => array(
  4456. 'id' => '3',
  4457. 'author_id' => '1',
  4458. 'title' => 'Third Post',
  4459. 'body' => 'Third Post Body',
  4460. 'published' => 'Y',
  4461. )),
  4462. array(
  4463. 'Post' => array(
  4464. 'id' => '4',
  4465. 'author_id' => '2',
  4466. 'title' => 'Creating a fourth post',
  4467. 'body' => 'Fourth post body',
  4468. 'published' => 'N',
  4469. )));
  4470. $this->assertEquals($expected, $result);
  4471. $this->assertEquals($TestModel->validationErrors, $errors);
  4472. $data = array(
  4473. array(
  4474. 'id' => '1',
  4475. 'title' => 'Re-Baleeted First Post',
  4476. 'body' => 'Baleeted!',
  4477. 'published' => 'N'
  4478. ),
  4479. array(
  4480. 'id' => '2',
  4481. 'title' => '',
  4482. 'body' => 'Trying to get away with an empty title'
  4483. ));
  4484. $this->assertFalse($TestModel->saveMany($data, array('validate' => 'first')));
  4485. $result = $TestModel->find('all', array(
  4486. 'fields' => array('id', 'author_id', 'title', 'body', 'published'),
  4487. 'recursive' => -1,
  4488. 'order' => 'Post.id ASC'
  4489. ));
  4490. $this->assertEquals($expected, $result);
  4491. $this->assertEquals($TestModel->validationErrors, $errors);
  4492. }
  4493. /**
  4494. * testValidateMany method
  4495. *
  4496. * @return void
  4497. */
  4498. public function testValidateMany() {
  4499. $TestModel = new Article();
  4500. $TestModel->validate = array('title' => 'notEmpty');
  4501. $result = $TestModel->validateMany(
  4502. array(
  4503. 0 => array('title' => ''),
  4504. 1 => array('title' => 'title 1'),
  4505. 2 => array('title' => 'title 2'),
  4506. ));
  4507. $this->assertFalse($result);
  4508. $expected = array(
  4509. 0 => array('title' => array('This field cannot be left blank')),
  4510. );
  4511. $this->assertEquals($TestModel->validationErrors, $expected);
  4512. $result = $TestModel->validateMany(
  4513. array(
  4514. 0 => array('title' => 'title 0'),
  4515. 1 => array('title' => ''),
  4516. 2 => array('title' => 'title 2'),
  4517. ));
  4518. $this->assertFalse($result);
  4519. $expected = array(
  4520. 1 => array('title' => array('This field cannot be left blank')),
  4521. );
  4522. $this->assertEquals($TestModel->validationErrors, $expected);
  4523. }
  4524. /**
  4525. * testSaveAssociatedValidateFirst method
  4526. *
  4527. * @return void
  4528. */
  4529. public function testSaveAssociatedValidateFirst() {
  4530. $this->loadFixtures('Article', 'Comment', 'Attachment');
  4531. $model = new Article();
  4532. $model->deleteAll(true);
  4533. $model->Comment->validate = array('comment' => 'notEmpty');
  4534. $result = $model->saveAssociated(array(
  4535. 'Article' => array(
  4536. 'title' => 'Post with Author',
  4537. 'body' => 'This post will be saved author'
  4538. ),
  4539. 'Comment' => array(
  4540. array('comment' => 'First new comment'),
  4541. array('comment' => '')
  4542. )
  4543. ), array('validate' => 'first'));
  4544. $this->assertFalse($result);
  4545. $result = $model->find('all');
  4546. $this->assertEquals($result, array());
  4547. $expected = array('Comment' => array(
  4548. 1 => array('comment' => array('This field cannot be left blank'))
  4549. ));
  4550. $this->assertEquals($model->Comment->validationErrors, $expected['Comment']);
  4551. $this->assertSame($model->Comment->find('count'), 0);
  4552. $result = $model->saveAssociated(
  4553. array(
  4554. 'Article' => array(
  4555. 'title' => 'Post with Author',
  4556. 'body' => 'This post will be saved with an author',
  4557. 'user_id' => 2
  4558. ),
  4559. 'Comment' => array(
  4560. array(
  4561. 'comment' => 'Only new comment',
  4562. 'user_id' => 2
  4563. ))),
  4564. array('validate' => 'first')
  4565. );
  4566. $this->assertSame($result, true);
  4567. $result = $model->Comment->find('all');
  4568. $this->assertSame(count($result), 1);
  4569. $result = Set::extract('/Comment/article_id', $result);
  4570. $this->assertEquals($result[0], 4);
  4571. $model->deleteAll(true);
  4572. $data = array(
  4573. 'Article' => array(
  4574. 'title' => 'Post with Author saveAlled from comment',
  4575. 'body' => 'This post will be saved with an author',
  4576. 'user_id' => 2
  4577. ),
  4578. 'Comment' => array(
  4579. 'comment' => 'Only new comment', 'user_id' => 2
  4580. ));
  4581. $result = $model->Comment->saveAssociated($data, array('validate' => 'first'));
  4582. $this->assertFalse(empty($result));
  4583. $result = $model->find('all');
  4584. $this->assertEquals(
  4585. $result[0]['Article']['title'],
  4586. 'Post with Author saveAlled from comment'
  4587. );
  4588. $this->assertEquals($result[0]['Comment'][0]['comment'], 'Only new comment');
  4589. }
  4590. /**
  4591. * test saveMany()'s return is correct when using atomic = false and validate = first.
  4592. *
  4593. * @return void
  4594. */
  4595. public function testSaveManyValidateFirstAtomicFalse() {
  4596. $Something = new Something();
  4597. $invalidData = array(
  4598. array(
  4599. 'title' => 'foo',
  4600. 'body' => 'bar',
  4601. 'published' => 'baz',
  4602. ),
  4603. array(
  4604. 'body' => 3,
  4605. 'published' => 'sd',
  4606. ),
  4607. );
  4608. $Something->create();
  4609. $Something->validate = array(
  4610. 'title' => array(
  4611. 'rule' => 'alphaNumeric',
  4612. 'required' => true,
  4613. ),
  4614. 'body' => array(
  4615. 'rule' => 'alphaNumeric',
  4616. 'required' => true,
  4617. 'allowEmpty' => true,
  4618. ),
  4619. );
  4620. $result = $Something->saveMany($invalidData, array(
  4621. 'atomic' => false,
  4622. 'validate' => 'first',
  4623. ));
  4624. $expected = array(true, false);
  4625. $this->assertEquals($expected, $result);
  4626. $Something = new Something();
  4627. $validData = array(
  4628. array(
  4629. 'title' => 'title value',
  4630. 'body' => 'body value',
  4631. 'published' => 'baz',
  4632. ),
  4633. array(
  4634. 'title' => 'valid',
  4635. 'body' => 'this body',
  4636. 'published' => 'sd',
  4637. ),
  4638. );
  4639. $Something->create();
  4640. $result = $Something->saveMany($validData, array(
  4641. 'atomic' => false,
  4642. 'validate' => 'first',
  4643. ));
  4644. $expected = array(true, true);
  4645. $this->assertEquals($expected, $result);
  4646. }
  4647. /**
  4648. * testValidateAssociated method
  4649. *
  4650. * @return void
  4651. */
  4652. public function testValidateAssociated() {
  4653. $TestModel = new Comment();
  4654. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  4655. $data = array(
  4656. 'Comment' => array(
  4657. 'comment' => 'This is the comment'
  4658. ),
  4659. 'Attachment' => array(
  4660. 'attachment' => ''
  4661. )
  4662. );
  4663. $result = $TestModel->validateAssociated($data);
  4664. $this->assertFalse($result);
  4665. $TestModel = new Article();
  4666. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  4667. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  4668. $result = $TestModel->validateAssociated(
  4669. array(
  4670. 'Article' => array('id' => 2),
  4671. 'Comment' => array(
  4672. array(
  4673. 'id' => 1,
  4674. 'comment' => '',
  4675. 'published' => 'Y',
  4676. 'user_id' => 1),
  4677. array(
  4678. 'id' => 2,
  4679. 'comment' =>
  4680. 'comment',
  4681. 'published' => 'Y',
  4682. 'user_id' => 1
  4683. ))));
  4684. $this->assertFalse($result);
  4685. $result = $TestModel->validateAssociated(
  4686. array(
  4687. 'Article' => array('id' => 2),
  4688. 'Comment' => array(
  4689. array(
  4690. 'id' => 1,
  4691. 'comment' => '',
  4692. 'published' => 'Y',
  4693. 'user_id' => 1
  4694. ),
  4695. array(
  4696. 'id' => 2,
  4697. 'comment' => 'comment',
  4698. 'published' => 'Y',
  4699. 'user_id' => 1
  4700. ),
  4701. array(
  4702. 'id' => 3,
  4703. 'comment' => '',
  4704. 'published' => 'Y',
  4705. 'user_id' => 1
  4706. ))),
  4707. array(
  4708. 'atomic' => false
  4709. ));
  4710. $expected = array(
  4711. 'Article' => true,
  4712. 'Comment' => array(false, true, false)
  4713. );
  4714. $this->assertSame($expected, $result);
  4715. $expected = array('Comment' => array(
  4716. 0 => array('comment' => array('This field cannot be left blank')),
  4717. 2 => array('comment' => array('This field cannot be left blank'))
  4718. ));
  4719. $this->assertEquals($TestModel->validationErrors, $expected);
  4720. $expected = array(
  4721. 0 => array('comment' => array('This field cannot be left blank')),
  4722. 2 => array('comment' => array('This field cannot be left blank'))
  4723. );
  4724. $this->assertEquals($TestModel->Comment->validationErrors, $expected);
  4725. }
  4726. /**
  4727. * test that saveMany behaves like plain save() when suplied empty data
  4728. *
  4729. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4730. * @return void
  4731. */
  4732. public function testSaveManyEmptyData() {
  4733. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4734. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4735. $model = new Article();
  4736. $result = $model->saveMany(array(), array('validate' => true));
  4737. $this->assertFalse(empty($result));
  4738. $model = new ProductUpdateAll();
  4739. $result = $model->saveMany(array());
  4740. $this->assertFalse($result);
  4741. }
  4742. /**
  4743. * test that saveAssociated behaves like plain save() when suplied empty data
  4744. *
  4745. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  4746. * @return void
  4747. */
  4748. public function testSaveAssociatedEmptyData() {
  4749. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  4750. $this->loadFixtures('Article', 'ProductUpdateAll', 'Comment', 'Attachment');
  4751. $model = new Article();
  4752. $result = $model->saveAssociated(array(), array('validate' => true));
  4753. $this->assertFalse(empty($result));
  4754. $model = new ProductUpdateAll();
  4755. $result = $model->saveAssociated(array());
  4756. $this->assertFalse($result);
  4757. }
  4758. /**
  4759. * testUpdateWithCalculation method
  4760. *
  4761. * @return void
  4762. */
  4763. public function testUpdateWithCalculation() {
  4764. $this->loadFixtures('DataTest');
  4765. $model = new DataTest();
  4766. $model->deleteAll(true);
  4767. $result = $model->saveMany(array(
  4768. array('count' => 5, 'float' => 1.1),
  4769. array('count' => 3, 'float' => 1.2),
  4770. array('count' => 4, 'float' => 1.3),
  4771. array('count' => 1, 'float' => 2.0),
  4772. ));
  4773. $this->assertFalse(empty($result));
  4774. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  4775. $this->assertEquals($result, array(5, 3, 4, 1));
  4776. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  4777. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  4778. $this->assertEquals($result, array(7, 5, 6, 3));
  4779. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  4780. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  4781. $this->assertEquals($result, array(6, 4, 5, 2));
  4782. }
  4783. /**
  4784. * TestFindAllWithoutForeignKey
  4785. *
  4786. * @return void
  4787. */
  4788. public function testFindAllForeignKey() {
  4789. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4790. $ProductUpdateAll = new ProductUpdateAll();
  4791. $conditions = array('Group.name' => 'group one');
  4792. $ProductUpdateAll->bindModel(array(
  4793. 'belongsTo' => array(
  4794. 'Group' => array('className' => 'GroupUpdateAll')
  4795. )
  4796. ));
  4797. $ProductUpdateAll->belongsTo = array(
  4798. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  4799. );
  4800. $results = $ProductUpdateAll->find('all', compact('conditions'));
  4801. $this->assertTrue(!empty($results));
  4802. $ProductUpdateAll->bindModel(array('belongsTo' => array('Group')));
  4803. $ProductUpdateAll->belongsTo = array(
  4804. 'Group' => array(
  4805. 'className' => 'GroupUpdateAll',
  4806. 'foreignKey' => false,
  4807. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  4808. ));
  4809. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  4810. $this->assertTrue(!empty($resultsFkFalse));
  4811. $expected = array(
  4812. '0' => array(
  4813. 'ProductUpdateAll' => array(
  4814. 'id' => 1,
  4815. 'name' => 'product one',
  4816. 'groupcode' => 120,
  4817. 'group_id' => 1),
  4818. 'Group' => array(
  4819. 'id' => 1,
  4820. 'name' => 'group one',
  4821. 'code' => 120)
  4822. ),
  4823. '1' => array(
  4824. 'ProductUpdateAll' => array(
  4825. 'id' => 2,
  4826. 'name' => 'product two',
  4827. 'groupcode' => 120,
  4828. 'group_id' => 1),
  4829. 'Group' => array(
  4830. 'id' => 1,
  4831. 'name' => 'group one',
  4832. 'code' => 120)
  4833. )
  4834. );
  4835. $this->assertEquals($results, $expected);
  4836. $this->assertEquals($resultsFkFalse, $expected);
  4837. }
  4838. /**
  4839. * test updateAll with empty values.
  4840. *
  4841. * @return void
  4842. */
  4843. public function testUpdateAllEmptyValues() {
  4844. $this->skipIf($this->db instanceof Sqlserver || $this->db instanceof Postgres, 'This test is not compatible with Postgres or SQL Server.');
  4845. $this->loadFixtures('Author', 'Post');
  4846. $model = new Author();
  4847. $result = $model->updateAll(array('user' => '""'));
  4848. $this->assertTrue($result);
  4849. }
  4850. /**
  4851. * testUpdateAllWithJoins
  4852. *
  4853. * @return void
  4854. */
  4855. public function testUpdateAllWithJoins() {
  4856. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql or sqlite');
  4857. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4858. $ProductUpdateAll = new ProductUpdateAll();
  4859. $conditions = array('Group.name' => 'group one');
  4860. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  4861. 'Group' => array('className' => 'GroupUpdateAll')))
  4862. );
  4863. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  4864. $results = $ProductUpdateAll->find('all', array(
  4865. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  4866. ));
  4867. $expected = array(
  4868. '0' => array(
  4869. 'ProductUpdateAll' => array(
  4870. 'id' => 1,
  4871. 'name' => 'new product',
  4872. 'groupcode' => 120,
  4873. 'group_id' => 1),
  4874. 'Group' => array(
  4875. 'id' => 1,
  4876. 'name' => 'group one',
  4877. 'code' => 120)
  4878. ),
  4879. '1' => array(
  4880. 'ProductUpdateAll' => array(
  4881. 'id' => 2,
  4882. 'name' => 'new product',
  4883. 'groupcode' => 120,
  4884. 'group_id' => 1),
  4885. 'Group' => array(
  4886. 'id' => 1,
  4887. 'name' => 'group one',
  4888. 'code' => 120)));
  4889. $this->assertEquals($results, $expected);
  4890. }
  4891. /**
  4892. * testUpdateAllWithoutForeignKey
  4893. *
  4894. * @return void
  4895. */
  4896. public function testUpdateAllWithoutForeignKey() {
  4897. $this->skipIf(!$this->db instanceof Mysql, 'Currently, there is no way of doing joins in an update statement in postgresql');
  4898. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  4899. $ProductUpdateAll = new ProductUpdateAll();
  4900. $conditions = array('Group.name' => 'group one');
  4901. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  4902. 'Group' => array('className' => 'GroupUpdateAll')
  4903. )));
  4904. $ProductUpdateAll->belongsTo = array(
  4905. 'Group' => array(
  4906. 'className' => 'GroupUpdateAll',
  4907. 'foreignKey' => false,
  4908. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  4909. )
  4910. );
  4911. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  4912. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name' => 'new product')));
  4913. $expected = array(
  4914. '0' => array(
  4915. 'ProductUpdateAll' => array(
  4916. 'id' => 1,
  4917. 'name' => 'new product',
  4918. 'groupcode' => 120,
  4919. 'group_id' => 1),
  4920. 'Group' => array(
  4921. 'id' => 1,
  4922. 'name' => 'group one',
  4923. 'code' => 120)
  4924. ),
  4925. '1' => array(
  4926. 'ProductUpdateAll' => array(
  4927. 'id' => 2,
  4928. 'name' => 'new product',
  4929. 'groupcode' => 120,
  4930. 'group_id' => 1),
  4931. 'Group' => array(
  4932. 'id' => 1,
  4933. 'name' => 'group one',
  4934. 'code' => 120)));
  4935. $this->assertEquals($resultsFkFalse, $expected);
  4936. }
  4937. /**
  4938. * test writing floats in german locale.
  4939. *
  4940. * @return void
  4941. */
  4942. public function testWriteFloatAsGerman() {
  4943. $restore = setlocale(LC_ALL, null);
  4944. setlocale(LC_ALL, 'de_DE');
  4945. $model = new DataTest();
  4946. $result = $model->save(array(
  4947. 'count' => 1,
  4948. 'float' => 3.14593
  4949. ));
  4950. $this->assertTrue((bool)$result);
  4951. setlocale(LC_ALL, $restore);
  4952. }
  4953. /**
  4954. * Test returned array contains primary key when save creates a new record
  4955. *
  4956. * @return void
  4957. */
  4958. public function testPkInReturnArrayForCreate() {
  4959. $this->loadFixtures('Article');
  4960. $TestModel = new Article();
  4961. $data = array('Article' => array(
  4962. 'user_id' => '1',
  4963. 'title' => 'Fourth Article',
  4964. 'body' => 'Fourth Article Body',
  4965. 'published' => 'Y'
  4966. ));
  4967. $result = $TestModel->save($data);
  4968. $this->assertSame($result['Article']['id'], $TestModel->id);
  4969. }
  4970. }