PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 5499 lines | 4459 code | 529 blank | 511 comment | 27 complexity | fc6cf55090f458839ee9496a05a687bf MD5 | raw file

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

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