PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/daveschwan/ronin-group
PHP | 7209 lines | 6007 code | 615 blank | 587 comment | 26 complexity | e57f7fb416a3ffb68e2cc4cd450e30a6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * ModelWriteTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Model
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('MockTransactionDboSource', 'Model/Datasource');
  19. App::uses('MockTransactionAssociatedDboSource', 'Model/Datasource');
  20. App::uses('MockManyTransactionDboSource', 'Model/Datasource');
  21. App::uses('MockAssociatedTransactionDboSource', 'Model/Datasource');
  22. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  23. /**
  24. * ModelWriteTest
  25. *
  26. * @package Cake.Test.Case.Model
  27. */
  28. class ModelWriteTest extends BaseModelTest {
  29. /**
  30. * override locale to the default (eng).
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. Configure::write('Config.language', 'eng');
  37. }
  38. /**
  39. * Test save() failing when there is no data.
  40. *
  41. * @return void
  42. */
  43. public function testInsertNoData() {
  44. $this->loadFixtures('Bid');
  45. $Bid = ClassRegistry::init('Bid');
  46. $this->assertFalse($Bid->save());
  47. $result = $Bid->save(array('Bid' => array()));
  48. $this->assertFalse($result);
  49. $result = $Bid->save(array('Bid' => array('not in schema' => 1)));
  50. $this->assertFalse($result);
  51. }
  52. /**
  53. * testInsertAnotherHabtmRecordWithSameForeignKey method
  54. *
  55. * @return void
  56. */
  57. public function testInsertAnotherHabtmRecordWithSameForeignKey() {
  58. $this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC');
  59. $TestModel = new JoinA();
  60. $result = $TestModel->JoinAsJoinB->findById(1);
  61. $expected = array(
  62. 'JoinAsJoinB' => array(
  63. 'id' => 1,
  64. 'join_a_id' => 1,
  65. 'join_b_id' => 2,
  66. 'other' => 'Data for Join A 1 Join B 2',
  67. 'created' => '2008-01-03 10:56:33',
  68. 'updated' => '2008-01-03 10:56:33'
  69. ));
  70. $this->assertEquals($expected, $result);
  71. $TestModel->JoinAsJoinB->create();
  72. $data = array(
  73. 'join_a_id' => 1,
  74. 'join_b_id' => 1,
  75. 'other' => 'Data for Join A 1 Join B 1',
  76. 'created' => '2008-01-03 10:56:44',
  77. 'updated' => '2008-01-03 10:56:44'
  78. );
  79. $result = $TestModel->JoinAsJoinB->save($data);
  80. $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
  81. $data['id'] = $lastInsertId;
  82. $this->assertEquals(array('JoinAsJoinB' => $data), $result);
  83. $this->assertTrue($lastInsertId > 0);
  84. $result = $TestModel->JoinAsJoinB->findById(1);
  85. $expected = array(
  86. 'JoinAsJoinB' => array(
  87. 'id' => 1,
  88. 'join_a_id' => 1,
  89. 'join_b_id' => 2,
  90. 'other' => 'Data for Join A 1 Join B 2',
  91. 'created' => '2008-01-03 10:56:33',
  92. 'updated' => '2008-01-03 10:56:33'
  93. ));
  94. $this->assertEquals($expected, $result);
  95. $updatedValue = 'UPDATED Data for Join A 1 Join B 2';
  96. $TestModel->JoinAsJoinB->id = 1;
  97. $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
  98. $this->assertFalse(empty($result));
  99. $result = $TestModel->JoinAsJoinB->findById(1);
  100. $this->assertEquals($updatedValue, $result['JoinAsJoinB']['other']);
  101. }
  102. /**
  103. * testSaveDateAsFirstEntry method
  104. *
  105. * @return void
  106. */
  107. public function testSaveDateAsFirstEntry() {
  108. $this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
  109. $Article = new Article();
  110. $data = array(
  111. 'Article' => array(
  112. 'created' => array(
  113. 'day' => '1',
  114. 'month' => '1',
  115. 'year' => '2008'
  116. ),
  117. 'title' => 'Test Title',
  118. 'user_id' => 1
  119. ));
  120. $Article->create();
  121. $result = $Article->save($data);
  122. $this->assertFalse(empty($result));
  123. $testResult = $Article->find('first', array('conditions' => array('Article.title' => 'Test Title')));
  124. $this->assertEquals($data['Article']['title'], $testResult['Article']['title']);
  125. $this->assertEquals('2008-01-01 00:00:00', $testResult['Article']['created']);
  126. }
  127. /**
  128. * testUnderscoreFieldSave method
  129. *
  130. * @return void
  131. */
  132. public function testUnderscoreFieldSave() {
  133. $this->loadFixtures('UnderscoreField');
  134. $UnderscoreField = new UnderscoreField();
  135. $currentCount = $UnderscoreField->find('count');
  136. $this->assertEquals(3, $currentCount);
  137. $data = array('UnderscoreField' => array(
  138. 'user_id' => '1',
  139. 'my_model_has_a_field' => 'Content here',
  140. 'body' => 'Body',
  141. 'published' => 'Y',
  142. 'another_field' => 4
  143. ));
  144. $ret = $UnderscoreField->save($data);
  145. $this->assertFalse(empty($ret));
  146. $currentCount = $UnderscoreField->find('count');
  147. $this->assertEquals(4, $currentCount);
  148. }
  149. /**
  150. * testAutoSaveUuid method
  151. *
  152. * @return void
  153. */
  154. public function testAutoSaveUuid() {
  155. // SQLite does not support non-integer primary keys
  156. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  157. $this->loadFixtures('Uuid');
  158. $TestModel = new Uuid();
  159. $TestModel->save(array('title' => 'Test record'));
  160. $result = $TestModel->findByTitle('Test record');
  161. $this->assertEquals(
  162. array('id', 'title', 'count', 'created', 'updated'),
  163. array_keys($result['Uuid'])
  164. );
  165. $this->assertEquals(36, strlen($result['Uuid']['id']));
  166. }
  167. /**
  168. * Ensure that if the id key is null but present the save doesn't fail (with an
  169. * x sql error: "Column id specified twice")
  170. *
  171. * @return void
  172. */
  173. public function testSaveUuidNull() {
  174. // SQLite does not support non-integer primary keys
  175. $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with SQLite.');
  176. $this->loadFixtures('Uuid');
  177. $TestModel = new Uuid();
  178. $TestModel->save(array('title' => 'Test record', 'id' => null));
  179. $result = $TestModel->findByTitle('Test record');
  180. $this->assertEquals(
  181. array('id', 'title', 'count', 'created', 'updated'),
  182. array_keys($result['Uuid'])
  183. );
  184. $this->assertEquals(36, strlen($result['Uuid']['id']));
  185. }
  186. /**
  187. * testZeroDefaultFieldValue method
  188. *
  189. * @return void
  190. */
  191. public function testZeroDefaultFieldValue() {
  192. $this->skipIf($this->db instanceof Sqlite, 'SQLite uses loose typing, this operation is unsupported.');
  193. $this->loadFixtures('DataTest');
  194. $TestModel = new DataTest();
  195. $TestModel->create(array());
  196. $TestModel->save();
  197. $result = $TestModel->findById($TestModel->id);
  198. $this->assertEquals(0, $result['DataTest']['count']);
  199. $this->assertEquals(0, $result['DataTest']['float']);
  200. }
  201. /**
  202. * Tests validation parameter order in custom validation methods
  203. *
  204. * @return void
  205. */
  206. public function testAllowSimulatedFields() {
  207. $TestModel = new ValidationTest1();
  208. $TestModel->create(array(
  209. 'title' => 'foo',
  210. 'bar' => 'baz'
  211. ));
  212. $expected = array(
  213. 'ValidationTest1' => array(
  214. 'title' => 'foo',
  215. 'bar' => 'baz'
  216. ));
  217. $this->assertEquals($expected, $TestModel->data);
  218. }
  219. /**
  220. * test that Caches are getting cleared on save().
  221. * ensure that both inflections of controller names are getting cleared
  222. * as URL for controller could be either overallFavorites/index or overall_favorites/index
  223. *
  224. * @return void
  225. */
  226. public function testCacheClearOnSave() {
  227. $_back = array(
  228. 'check' => Configure::read('Cache.check'),
  229. 'disable' => Configure::read('Cache.disable'),
  230. );
  231. Configure::write('Cache.check', true);
  232. Configure::write('Cache.disable', false);
  233. $this->loadFixtures('OverallFavorite');
  234. $OverallFavorite = new OverallFavorite();
  235. touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php');
  236. touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php');
  237. $data = array(
  238. 'OverallFavorite' => array(
  239. 'id' => 22,
  240. 'model_type' => '8-track',
  241. 'model_id' => '3',
  242. 'priority' => '1'
  243. )
  244. );
  245. $OverallFavorite->create($data);
  246. $OverallFavorite->save();
  247. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'));
  248. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'));
  249. Configure::write('Cache.check', $_back['check']);
  250. Configure::write('Cache.disable', $_back['disable']);
  251. }
  252. /**
  253. * test that save() resets whitelist on failed save
  254. */
  255. public function testSaveFieldListResetsWhitelistOnFailedSave() {
  256. $this->loadFixtures('Bidding');
  257. $model = new Bidding();
  258. $whitelist = array('title');
  259. $model->whitelist = $whitelist;
  260. $result = $model->save(
  261. array(),
  262. array('fieldList' => array('body'))
  263. );
  264. $this->assertFalse($result);
  265. $this->assertEquals($whitelist, $model->whitelist);
  266. }
  267. /**
  268. * testSaveWithCounterCache method
  269. *
  270. * @return void
  271. */
  272. public function testSaveWithCounterCache() {
  273. $this->loadFixtures('Syfile', 'Item', 'Image', 'Portfolio', 'ItemsPortfolio');
  274. $TestModel = new Syfile();
  275. $TestModel2 = new Item();
  276. $result = $TestModel->findById(1);
  277. $this->assertNull($result['Syfile']['item_count']);
  278. $TestModel2->save(array(
  279. 'name' => 'Item 7',
  280. 'syfile_id' => 1,
  281. 'published' => false
  282. ));
  283. $result = $TestModel->findById(1);
  284. $this->assertEquals(2, $result['Syfile']['item_count']);
  285. $TestModel2->delete(1);
  286. $result = $TestModel->findById(1);
  287. $this->assertEquals(1, $result['Syfile']['item_count']);
  288. $TestModel2->id = 2;
  289. $TestModel2->saveField('syfile_id', 1);
  290. $result = $TestModel->findById(1);
  291. $this->assertEquals(2, $result['Syfile']['item_count']);
  292. $result = $TestModel->findById(2);
  293. $this->assertEquals(0, $result['Syfile']['item_count']);
  294. }
  295. /**
  296. * Tests that counter caches are updated when records are added
  297. *
  298. * @return void
  299. */
  300. public function testCounterCacheIncrease() {
  301. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  302. $User = new CounterCacheUser();
  303. $Post = new CounterCachePost();
  304. $data = array('Post' => array(
  305. 'id' => 22,
  306. 'title' => 'New Post',
  307. 'user_id' => 66
  308. ));
  309. $Post->save($data);
  310. $user = $User->find('first', array(
  311. 'conditions' => array('id' => 66),
  312. 'recursive' => -1
  313. ));
  314. $result = $user[$User->alias]['post_count'];
  315. $expected = 3;
  316. $this->assertEquals($expected, $result);
  317. }
  318. /**
  319. * Tests that counter caches are updated when records are deleted
  320. *
  321. * @return void
  322. */
  323. public function testCounterCacheDecrease() {
  324. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  325. $User = new CounterCacheUser();
  326. $Post = new CounterCachePost();
  327. $Post->delete(2);
  328. $user = $User->find('first', array(
  329. 'conditions' => array('id' => 66),
  330. 'recursive' => -1
  331. ));
  332. $result = $user[$User->alias]['post_count'];
  333. $expected = 1;
  334. $this->assertEquals($expected, $result);
  335. }
  336. /**
  337. * Tests that counter caches are updated when foreign keys of counted records change
  338. *
  339. * @return void
  340. */
  341. public function testCounterCacheUpdated() {
  342. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  343. $User = new CounterCacheUser();
  344. $Post = new CounterCachePost();
  345. $data = $Post->find('first', array(
  346. 'conditions' => array('id' => 1),
  347. 'recursive' => -1
  348. ));
  349. $data[$Post->alias]['user_id'] = 301;
  350. $Post->save($data);
  351. $users = $User->find('all', array('order' => 'User.id'));
  352. $this->assertEquals(1, $users[0]['User']['post_count']);
  353. $this->assertEquals(2, $users[1]['User']['post_count']);
  354. }
  355. /**
  356. * Test counter cache with models that use a non-standard (i.e. not using 'id')
  357. * as their primary key.
  358. *
  359. * @return void
  360. */
  361. public function testCounterCacheWithNonstandardPrimaryKey() {
  362. $this->loadFixtures(
  363. 'CounterCacheUserNonstandardPrimaryKey',
  364. 'CounterCachePostNonstandardPrimaryKey'
  365. );
  366. $User = new CounterCacheUserNonstandardPrimaryKey();
  367. $Post = new CounterCachePostNonstandardPrimaryKey();
  368. $data = $Post->find('first', array(
  369. 'conditions' => array('pid' => 1),
  370. 'recursive' => -1
  371. ));
  372. $data[$Post->alias]['uid'] = 301;
  373. $Post->save($data);
  374. $users = $User->find('all', array('order' => 'User.uid'));
  375. $this->assertEquals(1, $users[0]['User']['post_count']);
  376. $this->assertEquals(2, $users[1]['User']['post_count']);
  377. }
  378. /**
  379. * test Counter Cache With Self Joining table
  380. *
  381. * @return void
  382. */
  383. public function testCounterCacheWithSelfJoin() {
  384. $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN');
  385. $this->loadFixtures('CategoryThread');
  386. $column = 'COLUMN ';
  387. if ($this->db instanceof Sqlserver) {
  388. $column = '';
  389. }
  390. $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer'));
  391. $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column);
  392. $this->db->flushMethodCache();
  393. $Category = new CategoryThread();
  394. $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5));
  395. $this->assertFalse(empty($result));
  396. $Category = new CategoryThread();
  397. $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
  398. $Category->updateCounterCache(array('parent_id' => 5));
  399. $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
  400. $expected = array(1);
  401. $this->assertEquals($expected, $result);
  402. }
  403. /**
  404. * testSaveWithCounterCacheScope method
  405. *
  406. * @return void
  407. */
  408. public function testSaveWithCounterCacheScope() {
  409. $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio');
  410. $TestModel = new Syfile();
  411. $TestModel2 = new Item();
  412. $TestModel2->belongsTo['Syfile']['counterCache'] = true;
  413. $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true);
  414. $result = $TestModel->findById(1);
  415. $this->assertNull($result['Syfile']['item_count']);
  416. $TestModel2->save(array(
  417. 'name' => 'Item 7',
  418. 'syfile_id' => 1,
  419. 'published' => true
  420. ));
  421. $result = $TestModel->findById(1);
  422. $this->assertEquals(1, $result['Syfile']['item_count']);
  423. $TestModel2->id = 1;
  424. $TestModel2->saveField('published', true);
  425. $result = $TestModel->findById(1);
  426. $this->assertEquals(2, $result['Syfile']['item_count']);
  427. $TestModel2->save(array(
  428. 'id' => 1,
  429. 'syfile_id' => 1,
  430. 'published' => false
  431. ));
  432. $result = $TestModel->findById(1);
  433. $this->assertEquals(1, $result['Syfile']['item_count']);
  434. }
  435. /**
  436. * Tests having multiple counter caches for an associated model
  437. *
  438. * @return void
  439. */
  440. public function testCounterCacheMultipleCaches() {
  441. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  442. $User = new CounterCacheUser();
  443. $Post = new CounterCachePost();
  444. $Post->unbindModel(array('belongsTo' => array('User')), false);
  445. $Post->bindModel(array(
  446. 'belongsTo' => array(
  447. 'User' => array(
  448. 'className' => 'CounterCacheUser',
  449. 'foreignKey' => 'user_id',
  450. 'counterCache' => array(
  451. true,
  452. 'posts_published' => array('Post.published' => true)
  453. )
  454. )
  455. )
  456. ), false);
  457. // Count Increase
  458. $data = array('Post' => array(
  459. 'id' => 22,
  460. 'title' => 'New Post',
  461. 'user_id' => 66,
  462. 'published' => true
  463. ));
  464. $Post->save($data);
  465. $result = $User->find('first', array(
  466. 'conditions' => array('id' => 66),
  467. 'recursive' => -1
  468. ));
  469. $this->assertEquals(3, $result[$User->alias]['post_count']);
  470. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  471. // Count decrease
  472. $Post->delete(1);
  473. $result = $User->find('first', array(
  474. 'conditions' => array('id' => 66),
  475. 'recursive' => -1
  476. ));
  477. $this->assertEquals(2, $result[$User->alias]['post_count']);
  478. $this->assertEquals(2, $result[$User->alias]['posts_published']);
  479. // Count update
  480. $data = $Post->find('first', array(
  481. 'conditions' => array('id' => 1),
  482. 'recursive' => -1
  483. ));
  484. $data[$Post->alias]['user_id'] = 301;
  485. $Post->save($data);
  486. $result = $User->find('all', array('order' => 'User.id'));
  487. $this->assertEquals(2, $result[0]['User']['post_count']);
  488. $this->assertEquals(1, $result[1]['User']['posts_published']);
  489. }
  490. /**
  491. * Tests that counter caches are unchanged when using 'counterCache' => false
  492. *
  493. * @return void
  494. */
  495. public function testCounterCacheSkip() {
  496. $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
  497. $User = new CounterCacheUser();
  498. $Post = new CounterCachePost();
  499. $data = $Post->find('first', array(
  500. 'conditions' => array('id' => 1),
  501. 'recursive' => -1
  502. ));
  503. $data[$Post->alias]['user_id'] = 301;
  504. $Post->save($data, array('counterCache' => false));
  505. $users = $User->find('all', array('order' => 'User.id'));
  506. $this->assertEquals(2, $users[0]['User']['post_count']);
  507. $this->assertEquals(1, $users[1]['User']['post_count']);
  508. }
  509. /**
  510. * test that beforeValidate returning false can abort saves.
  511. *
  512. * @return void
  513. */
  514. public function testBeforeValidateSaveAbortion() {
  515. $this->loadFixtures('Post');
  516. $Model = new CallbackPostTestModel();
  517. $Model->beforeValidateReturn = false;
  518. $data = array(
  519. 'title' => 'new article',
  520. 'body' => 'this is some text.'
  521. );
  522. $Model->create();
  523. $result = $Model->save($data);
  524. $this->assertFalse($result);
  525. }
  526. /**
  527. * test that beforeSave returning false can abort saves.
  528. *
  529. * @return void
  530. */
  531. public function testBeforeSaveSaveAbortion() {
  532. $this->loadFixtures('Post');
  533. $Model = new CallbackPostTestModel();
  534. $Model->beforeSaveReturn = false;
  535. $data = array(
  536. 'title' => 'new article',
  537. 'body' => 'this is some text.'
  538. );
  539. $Model->create();
  540. $result = $Model->save($data);
  541. $this->assertFalse($result);
  542. }
  543. /**
  544. * testSaveField method
  545. *
  546. * @return void
  547. */
  548. public function testSaveField() {
  549. $this->loadFixtures('Article');
  550. $TestModel = new Article();
  551. $TestModel->id = 1;
  552. $result = $TestModel->saveField('title', 'New First Article');
  553. $this->assertFalse(empty($result));
  554. $TestModel->recursive = -1;
  555. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  556. $expected = array('Article' => array(
  557. 'id' => '1',
  558. 'user_id' => '1',
  559. 'title' => 'New First Article',
  560. 'body' => 'First Article Body'
  561. ));
  562. $this->assertEquals($expected, $result);
  563. $TestModel->id = 1;
  564. $result = $TestModel->saveField('title', '');
  565. $this->assertFalse(empty($result));
  566. $TestModel->recursive = -1;
  567. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  568. $expected = array('Article' => array(
  569. 'id' => '1',
  570. 'user_id' => '1',
  571. 'title' => '',
  572. 'body' => 'First Article Body'
  573. ));
  574. $result['Article']['title'] = trim($result['Article']['title']);
  575. $this->assertEquals($expected, $result);
  576. $TestModel->id = 1;
  577. $TestModel->set('body', 'Messed up data');
  578. $result = $TestModel->saveField('title', 'First Article');
  579. $this->assertFalse(empty($result));
  580. $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  581. $expected = array('Article' => array(
  582. 'id' => '1',
  583. 'user_id' => '1',
  584. 'title' => 'First Article',
  585. 'body' => 'First Article Body'
  586. ));
  587. $this->assertEquals($expected, $result);
  588. $TestModel->recursive = -1;
  589. $TestModel->read(array('id', 'user_id', 'title', 'body'), 1);
  590. $TestModel->id = 1;
  591. $result = $TestModel->saveField('title', '', true);
  592. $this->assertFalse($result);
  593. $TestModel->recursive = -1;
  594. $TestModel->id = 1;
  595. $result = $TestModel->saveField('user_id', 9999);
  596. $this->assertTrue((bool)$result);
  597. $result = $TestModel->read(array('id', 'user_id'), 1);
  598. $expected = array('Article' => array(
  599. 'id' => '1',
  600. 'user_id' => '9999',
  601. ));
  602. $this->assertEquals($expected, $result);
  603. $this->loadFixtures('Node', 'Dependency');
  604. $Node = new Node();
  605. $Node->set('id', 1);
  606. $result = $Node->read();
  607. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  608. $Node->saveField('state', 10);
  609. $result = $Node->read();
  610. $this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
  611. }
  612. /**
  613. * testSaveWithCreate method
  614. *
  615. * @return void
  616. */
  617. public function testSaveWithCreate() {
  618. $this->loadFixtures(
  619. 'User',
  620. 'Article',
  621. 'User',
  622. 'Comment',
  623. 'Tag',
  624. 'ArticlesTag',
  625. 'Attachment'
  626. );
  627. $TestModel = new User();
  628. $data = array('User' => array(
  629. 'user' => 'user',
  630. 'password' => ''
  631. ));
  632. $result = $TestModel->save($data);
  633. $this->assertFalse($result);
  634. $this->assertTrue(!empty($TestModel->validationErrors));
  635. $TestModel = new Article();
  636. $data = array('Article' => array(
  637. 'user_id' => '',
  638. 'title' => '',
  639. 'body' => ''
  640. ));
  641. $result = $TestModel->create($data) && $TestModel->save();
  642. $this->assertFalse($result);
  643. $this->assertTrue(!empty($TestModel->validationErrors));
  644. $data = array('Article' => array(
  645. 'id' => 1,
  646. 'user_id' => '1',
  647. 'title' => 'New First Article',
  648. 'body' => ''
  649. ));
  650. $result = $TestModel->create($data) && $TestModel->save();
  651. $this->assertFalse($result);
  652. $data = array('Article' => array(
  653. 'id' => 1,
  654. 'title' => 'New First Article'
  655. ));
  656. $result = $TestModel->create() && $TestModel->save($data, false);
  657. $this->assertFalse(empty($result));
  658. $TestModel->recursive = -1;
  659. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  660. $expected = array('Article' => array(
  661. 'id' => '1',
  662. 'user_id' => '1',
  663. 'title' => 'New First Article',
  664. 'body' => 'First Article Body',
  665. 'published' => 'N'
  666. ));
  667. $this->assertEquals($expected, $result);
  668. $data = array('Article' => array(
  669. 'id' => 1,
  670. 'user_id' => '2',
  671. 'title' => 'First Article',
  672. 'body' => 'New First Article Body',
  673. 'published' => 'Y'
  674. ));
  675. $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published'));
  676. $this->assertFalse(empty($result));
  677. $TestModel->recursive = -1;
  678. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1);
  679. $expected = array('Article' => array(
  680. 'id' => '1',
  681. 'user_id' => '1',
  682. 'title' => 'First Article',
  683. 'body' => 'First Article Body',
  684. 'published' => 'Y'
  685. ));
  686. $this->assertEquals($expected, $result);
  687. $data = array(
  688. 'Article' => array(
  689. 'user_id' => '2',
  690. 'title' => 'New Article',
  691. 'body' => 'New Article Body',
  692. 'created' => '2007-03-18 14:55:23',
  693. 'updated' => '2007-03-18 14:57:31'
  694. ),
  695. 'Tag' => array('Tag' => array(1, 3))
  696. );
  697. $TestModel->create();
  698. $result = $TestModel->create() && $TestModel->save($data);
  699. $this->assertFalse(empty($result));
  700. $TestModel->recursive = 2;
  701. $result = $TestModel->read(null, 4);
  702. $expected = array(
  703. 'Article' => array(
  704. 'id' => '4',
  705. 'user_id' => '2',
  706. 'title' => 'New Article',
  707. 'body' => 'New Article Body',
  708. 'published' => 'N',
  709. 'created' => '2007-03-18 14:55:23',
  710. 'updated' => '2007-03-18 14:57:31'
  711. ),
  712. 'User' => array(
  713. 'id' => '2',
  714. 'user' => 'nate',
  715. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  716. 'created' => '2007-03-17 01:18:23',
  717. 'updated' => '2007-03-17 01:20:31'
  718. ),
  719. 'Comment' => array(),
  720. 'Tag' => array(
  721. array(
  722. 'id' => '1',
  723. 'tag' => 'tag1',
  724. 'created' => '2007-03-18 12:22:23',
  725. 'updated' => '2007-03-18 12:24:31'
  726. ),
  727. array(
  728. 'id' => '3',
  729. 'tag' => 'tag3',
  730. 'created' => '2007-03-18 12:26:23',
  731. 'updated' => '2007-03-18 12:28:31'
  732. )));
  733. $this->assertEquals($expected, $result);
  734. $data = array('Comment' => array(
  735. 'article_id' => '4',
  736. 'user_id' => '1',
  737. 'comment' => 'Comment New Article',
  738. 'published' => 'Y',
  739. 'created' => '2007-03-18 14:57:23',
  740. 'updated' => '2007-03-18 14:59:31'
  741. ));
  742. $result = $TestModel->Comment->create() && $TestModel->Comment->save($data);
  743. $this->assertFalse(empty($result));
  744. $data = array('Attachment' => array(
  745. 'comment_id' => '7',
  746. 'attachment' => 'newattachment.zip',
  747. 'created' => '2007-03-18 15:02:23',
  748. 'updated' => '2007-03-18 15:04:31'
  749. ));
  750. $result = $TestModel->Comment->Attachment->save($data);
  751. $this->assertFalse(empty($result));
  752. $TestModel->recursive = 2;
  753. $result = $TestModel->read(null, 4);
  754. $expected = array(
  755. 'Article' => array(
  756. 'id' => '4',
  757. 'user_id' => '2',
  758. 'title' => 'New Article',
  759. 'body' => 'New Article Body',
  760. 'published' => 'N',
  761. 'created' => '2007-03-18 14:55:23',
  762. 'updated' => '2007-03-18 14:57:31'
  763. ),
  764. 'User' => array(
  765. 'id' => '2',
  766. 'user' => 'nate',
  767. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  768. 'created' => '2007-03-17 01:18:23',
  769. 'updated' => '2007-03-17 01:20:31'
  770. ),
  771. 'Comment' => array(
  772. array(
  773. 'id' => '7',
  774. 'article_id' => '4',
  775. 'user_id' => '1',
  776. 'comment' => 'Comment New Article',
  777. 'published' => 'Y',
  778. 'created' => '2007-03-18 14:57:23',
  779. 'updated' => '2007-03-18 14:59:31',
  780. 'Article' => array(
  781. 'id' => '4',
  782. 'user_id' => '2',
  783. 'title' => 'New Article',
  784. 'body' => 'New Article Body',
  785. 'published' => 'N',
  786. 'created' => '2007-03-18 14:55:23',
  787. 'updated' => '2007-03-18 14:57:31'
  788. ),
  789. 'User' => array(
  790. 'id' => '1',
  791. 'user' => 'mariano',
  792. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  793. 'created' => '2007-03-17 01:16:23',
  794. 'updated' => '2007-03-17 01:18:31'
  795. ),
  796. 'Attachment' => array(
  797. 'id' => '2',
  798. 'comment_id' => '7',
  799. 'attachment' => 'newattachment.zip',
  800. 'created' => '2007-03-18 15:02:23',
  801. 'updated' => '2007-03-18 15:04:31'
  802. ))),
  803. 'Tag' => array(
  804. array(
  805. 'id' => '1',
  806. 'tag' => 'tag1',
  807. 'created' => '2007-03-18 12:22:23',
  808. 'updated' => '2007-03-18 12:24:31'
  809. ),
  810. array(
  811. 'id' => '3',
  812. 'tag' => 'tag3',
  813. 'created' => '2007-03-18 12:26:23',
  814. 'updated' => '2007-03-18 12:28:31'
  815. )));
  816. $this->assertEquals($expected, $result);
  817. }
  818. /**
  819. * test that a null Id doesn't cause errors
  820. *
  821. * @return void
  822. */
  823. public function testSaveWithNullId() {
  824. $this->loadFixtures('User');
  825. $User = new User();
  826. $User->read(null, 1);
  827. $User->data['User']['id'] = null;
  828. $result = $User->save(array('password' => 'test'));
  829. $this->assertFalse(empty($result));
  830. $this->assertTrue($User->id > 0);
  831. $User->read(null, 2);
  832. $User->data['User']['id'] = null;
  833. $result = $User->save(array('password' => 'test'));
  834. $this->assertFalse(empty($result));
  835. $this->assertTrue($User->id > 0);
  836. $User->data['User'] = array('password' => 'something');
  837. $result = $User->save();
  838. $this->assertFalse(empty($result));
  839. $result = $User->read();
  840. $this->assertEquals('something', $User->data['User']['password']);
  841. }
  842. /**
  843. * testSaveWithSet method
  844. *
  845. * @return void
  846. */
  847. public function testSaveWithSet() {
  848. $this->loadFixtures('Article');
  849. $TestModel = new Article();
  850. // Create record we will be updating later
  851. $data = array('Article' => array(
  852. 'user_id' => '1',
  853. 'title' => 'Fourth Article',
  854. 'body' => 'Fourth Article Body',
  855. 'published' => 'Y'
  856. ));
  857. $result = $TestModel->create() && $TestModel->save($data);
  858. $this->assertFalse(empty($result));
  859. // Check record we created
  860. $TestModel->recursive = -1;
  861. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  862. $expected = array('Article' => array(
  863. 'id' => '4',
  864. 'user_id' => '1',
  865. 'title' => 'Fourth Article',
  866. 'body' => 'Fourth Article Body',
  867. 'published' => 'Y'
  868. ));
  869. $this->assertEquals($expected, $result);
  870. // Create new record just to overlap Model->id on previously created record
  871. $data = array('Article' => array(
  872. 'user_id' => '4',
  873. 'title' => 'Fifth Article',
  874. 'body' => 'Fifth Article Body',
  875. 'published' => 'Y'
  876. ));
  877. $result = $TestModel->create() && $TestModel->save($data);
  878. $this->assertFalse(empty($result));
  879. $TestModel->recursive = -1;
  880. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  881. $expected = array('Article' => array(
  882. 'id' => '5',
  883. 'user_id' => '4',
  884. 'title' => 'Fifth Article',
  885. 'body' => 'Fifth Article Body',
  886. 'published' => 'Y'
  887. ));
  888. $this->assertEquals($expected, $result);
  889. // Go back and edit the first article we created, starting by checking it's still there
  890. $TestModel->recursive = -1;
  891. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  892. $expected = array('Article' => array(
  893. 'id' => '4',
  894. 'user_id' => '1',
  895. 'title' => 'Fourth Article',
  896. 'body' => 'Fourth Article Body',
  897. 'published' => 'Y'
  898. ));
  899. $this->assertEquals($expected, $result);
  900. // And now do the update with set()
  901. $data = array('Article' => array(
  902. 'id' => '4',
  903. 'title' => 'Fourth Article - New Title',
  904. 'published' => 'N'
  905. ));
  906. $result = $TestModel->set($data) && $TestModel->save();
  907. $this->assertFalse(empty($result));
  908. $TestModel->recursive = -1;
  909. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  910. $expected = array('Article' => array(
  911. 'id' => '4',
  912. 'user_id' => '1',
  913. 'title' => 'Fourth Article - New Title',
  914. 'body' => 'Fourth Article Body',
  915. 'published' => 'N'
  916. ));
  917. $this->assertEquals($expected, $result);
  918. $TestModel->recursive = -1;
  919. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  920. $expected = array('Article' => array(
  921. 'id' => '5',
  922. 'user_id' => '4',
  923. 'title' => 'Fifth Article',
  924. 'body' => 'Fifth Article Body',
  925. 'published' => 'Y'
  926. ));
  927. $this->assertEquals($expected, $result);
  928. $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5'));
  929. $result = ($TestModel->set($data) && $TestModel->save());
  930. $this->assertFalse(empty($result));
  931. $TestModel->recursive = -1;
  932. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  933. $expected = array('Article' => array(
  934. 'id' => '5',
  935. 'user_id' => '4',
  936. 'title' => 'Fifth Article - New Title 5',
  937. 'body' => 'Fifth Article Body',
  938. 'published' => 'Y'
  939. ));
  940. $this->assertEquals($expected, $result);
  941. $TestModel->recursive = -1;
  942. $result = $TestModel->find('all', array(
  943. 'fields' => array('id', 'title'),
  944. 'order' => array('Article.id' => 'ASC')
  945. ));
  946. $expected = array(
  947. array('Article' => array('id' => 1, 'title' => 'First Article')),
  948. array('Article' => array('id' => 2, 'title' => 'Second Article')),
  949. array('Article' => array('id' => 3, 'title' => 'Third Article')),
  950. array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title')),
  951. array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5'))
  952. );
  953. $this->assertEquals($expected, $result);
  954. }
  955. /**
  956. * testSaveWithNonExistentFields method
  957. *
  958. * @return void
  959. */
  960. public function testSaveWithNonExistentFields() {
  961. $this->loadFixtures('Article');
  962. $TestModel = new Article();
  963. $TestModel->recursive = -1;
  964. $data = array(
  965. 'non_existent' => 'This field does not exist',
  966. 'user_id' => '1',
  967. 'title' => 'Fourth Article - New Title',
  968. 'body' => 'Fourth Article Body',
  969. 'published' => 'N'
  970. );
  971. $result = $TestModel->create() && $TestModel->save($data);
  972. $this->assertFalse(empty($result));
  973. $expected = array('Article' => array(
  974. 'id' => '4',
  975. 'user_id' => '1',
  976. 'title' => 'Fourth Article - New Title',
  977. 'body' => 'Fourth Article Body',
  978. 'published' => 'N'
  979. ));
  980. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4);
  981. $this->assertEquals($expected, $result);
  982. $data = array(
  983. 'user_id' => '1',
  984. 'non_existent' => 'This field does not exist',
  985. 'title' => 'Fifth Article - New Title',
  986. 'body' => 'Fifth Article Body',
  987. 'published' => 'N'
  988. );
  989. $result = $TestModel->create() && $TestModel->save($data);
  990. $this->assertFalse(empty($result));
  991. $expected = array('Article' => array(
  992. 'id' => '5',
  993. 'user_id' => '1',
  994. 'title' => 'Fifth Article - New Title',
  995. 'body' => 'Fifth Article Body',
  996. 'published' => 'N'
  997. ));
  998. $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5);
  999. $this->assertEquals($expected, $result);
  1000. }
  1001. /**
  1002. * testSaveFromXml method
  1003. *
  1004. * @return void
  1005. */
  1006. public function testSaveFromXml() {
  1007. $this->markTestSkipped('This feature needs to be fixed or dropped');
  1008. $this->loadFixtures('Article');
  1009. App::uses('Xml', 'Utility');
  1010. $Article = new Article();
  1011. $result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
  1012. $this->assertFalse(empty($result));
  1013. $results = $Article->find('first', array('conditions' => array('Article.title' => 'test xml')));
  1014. $this->assertFalse(empty($results));
  1015. $result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
  1016. $this->assertFalse(empty($result));
  1017. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing')));
  1018. $this->assertFalse(empty($results));
  1019. $result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
  1020. $this->assertFalse(empty($result));
  1021. $results = $Article->find('first', array('conditions' => array('Article.title' => 'testing with DOMDocument')));
  1022. $this->assertFalse(empty($results));
  1023. }
  1024. /**
  1025. * testSaveHabtm method
  1026. *
  1027. * @return void
  1028. */
  1029. public function testSaveHabtm() {
  1030. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1031. $TestModel = new Article();
  1032. $result = $TestModel->findById(2);
  1033. $expected = array(
  1034. 'Article' => array(
  1035. 'id' => '2',
  1036. 'user_id' => '3',
  1037. 'title' => 'Second Article',
  1038. 'body' => 'Second Article Body',
  1039. 'published' => 'Y',
  1040. 'created' => '2007-03-18 10:41:23',
  1041. 'updated' => '2007-03-18 10:43:31'
  1042. ),
  1043. 'User' => array(
  1044. 'id' => '3',
  1045. 'user' => 'larry',
  1046. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1047. 'created' => '2007-03-17 01:20:23',
  1048. 'updated' => '2007-03-17 01:22:31'
  1049. ),
  1050. 'Comment' => array(
  1051. array(
  1052. 'id' => '5',
  1053. 'article_id' => '2',
  1054. 'user_id' => '1',
  1055. 'comment' => 'First Comment for Second Article',
  1056. 'published' => 'Y',
  1057. 'created' => '2007-03-18 10:53:23',
  1058. 'updated' => '2007-03-18 10:55:31'
  1059. ),
  1060. array(
  1061. 'id' => '6',
  1062. 'article_id' => '2',
  1063. 'user_id' => '2',
  1064. 'comment' => 'Second Comment for Second Article',
  1065. 'published' => 'Y',
  1066. 'created' => '2007-03-18 10:55:23',
  1067. 'updated' => '2007-03-18 10:57:31'
  1068. )),
  1069. 'Tag' => array(
  1070. array(
  1071. 'id' => '1',
  1072. 'tag' => 'tag1',
  1073. 'created' => '2007-03-18 12:22:23',
  1074. 'updated' => '2007-03-18 12:24:31'
  1075. ),
  1076. array(
  1077. 'id' => '3',
  1078. 'tag' => 'tag3',
  1079. 'created' => '2007-03-18 12:26:23',
  1080. 'updated' => '2007-03-18 12:28:31'
  1081. )
  1082. )
  1083. );
  1084. $this->assertEquals($expected, $result);
  1085. $data = array(
  1086. 'Article' => array(
  1087. 'id' => '2',
  1088. 'title' => 'New Second Article'
  1089. ),
  1090. 'Tag' => array('Tag' => array(1, 2))
  1091. );
  1092. $result = $TestModel->set($data);
  1093. $this->assertFalse(empty($result));
  1094. $result = $TestModel->save();
  1095. $this->assertFalse(empty($result));
  1096. $this->assertEquals($data['Tag'], $result['Tag']);
  1097. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
  1098. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1099. $expected = array(
  1100. 'Article' => array(
  1101. 'id' => '2',
  1102. 'user_id' => '3',
  1103. 'title' => 'New Second Article',
  1104. 'body' => 'Second Article Body'
  1105. ),
  1106. 'Tag' => array(
  1107. array(
  1108. 'id' => '1',
  1109. 'tag' => 'tag1',
  1110. 'created' => '2007-03-18 12:22:23',
  1111. 'updated' => '2007-03-18 12:24:31'
  1112. ),
  1113. array(
  1114. 'id' => '2',
  1115. 'tag' => 'tag2',
  1116. 'created' => '2007-03-18 12:24:23',
  1117. 'updated' => '2007-03-18 12:26:31'
  1118. )));
  1119. $this->assertEquals($expected, $result);
  1120. $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
  1121. $result = $TestModel->set($data);
  1122. $this->assertFalse(empty($result));
  1123. $result = $TestModel->save();
  1124. $this->assertFalse(empty($result));
  1125. $TestModel->unbindModel(array(
  1126. 'belongsTo' => array('User'),
  1127. 'hasMany' => array('Comment')
  1128. ));
  1129. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1130. $expected = array(
  1131. 'Article' => array(
  1132. 'id' => '2',
  1133. 'user_id' => '3',
  1134. 'title' => 'New Second Article',
  1135. 'body' => 'Second Article Body'
  1136. ),
  1137. 'Tag' => array(
  1138. array(
  1139. 'id' => '2',
  1140. 'tag' => 'tag2',
  1141. 'created' => '2007-03-18 12:24:23',
  1142. 'updated' => '2007-03-18 12:26:31'
  1143. ),
  1144. array(
  1145. 'id' => '3',
  1146. 'tag' => 'tag3',
  1147. 'created' => '2007-03-18 12:26:23',
  1148. 'updated' => '2007-03-18 12:28:31'
  1149. )));
  1150. $this->assertEquals($expected, $result);
  1151. $data = array('Tag' => array('Tag' => array(1, 2, 3)));
  1152. $result = $TestModel->set($data);
  1153. $this->assertFalse(empty($result));
  1154. $result = $TestModel->save();
  1155. $this->assertFalse(empty($result));
  1156. $TestModel->unbindModel(array(
  1157. 'belongsTo' => array('User'),
  1158. 'hasMany' => array('Comment')
  1159. ));
  1160. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1161. $expected = array(
  1162. 'Article' => array(
  1163. 'id' => '2',
  1164. 'user_id' => '3',
  1165. 'title' => 'New Second Article',
  1166. 'body' => 'Second Article Body'
  1167. ),
  1168. 'Tag' => array(
  1169. array(
  1170. 'id' => '1',
  1171. 'tag' => 'tag1',
  1172. 'created' => '2007-03-18 12:22:23',
  1173. 'updated' => '2007-03-18 12:24:31'
  1174. ),
  1175. array(
  1176. 'id' => '2',
  1177. 'tag' => 'tag2',
  1178. 'created' => '2007-03-18 12:24:23',
  1179. 'updated' => '2007-03-18 12:26:31'
  1180. ),
  1181. array(
  1182. 'id' => '3',
  1183. 'tag' => 'tag3',
  1184. 'created' => '2007-03-18 12:26:23',
  1185. 'updated' => '2007-03-18 12:28:31'
  1186. )));
  1187. $this->assertEquals($expected, $result);
  1188. $data = array('Tag' => array('Tag' => array()));
  1189. $result = $TestModel->set($data);
  1190. $this->assertFalse(empty($result));
  1191. $result = $TestModel->save();
  1192. $this->assertFalse(empty($result));
  1193. $data = array('Tag' => array('Tag' => ''));
  1194. $result = $TestModel->set($data);
  1195. $this->assertFalse(empty($result));
  1196. $result = $TestModel->save();
  1197. $this->assertFalse(empty($result));
  1198. $TestModel->unbindModel(array(
  1199. 'belongsTo' => array('User'),
  1200. 'hasMany' => array('Comment')
  1201. ));
  1202. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1203. $expected = array(
  1204. 'Article' => array(
  1205. 'id' => '2',
  1206. 'user_id' => '3',
  1207. 'title' => 'New Second Article',
  1208. 'body' => 'Second Article Body'
  1209. ),
  1210. 'Tag' => array()
  1211. );
  1212. $this->assertEquals($expected, $result);
  1213. $data = array('Tag' => array('Tag' => array(2, 3)));
  1214. $result = $TestModel->set($data);
  1215. $this->assertFalse(empty($result));
  1216. $result = $TestModel->save();
  1217. $this->assertFalse(empty($result));
  1218. $TestModel->unbindModel(array(
  1219. 'belongsTo' => array('User'),
  1220. 'hasMany' => array('Comment')
  1221. ));
  1222. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1223. $expected = array(
  1224. 'Article' => array(
  1225. 'id' => '2',
  1226. 'user_id' => '3',
  1227. 'title' => 'New Second Article',
  1228. 'body' => 'Second Article Body'
  1229. ),
  1230. 'Tag' => array(
  1231. array(
  1232. 'id' => '2',
  1233. 'tag' => 'tag2',
  1234. 'created' => '2007-03-18 12:24:23',
  1235. 'updated' => '2007-03-18 12:26:31'
  1236. ),
  1237. array(
  1238. 'id' => '3',
  1239. 'tag' => 'tag3',
  1240. 'created' => '2007-03-18 12:26:23',
  1241. 'updated' => '2007-03-18 12:28:31'
  1242. )));
  1243. $this->assertEquals($expected, $result);
  1244. $data = array(
  1245. 'Tag' => array(
  1246. 'Tag' => array(1, 2)
  1247. ),
  1248. 'Article' => array(
  1249. 'id' => '2',
  1250. 'title' => 'New Second Article'
  1251. ));
  1252. $result = $TestModel->set($data);
  1253. $this->assertFalse(empty($result));
  1254. $result = $TestModel->save();
  1255. $this->assertFalse(empty($result));
  1256. $TestModel->unbindModel(array(
  1257. 'belongsTo' => array('User'),
  1258. 'hasMany' => array('Comment')
  1259. ));
  1260. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1261. $expected = array(
  1262. 'Article' => array(
  1263. 'id' => '2',
  1264. 'user_id' => '3',
  1265. 'title' => 'New Second Article',
  1266. 'body' => 'Second Article Body'
  1267. ),
  1268. 'Tag' => array(
  1269. array(
  1270. 'id' => '1',
  1271. 'tag' => 'tag1',
  1272. 'created' => '2007-03-18 12:22:23',
  1273. 'updated' => '2007-03-18 12:24:31'
  1274. ),
  1275. array(
  1276. 'id' => '2',
  1277. 'tag' => 'tag2',
  1278. 'created' => '2007-03-18 12:24:23',
  1279. 'updated' => '2007-03-18 12:26:31'
  1280. )));
  1281. $this->assertEquals($expected, $result);
  1282. $data = array(
  1283. 'Tag' => array(
  1284. 'Tag' => array(1, 2)
  1285. ),
  1286. 'Article' => array(
  1287. 'id' => '2',
  1288. 'title' => 'New Second Article Title'
  1289. ));
  1290. $result = $TestModel->set($data);
  1291. $this->assertFalse(empty($result));
  1292. $result = $TestModel->save();
  1293. $this->assertFalse(empty($result));
  1294. $TestModel->unbindModel(array(
  1295. 'belongsTo' => array('User'),
  1296. 'hasMany' => array('Comment')
  1297. ));
  1298. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1299. $expected = array(
  1300. 'Article' => array(
  1301. 'id' => '2',
  1302. 'user_id' => '3',
  1303. 'title' => 'New Second Article Title',
  1304. 'body' => 'Second Article Body'
  1305. ),
  1306. 'Tag' => array(
  1307. array(
  1308. 'id' => '1',
  1309. 'tag' => 'tag1',
  1310. 'created' => '2007-03-18 12:22:23',
  1311. 'updated' => '2007-03-18 12:24:31'
  1312. ),
  1313. array(
  1314. 'id' => '2',
  1315. 'tag' => 'tag2',
  1316. 'created' => '2007-03-18 12:24:23',
  1317. 'updated' => '2007-03-18 12:26:31'
  1318. )
  1319. )
  1320. );
  1321. $this->assertEquals($expected, $result);
  1322. $data = array(
  1323. 'Tag' => array(
  1324. 'Tag' => array(2, 3)
  1325. ),
  1326. 'Article' => array(
  1327. 'id' => '2',
  1328. 'title' => 'Changed Second Article'
  1329. ));
  1330. $result = $TestModel->set($data);
  1331. $this->assertFalse(empty($result));
  1332. $result = $TestModel->save();
  1333. $this->assertFalse(empty($result));
  1334. $TestModel->unbindModel(array(
  1335. 'belongsTo' => array('User'),
  1336. 'hasMany' => array('Comment')
  1337. ));
  1338. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1339. $expected = array(
  1340. 'Article' => array(
  1341. 'id' => '2',
  1342. 'user_id' => '3',
  1343. 'title' => 'Changed Second Article',
  1344. 'body' => 'Second Article Body'
  1345. ),
  1346. 'Tag' => array(
  1347. array(
  1348. 'id' => '2',
  1349. 'tag' => 'tag2',
  1350. 'created' => '2007-03-18 12:24:23',
  1351. 'updated' => '2007-03-18 12:26:31'
  1352. ),
  1353. array(
  1354. 'id' => '3',
  1355. 'tag' => 'tag3',
  1356. 'created' => '2007-03-18 12:26:23',
  1357. 'updated' => '2007-03-18 12:28:31'
  1358. )
  1359. )
  1360. );
  1361. $this->assertEquals($expected, $result);
  1362. $data = array(
  1363. 'Tag' => array(
  1364. 'Tag' => array(1, 3)
  1365. ),
  1366. 'Article' => array('id' => '2'),
  1367. );
  1368. $result = $TestModel->set($data);
  1369. $this->assertFalse(empty($result));
  1370. $result = $TestModel->save();
  1371. $this->assertFalse(empty($result));
  1372. $TestModel->unbindModel(array(
  1373. 'belongsTo' => array('User'),
  1374. 'hasMany' => array('Comment')
  1375. ));
  1376. $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
  1377. $expected = array(
  1378. 'Article' => array(
  1379. 'id' => '2',
  1380. 'user_id' => '3',
  1381. 'title' => 'Changed Second Article',
  1382. 'body' => 'Second Article Body'
  1383. ),
  1384. 'Tag' => array(
  1385. array(
  1386. 'id' => '1',
  1387. 'tag' => 'tag1',
  1388. 'created' => '2007-03-18 12:22:23',
  1389. 'updated' => '2007-03-18 12:24:31'
  1390. ),
  1391. array(
  1392. 'id' => '3',
  1393. 'tag' => 'tag3',
  1394. 'created' => '2007-03-18 12:26:23',
  1395. 'updated' => '2007-03-18 12:28:31'
  1396. )));
  1397. $this->assertEquals($expected, $result);
  1398. $data = array(
  1399. 'Article' => array(
  1400. 'id' => 10,
  1401. 'user_id' => '2',
  1402. 'title' => 'New Article With Tags and fieldList',
  1403. 'body' => 'New Article Body with Tags and fieldList',
  1404. 'created' => '2007-03-18 14:55:23',
  1405. 'updated' => '2007-03-18 14:57:31'
  1406. ),
  1407. 'Tag' => array(
  1408. 'Tag' => array(1, 2, 3)
  1409. )
  1410. );
  1411. $result = $TestModel->create()
  1412. && $TestModel->save($data, true, array('user_id', 'title', 'published'));
  1413. $this->assertFalse(empty($result));
  1414. $TestModel->unbindModel(array(
  1415. 'belongsTo' => array('User'),
  1416. 'hasMany' => array('Comment')
  1417. ));
  1418. $result = $TestModel->read();
  1419. $expected = array(
  1420. 'Article' => array(
  1421. 'id' => 4,
  1422. 'user_id' => 2,
  1423. 'title' => 'New Article With Tags and fieldList',
  1424. 'body' => '',
  1425. 'published' => 'N',
  1426. 'created' => '',
  1427. 'updated' => ''
  1428. ),
  1429. 'Tag' => array(
  1430. 0 => array(
  1431. 'id' => 1,
  1432. 'tag' => 'tag1',
  1433. 'created' => '2007-03-18 12:22:23',
  1434. 'updated' => '2007-03-18 12:24:31'
  1435. ),
  1436. 1 => array(
  1437. 'id' => 2,
  1438. 'tag' => 'tag2',
  1439. 'created' => '2007-03-18 12:24:23',
  1440. 'updated' => '2007-03-18 12:26:31'
  1441. ),
  1442. 2 => array(
  1443. 'id' => 3,
  1444. 'tag' => 'tag3',
  1445. 'created' => '2007-03-18 12:26:23',
  1446. 'updated' => '2007-03-18 12:28:31'
  1447. )));
  1448. $this->assertEquals($expected, $result);
  1449. $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
  1450. $TestModel = new JoinA();
  1451. $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
  1452. $data = array(
  1453. 'JoinA' => array(
  1454. 'id' => 1,
  1455. 'name' => 'Join A 1',
  1456. 'body' => 'Join A 1 Body',
  1457. ),
  1458. 'JoinC' => array(
  1459. 'JoinC' => array(
  1460. array('join_c_id' => 2, 'other' => 'new record'),
  1461. array('join_c_id' => 3, 'other' => 'new record')
  1462. )
  1463. )
  1464. );
  1465. $TestModel->save($data);
  1466. $result = $TestModel->read(null, 1);
  1467. $expected = array(4, 5);
  1468. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
  1469. $expected = array('new record', 'new record');
  1470. $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
  1471. }
  1472. /**
  1473. * testSaveHabtmNoPrimaryData method
  1474. *
  1475. * @return void
  1476. */
  1477. public function testSaveHabtmNoPrimaryData() {
  1478. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  1479. $TestModel = new Article();
  1480. $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
  1481. $result = $TestModel->findById(2);
  1482. $expected = array(
  1483. 'Article' => array(
  1484. 'id' => '2',
  1485. 'user_id' => '3',
  1486. 'title' => 'Second Article',
  1487. 'body' => 'Second Article Body',
  1488. 'published' => 'Y',
  1489. 'created' => '2007-03-18 10:41:23',
  1490. 'updated' => '2007-03-18 10:43:31'
  1491. ),
  1492. 'Tag' => array(
  1493. array(
  1494. 'id' => '1',
  1495. 'tag' => 'tag1',
  1496. 'created' => '2007-03-18 12:22:23',
  1497. 'updated' => '2007-03-18 12:24:31'
  1498. ),
  1499. array(
  1500. 'id' => '3',
  1501. 'tag' => 'tag3',
  1502. 'created' => '2007-03-18 12:26:23',
  1503. 'updated' => '2007-03-18 12:28:31'
  1504. )
  1505. )
  1506. );
  1507. $this->assertEquals($expected, $result);
  1508. $TestModel->id = 2;
  1509. $data = array('Tag' => array('Tag' => array(2)));
  1510. $result = $TestModel->save($data);
  1511. $this->assertEquals($data['Tag'], $result['Tag']);
  1512. $result = $TestModel->findById(2);
  1513. $expected = array(
  1514. 'Article' => array(
  1515. 'id' => '2',
  1516. 'user_id' => '3',
  1517. 'title' => 'Second Article',
  1518. 'body' => 'Second Article Body',
  1519. 'published' => 'Y',
  1520. 'created' => '2007-03-18 10:41:23',
  1521. 'updated' => self::date()
  1522. ),
  1523. 'Tag' => array(
  1524. array(
  1525. 'id' => '2',
  1526. 'tag' => 'tag2',
  1527. 'created' => '2007-03-18 12:24:23',
  1528. 'updated' => '2007-03-18 12:26:31'
  1529. )
  1530. )
  1531. );
  1532. $this->assertEquals($expected, $result);
  1533. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
  1534. $TestModel = new Portfolio();
  1535. $result = $TestModel->findById(2);
  1536. $expected = array(
  1537. 'Portfolio' => array(
  1538. 'id' => 2,
  1539. 'seller_id' => 1,
  1540. 'name' => 'Portfolio 2'
  1541. ),
  1542. 'Item' => array(
  1543. array(
  1544. 'id' => 2,
  1545. 'syfile_id' => 2,
  1546. 'published' => '',
  1547. 'name' => 'Item 2',
  1548. 'ItemsPortfolio' => array(
  1549. 'id' => 2,
  1550. 'item_id' => 2,
  1551. 'portfolio_id' => 2
  1552. )
  1553. ),
  1554. array(
  1555. 'id' => 6,
  1556. 'syfile_id' => 6,
  1557. 'published' => '',
  1558. 'name' => 'Item 6',
  1559. 'ItemsPortfolio' => array(
  1560. 'id' => 6,
  1561. 'item_id' => 6,
  1562. 'portfolio_id' => 2
  1563. )
  1564. )
  1565. )
  1566. );
  1567. $this->assertEquals($expected, $result);
  1568. $data = array('Item' => array('Item' => array(1, 2)));
  1569. $TestModel->id = 2;
  1570. $TestModel->save($data);
  1571. $result = $TestModel->findById(2);
  1572. $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
  1573. $expected = array(
  1574. 'Portfolio' => array(
  1575. 'id' => 2,
  1576. 'seller_id' => 1,
  1577. 'name' => 'Portfolio 2'
  1578. ),
  1579. 'Item' => array(
  1580. array(
  1581. 'id' => 1,
  1582. 'syfile_id' => 1,
  1583. 'published' => '',
  1584. 'name' => 'Item 1',
  1585. 'ItemsPortfolio' => array(
  1586. 'id' => 7,
  1587. 'item_id' => 1,
  1588. 'portfolio_id' => 2
  1589. )
  1590. ),
  1591. array(
  1592. 'id' => 2,
  1593. 'syfile_id' => 2,
  1594. 'published' => '',
  1595. 'name' => 'Item 2',
  1596. 'ItemsPortfolio' => array(
  1597. 'id' => 8,
  1598. 'item_id' => 2,
  1599. 'portfolio_id' => 2
  1600. )
  1601. )
  1602. )
  1603. );
  1604. $this->assertEquals($expected, $result);
  1605. }
  1606. /**
  1607. * testSaveHabtmCustomKeys method
  1608. *
  1609. * @return void
  1610. */
  1611. public function testSaveHabtmCustomKeys() {
  1612. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  1613. $Story = new Story();
  1614. $data = array(
  1615. 'Story' => array('story' => '1'),
  1616. 'Tag' => array(
  1617. 'Tag' => array(2, 3)
  1618. ));
  1619. $result = $Story->set($data);
  1620. $this->assertFalse(empty($result));
  1621. $result = $Story->save();
  1622. $this->assertFalse(empty($result));
  1623. $result = $Story->find('all', array('order' => array('Story.story')));
  1624. $expected = array(
  1625. array(
  1626. 'Story' => array(
  1627. 'story' => 1,
  1628. 'title' => 'First Story'
  1629. ),
  1630. 'Tag' => array(
  1631. array(
  1632. 'id' => 2,
  1633. 'tag' => 'tag2',
  1634. 'created' => '2007-03-18 12:24:23',
  1635. 'updated' => '2007-03-18 12:26:31'
  1636. ),
  1637. array(
  1638. 'id' => 3,
  1639. 'tag' => 'tag3',
  1640. 'created' => '2007-03-18 12:26:23',
  1641. 'updated' => '2007-03-18 12:28:31'
  1642. ))),
  1643. array(
  1644. 'Story' => array(
  1645. 'story' => 2,
  1646. 'title' => 'Secon…

Large files files are truncated, but you can click here to view the full file