QueryRegressionTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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\ORM\Query;
  17. use Cake\ORM\Table;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\Utility\Time;
  21. /**
  22. * Contains regression test for the Query builder
  23. *
  24. */
  25. class QueryRegressionTest extends TestCase {
  26. /**
  27. * Fixture to be used
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.user',
  33. 'core.article',
  34. 'core.tag',
  35. 'core.articles_tag',
  36. 'core.author',
  37. 'core.special_tag'
  38. ];
  39. /**
  40. * Tear down
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. parent::tearDown();
  46. TableRegistry::clear();
  47. }
  48. /**
  49. * Test for https://github.com/cakephp/cakephp/issues/3087
  50. *
  51. * @return void
  52. */
  53. public function testSelectTimestampColumn() {
  54. $table = TableRegistry::get('users');
  55. $user = $table->find()->where(['id' => 1])->first();
  56. $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created);
  57. $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated);
  58. }
  59. /**
  60. * Tests that EagerLoader does not try to create queries for associations having no
  61. * keys to compare against
  62. *
  63. * @return void
  64. */
  65. public function testEagerLoadingFromEmptyResults() {
  66. $table = TableRegistry::get('Articles');
  67. $table->belongsToMany('ArticlesTags');
  68. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  69. $this->assertEmpty($results);
  70. }
  71. /**
  72. * Tests that duplicate aliases in contain() can be used, even when they would
  73. * naturally be attached to the query instead of eagerly loaded. What should
  74. * happen here is that One of the duplicates will be changed to be loaded using
  75. * an extra query, but yielding the same results
  76. *
  77. * @return void
  78. */
  79. public function testDuplicateAttachableAliases() {
  80. TableRegistry::get('Stuff', ['table' => 'tags']);
  81. TableRegistry::get('Things', ['table' => 'articles_tags']);
  82. $table = TableRegistry::get('Articles');
  83. $table->belongsTo('Authors');
  84. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  85. $table->Authors->target()->hasOne('Stuff', [
  86. 'foreignKey' => 'id',
  87. 'propertyName' => 'favorite_tag'
  88. ]);
  89. $table->Things->target()->belongsTo('Stuff', [
  90. 'foreignKey' => 'tag_id',
  91. 'propertyName' => 'foo']
  92. );
  93. $results = $table->find()
  94. ->contain(['Authors.Stuff', 'Things.Stuff'])
  95. ->toArray();
  96. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  97. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  98. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  99. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  100. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  101. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  102. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  103. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  104. }
  105. /**
  106. * Test for https://github.com/cakephp/cakephp/issues/3410
  107. *
  108. * @return void
  109. */
  110. public function testNullableTimeColumn() {
  111. $table = TableRegistry::get('users');
  112. $entity = $table->newEntity(['username' => 'derp', 'created' => null]);
  113. $this->assertSame($entity, $table->save($entity));
  114. $this->assertNull($entity->created);
  115. }
  116. /**
  117. * Test for https://github.com/cakephp/cakephp/issues/3626
  118. *
  119. * Checks that join data is actually created and not tried to be updated every time
  120. * @return void
  121. */
  122. public function testCreateJointData() {
  123. $articles = TableRegistry::get('Articles');
  124. $articles->belongsToMany('Highlights', [
  125. 'className' => 'TestApp\Model\Table\TagsTable',
  126. 'foreignKey' => 'article_id',
  127. 'targetForeignKey' => 'tag_id',
  128. 'through' => 'SpecialTags'
  129. ]);
  130. $entity = $articles->get(2);
  131. $data = [
  132. 'id' => 2,
  133. 'highlights' => [
  134. [
  135. 'name' => 'New Special Tag',
  136. '_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00']
  137. ]
  138. ]
  139. ];
  140. $entity = $articles->patchEntity($entity, $data, ['Highlights._joinData']);
  141. $articles->save($entity);
  142. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  143. $this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id);
  144. $this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d'));
  145. }
  146. /**
  147. * Tests that the junction table instance taken from both sides of a belongsToMany
  148. * relationship is actually the same object.
  149. *
  150. * @return void
  151. */
  152. public function testReciprocalBelongsToMany() {
  153. $articles = TableRegistry::get('Articles');
  154. $tags = TableRegistry::get('Tags');
  155. $articles->belongsToMany('Tags');
  156. $tags->belongsToMany('Articles');
  157. $left = $articles->Tags->junction();
  158. $right = $tags->Articles->junction();
  159. $this->assertSame($left, $right);
  160. }
  161. /**
  162. * Returns an array with the saving strategies for a belongsTo association
  163. *
  164. * @return array
  165. */
  166. public function strategyProvider() {
  167. return [['append', 'replace']];
  168. }
  169. /**
  170. * Test for https://github.com/cakephp/cakephp/issues/3677 and
  171. * https://github.com/cakephp/cakephp/issues/3714
  172. *
  173. * Checks that only relevant associations are passed when saving _joinData
  174. * Tests that _joinData can also save deeper associations
  175. *
  176. * @dataProvider strategyProvider
  177. * @param string $strategy
  178. * @return void
  179. */
  180. public function testBelongsToManyDeepSave($strategy) {
  181. $articles = TableRegistry::get('Articles');
  182. $articles->belongsToMany('Highlights', [
  183. 'className' => 'TestApp\Model\Table\TagsTable',
  184. 'foreignKey' => 'article_id',
  185. 'targetForeignKey' => 'tag_id',
  186. 'through' => 'SpecialTags',
  187. 'saveStrategy' => $strategy
  188. ]);
  189. $articles->Highlights->junction()->belongsTo('Authors');
  190. $articles->Highlights->hasOne('Authors', [
  191. 'foreignKey' => 'id'
  192. ]);
  193. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  194. $data = [
  195. 'highlights' => [
  196. [
  197. 'name' => 'New Special Tag',
  198. '_joinData' => [
  199. 'highlighted' => true,
  200. 'highlighted_time' => '2014-06-01 10:10:00',
  201. 'author' => [
  202. 'name' => 'mariano'
  203. ]
  204. ],
  205. 'author' => ['name' => 'mark']
  206. ]
  207. ]
  208. ];
  209. $entity = $articles->patchEntity($entity, $data, [
  210. 'Highlights._joinData.Authors', 'Highlights.Authors'
  211. ]);
  212. $articles->save($entity, [
  213. 'associated' => [
  214. 'Highlights._joinData.Authors', 'Highlights.Authors'
  215. ]
  216. ]);
  217. $entity = $articles->get(2, [
  218. 'contain' => [
  219. 'SpecialTags' => ['sort' => ['SpecialTags.id' => 'ASC']],
  220. 'SpecialTags.Authors',
  221. 'Highlights.Authors'
  222. ]
  223. ]);
  224. $this->assertEquals('mariano', end($entity->special_tags)->author->name);
  225. $this->assertEquals('mark', end($entity->highlights)->author->name);
  226. }
  227. }