QueryRegressionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * Fixture to be used
  29. *
  30. * @var array
  31. */
  32. public $fixtures = [
  33. 'core.user',
  34. 'core.article',
  35. 'core.comment',
  36. 'core.tag',
  37. 'core.articles_tag',
  38. 'core.author',
  39. 'core.special_tag'
  40. ];
  41. /**
  42. * Tear down
  43. *
  44. * @return void
  45. */
  46. public function tearDown() {
  47. parent::tearDown();
  48. TableRegistry::clear();
  49. }
  50. /**
  51. * Test for https://github.com/cakephp/cakephp/issues/3087
  52. *
  53. * @return void
  54. */
  55. public function testSelectTimestampColumn() {
  56. $table = TableRegistry::get('users');
  57. $user = $table->find()->where(['id' => 1])->first();
  58. $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created);
  59. $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated);
  60. }
  61. /**
  62. * Tests that EagerLoader does not try to create queries for associations having no
  63. * keys to compare against
  64. *
  65. * @return void
  66. */
  67. public function testEagerLoadingFromEmptyResults() {
  68. $table = TableRegistry::get('Articles');
  69. $table->belongsToMany('ArticlesTags');
  70. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  71. $this->assertEmpty($results);
  72. }
  73. /**
  74. * Tests that duplicate aliases in contain() can be used, even when they would
  75. * naturally be attached to the query instead of eagerly loaded. What should
  76. * happen here is that One of the duplicates will be changed to be loaded using
  77. * an extra query, but yielding the same results
  78. *
  79. * @return void
  80. */
  81. public function testDuplicateAttachableAliases() {
  82. TableRegistry::get('Stuff', ['table' => 'tags']);
  83. TableRegistry::get('Things', ['table' => 'articles_tags']);
  84. $table = TableRegistry::get('Articles');
  85. $table->belongsTo('Authors');
  86. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  87. $table->Authors->target()->hasOne('Stuff', [
  88. 'foreignKey' => 'id',
  89. 'propertyName' => 'favorite_tag'
  90. ]);
  91. $table->Things->target()->belongsTo('Stuff', [
  92. 'foreignKey' => 'tag_id',
  93. 'propertyName' => 'foo']
  94. );
  95. $results = $table->find()
  96. ->contain(['Authors.Stuff', 'Things.Stuff'])
  97. ->toArray();
  98. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  99. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  100. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  101. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  102. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  103. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  104. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  105. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  106. }
  107. /**
  108. * Test for https://github.com/cakephp/cakephp/issues/3410
  109. *
  110. * @return void
  111. */
  112. public function testNullableTimeColumn() {
  113. $table = TableRegistry::get('users');
  114. $entity = $table->newEntity(['username' => 'derp', 'created' => null]);
  115. $this->assertSame($entity, $table->save($entity));
  116. $this->assertNull($entity->created);
  117. }
  118. /**
  119. * Test for https://github.com/cakephp/cakephp/issues/3626
  120. *
  121. * Checks that join data is actually created and not tried to be updated every time
  122. * @return void
  123. */
  124. public function testCreateJointData() {
  125. $articles = TableRegistry::get('Articles');
  126. $articles->belongsToMany('Highlights', [
  127. 'className' => 'TestApp\Model\Table\TagsTable',
  128. 'foreignKey' => 'article_id',
  129. 'targetForeignKey' => 'tag_id',
  130. 'through' => 'SpecialTags'
  131. ]);
  132. $entity = $articles->get(2);
  133. $data = [
  134. 'id' => 2,
  135. 'highlights' => [
  136. [
  137. 'name' => 'New Special Tag',
  138. '_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00']
  139. ]
  140. ]
  141. ];
  142. $entity = $articles->patchEntity($entity, $data, ['Highlights._joinData']);
  143. $articles->save($entity);
  144. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  145. $this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id);
  146. $this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d'));
  147. }
  148. /**
  149. * Tests that the junction table instance taken from both sides of a belongsToMany
  150. * relationship is actually the same object.
  151. *
  152. * @return void
  153. */
  154. public function testReciprocalBelongsToMany() {
  155. $articles = TableRegistry::get('Articles');
  156. $tags = TableRegistry::get('Tags');
  157. $articles->belongsToMany('Tags');
  158. $tags->belongsToMany('Articles');
  159. $left = $articles->Tags->junction();
  160. $right = $tags->Articles->junction();
  161. $this->assertSame($left, $right);
  162. }
  163. /**
  164. * Test for https://github.com/cakephp/cakephp/issues/4253
  165. *
  166. * Makes sure that the belongsToMany association is not overwritten with conflicting information
  167. * by any of the sides when the junction() function is invoked
  168. *
  169. * @return void
  170. */
  171. public function testReciprocalBelongsToMany2() {
  172. $articles = TableRegistry::get('Articles');
  173. $tags = TableRegistry::get('Tags');
  174. $articles->belongsToMany('Tags');
  175. $tags->belongsToMany('Articles');
  176. $result = $articles->find()->contain(['Tags'])->first();
  177. $sub = $articles->Tags->find()->select(['id'])->matching('Articles', function($q) use ($result) {
  178. return $q->where(['Articles.id' => 1]);
  179. });
  180. $query = $articles->Tags->find()->where(['id NOT IN' => $sub]);
  181. $this->assertEquals(1, $query->count());
  182. }
  183. /**
  184. * Returns an array with the saving strategies for a belongsTo association
  185. *
  186. * @return array
  187. */
  188. public function strategyProvider() {
  189. return [['append', 'replace']];
  190. }
  191. /**
  192. * Test for https://github.com/cakephp/cakephp/issues/3677 and
  193. * https://github.com/cakephp/cakephp/issues/3714
  194. *
  195. * Checks that only relevant associations are passed when saving _joinData
  196. * Tests that _joinData can also save deeper associations
  197. *
  198. * @dataProvider strategyProvider
  199. * @param string $strategy
  200. * @return void
  201. */
  202. public function testBelongsToManyDeepSave($strategy) {
  203. $articles = TableRegistry::get('Articles');
  204. $articles->belongsToMany('Highlights', [
  205. 'className' => 'TestApp\Model\Table\TagsTable',
  206. 'foreignKey' => 'article_id',
  207. 'targetForeignKey' => 'tag_id',
  208. 'through' => 'SpecialTags',
  209. 'saveStrategy' => $strategy
  210. ]);
  211. $articles->Highlights->junction()->belongsTo('Authors');
  212. $articles->Highlights->hasOne('Authors', [
  213. 'foreignKey' => 'id'
  214. ]);
  215. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  216. $data = [
  217. 'highlights' => [
  218. [
  219. 'name' => 'New Special Tag',
  220. '_joinData' => [
  221. 'highlighted' => true,
  222. 'highlighted_time' => '2014-06-01 10:10:00',
  223. 'author' => [
  224. 'name' => 'mariano'
  225. ]
  226. ],
  227. 'author' => ['name' => 'mark']
  228. ]
  229. ]
  230. ];
  231. $options = [
  232. 'associated' => [
  233. 'Highlights._joinData.Authors', 'Highlights.Authors'
  234. ]
  235. ];
  236. $entity = $articles->patchEntity($entity, $data, $options);
  237. $articles->save($entity, $options);
  238. $entity = $articles->get(2, [
  239. 'contain' => [
  240. 'SpecialTags' => ['sort' => ['SpecialTags.id' => 'ASC']],
  241. 'SpecialTags.Authors',
  242. 'Highlights.Authors'
  243. ]
  244. ]);
  245. $this->assertEquals('mariano', end($entity->special_tags)->author->name);
  246. $this->assertEquals('mark', end($entity->highlights)->author->name);
  247. }
  248. /**
  249. * Tests that no exceptions are generated becuase of ambiguous column names in queries
  250. * during a save operation
  251. *
  252. * @see https://github.com/cakephp/cakephp/issues/3803
  253. * @return void
  254. */
  255. public function testSaveWithCallbacks() {
  256. $articles = TableRegistry::get('Articles');
  257. $articles->belongsTo('Authors');
  258. $articles->eventManager()->attach(function($event, $query) {
  259. return $query->contain('Authors');
  260. }, 'Model.beforeFind');
  261. $article = $articles->newEntity();
  262. $article->title = 'Foo';
  263. $article->body = 'Bar';
  264. $this->assertSame($article, $articles->save($article));
  265. }
  266. /**
  267. * Tests that whe saving deep associations for a belongsToMany property,
  268. * data is not removed becuase of excesive associations filtering.
  269. *
  270. * @see https://github.com/cakephp/cakephp/issues/4009
  271. * @return void
  272. */
  273. public function testBelongsToManyDeepSave2() {
  274. $articles = TableRegistry::get('Articles');
  275. $articles->belongsToMany('Highlights', [
  276. 'className' => 'TestApp\Model\Table\TagsTable',
  277. 'foreignKey' => 'article_id',
  278. 'targetForeignKey' => 'tag_id',
  279. 'through' => 'SpecialTags',
  280. ]);
  281. $articles->Highlights->hasMany('TopArticles', [
  282. 'className' => 'TestApp\Model\Table\ArticlesTable',
  283. 'foreignKey' => 'author_id',
  284. ]);
  285. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  286. $data = [
  287. 'highlights' => [
  288. [
  289. 'name' => 'New Special Tag',
  290. '_joinData' => [
  291. 'highlighted' => true,
  292. 'highlighted_time' => '2014-06-01 10:10:00',
  293. ],
  294. 'top_articles' => [
  295. ['title' => 'First top article'],
  296. ['title' => 'Second top article'],
  297. ]
  298. ]
  299. ]
  300. ];
  301. $options = [
  302. 'associated' => [
  303. 'Highlights._joinData', 'Highlights.TopArticles'
  304. ]
  305. ];
  306. $entity = $articles->patchEntity($entity, $data, $options);
  307. $articles->save($entity, $options);
  308. $entity = $articles->get(2, [
  309. 'contain' => [
  310. 'Highlights.TopArticles'
  311. ]
  312. ]);
  313. $highlights = $entity->highlights[0];
  314. $this->assertEquals('First top article', $highlights->top_articles[0]->title);
  315. $this->assertEquals('Second top article', $highlights->top_articles[1]->title);
  316. $this->assertEquals(
  317. new Time('2014-06-01 10:10:00'),
  318. $highlights->_joinData->highlighted_time
  319. );
  320. }
  321. /**
  322. * An integration test that spot checks that associations use the
  323. * correct alias names to generate queries.
  324. *
  325. * @return void
  326. */
  327. public function testPluginAssociationQueryGeneration() {
  328. Plugin::load('TestPlugin');
  329. $articles = TableRegistry::get('Articles');
  330. $articles->hasMany('TestPlugin.Comments');
  331. $articles->belongsTo('TestPlugin.Authors');
  332. $result = $articles->find()
  333. ->where(['Articles.id' => 2])
  334. ->contain(['Comments', 'Authors'])
  335. ->first();
  336. $this->assertNotEmpty(
  337. $result->comments[0]->id,
  338. 'No SQL error and comment exists.'
  339. );
  340. $this->assertNotEmpty(
  341. $result->author->id,
  342. 'No SQL error and author exists.'
  343. );
  344. }
  345. }