QueryRegressionTest.php 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Core\Plugin;
  17. use Cake\Database\Expression\Comparison;
  18. use Cake\Database\Expression\QueryExpression;
  19. use Cake\Datasource\EntityInterface;
  20. use Cake\Event\Event;
  21. use Cake\I18n\Time;
  22. use Cake\ORM\Query;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Contains regression test for the Query builder
  26. */
  27. class QueryRegressionTest extends TestCase
  28. {
  29. /**
  30. * Fixture to be used
  31. *
  32. * @var array
  33. */
  34. public $fixtures = [
  35. 'core.Articles',
  36. 'core.Tags',
  37. 'core.ArticlesTags',
  38. 'core.Authors',
  39. 'core.AuthorsTags',
  40. 'core.Comments',
  41. 'core.FeaturedTags',
  42. 'core.SpecialTags',
  43. 'core.TagsTranslations',
  44. 'core.Translates',
  45. 'core.Users',
  46. ];
  47. public $autoFixtures = false;
  48. /**
  49. * Test for https://github.com/cakephp/cakephp/issues/3087
  50. *
  51. * @return void
  52. */
  53. public function testSelectTimestampColumn()
  54. {
  55. $this->loadFixtures('Users');
  56. $table = $this->getTableLocator()->get('users');
  57. $user = $table->find()->where(['id' => 1])->first();
  58. $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created);
  59. $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated);
  60. }
  61. /**
  62. * Tests that EagerLoader does not try to create queries for associations having no
  63. * keys to compare against
  64. *
  65. * @return void
  66. */
  67. public function testEagerLoadingFromEmptyResults()
  68. {
  69. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  70. $table = $this->getTableLocator()->get('Articles');
  71. $table->belongsToMany('ArticlesTags');
  72. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  73. $this->assertEmpty($results);
  74. }
  75. /**
  76. * Tests that eagerloading associations with aliased fields works.
  77. *
  78. * @return void
  79. */
  80. public function testEagerLoadingAliasedAssociationFields()
  81. {
  82. $this->loadFixtures('Articles', 'Authors');
  83. $table = $this->getTableLocator()->get('Articles');
  84. $table->belongsTo('Authors', [
  85. 'foreignKey' => 'author_id',
  86. ]);
  87. $result = $table->find()
  88. ->contain(['Authors' => [
  89. 'fields' => [
  90. 'id',
  91. 'Authors__aliased_name' => 'name',
  92. ],
  93. ]])
  94. ->where(['Articles.id' => 1])
  95. ->first();
  96. $this->assertInstanceOf(EntityInterface::class, $result);
  97. $this->assertInstanceOf(EntityInterface::class, $result->author);
  98. $this->assertSame('mariano', $result->author->aliased_name);
  99. }
  100. /**
  101. * Tests that eagerloading and hydration works for associations that have
  102. * different aliases in the association and targetTable
  103. *
  104. * @return void
  105. */
  106. public function testEagerLoadingMismatchingAliasInBelongsTo()
  107. {
  108. $this->loadFixtures('Articles', 'Users');
  109. $table = $this->getTableLocator()->get('Articles');
  110. $users = $this->getTableLocator()->get('Users');
  111. $table->belongsTo('Authors', [
  112. 'targetTable' => $users,
  113. 'foreignKey' => 'author_id',
  114. ]);
  115. $result = $table->find()->where(['Articles.id' => 1])->contain('Authors')->first();
  116. $this->assertInstanceOf(EntityInterface::class, $result);
  117. $this->assertInstanceOf(EntityInterface::class, $result->author);
  118. $this->assertSame('mariano', $result->author->username);
  119. }
  120. /**
  121. * Tests that eagerloading and hydration works for associations that have
  122. * different aliases in the association and targetTable
  123. *
  124. * @return void
  125. */
  126. public function testEagerLoadingMismatchingAliasInHasOne()
  127. {
  128. $this->loadFixtures('Articles', 'Users');
  129. $articles = $this->getTableLocator()->get('Articles');
  130. $users = $this->getTableLocator()->get('Users');
  131. $users->hasOne('Posts', [
  132. 'targetTable' => $articles,
  133. 'foreignKey' => 'author_id',
  134. ]);
  135. $result = $users->find()->where(['Users.id' => 1])->contain('Posts')->first();
  136. $this->assertInstanceOf(EntityInterface::class, $result);
  137. $this->assertInstanceOf(EntityInterface::class, $result->post);
  138. $this->assertSame('First Article', $result->post->title);
  139. }
  140. /**
  141. * Tests that eagerloading belongsToMany with find list fails with a helpful message.
  142. *
  143. * @return void
  144. */
  145. public function testEagerLoadingBelongsToManyList()
  146. {
  147. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  148. $table = $this->getTableLocator()->get('Articles');
  149. $table->belongsToMany('Tags', [
  150. 'finder' => 'list',
  151. ]);
  152. $this->expectException(\RuntimeException::class);
  153. $this->expectExceptionMessage('"_joinData" is missing from the belongsToMany results');
  154. $table->find()->contain('Tags')->toArray();
  155. }
  156. /**
  157. * Tests that eagerloading and hydration works for associations that have
  158. * different aliases in the association and targetTable
  159. *
  160. * @return void
  161. */
  162. public function testEagerLoadingNestedMatchingCalls()
  163. {
  164. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags', 'AuthorsTags');
  165. $articles = $this->getTableLocator()->get('Articles');
  166. $articles->belongsToMany('Tags', [
  167. 'foreignKey' => 'article_id',
  168. 'targetForeignKey' => 'tag_id',
  169. 'joinTable' => 'articles_tags',
  170. ]);
  171. $tags = $this->getTableLocator()->get('Tags');
  172. $tags->belongsToMany('Authors', [
  173. 'foreignKey' => 'tag_id',
  174. 'targetForeignKey' => 'author_id',
  175. 'joinTable' => 'authors_tags',
  176. ]);
  177. $query = $articles->find()
  178. ->matching('Tags', function ($q) {
  179. return $q->matching('Authors', function ($q) {
  180. return $q->where(['Authors.name' => 'larry']);
  181. });
  182. });
  183. $this->assertEquals(3, $query->count());
  184. $result = $query->first();
  185. $this->assertInstanceOf(EntityInterface::class, $result);
  186. $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Tags']);
  187. $this->assertInstanceOf(EntityInterface::class, $result->_matchingData['Authors']);
  188. }
  189. /**
  190. * Tests that duplicate aliases in contain() can be used, even when they would
  191. * naturally be attached to the query instead of eagerly loaded. What should
  192. * happen here is that One of the duplicates will be changed to be loaded using
  193. * an extra query, but yielding the same results
  194. *
  195. * @return void
  196. */
  197. public function testDuplicateAttachableAliases()
  198. {
  199. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags', 'Authors');
  200. $this->getTableLocator()->get('Stuff', ['table' => 'tags']);
  201. $this->getTableLocator()->get('Things', ['table' => 'articles_tags']);
  202. $table = $this->getTableLocator()->get('Articles');
  203. $table->belongsTo('Authors');
  204. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  205. $table->Authors->getTarget()->hasOne('Stuff', [
  206. 'foreignKey' => 'id',
  207. 'propertyName' => 'favorite_tag',
  208. ]);
  209. $table->Things->getTarget()->belongsTo('Stuff', [
  210. 'foreignKey' => 'tag_id',
  211. 'propertyName' => 'foo',
  212. ]);
  213. $results = $table->find()
  214. ->contain(['Authors.Stuff', 'Things.Stuff'])
  215. ->order(['Articles.id' => 'ASC'])
  216. ->toArray();
  217. $this->assertCount(5, $results);
  218. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  219. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  220. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  221. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  222. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  223. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  224. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  225. }
  226. /**
  227. * Test for https://github.com/cakephp/cakephp/issues/3410
  228. *
  229. * @return void
  230. */
  231. public function testNullableTimeColumn()
  232. {
  233. $this->loadFixtures('Users');
  234. $table = $this->getTableLocator()->get('users');
  235. $entity = $table->newEntity(['username' => 'derp', 'created' => null]);
  236. $this->assertSame($entity, $table->save($entity));
  237. $this->assertNull($entity->created);
  238. }
  239. /**
  240. * Test for https://github.com/cakephp/cakephp/issues/3626
  241. *
  242. * Checks that join data is actually created and not tried to be updated every time
  243. *
  244. * @return void
  245. */
  246. public function testCreateJointData()
  247. {
  248. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  249. $articles = $this->getTableLocator()->get('Articles');
  250. $articles->belongsToMany('Highlights', [
  251. 'className' => 'TestApp\Model\Table\TagsTable',
  252. 'foreignKey' => 'article_id',
  253. 'targetForeignKey' => 'tag_id',
  254. 'through' => 'SpecialTags',
  255. ]);
  256. $entity = $articles->get(2);
  257. $data = [
  258. 'id' => 2,
  259. 'highlights' => [
  260. [
  261. 'name' => 'New Special Tag',
  262. '_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00'],
  263. ],
  264. ],
  265. ];
  266. $entity = $articles->patchEntity($entity, $data, ['Highlights._joinData']);
  267. $articles->save($entity);
  268. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  269. $this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id);
  270. $this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d'));
  271. }
  272. /**
  273. * Tests that the junction table instance taken from both sides of a belongsToMany
  274. * relationship is actually the same object.
  275. *
  276. * @return void
  277. */
  278. public function testReciprocalBelongsToMany()
  279. {
  280. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  281. $articles = $this->getTableLocator()->get('Articles');
  282. $tags = $this->getTableLocator()->get('Tags');
  283. $articles->belongsToMany('Tags');
  284. $tags->belongsToMany('Articles');
  285. $left = $articles->Tags->junction();
  286. $right = $tags->Articles->junction();
  287. $this->assertSame($left, $right);
  288. }
  289. /**
  290. * Test for https://github.com/cakephp/cakephp/issues/4253
  291. *
  292. * Makes sure that the belongsToMany association is not overwritten with conflicting information
  293. * by any of the sides when the junction() function is invoked
  294. *
  295. * @return void
  296. */
  297. public function testReciprocalBelongsToManyNoOverwrite()
  298. {
  299. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  300. $articles = $this->getTableLocator()->get('Articles');
  301. $tags = $this->getTableLocator()->get('Tags');
  302. $articles->belongsToMany('Tags');
  303. $tags->belongsToMany('Articles');
  304. $sub = $articles->Tags->find()->select(['Tags.id'])->matching('Articles', function ($q) {
  305. return $q->where(['Articles.id' => 1]);
  306. });
  307. $query = $articles->Tags->find()->where(['Tags.id NOT IN' => $sub]);
  308. $this->assertEquals(1, $query->count());
  309. }
  310. /**
  311. * Returns an array with the saving strategies for a belongsTo association
  312. *
  313. * @return array
  314. */
  315. public function strategyProvider()
  316. {
  317. return [
  318. ['append'],
  319. ['replace'],
  320. ];
  321. }
  322. /**
  323. * Test for https://github.com/cakephp/cakephp/issues/3677 and
  324. * https://github.com/cakephp/cakephp/issues/3714
  325. *
  326. * Checks that only relevant associations are passed when saving _joinData
  327. * Tests that _joinData can also save deeper associations
  328. *
  329. * @dataProvider strategyProvider
  330. * @param string $strategy
  331. * @return void
  332. */
  333. public function testBelongsToManyDeepSave($strategy)
  334. {
  335. $this->loadFixtures('Articles', 'Tags', 'SpecialTags', 'Authors');
  336. $articles = $this->getTableLocator()->get('Articles');
  337. $articles->belongsToMany('Highlights', [
  338. 'className' => 'TestApp\Model\Table\TagsTable',
  339. 'foreignKey' => 'article_id',
  340. 'targetForeignKey' => 'tag_id',
  341. 'through' => 'SpecialTags',
  342. 'saveStrategy' => $strategy,
  343. ]);
  344. $articles->Highlights->junction()->belongsTo('Authors');
  345. $articles->Highlights->hasOne('Authors', [
  346. 'foreignKey' => 'id',
  347. ]);
  348. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  349. $data = [
  350. 'highlights' => [
  351. [
  352. 'name' => 'New Special Tag',
  353. '_joinData' => [
  354. 'highlighted' => true,
  355. 'highlighted_time' => '2014-06-01 10:10:00',
  356. 'author' => [
  357. 'name' => 'mariano',
  358. ],
  359. ],
  360. 'author' => ['name' => 'mark'],
  361. ],
  362. ],
  363. ];
  364. $options = [
  365. 'associated' => [
  366. 'Highlights._joinData.Authors', 'Highlights.Authors',
  367. ],
  368. ];
  369. $entity = $articles->patchEntity($entity, $data, $options);
  370. $articles->save($entity, $options);
  371. $entity = $articles->get(2, [
  372. 'contain' => [
  373. 'SpecialTags' => ['sort' => ['SpecialTags.id' => 'ASC']],
  374. 'SpecialTags.Authors',
  375. 'Highlights.Authors',
  376. ],
  377. ]);
  378. $this->assertEquals('mark', end($entity->highlights)->author->name);
  379. $lastTag = end($entity->special_tags);
  380. $this->assertTrue($lastTag->highlighted);
  381. $this->assertEquals('2014-06-01 10:10:00', $lastTag->highlighted_time->format('Y-m-d H:i:s'));
  382. $this->assertEquals('mariano', $lastTag->author->name);
  383. }
  384. /**
  385. * Tests that no exceptions are generated because of ambiguous column names in queries
  386. * during a save operation
  387. *
  388. * @see https://github.com/cakephp/cakephp/issues/3803
  389. * @return void
  390. */
  391. public function testSaveWithCallbacks()
  392. {
  393. $this->loadFixtures('Articles', 'Authors');
  394. $articles = $this->getTableLocator()->get('Articles');
  395. $articles->belongsTo('Authors');
  396. $articles->getEventManager()->on('Model.beforeFind', function (Event $event, $query) {
  397. return $query->contain('Authors');
  398. });
  399. $article = $articles->newEntity();
  400. $article->title = 'Foo';
  401. $article->body = 'Bar';
  402. $this->assertSame($article, $articles->save($article));
  403. }
  404. /**
  405. * Test that save() works with entities containing expressions
  406. * as properties.
  407. *
  408. * @return void
  409. */
  410. public function testSaveWithExpressionProperty()
  411. {
  412. $this->loadFixtures('Articles');
  413. $articles = $this->getTableLocator()->get('Articles');
  414. $article = $articles->newEntity();
  415. $article->title = new QueryExpression("SELECT 'jose'");
  416. $this->assertSame($article, $articles->save($article));
  417. }
  418. /**
  419. * Tests that whe saving deep associations for a belongsToMany property,
  420. * data is not removed because of excessive associations filtering.
  421. *
  422. * @see https://github.com/cakephp/cakephp/issues/4009
  423. * @return void
  424. */
  425. public function testBelongsToManyDeepSave2()
  426. {
  427. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  428. $articles = $this->getTableLocator()->get('Articles');
  429. $articles->belongsToMany('Highlights', [
  430. 'className' => 'TestApp\Model\Table\TagsTable',
  431. 'foreignKey' => 'article_id',
  432. 'targetForeignKey' => 'tag_id',
  433. 'through' => 'SpecialTags',
  434. ]);
  435. $articles->Highlights->hasMany('TopArticles', [
  436. 'className' => 'TestApp\Model\Table\ArticlesTable',
  437. 'foreignKey' => 'author_id',
  438. ]);
  439. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  440. $data = [
  441. 'highlights' => [
  442. [
  443. 'name' => 'New Special Tag',
  444. '_joinData' => [
  445. 'highlighted' => true,
  446. 'highlighted_time' => '2014-06-01 10:10:00',
  447. ],
  448. 'top_articles' => [
  449. ['title' => 'First top article'],
  450. ['title' => 'Second top article'],
  451. ],
  452. ],
  453. ],
  454. ];
  455. $options = [
  456. 'associated' => [
  457. 'Highlights._joinData', 'Highlights.TopArticles',
  458. ],
  459. ];
  460. $entity = $articles->patchEntity($entity, $data, $options);
  461. $articles->save($entity, $options);
  462. $entity = $articles->get(2, [
  463. 'contain' => [
  464. 'Highlights.TopArticles',
  465. ],
  466. ]);
  467. $highlights = $entity->highlights[0];
  468. $this->assertEquals('First top article', $highlights->top_articles[0]->title);
  469. $this->assertEquals('Second top article', $highlights->top_articles[1]->title);
  470. $this->assertEquals(
  471. new Time('2014-06-01 10:10:00'),
  472. $highlights->_joinData->highlighted_time
  473. );
  474. }
  475. /**
  476. * An integration test that spot checks that associations use the
  477. * correct alias names to generate queries.
  478. *
  479. * @return void
  480. */
  481. public function testPluginAssociationQueryGeneration()
  482. {
  483. $this->loadFixtures('Articles', 'Comments', 'Authors');
  484. $this->loadPlugins(['TestPlugin']);
  485. $articles = $this->getTableLocator()->get('Articles');
  486. $articles->hasMany('TestPlugin.Comments');
  487. $articles->belongsTo('TestPlugin.Authors');
  488. $result = $articles->find()
  489. ->where(['Articles.id' => 2])
  490. ->contain(['Comments', 'Authors'])
  491. ->first();
  492. $this->assertNotEmpty(
  493. $result->comments[0]->id,
  494. 'No SQL error and comment exists.'
  495. );
  496. $this->assertNotEmpty(
  497. $result->author->id,
  498. 'No SQL error and author exists.'
  499. );
  500. $this->clearPlugins();
  501. }
  502. /**
  503. * Tests that loading associations having the same alias in the
  504. * joinable associations chain is not sensitive to the order in which
  505. * the associations are selected.
  506. *
  507. * @see https://github.com/cakephp/cakephp/issues/4454
  508. * @return void
  509. */
  510. public function testAssociationChainOrder()
  511. {
  512. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags', 'Authors');
  513. $articles = $this->getTableLocator()->get('Articles');
  514. $articles->belongsTo('Authors');
  515. $articles->hasOne('ArticlesTags');
  516. $articlesTags = $this->getTableLocator()->get('ArticlesTags');
  517. $articlesTags->belongsTo('Authors', [
  518. 'foreignKey' => 'tag_id',
  519. ]);
  520. $resultA = $articles->find()
  521. ->contain(['ArticlesTags.Authors', 'Authors'])
  522. ->first();
  523. $resultB = $articles->find()
  524. ->contain(['Authors', 'ArticlesTags.Authors'])
  525. ->first();
  526. $this->assertEquals($resultA, $resultB);
  527. $this->assertNotEmpty($resultA->author);
  528. $this->assertNotEmpty($resultA->articles_tag->author);
  529. }
  530. /**
  531. * Test that offset/limit are elided from subquery loads.
  532. *
  533. * @return void
  534. */
  535. public function testAssociationSubQueryNoOffset()
  536. {
  537. $this->loadFixtures('Articles', 'Translates');
  538. $table = $this->getTableLocator()->get('Articles');
  539. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  540. $table->setLocale('eng');
  541. $query = $table->find('translations')
  542. ->order(['Articles.id' => 'ASC'])
  543. ->limit(10)
  544. ->offset(1);
  545. $result = $query->toArray();
  546. $this->assertCount(2, $result);
  547. }
  548. /**
  549. * Tests that using the subquery strategy in a deep association returns the right results
  550. *
  551. * @see https://github.com/cakephp/cakephp/issues/4484
  552. * @return void
  553. */
  554. public function testDeepBelongsToManySubqueryStrategy()
  555. {
  556. $this->loadFixtures('Authors', 'Tags', 'Articles', 'ArticlesTags');
  557. $table = $this->getTableLocator()->get('Authors');
  558. $table->hasMany('Articles');
  559. $table->Articles->belongsToMany('Tags', [
  560. 'strategy' => 'subquery',
  561. ]);
  562. $result = $table->find()->contain(['Articles.Tags'])->toArray();
  563. $this->skipIf(count($result) == 0, 'No results, this test sometimes acts up on PHP 5.6');
  564. $this->assertEquals(
  565. ['tag1', 'tag3'],
  566. collection($result[2]->articles[0]->tags)->extract('name')->toArray()
  567. );
  568. }
  569. /**
  570. * Tests that using the subquery strategy in a deep association returns the right results
  571. *
  572. * @see https://github.com/cakephp/cakephp/issues/5769
  573. * @return void
  574. */
  575. public function testDeepBelongsToManySubqueryStrategy2()
  576. {
  577. $this->loadFixtures('Articles', 'Authors', 'Tags', 'Authors', 'AuthorsTags');
  578. $table = $this->getTableLocator()->get('Authors');
  579. $table->hasMany('Articles');
  580. $table->Articles->belongsToMany('Tags', [
  581. 'strategy' => 'subquery',
  582. ]);
  583. $table->belongsToMany('Tags', [
  584. 'strategy' => 'subquery',
  585. ]);
  586. $table->Articles->belongsTo('Authors');
  587. $result = $table->Articles->find()
  588. ->where(['Authors.id >' => 1])
  589. ->contain(['Authors.Tags'])
  590. ->toArray();
  591. $this->assertEquals(
  592. ['tag1', 'tag2'],
  593. collection($result[0]->author->tags)->extract('name')->toArray()
  594. );
  595. $this->assertEquals(3, $result[0]->author->id);
  596. }
  597. /**
  598. * Tests that finding on a table with a primary key other than `id` will work
  599. * seamlessly with either select or subquery.
  600. *
  601. * @see https://github.com/cakephp/cakephp/issues/6781
  602. * @return void
  603. */
  604. public function testDeepHasManyEitherStrategy()
  605. {
  606. $this->loadFixtures('Tags', 'FeaturedTags', 'TagsTranslations');
  607. $tags = $this->getTableLocator()->get('Tags');
  608. $this->skipIf(
  609. $tags->getConnection()->getDriver() instanceof \Cake\Database\Driver\Sqlserver,
  610. 'SQL server is temporarily weird in this test, will investigate later'
  611. );
  612. $tags = $this->getTableLocator()->get('Tags');
  613. $featuredTags = $this->getTableLocator()->get('FeaturedTags');
  614. $featuredTags->belongsTo('Tags');
  615. $tags->hasMany('TagsTranslations', [
  616. 'foreignKey' => 'id',
  617. 'strategy' => 'select',
  618. ]);
  619. $findViaSelect = $featuredTags
  620. ->find()
  621. ->where(['FeaturedTags.tag_id' => 2])
  622. ->contain('Tags.TagsTranslations')
  623. ->all();
  624. $tags->hasMany('TagsTranslations', [
  625. 'foreignKey' => 'id',
  626. 'strategy' => 'subquery',
  627. ]);
  628. $findViaSubquery = $featuredTags
  629. ->find()
  630. ->where(['FeaturedTags.tag_id' => 2])
  631. ->contain('Tags.TagsTranslations')
  632. ->all();
  633. $expected = [2 => 'tag 2 translated into en_us'];
  634. $this->assertEquals($expected, $findViaSelect->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  635. $this->assertEquals($expected, $findViaSubquery->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  636. }
  637. /**
  638. * Tests that getting the count of a query having containments return
  639. * the correct results
  640. *
  641. * @see https://github.com/cakephp/cakephp/issues/4511
  642. * @return void
  643. */
  644. public function testCountWithContain()
  645. {
  646. $this->loadFixtures('Articles', 'Authors');
  647. $table = $this->getTableLocator()->get('Articles');
  648. $table->belongsTo('Authors', ['joinType' => 'inner']);
  649. $count = $table
  650. ->find()
  651. ->contain(['Authors' => function ($q) {
  652. return $q->where(['Authors.id' => 1]);
  653. }])
  654. ->count();
  655. $this->assertEquals(2, $count);
  656. }
  657. /**
  658. * Tests that getting the count of a query with bind is correct
  659. *
  660. * @see https://github.com/cakephp/cakephp/issues/8466
  661. * @return void
  662. */
  663. public function testCountWithBind()
  664. {
  665. $this->loadFixtures('Articles');
  666. $table = $this->getTableLocator()->get('Articles');
  667. $query = $table
  668. ->find()
  669. ->select(['title', 'id'])
  670. ->where('title LIKE :val')
  671. ->group(['id', 'title'])
  672. ->bind(':val', '%Second%');
  673. $count = $query->count();
  674. $this->assertEquals(1, $count);
  675. }
  676. /**
  677. * Test count() with inner join containments.
  678. *
  679. * @return void
  680. */
  681. public function testCountWithInnerJoinContain()
  682. {
  683. $this->loadFixtures('Articles', 'Authors');
  684. $table = $this->getTableLocator()->get('Articles');
  685. $table->belongsTo('Authors')->setJoinType('INNER');
  686. $result = $table->save($table->newEntity([
  687. 'author_id' => null,
  688. 'title' => 'title',
  689. 'body' => 'body',
  690. 'published' => 'Y',
  691. ]));
  692. $this->assertNotFalse($result);
  693. $table->getEventManager()
  694. ->on('Model.beforeFind', function (Event $event, $query) {
  695. $query->contain(['Authors']);
  696. });
  697. $count = $table->find()->count();
  698. $this->assertEquals(3, $count);
  699. }
  700. /**
  701. * Tests that bind in subqueries works.
  702. *
  703. * @return void
  704. */
  705. public function testSubqueryBind()
  706. {
  707. $this->loadFixtures('Articles');
  708. $table = $this->getTableLocator()->get('Articles');
  709. $sub = $table->find()
  710. ->select(['id'])
  711. ->where('title LIKE :val')
  712. ->bind(':val', 'Second %');
  713. $query = $table
  714. ->find()
  715. ->select(['title'])
  716. ->where(['id NOT IN' => $sub]);
  717. $result = $query->toArray();
  718. $this->assertCount(2, $result);
  719. $this->assertEquals('First Article', $result[0]->title);
  720. $this->assertEquals('Third Article', $result[1]->title);
  721. }
  722. /**
  723. * Test that deep containments don't generate empty entities for
  724. * intermediary relations.
  725. *
  726. * @return void
  727. */
  728. public function testContainNoEmptyAssociatedObjects()
  729. {
  730. $this->loadFixtures('Comments', 'Users', 'Articles');
  731. $comments = $this->getTableLocator()->get('Comments');
  732. $comments->belongsTo('Users');
  733. $users = $this->getTableLocator()->get('Users');
  734. $users->hasMany('Articles', [
  735. 'foreignKey' => 'author_id',
  736. ]);
  737. $comments->updateAll(['user_id' => 99], ['id' => 1]);
  738. $result = $comments->find()
  739. ->contain(['Users'])
  740. ->where(['Comments.id' => 1])
  741. ->first();
  742. $this->assertNull($result->user, 'No record should be null.');
  743. $result = $comments->find()
  744. ->contain(['Users', 'Users.Articles'])
  745. ->where(['Comments.id' => 1])
  746. ->first();
  747. $this->assertNull($result->user, 'No record should be null.');
  748. }
  749. /**
  750. * Tests that using a comparison expression inside an OR condition works
  751. *
  752. * @see https://github.com/cakephp/cakephp/issues/5081
  753. * @return void
  754. */
  755. public function testOrConditionsWithExpression()
  756. {
  757. $this->loadFixtures('Articles');
  758. $table = $this->getTableLocator()->get('Articles');
  759. $query = $table->find();
  760. $query->where([
  761. 'OR' => [
  762. new Comparison('id', 1, 'integer', '>'),
  763. new Comparison('id', 3, 'integer', '<'),
  764. ],
  765. ]);
  766. $results = $query->toArray();
  767. $this->assertCount(3, $results);
  768. }
  769. /**
  770. * Tests that calling count on a query having a union works correctly
  771. *
  772. * @see https://github.com/cakephp/cakephp/issues/5107
  773. * @return void
  774. */
  775. public function testCountWithUnionQuery()
  776. {
  777. $this->loadFixtures('Articles');
  778. $table = $this->getTableLocator()->get('Articles');
  779. $query = $table->find()->where(['id' => 1]);
  780. $query2 = $table->find()->where(['id' => 2]);
  781. $query->union($query2);
  782. $this->assertEquals(2, $query->count());
  783. }
  784. /**
  785. * Integration test when selecting no fields on the primary table.
  786. *
  787. * @return void
  788. */
  789. public function testSelectNoFieldsOnPrimaryAlias()
  790. {
  791. $this->loadFixtures('Articles', 'Users');
  792. $table = $this->getTableLocator()->get('Articles');
  793. $table->belongsTo('Users');
  794. $query = $table->find()
  795. ->select(['Users__id' => 'id']);
  796. $results = $query->toArray();
  797. $this->assertCount(3, $results);
  798. }
  799. /**
  800. * Test selecting with aliased aggregates and identifier quoting
  801. * does not emit notice errors.
  802. *
  803. * @see https://github.com/cakephp/cakephp/issues/12766
  804. * @return void
  805. */
  806. public function testAliasedAggregateFieldTypeConversionSafe()
  807. {
  808. $this->loadFixtures('Articles');
  809. $articles = $this->getTableLocator()->get('Articles');
  810. $driver = $articles->getConnection()->getDriver();
  811. $restore = $driver->isAutoQuotingEnabled();
  812. $driver->enableAutoQuoting(true);
  813. $query = $articles->find();
  814. $query->select([
  815. 'sumUsers' => $articles->find()->func()->sum('author_id'),
  816. ]);
  817. $driver->enableAutoQuoting($restore);
  818. $result = $query->execute()->fetchAll('assoc');
  819. $this->assertArrayHasKey('sumUsers', $result[0]);
  820. }
  821. /**
  822. * Tests that calling first on the query results will not remove all other results
  823. * from the set.
  824. *
  825. * @return void
  826. */
  827. public function testFirstOnResultSet()
  828. {
  829. $this->loadFixtures('Articles');
  830. $results = $this->getTableLocator()->get('Articles')->find()->all();
  831. $this->assertEquals(3, $results->count());
  832. $this->assertNotNull($results->first());
  833. $this->assertCount(3, $results->toArray());
  834. }
  835. /**
  836. * Checks that matching and contain can be called for the same belongsTo association
  837. *
  838. * @see https://github.com/cakephp/cakephp/issues/5463
  839. * @return void
  840. */
  841. public function testFindMatchingAndContain()
  842. {
  843. $this->loadFixtures('Articles', 'Authors');
  844. $table = $this->getTableLocator()->get('Articles');
  845. $table->belongsTo('Authors');
  846. $article = $table->find()
  847. ->contain('Authors')
  848. ->matching('Authors', function ($q) {
  849. return $q->where(['Authors.id' => 1]);
  850. })
  851. ->first();
  852. $this->assertNotNull($article->author);
  853. $this->assertEquals($article->author, $article->_matchingData['Authors']);
  854. }
  855. /**
  856. * Checks that matching and contain can be called for the same belongsTo association
  857. *
  858. * @see https://github.com/cakephp/cakephp/issues/5463
  859. * @return void
  860. */
  861. public function testFindMatchingAndContainWithSubquery()
  862. {
  863. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  864. $table = $this->getTableLocator()->get('authors');
  865. $table->hasMany('articles', ['strategy' => 'subquery']);
  866. $table->articles->belongsToMany('tags');
  867. $result = $table->find()
  868. ->matching('articles.tags', function ($q) {
  869. return $q->where(['tags.id' => 2]);
  870. })
  871. ->contain('articles');
  872. $this->assertCount(2, $result->first()->articles);
  873. }
  874. /**
  875. * Tests that matching does not overwrite associations in contain
  876. *
  877. * @see https://github.com/cakephp/cakephp/issues/5584
  878. * @return void
  879. */
  880. public function testFindMatchingOverwrite()
  881. {
  882. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags');
  883. $comments = $this->getTableLocator()->get('Comments');
  884. $comments->belongsTo('Articles');
  885. $articles = $this->getTableLocator()->get('Articles');
  886. $articles->belongsToMany('Tags');
  887. $result = $comments
  888. ->find()
  889. ->matching('Articles.Tags', function ($q) {
  890. return $q->where(['Tags.id' => 2]);
  891. })
  892. ->contain('Articles')
  893. ->first();
  894. $this->assertEquals(1, $result->id);
  895. $this->assertEquals(1, $result->_matchingData['Articles']->id);
  896. $this->assertEquals(2, $result->_matchingData['Tags']->id);
  897. $this->assertNotNull($result->article);
  898. $this->assertEquals($result->article, $result->_matchingData['Articles']);
  899. }
  900. /**
  901. * Tests that matching does not overwrite associations in contain
  902. *
  903. * @see https://github.com/cakephp/cakephp/issues/5584
  904. * @return void
  905. */
  906. public function testFindMatchingOverwrite2()
  907. {
  908. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags', 'Authors');
  909. $comments = $this->getTableLocator()->get('Comments');
  910. $comments->belongsTo('Articles');
  911. $articles = $this->getTableLocator()->get('Articles');
  912. $articles->belongsTo('Authors');
  913. $articles->belongsToMany('Tags');
  914. $result = $comments
  915. ->find()
  916. ->matching('Articles.Tags', function ($q) {
  917. return $q->where(['Tags.id' => 2]);
  918. })
  919. ->contain('Articles.Authors')
  920. ->first();
  921. $this->assertNotNull($result->article->author);
  922. }
  923. /**
  924. * Tests that trying to contain an inexistent association
  925. * throws an exception and not a fatal error.
  926. *
  927. * @return void
  928. */
  929. public function testQueryNotFatalError()
  930. {
  931. $this->expectException(\InvalidArgumentException::class);
  932. $this->loadFixtures('Comments');
  933. $comments = $this->getTableLocator()->get('Comments');
  934. $comments->find()->contain('Deprs')->all();
  935. }
  936. /**
  937. * Tests that using matching and contain on belongsTo associations
  938. * works correctly.
  939. *
  940. * @see https://github.com/cakephp/cakephp/issues/5721
  941. * @return void
  942. */
  943. public function testFindMatchingWithContain()
  944. {
  945. $this->loadFixtures('Articles', 'Comments', 'Users');
  946. $comments = $this->getTableLocator()->get('Comments');
  947. $comments->belongsTo('Articles');
  948. $comments->belongsTo('Users');
  949. $result = $comments->find()
  950. ->contain(['Articles', 'Users'])
  951. ->matching('Articles', function ($q) {
  952. return $q->where(['Articles.id >=' => 1]);
  953. })
  954. ->matching('Users', function ($q) {
  955. return $q->where(['Users.id >=' => 1]);
  956. })
  957. ->order(['Comments.id' => 'ASC'])
  958. ->first();
  959. $this->assertInstanceOf('Cake\ORM\Entity', $result->article);
  960. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  961. $this->assertEquals(2, $result->user->id);
  962. $this->assertEquals(1, $result->article->id);
  963. }
  964. /**
  965. * Tests that HasMany associations don't use duplicate PK values.
  966. *
  967. * @return void
  968. */
  969. public function testHasManyEagerLoadingUniqueKey()
  970. {
  971. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  972. $table = $this->getTableLocator()->get('ArticlesTags');
  973. $table->belongsTo('Articles', [
  974. 'strategy' => 'select',
  975. ]);
  976. $result = $table->find()
  977. ->contain(['Articles' => function ($q) {
  978. $result = $q->sql();
  979. $this->assertNotContains(':c2', $result, 'Only 2 bindings as there are only 2 rows.');
  980. $this->assertNotContains(':c3', $result, 'Only 2 bindings as there are only 2 rows.');
  981. return $q;
  982. }])
  983. ->toArray();
  984. $this->assertNotEmpty($result[0]->article);
  985. }
  986. /**
  987. * Tests that using contain but selecting no fields from the association
  988. * does not trigger any errors and fetches the right results.
  989. *
  990. * @see https://github.com/cakephp/cakephp/issues/6214
  991. * @return void
  992. */
  993. public function testContainWithNoFields()
  994. {
  995. $this->loadFixtures('Comments', 'Users');
  996. $table = $this->getTableLocator()->get('Comments');
  997. $table->belongsTo('Users');
  998. $results = $table->find()
  999. ->select(['Comments.id', 'Comments.user_id'])
  1000. ->contain(['Users'])
  1001. ->where(['Users.id' => 1])
  1002. ->combine('id', 'user_id');
  1003. $this->assertEquals([3 => 1, 4 => 1, 5 => 1], $results->toArray());
  1004. }
  1005. /**
  1006. * Tests that find() and contained associations using computed fields doesn't error out.
  1007. *
  1008. * @see https://github.com/cakephp/cakephp/issues/9326
  1009. * @return void
  1010. */
  1011. public function testContainWithComputedField()
  1012. {
  1013. $this->loadFixtures('Comments', 'Users');
  1014. $table = $this->getTableLocator()->get('Users');
  1015. $table->hasMany('Comments');
  1016. $query = $table->find()->contain([
  1017. 'Comments' => function ($q) {
  1018. return $q->select([
  1019. 'concat' => $q->func()->concat(['red', 'blue']),
  1020. 'user_id',
  1021. ]);
  1022. }])
  1023. ->where(['Users.id' => 2]);
  1024. $results = $query->toArray();
  1025. $this->assertCount(1, $results);
  1026. $this->assertEquals('redblue', $results[0]->comments[0]->concat);
  1027. }
  1028. /**
  1029. * Tests that using matching and selecting no fields for that association
  1030. * will no trigger any errors and fetch the right results
  1031. *
  1032. * @see https://github.com/cakephp/cakephp/issues/6223
  1033. * @return void
  1034. */
  1035. public function testMatchingWithNoFields()
  1036. {
  1037. $this->loadFixtures('Comments', 'Users');
  1038. $table = $this->getTableLocator()->get('Users');
  1039. $table->hasMany('Comments');
  1040. $results = $table->find()
  1041. ->select(['Users.id'])
  1042. ->matching('Comments', function ($q) {
  1043. return $q->where(['Comments.id' => 1]);
  1044. })
  1045. ->extract('id')
  1046. ->toList();
  1047. $this->assertEquals([2], $results);
  1048. }
  1049. /**
  1050. * Test that empty conditions in a matching clause don't cause errors.
  1051. *
  1052. * @return void
  1053. */
  1054. public function testMatchingEmptyQuery()
  1055. {
  1056. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  1057. $table = $this->getTableLocator()->get('Articles');
  1058. $table->belongsToMany('Tags');
  1059. $rows = $table->find()
  1060. ->matching('Tags', function ($q) {
  1061. return $q->where([]);
  1062. })
  1063. ->all();
  1064. $this->assertNotEmpty($rows);
  1065. $rows = $table->find()
  1066. ->matching('Tags', function ($q) {
  1067. return $q->where(null);
  1068. })
  1069. ->all();
  1070. $this->assertNotEmpty($rows);
  1071. }
  1072. /**
  1073. * Tests that using a subquery as part of an expression will not make invalid SQL
  1074. *
  1075. * @return void
  1076. */
  1077. public function testSubqueryInSelectExpression()
  1078. {
  1079. $this->loadFixtures('Comments');
  1080. $table = $this->getTableLocator()->get('Comments');
  1081. $ratio = $table->find()
  1082. ->select(function ($query) use ($table) {
  1083. $allCommentsCount = $table->find()->select($query->func()->count('*'));
  1084. $countToFloat = $query->newExpr([$query->func()->count('*'), '1.0'])->setConjunction('*');
  1085. return [
  1086. 'ratio' => $query
  1087. ->newExpr($countToFloat)
  1088. ->add($allCommentsCount)
  1089. ->setConjunction('/'),
  1090. ];
  1091. })
  1092. ->where(['user_id' => 1])
  1093. ->first()
  1094. ->ratio;
  1095. $this->assertEquals(0.5, $ratio);
  1096. }
  1097. /**
  1098. * Tests calling last on an empty table
  1099. *
  1100. * @see https://github.com/cakephp/cakephp/issues/6683
  1101. * @return void
  1102. */
  1103. public function testFindLastOnEmptyTable()
  1104. {
  1105. $this->loadFixtures('Comments');
  1106. $table = $this->getTableLocator()->get('Comments');
  1107. $table->deleteAll(['1 = 1']);
  1108. $this->assertEquals(0, $table->find()->count());
  1109. $this->assertNull($table->find()->last());
  1110. }
  1111. /**
  1112. * Tests calling contain in a nested closure
  1113. *
  1114. * @see https://github.com/cakephp/cakephp/issues/7591
  1115. * @return void
  1116. */
  1117. public function testContainInNestedClosure()
  1118. {
  1119. $this->loadFixtures('Comments', 'Articles', 'Authors', 'Tags', 'AuthorsTags');
  1120. $table = $this->getTableLocator()->get('Comments');
  1121. $table->belongsTo('Articles');
  1122. $table->Articles->belongsTo('Authors');
  1123. $table->Articles->Authors->belongsToMany('Tags');
  1124. $query = $table->find()->where(['Comments.id' => 5])->contain(['Articles' => function ($q) {
  1125. return $q->contain(['Authors' => function ($q) {
  1126. return $q->contain('Tags');
  1127. }]);
  1128. }]);
  1129. $this->assertCount(2, $query->first()->article->author->tags);
  1130. }
  1131. /**
  1132. * Test that the typemaps used in function expressions
  1133. * create the correct results.
  1134. *
  1135. * @return void
  1136. */
  1137. public function testTypemapInFunctions()
  1138. {
  1139. $this->loadFixtures('Comments');
  1140. $table = $this->getTableLocator()->get('Comments');
  1141. $table->updateAll(['published' => null], ['1 = 1']);
  1142. $query = $table->find();
  1143. $query->select([
  1144. 'id',
  1145. 'coalesced' => $query->func()->coalesce(
  1146. ['published' => 'identifier', -1],
  1147. ['integer']
  1148. ),
  1149. ]);
  1150. $result = $query->all()->first();
  1151. $this->assertSame(
  1152. -1,
  1153. $result['coalesced'],
  1154. 'Output values for functions should be casted'
  1155. );
  1156. }
  1157. /**
  1158. * Test that the typemaps used in function expressions
  1159. * create the correct results.
  1160. *
  1161. * @return void
  1162. */
  1163. public function testTypemapInFunctions2()
  1164. {
  1165. $this->loadFixtures('Comments');
  1166. $table = $this->getTableLocator()->get('Comments');
  1167. $query = $table->find();
  1168. $query->select([
  1169. 'max' => $query->func()->max('created', ['datetime']),
  1170. ]);
  1171. $result = $query->all()->first();
  1172. $this->assertEquals(new Time('2007-03-18 10:55:23'), $result['max']);
  1173. }
  1174. /**
  1175. * Test that contain queries map types correctly.
  1176. *
  1177. * @return void
  1178. */
  1179. public function testBooleanConditionsInContain()
  1180. {
  1181. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  1182. $table = $this->getTableLocator()->get('Articles');
  1183. $table->belongsToMany('Tags', [
  1184. 'foreignKey' => 'article_id',
  1185. 'associationForeignKey' => 'tag_id',
  1186. 'through' => 'SpecialTags',
  1187. ]);
  1188. $query = $table->find()
  1189. ->contain(['Tags' => function ($q) {
  1190. return $q->where(['SpecialTags.highlighted_time >' => new Time('2014-06-01 00:00:00')]);
  1191. }])
  1192. ->where(['Articles.id' => 2]);
  1193. $result = $query->first();
  1194. $this->assertEquals(2, $result->id);
  1195. $this->assertNotEmpty($result->tags, 'Missing tags');
  1196. $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
  1197. }
  1198. /**
  1199. * Test that contain queries map types correctly.
  1200. *
  1201. * @return void
  1202. */
  1203. public function testComplexTypesInJoinedWhere()
  1204. {
  1205. $this->loadFixtures('Comments', 'Users');
  1206. $table = $this->getTableLocator()->get('Users');
  1207. $table->hasOne('Comments', [
  1208. 'foreignKey' => 'user_id',
  1209. ]);
  1210. $query = $table->find()
  1211. ->contain('Comments')
  1212. ->where([
  1213. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00'),
  1214. ]);
  1215. $result = $query->first();
  1216. $this->assertNotEmpty($result);
  1217. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->updated);
  1218. }
  1219. /**
  1220. * Test that nested contain queries map types correctly.
  1221. *
  1222. * @return void
  1223. */
  1224. public function testComplexNestedTypesInJoinedWhere()
  1225. {
  1226. $this->loadFixtures('Comments', 'Users', 'Articles');
  1227. $table = $this->getTableLocator()->get('Users');
  1228. $table->hasOne('Comments', [
  1229. 'foreignKey' => 'user_id',
  1230. ]);
  1231. $table->Comments->belongsTo('Articles');
  1232. $table->Comments->Articles->belongsTo('Authors', [
  1233. 'className' => 'Users',
  1234. 'foreignKey' => 'author_id',
  1235. ]);
  1236. $query = $table->find()
  1237. ->contain('Comments.Articles.Authors')
  1238. ->where([
  1239. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00'),
  1240. ]);
  1241. $result = $query->first();
  1242. $this->assertNotEmpty($result);
  1243. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->article->author->updated);
  1244. }
  1245. /**
  1246. * Test that matching queries map types correctly.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testComplexTypesInJoinedWhereWithMatching()
  1251. {
  1252. $this->loadFixtures('Comments', 'Users', 'Articles');
  1253. $table = $this->getTableLocator()->get('Users');
  1254. $table->hasOne('Comments', [
  1255. 'foreignKey' => 'user_id',
  1256. ]);
  1257. $table->Comments->belongsTo('Articles');
  1258. $table->Comments->Articles->belongsTo('Authors', [
  1259. 'className' => 'Users',
  1260. 'foreignKey' => 'author_id',
  1261. ]);
  1262. $query = $table->find()
  1263. ->matching('Comments')
  1264. ->where([
  1265. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00'),
  1266. ]);
  1267. $result = $query->first();
  1268. $this->assertNotEmpty($result);
  1269. $this->assertInstanceOf('Cake\I18n\Time', $result->_matchingData['Comments']->updated);
  1270. $query = $table->find()
  1271. ->matching('Comments.Articles.Authors')
  1272. ->where([
  1273. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00'),
  1274. ]);
  1275. $result = $query->first();
  1276. $this->assertNotEmpty($result);
  1277. $this->assertInstanceOf('Cake\I18n\Time', $result->_matchingData['Authors']->updated);
  1278. }
  1279. /**
  1280. * Test that notMatching queries map types correctly.
  1281. *
  1282. * @return void
  1283. */
  1284. public function testComplexTypesInJoinedWhereWithNotMatching()
  1285. {
  1286. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  1287. $Tags = $this->getTableLocator()->get('Tags');
  1288. $Tags->belongsToMany('Articles');
  1289. $query = $Tags->find()
  1290. ->notMatching('Articles', function ($q) {
  1291. return $q ->where(['ArticlesTags.tag_id !=' => 3 ]);
  1292. })
  1293. ->where([
  1294. 'Tags.created <' => new \DateTime('2016-01-02 00:00:00'),
  1295. ]);
  1296. $result = $query->first();
  1297. $this->assertNotEmpty($result);
  1298. $this->assertEquals(3, $result->id);
  1299. $this->assertInstanceOf('Cake\I18n\Time', $result->created);
  1300. }
  1301. /**
  1302. * Test that innerJoinWith queries map types correctly.
  1303. *
  1304. * @return void
  1305. */
  1306. public function testComplexTypesInJoinedWhereWithInnerJoinWith()
  1307. {
  1308. $this->loadFixtures('Comments', 'Users', 'Articles');
  1309. $table = $this->getTableLocator()->get('Users');
  1310. $table->hasOne('Comments', [
  1311. 'foreignKey' => 'user_id',
  1312. ]);
  1313. $table->Comments->belongsTo('Articles');
  1314. $table->Comments->Articles->belongsTo('Authors', [
  1315. 'className' => 'Users',
  1316. 'foreignKey' => 'author_id',
  1317. ]);
  1318. $query = $table->find()
  1319. ->innerJoinWith('Comments')
  1320. ->where([
  1321. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00'),
  1322. ]);
  1323. $result = $query->first();
  1324. $this->assertNotEmpty($result);
  1325. $this->assertInstanceOf('Cake\I18n\Time', $result->updated);
  1326. $query = $table->find()
  1327. ->innerJoinWith('Comments.Articles.Authors')
  1328. ->where([
  1329. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00'),
  1330. ]);
  1331. $result = $query->first();
  1332. $this->assertNotEmpty($result);
  1333. $this->assertInstanceOf('Cake\I18n\Time', $result->updated);
  1334. }
  1335. /**
  1336. * Test that leftJoinWith queries map types correctly.
  1337. *
  1338. * @return void
  1339. */
  1340. public function testComplexTypesInJoinedWhereWithLeftJoinWith()
  1341. {
  1342. $this->loadFixtures('Comments', 'Users', 'Articles');
  1343. $table = $this->getTableLocator()->get('Users');
  1344. $table->hasOne('Comments', [
  1345. 'foreignKey' => 'user_id',
  1346. ]);
  1347. $table->Comments->belongsTo('Articles');
  1348. $table->Comments->Articles->belongsTo('Authors', [
  1349. 'className' => 'Users',
  1350. 'foreignKey' => 'author_id',
  1351. ]);
  1352. $query = $table->find()
  1353. ->leftJoinWith('Comments')
  1354. ->where([
  1355. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00'),
  1356. ]);
  1357. $result = $query->first();
  1358. $this->assertNotEmpty($result);
  1359. $this->assertInstanceOf('Cake\I18n\Time', $result->updated);
  1360. $query = $table->find()
  1361. ->leftJoinWith('Comments.Articles.Authors')
  1362. ->where([
  1363. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00'),
  1364. ]);
  1365. $result = $query->first();
  1366. $this->assertNotEmpty($result);
  1367. $this->assertInstanceOf('Cake\I18n\Time', $result->updated);
  1368. }
  1369. /**
  1370. * Tests that it is possible to contain to fetch
  1371. * associations off of a junction table.
  1372. *
  1373. * @return void
  1374. */
  1375. public function testBelongsToManyJoinDataAssociation()
  1376. {
  1377. $this->loadFixtures('Authors', 'Articles', 'Tags', 'SpecialTags');
  1378. $articles = $this->getTableLocator()->get('Articles');
  1379. $tags = $this->getTableLocator()->get('Tags');
  1380. $tags->hasMany('SpecialTags');
  1381. $specialTags = $this->getTableLocator()->get('SpecialTags');
  1382. $specialTags->belongsTo('Authors');
  1383. $specialTags->belongsTo('Articles');
  1384. $specialTags->belongsTo('Tags');
  1385. $articles->belongsToMany('Tags', [
  1386. 'through' => $specialTags,
  1387. ]);
  1388. $query = $articles->find()
  1389. ->contain(['Tags', 'Tags.SpecialTags.Authors'])
  1390. ->where(['Articles.id' => 1]);
  1391. $result = $query->first();
  1392. $this->assertNotEmpty($result->tags, 'Missing tags');
  1393. $this->assertNotEmpty($result->tags[0], 'Missing first tag');
  1394. $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing _joinData');
  1395. $this->assertNotEmpty($result->tags[0]->special_tags[0]->author, 'Missing author on _joinData');
  1396. }
  1397. /**
  1398. * Tests that it is possible to use matching with dot notation
  1399. * even when part of the part of the path in the dot notation is
  1400. * shared for two different calls
  1401. *
  1402. * @return void
  1403. */
  1404. public function testDotNotationNotOverride()
  1405. {
  1406. $this->loadFixtures('Comments', 'Articles', 'Tags', 'Authors', 'SpecialTags');
  1407. $table = $this->getTableLocator()->get('Comments');
  1408. $articles = $table->belongsTo('Articles');
  1409. $specialTags = $articles->hasMany('SpecialTags');
  1410. $specialTags->belongsTo('Authors');
  1411. $specialTags->belongsTo('Tags');
  1412. $results = $table
  1413. ->find()
  1414. ->select(['name' => 'Authors.name', 'tag' => 'Tags.name'])
  1415. ->matching('Articles.SpecialTags.Tags')
  1416. ->matching('Articles.SpecialTags.Authors', function ($q) {
  1417. return $q->where(['Authors.id' => 2]);
  1418. })
  1419. ->distinct()
  1420. ->enableHydration(false)
  1421. ->toArray();
  1422. $this->assertEquals([['name' => 'nate', 'tag' => 'tag1']], $results);
  1423. }
  1424. /**
  1425. * Test expression based ordering with unions.
  1426. *
  1427. * @return void
  1428. */
  1429. public function testComplexOrderWithUnion()
  1430. {
  1431. $this->loadFixtures('Comments');
  1432. $table = $this->getTableLocator()->get('Comments');
  1433. $query = $table->find();
  1434. $inner = $table->find()
  1435. ->select(['content' => 'comment'])
  1436. ->where(['id >' => 3]);
  1437. $inner2 = $table->find()
  1438. ->select(['content' => 'comment'])
  1439. ->where(['id <' => 3]);
  1440. $order = $query->func()->concat(['content' => 'literal', 'test']);
  1441. $query->select(['inside.content'])
  1442. ->from(['inside' => $inner->unionAll($inner2)])
  1443. ->orderAsc($order);
  1444. $results = $query->toArray();
  1445. $this->assertCount(5, $results);
  1446. }
  1447. /**
  1448. * Test that associations that are loaded with subqueries
  1449. * do not cause errors when the subquery has a limit & order clause.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testEagerLoadOrderAndSubquery()
  1454. {
  1455. $this->loadFixtures('Articles', 'Comments');
  1456. $table = $this->getTableLocator()->get('Articles');
  1457. $table->hasMany('Comments', [
  1458. 'strategy' => 'subquery',
  1459. ]);
  1460. $query = $table->find()
  1461. ->select(['score' => 100])
  1462. ->enableAutoFields(true)
  1463. ->contain(['Comments'])
  1464. ->limit(5)
  1465. ->order(['score' => 'desc']);
  1466. $result = $query->all();
  1467. $this->assertCount(3, $result);
  1468. }
  1469. /**
  1470. * Tests that decorating the results does not result in a memory leak
  1471. *
  1472. * @return void
  1473. */
  1474. public function testFormatResultsMemoryLeak()
  1475. {
  1476. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  1477. $this->skipIf(env('CODECOVERAGE') == 1, 'Running coverage this causes this tests to fail sometimes.');
  1478. $table = $this->getTableLocator()->get('Articles');
  1479. $table->belongsTo('Authors');
  1480. $table->belongsToMany('Tags');
  1481. gc_collect_cycles();
  1482. $memory = memory_get_usage() / 1024 / 1024;
  1483. foreach (range(1, 3) as $time) {
  1484. $table->find()
  1485. ->contain(['Authors', 'Tags'])
  1486. ->formatResults(function ($results) {
  1487. return $results;
  1488. })
  1489. ->all();
  1490. }
  1491. gc_collect_cycles();
  1492. $endMemory = memory_get_usage() / 1024 / 1024;
  1493. $this->assertWithinRange($endMemory, $memory, 1.25, 'Memory leak in ResultSet');
  1494. }
  1495. /**
  1496. * Tests that having bound placeholders in the order clause does not result
  1497. * in an error when trying to count a query.
  1498. *
  1499. * @return void
  1500. */
  1501. public function testCountWithComplexOrderBy()
  1502. {
  1503. $this->loadFixtures('Articles');
  1504. $table = $this->getTableLocator()->get('Articles');
  1505. $query = $table->find();
  1506. $query->orderDesc($query->newExpr()->addCase(
  1507. [$query->newExpr()->add(['id' => 3])],
  1508. [1, 0]
  1509. ));
  1510. $query->order(['title' => 'desc']);
  1511. // Executing the normal query before getting the count
  1512. $query->all();
  1513. $this->assertEquals(3, $query->count());
  1514. $table = $this->getTableLocator()->get('Articles');
  1515. $query = $table->find();
  1516. $query->orderDesc($query->newExpr()->addCase(
  1517. [$query->newExpr()->add(['id' => 3])],
  1518. [1, 0]
  1519. ));
  1520. $query->orderDesc($query->newExpr()->add(['id' => 3]));
  1521. // Not executing the query first, just getting the count
  1522. $this->assertEquals(3, $query->count());
  1523. }
  1524. /**
  1525. * Tests that the now() function expression can be used in the
  1526. * where clause of a query
  1527. *
  1528. * @see https://github.com/cakephp/cakephp/issues/7943
  1529. * @return void
  1530. */
  1531. public function testFunctionInWhereClause()
  1532. {
  1533. $this->loadFixtures('Comments');
  1534. $table = $this->getTableLocator()->get('Comments');
  1535. $table->updateAll(['updated' => Time::tomorrow()], ['id' => 6]);
  1536. $query = $table->find();
  1537. $result = $query->where(['updated >' => $query->func()->now('datetime')])->first();
  1538. $this->assertSame(6, $result->id);
  1539. }
  1540. /**
  1541. * Tests that `notMatching()` can be used on `belongsToMany`
  1542. * associations without passing a query builder callback.
  1543. *
  1544. * @return void
  1545. */
  1546. public function testNotMatchingForBelongsToManyWithoutQueryBuilder()
  1547. {
  1548. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  1549. $Articles = $this->getTableLocator()->get('Articles');
  1550. $Articles->belongsToMany('Tags');
  1551. $result = $Articles->find('list')->notMatching('Tags')->toArray();
  1552. $expected = [
  1553. 3 => 'Third Article',
  1554. ];
  1555. $this->assertEquals($expected, $result);
  1556. }
  1557. /**
  1558. * Tests deep formatters get the right object type when applied in a beforeFind
  1559. *
  1560. * @see https://github.com/cakephp/cakephp/issues/9787
  1561. * @return void
  1562. */
  1563. public function testFormatDeepDistantAssociationRecords2()
  1564. {
  1565. $this->loadFixtures('Authors', 'Articles', 'Tags', 'ArticlesTags');
  1566. $table = $this->getTableLocator()->get('authors');
  1567. $table->hasMany('articles');
  1568. $articles = $table->getAssociation('articles')->getTarget();
  1569. $articles->hasMany('articlesTags');
  1570. $tags = $articles->getAssociation('articlesTags')->getTarget()->belongsTo('tags');
  1571. $tags->getTarget()->getEventManager()->on('Model.beforeFind', function ($e, $query) {
  1572. return $query->formatResults(function ($results) {
  1573. return $results->map(function (\Cake\ORM\Entity $tag) {
  1574. $tag->name .= ' - visited';
  1575. return $tag;
  1576. });
  1577. });
  1578. });
  1579. $query = $table->find()->contain(['articles.articlesTags.tags']);
  1580. $query->mapReduce(function ($row, $key, $mr) {
  1581. foreach ((array)$row->articles as $article) {
  1582. foreach ((array)$article->articles_tags as $articleTag) {
  1583. $mr->emit($articleTag->tag->name);
  1584. }
  1585. }
  1586. });
  1587. $expected = ['tag1 - visited', 'tag2 - visited', 'tag1 - visited', 'tag3 - visited'];
  1588. $this->assertEquals($expected, $query->toArray());
  1589. }
  1590. /**
  1591. * Tests that subqueries can be used with function expressions.
  1592. *
  1593. * @return void
  1594. */
  1595. public function testFunctionExpressionWithSubquery()
  1596. {
  1597. $this->loadFixtures('Articles');
  1598. $table = $this->getTableLocator()->get('Articles');
  1599. $query = $table
  1600. ->find()
  1601. ->select(function (Query $q) use ($table) {
  1602. return [
  1603. 'value' => $q
  1604. ->func()
  1605. ->ABS([
  1606. $table
  1607. ->getConnection()
  1608. ->newQuery()
  1609. ->select(-1),
  1610. ])
  1611. ->setReturnType('integer'),
  1612. ];
  1613. });
  1614. $result = $query->first()->get('value');
  1615. $this->assertEquals(1, $result);
  1616. }
  1617. /**
  1618. * Tests that correlated subqueries can be used with function expressions.
  1619. *
  1620. * @return void
  1621. */
  1622. public function testFunctionExpressionWithCorrelatedSubquery()
  1623. {
  1624. $this->loadFixtures('Articles', 'Authors');
  1625. $table = $this->getTableLocator()->get('Articles');
  1626. $table->belongsTo('Authors');
  1627. $query = $table
  1628. ->find()
  1629. ->select(function (Query $q) use ($table) {
  1630. return [
  1631. 'value' => $q->func()->UPPER([
  1632. $table
  1633. ->getAssociation('Authors')
  1634. ->find()
  1635. ->select(['Authors.name'])
  1636. ->where(function (QueryExpression $exp) {
  1637. return $exp->equalFields('Authors.id', 'Articles.author_id');
  1638. }),
  1639. ]),
  1640. ];
  1641. });
  1642. $result = $query->first()->get('value');
  1643. $this->assertEquals('MARIANO', $result);
  1644. }
  1645. /**
  1646. * Tests that subqueries can be used with multi argument function expressions.
  1647. *
  1648. * @return void
  1649. */
  1650. public function testMultiArgumentFunctionExpressionWithSubquery()
  1651. {
  1652. $this->loadFixtures('Articles', 'Authors');
  1653. $table = $this->getTableLocator()->get('Articles');
  1654. $query = $table
  1655. ->find()
  1656. ->select(function (Query $q) use ($table) {
  1657. return [
  1658. 'value' => $q
  1659. ->func()
  1660. ->ROUND(
  1661. [
  1662. $table
  1663. ->getConnection()
  1664. ->newQuery()
  1665. ->select(1.23456),
  1666. 2,
  1667. ],
  1668. [null, 'integer']
  1669. )
  1670. ->setReturnType('float'),
  1671. ];
  1672. });
  1673. $result = $query->first()->get('value');
  1674. $this->assertEquals(1.23, $result);
  1675. }
  1676. /**
  1677. * Tests that correlated subqueries can be used with multi argument function expressions.
  1678. *
  1679. * @return void
  1680. */
  1681. public function testMultiArgumentFunctionExpressionWithCorrelatedSubquery()
  1682. {
  1683. $this->loadFixtures('Articles', 'Authors');
  1684. $table = $this->getTableLocator()->get('Articles');
  1685. $table->belongsTo('Authors');
  1686. $this->assertEquals(
  1687. 1,
  1688. $table->getAssociation('Authors')->updateAll(['name' => null], ['id' => 3])
  1689. );
  1690. $query = $table
  1691. ->find()
  1692. ->select(function (Query $q) use ($table) {
  1693. return [
  1694. 'value' => $q->func()->coalesce([
  1695. $table
  1696. ->getAssociation('Authors')
  1697. ->find()
  1698. ->select(['Authors.name'])
  1699. ->where(function (QueryExpression $exp) {
  1700. return $exp->equalFields('Authors.id', 'Articles.author_id');
  1701. }),
  1702. '1',
  1703. ]),
  1704. ];
  1705. });
  1706. $results = $query->extract('value')->toArray();
  1707. $this->assertEquals(['mariano', '1', 'mariano'], $results);
  1708. }
  1709. /**
  1710. * Tests that subqueries can be used with function expressions that are being transpiled.
  1711. *
  1712. * @return void
  1713. */
  1714. public function testTranspiledFunctionExpressionWithSubquery()
  1715. {
  1716. $this->loadFixtures('Articles', 'Authors');
  1717. $table = $this->getTableLocator()->get('Articles');
  1718. $table->belongsTo('Authors');
  1719. $query = $table
  1720. ->find()
  1721. ->select(function (Query $q) use ($table) {
  1722. return [
  1723. 'value' => $q->func()->concat([
  1724. $table
  1725. ->getAssociation('Authors')
  1726. ->find()
  1727. ->select(['Authors.name'])
  1728. ->where(['Authors.id' => 1]),
  1729. ' appended',
  1730. ]),
  1731. ];
  1732. });
  1733. $result = $query->first()->get('value');
  1734. $this->assertEquals('mariano appended', $result);
  1735. }
  1736. /**
  1737. * Tests that correlated subqueries can be used with function expressions that are being transpiled.
  1738. *
  1739. * @return void
  1740. */
  1741. public function testTranspiledFunctionExpressionWithCorrelatedSubquery()
  1742. {
  1743. $this->loadFixtures('Articles', 'Authors');
  1744. $table = $this->getTableLocator()->get('Articles');
  1745. $table->belongsTo('Authors');
  1746. $query = $table
  1747. ->find()
  1748. ->select(function (Query $q) use ($table) {
  1749. return [
  1750. 'value' => $q->func()->concat([
  1751. $table
  1752. ->getAssociation('Authors')
  1753. ->find()
  1754. ->select(['Authors.name'])
  1755. ->where(function (QueryExpression $exp) {
  1756. return $exp->equalFields('Authors.id', 'Articles.author_id');
  1757. }),
  1758. ' appended',
  1759. ]),
  1760. ];
  1761. });
  1762. $result = $query->first()->get('value');
  1763. $this->assertEquals('mariano appended', $result);
  1764. }
  1765. }