MarshallerTest.php 101 KB

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