QueryRegressionTest.php 39 KB

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