MarshallerTest.php 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Database\Expression\IdentifierExpression;
  17. use Cake\Event\Event;
  18. use Cake\I18n\Time;
  19. use Cake\ORM\Entity;
  20. use Cake\ORM\Marshaller;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Validation\Validator;
  25. /**
  26. * Test entity for mass assignment.
  27. */
  28. class OpenEntity extends Entity
  29. {
  30. protected $_accessible = [
  31. '*' => true,
  32. ];
  33. }
  34. /**
  35. * Test entity for mass assignment.
  36. */
  37. class Tag extends Entity
  38. {
  39. protected $_accessible = [
  40. 'tag' => true,
  41. ];
  42. }
  43. /**
  44. * Test entity for mass assignment.
  45. */
  46. class ProtectedArticle extends Entity
  47. {
  48. protected $_accessible = [
  49. 'title' => true,
  50. 'body' => true
  51. ];
  52. }
  53. /**
  54. * Test stub for greedy find operations.
  55. */
  56. class GreedyCommentsTable extends Table
  57. {
  58. /**
  59. * initialize hook
  60. *
  61. * @param array $config Config data.
  62. * @return void
  63. */
  64. public function initialize(array $config)
  65. {
  66. $this->setTable('comments');
  67. $this->setAlias('Comments');
  68. }
  69. /**
  70. * Overload find to cause issues.
  71. *
  72. * @param string $type Find type
  73. * @param array $options find options
  74. * @return object
  75. */
  76. public function find($type = 'all', $options = [])
  77. {
  78. if (empty($options['conditions'])) {
  79. $options['conditions'] = [];
  80. }
  81. $options['conditions'] = array_merge($options['conditions'], ['Comments.published' => 'Y']);
  82. return parent::find($type, $options);
  83. }
  84. }
  85. /**
  86. * Marshaller test case
  87. */
  88. class MarshallerTest extends TestCase
  89. {
  90. public $fixtures = [
  91. 'core.articles',
  92. 'core.articles_tags',
  93. 'core.comments',
  94. 'core.special_tags',
  95. 'core.tags',
  96. 'core.users'
  97. ];
  98. /**
  99. * @var Table
  100. */
  101. protected $articles;
  102. /**
  103. * @var Table
  104. */
  105. protected $comments;
  106. /**
  107. * @var Table
  108. */
  109. protected $users;
  110. /**
  111. * @var Table
  112. */
  113. protected $tags;
  114. /**
  115. * @var Table
  116. */
  117. protected $articleTags;
  118. /**
  119. * setup
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. $this->articles = TableRegistry::get('Articles');
  127. $this->articles->belongsTo('Users', [
  128. 'foreignKey' => 'author_id'
  129. ]);
  130. $this->articles->hasMany('Comments');
  131. $this->articles->belongsToMany('Tags');
  132. $this->comments = TableRegistry::get('Comments');
  133. $this->users = TableRegistry::get('Users');
  134. $this->tags = TableRegistry::get('Tags');
  135. $this->articleTags = TableRegistry::get('ArticlesTags');
  136. $this->comments->belongsTo('Articles');
  137. $this->comments->belongsTo('Users');
  138. $this->articles->setEntityClass(__NAMESPACE__ . '\OpenEntity');
  139. $this->comments->setEntityClass(__NAMESPACE__ . '\OpenEntity');
  140. $this->users->setEntityClass(__NAMESPACE__ . '\OpenEntity');
  141. $this->tags->setEntityClass(__NAMESPACE__ . '\OpenEntity');
  142. $this->articleTags->setEntityClass(__NAMESPACE__ . '\OpenEntity');
  143. }
  144. /**
  145. * Teardown
  146. *
  147. * @return void
  148. */
  149. public function tearDown()
  150. {
  151. parent::tearDown();
  152. TableRegistry::clear();
  153. unset($this->articles, $this->comments, $this->users, $this->tags);
  154. }
  155. /**
  156. * Test one() in a simple use.
  157. *
  158. * @return void
  159. */
  160. public function testOneSimple()
  161. {
  162. $data = [
  163. 'title' => 'My title',
  164. 'body' => 'My content',
  165. 'author_id' => 1,
  166. 'not_in_schema' => true
  167. ];
  168. $marshall = new Marshaller($this->articles);
  169. $result = $marshall->one($data, []);
  170. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  171. $this->assertEquals($data, $result->toArray());
  172. $this->assertTrue($result->isDirty(), 'Should be a dirty entity.');
  173. $this->assertTrue($result->isNew(), 'Should be new');
  174. $this->assertEquals('Articles', $result->getSource());
  175. }
  176. /**
  177. * Test that marshalling an entity with '' for pk values results
  178. * in no pk value being set.
  179. *
  180. * @return void
  181. */
  182. public function testOneEmptyStringPrimaryKey()
  183. {
  184. $data = [
  185. 'id' => '',
  186. 'username' => 'superuser',
  187. 'password' => 'root',
  188. 'created' => new Time('2013-10-10 00:00'),
  189. 'updated' => new Time('2013-10-10 00:00')
  190. ];
  191. $marshall = new Marshaller($this->articles);
  192. $result = $marshall->one($data, []);
  193. $this->assertFalse($result->isDirty('id'));
  194. $this->assertNull($result->id);
  195. }
  196. /**
  197. * Test marshalling datetime/date field.
  198. *
  199. * @return void
  200. */
  201. public function testOneWithDatetimeField()
  202. {
  203. $data = [
  204. 'comment' => 'My Comment text',
  205. 'created' => [
  206. 'year' => '2014',
  207. 'month' => '2',
  208. 'day' => 14
  209. ]
  210. ];
  211. $marshall = new Marshaller($this->comments);
  212. $result = $marshall->one($data, []);
  213. $this->assertEquals(new Time('2014-02-14 00:00:00'), $result->created);
  214. $data['created'] = [
  215. 'year' => '2014',
  216. 'month' => '2',
  217. 'day' => 14,
  218. 'hour' => 9,
  219. 'minute' => 25,
  220. 'meridian' => 'pm'
  221. ];
  222. $result = $marshall->one($data, []);
  223. $this->assertEquals(new Time('2014-02-14 21:25:00'), $result->created);
  224. $data['created'] = [
  225. 'year' => '2014',
  226. 'month' => '2',
  227. 'day' => 14,
  228. 'hour' => 9,
  229. 'minute' => 25,
  230. ];
  231. $result = $marshall->one($data, []);
  232. $this->assertEquals(new Time('2014-02-14 09:25:00'), $result->created);
  233. $data['created'] = '2014-02-14 09:25:00';
  234. $result = $marshall->one($data, []);
  235. $this->assertEquals(new Time('2014-02-14 09:25:00'), $result->created);
  236. $data['created'] = 1392387900;
  237. $result = $marshall->one($data, []);
  238. $this->assertEquals($data['created'], $result->created->getTimestamp());
  239. }
  240. /**
  241. * Ensure that marshalling casts reasonably.
  242. *
  243. * @return void
  244. */
  245. public function testOneOnlyCastMatchingData()
  246. {
  247. $data = [
  248. 'title' => 'My title',
  249. 'body' => 'My content',
  250. 'author_id' => 'derp',
  251. 'created' => 'fale'
  252. ];
  253. $this->articles->setEntityClass(__NAMESPACE__ . '\OpenEntity');
  254. $marshall = new Marshaller($this->articles);
  255. $result = $marshall->one($data, []);
  256. $this->assertSame($data['title'], $result->title);
  257. $this->assertNull($result->author_id, 'No cast on bad data.');
  258. $this->assertSame($data['created'], $result->created, 'No cast on bad data.');
  259. }
  260. /**
  261. * Test one() follows mass-assignment rules.
  262. *
  263. * @return void
  264. */
  265. public function testOneAccessibleProperties()
  266. {
  267. $data = [
  268. 'title' => 'My title',
  269. 'body' => 'My content',
  270. 'author_id' => 1,
  271. 'not_in_schema' => true
  272. ];
  273. $this->articles->setEntityClass(__NAMESPACE__ . '\ProtectedArticle');
  274. $marshall = new Marshaller($this->articles);
  275. $result = $marshall->one($data, []);
  276. $this->assertInstanceOf(__NAMESPACE__ . '\ProtectedArticle', $result);
  277. $this->assertNull($result->author_id);
  278. $this->assertNull($result->not_in_schema);
  279. }
  280. /**
  281. * Test one() supports accessibleFields option
  282. *
  283. * @return void
  284. */
  285. public function testOneAccessibleFieldsOption()
  286. {
  287. $data = [
  288. 'title' => 'My title',
  289. 'body' => 'My content',
  290. 'author_id' => 1,
  291. 'not_in_schema' => true
  292. ];
  293. $this->articles->setEntityClass(__NAMESPACE__ . '\ProtectedArticle');
  294. $marshall = new Marshaller($this->articles);
  295. $result = $marshall->one($data, ['accessibleFields' => ['body' => false]]);
  296. $this->assertNull($result->body);
  297. $result = $marshall->one($data, ['accessibleFields' => ['author_id' => true]]);
  298. $this->assertEquals($data['author_id'], $result->author_id);
  299. $this->assertNull($result->not_in_schema);
  300. $result = $marshall->one($data, ['accessibleFields' => ['*' => true]]);
  301. $this->assertEquals($data['author_id'], $result->author_id);
  302. $this->assertTrue($result->not_in_schema);
  303. }
  304. /**
  305. * Test one() with an invalid association
  306. *
  307. * @return void
  308. */
  309. public function testOneInvalidAssociation()
  310. {
  311. $this->expectException(\InvalidArgumentException::class);
  312. $this->expectExceptionMessage('Cannot marshal data for "Derp" association. It is not associated with "Articles".');
  313. $data = [
  314. 'title' => 'My title',
  315. 'body' => 'My content',
  316. 'derp' => [
  317. 'id' => 1,
  318. 'username' => 'mark',
  319. ]
  320. ];
  321. $marshall = new Marshaller($this->articles);
  322. $marshall->one($data, [
  323. 'associated' => ['Derp']
  324. ]);
  325. }
  326. /**
  327. * Test that one() correctly handles an association beforeMarshal
  328. * making the association empty.
  329. *
  330. * @return void
  331. */
  332. public function testOneAssociationBeforeMarshalMutation()
  333. {
  334. $users = TableRegistry::get('Users');
  335. $articles = TableRegistry::get('Articles');
  336. $users->hasOne('Articles', [
  337. 'foreignKey' => 'author_id'
  338. ]);
  339. $articles->getEventManager()->on('Model.beforeMarshal', function ($event, $data, $options) {
  340. // Blank the association, so it doesn't become dirty.
  341. unset($data['not_a_real_field']);
  342. });
  343. $data = [
  344. 'username' => 'Jen',
  345. 'article' => [
  346. 'not_a_real_field' => 'whatever'
  347. ]
  348. ];
  349. $marshall = new Marshaller($users);
  350. $entity = $marshall->one($data, ['associated' => ['Articles']]);
  351. $this->assertTrue($entity->isDirty('username'));
  352. $this->assertFalse($entity->isDirty('article'));
  353. // Ensure consistency with merge()
  354. $entity = new Entity([
  355. 'username' => 'Jenny',
  356. ]);
  357. // Make the entity think it is new.
  358. $entity->setAccess('*', true);
  359. $entity->clean();
  360. $entity = $marshall->merge($entity, $data, ['associated' => ['Articles']]);
  361. $this->assertTrue($entity->isDirty('username'));
  362. $this->assertFalse($entity->isDirty('article'));
  363. }
  364. /**
  365. * Test one() supports accessibleFields option for associations
  366. *
  367. * @return void
  368. */
  369. public function testOneAccessibleFieldsOptionForAssociations()
  370. {
  371. $data = [
  372. 'title' => 'My title',
  373. 'body' => 'My content',
  374. 'user' => [
  375. 'id' => 1,
  376. 'username' => 'mark',
  377. ]
  378. ];
  379. $this->articles->setEntityClass(__NAMESPACE__ . '\ProtectedArticle');
  380. $this->users->setEntityClass(__NAMESPACE__ . '\ProtectedArticle');
  381. $marshall = new Marshaller($this->articles);
  382. $result = $marshall->one($data, [
  383. 'associated' => [
  384. 'Users' => ['accessibleFields' => ['id' => true]]
  385. ],
  386. 'accessibleFields' => ['body' => false, 'user' => true]
  387. ]);
  388. $this->assertNull($result->body);
  389. $this->assertNull($result->user->username);
  390. $this->assertEquals(1, $result->user->id);
  391. }
  392. /**
  393. * test one() with a wrapping model name.
  394. *
  395. * @return void
  396. */
  397. public function testOneWithAdditionalName()
  398. {
  399. $data = [
  400. 'title' => 'Original Title',
  401. 'Articles' => [
  402. 'title' => 'My title',
  403. 'body' => 'My content',
  404. 'author_id' => 1,
  405. 'not_in_schema' => true,
  406. 'user' => [
  407. 'username' => 'mark',
  408. ]
  409. ]
  410. ];
  411. $marshall = new Marshaller($this->articles);
  412. $result = $marshall->one($data, ['associated' => ['Users']]);
  413. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  414. $this->assertTrue($result->isDirty(), 'Should be a dirty entity.');
  415. $this->assertTrue($result->isNew(), 'Should be new');
  416. $this->assertFalse($result->has('Articles'), 'No prefixed field.');
  417. $this->assertEquals($data['title'], $result->title, 'Data from prefix should be merged.');
  418. $this->assertEquals($data['Articles']['user']['username'], $result->user->username);
  419. }
  420. /**
  421. * test one() with association data.
  422. *
  423. * @return void
  424. */
  425. public function testOneAssociationsSingle()
  426. {
  427. $data = [
  428. 'title' => 'My title',
  429. 'body' => 'My content',
  430. 'author_id' => 1,
  431. 'comments' => [
  432. ['comment' => 'First post', 'user_id' => 2],
  433. ['comment' => 'Second post', 'user_id' => 2],
  434. ],
  435. 'user' => [
  436. 'username' => 'mark',
  437. 'password' => 'secret'
  438. ]
  439. ];
  440. $marshall = new Marshaller($this->articles);
  441. $result = $marshall->one($data, ['associated' => ['Users']]);
  442. $this->assertEquals($data['title'], $result->title);
  443. $this->assertEquals($data['body'], $result->body);
  444. $this->assertEquals($data['author_id'], $result->author_id);
  445. $this->assertInternalType('array', $result->comments);
  446. $this->assertEquals($data['comments'], $result->comments);
  447. $this->assertTrue($result->isDirty('comments'));
  448. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  449. $this->assertTrue($result->isDirty('user'));
  450. $this->assertEquals($data['user']['username'], $result->user->username);
  451. $this->assertEquals($data['user']['password'], $result->user->password);
  452. }
  453. /**
  454. * test one() with association data.
  455. *
  456. * @return void
  457. */
  458. public function testOneAssociationsMany()
  459. {
  460. $data = [
  461. 'title' => 'My title',
  462. 'body' => 'My content',
  463. 'author_id' => 1,
  464. 'comments' => [
  465. ['comment' => 'First post', 'user_id' => 2],
  466. ['comment' => 'Second post', 'user_id' => 2],
  467. ],
  468. 'user' => [
  469. 'username' => 'mark',
  470. 'password' => 'secret'
  471. ]
  472. ];
  473. $marshall = new Marshaller($this->articles);
  474. $result = $marshall->one($data, ['associated' => ['Comments']]);
  475. $this->assertEquals($data['title'], $result->title);
  476. $this->assertEquals($data['body'], $result->body);
  477. $this->assertEquals($data['author_id'], $result->author_id);
  478. $this->assertInternalType('array', $result->comments);
  479. $this->assertCount(2, $result->comments);
  480. $this->assertInstanceOf('Cake\ORM\Entity', $result->comments[0]);
  481. $this->assertInstanceOf('Cake\ORM\Entity', $result->comments[1]);
  482. $this->assertEquals($data['comments'][0]['comment'], $result->comments[0]->comment);
  483. $this->assertInternalType('array', $result->user);
  484. $this->assertEquals($data['user'], $result->user);
  485. }
  486. /**
  487. * Test building the _joinData entity for belongstomany associations.
  488. *
  489. * @return void
  490. */
  491. public function testOneBelongsToManyJoinData()
  492. {
  493. $data = [
  494. 'title' => 'My title',
  495. 'body' => 'My content',
  496. 'author_id' => 1,
  497. 'tags' => [
  498. ['tag' => 'news', '_joinData' => ['active' => 1]],
  499. ['tag' => 'cakephp', '_joinData' => ['active' => 0]],
  500. ],
  501. ];
  502. $marshall = new Marshaller($this->articles);
  503. $result = $marshall->one($data, [
  504. 'associated' => ['Tags']
  505. ]);
  506. $this->assertEquals($data['title'], $result->title);
  507. $this->assertEquals($data['body'], $result->body);
  508. $this->assertInternalType('array', $result->tags);
  509. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  510. $this->assertEquals($data['tags'][0]['tag'], $result->tags[0]->tag);
  511. $this->assertInstanceOf(
  512. 'Cake\ORM\Entity',
  513. $result->tags[0]->_joinData,
  514. '_joinData should be an entity.'
  515. );
  516. $this->assertEquals(
  517. $data['tags'][0]['_joinData']['active'],
  518. $result->tags[0]->_joinData->active,
  519. '_joinData should be an entity.'
  520. );
  521. }
  522. /**
  523. * Test that the onlyIds option restricts to only accepting ids for belongs to many associations.
  524. *
  525. * @return void
  526. */
  527. public function testOneBelongsToManyOnlyIdsRejectArray()
  528. {
  529. $data = [
  530. 'title' => 'My title',
  531. 'body' => 'My content',
  532. 'author_id' => 1,
  533. 'tags' => [
  534. ['tag' => 'news'],
  535. ['tag' => 'cakephp'],
  536. ],
  537. ];
  538. $marshall = new Marshaller($this->articles);
  539. $result = $marshall->one($data, [
  540. 'associated' => ['Tags' => ['onlyIds' => true]]
  541. ]);
  542. $this->assertEmpty($result->tags, 'Only ids should be marshalled.');
  543. }
  544. /**
  545. * Test that the onlyIds option restricts to only accepting ids for belongs to many associations.
  546. *
  547. * @return void
  548. */
  549. public function testOneBelongsToManyOnlyIdsWithIds()
  550. {
  551. $data = [
  552. 'title' => 'My title',
  553. 'body' => 'My content',
  554. 'author_id' => 1,
  555. 'tags' => [
  556. '_ids' => [1, 2],
  557. ['tag' => 'news'],
  558. ],
  559. ];
  560. $marshall = new Marshaller($this->articles);
  561. $result = $marshall->one($data, [
  562. 'associated' => ['Tags' => ['onlyIds' => true]]
  563. ]);
  564. $this->assertCount(2, $result->tags, 'Ids should be marshalled.');
  565. }
  566. /**
  567. * Test marshalling nested associations on the _joinData structure.
  568. *
  569. * @return void
  570. */
  571. public function testOneBelongsToManyJoinDataAssociated()
  572. {
  573. $data = [
  574. 'title' => 'My title',
  575. 'body' => 'My content',
  576. 'author_id' => 1,
  577. 'tags' => [
  578. [
  579. 'tag' => 'news',
  580. '_joinData' => [
  581. 'active' => 1,
  582. 'user' => ['username' => 'Bill'],
  583. ]
  584. ],
  585. [
  586. 'tag' => 'cakephp',
  587. '_joinData' => [
  588. 'active' => 0,
  589. 'user' => ['username' => 'Mark'],
  590. ]
  591. ],
  592. ],
  593. ];
  594. $articlesTags = TableRegistry::get('ArticlesTags');
  595. $articlesTags->belongsTo('Users');
  596. $marshall = new Marshaller($this->articles);
  597. $result = $marshall->one($data, ['associated' => ['Tags._joinData.Users']]);
  598. $this->assertInstanceOf(
  599. 'Cake\ORM\Entity',
  600. $result->tags[0]->_joinData->user,
  601. 'joinData should contain a user entity.'
  602. );
  603. $this->assertEquals('Bill', $result->tags[0]->_joinData->user->username);
  604. $this->assertInstanceOf(
  605. 'Cake\ORM\Entity',
  606. $result->tags[1]->_joinData->user,
  607. 'joinData should contain a user entity.'
  608. );
  609. $this->assertEquals('Mark', $result->tags[1]->_joinData->user->username);
  610. }
  611. /**
  612. * Test one() with with id and _joinData.
  613. *
  614. * @return void
  615. */
  616. public function testOneBelongsToManyJoinDataAssociatedWithIds()
  617. {
  618. $data = [
  619. 'title' => 'My title',
  620. 'body' => 'My content',
  621. 'author_id' => 1,
  622. 'tags' => [
  623. 3 => [
  624. 'id' => 1,
  625. '_joinData' => [
  626. 'active' => 1,
  627. 'user' => ['username' => 'MyLux'],
  628. ]
  629. ],
  630. 5 => [
  631. 'id' => 2,
  632. '_joinData' => [
  633. 'active' => 0,
  634. 'user' => ['username' => 'IronFall'],
  635. ]
  636. ],
  637. ],
  638. ];
  639. $articlesTags = TableRegistry::get('ArticlesTags');
  640. $tags = TableRegistry::get('Tags');
  641. $t1 = $tags->find('all')->where(['id' => 1])->first();
  642. $t2 = $tags->find('all')->where(['id' => 2])->first();
  643. $articlesTags->belongsTo('Users');
  644. $marshall = new Marshaller($this->articles);
  645. $result = $marshall->one($data, ['associated' => ['Tags._joinData.Users']]);
  646. $this->assertInstanceOf(
  647. 'Cake\ORM\Entity',
  648. $result->tags[0]
  649. );
  650. $this->assertInstanceOf(
  651. 'Cake\ORM\Entity',
  652. $result->tags[1]
  653. );
  654. $this->assertInstanceOf(
  655. 'Cake\ORM\Entity',
  656. $result->tags[0]->_joinData->user
  657. );
  658. $this->assertInstanceOf(
  659. 'Cake\ORM\Entity',
  660. $result->tags[1]->_joinData->user
  661. );
  662. $this->assertFalse($result->tags[0]->isNew(), 'Should not be new, as id is in db.');
  663. $this->assertFalse($result->tags[1]->isNew(), 'Should not be new, as id is in db.');
  664. $this->assertEquals($t1->tag, $result->tags[0]->tag);
  665. $this->assertEquals($t2->tag, $result->tags[1]->tag);
  666. $this->assertEquals($data['tags'][3]['_joinData']['user']['username'], $result->tags[0]->_joinData->user->username);
  667. $this->assertEquals($data['tags'][5]['_joinData']['user']['username'], $result->tags[1]->_joinData->user->username);
  668. }
  669. /**
  670. * Test belongsToMany association with mixed data and _joinData
  671. *
  672. * @return void
  673. */
  674. public function testOneBelongsToManyWithMixedJoinData()
  675. {
  676. $data = [
  677. 'title' => 'My title',
  678. 'body' => 'My content',
  679. 'author_id' => 1,
  680. 'tags' => [
  681. [
  682. 'id' => 1,
  683. '_joinData' => [
  684. 'active' => 0,
  685. ]
  686. ],
  687. [
  688. 'name' => 'tag5',
  689. '_joinData' => [
  690. 'active' => 1,
  691. ]
  692. ]
  693. ]
  694. ];
  695. $marshall = new Marshaller($this->articles);
  696. $result = $marshall->one($data, ['associated' => ['Tags._joinData']]);
  697. $this->assertEquals($data['tags'][0]['id'], $result->tags[0]->id);
  698. $this->assertEquals($data['tags'][1]['name'], $result->tags[1]->name);
  699. $this->assertEquals(0, $result->tags[0]->_joinData->active);
  700. $this->assertEquals(1, $result->tags[1]->_joinData->active);
  701. }
  702. public function testOneBelongsToManyWithNestedAssociations()
  703. {
  704. $this->tags->belongsToMany('Articles');
  705. $data = [
  706. 'name' => 'new tag',
  707. 'articles' => [
  708. // This nested article exists, and we want to update it.
  709. [
  710. 'id' => 1,
  711. 'title' => 'New tagged article',
  712. 'body' => 'New tagged article',
  713. 'user' => [
  714. 'id' => 1,
  715. 'username' => 'newuser'
  716. ],
  717. 'comments' => [
  718. ['comment' => 'New comment', 'user_id' => 1],
  719. ['comment' => 'Second comment', 'user_id' => 1],
  720. ]
  721. ]
  722. ]
  723. ];
  724. $marshaller = new Marshaller($this->tags);
  725. $tag = $marshaller->one($data, ['associated' => ['Articles.Users', 'Articles.Comments']]);
  726. $this->assertNotEmpty($tag->articles);
  727. $this->assertCount(1, $tag->articles);
  728. $this->assertTrue($tag->isDirty('articles'), 'Updated prop should be dirty');
  729. $this->assertInstanceOf('Cake\ORM\Entity', $tag->articles[0]);
  730. $this->assertSame('New tagged article', $tag->articles[0]->title);
  731. $this->assertFalse($tag->articles[0]->isNew());
  732. $this->assertNotEmpty($tag->articles[0]->user);
  733. $this->assertInstanceOf('Cake\ORM\Entity', $tag->articles[0]->user);
  734. $this->assertTrue($tag->articles[0]->isDirty('user'), 'Updated prop should be dirty');
  735. $this->assertSame('newuser', $tag->articles[0]->user->username);
  736. $this->assertTrue($tag->articles[0]->user->isNew());
  737. $this->assertNotEmpty($tag->articles[0]->comments);
  738. $this->assertCount(2, $tag->articles[0]->comments);
  739. $this->assertTrue($tag->articles[0]->isDirty('comments'), 'Updated prop should be dirty');
  740. $this->assertInstanceOf('Cake\ORM\Entity', $tag->articles[0]->comments[0]);
  741. $this->assertTrue($tag->articles[0]->comments[0]->isNew());
  742. $this->assertTrue($tag->articles[0]->comments[1]->isNew());
  743. }
  744. /**
  745. * Test belongsToMany association with mixed data and _joinData
  746. *
  747. * @return void
  748. */
  749. public function testBelongsToManyAddingNewExisting()
  750. {
  751. $this->tags->setEntityClass(__NAMESPACE__ . '\Tag');
  752. $data = [
  753. 'title' => 'My title',
  754. 'body' => 'My content',
  755. 'author_id' => 1,
  756. 'tags' => [
  757. [
  758. 'id' => 1,
  759. '_joinData' => [
  760. 'active' => 0,
  761. ]
  762. ],
  763. ]
  764. ];
  765. $marshall = new Marshaller($this->articles);
  766. $result = $marshall->one($data, ['associated' => ['Tags._joinData']]);
  767. $data = [
  768. 'title' => 'New Title',
  769. 'tags' => [
  770. [
  771. 'id' => 1,
  772. '_joinData' => [
  773. 'active' => 0,
  774. ]
  775. ],
  776. [
  777. 'id' => 2,
  778. '_joinData' => [
  779. 'active' => 1,
  780. ]
  781. ]
  782. ]
  783. ];
  784. $result = $marshall->merge($result, $data, ['associated' => ['Tags._joinData']]);
  785. $this->assertEquals($data['title'], $result->title);
  786. $this->assertEquals($data['tags'][0]['id'], $result->tags[0]->id);
  787. $this->assertEquals($data['tags'][1]['id'], $result->tags[1]->id);
  788. $this->assertNotEmpty($result->tags[0]->_joinData);
  789. $this->assertNotEmpty($result->tags[1]->_joinData);
  790. $this->assertTrue($result->isDirty('tags'), 'Modified prop should be dirty');
  791. $this->assertEquals(0, $result->tags[0]->_joinData->active);
  792. $this->assertEquals(1, $result->tags[1]->_joinData->active);
  793. }
  794. /**
  795. * Test belongsToMany association with mixed data and _joinData
  796. *
  797. * @return void
  798. */
  799. public function testBelongsToManyWithMixedJoinDataOutOfOrder()
  800. {
  801. $data = [
  802. 'title' => 'My title',
  803. 'body' => 'My content',
  804. 'author_id' => 1,
  805. 'tags' => [
  806. [
  807. 'name' => 'tag5',
  808. '_joinData' => [
  809. 'active' => 1,
  810. ]
  811. ],
  812. [
  813. 'id' => 1,
  814. '_joinData' => [
  815. 'active' => 0,
  816. ]
  817. ],
  818. [
  819. 'name' => 'tag3',
  820. '_joinData' => [
  821. 'active' => 1,
  822. ]
  823. ],
  824. ]
  825. ];
  826. $marshall = new Marshaller($this->articles);
  827. $result = $marshall->one($data, ['associated' => ['Tags._joinData']]);
  828. $this->assertEquals($data['tags'][0]['name'], $result->tags[0]->name);
  829. $this->assertEquals($data['tags'][1]['id'], $result->tags[1]->id);
  830. $this->assertEquals($data['tags'][2]['name'], $result->tags[2]->name);
  831. $this->assertEquals(1, $result->tags[0]->_joinData->active);
  832. $this->assertEquals(0, $result->tags[1]->_joinData->active);
  833. $this->assertEquals(1, $result->tags[2]->_joinData->active);
  834. }
  835. /**
  836. * Test belongsToMany association with scalars
  837. *
  838. * @return void
  839. */
  840. public function testBelongsToManyInvalidData()
  841. {
  842. $data = [
  843. 'title' => 'My title',
  844. 'body' => 'My content',
  845. 'author_id' => 1,
  846. 'tags' => [
  847. 'id' => 1
  848. ]
  849. ];
  850. $article = $this->articles->newEntity($data, [
  851. 'associated' => ['Tags']
  852. ]);
  853. $this->assertEmpty($article->tags, 'No entity should be created');
  854. $data['tags'] = 1;
  855. $article = $this->articles->newEntity($data, [
  856. 'associated' => ['Tags']
  857. ]);
  858. $this->assertEmpty($article->tags, 'No entity should be created');
  859. }
  860. /**
  861. * Test belongsToMany association with mixed data array
  862. *
  863. * @return void
  864. */
  865. public function testBelongsToManyWithMixedData()
  866. {
  867. $data = [
  868. 'title' => 'My title',
  869. 'body' => 'My content',
  870. 'author_id' => 1,
  871. 'tags' => [
  872. [
  873. 'name' => 'tag4'
  874. ],
  875. [
  876. 'name' => 'tag5'
  877. ],
  878. [
  879. 'id' => 1
  880. ]
  881. ]
  882. ];
  883. $tags = TableRegistry::get('Tags');
  884. $marshaller = new Marshaller($this->articles);
  885. $article = $marshaller->one($data, ['associated' => ['Tags']]);
  886. $this->assertEquals($data['tags'][0]['name'], $article->tags[0]->name);
  887. $this->assertEquals($data['tags'][1]['name'], $article->tags[1]->name);
  888. $this->assertEquals($article->tags[2], $tags->get(1));
  889. $this->assertTrue($article->tags[0]->isNew());
  890. $this->assertTrue($article->tags[1]->isNew());
  891. $this->assertFalse($article->tags[2]->isNew());
  892. $tagCount = $tags->find()->count();
  893. $this->articles->save($article);
  894. $this->assertEquals($tagCount + 2, $tags->find()->count());
  895. }
  896. /**
  897. * Test belongsToMany association with the ForceNewTarget to force saving
  898. * new records on the target tables with BTM relationships when the primaryKey(s)
  899. * of the target table is specified.
  900. *
  901. * @return void
  902. */
  903. public function testBelongsToManyWithForceNew()
  904. {
  905. $data = [
  906. 'title' => 'Fourth Article',
  907. 'body' => 'Fourth Article Body',
  908. 'author_id' => 1,
  909. 'tags' => [
  910. [
  911. 'id' => 3
  912. ],
  913. [
  914. 'id' => 4,
  915. 'name' => 'tag4'
  916. ]
  917. ]
  918. ];
  919. $marshaller = new Marshaller($this->articles);
  920. $article = $marshaller->one($data, [
  921. 'associated' => ['Tags'],
  922. 'forceNew' => true
  923. ]);
  924. $this->assertFalse($article->tags[0]->isNew(), 'The tag should not be new');
  925. $this->assertTrue($article->tags[1]->isNew(), 'The tag should be new');
  926. $this->assertSame('tag4', $article->tags[1]->name, 'Property should match request data.');
  927. }
  928. /**
  929. * Test HasMany association with _ids attribute
  930. *
  931. * @return void
  932. */
  933. public function testOneHasManyWithIds()
  934. {
  935. $data = [
  936. 'title' => 'article',
  937. 'body' => 'some content',
  938. 'comments' => [
  939. '_ids' => [1, 2]
  940. ]
  941. ];
  942. $marshaller = new Marshaller($this->articles);
  943. $article = $marshaller->one($data, ['associated' => ['Comments']]);
  944. $this->assertEquals($article->comments[0], $this->comments->get(1));
  945. $this->assertEquals($article->comments[1], $this->comments->get(2));
  946. }
  947. /**
  948. * Test that the onlyIds option restricts to only accepting ids for hasmany associations.
  949. *
  950. * @return void
  951. */
  952. public function testOneHasManyOnlyIdsRejectArray()
  953. {
  954. $data = [
  955. 'title' => 'article',
  956. 'body' => 'some content',
  957. 'comments' => [
  958. ['comment' => 'first comment'],
  959. ['comment' => 'second comment'],
  960. ]
  961. ];
  962. $marshaller = new Marshaller($this->articles);
  963. $article = $marshaller->one($data, [
  964. 'associated' => ['Comments' => ['onlyIds' => true]]
  965. ]);
  966. $this->assertEmpty($article->comments);
  967. }
  968. /**
  969. * Test that the onlyIds option restricts to only accepting ids for hasmany associations.
  970. *
  971. * @return void
  972. */
  973. public function testOneHasManyOnlyIdsWithIds()
  974. {
  975. $data = [
  976. 'title' => 'article',
  977. 'body' => 'some content',
  978. 'comments' => [
  979. '_ids' => [1, 2],
  980. ['comment' => 'first comment'],
  981. ]
  982. ];
  983. $marshaller = new Marshaller($this->articles);
  984. $article = $marshaller->one($data, [
  985. 'associated' => ['Comments' => ['onlyIds' => true]]
  986. ]);
  987. $this->assertCount(2, $article->comments);
  988. }
  989. /**
  990. * Test HasMany association with invalid data
  991. *
  992. * @return void
  993. */
  994. public function testOneHasManyInvalidData()
  995. {
  996. $data = [
  997. 'title' => 'new title',
  998. 'body' => 'some content',
  999. 'comments' => [
  1000. 'id' => 1
  1001. ]
  1002. ];
  1003. $marshaller = new Marshaller($this->articles);
  1004. $article = $marshaller->one($data, ['associated' => ['Comments']]);
  1005. $this->assertEmpty($article->comments);
  1006. $data['comments'] = 1;
  1007. $article = $marshaller->one($data, ['associated' => ['Comments']]);
  1008. $this->assertEmpty($article->comments);
  1009. }
  1010. /**
  1011. * Test one() with deeper associations.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testOneDeepAssociations()
  1016. {
  1017. $data = [
  1018. 'comment' => 'First post',
  1019. 'user_id' => 2,
  1020. 'article' => [
  1021. 'title' => 'Article title',
  1022. 'body' => 'Article body',
  1023. 'user' => [
  1024. 'username' => 'mark',
  1025. 'password' => 'secret'
  1026. ],
  1027. ]
  1028. ];
  1029. $marshall = new Marshaller($this->comments);
  1030. $result = $marshall->one($data, ['associated' => ['Articles.Users']]);
  1031. $this->assertEquals(
  1032. $data['article']['title'],
  1033. $result->article->title
  1034. );
  1035. $this->assertEquals(
  1036. $data['article']['user']['username'],
  1037. $result->article->user->username
  1038. );
  1039. }
  1040. /**
  1041. * Test many() with a simple set of data.
  1042. *
  1043. * @return void
  1044. */
  1045. public function testManySimple()
  1046. {
  1047. $data = [
  1048. ['comment' => 'First post', 'user_id' => 2],
  1049. ['comment' => 'Second post', 'user_id' => 2],
  1050. ];
  1051. $marshall = new Marshaller($this->comments);
  1052. $result = $marshall->many($data);
  1053. $this->assertCount(2, $result);
  1054. $this->assertInstanceOf('Cake\ORM\Entity', $result[0]);
  1055. $this->assertInstanceOf('Cake\ORM\Entity', $result[1]);
  1056. $this->assertEquals($data[0]['comment'], $result[0]->comment);
  1057. $this->assertEquals($data[1]['comment'], $result[1]->comment);
  1058. }
  1059. /**
  1060. * Test many() with some invalid data
  1061. *
  1062. * @return void
  1063. */
  1064. public function testManyInvalidData()
  1065. {
  1066. $data = [
  1067. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  1068. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1],
  1069. '_csrfToken' => 'abc123',
  1070. ];
  1071. $marshall = new Marshaller($this->comments);
  1072. $result = $marshall->many($data);
  1073. $this->assertCount(2, $result);
  1074. }
  1075. /**
  1076. * test many() with nested associations.
  1077. *
  1078. * @return void
  1079. */
  1080. public function testManyAssociations()
  1081. {
  1082. $data = [
  1083. [
  1084. 'comment' => 'First post',
  1085. 'user_id' => 2,
  1086. 'user' => [
  1087. 'username' => 'mark',
  1088. ],
  1089. ],
  1090. [
  1091. 'comment' => 'Second post',
  1092. 'user_id' => 2,
  1093. 'user' => [
  1094. 'username' => 'jose',
  1095. ],
  1096. ],
  1097. ];
  1098. $marshall = new Marshaller($this->comments);
  1099. $result = $marshall->many($data, ['associated' => ['Users']]);
  1100. $this->assertCount(2, $result);
  1101. $this->assertInstanceOf('Cake\ORM\Entity', $result[0]);
  1102. $this->assertInstanceOf('Cake\ORM\Entity', $result[1]);
  1103. $this->assertEquals(
  1104. $data[0]['user']['username'],
  1105. $result[0]->user->username
  1106. );
  1107. $this->assertEquals(
  1108. $data[1]['user']['username'],
  1109. $result[1]->user->username
  1110. );
  1111. }
  1112. /**
  1113. * Test if exception is raised when called with [associated => NonExistingAssociation]
  1114. * Previously such association were simply ignored
  1115. *
  1116. * @return void
  1117. */
  1118. public function testManyInvalidAssociation()
  1119. {
  1120. $this->expectException(\InvalidArgumentException::class);
  1121. $data = [
  1122. [
  1123. 'comment' => 'First post',
  1124. 'user_id' => 2,
  1125. 'user' => [
  1126. 'username' => 'mark',
  1127. ],
  1128. ],
  1129. [
  1130. 'comment' => 'Second post',
  1131. 'user_id' => 2,
  1132. 'user' => [
  1133. 'username' => 'jose',
  1134. ],
  1135. ],
  1136. ];
  1137. $marshall = new Marshaller($this->comments);
  1138. $marshall->many($data, ['associated' => ['Users', 'People']]);
  1139. }
  1140. /**
  1141. * Test generating a list of entities from a list of ids.
  1142. *
  1143. * @return void
  1144. */
  1145. public function testOneGenerateBelongsToManyEntitiesFromIds()
  1146. {
  1147. $data = [
  1148. 'title' => 'Haz tags',
  1149. 'body' => 'Some content here',
  1150. 'tags' => ['_ids' => '']
  1151. ];
  1152. $marshall = new Marshaller($this->articles);
  1153. $result = $marshall->one($data, ['associated' => ['Tags']]);
  1154. $this->assertCount(0, $result->tags);
  1155. $data = [
  1156. 'title' => 'Haz tags',
  1157. 'body' => 'Some content here',
  1158. 'tags' => ['_ids' => false]
  1159. ];
  1160. $result = $marshall->one($data, ['associated' => ['Tags']]);
  1161. $this->assertCount(0, $result->tags);
  1162. $data = [
  1163. 'title' => 'Haz tags',
  1164. 'body' => 'Some content here',
  1165. 'tags' => ['_ids' => null]
  1166. ];
  1167. $result = $marshall->one($data, ['associated' => ['Tags']]);
  1168. $this->assertCount(0, $result->tags);
  1169. $data = [
  1170. 'title' => 'Haz tags',
  1171. 'body' => 'Some content here',
  1172. 'tags' => ['_ids' => []]
  1173. ];
  1174. $result = $marshall->one($data, ['associated' => ['Tags']]);
  1175. $this->assertCount(0, $result->tags);
  1176. $data = [
  1177. 'title' => 'Haz tags',
  1178. 'body' => 'Some content here',
  1179. 'tags' => ['_ids' => [1, 2, 3]]
  1180. ];
  1181. $marshall = new Marshaller($this->articles);
  1182. $result = $marshall->one($data, ['associated' => ['Tags']]);
  1183. $this->assertCount(3, $result->tags);
  1184. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  1185. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  1186. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  1187. }
  1188. /**
  1189. * Test merge() in a simple use.
  1190. *
  1191. * @return void
  1192. */
  1193. public function testMergeSimple()
  1194. {
  1195. $data = [
  1196. 'title' => 'My title',
  1197. 'author_id' => 1,
  1198. 'not_in_schema' => true
  1199. ];
  1200. $marshall = new Marshaller($this->articles);
  1201. $entity = new Entity([
  1202. 'title' => 'Foo',
  1203. 'body' => 'My Content'
  1204. ]);
  1205. $entity->setAccess('*', true);
  1206. $entity->isNew(false);
  1207. $entity->clean();
  1208. $result = $marshall->merge($entity, $data, []);
  1209. $this->assertSame($entity, $result);
  1210. $this->assertEquals($data + ['body' => 'My Content'], $result->toArray());
  1211. $this->assertTrue($result->isDirty(), 'Should be a dirty entity.');
  1212. $this->assertFalse($result->isNew(), 'Should not change the entity state');
  1213. }
  1214. /**
  1215. * Test merge() with accessibleFields options
  1216. *
  1217. * @return void
  1218. */
  1219. public function testMergeAccessibleFields()
  1220. {
  1221. $data = [
  1222. 'title' => 'My title',
  1223. 'body' => 'New content',
  1224. 'author_id' => 1,
  1225. 'not_in_schema' => true
  1226. ];
  1227. $marshall = new Marshaller($this->articles);
  1228. $entity = new Entity([
  1229. 'title' => 'Foo',
  1230. 'body' => 'My Content'
  1231. ]);
  1232. $entity->setAccess('*', false);
  1233. $entity->isNew(false);
  1234. $entity->clean();
  1235. $result = $marshall->merge($entity, $data, ['accessibleFields' => ['body' => true]]);
  1236. $this->assertSame($entity, $result);
  1237. $this->assertEquals(['title' => 'Foo', 'body' => 'New content'], $result->toArray());
  1238. $this->assertTrue($entity->isAccessible('body'));
  1239. }
  1240. /**
  1241. * Provides empty values.
  1242. *
  1243. * @return array
  1244. */
  1245. public function emptyProvider()
  1246. {
  1247. return [
  1248. [0],
  1249. ['0'],
  1250. ];
  1251. }
  1252. /**
  1253. * Test merging empty values into an entity.
  1254. *
  1255. * @dataProvider emptyProvider
  1256. * @return void
  1257. */
  1258. public function testMergeFalseyValues($value)
  1259. {
  1260. $marshall = new Marshaller($this->articles);
  1261. $entity = new Entity();
  1262. $entity->setAccess('*', true);
  1263. $entity->clean();
  1264. $entity = $marshall->merge($entity, ['author_id' => $value]);
  1265. $this->assertTrue($entity->isDirty('author_id'), 'Field should be dirty');
  1266. $this->assertSame(0, $entity->get('author_id'), 'Value should be zero');
  1267. }
  1268. /**
  1269. * Test merge() doesn't dirty values that were null and are null again.
  1270. *
  1271. * @return void
  1272. */
  1273. public function testMergeUnchangedNullValue()
  1274. {
  1275. $data = [
  1276. 'title' => 'My title',
  1277. 'author_id' => 1,
  1278. 'body' => null,
  1279. ];
  1280. $marshall = new Marshaller($this->articles);
  1281. $entity = new Entity([
  1282. 'title' => 'Foo',
  1283. 'body' => null
  1284. ]);
  1285. $entity->setAccess('*', true);
  1286. $entity->isNew(false);
  1287. $entity->clean();
  1288. $result = $marshall->merge($entity, $data, []);
  1289. $this->assertFalse($entity->isDirty('body'), 'unchanged null should not be dirty');
  1290. }
  1291. /**
  1292. * Tests that merge respects the entity accessible methods
  1293. *
  1294. * @return void
  1295. */
  1296. public function testMergeWhitelist()
  1297. {
  1298. $data = [
  1299. 'title' => 'My title',
  1300. 'author_id' => 1,
  1301. 'not_in_schema' => true
  1302. ];
  1303. $marshall = new Marshaller($this->articles);
  1304. $entity = new Entity([
  1305. 'title' => 'Foo',
  1306. 'body' => 'My Content'
  1307. ]);
  1308. $entity->setAccess('*', false);
  1309. $entity->setAccess('author_id', true);
  1310. $entity->isNew(false);
  1311. $entity->clean();
  1312. $result = $marshall->merge($entity, $data, []);
  1313. $expected = [
  1314. 'title' => 'Foo',
  1315. 'body' => 'My Content',
  1316. 'author_id' => 1
  1317. ];
  1318. $this->assertEquals($expected, $result->toArray());
  1319. }
  1320. /**
  1321. * Test merge() with an invalid association
  1322. *
  1323. * @return void
  1324. */
  1325. public function testMergeInvalidAssociation()
  1326. {
  1327. $this->expectException(\InvalidArgumentException::class);
  1328. $this->expectExceptionMessage('Cannot marshal data for "Derp" association. It is not associated with "Articles".');
  1329. $data = [
  1330. 'title' => 'My title',
  1331. 'body' => 'My content',
  1332. 'derp' => [
  1333. 'id' => 1,
  1334. 'username' => 'mark',
  1335. ]
  1336. ];
  1337. $article = new Entity([
  1338. 'title' => 'title for post',
  1339. 'body' => 'body',
  1340. ]);
  1341. $marshall = new Marshaller($this->articles);
  1342. $marshall->merge($article, $data, [
  1343. 'associated' => ['Derp']
  1344. ]);
  1345. }
  1346. /**
  1347. * Test merge when fields contains an association.
  1348. *
  1349. * @param $fields
  1350. * @return void
  1351. */
  1352. public function testMergeWithSingleAssociationAndFields()
  1353. {
  1354. $user = new Entity([
  1355. 'username' => 'user',
  1356. ]);
  1357. $article = new Entity([
  1358. 'title' => 'title for post',
  1359. 'body' => 'body',
  1360. 'user' => $user,
  1361. ]);
  1362. $user->setAccess('*', true);
  1363. $article->setAccess('*', true);
  1364. $data = [
  1365. 'title' => 'Chelsea',
  1366. 'user' => [
  1367. 'username' => 'dee'
  1368. ]
  1369. ];
  1370. $marshall = new Marshaller($this->articles);
  1371. $marshall->merge($article, $data, [
  1372. 'fields' => ['title', 'user'],
  1373. 'associated' => ['Users' => []]
  1374. ]);
  1375. $this->assertSame($user, $article->user);
  1376. $this->assertTrue($article->isDirty('user'));
  1377. }
  1378. /**
  1379. * Tests that fields with the same value are not marked as dirty
  1380. *
  1381. * @return void
  1382. */
  1383. public function testMergeDirty()
  1384. {
  1385. $marshall = new Marshaller($this->articles);
  1386. $entity = new Entity([
  1387. 'title' => 'Foo',
  1388. 'author_id' => 1
  1389. ]);
  1390. $data = [
  1391. 'title' => 'Foo',
  1392. 'author_id' => 1,
  1393. 'crazy' => true
  1394. ];
  1395. $entity->setAccess('*', true);
  1396. $entity->clean();
  1397. $result = $marshall->merge($entity, $data, []);
  1398. $expected = [
  1399. 'title' => 'Foo',
  1400. 'author_id' => 1,
  1401. 'crazy' => true
  1402. ];
  1403. $this->assertEquals($expected, $result->toArray());
  1404. $this->assertFalse($entity->isDirty('title'));
  1405. $this->assertFalse($entity->isDirty('author_id'));
  1406. $this->assertTrue($entity->isDirty('crazy'));
  1407. }
  1408. /**
  1409. * Tests merging data into an associated entity
  1410. *
  1411. * @return void
  1412. */
  1413. public function testMergeWithSingleAssociation()
  1414. {
  1415. $user = new Entity([
  1416. 'username' => 'mark',
  1417. 'password' => 'secret'
  1418. ]);
  1419. $entity = new Entity([
  1420. 'title' => 'My Title',
  1421. 'user' => $user
  1422. ]);
  1423. $user->setAccess('*', true);
  1424. $entity->setAccess('*', true);
  1425. $entity->clean();
  1426. $data = [
  1427. 'body' => 'My Content',
  1428. 'user' => [
  1429. 'password' => 'not a secret'
  1430. ]
  1431. ];
  1432. $marshall = new Marshaller($this->articles);
  1433. $marshall->merge($entity, $data, ['associated' => ['Users']]);
  1434. $this->assertTrue($entity->isDirty('user'), 'association should be dirty');
  1435. $this->assertTrue($entity->isDirty('body'), 'body should be dirty');
  1436. $this->assertEquals('My Content', $entity->body);
  1437. $this->assertSame($user, $entity->user);
  1438. $this->assertEquals('mark', $entity->user->username);
  1439. $this->assertEquals('not a secret', $entity->user->password);
  1440. }
  1441. /**
  1442. * Tests that new associated entities can be created when merging data into
  1443. * a parent entity
  1444. *
  1445. * @return void
  1446. */
  1447. public function testMergeCreateAssociation()
  1448. {
  1449. $entity = new Entity([
  1450. 'title' => 'My Title'
  1451. ]);
  1452. $entity->setAccess('*', true);
  1453. $entity->clean();
  1454. $data = [
  1455. 'body' => 'My Content',
  1456. 'user' => [
  1457. 'username' => 'mark',
  1458. 'password' => 'not a secret'
  1459. ]
  1460. ];
  1461. $marshall = new Marshaller($this->articles);
  1462. $marshall->merge($entity, $data, ['associated' => ['Users']]);
  1463. $this->assertEquals('My Content', $entity->body);
  1464. $this->assertInstanceOf('Cake\ORM\Entity', $entity->user);
  1465. $this->assertEquals('mark', $entity->user->username);
  1466. $this->assertEquals('not a secret', $entity->user->password);
  1467. $this->assertTrue($entity->isDirty('user'));
  1468. $this->assertTrue($entity->isDirty('body'));
  1469. $this->assertTrue($entity->user->isNew());
  1470. }
  1471. /**
  1472. * Test merge when an association has been replaced with null
  1473. *
  1474. * @return void
  1475. */
  1476. public function testMergeAssociationNullOut()
  1477. {
  1478. $user = new Entity([
  1479. 'id' => 1,
  1480. 'username' => 'user',
  1481. ]);
  1482. $article = new Entity([
  1483. 'title' => 'title for post',
  1484. 'user_id' => 1,
  1485. 'user' => $user,
  1486. ]);
  1487. $user->setAccess('*', true);
  1488. $article->setAccess('*', true);
  1489. $data = [
  1490. 'title' => 'Chelsea',
  1491. 'user_id' => '',
  1492. 'user' => ''
  1493. ];
  1494. $marshall = new Marshaller($this->articles);
  1495. $marshall->merge($article, $data, [
  1496. 'associated' => ['Users']
  1497. ]);
  1498. $this->assertNull($article->user);
  1499. $this->assertSame('', $article->user_id);
  1500. $this->assertTrue($article->isDirty('user'));
  1501. }
  1502. /**
  1503. * Tests merging one to many associations
  1504. *
  1505. * @return void
  1506. */
  1507. public function testMergeMultipleAssociations()
  1508. {
  1509. $user = new Entity(['username' => 'mark', 'password' => 'secret']);
  1510. $comment1 = new Entity(['id' => 1, 'comment' => 'A comment']);
  1511. $comment2 = new Entity(['id' => 2, 'comment' => 'Another comment']);
  1512. $entity = new Entity([
  1513. 'title' => 'My Title',
  1514. 'user' => $user,
  1515. 'comments' => [$comment1, $comment2]
  1516. ]);
  1517. $user->setAccess('*', true);
  1518. $comment1->setAccess('*', true);
  1519. $comment2->setAccess('*', true);
  1520. $entity->setAccess('*', true);
  1521. $entity->clean();
  1522. $data = [
  1523. 'title' => 'Another title',
  1524. 'user' => ['password' => 'not so secret'],
  1525. 'comments' => [
  1526. ['comment' => 'Extra comment 1'],
  1527. ['id' => 2, 'comment' => 'Altered comment 2'],
  1528. ['id' => 1, 'comment' => 'Altered comment 1'],
  1529. ['id' => 3, 'comment' => 'Extra comment 3'],
  1530. ['id' => 4, 'comment' => 'Extra comment 4'],
  1531. ['comment' => 'Extra comment 2']
  1532. ]
  1533. ];
  1534. $marshall = new Marshaller($this->articles);
  1535. $result = $marshall->merge($entity, $data, ['associated' => ['Users', 'Comments']]);
  1536. $this->assertSame($entity, $result);
  1537. $this->assertSame($user, $result->user);
  1538. $this->assertTrue($result->isDirty('user'), 'association should be dirty');
  1539. $this->assertEquals('not so secret', $entity->user->password);
  1540. $this->assertTrue($result->isDirty('comments'));
  1541. $this->assertSame($comment1, $entity->comments[0]);
  1542. $this->assertSame($comment2, $entity->comments[1]);
  1543. $this->assertEquals('Altered comment 1', $entity->comments[0]->comment);
  1544. $this->assertEquals('Altered comment 2', $entity->comments[1]->comment);
  1545. $thirdComment = $this->articles->Comments
  1546. ->find()
  1547. ->where(['id' => 3])
  1548. ->enableHydration(false)
  1549. ->first();
  1550. $this->assertEquals(
  1551. ['comment' => 'Extra comment 3'] + $thirdComment,
  1552. $entity->comments[2]->toArray()
  1553. );
  1554. $forthComment = $this->articles->Comments
  1555. ->find()
  1556. ->where(['id' => 4])
  1557. ->enableHydration(false)
  1558. ->first();
  1559. $this->assertEquals(
  1560. ['comment' => 'Extra comment 4'] + $forthComment,
  1561. $entity->comments[3]->toArray()
  1562. );
  1563. $this->assertEquals(
  1564. ['comment' => 'Extra comment 1'],
  1565. $entity->comments[4]->toArray()
  1566. );
  1567. $this->assertEquals(
  1568. ['comment' => 'Extra comment 2'],
  1569. $entity->comments[5]->toArray()
  1570. );
  1571. }
  1572. /**
  1573. * Tests that merging data to an entity containing belongsToMany and _ids
  1574. * will just overwrite the data
  1575. *
  1576. * @return void
  1577. */
  1578. public function testMergeBelongsToManyEntitiesFromIds()
  1579. {
  1580. $entity = new Entity([
  1581. 'title' => 'Haz tags',
  1582. 'body' => 'Some content here',
  1583. 'tags' => [
  1584. new Entity(['id' => 1, 'name' => 'Cake']),
  1585. new Entity(['id' => 2, 'name' => 'PHP'])
  1586. ]
  1587. ]);
  1588. $data = [
  1589. 'title' => 'Haz moar tags',
  1590. 'tags' => ['_ids' => [1, 2, 3]]
  1591. ];
  1592. $entity->setAccess('*', true);
  1593. $entity->clean();
  1594. $marshall = new Marshaller($this->articles);
  1595. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1596. $this->assertCount(3, $result->tags);
  1597. $this->assertTrue($result->isDirty('tags'), 'Updated prop should be dirty');
  1598. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  1599. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  1600. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  1601. }
  1602. /**
  1603. * Tests that merging data to an entity containing belongsToMany and _ids
  1604. * will not generate conflicting queries when associations are automatically selected
  1605. *
  1606. * @return void
  1607. */
  1608. public function testMergeFromIdsWithAutoAssociation()
  1609. {
  1610. $entity = new Entity([
  1611. 'title' => 'Haz tags',
  1612. 'body' => 'Some content here',
  1613. 'tags' => [
  1614. new Entity(['id' => 1, 'name' => 'Cake']),
  1615. new Entity(['id' => 2, 'name' => 'PHP'])
  1616. ]
  1617. ]);
  1618. $data = [
  1619. 'title' => 'Haz moar tags',
  1620. 'tags' => ['_ids' => [1, 2, 3]]
  1621. ];
  1622. $entity->setAccess('*', true);
  1623. $entity->clean();
  1624. // Adding a forced join to have another table with the same column names
  1625. $this->articles->Tags->getEventManager()->on('Model.beforeFind', function ($e, $query) {
  1626. $left = new IdentifierExpression('Tags.id');
  1627. $right = new IdentifierExpression('a.id');
  1628. $query->leftJoin(['a' => 'tags'], $query->newExpr()->eq($left, $right));
  1629. });
  1630. $marshall = new Marshaller($this->articles);
  1631. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1632. $this->assertCount(3, $result->tags);
  1633. $this->assertTrue($result->isDirty('tags'));
  1634. }
  1635. /**
  1636. * Tests that merging data to an entity containing belongsToMany and _ids
  1637. * with additional association conditions works.
  1638. *
  1639. * @return void
  1640. */
  1641. public function testMergeBelongsToManyFromIdsWithConditions()
  1642. {
  1643. $this->articles->belongsToMany('Tags', [
  1644. 'conditions' => ['ArticleTags.article_id' => 1]
  1645. ]);
  1646. $entity = new Entity([
  1647. 'title' => 'No tags',
  1648. 'body' => 'Some content here',
  1649. 'tags' => []
  1650. ]);
  1651. $data = [
  1652. 'title' => 'Haz moar tags',
  1653. 'tags' => ['_ids' => [1, 2, 3]]
  1654. ];
  1655. $entity->setAccess('*', true);
  1656. $entity->clean();
  1657. $marshall = new Marshaller($this->articles);
  1658. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1659. $this->assertCount(3, $result->tags);
  1660. $this->assertTrue($result->isDirty('tags'));
  1661. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  1662. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  1663. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  1664. }
  1665. /**
  1666. * Tests that merging data to an entity containing belongsToMany as an array
  1667. * with additional association conditions works.
  1668. *
  1669. * @return void
  1670. */
  1671. public function testMergeBelongsToManyFromArrayWithConditions()
  1672. {
  1673. $this->articles->belongsToMany('Tags', [
  1674. 'conditions' => ['ArticleTags.article_id' => 1]
  1675. ]);
  1676. $this->articles->Tags->getEventManager()
  1677. ->on('Model.beforeFind', function (Event $event, $query) use (&$called) {
  1678. $called = true;
  1679. return $query->where(['Tags.id >=' => 1]);
  1680. });
  1681. $entity = new Entity([
  1682. 'title' => 'No tags',
  1683. 'body' => 'Some content here',
  1684. 'tags' => []
  1685. ]);
  1686. $data = [
  1687. 'title' => 'Haz moar tags',
  1688. 'tags' => [
  1689. ['id' => 1],
  1690. ['id' => 2]
  1691. ]
  1692. ];
  1693. $entity->setAccess('*', true);
  1694. $marshall = new Marshaller($this->articles);
  1695. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1696. $this->assertCount(2, $result->tags);
  1697. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  1698. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  1699. $this->assertTrue($called);
  1700. }
  1701. /**
  1702. * Tests that merging data to an entity containing belongsToMany and _ids
  1703. * will ignore empty values.
  1704. *
  1705. * @return void
  1706. */
  1707. public function testMergeBelongsToManyEntitiesFromIdsEmptyValue()
  1708. {
  1709. $entity = new Entity([
  1710. 'title' => 'Haz tags',
  1711. 'body' => 'Some content here',
  1712. 'tags' => [
  1713. new Entity(['id' => 1, 'name' => 'Cake']),
  1714. new Entity(['id' => 2, 'name' => 'PHP'])
  1715. ]
  1716. ]);
  1717. $data = [
  1718. 'title' => 'Haz moar tags',
  1719. 'tags' => ['_ids' => '']
  1720. ];
  1721. $entity->setAccess('*', true);
  1722. $marshall = new Marshaller($this->articles);
  1723. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1724. $this->assertCount(0, $result->tags);
  1725. $data = [
  1726. 'title' => 'Haz moar tags',
  1727. 'tags' => ['_ids' => false]
  1728. ];
  1729. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1730. $this->assertCount(0, $result->tags);
  1731. $data = [
  1732. 'title' => 'Haz moar tags',
  1733. 'tags' => ['_ids' => null]
  1734. ];
  1735. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  1736. $this->assertCount(0, $result->tags);
  1737. $this->assertTrue($result->isDirty('tags'));
  1738. }
  1739. /**
  1740. * Test that the ids option restricts to only accepting ids for belongs to many associations.
  1741. *
  1742. * @return void
  1743. */
  1744. public function testMergeBelongsToManyOnlyIdsRejectArray()
  1745. {
  1746. $entity = new Entity([
  1747. 'title' => 'Haz tags',
  1748. 'body' => 'Some content here',
  1749. 'tags' => [
  1750. new Entity(['id' => 1, 'name' => 'Cake']),
  1751. new Entity(['id' => 2, 'name' => 'PHP'])
  1752. ]
  1753. ]);
  1754. $data = [
  1755. 'title' => 'Haz moar tags',
  1756. 'tags' => [
  1757. ['name' => 'new'],
  1758. ['name' => 'awesome']
  1759. ]
  1760. ];
  1761. $entity->setAccess('*', true);
  1762. $marshall = new Marshaller($this->articles);
  1763. $result = $marshall->merge($entity, $data, [
  1764. 'associated' => ['Tags' => ['onlyIds' => true]]
  1765. ]);
  1766. $this->assertCount(0, $result->tags);
  1767. $this->assertTrue($result->isDirty('tags'));
  1768. }
  1769. /**
  1770. * Test that the ids option restricts to only accepting ids for belongs to many associations.
  1771. *
  1772. * @return void
  1773. */
  1774. public function testMergeBelongsToManyOnlyIdsWithIds()
  1775. {
  1776. $entity = new Entity([
  1777. 'title' => 'Haz tags',
  1778. 'body' => 'Some content here',
  1779. 'tags' => [
  1780. new Entity(['id' => 1, 'name' => 'Cake']),
  1781. new Entity(['id' => 2, 'name' => 'PHP'])
  1782. ]
  1783. ]);
  1784. $data = [
  1785. 'title' => 'Haz moar tags',
  1786. 'tags' => [
  1787. '_ids' => [3]
  1788. ]
  1789. ];
  1790. $entity->setAccess('*', true);
  1791. $marshall = new Marshaller($this->articles);
  1792. $result = $marshall->merge($entity, $data, [
  1793. 'associated' => ['Tags' => ['ids' => true]]
  1794. ]);
  1795. $this->assertCount(1, $result->tags);
  1796. $this->assertEquals('tag3', $result->tags[0]->name);
  1797. $this->assertTrue($result->isDirty('tags'));
  1798. }
  1799. /**
  1800. * Test that invalid _joinData (scalar data) is not marshalled.
  1801. *
  1802. * @return void
  1803. */
  1804. public function testMergeBelongsToManyJoinDataScalar()
  1805. {
  1806. TableRegistry::clear();
  1807. $articles = TableRegistry::get('Articles');
  1808. $articles->belongsToMany('Tags', [
  1809. 'through' => 'SpecialTags'
  1810. ]);
  1811. $entity = $articles->get(1, ['contain' => 'Tags']);
  1812. $data = [
  1813. 'title' => 'Haz data',
  1814. 'tags' => [
  1815. ['id' => 3, 'tag' => 'Cake', '_joinData' => 'Invalid'],
  1816. ]
  1817. ];
  1818. $marshall = new Marshaller($articles);
  1819. $result = $marshall->merge($entity, $data, ['associated' => 'Tags._joinData']);
  1820. $articles->save($entity, ['associated' => ['Tags._joinData']]);
  1821. $this->assertFalse($entity->tags[0]->isDirty('_joinData'));
  1822. $this->assertEmpty($entity->tags[0]->_joinData);
  1823. }
  1824. /**
  1825. * Test merging the _joinData entity for belongstomany associations when * is not
  1826. * accessible.
  1827. *
  1828. * @return void
  1829. */
  1830. public function testMergeBelongsToManyJoinDataNotAccessible()
  1831. {
  1832. TableRegistry::clear();
  1833. $articles = TableRegistry::get('Articles');
  1834. $articles->belongsToMany('Tags', [
  1835. 'through' => 'SpecialTags'
  1836. ]);
  1837. $entity = $articles->get(1, ['contain' => 'Tags']);
  1838. // Make only specific fields accessible, but not _joinData.
  1839. $entity->tags[0]->setAccess('*', false);
  1840. $entity->tags[0]->setAccess(['article_id', 'tag_id'], true);
  1841. $data = [
  1842. 'title' => 'Haz data',
  1843. 'tags' => [
  1844. ['id' => 3, 'tag' => 'Cake', '_joinData' => ['highlighted' => '1', 'author_id' => '99']],
  1845. ]
  1846. ];
  1847. $marshall = new Marshaller($articles);
  1848. $result = $marshall->merge($entity, $data, ['associated' => 'Tags._joinData']);
  1849. $this->assertTrue($entity->isDirty('tags'), 'Association data changed');
  1850. $this->assertTrue($entity->tags[0]->isDirty('_joinData'));
  1851. $this->assertTrue($result->tags[0]->_joinData->isDirty('author_id'), 'Field not modified');
  1852. $this->assertTrue($result->tags[0]->_joinData->isDirty('highlighted'), 'Field not modified');
  1853. $this->assertSame(99, $result->tags[0]->_joinData->author_id);
  1854. $this->assertTrue($result->tags[0]->_joinData->highlighted);
  1855. }
  1856. /**
  1857. * Test that _joinData is marshalled consistently with both
  1858. * new and existing records
  1859. *
  1860. * @return void
  1861. */
  1862. public function testMergeBelongsToManyHandleJoinDataConsistently()
  1863. {
  1864. TableRegistry::clear();
  1865. $articles = TableRegistry::get('Articles');
  1866. $articles->belongsToMany('Tags', [
  1867. 'through' => 'SpecialTags'
  1868. ]);
  1869. $entity = $articles->get(1);
  1870. $data = [
  1871. 'title' => 'Haz data',
  1872. 'tags' => [
  1873. ['id' => 3, 'tag' => 'Cake', '_joinData' => ['highlighted' => true]],
  1874. ]
  1875. ];
  1876. $marshall = new Marshaller($articles);
  1877. $result = $marshall->merge($entity, $data, ['associated' => 'Tags']);
  1878. $this->assertTrue($entity->isDirty('tags'));
  1879. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]->_joinData);
  1880. $this->assertTrue($result->tags[0]->_joinData->highlighted);
  1881. // Also ensure merge() overwrites existing data.
  1882. $entity = $articles->get(1, ['contain' => 'Tags']);
  1883. $data = [
  1884. 'title' => 'Haz data',
  1885. 'tags' => [
  1886. ['id' => 3, 'tag' => 'Cake', '_joinData' => ['highlighted' => true]],
  1887. ]
  1888. ];
  1889. $marshall = new Marshaller($articles);
  1890. $result = $marshall->merge($entity, $data, ['associated' => 'Tags']);
  1891. $this->assertTrue($entity->isDirty('tags'), 'association data changed');
  1892. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]->_joinData);
  1893. $this->assertTrue($result->tags[0]->_joinData->highlighted);
  1894. }
  1895. /**
  1896. * Test merging belongsToMany data doesn't create 'new' entities.
  1897. *
  1898. * @return void
  1899. */
  1900. public function testMergeBelongsToManyJoinDataAssociatedWithIds()
  1901. {
  1902. $data = [
  1903. 'title' => 'My title',
  1904. 'tags' => [
  1905. [
  1906. 'id' => 1,
  1907. '_joinData' => [
  1908. 'active' => 1,
  1909. 'user' => ['username' => 'MyLux'],
  1910. ]
  1911. ],
  1912. [
  1913. 'id' => 2,
  1914. '_joinData' => [
  1915. 'active' => 0,
  1916. 'user' => ['username' => 'IronFall'],
  1917. ]
  1918. ],
  1919. ],
  1920. ];
  1921. $articlesTags = TableRegistry::get('ArticlesTags');
  1922. $articlesTags->belongsTo('Users');
  1923. $marshall = new Marshaller($this->articles);
  1924. $article = $this->articles->get(1, ['associated' => 'Tags']);
  1925. $result = $marshall->merge($article, $data, ['associated' => ['Tags._joinData.Users']]);
  1926. $this->assertTrue($result->isDirty('tags'));
  1927. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  1928. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  1929. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]->_joinData->user);
  1930. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]->_joinData->user);
  1931. $this->assertFalse($result->tags[0]->isNew(), 'Should not be new, as id is in db.');
  1932. $this->assertFalse($result->tags[1]->isNew(), 'Should not be new, as id is in db.');
  1933. $this->assertEquals(1, $result->tags[0]->id);
  1934. $this->assertEquals(2, $result->tags[1]->id);
  1935. $this->assertEquals(1, $result->tags[0]->_joinData->active);
  1936. $this->assertEquals(0, $result->tags[1]->_joinData->active);
  1937. $this->assertEquals(
  1938. $data['tags'][0]['_joinData']['user']['username'],
  1939. $result->tags[0]->_joinData->user->username
  1940. );
  1941. $this->assertEquals(
  1942. $data['tags'][1]['_joinData']['user']['username'],
  1943. $result->tags[1]->_joinData->user->username
  1944. );
  1945. }
  1946. /**
  1947. * Test merging the _joinData entity for belongstomany associations.
  1948. *
  1949. * @return void
  1950. */
  1951. public function testMergeBelongsToManyJoinData()
  1952. {
  1953. $data = [
  1954. 'title' => 'My title',
  1955. 'body' => 'My content',
  1956. 'author_id' => 1,
  1957. 'tags' => [
  1958. [
  1959. 'id' => 1,
  1960. 'tag' => 'news',
  1961. '_joinData' => [
  1962. 'active' => 0
  1963. ]
  1964. ],
  1965. [
  1966. 'id' => 2,
  1967. 'tag' => 'cakephp',
  1968. '_joinData' => [
  1969. 'active' => 0
  1970. ]
  1971. ],
  1972. ],
  1973. ];
  1974. $options = ['associated' => ['Tags._joinData']];
  1975. $marshall = new Marshaller($this->articles);
  1976. $entity = $marshall->one($data, $options);
  1977. $entity->setAccess('*', true);
  1978. $data = [
  1979. 'title' => 'Haz data',
  1980. 'tags' => [
  1981. ['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar']],
  1982. ['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]
  1983. ]
  1984. ];
  1985. $tag1 = $entity->tags[0];
  1986. $result = $marshall->merge($entity, $data, $options);
  1987. $this->assertEquals($data['title'], $result->title);
  1988. $this->assertEquals('My content', $result->body);
  1989. $this->assertTrue($result->isDirty('tags'));
  1990. $this->assertSame($tag1, $entity->tags[0]);
  1991. $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
  1992. $this->assertSame(
  1993. ['active' => 0, 'foo' => 'bar'],
  1994. $entity->tags[0]->_joinData->toArray()
  1995. );
  1996. $this->assertSame(
  1997. ['active' => 1, 'foo' => 'baz'],
  1998. $entity->tags[1]->_joinData->toArray()
  1999. );
  2000. $this->assertEquals('new tag', $entity->tags[1]->tag);
  2001. $this->assertTrue($entity->tags[0]->isDirty('_joinData'));
  2002. $this->assertTrue($entity->tags[1]->isDirty('_joinData'));
  2003. }
  2004. /**
  2005. * Test merging associations inside _joinData
  2006. *
  2007. * @return void
  2008. */
  2009. public function testMergeJoinDataAssociations()
  2010. {
  2011. $data = [
  2012. 'title' => 'My title',
  2013. 'body' => 'My content',
  2014. 'author_id' => 1,
  2015. 'tags' => [
  2016. [
  2017. 'id' => 1,
  2018. 'tag' => 'news',
  2019. '_joinData' => [
  2020. 'active' => 0,
  2021. 'user' => ['username' => 'Bill']
  2022. ]
  2023. ],
  2024. [
  2025. 'id' => 2,
  2026. 'tag' => 'cakephp',
  2027. '_joinData' => [
  2028. 'active' => 0
  2029. ]
  2030. ],
  2031. ]
  2032. ];
  2033. $articlesTags = TableRegistry::get('ArticlesTags');
  2034. $articlesTags->belongsTo('Users');
  2035. $options = ['associated' => ['Tags._joinData.Users']];
  2036. $marshall = new Marshaller($this->articles);
  2037. $entity = $marshall->one($data, $options);
  2038. $entity->setAccess('*', true);
  2039. $data = [
  2040. 'title' => 'Haz data',
  2041. 'tags' => [
  2042. [
  2043. 'id' => 1,
  2044. 'tag' => 'news',
  2045. '_joinData' => [
  2046. 'foo' => 'bar',
  2047. 'user' => ['password' => 'secret']
  2048. ]
  2049. ],
  2050. [
  2051. 'id' => 2,
  2052. '_joinData' => [
  2053. 'active' => 1,
  2054. 'foo' => 'baz',
  2055. 'user' => ['username' => 'ber']
  2056. ]
  2057. ]
  2058. ]
  2059. ];
  2060. $tag1 = $entity->tags[0];
  2061. $result = $marshall->merge($entity, $data, $options);
  2062. $this->assertEquals($data['title'], $result->title);
  2063. $this->assertEquals('My content', $result->body);
  2064. $this->assertTrue($entity->isDirty('tags'));
  2065. $this->assertSame($tag1, $entity->tags[0]);
  2066. $this->assertTrue($tag1->isDirty('_joinData'));
  2067. $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
  2068. $this->assertEquals('Bill', $entity->tags[0]->_joinData->user->username);
  2069. $this->assertEquals('secret', $entity->tags[0]->_joinData->user->password);
  2070. $this->assertEquals('ber', $entity->tags[1]->_joinData->user->username);
  2071. }
  2072. /**
  2073. * Tests that merging belongsToMany association doesn't erase _joinData
  2074. * on existing objects.
  2075. *
  2076. * @return void
  2077. */
  2078. public function testMergeBelongsToManyIdsRetainJoinData()
  2079. {
  2080. $this->articles->belongsToMany('Tags');
  2081. $entity = $this->articles->get(1, ['contain' => ['Tags']]);
  2082. $entity->setAccess('*', true);
  2083. $original = $entity->tags[0]->_joinData;
  2084. $this->assertInstanceOf('Cake\ORM\Entity', $entity->tags[0]->_joinData);
  2085. $data = [
  2086. 'title' => 'Haz moar tags',
  2087. 'tags' => [
  2088. ['id' => 1],
  2089. ]
  2090. ];
  2091. $marshall = new Marshaller($this->articles);
  2092. $result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
  2093. $this->assertCount(1, $result->tags);
  2094. $this->assertTrue($result->isDirty('tags'));
  2095. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  2096. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]->_joinData);
  2097. $this->assertSame($original, $result->tags[0]->_joinData, 'Should be same object');
  2098. }
  2099. /**
  2100. * Test mergeMany() with a simple set of data.
  2101. *
  2102. * @return void
  2103. */
  2104. public function testMergeManySimple()
  2105. {
  2106. $entities = [
  2107. new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2]),
  2108. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  2109. ];
  2110. $entities[0]->clean();
  2111. $entities[1]->clean();
  2112. $data = [
  2113. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  2114. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1]
  2115. ];
  2116. $marshall = new Marshaller($this->comments);
  2117. $result = $marshall->mergeMany($entities, $data);
  2118. $this->assertSame($entities[0], $result[0]);
  2119. $this->assertSame($entities[1], $result[1]);
  2120. $this->assertEquals('Changed 1', $result[0]->comment);
  2121. $this->assertEquals(1, $result[0]->user_id);
  2122. $this->assertEquals('Changed 2', $result[1]->comment);
  2123. $this->assertTrue($result[0]->isDirty('user_id'));
  2124. $this->assertFalse($result[1]->isDirty('user_id'));
  2125. }
  2126. /**
  2127. * Test mergeMany() with some invalid data
  2128. *
  2129. * @return void
  2130. */
  2131. public function testMergeManyInvalidData()
  2132. {
  2133. $entities = [
  2134. new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2]),
  2135. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  2136. ];
  2137. $entities[0]->clean();
  2138. $entities[1]->clean();
  2139. $data = [
  2140. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  2141. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1],
  2142. '_csrfToken' => 'abc123',
  2143. ];
  2144. $marshall = new Marshaller($this->comments);
  2145. $result = $marshall->mergeMany($entities, $data);
  2146. $this->assertSame($entities[0], $result[0]);
  2147. $this->assertSame($entities[1], $result[1]);
  2148. }
  2149. /**
  2150. * Tests that only records found in the data array are returned, those that cannot
  2151. * be matched are discarded
  2152. *
  2153. * @return void
  2154. */
  2155. public function testMergeManyWithAppend()
  2156. {
  2157. $entities = [
  2158. new OpenEntity(['comment' => 'First post', 'user_id' => 2]),
  2159. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  2160. ];
  2161. $entities[0]->clean();
  2162. $entities[1]->clean();
  2163. $data = [
  2164. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  2165. ['id' => 1, 'comment' => 'Comment 1', 'user_id' => 1]
  2166. ];
  2167. $marshall = new Marshaller($this->comments);
  2168. $result = $marshall->mergeMany($entities, $data);
  2169. $this->assertCount(2, $result);
  2170. $this->assertNotSame($entities[0], $result[0]);
  2171. $this->assertSame($entities[1], $result[0]);
  2172. $this->assertEquals('Changed 2', $result[0]->comment);
  2173. $this->assertEquals('Comment 1', $result[1]->comment);
  2174. }
  2175. /**
  2176. * Test that mergeMany() handles composite key associations properly.
  2177. *
  2178. * The articles_tags table has a composite primary key, and should be
  2179. * handled correctly.
  2180. *
  2181. * @return void
  2182. */
  2183. public function testMergeManyCompositeKey()
  2184. {
  2185. $articlesTags = TableRegistry::get('ArticlesTags');
  2186. $entities = [
  2187. new OpenEntity(['article_id' => 1, 'tag_id' => 2]),
  2188. new OpenEntity(['article_id' => 1, 'tag_id' => 1]),
  2189. ];
  2190. $entities[0]->clean();
  2191. $entities[1]->clean();
  2192. $data = [
  2193. ['article_id' => 1, 'tag_id' => 1],
  2194. ['article_id' => 1, 'tag_id' => 2]
  2195. ];
  2196. $marshall = new Marshaller($articlesTags);
  2197. $result = $marshall->mergeMany($entities, $data);
  2198. $this->assertCount(2, $result, 'Should have two records');
  2199. $this->assertSame($entities[0], $result[0], 'Should retain object');
  2200. $this->assertSame($entities[1], $result[1], 'Should retain object');
  2201. }
  2202. /**
  2203. * Test mergeMany() with forced contain to ensure aliases are used in queries.
  2204. *
  2205. * @return void
  2206. */
  2207. public function testMergeManyExistingQueryAliases()
  2208. {
  2209. $entities = [
  2210. new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2], ['markClean' => true]),
  2211. ];
  2212. $data = [
  2213. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1],
  2214. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  2215. ];
  2216. $this->comments->getEventManager()->on('Model.beforeFind', function (Event $event, $query) {
  2217. return $query->contain(['Articles']);
  2218. });
  2219. $marshall = new Marshaller($this->comments);
  2220. $result = $marshall->mergeMany($entities, $data);
  2221. $this->assertSame($entities[0], $result[0]);
  2222. }
  2223. /**
  2224. * Test mergeMany() when the exist check returns nothing.
  2225. *
  2226. * @return void
  2227. */
  2228. public function testMergeManyExistQueryFails()
  2229. {
  2230. $entities = [
  2231. new Entity(['id' => 1, 'comment' => 'First post', 'user_id' => 2]),
  2232. new Entity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  2233. ];
  2234. $entities[0]->clean();
  2235. $entities[1]->clean();
  2236. $data = [
  2237. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
  2238. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1],
  2239. ['id' => 3, 'comment' => 'New 1'],
  2240. ];
  2241. $comments = TableRegistry::get('GreedyComments', [
  2242. 'className' => __NAMESPACE__ . '\\GreedyCommentsTable'
  2243. ]);
  2244. $marshall = new Marshaller($comments);
  2245. $result = $marshall->mergeMany($entities, $data);
  2246. $this->assertCount(3, $result);
  2247. $this->assertEquals('Changed 1', $result[0]->comment);
  2248. $this->assertEquals(1, $result[0]->user_id);
  2249. $this->assertEquals('Changed 2', $result[1]->comment);
  2250. $this->assertEquals('New 1', $result[2]->comment);
  2251. }
  2252. /**
  2253. * Tests merge with data types that need to be marshalled
  2254. *
  2255. * @return void
  2256. */
  2257. public function testMergeComplexType()
  2258. {
  2259. $entity = new Entity(
  2260. ['comment' => 'My Comment text'],
  2261. ['markNew' => false, 'markClean' => true]
  2262. );
  2263. $data = [
  2264. 'created' => [
  2265. 'year' => '2014',
  2266. 'month' => '2',
  2267. 'day' => 14
  2268. ]
  2269. ];
  2270. $marshall = new Marshaller($this->comments);
  2271. $result = $marshall->merge($entity, $data);
  2272. $this->assertInstanceOf('DateTime', $entity->created);
  2273. $this->assertEquals('2014-02-14', $entity->created->format('Y-m-d'));
  2274. }
  2275. /**
  2276. * Tests that it is possible to pass a fields option to the marshaller
  2277. *
  2278. * @group deprecated
  2279. * @return void
  2280. */
  2281. public function testOneWithFieldList()
  2282. {
  2283. $this->deprecated(function () {
  2284. $data = [
  2285. 'title' => 'My title',
  2286. 'body' => 'My content',
  2287. 'author_id' => null
  2288. ];
  2289. $marshall = new Marshaller($this->articles);
  2290. $result = $marshall->one($data, ['fieldList' => ['title', 'author_id']]);
  2291. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  2292. unset($data['body']);
  2293. $this->assertEquals($data, $result->toArray());
  2294. });
  2295. }
  2296. /**
  2297. * Tests that it is possible to pass a fields option to the marshaller
  2298. *
  2299. * @return void
  2300. */
  2301. public function testOneWithFields()
  2302. {
  2303. $data = [
  2304. 'title' => 'My title',
  2305. 'body' => 'My content',
  2306. 'author_id' => null
  2307. ];
  2308. $marshall = new Marshaller($this->articles);
  2309. $result = $marshall->one($data, ['fields' => ['title', 'author_id']]);
  2310. $this->assertInstanceOf('Cake\ORM\Entity', $result);
  2311. unset($data['body']);
  2312. $this->assertEquals($data, $result->toArray());
  2313. }
  2314. /**
  2315. * Test one() with translations
  2316. *
  2317. * @return void
  2318. */
  2319. public function testOneWithTranslations()
  2320. {
  2321. $this->articles->addBehavior('Translate', [
  2322. 'fields' => ['title', 'body']
  2323. ]);
  2324. $data = [
  2325. 'author_id' => 1,
  2326. '_translations' => [
  2327. 'en' => [
  2328. 'title' => 'English Title',
  2329. 'body' => 'English Content'
  2330. ],
  2331. 'es' => [
  2332. 'title' => 'Titulo Español',
  2333. 'body' => 'Contenido Español'
  2334. ]
  2335. ],
  2336. 'user' => [
  2337. 'id' => 1,
  2338. 'username' => 'mark'
  2339. ]
  2340. ];
  2341. $marshall = new Marshaller($this->articles);
  2342. $result = $marshall->one($data, ['associated' => ['Users']]);
  2343. $this->assertEmpty($result->getErrors());
  2344. $this->assertEquals(1, $result->author_id);
  2345. $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $result->user);
  2346. $this->assertEquals('mark', $result->user->username);
  2347. $translations = $result->get('_translations');
  2348. $this->assertCount(2, $translations);
  2349. $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['en']);
  2350. $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['es']);
  2351. $this->assertEquals($data['_translations']['en'], $translations['en']->toArray());
  2352. }
  2353. /**
  2354. * Tests that it is possible to pass a fields option to the merge method
  2355. *
  2356. * @group deprecated
  2357. * @return void
  2358. */
  2359. public function testMergeWithFieldList()
  2360. {
  2361. $this->deprecated(function () {
  2362. $data = [
  2363. 'title' => 'My title',
  2364. 'body' => null,
  2365. 'author_id' => 1
  2366. ];
  2367. $marshall = new Marshaller($this->articles);
  2368. $entity = new Entity([
  2369. 'title' => 'Foo',
  2370. 'body' => 'My content',
  2371. 'author_id' => 2
  2372. ]);
  2373. $entity->setAccess('*', false);
  2374. $entity->isNew(false);
  2375. $entity->clean();
  2376. $result = $marshall->merge($entity, $data, ['fieldList' => ['title', 'body']]);
  2377. $expected = [
  2378. 'title' => 'My title',
  2379. 'body' => null,
  2380. 'author_id' => 2
  2381. ];
  2382. $this->assertSame($entity, $result);
  2383. $this->assertEquals($expected, $result->toArray());
  2384. $this->assertFalse($entity->isAccessible('*'));
  2385. });
  2386. }
  2387. /**
  2388. * Tests that it is possible to pass a fields option to the merge method
  2389. *
  2390. * @return void
  2391. */
  2392. public function testMergeWithFields()
  2393. {
  2394. $data = [
  2395. 'title' => 'My title',
  2396. 'body' => null,
  2397. 'author_id' => 1
  2398. ];
  2399. $marshall = new Marshaller($this->articles);
  2400. $entity = new Entity([
  2401. 'title' => 'Foo',
  2402. 'body' => 'My content',
  2403. 'author_id' => 2
  2404. ]);
  2405. $entity->setAccess('*', false);
  2406. $entity->isNew(false);
  2407. $entity->clean();
  2408. $result = $marshall->merge($entity, $data, ['fields' => ['title', 'body']]);
  2409. $expected = [
  2410. 'title' => 'My title',
  2411. 'body' => null,
  2412. 'author_id' => 2
  2413. ];
  2414. $this->assertSame($entity, $result);
  2415. $this->assertEquals($expected, $result->toArray());
  2416. $this->assertFalse($entity->isAccessible('*'));
  2417. }
  2418. /**
  2419. * Test that many() also receives a fields option
  2420. *
  2421. * @return void
  2422. */
  2423. public function testManyFields()
  2424. {
  2425. $data = [
  2426. ['comment' => 'First post', 'user_id' => 2, 'foo' => 'bar'],
  2427. ['comment' => 'Second post', 'user_id' => 2, 'foo' => 'bar'],
  2428. ];
  2429. $marshall = new Marshaller($this->comments);
  2430. $result = $marshall->many($data, ['fields' => ['comment', 'user_id']]);
  2431. $this->assertCount(2, $result);
  2432. unset($data[0]['foo'], $data[1]['foo']);
  2433. $this->assertEquals($data[0], $result[0]->toArray());
  2434. $this->assertEquals($data[1], $result[1]->toArray());
  2435. }
  2436. /**
  2437. * Test that many() also receives a fields option
  2438. *
  2439. * @return void
  2440. */
  2441. public function testMergeManyFields()
  2442. {
  2443. $entities = [
  2444. new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2]),
  2445. new OpenEntity(['id' => 2, 'comment' => 'Second post', 'user_id' => 2])
  2446. ];
  2447. $entities[0]->clean();
  2448. $entities[1]->clean();
  2449. $data = [
  2450. ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 10],
  2451. ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 20]
  2452. ];
  2453. $marshall = new Marshaller($this->comments);
  2454. $result = $marshall->mergeMany($entities, $data, ['fields' => ['id', 'comment']]);
  2455. $this->assertSame($entities[0], $result[0]);
  2456. $this->assertSame($entities[1], $result[1]);
  2457. $expected = ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2];
  2458. $this->assertEquals($expected, $entities[1]->toArray());
  2459. $expected = ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 2];
  2460. $this->assertEquals($expected, $entities[0]->toArray());
  2461. }
  2462. /**
  2463. * test marshalling association data while passing a fields
  2464. *
  2465. * @return void
  2466. */
  2467. public function testAssociationsFields()
  2468. {
  2469. $data = [
  2470. 'title' => 'My title',
  2471. 'body' => 'My content',
  2472. 'author_id' => 1,
  2473. 'user' => [
  2474. 'username' => 'mark',
  2475. 'password' => 'secret',
  2476. 'foo' => 'bar'
  2477. ]
  2478. ];
  2479. $marshall = new Marshaller($this->articles);
  2480. $result = $marshall->one($data, [
  2481. 'fields' => ['title', 'body', 'user'],
  2482. 'associated' => [
  2483. 'Users' => ['fields' => ['username', 'foo']]
  2484. ]
  2485. ]);
  2486. $this->assertEquals($data['title'], $result->title);
  2487. $this->assertEquals($data['body'], $result->body);
  2488. $this->assertNull($result->author_id);
  2489. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  2490. $this->assertEquals($data['user']['username'], $result->user->username);
  2491. $this->assertNull($result->user->password);
  2492. }
  2493. /**
  2494. * Tests merging associated data with a fields
  2495. *
  2496. * @return void
  2497. */
  2498. public function testMergeAssociationWithfields()
  2499. {
  2500. $user = new Entity([
  2501. 'username' => 'mark',
  2502. 'password' => 'secret'
  2503. ]);
  2504. $entity = new Entity([
  2505. 'tile' => 'My Title',
  2506. 'user' => $user
  2507. ]);
  2508. $user->setAccess('*', true);
  2509. $entity->setAccess('*', true);
  2510. $data = [
  2511. 'body' => 'My Content',
  2512. 'something' => 'else',
  2513. 'user' => [
  2514. 'password' => 'not a secret',
  2515. 'extra' => 'data'
  2516. ]
  2517. ];
  2518. $marshall = new Marshaller($this->articles);
  2519. $marshall->merge($entity, $data, [
  2520. 'fields' => ['something'],
  2521. 'associated' => ['Users' => ['fields' => ['extra']]]
  2522. ]);
  2523. $this->assertNull($entity->body);
  2524. $this->assertEquals('else', $entity->something);
  2525. $this->assertSame($user, $entity->user);
  2526. $this->assertEquals('mark', $entity->user->username);
  2527. $this->assertEquals('secret', $entity->user->password);
  2528. $this->assertEquals('data', $entity->user->extra);
  2529. $this->assertTrue($entity->isDirty('user'));
  2530. }
  2531. /**
  2532. * Test marshalling nested associations on the _joinData structure
  2533. * while having a fields
  2534. *
  2535. * @return void
  2536. */
  2537. public function testJoinDataWhiteList()
  2538. {
  2539. $data = [
  2540. 'title' => 'My title',
  2541. 'body' => 'My content',
  2542. 'author_id' => 1,
  2543. 'tags' => [
  2544. [
  2545. 'tag' => 'news',
  2546. '_joinData' => [
  2547. 'active' => 1,
  2548. 'crazy' => 'data',
  2549. 'user' => ['username' => 'Bill'],
  2550. ]
  2551. ],
  2552. [
  2553. 'tag' => 'cakephp',
  2554. '_joinData' => [
  2555. 'active' => 0,
  2556. 'crazy' => 'stuff',
  2557. 'user' => ['username' => 'Mark'],
  2558. ]
  2559. ],
  2560. ],
  2561. ];
  2562. $articlesTags = TableRegistry::get('ArticlesTags');
  2563. $articlesTags->belongsTo('Users');
  2564. $marshall = new Marshaller($this->articles);
  2565. $result = $marshall->one($data, [
  2566. 'associated' => [
  2567. 'Tags._joinData' => ['fields' => ['active', 'user']],
  2568. 'Tags._joinData.Users'
  2569. ]
  2570. ]);
  2571. $this->assertInstanceOf(
  2572. 'Cake\ORM\Entity',
  2573. $result->tags[0]->_joinData->user,
  2574. 'joinData should contain a user entity.'
  2575. );
  2576. $this->assertEquals('Bill', $result->tags[0]->_joinData->user->username);
  2577. $this->assertInstanceOf(
  2578. 'Cake\ORM\Entity',
  2579. $result->tags[1]->_joinData->user,
  2580. 'joinData should contain a user entity.'
  2581. );
  2582. $this->assertEquals('Mark', $result->tags[1]->_joinData->user->username);
  2583. $this->assertNull($result->tags[0]->_joinData->crazy);
  2584. $this->assertNull($result->tags[1]->_joinData->crazy);
  2585. }
  2586. /**
  2587. * Test merging the _joinData entity for belongstomany associations
  2588. * while passing a whitelist
  2589. *
  2590. * @return void
  2591. */
  2592. public function testMergeJoinDataWithFields()
  2593. {
  2594. $data = [
  2595. 'title' => 'My title',
  2596. 'body' => 'My content',
  2597. 'author_id' => 1,
  2598. 'tags' => [
  2599. [
  2600. 'id' => 1,
  2601. 'tag' => 'news',
  2602. '_joinData' => [
  2603. 'active' => 0
  2604. ]
  2605. ],
  2606. [
  2607. 'id' => 2,
  2608. 'tag' => 'cakephp',
  2609. '_joinData' => [
  2610. 'active' => 0
  2611. ]
  2612. ],
  2613. ],
  2614. ];
  2615. $options = ['associated' => ['Tags' => ['associated' => ['_joinData']]]];
  2616. $marshall = new Marshaller($this->articles);
  2617. $entity = $marshall->one($data, $options);
  2618. $entity->setAccess('*', true);
  2619. $data = [
  2620. 'title' => 'Haz data',
  2621. 'tags' => [
  2622. ['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar', 'crazy' => 'something']],
  2623. ['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]
  2624. ]
  2625. ];
  2626. $tag1 = $entity->tags[0];
  2627. $result = $marshall->merge($entity, $data, [
  2628. 'associated' => ['Tags._joinData' => ['fields' => ['foo']]]
  2629. ]);
  2630. $this->assertEquals($data['title'], $result->title);
  2631. $this->assertEquals('My content', $result->body);
  2632. $this->assertSame($tag1, $entity->tags[0]);
  2633. $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
  2634. $this->assertSame(
  2635. ['active' => 0, 'foo' => 'bar'],
  2636. $entity->tags[0]->_joinData->toArray()
  2637. );
  2638. $this->assertSame(
  2639. ['foo' => 'baz'],
  2640. $entity->tags[1]->_joinData->toArray()
  2641. );
  2642. $this->assertEquals('new tag', $entity->tags[1]->tag);
  2643. $this->assertTrue($entity->tags[0]->isDirty('_joinData'));
  2644. $this->assertTrue($entity->tags[1]->isDirty('_joinData'));
  2645. }
  2646. /**
  2647. * Tests marshalling with validation errors
  2648. *
  2649. * @return void
  2650. */
  2651. public function testValidationFail()
  2652. {
  2653. $data = [
  2654. 'title' => 'Thing',
  2655. 'body' => 'hey'
  2656. ];
  2657. $this->articles->getValidator()->requirePresence('thing');
  2658. $marshall = new Marshaller($this->articles);
  2659. $entity = $marshall->one($data);
  2660. $this->assertNotEmpty($entity->getError('thing'));
  2661. }
  2662. /**
  2663. * Test that invalid validate options raise exceptions
  2664. *
  2665. * @return void
  2666. */
  2667. public function testValidateInvalidType()
  2668. {
  2669. $this->expectException(\RuntimeException::class);
  2670. $data = ['title' => 'foo'];
  2671. $marshaller = new Marshaller($this->articles);
  2672. $marshaller->one($data, [
  2673. 'validate' => ['derp'],
  2674. ]);
  2675. }
  2676. /**
  2677. * Tests that associations are validated and custom validators can be used
  2678. *
  2679. * @return void
  2680. */
  2681. public function testValidateWithAssociationsAndCustomValidator()
  2682. {
  2683. $data = [
  2684. 'title' => 'foo',
  2685. 'body' => 'bar',
  2686. 'user' => [
  2687. 'name' => 'Susan'
  2688. ],
  2689. 'comments' => [
  2690. [
  2691. 'comment' => 'foo'
  2692. ]
  2693. ]
  2694. ];
  2695. $validator = (new Validator)->add('body', 'numeric', ['rule' => 'numeric']);
  2696. $this->articles->setValidator('custom', $validator);
  2697. $validator2 = (new Validator)->requirePresence('thing');
  2698. $this->articles->Users->setValidator('customThing', $validator2);
  2699. $this->articles->Comments->setValidator('default', $validator2);
  2700. $entity = (new Marshaller($this->articles))->one($data, [
  2701. 'validate' => 'custom',
  2702. 'associated' => ['Users', 'Comments']
  2703. ]);
  2704. $this->assertNotEmpty($entity->getError('body'), 'custom was not used');
  2705. $this->assertNull($entity->body);
  2706. $this->assertEmpty($entity->user->getError('thing'));
  2707. $this->assertNotEmpty($entity->comments[0]->getError('thing'));
  2708. $entity = (new Marshaller($this->articles))->one($data, [
  2709. 'validate' => 'custom',
  2710. 'associated' => ['Users' => ['validate' => 'customThing'], 'Comments']
  2711. ]);
  2712. $this->assertNotEmpty($entity->getError('body'));
  2713. $this->assertNull($entity->body);
  2714. $this->assertNotEmpty($entity->user->getError('thing'), 'customThing was not used');
  2715. $this->assertNotEmpty($entity->comments[0]->getError('thing'));
  2716. }
  2717. /**
  2718. * Tests that validation can be bypassed
  2719. *
  2720. * @return void
  2721. */
  2722. public function testSkipValidation()
  2723. {
  2724. $data = [
  2725. 'title' => 'foo',
  2726. 'body' => 'bar',
  2727. 'user' => [
  2728. 'name' => 'Susan'
  2729. ],
  2730. ];
  2731. $validator = (new Validator)->requirePresence('thing');
  2732. $this->articles->setValidator('default', $validator);
  2733. $this->articles->Users->setValidator('default', $validator);
  2734. $entity = (new Marshaller($this->articles))->one($data, [
  2735. 'validate' => false,
  2736. 'associated' => ['Users']
  2737. ]);
  2738. $this->assertEmpty($entity->getError('thing'));
  2739. $this->assertNotEmpty($entity->user->getError('thing'));
  2740. $entity = (new Marshaller($this->articles))->one($data, [
  2741. 'associated' => ['Users' => ['validate' => false]]
  2742. ]);
  2743. $this->assertNotEmpty($entity->getError('thing'));
  2744. $this->assertEmpty($entity->user->getError('thing'));
  2745. }
  2746. /**
  2747. * Tests that it is possible to pass a validator directly in the options
  2748. *
  2749. * @return void
  2750. */
  2751. public function testPassingCustomValidator()
  2752. {
  2753. $data = [
  2754. 'title' => 'Thing',
  2755. 'body' => 'hey'
  2756. ];
  2757. $validator = clone $this->articles->getValidator();
  2758. $validator->requirePresence('thing');
  2759. $marshall = new Marshaller($this->articles);
  2760. $entity = $marshall->one($data, ['validate' => $validator]);
  2761. $this->assertNotEmpty($entity->getError('thing'));
  2762. }
  2763. /**
  2764. * Tests that invalid property is being filled when data cannot be patched into an entity.
  2765. *
  2766. * @return void
  2767. */
  2768. public function testValidationWithInvalidFilled()
  2769. {
  2770. $data = [
  2771. 'title' => 'foo',
  2772. 'number' => 'bar',
  2773. ];
  2774. $validator = (new Validator)->add('number', 'numeric', ['rule' => 'numeric']);
  2775. $marshall = new Marshaller($this->articles);
  2776. $entity = $marshall->one($data, ['validate' => $validator]);
  2777. $this->assertNotEmpty($entity->getError('number'));
  2778. $this->assertNull($entity->number);
  2779. $this->assertSame(['number' => 'bar'], $entity->getInvalid());
  2780. }
  2781. /**
  2782. * Test merge with validation error
  2783. *
  2784. * @return void
  2785. */
  2786. public function testMergeWithValidation()
  2787. {
  2788. $data = [
  2789. 'title' => 'My title',
  2790. 'author_id' => 'foo',
  2791. ];
  2792. $marshall = new Marshaller($this->articles);
  2793. $entity = new Entity([
  2794. 'id' => 1,
  2795. 'title' => 'Foo',
  2796. 'body' => 'My Content',
  2797. 'author_id' => 1
  2798. ]);
  2799. $this->assertEmpty($entity->getInvalid());
  2800. $entity->setAccess('*', true);
  2801. $entity->isNew(false);
  2802. $entity->clean();
  2803. $this->articles->getValidator()
  2804. ->requirePresence('thing', 'update')
  2805. ->requirePresence('id', 'update')
  2806. ->add('author_id', 'numeric', ['rule' => 'numeric'])
  2807. ->add('id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);
  2808. $expected = clone $entity;
  2809. $result = $marshall->merge($expected, $data, []);
  2810. $this->assertSame($expected, $result);
  2811. $this->assertSame(1, $result->author_id);
  2812. $this->assertNotEmpty($result->getError('thing'));
  2813. $this->assertEmpty($result->getError('id'));
  2814. $this->articles->getValidator()->requirePresence('thing', 'create');
  2815. $result = $marshall->merge($entity, $data, []);
  2816. $this->assertEmpty($result->getError('thing'));
  2817. $this->assertSame(['author_id' => 'foo'], $result->getInvalid());
  2818. }
  2819. /**
  2820. * Test merge with validation and create or update validation rules
  2821. *
  2822. * @return void
  2823. */
  2824. public function testMergeWithCreate()
  2825. {
  2826. $data = [
  2827. 'title' => 'My title',
  2828. 'author_id' => 'foo',
  2829. ];
  2830. $marshall = new Marshaller($this->articles);
  2831. $entity = new Entity([
  2832. 'title' => 'Foo',
  2833. 'body' => 'My Content',
  2834. 'author_id' => 1
  2835. ]);
  2836. $entity->setAccess('*', true);
  2837. $entity->isNew(true);
  2838. $entity->clean();
  2839. $this->articles->getValidator()
  2840. ->requirePresence('thing', 'update')
  2841. ->add('author_id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);
  2842. $expected = clone $entity;
  2843. $result = $marshall->merge($expected, $data, []);
  2844. $this->assertEmpty($result->getError('author_id'));
  2845. $this->assertEmpty($result->getError('thing'));
  2846. $entity->clean();
  2847. $entity->isNew(false);
  2848. $result = $marshall->merge($entity, $data, []);
  2849. $this->assertNotEmpty($result->getError('author_id'));
  2850. $this->assertNotEmpty($result->getError('thing'));
  2851. }
  2852. /**
  2853. * Test merge() with translate behavior integration
  2854. *
  2855. * @return void
  2856. */
  2857. public function testMergeWithTranslations()
  2858. {
  2859. $this->articles->addBehavior('Translate', [
  2860. 'fields' => ['title', 'body']
  2861. ]);
  2862. $data = [
  2863. 'author_id' => 1,
  2864. '_translations' => [
  2865. 'en' => [
  2866. 'title' => 'English Title',
  2867. 'body' => 'English Content'
  2868. ],
  2869. 'es' => [
  2870. 'title' => 'Titulo Español',
  2871. 'body' => 'Contenido Español'
  2872. ]
  2873. ]
  2874. ];
  2875. $marshall = new Marshaller($this->articles);
  2876. $entity = $this->articles->newEntity();
  2877. $result = $marshall->merge($entity, $data, []);
  2878. $this->assertSame($entity, $result);
  2879. $this->assertEmpty($result->getErrors());
  2880. $this->assertTrue($result->isDirty('_translations'));
  2881. $translations = $result->get('_translations');
  2882. $this->assertCount(2, $translations);
  2883. $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['en']);
  2884. $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['es']);
  2885. $this->assertEquals($data['_translations']['en'], $translations['en']->toArray());
  2886. }
  2887. /**
  2888. * Test Model.beforeMarshal event.
  2889. *
  2890. * @return void
  2891. */
  2892. public function testBeforeMarshalEvent()
  2893. {
  2894. $data = [
  2895. 'title' => 'My title',
  2896. 'body' => 'My content',
  2897. 'user' => [
  2898. 'name' => 'Robert',
  2899. 'username' => 'rob'
  2900. ]
  2901. ];
  2902. $marshall = new Marshaller($this->articles);
  2903. $this->articles->getEventManager()->on(
  2904. 'Model.beforeMarshal',
  2905. function ($e, $data, $options) {
  2906. $this->assertArrayHasKey('validate', $options);
  2907. $data['title'] = 'Modified title';
  2908. $data['user']['username'] = 'robert';
  2909. $options['associated'] = ['Users'];
  2910. }
  2911. );
  2912. $entity = $marshall->one($data);
  2913. $this->assertEquals('Modified title', $entity->title);
  2914. $this->assertEquals('My content', $entity->body);
  2915. $this->assertEquals('Robert', $entity->user->name);
  2916. $this->assertEquals('robert', $entity->user->username);
  2917. }
  2918. /**
  2919. * Test Model.beforeMarshal event on associated tables.
  2920. *
  2921. * @return void
  2922. */
  2923. public function testBeforeMarshalEventOnAssociations()
  2924. {
  2925. $data = [
  2926. 'title' => 'My title',
  2927. 'body' => 'My content',
  2928. 'author_id' => 1,
  2929. 'user' => [
  2930. 'username' => 'mark',
  2931. 'password' => 'secret'
  2932. ],
  2933. 'comments' => [
  2934. ['comment' => 'First post', 'user_id' => 2],
  2935. ['comment' => 'Second post', 'user_id' => 2],
  2936. ],
  2937. 'tags' => [
  2938. ['tag' => 'news', '_joinData' => ['active' => 1]],
  2939. ['tag' => 'cakephp', '_joinData' => ['active' => 0]],
  2940. ],
  2941. ];
  2942. $marshall = new Marshaller($this->articles);
  2943. // Assert event options are correct
  2944. $this->articles->users->getEventManager()->on(
  2945. 'Model.beforeMarshal',
  2946. function ($e, $data, $options) {
  2947. $this->assertArrayHasKey('validate', $options);
  2948. $this->assertTrue($options['validate']);
  2949. $this->assertArrayHasKey('associated', $options);
  2950. $this->assertSame([], $options['associated']);
  2951. $this->assertArrayHasKey('association', $options);
  2952. $this->assertInstanceOf('Cake\ORM\Association', $options['association']);
  2953. }
  2954. );
  2955. $this->articles->users->getEventManager()->on(
  2956. 'Model.beforeMarshal',
  2957. function ($e, $data, $options) {
  2958. $data['secret'] = 'h45h3d';
  2959. }
  2960. );
  2961. $this->articles->comments->getEventManager()->on(
  2962. 'Model.beforeMarshal',
  2963. function ($e, $data) {
  2964. $data['comment'] .= ' (modified)';
  2965. }
  2966. );
  2967. $this->articles->tags->getEventManager()->on(
  2968. 'Model.beforeMarshal',
  2969. function ($e, $data) {
  2970. $data['tag'] .= ' (modified)';
  2971. }
  2972. );
  2973. $this->articles->tags->junction()->getEventManager()->on(
  2974. 'Model.beforeMarshal',
  2975. function ($e, $data) {
  2976. $data['modified_by'] = 1;
  2977. }
  2978. );
  2979. $entity = $marshall->one($data, [
  2980. 'associated' => ['Users', 'Comments', 'Tags']
  2981. ]);
  2982. $this->assertEquals('h45h3d', $entity->user->secret);
  2983. $this->assertEquals('First post (modified)', $entity->comments[0]->comment);
  2984. $this->assertEquals('Second post (modified)', $entity->comments[1]->comment);
  2985. $this->assertEquals('news (modified)', $entity->tags[0]->tag);
  2986. $this->assertEquals('cakephp (modified)', $entity->tags[1]->tag);
  2987. $this->assertEquals(1, $entity->tags[0]->_joinData->modified_by);
  2988. $this->assertEquals(1, $entity->tags[1]->_joinData->modified_by);
  2989. }
  2990. /**
  2991. * Tests that patching an association resulting in no changes, will
  2992. * not mark the parent entity as dirty
  2993. *
  2994. * @return void
  2995. */
  2996. public function testAssociationNoChanges()
  2997. {
  2998. $options = ['markClean' => true, 'isNew' => false];
  2999. $entity = new Entity([
  3000. 'title' => 'My Title',
  3001. 'user' => new Entity([
  3002. 'username' => 'mark',
  3003. 'password' => 'not a secret'
  3004. ], $options)
  3005. ], $options);
  3006. $data = [
  3007. 'body' => 'My Content',
  3008. 'user' => [
  3009. 'username' => 'mark',
  3010. 'password' => 'not a secret'
  3011. ]
  3012. ];
  3013. $marshall = new Marshaller($this->articles);
  3014. $marshall->merge($entity, $data, ['associated' => ['Users']]);
  3015. $this->assertEquals('My Content', $entity->body);
  3016. $this->assertInstanceOf('Cake\ORM\Entity', $entity->user);
  3017. $this->assertEquals('mark', $entity->user->username);
  3018. $this->assertEquals('not a secret', $entity->user->password);
  3019. $this->assertFalse($entity->isDirty('user'));
  3020. $this->assertTrue($entity->user->isNew());
  3021. }
  3022. /**
  3023. * Test that primary key meta data is being read from the table
  3024. * and not the schema reflection when handling belongsToMany associations.
  3025. *
  3026. * @return void
  3027. */
  3028. public function testEnsurePrimaryKeyBeingReadFromTableForHandlingEmptyStringPrimaryKey()
  3029. {
  3030. $data = [
  3031. 'id' => ''
  3032. ];
  3033. $articles = TableRegistry::get('Articles');
  3034. $articles->getSchema()->dropConstraint('primary');
  3035. $articles->setPrimaryKey('id');
  3036. $marshall = new Marshaller($articles);
  3037. $result = $marshall->one($data);
  3038. $this->assertFalse($result->isDirty('id'));
  3039. $this->assertNull($result->id);
  3040. }
  3041. /**
  3042. * Test that primary key meta data is being read from the table
  3043. * and not the schema reflection when handling belongsToMany associations.
  3044. *
  3045. * @return void
  3046. */
  3047. public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRecordsByPrimaryKey()
  3048. {
  3049. $data = [
  3050. 'tags' => [
  3051. [
  3052. 'id' => 1
  3053. ],
  3054. [
  3055. 'id' => 2
  3056. ]
  3057. ]
  3058. ];
  3059. $tags = TableRegistry::get('Tags');
  3060. $tags->getSchema()->dropConstraint('primary');
  3061. $tags->setPrimaryKey('id');
  3062. $marshall = new Marshaller($this->articles);
  3063. $result = $marshall->one($data, ['associated' => ['Tags']]);
  3064. $expected = [
  3065. 'tags' => [
  3066. [
  3067. 'id' => 1,
  3068. 'name' => 'tag1',
  3069. 'description' => 'A big description',
  3070. 'created' => new Time('2016-01-01 00:00'),
  3071. ],
  3072. [
  3073. 'id' => 2,
  3074. 'name' => 'tag2',
  3075. 'description' => 'Another big description',
  3076. 'created' => new Time('2016-01-01 00:00'),
  3077. ]
  3078. ]
  3079. ];
  3080. $this->assertEquals($expected, $result->toArray());
  3081. }
  3082. }