QueryRegressionTest.php 65 KB

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