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

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/model/model_write.test.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 4048 lines | 3230 code | 385 blank | 433 comment | 28 complexity | 8124cf6b322f579dde347b398d42a55f MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1

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

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

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