MarshallerTest.php 67 KB

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