PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://bitbucket.org/tlorens/cakefoundation
PHP | 6876 lines | 5611 code | 646 blank | 619 comment | 27 complexity | 940f7f64e94dd8c75f79a8da9d928077 MD5 | raw file

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

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