CompositeKeysTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Datasource\ConnectionManager;
  17. use Cake\ORM\Entity;
  18. use Cake\ORM\Marshaller;
  19. use Cake\ORM\Query;
  20. use Cake\ORM\Table;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Test entity for mass assignment.
  25. */
  26. class OpenArticleEntity extends Entity {
  27. protected $_accessible = [
  28. '*' => true
  29. ];
  30. }
  31. /**
  32. * Integration tetss for table operations involving composite keys
  33. */
  34. class CompositeKeyTest extends TestCase {
  35. /**
  36. * Fixture to be used
  37. *
  38. * @var array
  39. */
  40. public $fixtures = [
  41. 'core.site_article',
  42. 'core.site_author',
  43. 'core.site_tag',
  44. 'core.site_articles_tag'
  45. ];
  46. /**
  47. * setUp method
  48. *
  49. * @return void
  50. */
  51. public function setUp() {
  52. parent::setUp();
  53. $this->connection = ConnectionManager::get('test');
  54. }
  55. /**
  56. * Data provider for the two types of strategies HasMany implements
  57. *
  58. * @return void
  59. */
  60. public function strategiesProvider() {
  61. return [['subquery'], ['select']];
  62. }
  63. /**
  64. * Tests that HasMany associations are correctly eager loaded and results
  65. * correctly nested when multiple foreignKeys are used
  66. *
  67. * @dataProvider strategiesProvider
  68. * @return void
  69. */
  70. public function testHasManyEager($strategy) {
  71. $table = TableRegistry::get('SiteAuthors');
  72. $table->hasMany('SiteArticles', [
  73. 'propertyName' => 'articles',
  74. 'strategy' => $strategy,
  75. 'sort' => ['SiteArticles.id' => 'asc'],
  76. 'foreignKey' => ['author_id', 'site_id']
  77. ]);
  78. $query = new Query($this->connection, $table);
  79. $results = $query->select()
  80. ->contain('SiteArticles')
  81. ->hydrate(false)
  82. ->toArray();
  83. $expected = [
  84. [
  85. 'id' => 1,
  86. 'name' => 'mark',
  87. 'site_id' => 1,
  88. 'articles' => [
  89. [
  90. 'id' => 1,
  91. 'title' => 'First Article',
  92. 'body' => 'First Article Body',
  93. 'author_id' => 1,
  94. 'site_id' => 1
  95. ]
  96. ]
  97. ],
  98. [
  99. 'id' => 2,
  100. 'name' => 'juan',
  101. 'site_id' => 2
  102. ],
  103. [
  104. 'id' => 3,
  105. 'name' => 'jose',
  106. 'site_id' => 2,
  107. 'articles' => [
  108. [
  109. 'id' => 2,
  110. 'title' => 'Second Article',
  111. 'body' => 'Second Article Body',
  112. 'author_id' => 3,
  113. 'site_id' => 2
  114. ]
  115. ]
  116. ],
  117. [
  118. 'id' => 4,
  119. 'name' => 'andy',
  120. 'site_id' => 1
  121. ]
  122. ];
  123. $this->assertEquals($expected, $results);
  124. $results = $query->repository($table)
  125. ->select()
  126. ->contain(['SiteArticles' => ['conditions' => ['id' => 2]]])
  127. ->hydrate(false)
  128. ->toArray();
  129. unset($expected[0]['articles']);
  130. $this->assertEquals($expected, $results);
  131. $this->assertEquals($table->association('SiteArticles')->strategy(), $strategy);
  132. }
  133. /**
  134. * Tests that BelongsToMany associations are correctly eager loaded when multiple
  135. * foreignKeys are used
  136. *
  137. * @dataProvider strategiesProvider
  138. * @return void
  139. **/
  140. public function testBelongsToManyEager($strategy) {
  141. $articles = TableRegistry::get('SiteArticles');
  142. $tags = TableRegistry::get('SiteTags');
  143. $junction = TableRegistry::get('SiteArticlesTags');
  144. $articles->belongsToMany('SiteTags', [
  145. 'strategy' => $strategy,
  146. 'targetTable' => $tags,
  147. 'propertyName' => 'tags',
  148. 'through' => 'SiteArticlesTags',
  149. 'foreignKey' => ['article_id', 'site_id'],
  150. 'targetForeignKey' => ['tag_id', 'site_id']
  151. ]);
  152. $query = new Query($this->connection, $articles);
  153. $results = $query->select()->contain('SiteTags')->hydrate(false)->toArray();
  154. $expected = [
  155. [
  156. 'id' => 1,
  157. 'author_id' => 1,
  158. 'title' => 'First Article',
  159. 'body' => 'First Article Body',
  160. 'site_id' => 1,
  161. 'tags' => [
  162. [
  163. 'id' => 1,
  164. 'name' => 'tag1',
  165. '_joinData' => ['article_id' => 1, 'tag_id' => 1, 'site_id' => 1],
  166. 'site_id' => 1
  167. ],
  168. [
  169. 'id' => 3,
  170. 'name' => 'tag3',
  171. '_joinData' => ['article_id' => 1, 'tag_id' => 3, 'site_id' => 1],
  172. 'site_id' => 1
  173. ]
  174. ]
  175. ],
  176. [
  177. 'id' => 2,
  178. 'title' => 'Second Article',
  179. 'body' => 'Second Article Body',
  180. 'author_id' => 3,
  181. 'site_id' => 2,
  182. 'tags' => [
  183. [
  184. 'id' => 4,
  185. 'name' => 'tag4',
  186. '_joinData' => ['article_id' => 2, 'tag_id' => 4, 'site_id' => 2],
  187. 'site_id' => 2
  188. ]
  189. ]
  190. ],
  191. [
  192. 'id' => 3,
  193. 'title' => 'Third Article',
  194. 'body' => 'Third Article Body',
  195. 'author_id' => 1,
  196. 'site_id' => 2
  197. ],
  198. [
  199. 'id' => 4,
  200. 'title' => 'Fourth Article',
  201. 'body' => 'Fourth Article Body',
  202. 'author_id' => 3,
  203. 'site_id' => 1,
  204. 'tags' => [
  205. [
  206. 'id' => 1,
  207. 'name' => 'tag1',
  208. '_joinData' => ['article_id' => 4, 'tag_id' => 1, 'site_id' => 1],
  209. 'site_id' => 1
  210. ]
  211. ]
  212. ],
  213. ];
  214. $this->assertEquals($expected, $results);
  215. $this->assertEquals($articles->association('SiteTags')->strategy(), $strategy);
  216. }
  217. /**
  218. * Tests that it is possible to insert a new row using the save method
  219. * if the entity has composite primary key
  220. *
  221. * @group save
  222. * @return void
  223. */
  224. public function testSaveNewEntity() {
  225. $entity = new \Cake\ORM\Entity([
  226. 'id' => 5,
  227. 'site_id' => 1,
  228. 'title' => 'Fifth Article',
  229. 'body' => 'Fifth Article Body',
  230. 'author_id' => 3,
  231. ]);
  232. $table = TableRegistry::get('SiteArticles');
  233. $this->assertSame($entity, $table->save($entity));
  234. $this->assertEquals($entity->id, 5);
  235. $row = $table->find('all')->where(['id' => 5, 'site_id' => 1])->first();
  236. $this->assertEquals($entity->toArray(), $row->toArray());
  237. }
  238. /**
  239. * Tests that it is possible to insert a new row using the save method
  240. * if the entity has composite primary key
  241. *
  242. * @group save
  243. * @expectedException \RuntimeException
  244. * @expectedExceptionMessage Cannot insert row, some of the primary key values are missing. Got (5, ), expecting (id, site_id)
  245. * @return void
  246. */
  247. public function testSaveNewEntityMissingKey() {
  248. $entity = new \Cake\ORM\Entity([
  249. 'id' => 5,
  250. 'title' => 'Fifth Article',
  251. 'body' => 'Fifth Article Body',
  252. 'author_id' => 3,
  253. ]);
  254. $table = TableRegistry::get('SiteArticles');
  255. $table->save($entity);
  256. }
  257. /**
  258. * Test simple delete with composite primary key
  259. *
  260. * @return void
  261. */
  262. public function testDelete() {
  263. $table = TableRegistry::get('SiteAuthors');
  264. $table->save(new \Cake\ORM\Entity(['id' => 1, 'site_id' => 2]));
  265. $entity = $table->get([1, 1]);
  266. $result = $table->delete($entity);
  267. $this->assertTrue($result);
  268. $this->assertEquals(4, $table->find('all')->count());
  269. $this->assertEmpty($table->find()->where(['id' => 1, 'site_id' => 1])->first());
  270. }
  271. /**
  272. * Test delete with dependent records having composite keys
  273. *
  274. * @return void
  275. */
  276. public function testDeleteDependent() {
  277. $table = TableRegistry::get('SiteAuthors');
  278. $table->hasMany('SiteArticles', [
  279. 'foreignKey' => ['author_id', 'site_id'],
  280. 'dependent' => true,
  281. ]);
  282. $entity = $table->get([3, 2]);
  283. $result = $table->delete($entity);
  284. $query = $table->association('SiteArticles')->find('all', [
  285. 'conditions' => [
  286. 'author_id' => $entity->id,
  287. 'site_id' => $entity->site_id
  288. ]
  289. ]);
  290. $this->assertNull($query->all()->first(), 'Should not find any rows.');
  291. }
  292. /**
  293. * Test generating a list of entities from a list of composite ids
  294. *
  295. * @return void
  296. */
  297. public function testOneGenerateBelongsToManyEntitiesFromIds() {
  298. $articles = TableRegistry::get('SiteArticles');
  299. $articles->entityClass(__NAMESPACE__ . '\OpenArticleEntity');
  300. $tags = TableRegistry::get('SiteTags');
  301. $junction = TableRegistry::get('SiteArticlesTags');
  302. $articles->belongsToMany('SiteTags', [
  303. 'targetTable' => $tags,
  304. 'propertyName' => 'tags',
  305. 'through' => 'SiteArticlesTags',
  306. 'foreignKey' => ['article_id', 'site_id'],
  307. 'targetForeignKey' => ['tag_id', 'site_id']
  308. ]);
  309. $data = [
  310. 'title' => 'Haz tags',
  311. 'body' => 'Some content here',
  312. 'tags' => ['_ids' => [[1, 1], [2, 2], [3, 1]]]
  313. ];
  314. $marshall = new Marshaller($articles);
  315. $result = $marshall->one($data, ['SiteTags']);
  316. $this->assertCount(3, $result->tags);
  317. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  318. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  319. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  320. $data = [
  321. 'title' => 'Haz tags',
  322. 'body' => 'Some content here',
  323. 'tags' => ['_ids' => [1, 2, 3]]
  324. ];
  325. $marshall = new Marshaller($articles);
  326. $result = $marshall->one($data, ['SiteTags']);
  327. $this->assertEmpty($result->tags);
  328. }
  329. /**
  330. * Tests find('list') with composite keys
  331. *
  332. * @return void
  333. */
  334. public function testFindListCompositeKeys() {
  335. $table = new Table([
  336. 'table' => 'site_authors',
  337. 'connection' => $this->connection,
  338. ]);
  339. $table->displayField('name');
  340. $query = $table->find('list')
  341. ->hydrate(false)
  342. ->order('id');
  343. $expected = [
  344. '1;1' => 'mark',
  345. '2;2' => 'juan',
  346. '3;2' => 'jose',
  347. '4;1' => 'andy'
  348. ];
  349. $this->assertEquals($expected, $query->toArray());
  350. $table->displayField(['name', 'site_id']);
  351. $query = $table->find('list')
  352. ->hydrate(false)
  353. ->order('id');
  354. $expected = [
  355. '1;1' => 'mark;1',
  356. '2;2' => 'juan;2',
  357. '3;2' => 'jose;2',
  358. '4;1' => 'andy;1'
  359. ];
  360. $this->assertEquals($expected, $query->toArray());
  361. $query = $table->find('list', ['groupField' => ['site_id', 'site_id']])
  362. ->hydrate(false)
  363. ->order('id');
  364. $expected = [
  365. '1;1' => [
  366. '1;1' => 'mark;1',
  367. '4;1' => 'andy;1'
  368. ],
  369. '2;2' => [
  370. '2;2' => 'juan;2',
  371. '3;2' => 'jose;2'
  372. ]
  373. ];
  374. $this->assertEquals($expected, $query->toArray());
  375. }
  376. /**
  377. * Tests find('threaded') with composite keys
  378. *
  379. * @return void
  380. */
  381. public function testFindThreadedCompositeKeys() {
  382. $table = TableRegistry::get('SiteAuthors');
  383. $query = $this->getMock(
  384. '\Cake\ORM\Query',
  385. ['_addDefaultFields', 'execute'],
  386. [null, $table]
  387. );
  388. $items = new \Cake\Datasource\ResultSetDecorator([
  389. ['id' => 1, 'name' => 'a', 'site_id' => 1, 'parent_id' => null],
  390. ['id' => 2, 'name' => 'a', 'site_id' => 2, 'parent_id' => null],
  391. ['id' => 3, 'name' => 'a', 'site_id' => 1, 'parent_id' => 1],
  392. ['id' => 4, 'name' => 'a', 'site_id' => 2, 'parent_id' => 2],
  393. ['id' => 5, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
  394. ['id' => 6, 'name' => 'a', 'site_id' => 1, 'parent_id' => 2],
  395. ['id' => 7, 'name' => 'a', 'site_id' => 1, 'parent_id' => 3],
  396. ['id' => 8, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
  397. ]);
  398. $query->find('threaded', ['parentField' => ['parent_id', 'site_id']]);
  399. $formatter = $query->formatResults()[0];
  400. $expected = [
  401. [
  402. 'id' => 1,
  403. 'name' => 'a',
  404. 'site_id' => 1,
  405. 'parent_id' => null,
  406. 'children' => [
  407. [
  408. 'id' => 3,
  409. 'name' => 'a',
  410. 'site_id' => 1,
  411. 'parent_id' => 1,
  412. 'children' => [
  413. [
  414. 'id' => 7,
  415. 'name' => 'a',
  416. 'site_id' => 1,
  417. 'parent_id' => 3,
  418. 'children' => []
  419. ]
  420. ]
  421. ]
  422. ]
  423. ],
  424. [
  425. 'id' => 2,
  426. 'name' => 'a',
  427. 'site_id' => 2,
  428. 'parent_id' => null,
  429. 'children' => [
  430. [
  431. 'id' => 4,
  432. 'name' => 'a',
  433. 'site_id' => 2,
  434. 'parent_id' => 2,
  435. 'children' => [
  436. [
  437. 'id' => 5,
  438. 'name' => 'a',
  439. 'site_id' => 2,
  440. 'parent_id' => 4,
  441. 'children' => []
  442. ],
  443. [
  444. 'id' => 8,
  445. 'name' => 'a',
  446. 'site_id' => 2,
  447. 'parent_id' => 4,
  448. 'children' => []
  449. ]
  450. ]
  451. ]
  452. ]
  453. ],
  454. [
  455. 'id' => 6,
  456. 'name' => 'a',
  457. 'site_id' => 1,
  458. 'parent_id' => 2,
  459. 'children' => []
  460. ]
  461. ];
  462. $this->assertEquals($expected, $formatter($items)->toArray());
  463. }
  464. }