QueryRegressionTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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\ORM\Query;
  18. use Cake\ORM\Table;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Time;
  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. * Returns an array with the saving strategies for a belongsTo association
  165. *
  166. * @return array
  167. */
  168. public function strategyProvider() {
  169. return [['append', 'replace']];
  170. }
  171. /**
  172. * Test for https://github.com/cakephp/cakephp/issues/3677 and
  173. * https://github.com/cakephp/cakephp/issues/3714
  174. *
  175. * Checks that only relevant associations are passed when saving _joinData
  176. * Tests that _joinData can also save deeper associations
  177. *
  178. * @dataProvider strategyProvider
  179. * @param string $strategy
  180. * @return void
  181. */
  182. public function testBelongsToManyDeepSave($strategy) {
  183. $articles = TableRegistry::get('Articles');
  184. $articles->belongsToMany('Highlights', [
  185. 'className' => 'TestApp\Model\Table\TagsTable',
  186. 'foreignKey' => 'article_id',
  187. 'targetForeignKey' => 'tag_id',
  188. 'through' => 'SpecialTags',
  189. 'saveStrategy' => $strategy
  190. ]);
  191. $articles->Highlights->junction()->belongsTo('Authors');
  192. $articles->Highlights->hasOne('Authors', [
  193. 'foreignKey' => 'id'
  194. ]);
  195. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  196. $data = [
  197. 'highlights' => [
  198. [
  199. 'name' => 'New Special Tag',
  200. '_joinData' => [
  201. 'highlighted' => true,
  202. 'highlighted_time' => '2014-06-01 10:10:00',
  203. 'author' => [
  204. 'name' => 'mariano'
  205. ]
  206. ],
  207. 'author' => ['name' => 'mark']
  208. ]
  209. ]
  210. ];
  211. $options = [
  212. 'associated' => [
  213. 'Highlights._joinData.Authors', 'Highlights.Authors'
  214. ]
  215. ];
  216. $entity = $articles->patchEntity($entity, $data, $options);
  217. $articles->save($entity, $options);
  218. $entity = $articles->get(2, [
  219. 'contain' => [
  220. 'SpecialTags' => ['sort' => ['SpecialTags.id' => 'ASC']],
  221. 'SpecialTags.Authors',
  222. 'Highlights.Authors'
  223. ]
  224. ]);
  225. $this->assertEquals('mariano', end($entity->special_tags)->author->name);
  226. $this->assertEquals('mark', end($entity->highlights)->author->name);
  227. }
  228. /**
  229. * Tests that no exceptions are generated becuase of ambiguous column names in queries
  230. * during a save operation
  231. *
  232. * @see https://github.com/cakephp/cakephp/issues/3803
  233. * @return void
  234. */
  235. public function testSaveWithCallbacks() {
  236. $articles = TableRegistry::get('Articles');
  237. $articles->belongsTo('Authors');
  238. $articles->eventManager()->attach(function($event, $query) {
  239. return $query->contain('Authors');
  240. }, 'Model.beforeFind');
  241. $article = $articles->newEntity();
  242. $article->title = 'Foo';
  243. $article->body = 'Bar';
  244. $this->assertSame($article, $articles->save($article));
  245. }
  246. /**
  247. * Tests that whe saving deep associations for a belongsToMany property,
  248. * data is not removed becuase of excesive associations filtering.
  249. *
  250. * @see https://github.com/cakephp/cakephp/issues/4009
  251. * @return void
  252. */
  253. public function testBelongsToManyDeepSave2() {
  254. $articles = TableRegistry::get('Articles');
  255. $articles->belongsToMany('Highlights', [
  256. 'className' => 'TestApp\Model\Table\TagsTable',
  257. 'foreignKey' => 'article_id',
  258. 'targetForeignKey' => 'tag_id',
  259. 'through' => 'SpecialTags',
  260. ]);
  261. $articles->Highlights->hasMany('TopArticles', [
  262. 'className' => 'TestApp\Model\Table\ArticlesTable',
  263. 'foreignKey' => 'author_id',
  264. ]);
  265. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  266. $data = [
  267. 'highlights' => [
  268. [
  269. 'name' => 'New Special Tag',
  270. '_joinData' => [
  271. 'highlighted' => true,
  272. 'highlighted_time' => '2014-06-01 10:10:00',
  273. ],
  274. 'top_articles' => [
  275. ['title' => 'First top article'],
  276. ['title' => 'Second top article'],
  277. ]
  278. ]
  279. ]
  280. ];
  281. $options = [
  282. 'associated' => [
  283. 'Highlights._joinData', 'Highlights.TopArticles'
  284. ]
  285. ];
  286. $entity = $articles->patchEntity($entity, $data, $options);
  287. $articles->save($entity, $options);
  288. $entity = $articles->get(2, [
  289. 'contain' => [
  290. 'Highlights.TopArticles'
  291. ]
  292. ]);
  293. $highlights = $entity->highlights[0];
  294. $this->assertEquals('First top article', $highlights->top_articles[0]->title);
  295. $this->assertEquals('Second top article', $highlights->top_articles[1]->title);
  296. $this->assertEquals(
  297. new Time('2014-06-01 10:10:00'),
  298. $highlights->_joinData->highlighted_time
  299. );
  300. }
  301. /**
  302. * An integration test that spot checks that associations use the
  303. * correct alias names to generate queries.
  304. *
  305. * @return void
  306. */
  307. public function testPluginAssociationQueryGeneration() {
  308. Plugin::load('TestPlugin');
  309. $articles = TableRegistry::get('Articles');
  310. $articles->hasMany('TestPlugin.Comments');
  311. $articles->belongsTo('TestPlugin.Authors');
  312. $result = $articles->find()
  313. ->where(['Articles.id' => 2])
  314. ->contain(['Comments', 'Authors'])
  315. ->first();
  316. $this->assertNotEmpty(
  317. $result->comments[0]->id,
  318. 'No SQL error and comment exists.'
  319. );
  320. $this->assertNotEmpty(
  321. $result->author->id,
  322. 'No SQL error and author exists.'
  323. );
  324. }
  325. }