PageRenderTime 46ms CodeModel.GetById 27ms 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
  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. 'title' => 'First Article'
  1737. )),
  1738. array('Article' => array(
  1739. 'id' => 2,
  1740. 'title' => 'Second Article'
  1741. )),
  1742. array('Article' => array(
  1743. 'id' => 3,
  1744. 'title' => 'Third Article'
  1745. ))));
  1746. $this->assertEqual($comments, array(
  1747. array('Comment' => array(
  1748. 'id' => 1,
  1749. 'article_id' => 1,
  1750. 'user_id' => 2,
  1751. 'comment' => 'First Comment for First Article',
  1752. 'published' => 'Y'
  1753. )),
  1754. array('Comment' => array(
  1755. 'id' => 2,
  1756. 'article_id' => 1,
  1757. 'user_id' => 4,
  1758. 'comment' => 'Second Comment for First Article',
  1759. 'published' => 'Y'
  1760. )),
  1761. array('Comment' => array(
  1762. 'id' => 3,
  1763. 'article_id' => 1,
  1764. 'user_id' => 1,
  1765. 'comment' => 'Third Comment for First Article',
  1766. 'published' => 'Y'
  1767. )),
  1768. array('Comment' => array(
  1769. 'id' => 4,
  1770. 'article_id' => 1,
  1771. 'user_id' => 1,
  1772. 'comment' => 'Fourth Comment for First Article',
  1773. 'published' => 'N'
  1774. )),
  1775. array('Comment' => array(
  1776. 'id' => 5,
  1777. 'article_id' => 2,
  1778. 'user_id' => 1,
  1779. 'comment' => 'First Comment for Second Article',
  1780. 'published' => 'Y'
  1781. )),
  1782. array('Comment' => array(
  1783. 'id' => 6,
  1784. 'article_id' => 2,
  1785. 'user_id' => 2,
  1786. 'comment' => 'Second Comment for Second Article',
  1787. 'published' => 'Y'
  1788. ))));
  1789. $data = array(
  1790. 'Comment' => array(
  1791. 'article_id' => 2,
  1792. 'user_id' => 4,
  1793. 'comment' => 'Brand New Comment',
  1794. 'published' => 'N'
  1795. ),
  1796. 'Article' => array(
  1797. 'id' => 2,
  1798. 'title' => 'Second Article Modified'
  1799. ));
  1800. $result = $Comment->create($data);
  1801. $this->assertTrue($result);
  1802. $result = $Comment->save();
  1803. $this->assertTrue($result);
  1804. $articles = $Article->find('all', array(
  1805. 'fields' => array('id','title'),
  1806. 'recursive' => -1
  1807. ));
  1808. $comments = $Comment->find('all', array(
  1809. 'fields' => array('id','article_id','user_id','comment','published'),
  1810. 'recursive' => -1
  1811. ));
  1812. $this->assertEqual($articles, array(
  1813. array('Article' => array(
  1814. 'id' => 1,
  1815. 'title' => 'First Article'
  1816. )),
  1817. array('Article' => array(
  1818. 'id' => 2,
  1819. 'title' => 'Second Article'
  1820. )),
  1821. array('Article' => array(
  1822. 'id' => 3,
  1823. 'title' => 'Third Article'
  1824. ))));
  1825. $this->assertEqual($comments, array(
  1826. array('Comment' => array(
  1827. 'id' => 1,
  1828. 'article_id' => 1,
  1829. 'user_id' => 2,
  1830. 'comment' => 'First Comment for First Article',
  1831. 'published' => 'Y'
  1832. )),
  1833. array('Comment' => array(
  1834. 'id' => 2,
  1835. 'article_id' => 1,
  1836. 'user_id' => 4,
  1837. 'comment' => 'Second Comment for First Article',
  1838. 'published' => 'Y'
  1839. )),
  1840. array('Comment' => array(
  1841. 'id' => 3,
  1842. 'article_id' => 1,
  1843. 'user_id' => 1,
  1844. 'comment' => 'Third Comment for First Article',
  1845. 'published' => 'Y'
  1846. )),
  1847. array('Comment' => array(
  1848. 'id' => 4,
  1849. 'article_id' => 1,
  1850. 'user_id' => 1,
  1851. 'comment' => 'Fourth Comment for First Article',
  1852. 'published' => 'N'
  1853. )),
  1854. array('Comment' => array(
  1855. 'id' => 5,
  1856. 'article_id' => 2,
  1857. 'user_id' => 1,
  1858. 'comment' => 'First Comment for Second Article',
  1859. 'published' => 'Y'
  1860. )),
  1861. array('Comment' => array(
  1862. 'id' => 6,
  1863. 'article_id' => 2,
  1864. 'user_id' => 2, 'comment' =>
  1865. 'Second Comment for Second Article',
  1866. 'published' => 'Y'
  1867. )),
  1868. array('Comment' => array(
  1869. 'id' => 7,
  1870. 'article_id' => 2,
  1871. 'user_id' => 4,
  1872. 'comment' => 'Brand New Comment',
  1873. 'published' => 'N'
  1874. ))));
  1875. }
  1876. /**
  1877. * testCreationWithMultipleDataSameModel method
  1878. *
  1879. * @access public
  1880. * @return void
  1881. */
  1882. function testCreationWithMultipleDataSameModel() {
  1883. $this->loadFixtures('Article');
  1884. $Article =& new Article();
  1885. $SecondaryArticle =& new Article();
  1886. $result = $Article->field('title', array('id' => 1));
  1887. $this->assertEqual($result, 'First Article');
  1888. $data = array(
  1889. 'Article' => array(
  1890. 'user_id' => 2,
  1891. 'title' => 'Brand New Article',
  1892. 'body' => 'Brand New Article Body',
  1893. 'published' => 'Y'
  1894. ),
  1895. 'SecondaryArticle' => array(
  1896. 'id' => 1
  1897. ));
  1898. $Article->create();
  1899. $result = $Article->save($data);
  1900. $this->assertTrue($result);
  1901. $result = $Article->getInsertID();
  1902. $this->assertTrue(!empty($result));
  1903. $result = $Article->field('title', array('id' => 1));
  1904. $this->assertEqual($result, 'First Article');
  1905. $articles = $Article->find('all', array(
  1906. 'fields' => array('id','title'),
  1907. 'recursive' => -1
  1908. ));
  1909. $this->assertEqual($articles, array(
  1910. array('Article' => array(
  1911. 'id' => 1,
  1912. 'title' => 'First Article'
  1913. )),
  1914. array('Article' => array(
  1915. 'id' => 2,
  1916. 'title' => 'Second Article'
  1917. )),
  1918. array('Article' => array(
  1919. 'id' => 3,
  1920. 'title' => 'Third Article'
  1921. )),
  1922. array('Article' => array(
  1923. 'id' => 4,
  1924. 'title' => 'Brand New Article'
  1925. ))));
  1926. }
  1927. /**
  1928. * testCreationWithMultipleDataSameModelManualInstances method
  1929. *
  1930. * @access public
  1931. * @return void
  1932. */
  1933. function testCreationWithMultipleDataSameModelManualInstances() {
  1934. $this->loadFixtures('PrimaryModel');
  1935. $Primary =& new PrimaryModel();
  1936. $Secondary =& new PrimaryModel();
  1937. $result = $Primary->field('primary_name', array('id' => 1));
  1938. $this->assertEqual($result, 'Primary Name Existing');
  1939. $data = array(
  1940. 'PrimaryModel' => array(
  1941. 'primary_name' => 'Primary Name New'
  1942. ),
  1943. 'SecondaryModel' => array(
  1944. 'id' => array(1)
  1945. ));
  1946. $Primary->create();
  1947. $result = $Primary->save($data);
  1948. $this->assertTrue($result);
  1949. $result = $Primary->field('primary_name', array('id' => 1));
  1950. $this->assertEqual($result, 'Primary Name Existing');
  1951. $result = $Primary->getInsertID();
  1952. $this->assertTrue(!empty($result));
  1953. $result = $Primary->field('primary_name', array('id' => $result));
  1954. $this->assertEqual($result, 'Primary Name New');
  1955. $result = $Primary->find('count');
  1956. $this->assertEqual($result, 2);
  1957. }
  1958. /**
  1959. * testRecordExists method
  1960. *
  1961. * @access public
  1962. * @return void
  1963. */
  1964. function testRecordExists() {
  1965. $this->loadFixtures('User');
  1966. $TestModel =& new User();
  1967. $this->assertFalse($TestModel->exists());
  1968. $TestModel->read(null, 1);
  1969. $this->assertTrue($TestModel->exists());
  1970. $TestModel->create();
  1971. $this->assertFalse($TestModel->exists());
  1972. $TestModel->id = 4;
  1973. $this->assertTrue($TestModel->exists());
  1974. $TestModel =& new TheVoid();
  1975. $this->assertFalse($TestModel->exists());
  1976. $TestModel->id = 5;
  1977. $this->expectError();
  1978. ob_start();
  1979. $this->assertFalse($TestModel->exists());
  1980. $output = ob_get_clean();
  1981. }
  1982. /**
  1983. * testUpdateExisting method
  1984. *
  1985. * @access public
  1986. * @return void
  1987. */
  1988. function testUpdateExisting() {
  1989. $this->loadFixtures('User', 'Article', 'Comment');
  1990. $TestModel =& new User();
  1991. $TestModel->create();
  1992. $TestModel->save(array(
  1993. 'User' => array(
  1994. 'user' => 'some user',
  1995. 'password' => 'some password'
  1996. )));
  1997. $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5));
  1998. $id = $TestModel->id;
  1999. $TestModel->save(array(
  2000. 'User' => array(
  2001. 'user' => 'updated user'
  2002. )));
  2003. $this->assertEqual($TestModel->id, $id);
  2004. $result = $TestModel->findById($id);
  2005. $this->assertEqual($result['User']['user'], 'updated user');
  2006. $this->assertEqual($result['User']['password'], 'some password');
  2007. $Article =& new Article();
  2008. $Comment =& new Comment();
  2009. $data = array(
  2010. 'Comment' => array(
  2011. 'id' => 1,
  2012. 'comment' => 'First Comment for First Article'
  2013. ),
  2014. 'Article' => array(
  2015. 'id' => 2,
  2016. 'title' => 'Second Article'
  2017. ));
  2018. $result = $Article->save($data);
  2019. $this->assertTrue($result);
  2020. $result = $Comment->save($data);
  2021. $this->assertTrue($result);
  2022. }
  2023. /**
  2024. * test updating records and saving blank values.
  2025. *
  2026. * @return void
  2027. */
  2028. function testUpdateSavingBlankValues() {
  2029. $this->loadFixtures('Article');
  2030. $Article =& new Article();
  2031. $Article->validate = array();
  2032. $Article->create();
  2033. $result = $Article->save(array(
  2034. 'id' => 1,
  2035. 'title' => '',
  2036. 'body' => ''
  2037. ));
  2038. $this->assertTrue($result);
  2039. $result = $Article->find('first', array('conditions' => array('Article.id' => 1)));
  2040. $this->assertEqual('', $result['Article']['title'], 'Title is not blank');
  2041. $this->assertEqual('', $result['Article']['body'], 'Body is not blank');
  2042. }
  2043. /**
  2044. * testUpdateMultiple method
  2045. *
  2046. * @access public
  2047. * @return void
  2048. */
  2049. function testUpdateMultiple() {
  2050. $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
  2051. $TestModel =& new Comment();
  2052. $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
  2053. $expected = array('2', '4', '1', '1', '1', '2');
  2054. $this->assertEqual($result, $expected);
  2055. $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
  2056. $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
  2057. $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
  2058. $this->assertEqual($result, $expected);
  2059. $result = $TestModel->updateAll(
  2060. array('Comment.comment' => "'Updated today'"),
  2061. array('Comment.user_id' => 5)
  2062. );
  2063. $this->assertTrue($result);
  2064. $result = Set::extract(
  2065. $TestModel->find('all', array(
  2066. 'conditions' => array(
  2067. 'Comment.user_id' => 5
  2068. ))),
  2069. '{n}.Comment.comment'
  2070. );
  2071. $expected = array_fill(0, 2, 'Updated today');
  2072. $this->assertEqual($result, $expected);
  2073. }
  2074. /**
  2075. * testHabtmUuidWithUuidId method
  2076. *
  2077. * @access public
  2078. * @return void
  2079. */
  2080. function testHabtmUuidWithUuidId() {
  2081. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio');
  2082. $TestModel =& new Uuidportfolio();
  2083. $data = array('Uuidportfolio' => array('name' => 'Portfolio 3'));
  2084. $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569');
  2085. $TestModel->create($data);
  2086. $TestModel->save();
  2087. $id = $TestModel->id;
  2088. $result = $TestModel->read(null, $id);
  2089. $this->assertEqual(1, count($result['Uuiditem']));
  2090. $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
  2091. }
  2092. /**
  2093. * test HABTM saving when join table has no primary key and only 2 columns.
  2094. *
  2095. * @return void
  2096. */
  2097. function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
  2098. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2099. $Fruit =& new Fruit();
  2100. $data = array(
  2101. 'Fruit' => array(
  2102. 'color' => 'Red',
  2103. 'shape' => 'Heart-shaped',
  2104. 'taste' => 'sweet',
  2105. 'name' => 'Strawberry',
  2106. ),
  2107. 'UuidTag' => array(
  2108. 'UuidTag' => array(
  2109. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2110. )
  2111. )
  2112. );
  2113. $this->assertTrue($Fruit->save($data));
  2114. }
  2115. /**
  2116. * test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
  2117. *
  2118. * @return void
  2119. */
  2120. function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
  2121. $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
  2122. $Fruit =& new FruitNoWith();
  2123. $data = array(
  2124. 'Fruit' => array(
  2125. 'color' => 'Red',
  2126. 'shape' => 'Heart-shaped',
  2127. 'taste' => 'sweet',
  2128. 'name' => 'Strawberry',
  2129. ),
  2130. 'UuidTag' => array(
  2131. 'UuidTag' => array(
  2132. '481fc6d0-b920-43e0-e50f-6d1740cf8569'
  2133. )
  2134. )
  2135. );
  2136. $this->assertTrue($Fruit->save($data));
  2137. }
  2138. /**
  2139. * testHabtmUuidWithNumericId method
  2140. *
  2141. * @access public
  2142. * @return void
  2143. */
  2144. function testHabtmUuidWithNumericId() {
  2145. $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
  2146. $TestModel =& new Uuiditem();
  2147. $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
  2148. $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
  2149. $TestModel->create($data);
  2150. $TestModel->save();
  2151. $id = $TestModel->id;
  2152. $result = $TestModel->read(null, $id);
  2153. $this->assertEqual(1, count($result['Uuidportfolio']));
  2154. }
  2155. /**
  2156. * testSaveMultipleHabtm method
  2157. *
  2158. * @access public
  2159. * @return void
  2160. */
  2161. function testSaveMultipleHabtm() {
  2162. $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
  2163. $TestModel = new JoinA();
  2164. $result = $TestModel->findById(1);
  2165. $expected = array(
  2166. 'JoinA' => array(
  2167. 'id' => 1,
  2168. 'name' => 'Join A 1',
  2169. 'body' => 'Join A 1 Body',
  2170. 'created' => '2008-01-03 10:54:23',
  2171. 'updated' => '2008-01-03 10:54:23'
  2172. ),
  2173. 'JoinB' => array(
  2174. 0 => array(
  2175. 'id' => 2,
  2176. 'name' => 'Join B 2',
  2177. 'created' => '2008-01-03 10:55:02',
  2178. 'updated' => '2008-01-03 10:55:02',
  2179. 'JoinAsJoinB' => array(
  2180. 'id' => 1,
  2181. 'join_a_id' => 1,
  2182. 'join_b_id' => 2,
  2183. 'other' => 'Data for Join A 1 Join B 2',
  2184. 'created' => '2008-01-03 10:56:33',
  2185. 'updated' => '2008-01-03 10:56:33'
  2186. ))),
  2187. 'JoinC' => array(
  2188. 0 => array(
  2189. 'id' => 2,
  2190. 'name' => 'Join C 2',
  2191. 'created' => '2008-01-03 10:56:12',
  2192. 'updated' => '2008-01-03 10:56:12',
  2193. 'JoinAsJoinC' => array(
  2194. 'id' => 1,
  2195. 'join_a_id' => 1,
  2196. 'join_c_id' => 2,
  2197. 'other' => 'Data for Join A 1 Join C 2',
  2198. 'created' => '2008-01-03 10:57:22',
  2199. 'updated' => '2008-01-03 10:57:22'
  2200. ))));
  2201. $this->assertEqual($result, $expected);
  2202. $ts = date('Y-m-d H:i:s');
  2203. $TestModel->id = 1;
  2204. $data = array(
  2205. 'JoinA' => array(
  2206. 'id' => '1',
  2207. 'name' => 'New name for Join A 1',
  2208. 'updated' => $ts
  2209. ),
  2210. 'JoinB' => array(
  2211. array(
  2212. 'id' => 1,
  2213. 'join_b_id' => 2,
  2214. 'other' => 'New data for Join A 1 Join B 2',
  2215. 'created' => $ts,
  2216. 'updated' => $ts
  2217. )),
  2218. 'JoinC' => array(
  2219. array(
  2220. 'id' => 1,
  2221. 'join_c_id' => 2,
  2222. 'other' => 'New data for Join A 1 Join C 2',
  2223. 'created' => $ts,
  2224. 'updated' => $ts
  2225. )));
  2226. $TestModel->set($data);
  2227. $TestModel->save();
  2228. $result = $TestModel->findById(1);
  2229. $expected = array(
  2230. 'JoinA' => array(
  2231. 'id' => 1,
  2232. 'name' => 'New name for Join A 1',
  2233. 'body' => 'Join A 1 Body',
  2234. 'created' => '2008-01-03 10:54:23',
  2235. 'updated' => $ts
  2236. ),
  2237. 'JoinB' => array(
  2238. 0 => array(
  2239. 'id' => 2,
  2240. 'name' => 'Join B 2',
  2241. 'created' => '2008-01-03 10:55:02',
  2242. 'updated' => '2008-01-03 10:55:02',
  2243. 'JoinAsJoinB' => array(
  2244. 'id' => 1,
  2245. 'join_a_id' => 1,
  2246. 'join_b_id' => 2,
  2247. 'other' => 'New data for Join A 1 Join B 2',
  2248. 'created' => $ts,
  2249. 'updated' => $ts
  2250. ))),
  2251. 'JoinC' => array(
  2252. 0 => array(
  2253. 'id' => 2,
  2254. 'name' => 'Join C 2',
  2255. 'created' => '2008-01-03 10:56:12',
  2256. 'updated' => '2008-01-03 10:56:12',
  2257. 'JoinAsJoinC' => array(
  2258. 'id' => 1,
  2259. 'join_a_id' => 1,
  2260. 'join_c_id' => 2,
  2261. 'other' => 'New data for Join A 1 Join C 2',
  2262. 'created' => $ts,
  2263. 'updated' => $ts
  2264. ))));
  2265. $this->assertEqual($result, $expected);
  2266. }
  2267. /**
  2268. * testSaveAll method
  2269. *
  2270. * @access public
  2271. * @return void
  2272. */
  2273. function testSaveAll() {
  2274. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2275. $TestModel =& new Post();
  2276. $result = $TestModel->find('all');
  2277. $this->assertEqual(count($result), 3);
  2278. $this->assertFalse(isset($result[3]));
  2279. $ts = date('Y-m-d H:i:s');
  2280. $TestModel->saveAll(array(
  2281. 'Post' => array(
  2282. 'title' => 'Post with Author',
  2283. 'body' => 'This post will be saved with an author'
  2284. ),
  2285. 'Author' => array(
  2286. 'user' => 'bob',
  2287. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
  2288. )));
  2289. $result = $TestModel->find('all');
  2290. $expected = array(
  2291. 'Post' => array(
  2292. 'id' => '4',
  2293. 'author_id' => '5',
  2294. 'title' => 'Post with Author',
  2295. 'body' => 'This post will be saved with an author',
  2296. 'published' => 'N',
  2297. 'created' => $ts,
  2298. 'updated' => $ts
  2299. ),
  2300. 'Author' => array(
  2301. 'id' => '5',
  2302. 'user' => 'bob',
  2303. 'password' => '5f4dcc3b5aa765d61d8327deb882cf90',
  2304. 'created' => $ts,
  2305. 'updated' => $ts,
  2306. 'test' => 'working'
  2307. ));
  2308. $this->assertEqual($result[3], $expected);
  2309. $this->assertEqual(count($result), 4);
  2310. $TestModel->deleteAll(true);
  2311. $this->assertEqual($TestModel->find('all'), array());
  2312. // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
  2313. $this->db->truncate($TestModel);
  2314. $ts = date('Y-m-d H:i:s');
  2315. $TestModel->saveAll(array(
  2316. array(
  2317. 'title' => 'Multi-record post 1',
  2318. 'body' => 'First multi-record post',
  2319. 'author_id' => 2
  2320. ),
  2321. array(
  2322. 'title' => 'Multi-record post 2',
  2323. 'body' => 'Second multi-record post',
  2324. 'author_id' => 2
  2325. )));
  2326. $result = $TestModel->find('all', array(
  2327. 'recursive' => -1,
  2328. 'order' => 'Post.id ASC'
  2329. ));
  2330. $expected = array(
  2331. array(
  2332. 'Post' => array(
  2333. 'id' => '1',
  2334. 'author_id' => '2',
  2335. 'title' => 'Multi-record post 1',
  2336. 'body' => 'First multi-record post',
  2337. 'published' => 'N',
  2338. 'created' => $ts,
  2339. 'updated' => $ts
  2340. )),
  2341. array(
  2342. 'Post' => array(
  2343. 'id' => '2',
  2344. 'author_id' => '2',
  2345. 'title' => 'Multi-record post 2',
  2346. 'body' => 'Second multi-record post',
  2347. 'published' => 'N',
  2348. 'created' => $ts,
  2349. 'updated' => $ts
  2350. )));
  2351. $this->assertEqual($result, $expected);
  2352. $TestModel =& new Comment();
  2353. $ts = date('Y-m-d H:i:s');
  2354. $result = $TestModel->saveAll(array(
  2355. 'Comment' => array(
  2356. 'article_id' => 2,
  2357. 'user_id' => 2,
  2358. 'comment' => 'New comment with attachment',
  2359. 'published' => 'Y'
  2360. ),
  2361. 'Attachment' => array(
  2362. 'attachment' => 'some_file.tgz'
  2363. )));
  2364. $this->assertTrue($result);
  2365. $result = $TestModel->find('all');
  2366. $expected = array(
  2367. 'id' => '7',
  2368. 'article_id' => '2',
  2369. 'user_id' => '2',
  2370. 'comment' => 'New comment with attachment',
  2371. 'published' => 'Y',
  2372. 'created' => $ts,
  2373. 'updated' => $ts
  2374. );
  2375. $this->assertEqual($result[6]['Comment'], $expected);
  2376. $expected = array(
  2377. 'id' => '7',
  2378. 'article_id' => '2',
  2379. 'user_id' => '2',
  2380. 'comment' => 'New comment with attachment',
  2381. 'published' => 'Y',
  2382. 'created' => $ts,
  2383. 'updated' => $ts
  2384. );
  2385. $this->assertEqual($result[6]['Comment'], $expected);
  2386. $expected = array(
  2387. 'id' => '2',
  2388. 'comment_id' => '7',
  2389. 'attachment' => 'some_file.tgz',
  2390. 'created' => $ts,
  2391. 'updated' => $ts
  2392. );
  2393. $this->assertEqual($result[6]['Attachment'], $expected);
  2394. }
  2395. /**
  2396. * Test SaveAll with Habtm relations
  2397. *
  2398. * @access public
  2399. * @return void
  2400. */
  2401. function testSaveAllHabtm() {
  2402. $this->loadFixtures('Article', 'Tag', 'Comment', 'User');
  2403. $data = array(
  2404. 'Article' => array(
  2405. 'user_id' => 1,
  2406. 'title' => 'Article Has and belongs to Many Tags'
  2407. ),
  2408. 'Tag' => array(
  2409. 'Tag' => array(1, 2)
  2410. ),
  2411. 'Comment' => array(
  2412. array(
  2413. 'comment' => 'Article comment',
  2414. 'user_id' => 1
  2415. )));
  2416. $Article =& new Article();
  2417. $result = $Article->saveAll($data);
  2418. $this->assertTrue($result);
  2419. $result = $Article->read();
  2420. $this->assertEqual(count($result['Tag']), 2);
  2421. $this->assertEqual($result['Tag'][0]['tag'], 'tag1');
  2422. $this->assertEqual(count($result['Comment']), 1);
  2423. $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1);
  2424. }
  2425. /**
  2426. * Test SaveAll with Habtm relations and extra join table fields
  2427. *
  2428. * @access public
  2429. * @return void
  2430. */
  2431. function testSaveAllHabtmWithExtraJoinTableFields() {
  2432. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  2433. $data = array(
  2434. 'Something' => array(
  2435. 'id' => 4,
  2436. 'title' => 'Extra Fields',
  2437. 'body' => 'Extra Fields Body',
  2438. 'published' => '1'
  2439. ),
  2440. 'SomethingElse' => array(
  2441. array('something_else_id' => 1, 'doomed' => '1'),
  2442. array('something_else_id' => 2, 'doomed' => '0'),
  2443. array('something_else_id' => 3, 'doomed' => '1')
  2444. )
  2445. );
  2446. $Something =& new Something();
  2447. $result = $Something->saveAll($data);
  2448. $this->assertTrue($result);
  2449. $result = $Something->read();
  2450. $this->assertEqual(count($result['SomethingElse']), 3);
  2451. $this->assertTrue(Set::matches('/Something[id=4]', $result));
  2452. $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result));
  2453. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result));
  2454. $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result));
  2455. $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result));
  2456. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result));
  2457. $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result));
  2458. $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result));
  2459. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result));
  2460. $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result));
  2461. }
  2462. /**
  2463. * testSaveAllHasOne method
  2464. *
  2465. * @access public
  2466. * @return void
  2467. */
  2468. function testSaveAllHasOne() {
  2469. $model = new Comment();
  2470. $model->deleteAll(true);
  2471. $this->assertEqual($model->find('all'), array());
  2472. $model->Attachment->deleteAll(true);
  2473. $this->assertEqual($model->Attachment->find('all'), array());
  2474. $this->assertTrue($model->saveAll(array(
  2475. 'Comment' => array(
  2476. 'comment' => 'Comment with attachment',
  2477. 'article_id' => 1,
  2478. 'user_id' => 1
  2479. ),
  2480. 'Attachment' => array(
  2481. 'attachment' => 'some_file.zip'
  2482. ))));
  2483. $result = $model->find('all', array('fields' => array(
  2484. 'Comment.id', 'Comment.comment', 'Attachment.id',
  2485. 'Attachment.comment_id', 'Attachment.attachment'
  2486. )));
  2487. $expected = array(array(
  2488. 'Comment' => array(
  2489. 'id' => '1',
  2490. 'comment' => 'Comment with attachment'
  2491. ),
  2492. 'Attachment' => array(
  2493. 'id' => '1',
  2494. 'comment_id' => '1',
  2495. 'attachment' => 'some_file.zip'
  2496. )));
  2497. $this->assertEqual($result, $expected);
  2498. $model->Attachment->bindModel(array('belongsTo' => array('Comment')), false);
  2499. $data = array(
  2500. 'Comment' => array(
  2501. 'comment' => 'Comment with attachment',
  2502. 'article_id' => 1,
  2503. 'user_id' => 1
  2504. ),
  2505. 'Attachment' => array(
  2506. 'attachment' => 'some_file.zip'
  2507. ));
  2508. $this->assertTrue($model->saveAll($data, array('validate' => 'first')));
  2509. }
  2510. /**
  2511. * testSaveAllBelongsTo method
  2512. *
  2513. * @access public
  2514. * @return void
  2515. */
  2516. function testSaveAllBelongsTo() {
  2517. $model = new Comment();
  2518. $model->deleteAll(true);
  2519. $this->assertEqual($model->find('all'), array());
  2520. $model->Article->deleteAll(true);
  2521. $this->assertEqual($model->Article->find('all'), array());
  2522. $this->assertTrue($model->saveAll(array(
  2523. 'Comment' => array(
  2524. 'comment' => 'Article comment',
  2525. 'article_id' => 1,
  2526. 'user_id' => 1
  2527. ),
  2528. 'Article' => array(
  2529. 'title' => 'Model Associations 101',
  2530. 'user_id' => 1
  2531. ))));
  2532. $result = $model->find('all', array('fields' => array(
  2533. 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
  2534. )));
  2535. $expected = array(array(
  2536. 'Comment' => array(
  2537. 'id' => '1',
  2538. 'article_id' => '1',
  2539. 'comment' => 'Article comment'
  2540. ),
  2541. 'Article' => array(
  2542. 'id' => '1',
  2543. 'title' => 'Model Associations 101'
  2544. )));
  2545. $this->assertEqual($result, $expected);
  2546. }
  2547. /**
  2548. * testSaveAllHasOneValidation method
  2549. *
  2550. * @access public
  2551. * @return void
  2552. */
  2553. function testSaveAllHasOneValidation() {
  2554. $model = new Comment();
  2555. $model->deleteAll(true);
  2556. $this->assertEqual($model->find('all'), array());
  2557. $model->Attachment->deleteAll(true);
  2558. $this->assertEqual($model->Attachment->find('all'), array());
  2559. $model->validate = array('comment' => 'notEmpty');
  2560. $model->Attachment->validate = array('attachment' => 'notEmpty');
  2561. $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
  2562. $this->assertFalse($model->saveAll(
  2563. array(
  2564. 'Comment' => array(
  2565. 'comment' => '',
  2566. 'article_id' => 1,
  2567. 'user_id' => 1
  2568. ),
  2569. 'Attachment' => array('attachment' => '')
  2570. ),
  2571. array('validate' => 'first')
  2572. ));
  2573. $expected = array(
  2574. 'Comment' => array('comment' => 'This field cannot be left blank'),
  2575. 'Attachment' => array('attachment' => 'This field cannot be left blank')
  2576. );
  2577. $this->assertEqual($model->validationErrors, $expected['Comment']);
  2578. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  2579. $this->assertFalse($model->saveAll(
  2580. array(
  2581. 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
  2582. 'Attachment' => array('attachment' => '')
  2583. ),
  2584. array('validate' => 'only')
  2585. ));
  2586. $this->assertEqual($model->validationErrors, $expected['Comment']);
  2587. $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
  2588. }
  2589. /**
  2590. * testSaveAllAtomic method
  2591. *
  2592. * @access public
  2593. * @return void
  2594. */
  2595. function testSaveAllAtomic() {
  2596. $this->loadFixtures('Article', 'User');
  2597. $TestModel =& new Article();
  2598. $result = $TestModel->saveAll(array(
  2599. 'Article' => array(
  2600. 'title' => 'Post with Author',
  2601. 'body' => 'This post will be saved with an author',
  2602. 'user_id' => 2
  2603. ),
  2604. 'Comment' => array(
  2605. array('comment' => 'First new comment', 'user_id' => 2))
  2606. ), array('atomic' => false));
  2607. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true)));
  2608. $result = $TestModel->saveAll(array(
  2609. array(
  2610. 'id' => '1',
  2611. 'title' => 'Baleeted First Post',
  2612. 'body' => 'Baleeted!',
  2613. 'published' => 'N'
  2614. ),
  2615. array(
  2616. 'id' => '2',
  2617. 'title' => 'Just update the title'
  2618. ),
  2619. array(
  2620. 'title' => 'Creating a fourth post',
  2621. 'body' => 'Fourth post body',
  2622. 'user_id' => 2
  2623. )
  2624. ), array('atomic' => false));
  2625. $this->assertIdentical($result, array(true, true, true));
  2626. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  2627. $result = $TestModel->saveAll(array(
  2628. array(
  2629. 'id' => '1',
  2630. 'title' => 'Un-Baleeted First Post',
  2631. 'body' => 'Not Baleeted!',
  2632. 'published' => 'Y'
  2633. ),
  2634. array(
  2635. 'id' => '2',
  2636. 'title' => '',
  2637. 'body' => 'Trying to get away with an empty title'
  2638. )
  2639. ), array('validate' => true, 'atomic' => false));
  2640. $this->assertIdentical($result, array(true, false));
  2641. $result = $TestModel->saveAll(array(
  2642. 'Article' => array('id' => 2),
  2643. 'Comment' => array(
  2644. array(
  2645. 'comment' => 'First new comment',
  2646. 'published' => 'Y',
  2647. 'user_id' => 1
  2648. ),
  2649. array(
  2650. 'comment' => 'Second new comment',
  2651. 'published' => 'Y',
  2652. 'user_id' => 2
  2653. ))
  2654. ), array('validate' => true, 'atomic' => false));
  2655. $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true)));
  2656. }
  2657. /**
  2658. * testSaveAllHasMany method
  2659. *
  2660. * @access public
  2661. * @return void
  2662. */
  2663. function testSaveAllHasMany() {
  2664. $this->loadFixtures('Article', 'Comment');
  2665. $TestModel =& new Article();
  2666. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2667. $result = $TestModel->saveAll(array(
  2668. 'Article' => array('id' => 2),
  2669. 'Comment' => array(
  2670. array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
  2671. array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
  2672. )
  2673. ));
  2674. $this->assertTrue($result);
  2675. $result = $TestModel->findById(2);
  2676. $expected = array(
  2677. 'First Comment for Second Article',
  2678. 'Second Comment for Second Article',
  2679. 'First new comment',
  2680. 'Second new comment'
  2681. );
  2682. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2683. $result = $TestModel->saveAll(
  2684. array(
  2685. 'Article' => array('id' => 2),
  2686. 'Comment' => array(
  2687. array(
  2688. 'comment' => 'Third new comment',
  2689. 'published' => 'Y',
  2690. 'user_id' => 1
  2691. ))),
  2692. array('atomic' => false)
  2693. );
  2694. $this->assertTrue($result);
  2695. $result = $TestModel->findById(2);
  2696. $expected = array(
  2697. 'First Comment for Second Article',
  2698. 'Second Comment for Second Article',
  2699. 'First new comment',
  2700. 'Second new comment',
  2701. 'Third new comment'
  2702. );
  2703. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2704. $TestModel->beforeSaveReturn = false;
  2705. $result = $TestModel->saveAll(
  2706. array(
  2707. 'Article' => array('id' => 2),
  2708. 'Comment' => array(
  2709. array(
  2710. 'comment' => 'Fourth new comment',
  2711. 'published' => 'Y',
  2712. 'user_id' => 1
  2713. ))),
  2714. array('atomic' => false)
  2715. );
  2716. $this->assertEqual($result, array('Article' => false));
  2717. $result = $TestModel->findById(2);
  2718. $expected = array(
  2719. 'First Comment for Second Article',
  2720. 'Second Comment for Second Article',
  2721. 'First new comment',
  2722. 'Second new comment',
  2723. 'Third new comment'
  2724. );
  2725. $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
  2726. }
  2727. /**
  2728. * testSaveAllHasManyValidation method
  2729. *
  2730. * @access public
  2731. * @return void
  2732. */
  2733. function testSaveAllHasManyValidation() {
  2734. $this->loadFixtures('Article', 'Comment');
  2735. $TestModel =& new Article();
  2736. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  2737. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  2738. $result = $TestModel->saveAll(array(
  2739. 'Article' => array('id' => 2),
  2740. 'Comment' => array(
  2741. array('comment' => '', 'published' => 'Y', 'user_id' => 1),
  2742. )
  2743. ), array('validate' => true));
  2744. $expected = array('Comment' => array(false));
  2745. $this->assertEqual($result, $expected);
  2746. $expected = array('Comment' => array(
  2747. array('comment' => 'This field cannot be left blank')
  2748. ));
  2749. $this->assertEqual($TestModel->validationErrors, $expected);
  2750. $expected = array(
  2751. array('comment' => 'This field cannot be left blank')
  2752. );
  2753. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  2754. $result = $TestModel->saveAll(array(
  2755. 'Article' => array('id' => 2),
  2756. 'Comment' => array(
  2757. array(
  2758. 'comment' => '',
  2759. 'published' => 'Y',
  2760. 'user_id' => 1
  2761. ))
  2762. ), array('validate' => 'first'));
  2763. $this->assertFalse($result);
  2764. }
  2765. /**
  2766. * test saveAll with transactions and ensure there is no missing rollback.
  2767. *
  2768. * @return void
  2769. */
  2770. function testSaveAllManyRowsTransactionNoRollback() {
  2771. $this->loadFixtures('Post');
  2772. Mock::generate('DboSource', 'MockTransactionDboSource');
  2773. $db = ConnectionManager::create('mock_transaction', array(
  2774. 'datasource' => 'MockTransactionDbo',
  2775. ));
  2776. $db->expectOnce('rollback');
  2777. $Post =& new Post();
  2778. $Post->useDbConfig = 'mock_transaction';
  2779. $Post->validate = array(
  2780. 'title' => array('rule' => array('notEmpty'))
  2781. );
  2782. $data = array(
  2783. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2784. array('author_id' => 1, 'title' => '')
  2785. );
  2786. $Post->saveAll($data, array('atomic' => true));
  2787. }
  2788. /**
  2789. * test saveAll with transactions and ensure there is no missing rollback.
  2790. *
  2791. * @return void
  2792. */
  2793. function testSaveAllAssociatedTransactionNoRollback() {
  2794. $testDb = ConnectionManager::getDataSource('test_suite');
  2795. Mock::generate('DboSource', 'MockTransactionAssociatedDboSource');
  2796. $db = ConnectionManager::create('mock_transaction_assoc', array(
  2797. 'datasource' => 'MockTransactionAssociatedDbo',
  2798. ));
  2799. $db->columns = $testDb->columns;
  2800. $db->expectOnce('rollback');
  2801. $Post =& new Post();
  2802. $Post->useDbConfig = 'mock_transaction_assoc';
  2803. $Post->Author->useDbConfig = 'mock_transaction_assoc';
  2804. $Post->Author->validate = array(
  2805. 'user' => array('rule' => array('notEmpty'))
  2806. );
  2807. $data = array(
  2808. 'Post' => array(
  2809. 'title' => 'New post',
  2810. 'body' => 'Content',
  2811. 'published' => 'Y'
  2812. ),
  2813. 'Author' => array(
  2814. 'user' => '',
  2815. 'password' => "sekret"
  2816. )
  2817. );
  2818. $Post->saveAll($data);
  2819. }
  2820. /**
  2821. * test saveAll with nested saveAll call.
  2822. *
  2823. * @return void
  2824. */
  2825. function testSaveAllNestedSaveAll() {
  2826. $this->loadFixtures('Sample');
  2827. $TransactionTestModel =& new TransactionTestModel();
  2828. $data = array(
  2829. array('apple_id' => 1, 'name' => 'sample5'),
  2830. );
  2831. $this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
  2832. }
  2833. /**
  2834. * testSaveAllTransaction method
  2835. *
  2836. * @access public
  2837. * @return void
  2838. */
  2839. function testSaveAllTransaction() {
  2840. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  2841. $TestModel =& new Post();
  2842. $TestModel->validate = array('title' => 'notEmpty');
  2843. $data = array(
  2844. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2845. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2846. array('author_id' => 1, 'title' => '')
  2847. );
  2848. $ts = date('Y-m-d H:i:s');
  2849. $this->assertFalse($TestModel->saveAll($data));
  2850. $result = $TestModel->find('all', array('recursive' => -1));
  2851. $expected = array(
  2852. array('Post' => array(
  2853. 'id' => '1',
  2854. 'author_id' => 1,
  2855. 'title' => 'First Post',
  2856. 'body' => 'First Post Body',
  2857. 'published' => 'Y',
  2858. 'created' => '2007-03-18 10:39:23',
  2859. 'updated' => '2007-03-18 10:41:31'
  2860. )),
  2861. array('Post' => array(
  2862. 'id' => '2',
  2863. 'author_id' => 3,
  2864. 'title' => 'Second Post',
  2865. 'body' => 'Second Post Body',
  2866. 'published' => 'Y',
  2867. 'created' => '2007-03-18 10:41:23',
  2868. 'updated' => '2007-03-18 10:43:31'
  2869. )),
  2870. array('Post' => array(
  2871. 'id' => '3',
  2872. 'author_id' => 1,
  2873. 'title' => 'Third Post',
  2874. 'body' => 'Third Post Body',
  2875. 'published' => 'Y',
  2876. 'created' => '2007-03-18 10:43:23',
  2877. 'updated' => '2007-03-18 10:45:31'
  2878. )));
  2879. if (count($result) != 3) {
  2880. // Database doesn't support transactions
  2881. $expected[] = array(
  2882. 'Post' => array(
  2883. 'id' => '4',
  2884. 'author_id' => 1,
  2885. 'title' => 'New Fourth Post',
  2886. 'body' => null,
  2887. 'published' => 'N',
  2888. 'created' => $ts,
  2889. 'updated' => $ts
  2890. ));
  2891. $expected[] = array(
  2892. 'Post' => array(
  2893. 'id' => '5',
  2894. 'author_id' => 1,
  2895. 'title' => 'New Fifth Post',
  2896. 'body' => null,
  2897. 'published' => 'N',
  2898. 'created' => $ts,
  2899. 'updated' => $ts
  2900. ));
  2901. $this->assertEqual($result, $expected);
  2902. // Skip the rest of the transactional tests
  2903. return;
  2904. }
  2905. $this->assertEqual($result, $expected);
  2906. $data = array(
  2907. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2908. array('author_id' => 1, 'title' => ''),
  2909. array('author_id' => 1, 'title' => 'New Sixth Post')
  2910. );
  2911. $ts = date('Y-m-d H:i:s');
  2912. $this->assertFalse($TestModel->saveAll($data));
  2913. $result = $TestModel->find('all', array('recursive' => -1));
  2914. $expected = array(
  2915. array('Post' => array(
  2916. 'id' => '1',
  2917. 'author_id' => 1,
  2918. 'title' => 'First Post',
  2919. 'body' => 'First Post Body',
  2920. 'published' => 'Y',
  2921. 'created' => '2007-03-18 10:39:23',
  2922. 'updated' => '2007-03-18 10:41:31'
  2923. )),
  2924. array('Post' => array(
  2925. 'id' => '2',
  2926. 'author_id' => 3,
  2927. 'title' => 'Second Post',
  2928. 'body' => 'Second Post Body',
  2929. 'published' => 'Y',
  2930. 'created' => '2007-03-18 10:41:23',
  2931. 'updated' => '2007-03-18 10:43:31'
  2932. )),
  2933. array('Post' => array(
  2934. 'id' => '3',
  2935. 'author_id' => 1,
  2936. 'title' => 'Third Post',
  2937. 'body' => 'Third Post Body',
  2938. 'published' => 'Y',
  2939. 'created' => '2007-03-18 10:43:23',
  2940. 'updated' => '2007-03-18 10:45:31'
  2941. )));
  2942. if (count($result) != 3) {
  2943. // Database doesn't support transactions
  2944. $expected[] = array(
  2945. 'Post' => array(
  2946. 'id' => '4',
  2947. 'author_id' => 1,
  2948. 'title' => 'New Fourth Post',
  2949. 'body' => 'Third Post Body',
  2950. 'published' => 'N',
  2951. 'created' => $ts,
  2952. 'updated' => $ts
  2953. ));
  2954. $expected[] = array(
  2955. 'Post' => array(
  2956. 'id' => '5',
  2957. 'author_id' => 1,
  2958. 'title' => 'Third Post',
  2959. 'body' => 'Third Post Body',
  2960. 'published' => 'N',
  2961. 'created' => $ts,
  2962. 'updated' => $ts
  2963. ));
  2964. }
  2965. $this->assertEqual($result, $expected);
  2966. $TestModel->validate = array('title' => 'notEmpty');
  2967. $data = array(
  2968. array('author_id' => 1, 'title' => 'New Fourth Post'),
  2969. array('author_id' => 1, 'title' => 'New Fifth Post'),
  2970. array('author_id' => 1, 'title' => 'New Sixth Post')
  2971. );
  2972. $this->assertTrue($TestModel->saveAll($data));
  2973. $result = $TestModel->find('all', array(
  2974. 'recursive' => -1,
  2975. 'fields' => array('author_id', 'title','body','published')
  2976. ));
  2977. $expected = array(
  2978. array('Post' => array(
  2979. 'author_id' => 1,
  2980. 'title' => 'First Post',
  2981. 'body' => 'First Post Body',
  2982. 'published' => 'Y'
  2983. )),
  2984. array('Post' => array(
  2985. 'author_id' => 3,
  2986. 'title' => 'Second Post',
  2987. 'body' => 'Second Post Body',
  2988. 'published' => 'Y'
  2989. )),
  2990. array('Post' => array(
  2991. 'author_id' => 1,
  2992. 'title' => 'Third Post',
  2993. 'body' => 'Third Post Body',
  2994. 'published' => 'Y'
  2995. )),
  2996. array('Post' => array(
  2997. 'author_id' => 1,
  2998. 'title' => 'New Fourth Post',
  2999. 'body' => '',
  3000. 'published' => 'N'
  3001. )),
  3002. array('Post' => array(
  3003. 'author_id' => 1,
  3004. 'title' => 'New Fifth Post',
  3005. 'body' => '',
  3006. 'published' => 'N'
  3007. )),
  3008. array('Post' => array(
  3009. 'author_id' => 1,
  3010. 'title' => 'New Sixth Post',
  3011. 'body' => '',
  3012. 'published' => 'N'
  3013. )));
  3014. $this->assertEqual($result, $expected);
  3015. }
  3016. /**
  3017. * testSaveAllValidation method
  3018. *
  3019. * @access public
  3020. * @return void
  3021. */
  3022. function testSaveAllValidation() {
  3023. $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
  3024. $TestModel =& new Post();
  3025. $data = array(
  3026. array(
  3027. 'id' => '1',
  3028. 'title' => 'Baleeted First Post',
  3029. 'body' => 'Baleeted!',
  3030. 'published' => 'N'
  3031. ),
  3032. array(
  3033. 'id' => '2',
  3034. 'title' => 'Just update the title'
  3035. ),
  3036. array(
  3037. 'title' => 'Creating a fourth post',
  3038. 'body' => 'Fourth post body',
  3039. 'author_id' => 2
  3040. ));
  3041. $this->assertTrue($TestModel->saveAll($data));
  3042. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3043. $ts = date('Y-m-d H:i:s');
  3044. $expected = array(
  3045. array(
  3046. 'Post' => array(
  3047. 'id' => '1',
  3048. 'author_id' => '1',
  3049. 'title' => 'Baleeted First Post',
  3050. 'body' => 'Baleeted!',
  3051. 'published' => 'N',
  3052. 'created' => '2007-03-18 10:39:23',
  3053. 'updated' => $ts
  3054. )),
  3055. array(
  3056. 'Post' => array(
  3057. 'id' => '2',
  3058. 'author_id' => '3',
  3059. 'title' => 'Just update the title',
  3060. 'body' => 'Second Post Body',
  3061. 'published' => 'Y',
  3062. 'created' => '2007-03-18 10:41:23', 'updated' => $ts
  3063. )),
  3064. array(
  3065. 'Post' => array(
  3066. 'id' => '3',
  3067. 'author_id' => '1',
  3068. 'title' => 'Third Post',
  3069. 'body' => 'Third Post Body',
  3070. 'published' => 'Y',
  3071. 'created' => '2007-03-18 10:43:23',
  3072. 'updated' => '2007-03-18 10:45:31'
  3073. )),
  3074. array(
  3075. 'Post' => array(
  3076. 'id' => '4',
  3077. 'author_id' => '2',
  3078. 'title' => 'Creating a fourth post',
  3079. 'body' => 'Fourth post body',
  3080. 'published' => 'N',
  3081. 'created' => $ts,
  3082. 'updated' => $ts
  3083. )));
  3084. $this->assertEqual($result, $expected);
  3085. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3086. $data = array(
  3087. array(
  3088. 'id' => '1',
  3089. 'title' => 'Un-Baleeted First Post',
  3090. 'body' => 'Not Baleeted!',
  3091. 'published' => 'Y'
  3092. ),
  3093. array(
  3094. 'id' => '2',
  3095. 'title' => '',
  3096. 'body' => 'Trying to get away with an empty title'
  3097. ));
  3098. $result = $TestModel->saveAll($data);
  3099. $this->assertEqual($result, false);
  3100. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3101. $errors = array(1 => array('title' => 'This field cannot be left blank'));
  3102. $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
  3103. if (!$transactionWorked) {
  3104. $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
  3105. $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result));
  3106. }
  3107. $this->assertEqual($TestModel->validationErrors, $errors);
  3108. $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
  3109. $data = array(
  3110. array(
  3111. 'id' => '1',
  3112. 'title' => 'Un-Baleeted First Post',
  3113. 'body' => 'Not Baleeted!',
  3114. 'published' => 'Y'
  3115. ),
  3116. array(
  3117. 'id' => '2',
  3118. 'title' => '',
  3119. 'body' => 'Trying to get away with an empty title'
  3120. ));
  3121. $result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
  3122. $this->assertEqual($result, array(true, false));
  3123. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3124. $errors = array(1 => array('title' => 'This field cannot be left blank'));
  3125. $newTs = date('Y-m-d H:i:s');
  3126. $expected = array(
  3127. array(
  3128. 'Post' => array(
  3129. 'id' => '1',
  3130. 'author_id' => '1',
  3131. 'title' => 'Un-Baleeted First Post',
  3132. 'body' => 'Not Baleeted!',
  3133. 'published' => 'Y',
  3134. 'created' => '2007-03-18 10:39:23',
  3135. 'updated' => $newTs
  3136. )),
  3137. array(
  3138. 'Post' => array(
  3139. 'id' => '2',
  3140. 'author_id' => '3',
  3141. 'title' => 'Just update the title',
  3142. 'body' => 'Second Post Body',
  3143. 'published' => 'Y',
  3144. 'created' => '2007-03-18 10:41:23',
  3145. 'updated' => $ts
  3146. )),
  3147. array(
  3148. 'Post' => array(
  3149. 'id' => '3',
  3150. 'author_id' => '1',
  3151. 'title' => 'Third Post',
  3152. 'body' => 'Third Post Body',
  3153. 'published' => 'Y',
  3154. 'created' => '2007-03-18 10:43:23',
  3155. 'updated' => '2007-03-18 10:45:31'
  3156. )),
  3157. array(
  3158. 'Post' => array(
  3159. 'id' => '4',
  3160. 'author_id' => '2',
  3161. 'title' => 'Creating a fourth post',
  3162. 'body' => 'Fourth post body',
  3163. 'published' => 'N',
  3164. 'created' => $ts,
  3165. 'updated' => $ts
  3166. )));
  3167. $this->assertEqual($result, $expected);
  3168. $this->assertEqual($TestModel->validationErrors, $errors);
  3169. $data = array(
  3170. array(
  3171. 'id' => '1',
  3172. 'title' => 'Re-Baleeted First Post',
  3173. 'body' => 'Baleeted!',
  3174. 'published' => 'N'
  3175. ),
  3176. array(
  3177. 'id' => '2',
  3178. 'title' => '',
  3179. 'body' => 'Trying to get away with an empty title'
  3180. ));
  3181. $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first')));
  3182. $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
  3183. $this->assertEqual($result, $expected);
  3184. $this->assertEqual($TestModel->validationErrors, $errors);
  3185. $data = array(
  3186. array(
  3187. 'title' => 'First new post',
  3188. 'body' => 'Woohoo!',
  3189. 'published' => 'Y'
  3190. ),
  3191. array(
  3192. 'title' => 'Empty body',
  3193. 'body' => ''
  3194. ));
  3195. $TestModel->validate['body'] = 'notEmpty';
  3196. }
  3197. /**
  3198. * testSaveAllValidationOnly method
  3199. *
  3200. * @access public
  3201. * @return void
  3202. */
  3203. function testSaveAllValidationOnly() {
  3204. $TestModel =& new Comment();
  3205. $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
  3206. $data = array(
  3207. 'Comment' => array(
  3208. 'comment' => 'This is the comment'
  3209. ),
  3210. 'Attachment' => array(
  3211. 'attachment' => ''
  3212. )
  3213. );
  3214. $result = $TestModel->saveAll($data, array('validate' => 'only'));
  3215. $this->assertFalse($result);
  3216. $TestModel =& new Article();
  3217. $TestModel->validate = array('title' => 'notEmpty');
  3218. $result = $TestModel->saveAll(
  3219. array(
  3220. 0 => array('title' => ''),
  3221. 1 => array('title' => 'title 1'),
  3222. 2 => array('title' => 'title 2'),
  3223. ),
  3224. array('validate'=>'only')
  3225. );
  3226. $this->assertFalse($result);
  3227. $expected = array(
  3228. 0 => array('title' => 'This field cannot be left blank'),
  3229. );
  3230. $this->assertEqual($TestModel->validationErrors, $expected);
  3231. $result = $TestModel->saveAll(
  3232. array(
  3233. 0 => array('title' => 'title 0'),
  3234. 1 => array('title' => ''),
  3235. 2 => array('title' => 'title 2'),
  3236. ),
  3237. array('validate'=>'only')
  3238. );
  3239. $this->assertFalse($result);
  3240. $expected = array(
  3241. 1 => array('title' => 'This field cannot be left blank'),
  3242. );
  3243. $this->assertEqual($TestModel->validationErrors, $expected);
  3244. }
  3245. /**
  3246. * testSaveAllValidateFirst method
  3247. *
  3248. * @access public
  3249. * @return void
  3250. */
  3251. function testSaveAllValidateFirst() {
  3252. $model =& new Article();
  3253. $model->deleteAll(true);
  3254. $model->Comment->validate = array('comment' => 'notEmpty');
  3255. $result = $model->saveAll(array(
  3256. 'Article' => array(
  3257. 'title' => 'Post with Author',
  3258. 'body' => 'This post will be saved author'
  3259. ),
  3260. 'Comment' => array(
  3261. array('comment' => 'First new comment'),
  3262. array('comment' => '')
  3263. )
  3264. ), array('validate' => 'first'));
  3265. $this->assertFalse($result);
  3266. $result = $model->find('all');
  3267. $this->assertEqual($result, array());
  3268. $expected = array('Comment' => array(
  3269. 1 => array('comment' => 'This field cannot be left blank')
  3270. ));
  3271. $this->assertEqual($model->Comment->validationErrors, $expected['Comment']);
  3272. $this->assertIdentical($model->Comment->find('count'), 0);
  3273. $result = $model->saveAll(
  3274. array(
  3275. 'Article' => array(
  3276. 'title' => 'Post with Author',
  3277. 'body' => 'This post will be saved with an author',
  3278. 'user_id' => 2
  3279. ),
  3280. 'Comment' => array(
  3281. array(
  3282. 'comment' => 'Only new comment',
  3283. 'user_id' => 2
  3284. ))),
  3285. array('validate' => 'first')
  3286. );
  3287. $this->assertIdentical($result, true);
  3288. $result = $model->Comment->find('all');
  3289. $this->assertIdentical(count($result), 1);
  3290. $result = Set::extract('/Comment/article_id', $result);
  3291. $this->assertTrue($result[0] === 1 || $result[0] === '1');
  3292. $model->deleteAll(true);
  3293. $data = array(
  3294. 'Article' => array(
  3295. 'title' => 'Post with Author saveAlled from comment',
  3296. 'body' => 'This post will be saved with an author',
  3297. 'user_id' => 2
  3298. ),
  3299. 'Comment' => array(
  3300. 'comment' => 'Only new comment', 'user_id' => 2
  3301. ));
  3302. $result = $model->Comment->saveAll($data, array('validate' => 'first'));
  3303. $this->assertTrue($result);
  3304. $result = $model->find('all');
  3305. $this->assertEqual(
  3306. $result[0]['Article']['title'],
  3307. 'Post with Author saveAlled from comment'
  3308. );
  3309. $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment');
  3310. }
  3311. /**
  3312. * test saveAll()'s return is correct when using atomic = false and validate = first.
  3313. *
  3314. * @return void
  3315. */
  3316. function testSaveAllValidateFirstAtomicFalse() {
  3317. $Something =& new Something();
  3318. $invalidData = array(
  3319. array(
  3320. 'title' => 'foo',
  3321. 'body' => 'bar',
  3322. 'published' => 'baz',
  3323. ),
  3324. array(
  3325. 'body' => 3,
  3326. 'published' =>'sd',
  3327. ),
  3328. );
  3329. $Something->create();
  3330. $Something->validate = array(
  3331. 'title' => array(
  3332. 'rule' => 'alphaNumeric',
  3333. 'required' => true,
  3334. ),
  3335. 'body' => array(
  3336. 'rule' => 'alphaNumeric',
  3337. 'required' => true,
  3338. 'allowEmpty' => true,
  3339. ),
  3340. );
  3341. $result = $Something->saveAll($invalidData, array(
  3342. 'atomic' => false,
  3343. 'validate' => 'first',
  3344. ));
  3345. $expected = array(true, false);
  3346. $this->assertEqual($result, $expected);
  3347. $Something =& new Something();
  3348. $validData = array(
  3349. array(
  3350. 'title' => 'title value',
  3351. 'body' => 'body value',
  3352. 'published' => 'baz',
  3353. ),
  3354. array(
  3355. 'title' => 'valid',
  3356. 'body' => 'this body',
  3357. 'published' =>'sd',
  3358. ),
  3359. );
  3360. $Something->create();
  3361. $result = $Something->saveAll($validData, array(
  3362. 'atomic' => false,
  3363. 'validate' => 'first',
  3364. ));
  3365. $expected = array(true, true);
  3366. $this->assertEqual($result, $expected);
  3367. }
  3368. /**
  3369. * testUpdateWithCalculation method
  3370. *
  3371. * @access public
  3372. * @return void
  3373. */
  3374. function testUpdateWithCalculation() {
  3375. $this->loadFixtures('DataTest');
  3376. $model =& new DataTest();
  3377. $model->deleteAll(true);
  3378. $result = $model->saveAll(array(
  3379. array('count' => 5, 'float' => 1.1),
  3380. array('count' => 3, 'float' => 1.2),
  3381. array('count' => 4, 'float' => 1.3),
  3382. array('count' => 1, 'float' => 2.0),
  3383. ));
  3384. $this->assertTrue($result);
  3385. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  3386. $this->assertEqual($result, array(5, 3, 4, 1));
  3387. $this->assertTrue($model->updateAll(array('count' => 'count + 2')));
  3388. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  3389. $this->assertEqual($result, array(7, 5, 6, 3));
  3390. $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
  3391. $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
  3392. $this->assertEqual($result, array(6, 4, 5, 2));
  3393. }
  3394. /**
  3395. * testSaveAllHasManyValidationOnly method
  3396. *
  3397. * @access public
  3398. * @return void
  3399. */
  3400. function testSaveAllHasManyValidationOnly() {
  3401. $this->loadFixtures('Article', 'Comment');
  3402. $TestModel =& new Article();
  3403. $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
  3404. $TestModel->Comment->validate = array('comment' => 'notEmpty');
  3405. $result = $TestModel->saveAll(
  3406. array(
  3407. 'Article' => array('id' => 2),
  3408. 'Comment' => array(
  3409. array(
  3410. 'id' => 1,
  3411. 'comment' => '',
  3412. 'published' => 'Y',
  3413. 'user_id' => 1),
  3414. array(
  3415. 'id' => 2,
  3416. 'comment' =>
  3417. 'comment',
  3418. 'published' => 'Y',
  3419. 'user_id' => 1
  3420. ))),
  3421. array('validate' => 'only')
  3422. );
  3423. $this->assertFalse($result);
  3424. $result = $TestModel->saveAll(
  3425. array(
  3426. 'Article' => array('id' => 2),
  3427. 'Comment' => array(
  3428. array(
  3429. 'id' => 1,
  3430. 'comment' => '',
  3431. 'published' => 'Y',
  3432. 'user_id' => 1
  3433. ),
  3434. array(
  3435. 'id' => 2,
  3436. 'comment' => 'comment',
  3437. 'published' => 'Y',
  3438. 'user_id' => 1
  3439. ),
  3440. array(
  3441. 'id' => 3,
  3442. 'comment' => '',
  3443. 'published' => 'Y',
  3444. 'user_id' => 1
  3445. ))),
  3446. array(
  3447. 'validate' => 'only',
  3448. 'atomic' => false
  3449. ));
  3450. $expected = array(
  3451. 'Article' => true,
  3452. 'Comment' => array(false, true, false)
  3453. );
  3454. $this->assertIdentical($result, $expected);
  3455. $expected = array('Comment' => array(
  3456. 0 => array('comment' => 'This field cannot be left blank'),
  3457. 2 => array('comment' => 'This field cannot be left blank')
  3458. ));
  3459. $this->assertEqual($TestModel->validationErrors, $expected);
  3460. $expected = array(
  3461. 0 => array('comment' => 'This field cannot be left blank'),
  3462. 2 => array('comment' => 'This field cannot be left blank')
  3463. );
  3464. $this->assertEqual($TestModel->Comment->validationErrors, $expected);
  3465. }
  3466. /**
  3467. * TestFindAllWithoutForeignKey
  3468. *
  3469. * @link http://code.cakephp.org/tickets/view/69
  3470. * @access public
  3471. * @return void
  3472. */
  3473. function testFindAllForeignKey() {
  3474. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  3475. $ProductUpdateAll =& new ProductUpdateAll();
  3476. $conditions = array('Group.name' => 'group one');
  3477. $ProductUpdateAll->bindModel(array(
  3478. 'belongsTo' => array(
  3479. 'Group' => array('className' => 'GroupUpdateAll')
  3480. )
  3481. ));
  3482. $ProductUpdateAll->belongsTo = array(
  3483. 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
  3484. );
  3485. $results = $ProductUpdateAll->find('all', compact('conditions'));
  3486. $this->assertTrue(!empty($results));
  3487. $ProductUpdateAll->bindModel(array('belongsTo'=>array('Group')));
  3488. $ProductUpdateAll->belongsTo = array(
  3489. 'Group' => array(
  3490. 'className' => 'GroupUpdateAll',
  3491. 'foreignKey' => false,
  3492. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  3493. ));
  3494. $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
  3495. $this->assertTrue(!empty($resultsFkFalse));
  3496. $expected = array(
  3497. '0' => array(
  3498. 'ProductUpdateAll' => array(
  3499. 'id' => 1,
  3500. 'name' => 'product one',
  3501. 'groupcode' => 120,
  3502. 'group_id' => 1),
  3503. 'Group' => array(
  3504. 'id' => 1,
  3505. 'name' => 'group one',
  3506. 'code' => 120)
  3507. ),
  3508. '1' => array(
  3509. 'ProductUpdateAll' => array(
  3510. 'id' => 2,
  3511. 'name' => 'product two',
  3512. 'groupcode' => 120,
  3513. 'group_id' => 1),
  3514. 'Group' => array(
  3515. 'id' => 1,
  3516. 'name' => 'group one',
  3517. 'code' => 120)
  3518. )
  3519. );
  3520. $this->assertEqual($results, $expected);
  3521. $this->assertEqual($resultsFkFalse, $expected);
  3522. }
  3523. /**
  3524. * test updateAll with empty values.
  3525. *
  3526. * @return void
  3527. */
  3528. function testUpdateAllEmptyValues() {
  3529. $this->loadFixtures('Author', 'Post');
  3530. $model = new Author();
  3531. $result = $model->updateAll(array('user' => '""'));
  3532. $this->assertTrue($result);
  3533. }
  3534. /**
  3535. * testUpdateAllWithJoins
  3536. *
  3537. * @link http://code.cakephp.org/tickets/view/69
  3538. * @access public
  3539. * @return void
  3540. */
  3541. function testUpdateAllWithJoins() {
  3542. $this->skipIf(
  3543. $this->db->config['driver'] == 'postgres',
  3544. '%s Currently, there is no way of doing joins in an update statement in postgresql'
  3545. );
  3546. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  3547. $ProductUpdateAll =& new ProductUpdateAll();
  3548. $conditions = array('Group.name' => 'group one');
  3549. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  3550. 'Group' => array('className' => 'GroupUpdateAll')))
  3551. );
  3552. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  3553. $results = $ProductUpdateAll->find('all', array(
  3554. 'conditions' => array('ProductUpdateAll.name' => 'new product')
  3555. ));
  3556. $expected = array(
  3557. '0' => array(
  3558. 'ProductUpdateAll' => array(
  3559. 'id' => 1,
  3560. 'name' => 'new product',
  3561. 'groupcode' => 120,
  3562. 'group_id' => 1),
  3563. 'Group' => array(
  3564. 'id' => 1,
  3565. 'name' => 'group one',
  3566. 'code' => 120)
  3567. ),
  3568. '1' => array(
  3569. 'ProductUpdateAll' => array(
  3570. 'id' => 2,
  3571. 'name' => 'new product',
  3572. 'groupcode' => 120,
  3573. 'group_id' => 1),
  3574. 'Group' => array(
  3575. 'id' => 1,
  3576. 'name' => 'group one',
  3577. 'code' => 120)));
  3578. $this->assertEqual($results, $expected);
  3579. }
  3580. /**
  3581. * testUpdateAllWithoutForeignKey
  3582. *
  3583. * @link http://code.cakephp.org/tickets/view/69
  3584. * @access public
  3585. * @return void
  3586. */
  3587. function testUpdateAllWithoutForeignKey() {
  3588. $this->skipIf(
  3589. $this->db->config['driver'] == 'postgres',
  3590. '%s Currently, there is no way of doing joins in an update statement in postgresql'
  3591. );
  3592. $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
  3593. $ProductUpdateAll =& new ProductUpdateAll();
  3594. $conditions = array('Group.name' => 'group one');
  3595. $ProductUpdateAll->bindModel(array('belongsTo' => array(
  3596. 'Group' => array('className' => 'GroupUpdateAll')
  3597. )));
  3598. $ProductUpdateAll->belongsTo = array(
  3599. 'Group' => array(
  3600. 'className' => 'GroupUpdateAll',
  3601. 'foreignKey' => false,
  3602. 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
  3603. )
  3604. );
  3605. $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
  3606. $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name'=>'new product')));
  3607. $expected = array(
  3608. '0' => array(
  3609. 'ProductUpdateAll' => array(
  3610. 'id' => 1,
  3611. 'name' => 'new product',
  3612. 'groupcode' => 120,
  3613. 'group_id' => 1),
  3614. 'Group' => array(
  3615. 'id' => 1,
  3616. 'name' => 'group one',
  3617. 'code' => 120)
  3618. ),
  3619. '1' => array(
  3620. 'ProductUpdateAll' => array(
  3621. 'id' => 2,
  3622. 'name' => 'new product',
  3623. 'groupcode' => 120,
  3624. 'group_id' => 1),
  3625. 'Group' => array(
  3626. 'id' => 1,
  3627. 'name' => 'group one',
  3628. 'code' => 120)));
  3629. $this->assertEqual($resultsFkFalse, $expected);
  3630. }
  3631. /**
  3632. * test that saveAll behaves like plain save() when suplied empty data
  3633. *
  3634. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
  3635. * @access public
  3636. * @return void
  3637. */
  3638. function testSaveAllEmptyData() {
  3639. $this->loadFixtures('Article', 'ProductUpdateAll');
  3640. $model =& new Article();
  3641. $result = $model->saveAll(array(), array('validate' => 'first'));
  3642. $this->assertTrue($result);
  3643. $model =& new ProductUpdateAll();
  3644. $result = $model->saveAll(array());
  3645. $this->assertFalse($result);
  3646. }
  3647. /**
  3648. * test writing floats in german locale.
  3649. *
  3650. * @return void
  3651. */
  3652. function testWriteFloatAsGerman() {
  3653. $restore = setlocale(LC_ALL, null);
  3654. setlocale(LC_ALL, 'de_DE');
  3655. $model = new DataTest();
  3656. $result = $model->save(array(
  3657. 'count' => 1,
  3658. 'float' => 3.14593
  3659. ));
  3660. $this->assertTrue($result);
  3661. setlocale(LC_ALL, $restore);
  3662. }
  3663. }