QueryRegressionTest.php 43 KB

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