QueryRegressionTest.php 40 KB

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