PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://bitbucket.org/ds10/agent-j
PHP | 6833 lines | 5566 code | 648 blank | 619 comment | 27 complexity | 86f474b276862c4eae1dd1c0851cd0a3 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-3.0, GPL-2.0, LGPL-2.1, MIT

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

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

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