QueryRegressionTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Core\Plugin;
  17. use Cake\I18n\Time;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Contains regression test for the Query builder
  22. *
  23. */
  24. class QueryRegressionTest extends TestCase
  25. {
  26. /**
  27. * Fixture to be used
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.articles',
  33. 'core.tags',
  34. 'core.articles_tags',
  35. 'core.authors',
  36. 'core.authors_tags',
  37. 'core.comments',
  38. 'core.featured_tags',
  39. 'core.special_tags',
  40. 'core.tags_translations',
  41. 'core.translates',
  42. 'core.users'
  43. ];
  44. public $autoFixtures = false;
  45. /**
  46. * Tear down
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Test for https://github.com/cakephp/cakephp/issues/3087
  57. *
  58. * @return void
  59. */
  60. public function testSelectTimestampColumn()
  61. {
  62. $this->loadFixtures('Users');
  63. $table = TableRegistry::get('users');
  64. $user = $table->find()->where(['id' => 1])->first();
  65. $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created);
  66. $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated);
  67. }
  68. /**
  69. * Tests that EagerLoader does not try to create queries for associations having no
  70. * keys to compare against
  71. *
  72. * @return void
  73. */
  74. public function testEagerLoadingFromEmptyResults()
  75. {
  76. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  77. $table = TableRegistry::get('Articles');
  78. $table->belongsToMany('ArticlesTags');
  79. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  80. $this->assertEmpty($results);
  81. }
  82. /**
  83. * Tests that eagerloading belongsToMany with find list fails with a helpful message.
  84. *
  85. * @expectedException \InvalidArgumentException
  86. * @return void
  87. */
  88. public function testEagerLoadingBelongsToManyList()
  89. {
  90. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  91. $table = TableRegistry::get('Articles');
  92. $table->belongsToMany('Tags', [
  93. 'finder' => 'list'
  94. ]);
  95. $table->find()->contain('Tags')->toArray();
  96. }
  97. /**
  98. * Tests that duplicate aliases in contain() can be used, even when they would
  99. * naturally be attached to the query instead of eagerly loaded. What should
  100. * happen here is that One of the duplicates will be changed to be loaded using
  101. * an extra query, but yielding the same results
  102. *
  103. * @return void
  104. */
  105. public function testDuplicateAttachableAliases()
  106. {
  107. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags', 'Authors');
  108. TableRegistry::get('Stuff', ['table' => 'tags']);
  109. TableRegistry::get('Things', ['table' => 'articles_tags']);
  110. $table = TableRegistry::get('Articles');
  111. $table->belongsTo('Authors');
  112. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  113. $table->Authors->target()->hasOne('Stuff', [
  114. 'foreignKey' => 'id',
  115. 'propertyName' => 'favorite_tag'
  116. ]);
  117. $table->Things->target()->belongsTo('Stuff', [
  118. 'foreignKey' => 'tag_id',
  119. 'propertyName' => 'foo'
  120. ]);
  121. $results = $table->find()
  122. ->contain(['Authors.Stuff', 'Things.Stuff'])
  123. ->order(['Articles.id' => 'ASC'])
  124. ->toArray();
  125. $this->assertCount(5, $results);
  126. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  127. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  128. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  129. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  130. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  131. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  132. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  133. }
  134. /**
  135. * Test for https://github.com/cakephp/cakephp/issues/3410
  136. *
  137. * @return void
  138. */
  139. public function testNullableTimeColumn()
  140. {
  141. $this->loadFixtures('Users');
  142. $table = TableRegistry::get('users');
  143. $entity = $table->newEntity(['username' => 'derp', 'created' => null]);
  144. $this->assertSame($entity, $table->save($entity));
  145. $this->assertNull($entity->created);
  146. }
  147. /**
  148. * Test for https://github.com/cakephp/cakephp/issues/3626
  149. *
  150. * Checks that join data is actually created and not tried to be updated every time
  151. * @return void
  152. */
  153. public function testCreateJointData()
  154. {
  155. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  156. $articles = TableRegistry::get('Articles');
  157. $articles->belongsToMany('Highlights', [
  158. 'className' => 'TestApp\Model\Table\TagsTable',
  159. 'foreignKey' => 'article_id',
  160. 'targetForeignKey' => 'tag_id',
  161. 'through' => 'SpecialTags'
  162. ]);
  163. $entity = $articles->get(2);
  164. $data = [
  165. 'id' => 2,
  166. 'highlights' => [
  167. [
  168. 'name' => 'New Special Tag',
  169. '_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00']
  170. ]
  171. ]
  172. ];
  173. $entity = $articles->patchEntity($entity, $data, ['Highlights._joinData']);
  174. $articles->save($entity);
  175. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  176. $this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id);
  177. $this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d'));
  178. }
  179. /**
  180. * Tests that the junction table instance taken from both sides of a belongsToMany
  181. * relationship is actually the same object.
  182. *
  183. * @return void
  184. */
  185. public function testReciprocalBelongsToMany()
  186. {
  187. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  188. $articles = TableRegistry::get('Articles');
  189. $tags = TableRegistry::get('Tags');
  190. $articles->belongsToMany('Tags');
  191. $tags->belongsToMany('Articles');
  192. $left = $articles->Tags->junction();
  193. $right = $tags->Articles->junction();
  194. $this->assertSame($left, $right);
  195. }
  196. /**
  197. * Test for https://github.com/cakephp/cakephp/issues/4253
  198. *
  199. * Makes sure that the belongsToMany association is not overwritten with conflicting information
  200. * by any of the sides when the junction() function is invoked
  201. *
  202. * @return void
  203. */
  204. public function testReciprocalBelongsToManyNoOverwrite()
  205. {
  206. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  207. $articles = TableRegistry::get('Articles');
  208. $tags = TableRegistry::get('Tags');
  209. $articles->belongsToMany('Tags');
  210. $tags->belongsToMany('Articles');
  211. $sub = $articles->Tags->find()->select(['Tags.id'])->matching('Articles', function ($q) {
  212. return $q->where(['Articles.id' => 1]);
  213. });
  214. $query = $articles->Tags->find()->where(['Tags.id NOT IN' => $sub]);
  215. $this->assertEquals(1, $query->count());
  216. }
  217. /**
  218. * Returns an array with the saving strategies for a belongsTo association
  219. *
  220. * @return array
  221. */
  222. public function strategyProvider()
  223. {
  224. return [
  225. ['append'],
  226. ['replace'],
  227. ];
  228. }
  229. /**
  230. * Test for https://github.com/cakephp/cakephp/issues/3677 and
  231. * https://github.com/cakephp/cakephp/issues/3714
  232. *
  233. * Checks that only relevant associations are passed when saving _joinData
  234. * Tests that _joinData can also save deeper associations
  235. *
  236. * @dataProvider strategyProvider
  237. * @param string $strategy
  238. * @return void
  239. */
  240. public function testBelongsToManyDeepSave($strategy)
  241. {
  242. $this->loadFixtures('Articles', 'Tags', 'SpecialTags', 'Authors');
  243. $articles = TableRegistry::get('Articles');
  244. $articles->belongsToMany('Highlights', [
  245. 'className' => 'TestApp\Model\Table\TagsTable',
  246. 'foreignKey' => 'article_id',
  247. 'targetForeignKey' => 'tag_id',
  248. 'through' => 'SpecialTags',
  249. 'saveStrategy' => $strategy
  250. ]);
  251. $articles->Highlights->junction()->belongsTo('Authors');
  252. $articles->Highlights->hasOne('Authors', [
  253. 'foreignKey' => 'id'
  254. ]);
  255. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  256. $data = [
  257. 'highlights' => [
  258. [
  259. 'name' => 'New Special Tag',
  260. '_joinData' => [
  261. 'highlighted' => true,
  262. 'highlighted_time' => '2014-06-01 10:10:00',
  263. 'author' => [
  264. 'name' => 'mariano'
  265. ]
  266. ],
  267. 'author' => ['name' => 'mark']
  268. ]
  269. ]
  270. ];
  271. $options = [
  272. 'associated' => [
  273. 'Highlights._joinData.Authors', 'Highlights.Authors'
  274. ]
  275. ];
  276. $entity = $articles->patchEntity($entity, $data, $options);
  277. $articles->save($entity, $options);
  278. $entity = $articles->get(2, [
  279. 'contain' => [
  280. 'SpecialTags' => ['sort' => ['SpecialTags.id' => 'ASC']],
  281. 'SpecialTags.Authors',
  282. 'Highlights.Authors'
  283. ]
  284. ]);
  285. $this->assertEquals('mark', end($entity->highlights)->author->name);
  286. $lastTag = end($entity->special_tags);
  287. $this->assertTrue($lastTag->highlighted);
  288. $this->assertEquals('2014-06-01 10:10:00', $lastTag->highlighted_time->format('Y-m-d H:i:s'));
  289. $this->assertEquals('mariano', $lastTag->author->name);
  290. }
  291. /**
  292. * Tests that no exceptions are generated becuase of ambiguous column names in queries
  293. * during a save operation
  294. *
  295. * @see https://github.com/cakephp/cakephp/issues/3803
  296. * @return void
  297. */
  298. public function testSaveWithCallbacks()
  299. {
  300. $this->loadFixtures('Articles', 'Authors');
  301. $articles = TableRegistry::get('Articles');
  302. $articles->belongsTo('Authors');
  303. $articles->eventManager()->attach(function ($event, $query) {
  304. return $query->contain('Authors');
  305. }, 'Model.beforeFind');
  306. $article = $articles->newEntity();
  307. $article->title = 'Foo';
  308. $article->body = 'Bar';
  309. $this->assertSame($article, $articles->save($article));
  310. }
  311. /**
  312. * Test that save() works with entities containing expressions
  313. * as properties.
  314. *
  315. * @return void
  316. */
  317. public function testSaveWithExpressionProperty()
  318. {
  319. $this->loadFixtures('Articles');
  320. $articles = TableRegistry::get('Articles');
  321. $article = $articles->newEntity();
  322. $article->title = new \Cake\Database\Expression\QueryExpression("SELECT 'jose'");
  323. $this->assertSame($article, $articles->save($article));
  324. }
  325. /**
  326. * Tests that whe saving deep associations for a belongsToMany property,
  327. * data is not removed becuase of excesive associations filtering.
  328. *
  329. * @see https://github.com/cakephp/cakephp/issues/4009
  330. * @return void
  331. */
  332. public function testBelongsToManyDeepSave2()
  333. {
  334. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  335. $articles = TableRegistry::get('Articles');
  336. $articles->belongsToMany('Highlights', [
  337. 'className' => 'TestApp\Model\Table\TagsTable',
  338. 'foreignKey' => 'article_id',
  339. 'targetForeignKey' => 'tag_id',
  340. 'through' => 'SpecialTags',
  341. ]);
  342. $articles->Highlights->hasMany('TopArticles', [
  343. 'className' => 'TestApp\Model\Table\ArticlesTable',
  344. 'foreignKey' => 'author_id',
  345. ]);
  346. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  347. $data = [
  348. 'highlights' => [
  349. [
  350. 'name' => 'New Special Tag',
  351. '_joinData' => [
  352. 'highlighted' => true,
  353. 'highlighted_time' => '2014-06-01 10:10:00',
  354. ],
  355. 'top_articles' => [
  356. ['title' => 'First top article'],
  357. ['title' => 'Second top article'],
  358. ]
  359. ]
  360. ]
  361. ];
  362. $options = [
  363. 'associated' => [
  364. 'Highlights._joinData', 'Highlights.TopArticles'
  365. ]
  366. ];
  367. $entity = $articles->patchEntity($entity, $data, $options);
  368. $articles->save($entity, $options);
  369. $entity = $articles->get(2, [
  370. 'contain' => [
  371. 'Highlights.TopArticles'
  372. ]
  373. ]);
  374. $highlights = $entity->highlights[0];
  375. $this->assertEquals('First top article', $highlights->top_articles[0]->title);
  376. $this->assertEquals('Second top article', $highlights->top_articles[1]->title);
  377. $this->assertEquals(
  378. new Time('2014-06-01 10:10:00'),
  379. $highlights->_joinData->highlighted_time
  380. );
  381. }
  382. /**
  383. * An integration test that spot checks that associations use the
  384. * correct alias names to generate queries.
  385. *
  386. * @return void
  387. */
  388. public function testPluginAssociationQueryGeneration()
  389. {
  390. $this->loadFixtures('Articles', 'Comments', 'Authors');
  391. Plugin::load('TestPlugin');
  392. $articles = TableRegistry::get('Articles');
  393. $articles->hasMany('TestPlugin.Comments');
  394. $articles->belongsTo('TestPlugin.Authors');
  395. $result = $articles->find()
  396. ->where(['Articles.id' => 2])
  397. ->contain(['Comments', 'Authors'])
  398. ->first();
  399. $this->assertNotEmpty(
  400. $result->comments[0]->id,
  401. 'No SQL error and comment exists.'
  402. );
  403. $this->assertNotEmpty(
  404. $result->author->id,
  405. 'No SQL error and author exists.'
  406. );
  407. }
  408. /**
  409. * Tests that loading associations having the same alias in the
  410. * joinable associations chain is not sensitive to the order in which
  411. * the associations are selected.
  412. *
  413. * @see https://github.com/cakephp/cakephp/issues/4454
  414. * @return void
  415. */
  416. public function testAssociationChainOrder()
  417. {
  418. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags', 'Authors');
  419. $articles = TableRegistry::get('Articles');
  420. $articles->belongsTo('Authors');
  421. $articles->hasOne('ArticlesTags');
  422. $articlesTags = TableRegistry::get('ArticlesTags');
  423. $articlesTags->belongsTo('Authors', [
  424. 'foreignKey' => 'tag_id'
  425. ]);
  426. $resultA = $articles->find()
  427. ->contain(['ArticlesTags.Authors', 'Authors'])
  428. ->first();
  429. $resultB = $articles->find()
  430. ->contain(['Authors', 'ArticlesTags.Authors'])
  431. ->first();
  432. $this->assertEquals($resultA, $resultB);
  433. $this->assertNotEmpty($resultA->author);
  434. $this->assertNotEmpty($resultA->articles_tag->author);
  435. }
  436. /**
  437. * Test that offset/limit are elided from subquery loads.
  438. *
  439. * @return void
  440. */
  441. public function testAssociationSubQueryNoOffset()
  442. {
  443. $this->loadFixtures('Articles', 'Translates');
  444. $table = TableRegistry::get('Articles');
  445. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  446. $table->locale('eng');
  447. $query = $table->find('translations')
  448. ->order(['Articles.id' => 'ASC'])
  449. ->limit(10)
  450. ->offset(1);
  451. $result = $query->toArray();
  452. $this->assertCount(2, $result);
  453. }
  454. /**
  455. * Tests that using the subquery strategy in a deep association returns the right results
  456. *
  457. * @see https://github.com/cakephp/cakephp/issues/4484
  458. * @return void
  459. */
  460. public function testDeepBelongsToManySubqueryStrategy()
  461. {
  462. $this->loadFixtures('Authors', 'Tags', 'Articles', 'ArticlesTags');
  463. $table = TableRegistry::get('Authors');
  464. $table->hasMany('Articles');
  465. $table->Articles->belongsToMany('Tags', [
  466. 'strategy' => 'subquery'
  467. ]);
  468. $result = $table->find()->contain(['Articles.Tags'])->toArray();
  469. $this->skipIf(count($result) == 0, 'No results, this test sometimes acts up on PHP 5.6');
  470. $this->assertEquals(
  471. ['tag1', 'tag3'],
  472. collection($result[2]->articles[0]->tags)->extract('name')->toArray()
  473. );
  474. }
  475. /**
  476. * Tests that using the subquery strategy in a deep association returns the right results
  477. *
  478. * @see https://github.com/cakephp/cakephp/issues/5769
  479. * @return void
  480. */
  481. public function testDeepBelongsToManySubqueryStrategy2()
  482. {
  483. $this->loadFixtures('Articles', 'Authors', 'Tags', 'Authors', 'AuthorsTags');
  484. $table = TableRegistry::get('Authors');
  485. $table->hasMany('Articles');
  486. $table->Articles->belongsToMany('Tags', [
  487. 'strategy' => 'subquery'
  488. ]);
  489. $table->belongsToMany('Tags', [
  490. 'strategy' => 'subquery',
  491. ]);
  492. $table->Articles->belongsTo('Authors');
  493. $result = $table->Articles->find()
  494. ->where(['Authors.id >' => 1])
  495. ->contain(['Authors.Tags'])
  496. ->toArray();
  497. $this->assertEquals(
  498. ['tag1', 'tag2'],
  499. collection($result[0]->author->tags)->extract('name')->toArray()
  500. );
  501. $this->assertEquals(3, $result[0]->author->id);
  502. }
  503. /**
  504. * Tests that finding on a table with a primary key other than `id` will work
  505. * seamlessly with either select or subquery.
  506. *
  507. * @see https://github.com/cakephp/cakephp/issues/6781
  508. * @return void
  509. */
  510. public function testDeepHasManyEitherStrategy()
  511. {
  512. $this->loadFixtures('Tags', 'FeaturedTags', 'TagsTranslations');
  513. $tags = TableRegistry::get('Tags');
  514. $this->skipIf(
  515. $tags->connection()->driver() instanceof \Cake\Database\Driver\Sqlserver,
  516. 'SQL server is temporarily weird in this test, will investigate later'
  517. );
  518. $tags = TableRegistry::get('Tags');
  519. $featuredTags = TableRegistry::get('FeaturedTags');
  520. $featuredTags->belongsTo('Tags');
  521. $tags->hasMany('TagsTranslations', [
  522. 'foreignKey' => 'id',
  523. 'strategy' => 'select'
  524. ]);
  525. $findViaSelect = $featuredTags
  526. ->find()
  527. ->where(['FeaturedTags.tag_id' => 2])
  528. ->contain('Tags.TagsTranslations')
  529. ->all();
  530. $tags->hasMany('TagsTranslations', [
  531. 'foreignKey' => 'id',
  532. 'strategy' => 'subquery'
  533. ]);
  534. $findViaSubquery = $featuredTags
  535. ->find()
  536. ->where(['FeaturedTags.tag_id' => 2])
  537. ->contain('Tags.TagsTranslations')
  538. ->all();
  539. $expected = [2 => 'tag 2 translated into en_us'];
  540. $this->assertEquals($expected, $findViaSelect->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  541. $this->assertEquals($expected, $findViaSubquery->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  542. }
  543. /**
  544. * Tests that getting the count of a query having containments return
  545. * the correct results
  546. *
  547. * @see https://github.com/cakephp/cakephp/issues/4511
  548. * @return void
  549. */
  550. public function testCountWithContain()
  551. {
  552. $this->loadFixtures('Articles', 'Authors');
  553. $table = TableRegistry::get('Articles');
  554. $table->belongsTo('Authors', ['joinType' => 'inner']);
  555. $count = $table
  556. ->find()
  557. ->contain(['Authors' => function ($q) {
  558. return $q->where(['Authors.id' => 1]);
  559. }])
  560. ->count();
  561. $this->assertEquals(2, $count);
  562. }
  563. /**
  564. * Tests that getting the count of a query with bind is correct
  565. *
  566. * @see https://github.com/cakephp/cakephp/issues/8466
  567. * @return void
  568. */
  569. public function testCountWithBind()
  570. {
  571. $this->loadFixtures('Articles');
  572. $table = TableRegistry::get('Articles');
  573. $query = $table
  574. ->find()
  575. ->select(['title', 'id'])
  576. ->where("title LIKE :val")
  577. ->group(['id', 'title'])
  578. ->bind(':val', '%Second%');
  579. $count = $query->count();
  580. $this->assertEquals(1, $count);
  581. }
  582. /**
  583. * Tests that bind in subqueries works.
  584. *
  585. * @return void
  586. */
  587. public function testSubqueryBind()
  588. {
  589. $this->loadFixtures('Articles');
  590. $table = TableRegistry::get('Articles');
  591. $sub = $table->find()
  592. ->select(['id'])
  593. ->where("title LIKE :val")
  594. ->bind(':val', 'Second %');
  595. $query = $table
  596. ->find()
  597. ->select(['title'])
  598. ->where(["id NOT IN" => $sub]);
  599. $result = $query->toArray();
  600. $this->assertCount(2, $result);
  601. $this->assertEquals('First Article', $result[0]->title);
  602. $this->assertEquals('Third Article', $result[1]->title);
  603. }
  604. /**
  605. * Test that deep containments don't generate empty entities for
  606. * intermediary relations.
  607. *
  608. * @return void
  609. */
  610. public function testContainNoEmptyAssociatedObjects()
  611. {
  612. $this->loadFixtures('Comments', 'Users', 'Articles');
  613. $comments = TableRegistry::get('Comments');
  614. $comments->belongsTo('Users');
  615. $users = TableRegistry::get('Users');
  616. $users->hasMany('Articles', [
  617. 'foreignKey' => 'author_id'
  618. ]);
  619. $comments->updateAll(['user_id' => 99], ['id' => 1]);
  620. $result = $comments->find()
  621. ->contain(['Users'])
  622. ->where(['Comments.id' => 1])
  623. ->first();
  624. $this->assertNull($result->user, 'No record should be null.');
  625. $result = $comments->find()
  626. ->contain(['Users', 'Users.Articles'])
  627. ->where(['Comments.id' => 1])
  628. ->first();
  629. $this->assertNull($result->user, 'No record should be null.');
  630. }
  631. /**
  632. * Tests that using a comparison expression inside an OR condition works
  633. *
  634. * @see https://github.com/cakephp/cakephp/issues/5081
  635. * @return void
  636. */
  637. public function testOrConditionsWithExpression()
  638. {
  639. $this->loadFixtures('Articles');
  640. $table = TableRegistry::get('Articles');
  641. $query = $table->find();
  642. $query->where([
  643. 'OR' => [
  644. new \Cake\Database\Expression\Comparison('id', 1, 'integer', '>'),
  645. new \Cake\Database\Expression\Comparison('id', 3, 'integer', '<')
  646. ]
  647. ]);
  648. $results = $query->toArray();
  649. $this->assertCount(3, $results);
  650. }
  651. /**
  652. * Tests that calling count on a query having a union works correctly
  653. *
  654. * @see https://github.com/cakephp/cakephp/issues/5107
  655. * @return void
  656. */
  657. public function testCountWithUnionQuery()
  658. {
  659. $this->loadFixtures('Articles');
  660. $table = TableRegistry::get('Articles');
  661. $query = $table->find()->where(['id' => 1]);
  662. $query2 = $table->find()->where(['id' => 2]);
  663. $query->union($query2);
  664. $this->assertEquals(2, $query->count());
  665. }
  666. /**
  667. * Integration test when selecting no fields on the primary table.
  668. *
  669. * @return void
  670. */
  671. public function testSelectNoFieldsOnPrimaryAlias()
  672. {
  673. $this->loadFixtures('Articles', 'Users');
  674. $table = TableRegistry::get('Articles');
  675. $table->belongsTo('Users');
  676. $query = $table->find()
  677. ->select(['Users__id' => 'id']);
  678. $results = $query->toArray();
  679. $this->assertCount(3, $results);
  680. }
  681. /**
  682. * Tests that calling first on the query results will not remove all other results
  683. * from the set.
  684. *
  685. * @return void
  686. */
  687. public function testFirstOnResultSet()
  688. {
  689. $this->loadFixtures('Articles');
  690. $results = TableRegistry::get('Articles')->find()->all();
  691. $this->assertEquals(3, $results->count());
  692. $this->assertNotNull($results->first());
  693. $this->assertCount(3, $results->toArray());
  694. }
  695. /**
  696. * Checks that matching and contain can be called for the same belongsTo association
  697. *
  698. * @see https://github.com/cakephp/cakephp/issues/5463
  699. * @return void
  700. */
  701. public function testFindMatchingAndContain()
  702. {
  703. $this->loadFixtures('Articles', 'Authors');
  704. $table = TableRegistry::get('Articles');
  705. $table->belongsTo('Authors');
  706. $article = $table->find()
  707. ->contain('Authors')
  708. ->matching('Authors', function ($q) {
  709. return $q->where(['Authors.id' => 1]);
  710. })
  711. ->first();
  712. $this->assertNotNull($article->author);
  713. $this->assertEquals($article->author, $article->_matchingData['Authors']);
  714. }
  715. /**
  716. * Checks that matching and contain can be called for the same belongsTo association
  717. *
  718. * @see https://github.com/cakephp/cakephp/issues/5463
  719. * @return void
  720. */
  721. public function testFindMatchingAndContainWithSubquery()
  722. {
  723. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  724. $table = TableRegistry::get('authors');
  725. $table->hasMany('articles', ['strategy' => 'subquery']);
  726. $table->articles->belongsToMany('tags');
  727. $result = $table->find()
  728. ->matching('articles.tags', function ($q) {
  729. return $q->where(['tags.id' => 2]);
  730. })
  731. ->contain('articles');
  732. $this->assertCount(2, $result->first()->articles);
  733. }
  734. /**
  735. * Tests that matching does not overwrite associations in contain
  736. *
  737. * @see https://github.com/cakephp/cakephp/issues/5584
  738. * @return void
  739. */
  740. public function testFindMatchingOverwrite()
  741. {
  742. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags');
  743. $comments = TableRegistry::get('Comments');
  744. $comments->belongsTo('Articles');
  745. $articles = TableRegistry::get('Articles');
  746. $articles->belongsToMany('Tags');
  747. $result = $comments
  748. ->find()
  749. ->matching('Articles.Tags', function ($q) {
  750. return $q->where(['Tags.id' => 2]);
  751. })
  752. ->contain('Articles')
  753. ->first();
  754. $this->assertEquals(1, $result->id);
  755. $this->assertEquals(1, $result->_matchingData['Articles']->id);
  756. $this->assertEquals(2, $result->_matchingData['Tags']->id);
  757. $this->assertNotNull($result->article);
  758. $this->assertEquals($result->article, $result->_matchingData['Articles']);
  759. }
  760. /**
  761. * Tests that matching does not overwrite associations in contain
  762. *
  763. * @see https://github.com/cakephp/cakephp/issues/5584
  764. * @return void
  765. */
  766. public function testFindMatchingOverwrite2()
  767. {
  768. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags', 'Authors');
  769. $comments = TableRegistry::get('Comments');
  770. $comments->belongsTo('Articles');
  771. $articles = TableRegistry::get('Articles');
  772. $articles->belongsTo('Authors');
  773. $articles->belongsToMany('Tags');
  774. $result = $comments
  775. ->find()
  776. ->matching('Articles.Tags', function ($q) {
  777. return $q->where(['Tags.id' => 2]);
  778. })
  779. ->contain('Articles.Authors')
  780. ->first();
  781. $this->assertNotNull($result->article->author);
  782. }
  783. /**
  784. * Tests that trying to contain an inexistent association
  785. * throws an exception and not a fatal error.
  786. *
  787. * @expectedException InvalidArgumentException
  788. * @return void
  789. */
  790. public function testQueryNotFatalError()
  791. {
  792. $this->loadFixtures('Comments');
  793. $comments = TableRegistry::get('Comments');
  794. $comments->find()->contain('Deprs')->all();
  795. }
  796. /**
  797. * Tests that using matching and contain on belongsTo associations
  798. * works correctly.
  799. *
  800. * @see https://github.com/cakephp/cakephp/issues/5721
  801. * @return void
  802. */
  803. public function testFindMatchingWithContain()
  804. {
  805. $this->loadFixtures('Articles', 'Comments', 'Users');
  806. $comments = TableRegistry::get('Comments');
  807. $comments->belongsTo('Articles');
  808. $comments->belongsTo('Users');
  809. $result = $comments->find()
  810. ->contain(['Articles', 'Users'])
  811. ->matching('Articles', function ($q) {
  812. return $q->where(['Articles.id >=' => 1]);
  813. })
  814. ->matching('Users', function ($q) {
  815. return $q->where(['Users.id >=' => 1]);
  816. })
  817. ->order(['Comments.id' => 'ASC'])
  818. ->first();
  819. $this->assertInstanceOf('Cake\ORM\Entity', $result->article);
  820. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  821. $this->assertEquals(2, $result->user->id);
  822. $this->assertEquals(1, $result->article->id);
  823. }
  824. /**
  825. * Tests that HasMany associations don't use duplicate PK values.
  826. *
  827. * @return void
  828. */
  829. public function testHasManyEagerLoadingUniqueKey()
  830. {
  831. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  832. $table = TableRegistry::get('ArticlesTags');
  833. $table->belongsTo('Articles', [
  834. 'strategy' => 'select'
  835. ]);
  836. $result = $table->find()
  837. ->contain(['Articles' => function ($q) {
  838. $result = $q->sql();
  839. $this->assertNotContains(':c2', $result, 'Only 2 bindings as there are only 2 rows.');
  840. $this->assertNotContains(':c3', $result, 'Only 2 bindings as there are only 2 rows.');
  841. return $q;
  842. }])
  843. ->toArray();
  844. $this->assertNotEmpty($result[0]->article);
  845. }
  846. /**
  847. * Tests that using contain but selecting no fields from the association
  848. * does not trigger any errors and fetches the right results.
  849. *
  850. * @see https://github.com/cakephp/cakephp/issues/6214
  851. * @return void
  852. */
  853. public function testContainWithNoFields()
  854. {
  855. $this->loadFixtures('Comments', 'Users');
  856. $table = TableRegistry::get('Comments');
  857. $table->belongsTo('Users');
  858. $results = $table->find()
  859. ->select(['Comments.id', 'Comments.user_id'])
  860. ->contain(['Users'])
  861. ->where(['Users.id' => 1])
  862. ->combine('id', 'user_id');
  863. $this->assertEquals([3 => 1, 4 => 1, 5 => 1], $results->toArray());
  864. }
  865. /**
  866. * Tests that using matching and selecting no fields for that association
  867. * will no trigger any errors and fetch the right results
  868. *
  869. * @see https://github.com/cakephp/cakephp/issues/6223
  870. * @return void
  871. */
  872. public function testMatchingWithNoFields()
  873. {
  874. $this->loadFixtures('Comments', 'Users');
  875. $table = TableRegistry::get('Users');
  876. $table->hasMany('Comments');
  877. $results = $table->find()
  878. ->select(['Users.id'])
  879. ->matching('Comments', function ($q) {
  880. return $q->where(['Comments.id' => 1]);
  881. })
  882. ->extract('id')
  883. ->toList();
  884. $this->assertEquals([2], $results);
  885. }
  886. /**
  887. * Test that empty conditions in a matching clause don't cause errors.
  888. *
  889. * @return void
  890. */
  891. public function testMatchingEmptyQuery()
  892. {
  893. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  894. $table = TableRegistry::get('Articles');
  895. $table->belongsToMany('Tags');
  896. $rows = $table->find()
  897. ->matching('Tags', function ($q) {
  898. return $q->where([]);
  899. })
  900. ->all();
  901. $this->assertNotEmpty($rows);
  902. $rows = $table->find()
  903. ->matching('Tags', function ($q) {
  904. return $q->where(null);
  905. })
  906. ->all();
  907. $this->assertNotEmpty($rows);
  908. }
  909. /**
  910. * Tests that using a subquery as part of an expression will not make invalid SQL
  911. *
  912. * @return void
  913. */
  914. public function testSubqueryInSelectExpression()
  915. {
  916. $this->loadFixtures('Comments');
  917. $table = TableRegistry::get('Comments');
  918. $ratio = $table->find()
  919. ->select(function ($query) use ($table) {
  920. $allCommentsCount = $table->find()->select($query->func()->count('*'));
  921. $countToFloat = $query->newExpr([$query->func()->count('*'), '1.0'])->type('*');
  922. return [
  923. 'ratio' => $query
  924. ->newExpr($countToFloat)
  925. ->add($allCommentsCount)
  926. ->type('/')
  927. ];
  928. })
  929. ->where(['user_id' => 1])
  930. ->first()
  931. ->ratio;
  932. $this->assertEquals(0.5, $ratio);
  933. }
  934. /**
  935. * Tests calling last on an empty table
  936. *
  937. * @see https://github.com/cakephp/cakephp/issues/6683
  938. * @return void
  939. */
  940. public function testFindLastOnEmptyTable()
  941. {
  942. $this->loadFixtures('Comments');
  943. $table = TableRegistry::get('Comments');
  944. $table->deleteAll(['1 = 1']);
  945. $this->assertEquals(0, $table->find()->count());
  946. $this->assertNull($table->find()->last());
  947. }
  948. /**
  949. * Tests calling contain in a nested closure
  950. *
  951. * @see https://github.com/cakephp/cakephp/issues/7591
  952. * @return void
  953. */
  954. public function testContainInNestedClosure()
  955. {
  956. $this->loadFixtures('Comments', 'Articles', 'Authors', 'Tags', 'AuthorsTags');
  957. $table = TableRegistry::get('Comments');
  958. $table->belongsTo('Articles');
  959. $table->Articles->belongsTo('Authors');
  960. $table->Articles->Authors->belongsToMany('Tags');
  961. $query = $table->find()->where(['Comments.id' => 5])->contain(['Articles' => function ($q) {
  962. return $q->contain(['Authors' => function ($q) {
  963. return $q->contain('Tags');
  964. }]);
  965. }]);
  966. $this->assertCount(2, $query->first()->article->author->tags);
  967. }
  968. /**
  969. * Test that the typemaps used in function expressions
  970. * create the correct results.
  971. *
  972. * @return void
  973. */
  974. public function testTypemapInFunctions()
  975. {
  976. $this->loadFixtures('Comments');
  977. $table = TableRegistry::get('Comments');
  978. $table->updateAll(['published' => null], ['1 = 1']);
  979. $query = $table->find();
  980. $query->select([
  981. 'id',
  982. 'coalesced' => $query->func()->coalesce(
  983. ['published' => 'identifier', -1],
  984. ['integer']
  985. )
  986. ]);
  987. $result = $query->all()->first();
  988. $this->assertSame(
  989. -1,
  990. $result['coalesced'],
  991. 'Output values for functions should be casted'
  992. );
  993. }
  994. /**
  995. * Test that the typemaps used in function expressions
  996. * create the correct results.
  997. *
  998. * @return void
  999. */
  1000. public function testTypemapInFunctions2()
  1001. {
  1002. $this->loadFixtures('Comments');
  1003. $table = TableRegistry::get('Comments');
  1004. $query = $table->find();
  1005. $query->select([
  1006. 'max' => $query->func()->max('created', ['datetime'])
  1007. ]);
  1008. $result = $query->all()->first();
  1009. $this->assertEquals(new Time('2007-03-18 10:55:23'), $result['max']);
  1010. }
  1011. /**
  1012. * Test that contain queries map types correctly.
  1013. *
  1014. * @return void
  1015. */
  1016. public function testBooleanConditionsInContain()
  1017. {
  1018. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  1019. $table = TableRegistry::get('Articles');
  1020. $table->belongsToMany('Tags', [
  1021. 'foreignKey' => 'article_id',
  1022. 'associationForeignKey' => 'tag_id',
  1023. 'through' => 'SpecialTags'
  1024. ]);
  1025. $query = $table->find()
  1026. ->contain(['Tags' => function ($q) {
  1027. return $q->where(['SpecialTags.highlighted_time >' => new Time('2014-06-01 00:00:00')]);
  1028. }])
  1029. ->where(['Articles.id' => 2]);
  1030. $result = $query->first();
  1031. $this->assertEquals(2, $result->id);
  1032. $this->assertNotEmpty($result->tags, 'Missing tags');
  1033. $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
  1034. }
  1035. /**
  1036. * Test that contain queries map types correctly.
  1037. *
  1038. * @return void
  1039. */
  1040. public function testComplexTypesInJoinedWhere()
  1041. {
  1042. $this->loadFixtures('Comments', 'Users');
  1043. $table = TableRegistry::get('Users');
  1044. $table->hasOne('Comments', [
  1045. 'foreignKey' => 'user_id',
  1046. ]);
  1047. $query = $table->find()
  1048. ->contain('Comments')
  1049. ->where([
  1050. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00')
  1051. ]);
  1052. $result = $query->first();
  1053. $this->assertNotEmpty($result);
  1054. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->updated);
  1055. }
  1056. /**
  1057. * Test that nested contain queries map types correctly.
  1058. *
  1059. * @return void
  1060. */
  1061. public function testComplexNestedTypesInJoinedWhere()
  1062. {
  1063. $this->loadFixtures('Comments', 'Users', 'Articles');
  1064. $table = TableRegistry::get('Users');
  1065. $table->hasOne('Comments', [
  1066. 'foreignKey' => 'user_id',
  1067. ]);
  1068. $table->Comments->belongsTo('Articles');
  1069. $table->Comments->Articles->belongsTo('Authors', [
  1070. 'className' => 'Users',
  1071. 'foreignKey' => 'author_id'
  1072. ]);
  1073. $query = $table->find()
  1074. ->contain('Comments.Articles.Authors')
  1075. ->where([
  1076. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00')
  1077. ]);
  1078. $result = $query->first();
  1079. $this->assertNotEmpty($result);
  1080. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->article->author->updated);
  1081. }
  1082. /**
  1083. * Tests that it is possible to use matching with dot notation
  1084. * even when part of the part of the path in the dot notation is
  1085. * shared for two different calls
  1086. *
  1087. * @return void
  1088. */
  1089. public function testDotNotationNotOverride()
  1090. {
  1091. $this->loadFixtures('Comments', 'Articles', 'Tags', 'Authors', 'SpecialTags');
  1092. $table = TableRegistry::get('Comments');
  1093. $articles = $table->belongsTo('Articles');
  1094. $specialTags = $articles->hasMany('SpecialTags');
  1095. $specialTags->belongsTo('Authors');
  1096. $specialTags->belongsTo('Tags');
  1097. $results = $table
  1098. ->find()
  1099. ->select(['name' => 'Authors.name', 'tag' => 'Tags.name'])
  1100. ->matching('Articles.SpecialTags.Tags')
  1101. ->matching('Articles.SpecialTags.Authors', function ($q) {
  1102. return $q->where(['Authors.id' => 2]);
  1103. })
  1104. ->distinct()
  1105. ->hydrate(false)
  1106. ->toArray();
  1107. $this->assertEquals([['name' => 'nate', 'tag' => 'tag1']], $results);
  1108. }
  1109. /**
  1110. * Test expression based ordering with unions.
  1111. *
  1112. * @return void
  1113. */
  1114. public function testComplexOrderWithUnion()
  1115. {
  1116. $this->loadFixtures('Comments');
  1117. $table = TableRegistry::get('Comments');
  1118. $query = $table->find();
  1119. $inner = $table->find()
  1120. ->select(['content' => 'comment'])
  1121. ->where(['id >' => 3]);
  1122. $inner2 = $table->find()
  1123. ->select(['content' => 'comment'])
  1124. ->where(['id <' => 3]);
  1125. $order = $query->func()->concat(['content' => 'literal', 'test']);
  1126. $query->select(['inside.content'])
  1127. ->from(['inside' => $inner->unionAll($inner2)])
  1128. ->orderAsc($order);
  1129. $results = $query->toArray();
  1130. $this->assertCount(5, $results);
  1131. }
  1132. /**
  1133. * Test that associations that are loaded with subqueries
  1134. * do not cause errors when the subquery has a limit & order clause.
  1135. *
  1136. * @return void
  1137. */
  1138. public function testEagerLoadOrderAndSubquery()
  1139. {
  1140. $this->loadFixtures('Articles', 'Comments');
  1141. $table = TableRegistry::get('Articles');
  1142. $table->hasMany('Comments', [
  1143. 'strategy' => 'subquery'
  1144. ]);
  1145. $query = $table->find()
  1146. ->select(['score' => 100])
  1147. ->autoFields(true)
  1148. ->contain(['Comments'])
  1149. ->limit(5)
  1150. ->order(['score' => 'desc']);
  1151. $result = $query->all();
  1152. $this->assertCount(3, $result);
  1153. }
  1154. /**
  1155. * Tests that decorating the results does not result in a memory leak
  1156. *
  1157. * @return void
  1158. */
  1159. public function testFormatResultsMemoryLeak()
  1160. {
  1161. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  1162. $this->skipIf(env('CODECOVERAGE') == 1, 'Running coverage this causes this tests to fail sometimes.');
  1163. $table = TableRegistry::get('Articles');
  1164. $table->belongsTo('Authors');
  1165. $table->belongsToMany('Tags');
  1166. gc_collect_cycles();
  1167. $memory = memory_get_usage() / 1024 / 1024;
  1168. foreach (range(1, 3) as $time) {
  1169. $table->find()
  1170. ->contain(['Authors', 'Tags'])
  1171. ->formatResults(function ($results) {
  1172. return $results;
  1173. })
  1174. ->all();
  1175. }
  1176. gc_collect_cycles();
  1177. $endMemory = memory_get_usage() / 1024 / 1024;
  1178. $this->assertWithinRange($endMemory, $memory, 1.25, 'Memory leak in ResultSet');
  1179. }
  1180. /**
  1181. * Tests that having bound placeholders in the order clause does not result
  1182. * in an error when trying to count a query.
  1183. *
  1184. * @return void
  1185. */
  1186. public function testCountWithComplexOrderBy()
  1187. {
  1188. $this->loadFixtures('Articles');
  1189. $table = TableRegistry::get('Articles');
  1190. $query = $table->find();
  1191. $query->orderDesc($query->newExpr()->addCase(
  1192. [$query->newExpr()->add(['id' => 3])],
  1193. [1, 0]
  1194. ));
  1195. $query->order(['title' => 'desc']);
  1196. // Executing the normal query before getting the count
  1197. $query->all();
  1198. $this->assertEquals(3, $query->count());
  1199. $table = TableRegistry::get('Articles');
  1200. $query = $table->find();
  1201. $query->orderDesc($query->newExpr()->addCase(
  1202. [$query->newExpr()->add(['id' => 3])],
  1203. [1, 0]
  1204. ));
  1205. $query->orderDesc($query->newExpr()->add(['id' => 3]));
  1206. // Not executing the query first, just getting the count
  1207. $this->assertEquals(3, $query->count());
  1208. }
  1209. /**
  1210. * Tests that the now() function expression can be used in the
  1211. * where clause of a query
  1212. *
  1213. * @see https://github.com/cakephp/cakephp/issues/7943
  1214. * @return void
  1215. */
  1216. public function testFunctionInWhereClause()
  1217. {
  1218. $this->loadFixtures('Comments');
  1219. $table = TableRegistry::get('Comments');
  1220. $table->updateAll(['updated' => Time::tomorrow()], ['id' => 6]);
  1221. $query = $table->find();
  1222. $result = $query->where(['updated >' => $query->func()->now('datetime')])->first();
  1223. $this->assertSame(6, $result->id);
  1224. }
  1225. }