CompositeKeysTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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\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. 'articles' => [],
  103. ],
  104. [
  105. 'id' => 3,
  106. 'name' => 'jose',
  107. 'site_id' => 2,
  108. 'articles' => [
  109. [
  110. 'id' => 2,
  111. 'title' => 'Second Article',
  112. 'body' => 'Second Article Body',
  113. 'author_id' => 3,
  114. 'site_id' => 2
  115. ]
  116. ]
  117. ],
  118. [
  119. 'id' => 4,
  120. 'name' => 'andy',
  121. 'site_id' => 1,
  122. 'articles' => [],
  123. ]
  124. ];
  125. $this->assertEquals($expected, $results);
  126. $results = $query->repository($table)
  127. ->select()
  128. ->contain(['SiteArticles' => ['conditions' => ['id' => 2]]])
  129. ->hydrate(false)
  130. ->toArray();
  131. $expected[0]['articles'] = [];
  132. $this->assertEquals($expected, $results);
  133. $this->assertEquals($table->association('SiteArticles')->strategy(), $strategy);
  134. }
  135. /**
  136. * Tests that BelongsToMany associations are correctly eager loaded when multiple
  137. * foreignKeys are used
  138. *
  139. * @dataProvider strategiesProvider
  140. * @return void
  141. **/
  142. public function testBelongsToManyEager($strategy) {
  143. $articles = TableRegistry::get('SiteArticles');
  144. $tags = TableRegistry::get('SiteTags');
  145. $junction = TableRegistry::get('SiteArticlesTags');
  146. $articles->belongsToMany('SiteTags', [
  147. 'strategy' => $strategy,
  148. 'targetTable' => $tags,
  149. 'propertyName' => 'tags',
  150. 'through' => 'SiteArticlesTags',
  151. 'foreignKey' => ['article_id', 'site_id'],
  152. 'targetForeignKey' => ['tag_id', 'site_id']
  153. ]);
  154. $query = new Query($this->connection, $articles);
  155. $results = $query->select()->contain('SiteTags')->hydrate(false)->toArray();
  156. $expected = [
  157. [
  158. 'id' => 1,
  159. 'author_id' => 1,
  160. 'title' => 'First Article',
  161. 'body' => 'First Article Body',
  162. 'site_id' => 1,
  163. 'tags' => [
  164. [
  165. 'id' => 1,
  166. 'name' => 'tag1',
  167. '_joinData' => ['article_id' => 1, 'tag_id' => 1, 'site_id' => 1],
  168. 'site_id' => 1
  169. ],
  170. [
  171. 'id' => 3,
  172. 'name' => 'tag3',
  173. '_joinData' => ['article_id' => 1, 'tag_id' => 3, 'site_id' => 1],
  174. 'site_id' => 1
  175. ]
  176. ]
  177. ],
  178. [
  179. 'id' => 2,
  180. 'title' => 'Second Article',
  181. 'body' => 'Second Article Body',
  182. 'author_id' => 3,
  183. 'site_id' => 2,
  184. 'tags' => [
  185. [
  186. 'id' => 4,
  187. 'name' => 'tag4',
  188. '_joinData' => ['article_id' => 2, 'tag_id' => 4, 'site_id' => 2],
  189. 'site_id' => 2
  190. ]
  191. ]
  192. ],
  193. [
  194. 'id' => 3,
  195. 'title' => 'Third Article',
  196. 'body' => 'Third Article Body',
  197. 'author_id' => 1,
  198. 'site_id' => 2,
  199. 'tags' => [],
  200. ],
  201. [
  202. 'id' => 4,
  203. 'title' => 'Fourth Article',
  204. 'body' => 'Fourth Article Body',
  205. 'author_id' => 3,
  206. 'site_id' => 1,
  207. 'tags' => [
  208. [
  209. 'id' => 1,
  210. 'name' => 'tag1',
  211. '_joinData' => ['article_id' => 4, 'tag_id' => 1, 'site_id' => 1],
  212. 'site_id' => 1
  213. ]
  214. ]
  215. ],
  216. ];
  217. $this->assertEquals($expected, $results);
  218. $this->assertEquals($articles->association('SiteTags')->strategy(), $strategy);
  219. }
  220. /**
  221. * Provides strategies for associations that can be joined
  222. *
  223. * @return void
  224. */
  225. public function internalStategiesProvider() {
  226. return [['join'], ['select'], ['subquery']];
  227. }
  228. /**
  229. * Tests loding belongsTo with composite keys
  230. *
  231. * @dataProvider internalStategiesProvider
  232. * @return void
  233. */
  234. public function testBelongsToEager($strategy) {
  235. $table = TableRegistry::get('SiteArticles');
  236. $table->belongsTo('SiteAuthors', [
  237. 'propertyName' => 'author',
  238. 'strategy' => $strategy,
  239. 'foreignKey' => ['author_id', 'site_id']
  240. ]);
  241. $query = new Query($this->connection, $table);
  242. $results = $query->select()
  243. ->where(['SiteArticles.id IN' => [1, 2]])
  244. ->contain('SiteAuthors')
  245. ->hydrate(false)
  246. ->toArray();
  247. $expected = [
  248. [
  249. 'id' => 1,
  250. 'author_id' => 1,
  251. 'site_id' => 1,
  252. 'title' => 'First Article',
  253. 'body' => 'First Article Body',
  254. 'author' => [
  255. 'id' => 1,
  256. 'name' => 'mark',
  257. 'site_id' => 1
  258. ]
  259. ],
  260. [
  261. 'id' => 2,
  262. 'author_id' => 3,
  263. 'site_id' => 2,
  264. 'title' => 'Second Article',
  265. 'body' => 'Second Article Body',
  266. 'author' => [
  267. 'id' => 3,
  268. 'name' => 'jose',
  269. 'site_id' => 2
  270. ]
  271. ]
  272. ];
  273. $this->assertEquals($expected, $results);
  274. }
  275. /**
  276. * Tests loding hasOne with composite keys
  277. *
  278. * @dataProvider internalStategiesProvider
  279. * @return void
  280. */
  281. public function testHasOneEager($strategy) {
  282. $table = TableRegistry::get('SiteAuthors');
  283. $table->hasOne('SiteArticles', [
  284. 'propertyName' => 'first_article',
  285. 'strategy' => $strategy,
  286. 'foreignKey' => ['author_id', 'site_id']
  287. ]);
  288. $query = new Query($this->connection, $table);
  289. $results = $query->select()
  290. ->where(['SiteAuthors.id IN' => [1, 3]])
  291. ->contain('SiteArticles')
  292. ->hydrate(false)
  293. ->toArray();
  294. $expected = [
  295. [
  296. 'id' => 1,
  297. 'name' => 'mark',
  298. 'site_id' => 1,
  299. 'first_article' => [
  300. 'id' => 1,
  301. 'author_id' => 1,
  302. 'site_id' => 1,
  303. 'title' => 'First Article',
  304. 'body' => 'First Article Body'
  305. ]
  306. ],
  307. [
  308. 'id' => 3,
  309. 'name' => 'jose',
  310. 'site_id' => 2,
  311. 'first_article' => [
  312. 'id' => 2,
  313. 'author_id' => 3,
  314. 'site_id' => 2,
  315. 'title' => 'Second Article',
  316. 'body' => 'Second Article Body'
  317. ]
  318. ]
  319. ];
  320. $this->assertEquals($expected, $results);
  321. }
  322. /**
  323. * Tests that it is possible to insert a new row using the save method
  324. * if the entity has composite primary key
  325. *
  326. * @group save
  327. * @return void
  328. */
  329. public function testSaveNewEntity() {
  330. $entity = new \Cake\ORM\Entity([
  331. 'id' => 5,
  332. 'site_id' => 1,
  333. 'title' => 'Fifth Article',
  334. 'body' => 'Fifth Article Body',
  335. 'author_id' => 3,
  336. ]);
  337. $table = TableRegistry::get('SiteArticles');
  338. $this->assertSame($entity, $table->save($entity));
  339. $this->assertEquals($entity->id, 5);
  340. $row = $table->find('all')->where(['id' => 5, 'site_id' => 1])->first();
  341. $this->assertEquals($entity->toArray(), $row->toArray());
  342. }
  343. /**
  344. * Tests that it is possible to insert a new row using the save method
  345. * if the entity has composite primary key
  346. *
  347. * @group save
  348. * @expectedException \RuntimeException
  349. * @expectedExceptionMessage Cannot insert row, some of the primary key values are missing. Got (5, ), expecting (id, site_id)
  350. * @return void
  351. */
  352. public function testSaveNewEntityMissingKey() {
  353. $entity = new \Cake\ORM\Entity([
  354. 'id' => 5,
  355. 'title' => 'Fifth Article',
  356. 'body' => 'Fifth Article Body',
  357. 'author_id' => 3,
  358. ]);
  359. $table = TableRegistry::get('SiteArticles');
  360. $table->save($entity);
  361. }
  362. /**
  363. * Test simple delete with composite primary key
  364. *
  365. * @return void
  366. */
  367. public function testDelete() {
  368. $table = TableRegistry::get('SiteAuthors');
  369. $table->save(new \Cake\ORM\Entity(['id' => 1, 'site_id' => 2]));
  370. $entity = $table->get([1, 1]);
  371. $result = $table->delete($entity);
  372. $this->assertTrue($result);
  373. $this->assertEquals(4, $table->find('all')->count());
  374. $this->assertEmpty($table->find()->where(['id' => 1, 'site_id' => 1])->first());
  375. }
  376. /**
  377. * Test delete with dependent records having composite keys
  378. *
  379. * @return void
  380. */
  381. public function testDeleteDependent() {
  382. $table = TableRegistry::get('SiteAuthors');
  383. $table->hasMany('SiteArticles', [
  384. 'foreignKey' => ['author_id', 'site_id'],
  385. 'dependent' => true,
  386. ]);
  387. $entity = $table->get([3, 2]);
  388. $result = $table->delete($entity);
  389. $query = $table->association('SiteArticles')->find('all', [
  390. 'conditions' => [
  391. 'author_id' => $entity->id,
  392. 'site_id' => $entity->site_id
  393. ]
  394. ]);
  395. $this->assertNull($query->all()->first(), 'Should not find any rows.');
  396. }
  397. /**
  398. * Test generating a list of entities from a list of composite ids
  399. *
  400. * @return void
  401. */
  402. public function testOneGenerateBelongsToManyEntitiesFromIds() {
  403. $articles = TableRegistry::get('SiteArticles');
  404. $articles->entityClass(__NAMESPACE__ . '\OpenArticleEntity');
  405. $tags = TableRegistry::get('SiteTags');
  406. $junction = TableRegistry::get('SiteArticlesTags');
  407. $articles->belongsToMany('SiteTags', [
  408. 'targetTable' => $tags,
  409. 'propertyName' => 'tags',
  410. 'through' => 'SiteArticlesTags',
  411. 'foreignKey' => ['article_id', 'site_id'],
  412. 'targetForeignKey' => ['tag_id', 'site_id']
  413. ]);
  414. $data = [
  415. 'title' => 'Haz tags',
  416. 'body' => 'Some content here',
  417. 'tags' => ['_ids' => [[1, 1], [2, 2], [3, 1]]]
  418. ];
  419. $marshall = new Marshaller($articles);
  420. $result = $marshall->one($data, ['SiteTags']);
  421. $this->assertCount(3, $result->tags);
  422. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
  423. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
  424. $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
  425. $data = [
  426. 'title' => 'Haz tags',
  427. 'body' => 'Some content here',
  428. 'tags' => ['_ids' => [1, 2, 3]]
  429. ];
  430. $marshall = new Marshaller($articles);
  431. $result = $marshall->one($data, ['SiteTags']);
  432. $this->assertEmpty($result->tags);
  433. }
  434. /**
  435. * Tests find('list') with composite keys
  436. *
  437. * @return void
  438. */
  439. public function testFindListCompositeKeys() {
  440. $table = new Table([
  441. 'table' => 'site_authors',
  442. 'connection' => $this->connection,
  443. ]);
  444. $table->displayField('name');
  445. $query = $table->find('list')
  446. ->hydrate(false)
  447. ->order('id');
  448. $expected = [
  449. '1;1' => 'mark',
  450. '2;2' => 'juan',
  451. '3;2' => 'jose',
  452. '4;1' => 'andy'
  453. ];
  454. $this->assertEquals($expected, $query->toArray());
  455. $table->displayField(['name', 'site_id']);
  456. $query = $table->find('list')
  457. ->hydrate(false)
  458. ->order('id');
  459. $expected = [
  460. '1;1' => 'mark;1',
  461. '2;2' => 'juan;2',
  462. '3;2' => 'jose;2',
  463. '4;1' => 'andy;1'
  464. ];
  465. $this->assertEquals($expected, $query->toArray());
  466. $query = $table->find('list', ['groupField' => ['site_id', 'site_id']])
  467. ->hydrate(false)
  468. ->order('id');
  469. $expected = [
  470. '1;1' => [
  471. '1;1' => 'mark;1',
  472. '4;1' => 'andy;1'
  473. ],
  474. '2;2' => [
  475. '2;2' => 'juan;2',
  476. '3;2' => 'jose;2'
  477. ]
  478. ];
  479. $this->assertEquals($expected, $query->toArray());
  480. }
  481. /**
  482. * Tests find('threaded') with composite keys
  483. *
  484. * @return void
  485. */
  486. public function testFindThreadedCompositeKeys() {
  487. $table = TableRegistry::get('SiteAuthors');
  488. $query = $this->getMock(
  489. '\Cake\ORM\Query',
  490. ['_addDefaultFields', 'execute'],
  491. [null, $table]
  492. );
  493. $items = new \Cake\Datasource\ResultSetDecorator([
  494. ['id' => 1, 'name' => 'a', 'site_id' => 1, 'parent_id' => null],
  495. ['id' => 2, 'name' => 'a', 'site_id' => 2, 'parent_id' => null],
  496. ['id' => 3, 'name' => 'a', 'site_id' => 1, 'parent_id' => 1],
  497. ['id' => 4, 'name' => 'a', 'site_id' => 2, 'parent_id' => 2],
  498. ['id' => 5, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
  499. ['id' => 6, 'name' => 'a', 'site_id' => 1, 'parent_id' => 2],
  500. ['id' => 7, 'name' => 'a', 'site_id' => 1, 'parent_id' => 3],
  501. ['id' => 8, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
  502. ]);
  503. $query->find('threaded', ['parentField' => ['parent_id', 'site_id']]);
  504. $formatter = $query->formatResults()[0];
  505. $expected = [
  506. [
  507. 'id' => 1,
  508. 'name' => 'a',
  509. 'site_id' => 1,
  510. 'parent_id' => null,
  511. 'children' => [
  512. [
  513. 'id' => 3,
  514. 'name' => 'a',
  515. 'site_id' => 1,
  516. 'parent_id' => 1,
  517. 'children' => [
  518. [
  519. 'id' => 7,
  520. 'name' => 'a',
  521. 'site_id' => 1,
  522. 'parent_id' => 3,
  523. 'children' => []
  524. ]
  525. ]
  526. ]
  527. ]
  528. ],
  529. [
  530. 'id' => 2,
  531. 'name' => 'a',
  532. 'site_id' => 2,
  533. 'parent_id' => null,
  534. 'children' => [
  535. [
  536. 'id' => 4,
  537. 'name' => 'a',
  538. 'site_id' => 2,
  539. 'parent_id' => 2,
  540. 'children' => [
  541. [
  542. 'id' => 5,
  543. 'name' => 'a',
  544. 'site_id' => 2,
  545. 'parent_id' => 4,
  546. 'children' => []
  547. ],
  548. [
  549. 'id' => 8,
  550. 'name' => 'a',
  551. 'site_id' => 2,
  552. 'parent_id' => 4,
  553. 'children' => []
  554. ]
  555. ]
  556. ]
  557. ]
  558. ],
  559. [
  560. 'id' => 6,
  561. 'name' => 'a',
  562. 'site_id' => 1,
  563. 'parent_id' => 2,
  564. 'children' => []
  565. ]
  566. ];
  567. $this->assertEquals($expected, $formatter($items)->toArray());
  568. }
  569. }