QueryRegressionTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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->assertEquals(
  470. ['tag1', 'tag3'],
  471. collection($result[2]->articles[0]->tags)->extract('name')->toArray()
  472. );
  473. }
  474. /**
  475. * Tests that using the subquery strategy in a deep association returns the right results
  476. *
  477. * @see https://github.com/cakephp/cakephp/issues/5769
  478. * @return void
  479. */
  480. public function testDeepBelongsToManySubqueryStrategy2()
  481. {
  482. $this->loadFixtures('Articles', 'Authors', 'Tags', 'Authors', 'AuthorsTags');
  483. $table = TableRegistry::get('Authors');
  484. $table->hasMany('Articles');
  485. $table->Articles->belongsToMany('Tags', [
  486. 'strategy' => 'subquery'
  487. ]);
  488. $table->belongsToMany('Tags', [
  489. 'strategy' => 'subquery',
  490. ]);
  491. $table->Articles->belongsTo('Authors');
  492. $result = $table->Articles->find()
  493. ->where(['Authors.id >' => 1])
  494. ->contain(['Authors.Tags'])
  495. ->toArray();
  496. $this->assertEquals(
  497. ['tag1', 'tag2'],
  498. collection($result[0]->author->tags)->extract('name')->toArray()
  499. );
  500. $this->assertEquals(3, $result[0]->author->id);
  501. }
  502. /**
  503. * Tests that finding on a table with a primary key other than `id` will work
  504. * seamlessly with either select or subquery.
  505. *
  506. * @see https://github.com/cakephp/cakephp/issues/6781
  507. * @return void
  508. */
  509. public function testDeepHasManyEitherStrategy()
  510. {
  511. $this->loadFixtures('Tags', 'FeaturedTags', 'TagsTranslations');
  512. $tags = TableRegistry::get('Tags');
  513. $this->skipIf(
  514. $tags->connection()->driver() instanceof \Cake\Database\Driver\Sqlserver,
  515. 'SQL server is temporarily weird in this test, will investigate later'
  516. );
  517. $tags = TableRegistry::get('Tags');
  518. $featuredTags = TableRegistry::get('FeaturedTags');
  519. $featuredTags->belongsTo('Tags');
  520. $tags->hasMany('TagsTranslations', [
  521. 'foreignKey' => 'id',
  522. 'strategy' => 'select'
  523. ]);
  524. $findViaSelect = $featuredTags
  525. ->find()
  526. ->where(['FeaturedTags.tag_id' => 2])
  527. ->contain('Tags.TagsTranslations')
  528. ->all();
  529. $tags->hasMany('TagsTranslations', [
  530. 'foreignKey' => 'id',
  531. 'strategy' => 'subquery'
  532. ]);
  533. $findViaSubquery = $featuredTags
  534. ->find()
  535. ->where(['FeaturedTags.tag_id' => 2])
  536. ->contain('Tags.TagsTranslations')
  537. ->all();
  538. $expected = [2 => 'tag 2 translated into en_us'];
  539. $this->assertEquals($expected, $findViaSelect->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  540. $this->assertEquals($expected, $findViaSubquery->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  541. }
  542. /**
  543. * Tests that getting the count of a query having containments return
  544. * the correct results
  545. *
  546. * @see https://github.com/cakephp/cakephp/issues/4511
  547. * @return void
  548. */
  549. public function testCountWithContain()
  550. {
  551. $this->loadFixtures('Articles', 'Authors');
  552. $table = TableRegistry::get('Articles');
  553. $table->belongsTo('Authors', ['joinType' => 'inner']);
  554. $count = $table
  555. ->find()
  556. ->contain(['Authors' => function ($q) {
  557. return $q->where(['Authors.id' => 1]);
  558. }])
  559. ->count();
  560. $this->assertEquals(2, $count);
  561. }
  562. /**
  563. * Tests that getting the count of a query with bind is correct
  564. *
  565. * @see https://github.com/cakephp/cakephp/issues/8466
  566. * @return void
  567. */
  568. public function testCountWithBind()
  569. {
  570. $this->loadFixtures('Articles');
  571. $table = TableRegistry::get('Articles');
  572. $query = $table
  573. ->find()
  574. ->select(['title', 'id'])
  575. ->where("title LIKE :val")
  576. ->group(['id', 'title'])
  577. ->bind(':val', '%Second%');
  578. $count = $query->count();
  579. $this->assertEquals(1, $count);
  580. }
  581. /**
  582. * Tests that bind in subqueries works.
  583. *
  584. * @return void
  585. */
  586. public function testSubqueryBind()
  587. {
  588. $this->loadFixtures('Articles');
  589. $table = TableRegistry::get('Articles');
  590. $sub = $table->find()
  591. ->select(['id'])
  592. ->where("title LIKE :val")
  593. ->bind(':val', 'Second %');
  594. $query = $table
  595. ->find()
  596. ->select(['title'])
  597. ->where(["id NOT IN" => $sub]);
  598. $result = $query->toArray();
  599. $this->assertCount(2, $result);
  600. $this->assertEquals('First Article', $result[0]->title);
  601. $this->assertEquals('Third Article', $result[1]->title);
  602. }
  603. /**
  604. * Test that deep containments don't generate empty entities for
  605. * intermediary relations.
  606. *
  607. * @return void
  608. */
  609. public function testContainNoEmptyAssociatedObjects()
  610. {
  611. $this->loadFixtures('Comments', 'Users', 'Articles');
  612. $comments = TableRegistry::get('Comments');
  613. $comments->belongsTo('Users');
  614. $users = TableRegistry::get('Users');
  615. $users->hasMany('Articles', [
  616. 'foreignKey' => 'author_id'
  617. ]);
  618. $comments->updateAll(['user_id' => 99], ['id' => 1]);
  619. $result = $comments->find()
  620. ->contain(['Users'])
  621. ->where(['Comments.id' => 1])
  622. ->first();
  623. $this->assertNull($result->user, 'No record should be null.');
  624. $result = $comments->find()
  625. ->contain(['Users', 'Users.Articles'])
  626. ->where(['Comments.id' => 1])
  627. ->first();
  628. $this->assertNull($result->user, 'No record should be null.');
  629. }
  630. /**
  631. * Tests that using a comparison expression inside an OR condition works
  632. *
  633. * @see https://github.com/cakephp/cakephp/issues/5081
  634. * @return void
  635. */
  636. public function testOrConditionsWithExpression()
  637. {
  638. $this->loadFixtures('Articles');
  639. $table = TableRegistry::get('Articles');
  640. $query = $table->find();
  641. $query->where([
  642. 'OR' => [
  643. new \Cake\Database\Expression\Comparison('id', 1, 'integer', '>'),
  644. new \Cake\Database\Expression\Comparison('id', 3, 'integer', '<')
  645. ]
  646. ]);
  647. $results = $query->toArray();
  648. $this->assertCount(3, $results);
  649. }
  650. /**
  651. * Tests that calling count on a query having a union works correctly
  652. *
  653. * @see https://github.com/cakephp/cakephp/issues/5107
  654. * @return void
  655. */
  656. public function testCountWithUnionQuery()
  657. {
  658. $this->loadFixtures('Articles');
  659. $table = TableRegistry::get('Articles');
  660. $query = $table->find()->where(['id' => 1]);
  661. $query2 = $table->find()->where(['id' => 2]);
  662. $query->union($query2);
  663. $this->assertEquals(2, $query->count());
  664. }
  665. /**
  666. * Integration test when selecting no fields on the primary table.
  667. *
  668. * @return void
  669. */
  670. public function testSelectNoFieldsOnPrimaryAlias()
  671. {
  672. $this->loadFixtures('Articles', 'Users');
  673. $table = TableRegistry::get('Articles');
  674. $table->belongsTo('Users');
  675. $query = $table->find()
  676. ->select(['Users__id' => 'id']);
  677. $results = $query->toArray();
  678. $this->assertCount(3, $results);
  679. }
  680. /**
  681. * Tests that calling first on the query results will not remove all other results
  682. * from the set.
  683. *
  684. * @return void
  685. */
  686. public function testFirstOnResultSet()
  687. {
  688. $this->loadFixtures('Articles');
  689. $results = TableRegistry::get('Articles')->find()->all();
  690. $this->assertEquals(3, $results->count());
  691. $this->assertNotNull($results->first());
  692. $this->assertCount(3, $results->toArray());
  693. }
  694. /**
  695. * Checks that matching and contain can be called for the same belongsTo association
  696. *
  697. * @see https://github.com/cakephp/cakephp/issues/5463
  698. * @return void
  699. */
  700. public function testFindMatchingAndContain()
  701. {
  702. $this->loadFixtures('Articles', 'Authors');
  703. $table = TableRegistry::get('Articles');
  704. $table->belongsTo('Authors');
  705. $article = $table->find()
  706. ->contain('Authors')
  707. ->matching('Authors', function ($q) {
  708. return $q->where(['Authors.id' => 1]);
  709. })
  710. ->first();
  711. $this->assertNotNull($article->author);
  712. $this->assertEquals($article->author, $article->_matchingData['Authors']);
  713. }
  714. /**
  715. * Checks that matching and contain can be called for the same belongsTo association
  716. *
  717. * @see https://github.com/cakephp/cakephp/issues/5463
  718. * @return void
  719. */
  720. public function testFindMatchingAndContainWithSubquery()
  721. {
  722. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  723. $table = TableRegistry::get('authors');
  724. $table->hasMany('articles', ['strategy' => 'subquery']);
  725. $table->articles->belongsToMany('tags');
  726. $result = $table->find()
  727. ->matching('articles.tags', function ($q) {
  728. return $q->where(['tags.id' => 2]);
  729. })
  730. ->contain('articles');
  731. $this->assertCount(2, $result->first()->articles);
  732. }
  733. /**
  734. * Tests that matching does not overwrite associations in contain
  735. *
  736. * @see https://github.com/cakephp/cakephp/issues/5584
  737. * @return void
  738. */
  739. public function testFindMatchingOverwrite()
  740. {
  741. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags');
  742. $comments = TableRegistry::get('Comments');
  743. $comments->belongsTo('Articles');
  744. $articles = TableRegistry::get('Articles');
  745. $articles->belongsToMany('Tags');
  746. $result = $comments
  747. ->find()
  748. ->matching('Articles.Tags', function ($q) {
  749. return $q->where(['Tags.id' => 2]);
  750. })
  751. ->contain('Articles')
  752. ->first();
  753. $this->assertEquals(1, $result->id);
  754. $this->assertEquals(1, $result->_matchingData['Articles']->id);
  755. $this->assertEquals(2, $result->_matchingData['Tags']->id);
  756. $this->assertNotNull($result->article);
  757. $this->assertEquals($result->article, $result->_matchingData['Articles']);
  758. }
  759. /**
  760. * Tests that matching does not overwrite associations in contain
  761. *
  762. * @see https://github.com/cakephp/cakephp/issues/5584
  763. * @return void
  764. */
  765. public function testFindMatchingOverwrite2()
  766. {
  767. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags', 'Authors');
  768. $comments = TableRegistry::get('Comments');
  769. $comments->belongsTo('Articles');
  770. $articles = TableRegistry::get('Articles');
  771. $articles->belongsTo('Authors');
  772. $articles->belongsToMany('Tags');
  773. $result = $comments
  774. ->find()
  775. ->matching('Articles.Tags', function ($q) {
  776. return $q->where(['Tags.id' => 2]);
  777. })
  778. ->contain('Articles.Authors')
  779. ->first();
  780. $this->assertNotNull($result->article->author);
  781. }
  782. /**
  783. * Tests that trying to contain an inexistent association
  784. * throws an exception and not a fatal error.
  785. *
  786. * @expectedException InvalidArgumentException
  787. * @return void
  788. */
  789. public function testQueryNotFatalError()
  790. {
  791. $this->loadFixtures('Comments');
  792. $comments = TableRegistry::get('Comments');
  793. $comments->find()->contain('Deprs')->all();
  794. }
  795. /**
  796. * Tests that using matching and contain on belongsTo associations
  797. * works correctly.
  798. *
  799. * @see https://github.com/cakephp/cakephp/issues/5721
  800. * @return void
  801. */
  802. public function testFindMatchingWithContain()
  803. {
  804. $this->loadFixtures('Articles', 'Comments', 'Users');
  805. $comments = TableRegistry::get('Comments');
  806. $comments->belongsTo('Articles');
  807. $comments->belongsTo('Users');
  808. $result = $comments->find()
  809. ->contain(['Articles', 'Users'])
  810. ->matching('Articles', function ($q) {
  811. return $q->where(['Articles.id >=' => 1]);
  812. })
  813. ->matching('Users', function ($q) {
  814. return $q->where(['Users.id >=' => 1]);
  815. })
  816. ->order(['Comments.id' => 'ASC'])
  817. ->first();
  818. $this->assertInstanceOf('Cake\ORM\Entity', $result->article);
  819. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  820. $this->assertEquals(2, $result->user->id);
  821. $this->assertEquals(1, $result->article->id);
  822. }
  823. /**
  824. * Tests that HasMany associations don't use duplicate PK values.
  825. *
  826. * @return void
  827. */
  828. public function testHasManyEagerLoadingUniqueKey()
  829. {
  830. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  831. $table = TableRegistry::get('ArticlesTags');
  832. $table->belongsTo('Articles', [
  833. 'strategy' => 'select'
  834. ]);
  835. $result = $table->find()
  836. ->contain(['Articles' => function ($q) {
  837. $result = $q->sql();
  838. $this->assertNotContains(':c2', $result, 'Only 2 bindings as there are only 2 rows.');
  839. $this->assertNotContains(':c3', $result, 'Only 2 bindings as there are only 2 rows.');
  840. return $q;
  841. }])
  842. ->toArray();
  843. $this->assertNotEmpty($result[0]->article);
  844. }
  845. /**
  846. * Tests that using contain but selecting no fields from the association
  847. * does not trigger any errors and fetches the right results.
  848. *
  849. * @see https://github.com/cakephp/cakephp/issues/6214
  850. * @return void
  851. */
  852. public function testContainWithNoFields()
  853. {
  854. $this->loadFixtures('Comments', 'Users');
  855. $table = TableRegistry::get('Comments');
  856. $table->belongsTo('Users');
  857. $results = $table->find()
  858. ->select(['Comments.id', 'Comments.user_id'])
  859. ->contain(['Users'])
  860. ->where(['Users.id' => 1])
  861. ->combine('id', 'user_id');
  862. $this->assertEquals([3 => 1, 4 => 1, 5 => 1], $results->toArray());
  863. }
  864. /**
  865. * Tests that using matching and selecting no fields for that association
  866. * will no trigger any errors and fetch the right results
  867. *
  868. * @see https://github.com/cakephp/cakephp/issues/6223
  869. * @return void
  870. */
  871. public function testMatchingWithNoFields()
  872. {
  873. $this->loadFixtures('Comments', 'Users');
  874. $table = TableRegistry::get('Users');
  875. $table->hasMany('Comments');
  876. $results = $table->find()
  877. ->select(['Users.id'])
  878. ->matching('Comments', function ($q) {
  879. return $q->where(['Comments.id' => 1]);
  880. })
  881. ->extract('id')
  882. ->toList();
  883. $this->assertEquals([2], $results);
  884. }
  885. /**
  886. * Test that empty conditions in a matching clause don't cause errors.
  887. *
  888. * @return void
  889. */
  890. public function testMatchingEmptyQuery()
  891. {
  892. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  893. $table = TableRegistry::get('Articles');
  894. $table->belongsToMany('Tags');
  895. $rows = $table->find()
  896. ->matching('Tags', function ($q) {
  897. return $q->where([]);
  898. })
  899. ->all();
  900. $this->assertNotEmpty($rows);
  901. $rows = $table->find()
  902. ->matching('Tags', function ($q) {
  903. return $q->where(null);
  904. })
  905. ->all();
  906. $this->assertNotEmpty($rows);
  907. }
  908. /**
  909. * Tests that using a subquery as part of an expression will not make invalid SQL
  910. *
  911. * @return void
  912. */
  913. public function testSubqueryInSelectExpression()
  914. {
  915. $this->loadFixtures('Comments');
  916. $table = TableRegistry::get('Comments');
  917. $ratio = $table->find()
  918. ->select(function ($query) use ($table) {
  919. $allCommentsCount = $table->find()->select($query->func()->count('*'));
  920. $countToFloat = $query->newExpr([$query->func()->count('*'), '1.0'])->type('*');
  921. return [
  922. 'ratio' => $query
  923. ->newExpr($countToFloat)
  924. ->add($allCommentsCount)
  925. ->type('/')
  926. ];
  927. })
  928. ->where(['user_id' => 1])
  929. ->first()
  930. ->ratio;
  931. $this->assertEquals(0.5, $ratio);
  932. }
  933. /**
  934. * Tests calling last on an empty table
  935. *
  936. * @see https://github.com/cakephp/cakephp/issues/6683
  937. * @return void
  938. */
  939. public function testFindLastOnEmptyTable()
  940. {
  941. $this->loadFixtures('Comments');
  942. $table = TableRegistry::get('Comments');
  943. $table->deleteAll(['1 = 1']);
  944. $this->assertEquals(0, $table->find()->count());
  945. $this->assertNull($table->find()->last());
  946. }
  947. /**
  948. * Tests calling contain in a nested closure
  949. *
  950. * @see https://github.com/cakephp/cakephp/issues/7591
  951. * @return void
  952. */
  953. public function testContainInNestedClosure()
  954. {
  955. $this->loadFixtures('Comments', 'Articles', 'Authors', 'Tags', 'AuthorsTags');
  956. $table = TableRegistry::get('Comments');
  957. $table->belongsTo('Articles');
  958. $table->Articles->belongsTo('Authors');
  959. $table->Articles->Authors->belongsToMany('Tags');
  960. $query = $table->find()->where(['Comments.id' => 5])->contain(['Articles' => function ($q) {
  961. return $q->contain(['Authors' => function ($q) {
  962. return $q->contain('Tags');
  963. }]);
  964. }]);
  965. $this->assertCount(2, $query->first()->article->author->tags);
  966. }
  967. /**
  968. * Test that the typemaps used in function expressions
  969. * create the correct results.
  970. *
  971. * @return void
  972. */
  973. public function testTypemapInFunctions()
  974. {
  975. $this->loadFixtures('Comments');
  976. $table = TableRegistry::get('Comments');
  977. $table->updateAll(['published' => null], ['1 = 1']);
  978. $query = $table->find();
  979. $query->select([
  980. 'id',
  981. 'coalesced' => $query->func()->coalesce(
  982. ['published' => 'identifier', -1],
  983. ['integer']
  984. )
  985. ]);
  986. $result = $query->all()->first();
  987. $this->assertSame(
  988. -1,
  989. $result['coalesced'],
  990. 'Output values for functions should be casted'
  991. );
  992. }
  993. /**
  994. * Test that the typemaps used in function expressions
  995. * create the correct results.
  996. *
  997. * @return void
  998. */
  999. public function testTypemapInFunctions2()
  1000. {
  1001. $this->loadFixtures('Comments');
  1002. $table = TableRegistry::get('Comments');
  1003. $query = $table->find();
  1004. $query->select([
  1005. 'max' => $query->func()->max('created', ['datetime'])
  1006. ]);
  1007. $result = $query->all()->first();
  1008. $this->assertEquals(new Time('2007-03-18 10:55:23'), $result['max']);
  1009. }
  1010. /**
  1011. * Test that contain queries map types correctly.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testBooleanConditionsInContain()
  1016. {
  1017. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  1018. $table = TableRegistry::get('Articles');
  1019. $table->belongsToMany('Tags', [
  1020. 'foreignKey' => 'article_id',
  1021. 'associationForeignKey' => 'tag_id',
  1022. 'through' => 'SpecialTags'
  1023. ]);
  1024. $query = $table->find()
  1025. ->contain(['Tags' => function ($q) {
  1026. return $q->where(['SpecialTags.highlighted_time >' => new Time('2014-06-01 00:00:00')]);
  1027. }])
  1028. ->where(['Articles.id' => 2]);
  1029. $result = $query->first();
  1030. $this->assertEquals(2, $result->id);
  1031. $this->assertNotEmpty($result->tags, 'Missing tags');
  1032. $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
  1033. }
  1034. /**
  1035. * Test that contain queries map types correctly.
  1036. *
  1037. * @return void
  1038. */
  1039. public function testComplexTypesInJoinedWhere()
  1040. {
  1041. $this->loadFixtures('Comments', 'Users');
  1042. $table = TableRegistry::get('Users');
  1043. $table->hasOne('Comments', [
  1044. 'foreignKey' => 'user_id',
  1045. ]);
  1046. $query = $table->find()
  1047. ->contain('Comments')
  1048. ->where([
  1049. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00')
  1050. ]);
  1051. $result = $query->first();
  1052. $this->assertNotEmpty($result);
  1053. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->updated);
  1054. }
  1055. /**
  1056. * Test that nested contain queries map types correctly.
  1057. *
  1058. * @return void
  1059. */
  1060. public function testComplexNestedTypesInJoinedWhere()
  1061. {
  1062. $this->loadFixtures('Comments', 'Users', 'Articles');
  1063. $table = TableRegistry::get('Users');
  1064. $table->hasOne('Comments', [
  1065. 'foreignKey' => 'user_id',
  1066. ]);
  1067. $table->Comments->belongsTo('Articles');
  1068. $table->Comments->Articles->belongsTo('Authors', [
  1069. 'className' => 'Users',
  1070. 'foreignKey' => 'author_id'
  1071. ]);
  1072. $query = $table->find()
  1073. ->contain('Comments.Articles.Authors')
  1074. ->where([
  1075. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00')
  1076. ]);
  1077. $result = $query->first();
  1078. $this->assertNotEmpty($result);
  1079. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->article->author->updated);
  1080. }
  1081. /**
  1082. * Tests that it is possible to use matching with dot notation
  1083. * even when part of the part of the path in the dot notation is
  1084. * shared for two different calls
  1085. *
  1086. * @return void
  1087. */
  1088. public function testDotNotationNotOverride()
  1089. {
  1090. $this->loadFixtures('Comments', 'Articles', 'Tags', 'Authors', 'SpecialTags');
  1091. $table = TableRegistry::get('Comments');
  1092. $articles = $table->belongsTo('Articles');
  1093. $specialTags = $articles->hasMany('SpecialTags');
  1094. $specialTags->belongsTo('Authors');
  1095. $specialTags->belongsTo('Tags');
  1096. $results = $table
  1097. ->find()
  1098. ->select(['name' => 'Authors.name', 'tag' => 'Tags.name'])
  1099. ->matching('Articles.SpecialTags.Tags')
  1100. ->matching('Articles.SpecialTags.Authors', function ($q) {
  1101. return $q->where(['Authors.id' => 2]);
  1102. })
  1103. ->distinct()
  1104. ->hydrate(false)
  1105. ->toArray();
  1106. $this->assertEquals([['name' => 'nate', 'tag' => 'tag1']], $results);
  1107. }
  1108. /**
  1109. * Test expression based ordering with unions.
  1110. *
  1111. * @return void
  1112. */
  1113. public function testComplexOrderWithUnion()
  1114. {
  1115. $this->loadFixtures('Comments');
  1116. $table = TableRegistry::get('Comments');
  1117. $query = $table->find();
  1118. $inner = $table->find()
  1119. ->select(['content' => 'comment'])
  1120. ->where(['id >' => 3]);
  1121. $inner2 = $table->find()
  1122. ->select(['content' => 'comment'])
  1123. ->where(['id <' => 3]);
  1124. $order = $query->func()->concat(['content' => 'literal', 'test']);
  1125. $query->select(['inside.content'])
  1126. ->from(['inside' => $inner->unionAll($inner2)])
  1127. ->orderAsc($order);
  1128. $results = $query->toArray();
  1129. $this->assertCount(5, $results);
  1130. }
  1131. /**
  1132. * Test that associations that are loaded with subqueries
  1133. * do not cause errors when the subquery has a limit & order clause.
  1134. *
  1135. * @return void
  1136. */
  1137. public function testEagerLoadOrderAndSubquery()
  1138. {
  1139. $this->loadFixtures('Articles', 'Comments');
  1140. $table = TableRegistry::get('Articles');
  1141. $table->hasMany('Comments', [
  1142. 'strategy' => 'subquery'
  1143. ]);
  1144. $query = $table->find()
  1145. ->select(['score' => 100])
  1146. ->autoFields(true)
  1147. ->contain(['Comments'])
  1148. ->limit(5)
  1149. ->order(['score' => 'desc']);
  1150. $result = $query->all();
  1151. $this->assertCount(3, $result);
  1152. }
  1153. /**
  1154. * Tests that decorating the results does not result in a memory leak
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFormatResultsMemoryLeak()
  1159. {
  1160. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  1161. $this->skipIf(env('CODECOVERAGE') == 1, 'Running coverage this causes this tests to fail sometimes.');
  1162. $table = TableRegistry::get('Articles');
  1163. $table->belongsTo('Authors');
  1164. $table->belongsToMany('Tags');
  1165. gc_collect_cycles();
  1166. $memory = memory_get_usage() / 1024 / 1024;
  1167. foreach (range(1, 3) as $time) {
  1168. $table->find()
  1169. ->contain(['Authors', 'Tags'])
  1170. ->formatResults(function ($results) {
  1171. return $results;
  1172. })
  1173. ->all();
  1174. }
  1175. gc_collect_cycles();
  1176. $endMemory = memory_get_usage() / 1024 / 1024;
  1177. $this->assertWithinRange($endMemory, $memory, 1.25, 'Memory leak in ResultSet');
  1178. }
  1179. /**
  1180. * Tests that having bound placeholders in the order clause does not result
  1181. * in an error when trying to count a query.
  1182. *
  1183. * @return void
  1184. */
  1185. public function testCountWithComplexOrderBy()
  1186. {
  1187. $this->loadFixtures('Articles');
  1188. $table = TableRegistry::get('Articles');
  1189. $query = $table->find();
  1190. $query->orderDesc($query->newExpr()->addCase(
  1191. [$query->newExpr()->add(['id' => 3])],
  1192. [1, 0]
  1193. ));
  1194. $query->order(['title' => 'desc']);
  1195. // Executing the normal query before getting the count
  1196. $query->all();
  1197. $this->assertEquals(3, $query->count());
  1198. $table = TableRegistry::get('Articles');
  1199. $query = $table->find();
  1200. $query->orderDesc($query->newExpr()->addCase(
  1201. [$query->newExpr()->add(['id' => 3])],
  1202. [1, 0]
  1203. ));
  1204. $query->orderDesc($query->newExpr()->add(['id' => 3]));
  1205. // Not executing the query first, just getting the count
  1206. $this->assertEquals(3, $query->count());
  1207. }
  1208. /**
  1209. * Tests that the now() function expression can be used in the
  1210. * where clause of a query
  1211. *
  1212. * @see https://github.com/cakephp/cakephp/issues/7943
  1213. * @return void
  1214. */
  1215. public function testFunctionInWhereClause()
  1216. {
  1217. $this->loadFixtures('Comments');
  1218. $table = TableRegistry::get('Comments');
  1219. $table->updateAll(['updated' => Time::tomorrow()], ['id' => 6]);
  1220. $query = $table->find();
  1221. $result = $query->where(['updated >' => $query->func()->now('datetime')])->first();
  1222. $this->assertSame(6, $result->id);
  1223. }
  1224. }