MarshallerTest.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Database\Expression\IdentifierExpression;
  17. use Cake\I18n\Time;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\Marshaller;
  20. use Cake\ORM\Table;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Validation\Validator;
  24. /**
  25. * Test entity for mass assignment.
  26. */
  27. class OpenEntity extends Entity
  28. {
  29. protected $_accessible = [
  30. '*' => true,
  31. ];
  32. }
  33. /**
  34. * Test entity for mass assignment.
  35. */
  36. class ProtectedArticle extends Entity
  37. {
  38. protected $_accessible = [
  39. 'title' => true,
  40. 'body' => true
  41. ];
  42. }
  43. /**
  44. * Marshaller test case
  45. */
  46. class MarshallerTest extends TestCase
  47. {
  48. public $fixtures = ['core.tags', 'core.articles_tags', 'core.articles', 'core.users', 'core.comments'];
  49. /**
  50. * setup
  51. *
  52. * @return void
  53. */
  54. public function setUp()
  55. {
  56. parent::setUp();
  57. $articles = TableRegistry::get('Articles');
  58. $articles->belongsTo('Users');
  59. $articles->hasMany('Comments');
  60. $articles->belongsToMany('Tags');
  61. $comments = TableRegistry::get('Comments');
  62. $users = TableRegistry::get('Users');
  63. $tags = TableRegistry::get('Tags');
  64. $articleTags = TableRegistry::get('ArticlesTags');
  65. $comments->belongsTo('Articles');
  66. $comments->belongsTo('Users');
  67. $articles->entityClass(__NAMESPACE__ . '\OpenEntity');
  68. $comments->entityClass(__NAMESPACE__ . '\OpenEntity');
  69. $users->entityClass(__NAMESPACE__ . '\OpenEntity');
  70. $tags->entityClass(__NAMESPACE__ . '\OpenEntity');
  71. $articleTags->entityClass(__NAMESPACE__ . '\OpenEntity');
  72. $this->articles = $articles;
  73. $this->comments = $comments;
  74. }
  75. /**
  76. * Teardown
  77. *
  78. * @return void
  79. */
  80. public function tearDown()
  81. {
  82. parent::tearDown();
  83. TableRegistry::clear();
  84. unset($this->articles, $this->comments);
  85. }
  86. /**
  87. * Test one() in a simple use.
  88. *
  89. * @return void
  90. */
  91. public function testOneSimple()
  92. {
  93. $data = [
  94. 'title' => 'My title',
  95. 'body' => 'My content',
  96. 'author_id' => 1,
  97. 'not_in_schema' => true
  98. ];
  99. $marshall = new Marshaller($this->articles);
  100. $result = $marshall->one($data, []);
  101. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  102. $this->assertEquals($data, $result->toArray());
  103. $this->assertTrue($result->dirty(), 'Should be a dirty entity.');
  104. $this->assertTrue($result->isNew(), 'Should be new');
  105. $this->assertEquals('Articles', $result->source());
  106. }
  107. /**
  108. * Test that marshalling an entity with '' for pk values results
  109. * in no pk value being set.
  110. *
  111. * @return void
  112. */
  113. public function testOneEmptyStringPrimaryKey()
  114. {
  115. $data = [
  116. 'id' => '',
  117. 'username' => 'superuser',
  118. 'password' => 'root',
  119. 'created' => new Time('2013-10-10 00:00'),
  120. 'updated' => new Time('2013-10-10 00:00')
  121. ];
  122. $marshall = new Marshaller($this->articles);
  123. $result = $marshall->one($data, []);
  124. $this->assertFalse($result->dirty('id'));
  125. $this->assertNull($result->id);
  126. }
  127. /**
  128. * Test marshalling datetime/date field.
  129. *
  130. * @return void
  131. */
  132. public function testOneWithDatetimeField()
  133. {
  134. $data = [
  135. 'comment' => 'My Comment text',
  136. 'created' => [
  137. 'year' => '2014',
  138. 'month' => '2',
  139. 'day' => 14
  140. ]
  141. ];
  142. $marshall = new Marshaller($this->comments);
  143. $result = $marshall->one($data, []);
  144. $this->assertEquals(new Time('2014-02-14 00:00:00'), $result->created);
  145. $data['created'] = [
  146. 'year' => '2014',
  147. 'month' => '2',
  148. 'day' => 14,
  149. 'hour' => 9,
  150. 'minute' => 25,
  151. 'meridian' => 'pm'
  152. ];
  153. $result = $marshall->one($data, []);
  154. $this->assertEquals(new Time('2014-02-14 21:25:00'), $result->created);
  155. $data['created'] = [
  156. 'year' => '2014',
  157. 'month' => '2',
  158. 'day' => 14,
  159. 'hour' => 9,
  160. 'minute' => 25,
  161. ];
  162. $result = $marshall->one($data, []);
  163. $this->assertEquals(new Time('2014-02-14 09:25:00'), $result->created);
  164. $data['created'] = '2014-02-14 09:25:00';
  165. $result = $marshall->one($data, []);
  166. $this->assertEquals(new Time('2014-02-14 09:25:00'), $result->created);
  167. $data['created'] = 1392387900;
  168. $result = $marshall->one($data, []);
  169. $this->assertEquals($data['created'], $result->created->getTimestamp());
  170. }
  171. /**
  172. * Ensure that marshalling casts reasonably.
  173. *
  174. * @return void
  175. */
  176. public function testOneOnlyCastMatchingData()
  177. {
  178. $data = [
  179. 'title' => 'My title',
  180. 'body' => 'My content',
  181. 'author_id' => 'derp',
  182. 'created' => 'fale'
  183. ];
  184. $this->articles->entityClass(__NAMESPACE__ . '\OpenEntity');
  185. $marshall = new Marshaller($this->articles);
  186. $result = $marshall->one($data, []);
  187. $this->assertSame($data['title'], $result->title);
  188. $this->assertSame($data['author_id'], $result->author_id, 'No cast on bad data.');
  189. $this->assertSame($data['created'], $result->created, 'No cast on bad data.');
  190. }
  191. /**
  192. * Test one() follows mass-assignment rules.
  193. *
  194. * @return void
  195. */
  196. public function testOneAccessibleProperties()
  197. {
  198. $data = [
  199. 'title' => 'My title',
  200. 'body' => 'My content',
  201. 'author_id' => 1,
  202. 'not_in_schema' => true
  203. ];
  204. $this->articles->entityClass(__NAMESPACE__ . '\ProtectedArticle');
  205. $marshall = new Marshaller($this->articles);
  206. $result = $marshall->one($data, []);
  207. $this->assertInstanceOf(__NAMESPACE__ . '\ProtectedArticle', $result);
  208. $this->assertNull($result->author_id);
  209. $this->assertNull($result->not_in_schema);
  210. }
  211. /**
  212. * Test one() supports accessibleFields option
  213. *
  214. * @return void
  215. */
  216. public function testOneAccessibleFieldsOption()
  217. {
  218. $data = [
  219. 'title' => 'My title',
  220. 'body' => 'My content',
  221. 'author_id' => 1,
  222. 'not_in_schema' => true
  223. ];
  224. $this->articles->entityClass(__NAMESPACE__ . '\ProtectedArticle');
  225. $marshall = new Marshaller($this->articles);
  226. $result = $marshall->one($data, ['accessibleFields' => ['body' => false]]);
  227. $this->assertNull($result->body);
  228. $result = $marshall->one($data, ['accessibleFields' => ['author_id' => true]]);
  229. $this->assertEquals($data['author_id'], $result->author_id);
  230. $this->assertNull($result->not_in_schema);
  231. $result = $marshall->one($data, ['accessibleFields' => ['*' => true]]);
  232. $this->assertEquals($data['author_id'], $result->author_id);
  233. $this->assertTrue($result->not_in_schema);
  234. }
  235. /**
  236. * test one() with a wrapping model name.
  237. *
  238. * @return void
  239. */
  240. public function testOneWithAdditionalName()
  241. {
  242. $data = [
  243. 'Articles' => [
  244. 'title' => 'My title',
  245. 'body' => 'My content',
  246. 'author_id' => 1,
  247. 'not_in_schema' => true,
  248. 'user' => [
  249. 'username' => 'mark',
  250. ]
  251. ]
  252. ];
  253. $marshall = new Marshaller($this->articles);
  254. $result = $marshall->one($data, ['associated' => ['Users']]);
  255. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  256. $this->assertTrue($result->dirty(), 'Should be a dirty entity.');
  257. $this->assertTrue($result->isNew(), 'Should be new');
  258. $this->assertEquals($data['Articles']['title'], $result->title);
  259. $this->assertEquals($data['Articles']['user']['username'], $result->user->username);
  260. }
  261. /**
  262. * test one() with association data.
  263. *
  264. * @return void
  265. */
  266. public function testOneAssociationsSingle()
  267. {
  268. $data = [
  269. 'title' => 'My title',
  270. 'body' => 'My content',
  271. 'author_id' => 1,
  272. 'comments' => [
  273. ['comment' => 'First post', 'user_id' => 2],
  274. ['comment' => 'Second post', 'user_id' => 2],
  275. ],
  276. 'user' => [
  277. 'username' => 'mark',
  278. 'password' => 'secret'
  279. ]
  280. ];
  281. $marshall = new Marshaller($this->articles);
  282. $result = $marshall->one($data, ['associated' => ['Users']]);
  283. $this->assertEquals($data['title'], $result->title);
  284. $this->assertEquals($data['body'], $result->body);
  285. $this->assertEquals($data['author_id'], $result->author_id);
  286. $this->assertInternalType('array', $result->comments);
  287. $this->assertEquals($data['comments'], $result->comments);
  288. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  289. $this->assertEquals($data['user']['username'], $result->user->username);
  290. $this->assertEquals($data['user']['password'], $result->user->password);
  291. }
  292. /**
  293. * test one() with association data.
  294. *
  295. * @return void
  296. */
  297. public function testOneAssociationsMany()
  298. {
  299. $data = [
  300. 'title' => 'My title',
  301. 'body' => 'My content',
  302. 'author_id' => 1,
  303. 'comments' => [
  304. ['comment' => 'First post', 'user_id' => 2],
  305. ['comment' => 'Second post', 'user_id' => 2],
  306. ],
  307. 'user' => [
  308. 'username' => 'mark',
  309. 'password' => 'secret'
  310. ]
  311. ];
  312. $marshall = new Marshaller($this->articles);
  313. $result = $marshall->one($data, ['associated' => ['Comments']]);
  314. $this->assertEquals($data['title'], $result->title);
  315. $this->assertEquals($data['body'], $result->body);
  316. $this->assertEquals($data['author_id'], $result->author_id);
  317. $this->assertInternalType('array', $result->comments);
  318. $this->assertCount(2, $result->comments);
  319. $this->assertInstanceOf('Cake\ORM\Entity', $result->comments[0]);
  320. $this->assertInstanceOf('Cake\ORM\Entity', $result->comments[1]);
  321. $this->assertEquals($data['comments'][0]['comment'], $result->comments[0]->comment);
  322. $this->assertInternalType('array', $result->user);
  323. $this->assertEquals($data['user'], $result->user);
  324. }
  325. /**
  326. * Test building the _joinData entity for belongstomany associations.
  327. *
  328. * @return void
  329. */
  330. public function testOneBelongsToManyJoinData()
  331. {
  332. $data = [
  333. 'title' => 'My title',
  334. 'body' => 'My content',
  335. 'author_id' => 1,
  336. 'tags' => [
  337. ['tag' => 'news', '_joinData' => ['active' => 1]],
  338. ['tag' => 'cakephp', '_joinData' => ['active' => 0]],
  339. ],
  340. ];
  341. $marshall = new Marshaller($this->articles);
  342. $result = $marshall->one($data, [
  343. 'associated' => ['Tags']
  344. ]);
  345. $this->assertEquals($data['title'], $result->title);
  346. $this->assertEquals($data['body'], $result->body);
  347. $this->assertInternalType('array', $result->tags);
  348. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  349. $this->assertEquals($data['tags'][0]['tag'], $result->tags[0]->tag);
  350. $this->assertInstanceOf(
  351. 'Cake\ORM\Entity',
  352. $result->tags[0]->_joinData,
  353. '_joinData should be an entity.'
  354. );
  355. $this->assertEquals(
  356. $data['tags'][0]['_joinData']['active'],
  357. $result->tags[0]->_joinData->active,
  358. '_joinData should be an entity.'
  359. );
  360. }
  361. /**
  362. * Test marshalling nested associations on the _joinData structure.
  363. *
  364. * @return void
  365. */
  366. public function testOneBelongsToManyJoinDataAssociated()
  367. {
  368. $data = [
  369. 'title' => 'My title',
  370. 'body' => 'My content',
  371. 'author_id' => 1,
  372. 'tags' => [
  373. [
  374. 'tag' => 'news',
  375. '_joinData' => [
  376. 'active' => 1,
  377. 'user' => ['username' => 'Bill'],
  378. ]
  379. ],
  380. [
  381. 'tag' => 'cakephp',
  382. '_joinData' => [
  383. 'active' => 0,
  384. 'user' => ['username' => 'Mark'],
  385. ]
  386. ],
  387. ],
  388. ];
  389. $articlesTags = TableRegistry::get('ArticlesTags');
  390. $articlesTags->belongsTo('Users');
  391. $marshall = new Marshaller($this->articles);
  392. $result = $marshall->one($data, ['associated' => ['Tags._joinData.Users']]);
  393. $this->assertInstanceOf(
  394. 'Cake\ORM\Entity',
  395. $result->tags[0]->_joinData->user,
  396. 'joinData should contain a user entity.'
  397. );
  398. $this->assertEquals('Bill', $result->tags[0]->_joinData->user->username);
  399. $this->assertInstanceOf(
  400. 'Cake\ORM\Entity',
  401. $result->tags[1]->_joinData->user,
  402. 'joinData should contain a user entity.'
  403. );
  404. $this->assertEquals('Mark', $result->tags[1]->_joinData->user->username);
  405. }
  406. /**
  407. * Test one() with deeper associations.
  408. *
  409. * @return void
  410. */
  411. public function testOneDeepAssociations()
  412. {
  413. $data = [
  414. 'comment' => 'First post',
  415. 'user_id' => 2,
  416. 'article' => [
  417. 'title' => 'Article title',
  418. 'body' => 'Article body',
  419. 'user' => [
  420. 'username' => 'mark',
  421. 'password' => 'secret'
  422. ],
  423. ]
  424. ];
  425. $marshall = new Marshaller($this->comments);
  426. $result = $marshall->one($data, ['associated' => ['Articles.Users']]);
  427. $this->assertEquals(
  428. $data['article']['title'],
  429. $result->article->title
  430. );
  431. $this->assertEquals(
  432. $data['article']['user']['username'],
  433. $result->article->user->username
  434. );
  435. }
  436. /**
  437. * Test many() with a simple set of data.
  438. *
  439. * @return void
  440. */
  441. public function testManySimple()
  442. {
  443. $data = [
  444. ['comment' => 'First post', 'user_id' => 2],
  445. ['comment' => 'Second post', 'user_id' => 2],
  446. ];
  447. $marshall = new Marshaller($this->comments);
  448. $result = $marshall->many($data);
  449. $this->assertCount(2, $result);
  450. $this->assertInstanceOf('Cake\ORM\Entity', $result[0]);
  451. $this->assertInstanceOf('Cake\ORM\Entity', $result[1]);
  452. $this->assertEquals($data[0]['comment'], $result[0]->comment);
  453. $this->assertEquals($data[1]['comment'], $result[1]->comment);
  454. }
  455. /**
  456. * test many() with nested associations.
  457. *
  458. * @return void
  459. */
  460. public function testManyAssociations()
  461. {
  462. $data = [
  463. [
  464. 'comment' => 'First post',
  465. 'user_id' => 2,
  466. 'user' => [
  467. 'username' => 'mark',
  468. ],
  469. ],
  470. [
  471. 'comment' => 'Second post',
  472. 'user_id' => 2,
  473. 'user' => [
  474. 'username' => 'jose',
  475. ],
  476. ],
  477. ];
  478. $marshall = new Marshaller($this->comments);
  479. $result = $marshall->many($data, ['associated' => ['Users']]);
  480. $this->assertCount(2, $result);
  481. $this->assertInstanceOf('Cake\ORM\Entity', $result[0]);
  482. $this->assertInstanceOf('Cake\ORM\Entity', $result[1]);
  483. $this->assertEquals(
  484. $data[0]['user']['username'],
  485. $result[0]->user->username
  486. );
  487. $this->assertEquals(
  488. $data[1]['user']['username'],
  489. $result[1]->user->username
  490. );
  491. }
  492. /**
  493. * Test generating a list of entities from a list of ids.
  494. *
  495. * @return void
  496. */
  497. public function testOneGenerateBelongsToManyEntitiesFromIds()
  498. {
  499. $data = [
  500. 'title' => 'Haz tags',
  501. 'body' => 'Some content here',
  502. 'tags' => ['_ids' => '']
  503. ];
  504. $marshall = new Marshaller($this->articles);
  505. $result = $marshall->one($data, ['associated' => ['Tags']]);
  506. $this->assertCount(0, $result->tags);
  507. $data = [
  508. 'title' => 'Haz tags',
  509. 'body' => 'Some content here',
  510. 'tags' => ['_ids' => false]
  511. ];
  512. $result = $marshall->one($data, ['associated' => ['Tags']]);
  513. $this->assertCount(0, $result->tags);
  514. $data = [
  515. 'title' => 'Haz tags',
  516. 'body' => 'Some content here',
  517. 'tags' => ['_ids' => null]
  518. ];
  519. $result = $marshall->one($data, ['associated' => ['Tags']]);
  520. $this->assertCount(0, $result->tags);
  521. $data = [
  522. 'title' => 'Haz tags',
  523. 'body' => 'Some content here',
  524. 'tags' => ['_ids' => [1, 2, 3]]
  525. ];
  526. $marshall = new Marshaller($this->articles);
  527. $result = $marshall->one($data, ['associated' => ['Tags']]);
  528. $this->assertCount(3, $result->tags);
  529. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  530. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  531. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  532. }
  533. /**
  534. * Test merge() in a simple use.
  535. *
  536. * @return void
  537. */
  538. public function testMergeSimple()
  539. {
  540. $data = [
  541. 'title' => 'My title',
  542. 'author_id' => 1,
  543. 'not_in_schema' => true
  544. ];
  545. $marshall = new Marshaller($this->articles);
  546. $entity = new Entity([
  547. 'title' => 'Foo',
  548. 'body' => 'My Content'
  549. ]);
  550. $entity->accessible('*', true);
  551. $entity->isNew(false);
  552. $entity->clean();
  553. $result = $marshall->merge($entity, $data, []);
  554. $this->assertSame($entity, $result);
  555. $this->assertEquals($data + ['body' => 'My Content'], $result->toArray());
  556. $this->assertTrue($result->dirty(), 'Should be a dirty entity.');
  557. $this->assertFalse($result->isNew(), 'Should not change the entity state');
  558. }
  559. /**
  560. * Provides empty values.
  561. *
  562. * @return array
  563. */
  564. public function emptyProvider()
  565. {
  566. return [
  567. [0],
  568. ['0'],
  569. ];
  570. }
  571. /**
  572. * Test merging empty values into an entity.
  573. *
  574. * @dataProvider emptyProvider
  575. * @return void
  576. */
  577. public function testMergeFalseyValues($value)
  578. {
  579. $marshall = new Marshaller($this->articles);
  580. $entity = new Entity();
  581. $entity->accessible('*', true);
  582. $entity = $marshall->merge($entity, ['author_id' => $value]);
  583. $this->assertTrue($entity->dirty('author_id'), 'Field should be dirty');
  584. $this->assertSame(0, $entity->get('author_id'), 'Value should be zero');
  585. }
  586. /**
  587. * Tests that merge respects the entity accessible methods
  588. *
  589. * @return void
  590. */
  591. public function testMergeWhitelist()
  592. {
  593. $data = [
  594. 'title' => 'My title',
  595. 'author_id' => 1,
  596. 'not_in_schema' => true
  597. ];
  598. $marshall = new Marshaller($this->articles);
  599. $entity = new Entity([
  600. 'title' => 'Foo',
  601. 'body' => 'My Content'
  602. ]);
  603. $entity->accessible('*', false);
  604. $entity->accessible('author_id', true);
  605. $entity->isNew(false);
  606. $result = $marshall->merge($entity, $data, []);
  607. $expected = [
  608. 'title' => 'Foo',
  609. 'body' => 'My Content',
  610. 'author_id' => 1
  611. ];
  612. $this->assertEquals($expected, $result->toArray());
  613. }
  614. /**
  615. * Tests that fields with the same value are not marked as dirty
  616. *
  617. * @return void
  618. */
  619. public function testMergeDirty()
  620. {
  621. $marshall = new Marshaller($this->articles);
  622. $entity = new Entity([
  623. 'title' => 'Foo',
  624. 'author_id' => 1
  625. ]);
  626. $data = [
  627. 'title' => 'Foo',
  628. 'author_id' => 1,
  629. 'crazy' => true
  630. ];
  631. $entity->accessible('*', true);
  632. $entity->clean();
  633. $result = $marshall->merge($entity, $data, []);
  634. $expected = [
  635. 'title' => 'Foo',
  636. 'author_id' => 1,
  637. 'crazy' => true
  638. ];
  639. $this->assertEquals($expected, $result->toArray());
  640. $this->assertFalse($entity->dirty('title'));
  641. $this->assertFalse($entity->dirty('author_id'));
  642. $this->assertTrue($entity->dirty('crazy'));
  643. }
  644. /**
  645. * Tests merging data into an associated entity
  646. *
  647. * @return void
  648. */
  649. public function testMergeWithSingleAssociation()
  650. {
  651. $user = new Entity([
  652. 'username' => 'mark',
  653. 'password' => 'secret'
  654. ]);
  655. $entity = new Entity([
  656. 'tile' => 'My Title',
  657. 'user' => $user
  658. ]);
  659. $user->accessible('*', true);
  660. $entity->accessible('*', true);
  661. $data = [
  662. 'body' => 'My Content',
  663. 'user' => [
  664. 'password' => 'not a secret'
  665. ]
  666. ];
  667. $marshall = new Marshaller($this->articles);
  668. $marshall->merge($entity, $data, ['associated' => ['Users']]);
  669. $this->assertEquals('My Content', $entity->body);
  670. $this->assertSame($user, $entity->user);
  671. $this->assertEquals('mark', $entity->user->username);
  672. $this->assertEquals('not a secret', $entity->user->password);
  673. $this->assertTrue($entity->dirty('user'));
  674. }
  675. /**
  676. * Tests that new associated entities can be created when merging data into
  677. * a parent entity
  678. *
  679. * @return void
  680. */
  681. public function testMergeCreateAssociation()
  682. {
  683. $entity = new Entity([
  684. 'tile' => 'My Title'
  685. ]);
  686. $entity->accessible('*', true);
  687. $data = [
  688. 'body' => 'My Content',
  689. 'user' => [
  690. 'username' => 'mark',
  691. 'password' => 'not a secret'
  692. ]
  693. ];
  694. $marshall = new Marshaller($this->articles);
  695. $marshall->merge($entity, $data, ['associated' => ['Users']]);
  696. $this->assertEquals('My Content', $entity->body);
  697. $this->assertInstanceOf('Cake\ORM\Entity', $entity->user);
  698. $this->assertEquals('mark', $entity->user->username);
  699. $this->assertEquals('not a secret', $entity->user->password);
  700. $this->assertTrue($entity->dirty('user'));
  701. $this->assertTrue($entity->user->isNew());
  702. }
  703. /**
  704. * Tests merging one to many associations
  705. *
  706. * @return void
  707. */
  708. public function testMergeMultipleAssociations()
  709. {
  710. $user = new Entity(['username' => 'mark', 'password' => 'secret']);
  711. $comment1 = new Entity(['id' => 1, 'comment' => 'A comment']);
  712. $comment2 = new Entity(['id' => 2, 'comment' => 'Another comment']);
  713. $entity = new Entity([
  714. 'title' => 'My Title',
  715. 'user' => $user,
  716. 'comments' => [$comment1, $comment2]
  717. ]);
  718. $user->accessible('*', true);
  719. $comment1->accessible('*', true);
  720. $comment2->accessible('*', true);
  721. $entity->accessible('*', true);
  722. $data = [
  723. 'title' => 'Another title',
  724. 'user' => ['password' => 'not so secret'],
  725. 'comments' => [
  726. ['comment' => 'Extra comment 1'],
  727. ['id' => 2, 'comment' => 'Altered comment 2'],
  728. ['id' => 1, 'comment' => 'Altered comment 1'],
  729. ['id' => 3, 'comment' => 'Extra comment 3'],
  730. ['id' => 4, 'comment' => 'Extra comment 4'],
  731. ['comment' => 'Extra comment 2']
  732. ]
  733. ];
  734. $marshall = new Marshaller($this->articles);
  735. $result = $marshall->merge($entity, $data, ['associated' => ['Users', 'Comments']]);
  736. $this->assertSame($entity, $result);
  737. $this->assertSame($user, $result->user);
  738. $this->assertEquals('not so secret', $entity->user->password);
  739. $this->assertSame($comment1, $entity->comments[0]);
  740. $this->assertSame($comment2, $entity->comments[1]);
  741. $this->assertEquals('Altered comment 1', $entity->comments[0]->comment);
  742. $this->assertEquals('Altered comment 2', $entity->comments[1]->comment);
  743. $thirdComment = $this->articles->Comments
  744. ->find()
  745. ->where(['id' => 3])
  746. ->hydrate(false)
  747. ->first();
  748. $this->assertEquals(
  749. ['comment' => 'Extra comment 3'] + $thirdComment,
  750. $entity->comments[2]->toArray()
  751. );
  752. $forthComment = $this->articles->Comments
  753. ->find()
  754. ->where(['id' => 4])
  755. ->hydrate(false)
  756. ->first();
  757. $this->assertEquals(
  758. ['comment' => 'Extra comment 4'] + $forthComment,
  759. $entity->comments[3]->toArray()
  760. );
  761. $this->assertEquals(
  762. ['comment' => 'Extra comment 1'],
  763. $entity->comments[4]->toArray()
  764. );
  765. $this->assertEquals(
  766. ['comment' => 'Extra comment 2'],
  767. $entity->comments[5]->toArray()
  768. );
  769. }
  770. /**
  771. * Tests that merging data to an entity containing belongsToMany and _ids
  772. * will just overwrite the data
  773. *
  774. * @return void
  775. */
  776. public function testMergeBelongsToManyEntitiesFromIds()
  777. {
  778. $entity = new Entity([
  779. 'title' => 'Haz tags',
  780. 'body' => 'Some content here',
  781. 'tags' => [
  782. new Entity(['id' => 1, 'name' => 'Cake']),
  783. new Entity(['id' => 2, 'name' => 'PHP'])
  784. ]
  785. ]);
  786. $data = [
  787. 'title' => 'Haz moar tags',
  788. 'tags' => ['_ids' => [1, 2, 3]]
  789. ];
  790. $entity->accessible('*', true);
  791. $marshall = new Marshaller($this->articles);
  792. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  793. $this->assertCount(3, $result->tags);
  794. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  795. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  796. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  797. }
  798. /**
  799. * Tests that merging data to an entity containing belongsToMany and _ids
  800. * will not generate conflicting queries when associations are automatically selected
  801. *
  802. * @return void
  803. */
  804. public function testMergeFromIdsWithAutoAssociation()
  805. {
  806. $entity = new Entity([
  807. 'title' => 'Haz tags',
  808. 'body' => 'Some content here',
  809. 'tags' => [
  810. new Entity(['id' => 1, 'name' => 'Cake']),
  811. new Entity(['id' => 2, 'name' => 'PHP'])
  812. ]
  813. ]);
  814. $data = [
  815. 'title' => 'Haz moar tags',
  816. 'tags' => ['_ids' => [1, 2, 3]]
  817. ];
  818. $entity->accessible('*', true);
  819. // Adding a forced join to have another table with the same column names
  820. $this->articles->Tags->eventManager()->attach(function ($e, $query) {
  821. $left = new IdentifierExpression('Tags.id');
  822. $right = new IdentifierExpression('a.id');
  823. $query->leftJoin(['a' => 'tags'], $query->newExpr()->eq($left, $right));
  824. }, 'Model.beforeFind');
  825. $marshall = new Marshaller($this->articles);
  826. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  827. $this->assertCount(3, $result->tags);
  828. }
  829. /**
  830. * Tests that merging data to an entity containing belongsToMany and _ids
  831. * with additional association conditions works.
  832. *
  833. * @return void
  834. */
  835. public function testMergeBelongsToManyFromIdsWithConditions()
  836. {
  837. $this->articles->belongsToMany('Tags', [
  838. 'conditions' => ['ArticleTags.article_id' => 1]
  839. ]);
  840. $entity = new Entity([
  841. 'title' => 'No tags',
  842. 'body' => 'Some content here',
  843. 'tags' => []
  844. ]);
  845. $data = [
  846. 'title' => 'Haz moar tags',
  847. 'tags' => ['_ids' => [1, 2, 3]]
  848. ];
  849. $entity->accessible('*', true);
  850. $marshall = new Marshaller($this->articles);
  851. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  852. $this->assertCount(3, $result->tags);
  853. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  854. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  855. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  856. }
  857. /**
  858. * Tests that merging data to an entity containing belongsToMany and _ids
  859. * will ignore empty values.
  860. *
  861. * @return void
  862. */
  863. public function testMergeBelongsToManyEntitiesFromIdsEmptyValue()
  864. {
  865. $entity = new Entity([
  866. 'title' => 'Haz tags',
  867. 'body' => 'Some content here',
  868. 'tags' => [
  869. new Entity(['id' => 1, 'name' => 'Cake']),
  870. new Entity(['id' => 2, 'name' => 'PHP'])
  871. ]
  872. ]);
  873. $data = [
  874. 'title' => 'Haz moar tags',
  875. 'tags' => ['_ids' => '']
  876. ];
  877. $entity->accessible('*', true);
  878. $marshall = new Marshaller($this->articles);
  879. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  880. $this->assertCount(0, $result->tags);
  881. $data = [
  882. 'title' => 'Haz moar tags',
  883. 'tags' => ['_ids' => false]
  884. ];
  885. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  886. $this->assertCount(0, $result->tags);
  887. $data = [
  888. 'title' => 'Haz moar tags',
  889. 'tags' => ['_ids' => null]
  890. ];
  891. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  892. $this->assertCount(0, $result->tags);
  893. }
  894. /**
  895. * Test merging the _joinData entity for belongstomany associations.
  896. *
  897. * @return void
  898. */
  899. public function testMergeBelongsToManyJoinData()
  900. {
  901. $data = [
  902. 'title' => 'My title',
  903. 'body' => 'My content',
  904. 'author_id' => 1,
  905. 'tags' => [
  906. [
  907. 'id' => 1,
  908. 'tag' => 'news',
  909. '_joinData' => [
  910. 'active' => 0
  911. ]
  912. ],
  913. [
  914. 'id' => 2,
  915. 'tag' => 'cakephp',
  916. '_joinData' => [
  917. 'active' => 0
  918. ]
  919. ],
  920. ],
  921. ];
  922. $options = ['associated' => ['Tags' => ['associated' => ['_joinData']]]];
  923. $marshall = new Marshaller($this->articles);
  924. $entity = $marshall->one($data, $options);
  925. $entity->accessible('*', true);
  926. $data = [
  927. 'title' => 'Haz data',
  928. 'tags' => [
  929. ['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar']],
  930. ['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]
  931. ]
  932. ];
  933. $tag1 = $entity->tags[0];
  934. $result = $marshall->merge($entity, $data, $options);
  935. $this->assertEquals($data['title'], $result->title);
  936. $this->assertEquals('My content', $result->body);
  937. $this->assertSame($tag1, $entity->tags[0]);
  938. $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
  939. $this->assertSame(
  940. ['active' => 0, 'foo' => 'bar'],
  941. $entity->tags[0]->_joinData->toArray()
  942. );
  943. $this->assertSame(
  944. ['active' => 1, 'foo' => 'baz'],
  945. $entity->tags[1]->_joinData->toArray()
  946. );
  947. $this->assertEquals('new tag', $entity->tags[1]->tag);
  948. $this->assertTrue($entity->tags[0]->dirty('_joinData'));
  949. $this->assertTrue($entity->tags[1]->dirty('_joinData'));
  950. }
  951. /**
  952. * Test merging associations inside _joinData
  953. *
  954. * @return void
  955. */
  956. public function testMergeJoinDataAssociations()
  957. {
  958. $data = [
  959. 'title' => 'My title',
  960. 'body' => 'My content',
  961. 'author_id' => 1,
  962. 'tags' => [
  963. [
  964. 'id' => 1,
  965. 'tag' => 'news',
  966. '_joinData' => [
  967. 'active' => 0,
  968. 'user' => ['username' => 'Bill']
  969. ]
  970. ],
  971. [
  972. 'id' => 2,
  973. 'tag' => 'cakephp',
  974. '_joinData' => [
  975. 'active' => 0
  976. ]
  977. ],
  978. ]
  979. ];
  980. $articlesTags = TableRegistry::get('ArticlesTags');
  981. $articlesTags->belongsTo('Users');
  982. $options = ['associated' => ['Tags._joinData.Users']];
  983. $marshall = new Marshaller($this->articles);
  984. $entity = $marshall->one($data, $options);
  985. $entity->accessible('*', true);
  986. $data = [
  987. 'title' => 'Haz data',
  988. 'tags' => [
  989. [
  990. 'id' => 1,
  991. 'tag' => 'news',
  992. '_joinData' => [
  993. 'foo' => 'bar',
  994. 'user' => ['password' => 'secret']
  995. ]
  996. ],
  997. [
  998. 'id' => 2,
  999. '_joinData' => [
  1000. 'active' => 1,
  1001. 'foo' => 'baz',
  1002. 'user' => ['username' => 'ber']
  1003. ]
  1004. ]
  1005. ]
  1006. ];
  1007. $tag1 = $entity->tags[0];
  1008. $result = $marshall->merge($entity, $data, $options);
  1009. $this->assertEquals($data['title'], $result->title);
  1010. $this->assertEquals('My content', $result->body);
  1011. $this->assertSame($tag1, $entity->tags[0]);
  1012. $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
  1013. $this->assertEquals('Bill', $entity->tags[0]->_joinData->user->username);
  1014. $this->assertEquals('secret', $entity->tags[0]->_joinData->user->password);
  1015. $this->assertEquals('ber', $entity->tags[1]->_joinData->user->username);
  1016. }
  1017. /**
  1018. * Test mergeMany() with a simple set of data.
  1019. *
  1020. * @return void
  1021. */
  1022. public function testMergeManySimple()
  1023. {
  1024. $entities = [
  1025. new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2]),
  1026. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  1027. ];
  1028. $entities[0]->clean();
  1029. $entities[1]->clean();
  1030. $data = [
  1031. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  1032. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1]
  1033. ];
  1034. $marshall = new Marshaller($this->comments);
  1035. $result = $marshall->mergeMany($entities, $data);
  1036. $this->assertSame($entities[0], $result[0]);
  1037. $this->assertSame($entities[1], $result[1]);
  1038. $this->assertEquals('Changed 1', $result[0]->comment);
  1039. $this->assertEquals(1, $result[0]->user_id);
  1040. $this->assertEquals('Changed 2', $result[1]->comment);
  1041. $this->assertTrue($result[0]->dirty('user_id'));
  1042. $this->assertFalse($result[1]->dirty('user_id'));
  1043. }
  1044. /**
  1045. * Tests that only records found in the data array are returned, those that cannot
  1046. * be matched are discarded
  1047. *
  1048. * @return void
  1049. */
  1050. public function testMergeManyWithAppend()
  1051. {
  1052. $entities = [
  1053. new OpenEntity(['comment' => 'First post', 'user_id' => 2]),
  1054. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  1055. ];
  1056. $entities[0]->clean();
  1057. $entities[1]->clean();
  1058. $data = [
  1059. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  1060. ['id' => 1, 'comment' => 'Comment 1', 'user_id' => 1]
  1061. ];
  1062. $marshall = new Marshaller($this->comments);
  1063. $result = $marshall->mergeMany($entities, $data);
  1064. $this->assertCount(2, $result);
  1065. $this->assertNotSame($entities[0], $result[0]);
  1066. $this->assertSame($entities[1], $result[0]);
  1067. $this->assertEquals('Changed 2', $result[0]->comment);
  1068. $this->assertEquals('Comment 1', $result[1]->comment);
  1069. }
  1070. /**
  1071. * Test that mergeMany() handles composite key associations properly.
  1072. *
  1073. * The articles_tags table has a composite primary key, and should be
  1074. * handled correctly.
  1075. *
  1076. * @return void
  1077. */
  1078. public function testMergeManyCompositeKey()
  1079. {
  1080. $articlesTags = TableRegistry::get('ArticlesTags');
  1081. $entities = [
  1082. new OpenEntity(['article_id' => 1, 'tag_id' => 2]),
  1083. new OpenEntity(['article_id' => 1, 'tag_id' => 1]),
  1084. ];
  1085. $entities[0]->clean();
  1086. $entities[1]->clean();
  1087. $data = [
  1088. ['article_id' => 1, 'tag_id' => 1],
  1089. ['article_id' => 1, 'tag_id' => 2]
  1090. ];
  1091. $marshall = new Marshaller($articlesTags);
  1092. $result = $marshall->mergeMany($entities, $data);
  1093. $this->assertCount(2, $result, 'Should have two records');
  1094. $this->assertSame($entities[0], $result[0], 'Should retain object');
  1095. $this->assertSame($entities[1], $result[1], 'Should retain object');
  1096. }
  1097. /**
  1098. * Tests merge with data types that need to be marshalled
  1099. *
  1100. * @return void
  1101. */
  1102. public function testMergeComplexType()
  1103. {
  1104. $entity = new Entity(
  1105. ['comment' => 'My Comment text'],
  1106. ['markNew' => false, 'markClean' => true]
  1107. );
  1108. $data = [
  1109. 'created' => [
  1110. 'year' => '2014',
  1111. 'month' => '2',
  1112. 'day' => 14
  1113. ]
  1114. ];
  1115. $marshall = new Marshaller($this->comments);
  1116. $result = $marshall->merge($entity, $data);
  1117. $this->assertInstanceOf('DateTime', $entity->created);
  1118. $this->assertEquals('2014-02-14', $entity->created->format('Y-m-d'));
  1119. }
  1120. /**
  1121. * Tests that it is possible to pass a fieldList option to the marshaller
  1122. *
  1123. * @return void
  1124. */
  1125. public function testOneWithFieldList()
  1126. {
  1127. $data = [
  1128. 'title' => 'My title',
  1129. 'body' => 'My content',
  1130. 'author_id' => 1
  1131. ];
  1132. $marshall = new Marshaller($this->articles);
  1133. $result = $marshall->one($data, ['fieldList' => ['title', 'author_id']]);
  1134. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  1135. unset($data['body']);
  1136. $this->assertEquals($data, $result->toArray());
  1137. }
  1138. /**
  1139. * Tests that it is possible to pass a fieldList option to the merge method
  1140. *
  1141. * @return void
  1142. */
  1143. public function testMergeWithFieldList()
  1144. {
  1145. $data = [
  1146. 'title' => 'My title',
  1147. 'author_id' => 1
  1148. ];
  1149. $marshall = new Marshaller($this->articles);
  1150. $entity = new Entity([
  1151. 'title' => 'Foo',
  1152. 'body' => 'My Content',
  1153. 'author_id' => 2
  1154. ]);
  1155. $entity->accessible('*', false);
  1156. $entity->isNew(false);
  1157. $entity->clean();
  1158. $result = $marshall->merge($entity, $data, ['fieldList' => ['title', 'body']]);
  1159. $expected = [
  1160. 'title' => 'My title',
  1161. 'body' => 'My Content',
  1162. 'author_id' => 2
  1163. ];
  1164. $this->assertSame($entity, $result);
  1165. $this->assertEquals($expected, $result->toArray());
  1166. $this->assertFalse($entity->accessible('*'));
  1167. }
  1168. /**
  1169. * Test that many() also receives a fieldList option
  1170. *
  1171. * @return void
  1172. */
  1173. public function testManyFieldList()
  1174. {
  1175. $data = [
  1176. ['comment' => 'First post', 'user_id' => 2, 'foo' => 'bar'],
  1177. ['comment' => 'Second post', 'user_id' => 2, 'foo' => 'bar'],
  1178. ];
  1179. $marshall = new Marshaller($this->comments);
  1180. $result = $marshall->many($data, ['fieldList' => ['comment', 'user_id']]);
  1181. $this->assertCount(2, $result);
  1182. unset($data[0]['foo'], $data[1]['foo']);
  1183. $this->assertEquals($data[0], $result[0]->toArray());
  1184. $this->assertEquals($data[1], $result[1]->toArray());
  1185. }
  1186. /**
  1187. * Test that many() also receives a fieldList option
  1188. *
  1189. * @return void
  1190. */
  1191. public function testMergeManyFieldList()
  1192. {
  1193. $entities = [
  1194. new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2]),
  1195. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  1196. ];
  1197. $entities[0]->clean();
  1198. $entities[1]->clean();
  1199. $data = [
  1200. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 10],
  1201. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 20]
  1202. ];
  1203. $marshall = new Marshaller($this->comments);
  1204. $result = $marshall->mergeMany($entities, $data, ['fieldList' => ['id', 'comment']]);
  1205. $this->assertSame($entities[0], $result[0]);
  1206. $this->assertSame($entities[1], $result[1]);
  1207. $expected = ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2];
  1208. $this->assertEquals($expected, $entities[1]->toArray());
  1209. $expected = ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 2];
  1210. $this->assertEquals($expected, $entities[0]->toArray());
  1211. }
  1212. /**
  1213. * test marshalling association data while passing a fieldList
  1214. *
  1215. * @return void
  1216. */
  1217. public function testAssociatoinsFieldList()
  1218. {
  1219. $data = [
  1220. 'title' => 'My title',
  1221. 'body' => 'My content',
  1222. 'author_id' => 1,
  1223. 'user' => [
  1224. 'username' => 'mark',
  1225. 'password' => 'secret',
  1226. 'foo' => 'bar'
  1227. ]
  1228. ];
  1229. $marshall = new Marshaller($this->articles);
  1230. $result = $marshall->one($data, [
  1231. 'fieldList' => ['title', 'body', 'user'],
  1232. 'associated' => [
  1233. 'Users' => ['fieldList' => ['username', 'foo']]
  1234. ]
  1235. ]);
  1236. $this->assertEquals($data['title'], $result->title);
  1237. $this->assertEquals($data['body'], $result->body);
  1238. $this->assertNull($result->author_id);
  1239. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  1240. $this->assertEquals($data['user']['username'], $result->user->username);
  1241. $this->assertNull($result->user->password);
  1242. }
  1243. /**
  1244. * Tests merging associated data with a fieldList
  1245. *
  1246. * @return void
  1247. */
  1248. public function testMergeAssociationWithfieldList()
  1249. {
  1250. $user = new Entity([
  1251. 'username' => 'mark',
  1252. 'password' => 'secret'
  1253. ]);
  1254. $entity = new Entity([
  1255. 'tile' => 'My Title',
  1256. 'user' => $user
  1257. ]);
  1258. $user->accessible('*', true);
  1259. $entity->accessible('*', true);
  1260. $data = [
  1261. 'body' => 'My Content',
  1262. 'something' => 'else',
  1263. 'user' => [
  1264. 'password' => 'not a secret',
  1265. 'extra' => 'data'
  1266. ]
  1267. ];
  1268. $marshall = new Marshaller($this->articles);
  1269. $marshall->merge($entity, $data, [
  1270. 'fieldList' => ['something'],
  1271. 'associated' => ['Users' => ['fieldList' => ['extra']]]
  1272. ]);
  1273. $this->assertNull($entity->body);
  1274. $this->assertEquals('else', $entity->something);
  1275. $this->assertSame($user, $entity->user);
  1276. $this->assertEquals('mark', $entity->user->username);
  1277. $this->assertEquals('secret', $entity->user->password);
  1278. $this->assertEquals('data', $entity->user->extra);
  1279. $this->assertTrue($entity->dirty('user'));
  1280. }
  1281. /**
  1282. * Test marshalling nested associations on the _joinData structure
  1283. * while having a fieldList
  1284. *
  1285. * @return void
  1286. */
  1287. public function testJoinDataWhiteList()
  1288. {
  1289. $data = [
  1290. 'title' => 'My title',
  1291. 'body' => 'My content',
  1292. 'author_id' => 1,
  1293. 'tags' => [
  1294. [
  1295. 'tag' => 'news',
  1296. '_joinData' => [
  1297. 'active' => 1,
  1298. 'crazy' => 'data',
  1299. 'user' => ['username' => 'Bill'],
  1300. ]
  1301. ],
  1302. [
  1303. 'tag' => 'cakephp',
  1304. '_joinData' => [
  1305. 'active' => 0,
  1306. 'crazy' => 'stuff',
  1307. 'user' => ['username' => 'Mark'],
  1308. ]
  1309. ],
  1310. ],
  1311. ];
  1312. $articlesTags = TableRegistry::get('ArticlesTags');
  1313. $articlesTags->belongsTo('Users');
  1314. $marshall = new Marshaller($this->articles);
  1315. $result = $marshall->one($data, [
  1316. 'associated' => [
  1317. 'Tags._joinData' => ['fieldList' => ['active', 'user']],
  1318. 'Tags._joinData.Users'
  1319. ]
  1320. ]);
  1321. $this->assertInstanceOf(
  1322. 'Cake\ORM\Entity',
  1323. $result->tags[0]->_joinData->user,
  1324. 'joinData should contain a user entity.'
  1325. );
  1326. $this->assertEquals('Bill', $result->tags[0]->_joinData->user->username);
  1327. $this->assertInstanceOf(
  1328. 'Cake\ORM\Entity',
  1329. $result->tags[1]->_joinData->user,
  1330. 'joinData should contain a user entity.'
  1331. );
  1332. $this->assertEquals('Mark', $result->tags[1]->_joinData->user->username);
  1333. $this->assertNull($result->tags[0]->_joinData->crazy);
  1334. $this->assertNull($result->tags[1]->_joinData->crazy);
  1335. }
  1336. /**
  1337. * Test merging the _joinData entity for belongstomany associations
  1338. * while passing a whitelist
  1339. *
  1340. * @return void
  1341. */
  1342. public function testMergeJoinDataWithFieldList()
  1343. {
  1344. $data = [
  1345. 'title' => 'My title',
  1346. 'body' => 'My content',
  1347. 'author_id' => 1,
  1348. 'tags' => [
  1349. [
  1350. 'id' => 1,
  1351. 'tag' => 'news',
  1352. '_joinData' => [
  1353. 'active' => 0
  1354. ]
  1355. ],
  1356. [
  1357. 'id' => 2,
  1358. 'tag' => 'cakephp',
  1359. '_joinData' => [
  1360. 'active' => 0
  1361. ]
  1362. ],
  1363. ],
  1364. ];
  1365. $options = ['associated' => ['Tags' => ['associated' => ['_joinData']]]];
  1366. $marshall = new Marshaller($this->articles);
  1367. $entity = $marshall->one($data, $options);
  1368. $entity->accessible('*', true);
  1369. $data = [
  1370. 'title' => 'Haz data',
  1371. 'tags' => [
  1372. ['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar', 'crazy' => 'something']],
  1373. ['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]
  1374. ]
  1375. ];
  1376. $tag1 = $entity->tags[0];
  1377. $result = $marshall->merge($entity, $data, [
  1378. 'associated' => ['Tags._joinData' => ['fieldList' => ['foo']]]
  1379. ]);
  1380. $this->assertEquals($data['title'], $result->title);
  1381. $this->assertEquals('My content', $result->body);
  1382. $this->assertSame($tag1, $entity->tags[0]);
  1383. $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
  1384. $this->assertSame(
  1385. ['active' => 0, 'foo' => 'bar'],
  1386. $entity->tags[0]->_joinData->toArray()
  1387. );
  1388. $this->assertSame(
  1389. ['foo' => 'baz'],
  1390. $entity->tags[1]->_joinData->toArray()
  1391. );
  1392. $this->assertEquals('new tag', $entity->tags[1]->tag);
  1393. $this->assertTrue($entity->tags[0]->dirty('_joinData'));
  1394. $this->assertTrue($entity->tags[1]->dirty('_joinData'));
  1395. }
  1396. /**
  1397. * Tests marshalling with validation errors
  1398. *
  1399. * @return void
  1400. */
  1401. public function testValidationFail()
  1402. {
  1403. $data = [
  1404. 'title' => 'Thing',
  1405. 'body' => 'hey'
  1406. ];
  1407. $this->articles->validator()->requirePresence('thing');
  1408. $marshall = new Marshaller($this->articles);
  1409. $entity = $marshall->one($data);
  1410. $this->assertNotEmpty($entity->errors('thing'));
  1411. }
  1412. /**
  1413. * Test that invalid validate options raise exceptions
  1414. *
  1415. * @expectedException \RuntimeException
  1416. * @return void
  1417. */
  1418. public function testValidateInvalidType()
  1419. {
  1420. $data = ['title' => 'foo'];
  1421. $marshaller = new Marshaller($this->articles);
  1422. $marshaller->one($data, [
  1423. 'validate' => ['derp'],
  1424. ]);
  1425. }
  1426. /**
  1427. * Tests that associations are validated and custom validators can be used
  1428. *
  1429. * @return void
  1430. */
  1431. public function testValidateWithAssociationsAndCustomValidator()
  1432. {
  1433. $data = [
  1434. 'title' => 'foo',
  1435. 'body' => 'bar',
  1436. 'user' => [
  1437. 'name' => 'Susan'
  1438. ],
  1439. 'comments' => [
  1440. [
  1441. 'comment' => 'foo'
  1442. ]
  1443. ]
  1444. ];
  1445. $validator = (new Validator)->add('body', 'numeric', ['rule' => 'numeric']);
  1446. $this->articles->validator('custom', $validator);
  1447. $validator2 = (new Validator)->requirePresence('thing');
  1448. $this->articles->Users->validator('customThing', $validator2);
  1449. $this->articles->Comments->validator('default', $validator2);
  1450. $entity = (new Marshaller($this->articles))->one($data, [
  1451. 'validate' => 'custom',
  1452. 'associated' => ['Users', 'Comments']
  1453. ]);
  1454. $this->assertNotEmpty($entity->errors('body'), 'custom was not used');
  1455. $this->assertNull($entity->body);
  1456. $this->assertEmpty($entity->user->errors('thing'));
  1457. $this->assertNotEmpty($entity->comments[0]->errors('thing'));
  1458. $entity = (new Marshaller($this->articles))->one($data, [
  1459. 'validate' => 'custom',
  1460. 'associated' => ['Users' => ['validate' => 'customThing'], 'Comments']
  1461. ]);
  1462. $this->assertNotEmpty($entity->errors('body'));
  1463. $this->assertNull($entity->body);
  1464. $this->assertNotEmpty($entity->user->errors('thing'), 'customThing was not used');
  1465. $this->assertNotEmpty($entity->comments[0]->errors('thing'));
  1466. }
  1467. /**
  1468. * Tests that validation can be bypassed
  1469. *
  1470. * @return void
  1471. */
  1472. public function testSkipValidation()
  1473. {
  1474. $data = [
  1475. 'title' => 'foo',
  1476. 'body' => 'bar',
  1477. 'user' => [
  1478. 'name' => 'Susan'
  1479. ],
  1480. ];
  1481. $validator = (new Validator)->requirePresence('thing');
  1482. $this->articles->validator('default', $validator);
  1483. $this->articles->Users->validator('default', $validator);
  1484. $entity = (new Marshaller($this->articles))->one($data, [
  1485. 'validate' => false,
  1486. 'associated' => ['Users']
  1487. ]);
  1488. $this->assertEmpty($entity->errors('thing'));
  1489. $this->assertNotEmpty($entity->user->errors('thing'));
  1490. $entity = (new Marshaller($this->articles))->one($data, [
  1491. 'associated' => ['Users' => ['validate' => false]]
  1492. ]);
  1493. $this->assertNotEmpty($entity->errors('thing'));
  1494. $this->assertEmpty($entity->user->errors('thing'));
  1495. }
  1496. /**
  1497. * Tests that it is possible to pass a validator directly in the options
  1498. *
  1499. * @return void
  1500. */
  1501. public function testPassingCustomValidator()
  1502. {
  1503. $data = [
  1504. 'title' => 'Thing',
  1505. 'body' => 'hey'
  1506. ];
  1507. $validator = clone $this->articles->validator();
  1508. $validator->requirePresence('thing');
  1509. $marshall = new Marshaller($this->articles);
  1510. $entity = $marshall->one($data, ['validate' => $validator]);
  1511. $this->assertNotEmpty($entity->errors('thing'));
  1512. }
  1513. /**
  1514. * Test merge with validation error
  1515. *
  1516. * @return void
  1517. */
  1518. public function testMergeWithValidation()
  1519. {
  1520. $data = [
  1521. 'title' => 'My title',
  1522. 'author_id' => 'foo',
  1523. ];
  1524. $marshall = new Marshaller($this->articles);
  1525. $entity = new Entity([
  1526. 'id' => 1,
  1527. 'title' => 'Foo',
  1528. 'body' => 'My Content',
  1529. 'author_id' => 1
  1530. ]);
  1531. $entity->accessible('*', true);
  1532. $entity->isNew(false);
  1533. $entity->clean();
  1534. $this->articles->validator()
  1535. ->requirePresence('thing', 'update')
  1536. ->requirePresence('id', 'update')
  1537. ->add('author_id', 'numeric', ['rule' => 'numeric'])
  1538. ->add('id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);
  1539. $expected = clone $entity;
  1540. $result = $marshall->merge($expected, $data, []);
  1541. $this->assertSame($expected, $result);
  1542. $this->assertSame(1, $result->author_id);
  1543. $this->assertNotEmpty($result->errors('thing'));
  1544. $this->assertEmpty($result->errors('id'));
  1545. $this->articles->validator()->requirePresence('thing', 'create');
  1546. $result = $marshall->merge($entity, $data, []);
  1547. $this->assertEmpty($result->errors('thing'));
  1548. }
  1549. /**
  1550. * Test merge with validation and create or update validation rules
  1551. *
  1552. * @return void
  1553. */
  1554. public function testMergeWithCreate()
  1555. {
  1556. $data = [
  1557. 'title' => 'My title',
  1558. 'author_id' => 'foo',
  1559. ];
  1560. $marshall = new Marshaller($this->articles);
  1561. $entity = new Entity([
  1562. 'title' => 'Foo',
  1563. 'body' => 'My Content',
  1564. 'author_id' => 1
  1565. ]);
  1566. $entity->accessible('*', true);
  1567. $entity->isNew(true);
  1568. $entity->clean();
  1569. $this->articles->validator()
  1570. ->requirePresence('thing', 'update')
  1571. ->add('author_id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);
  1572. $expected = clone $entity;
  1573. $result = $marshall->merge($expected, $data, []);
  1574. $this->assertEmpty($result->errors('author_id'));
  1575. $this->assertEmpty($result->errors('thing'));
  1576. $entity->clean();
  1577. $entity->isNew(false);
  1578. $result = $marshall->merge($entity, $data, []);
  1579. $this->assertNotEmpty($result->errors('author_id'));
  1580. $this->assertNotEmpty($result->errors('thing'));
  1581. }
  1582. }