| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\Datasource\ConnectionManager;
- use Cake\ORM\Entity;
- use Cake\ORM\Marshaller;
- use Cake\ORM\Query;
- use Cake\ORM\Table;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- /**
- * Test entity for mass assignment.
- */
- class OpenArticleEntity extends Entity
- {
- protected $_accessible = [
- '*' => true
- ];
- }
- /**
- * Integration tetss for table operations involving composite keys
- */
- class CompositeKeyTest extends TestCase
- {
- /**
- * Fixture to be used
- *
- * @var array
- */
- public $fixtures = [
- 'core.site_articles',
- 'core.site_authors',
- 'core.site_tags',
- 'core.site_articles_tags'
- ];
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->connection = ConnectionManager::get('test');
- }
- /**
- * Data provider for the two types of strategies HasMany implements
- *
- * @return void
- */
- public function strategiesProvider()
- {
- return [['subquery'], ['select']];
- }
- /**
- * Tests that HasMany associations are correctly eager loaded and results
- * correctly nested when multiple foreignKeys are used
- *
- * @dataProvider strategiesProvider
- * @return void
- */
- public function testHasManyEager($strategy)
- {
- $table = TableRegistry::get('SiteAuthors');
- $table->hasMany('SiteArticles', [
- 'propertyName' => 'articles',
- 'strategy' => $strategy,
- 'sort' => ['SiteArticles.id' => 'asc'],
- 'foreignKey' => ['author_id', 'site_id']
- ]);
- $query = new Query($this->connection, $table);
- $results = $query->select()
- ->contain('SiteArticles')
- ->hydrate(false)
- ->toArray();
- $expected = [
- [
- 'id' => 1,
- 'name' => 'mark',
- 'site_id' => 1,
- 'articles' => [
- [
- 'id' => 1,
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'author_id' => 1,
- 'site_id' => 1
- ]
- ]
- ],
- [
- 'id' => 2,
- 'name' => 'juan',
- 'site_id' => 2,
- 'articles' => [],
- ],
- [
- 'id' => 3,
- 'name' => 'jose',
- 'site_id' => 2,
- 'articles' => [
- [
- 'id' => 2,
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'author_id' => 3,
- 'site_id' => 2
- ]
- ]
- ],
- [
- 'id' => 4,
- 'name' => 'andy',
- 'site_id' => 1,
- 'articles' => [],
- ]
- ];
- $this->assertEquals($expected, $results);
- $results = $query->repository($table)
- ->select()
- ->contain(['SiteArticles' => ['conditions' => ['SiteArticles.id' => 2]]])
- ->hydrate(false)
- ->toArray();
- $expected[0]['articles'] = [];
- $this->assertEquals($expected, $results);
- $this->assertEquals($table->association('SiteArticles')->strategy(), $strategy);
- }
- /**
- * Tests that BelongsToMany associations are correctly eager loaded when multiple
- * foreignKeys are used
- *
- * @dataProvider strategiesProvider
- * @return void
- */
- public function testBelongsToManyEager($strategy)
- {
- $articles = TableRegistry::get('SiteArticles');
- $tags = TableRegistry::get('SiteTags');
- $junction = TableRegistry::get('SiteArticlesTags');
- $articles->belongsToMany('SiteTags', [
- 'strategy' => $strategy,
- 'targetTable' => $tags,
- 'propertyName' => 'tags',
- 'through' => 'SiteArticlesTags',
- 'foreignKey' => ['article_id', 'site_id'],
- 'targetForeignKey' => ['tag_id', 'site_id']
- ]);
- $query = new Query($this->connection, $articles);
- $results = $query->select()->contain('SiteTags')->hydrate(false)->toArray();
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'site_id' => 1,
- 'tags' => [
- [
- 'id' => 1,
- 'name' => 'tag1',
- '_joinData' => ['article_id' => 1, 'tag_id' => 1, 'site_id' => 1],
- 'site_id' => 1
- ],
- [
- 'id' => 3,
- 'name' => 'tag3',
- '_joinData' => ['article_id' => 1, 'tag_id' => 3, 'site_id' => 1],
- 'site_id' => 1
- ]
- ]
- ],
- [
- 'id' => 2,
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'author_id' => 3,
- 'site_id' => 2,
- 'tags' => [
- [
- 'id' => 4,
- 'name' => 'tag4',
- '_joinData' => ['article_id' => 2, 'tag_id' => 4, 'site_id' => 2],
- 'site_id' => 2
- ]
- ]
- ],
- [
- 'id' => 3,
- 'title' => 'Third Article',
- 'body' => 'Third Article Body',
- 'author_id' => 1,
- 'site_id' => 2,
- 'tags' => [],
- ],
- [
- 'id' => 4,
- 'title' => 'Fourth Article',
- 'body' => 'Fourth Article Body',
- 'author_id' => 3,
- 'site_id' => 1,
- 'tags' => [
- [
- 'id' => 1,
- 'name' => 'tag1',
- '_joinData' => ['article_id' => 4, 'tag_id' => 1, 'site_id' => 1],
- 'site_id' => 1
- ]
- ]
- ],
- ];
- $this->assertEquals($expected, $results);
- $this->assertEquals($articles->association('SiteTags')->strategy(), $strategy);
- }
- /**
- * Provides strategies for associations that can be joined
- *
- * @return void
- */
- public function internalStategiesProvider()
- {
- return [['join'], ['select'], ['subquery']];
- }
- /**
- * Tests loding belongsTo with composite keys
- *
- * @dataProvider internalStategiesProvider
- * @return void
- */
- public function testBelongsToEager($strategy)
- {
- $table = TableRegistry::get('SiteArticles');
- $table->belongsTo('SiteAuthors', [
- 'propertyName' => 'author',
- 'strategy' => $strategy,
- 'foreignKey' => ['author_id', 'site_id']
- ]);
- $query = new Query($this->connection, $table);
- $results = $query->select()
- ->where(['SiteArticles.id IN' => [1, 2]])
- ->contain('SiteAuthors')
- ->hydrate(false)
- ->toArray();
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- 'site_id' => 1,
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'author' => [
- 'id' => 1,
- 'name' => 'mark',
- 'site_id' => 1
- ]
- ],
- [
- 'id' => 2,
- 'author_id' => 3,
- 'site_id' => 2,
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'author' => [
- 'id' => 3,
- 'name' => 'jose',
- 'site_id' => 2
- ]
- ]
- ];
- $this->assertEquals($expected, $results);
- }
- /**
- * Tests loding hasOne with composite keys
- *
- * @dataProvider internalStategiesProvider
- * @return void
- */
- public function testHasOneEager($strategy)
- {
- $table = TableRegistry::get('SiteAuthors');
- $table->hasOne('SiteArticles', [
- 'propertyName' => 'first_article',
- 'strategy' => $strategy,
- 'foreignKey' => ['author_id', 'site_id']
- ]);
- $query = new Query($this->connection, $table);
- $results = $query->select()
- ->where(['SiteAuthors.id IN' => [1, 3]])
- ->contain('SiteArticles')
- ->hydrate(false)
- ->toArray();
- $expected = [
- [
- 'id' => 1,
- 'name' => 'mark',
- 'site_id' => 1,
- 'first_article' => [
- 'id' => 1,
- 'author_id' => 1,
- 'site_id' => 1,
- 'title' => 'First Article',
- 'body' => 'First Article Body'
- ]
- ],
- [
- 'id' => 3,
- 'name' => 'jose',
- 'site_id' => 2,
- 'first_article' => [
- 'id' => 2,
- 'author_id' => 3,
- 'site_id' => 2,
- 'title' => 'Second Article',
- 'body' => 'Second Article Body'
- ]
- ]
- ];
- $this->assertEquals($expected, $results);
- }
- /**
- * Tests that it is possible to insert a new row using the save method
- * if the entity has composite primary key
- *
- * @group save
- * @return void
- */
- public function testSaveNewEntity()
- {
- $entity = new \Cake\ORM\Entity([
- 'id' => 5,
- 'site_id' => 1,
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'author_id' => 3,
- ]);
- $table = TableRegistry::get('SiteArticles');
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals($entity->id, 5);
- $row = $table->find('all')->where(['id' => 5, 'site_id' => 1])->first();
- $this->assertEquals($entity->toArray(), $row->toArray());
- }
- /**
- * Tests that it is possible to insert a new row using the save method
- * if the entity has composite primary key
- *
- * @group save
- * @expectedException \RuntimeException
- * @expectedExceptionMessage Cannot insert row, some of the primary key values are missing. Got (5, ), expecting (id, site_id)
- * @return void
- */
- public function testSaveNewEntityMissingKey()
- {
- $entity = new \Cake\ORM\Entity([
- 'id' => 5,
- 'title' => 'Fifth Article',
- 'body' => 'Fifth Article Body',
- 'author_id' => 3,
- ]);
- $table = TableRegistry::get('SiteArticles');
- $table->save($entity);
- }
- /**
- * Test simple delete with composite primary key
- *
- * @return void
- */
- public function testDelete()
- {
- $table = TableRegistry::get('SiteAuthors');
- $table->save(new \Cake\ORM\Entity(['id' => 1, 'site_id' => 2]));
- $entity = $table->get([1, 1]);
- $result = $table->delete($entity);
- $this->assertTrue($result);
- $this->assertEquals(4, $table->find('all')->count());
- $this->assertEmpty($table->find()->where(['id' => 1, 'site_id' => 1])->first());
- }
- /**
- * Test delete with dependent records having composite keys
- *
- * @return void
- */
- public function testDeleteDependent()
- {
- $table = TableRegistry::get('SiteAuthors');
- $table->hasMany('SiteArticles', [
- 'foreignKey' => ['author_id', 'site_id'],
- 'dependent' => true,
- ]);
- $entity = $table->get([3, 2]);
- $result = $table->delete($entity);
- $query = $table->association('SiteArticles')->find('all', [
- 'conditions' => [
- 'author_id' => $entity->id,
- 'site_id' => $entity->site_id
- ]
- ]);
- $this->assertNull($query->all()->first(), 'Should not find any rows.');
- }
- /**
- * Test generating a list of entities from a list of composite ids
- *
- * @return void
- */
- public function testOneGenerateBelongsToManyEntitiesFromIds()
- {
- $articles = TableRegistry::get('SiteArticles');
- $articles->entityClass(__NAMESPACE__ . '\OpenArticleEntity');
- $tags = TableRegistry::get('SiteTags');
- $junction = TableRegistry::get('SiteArticlesTags');
- $articles->belongsToMany('SiteTags', [
- 'targetTable' => $tags,
- 'propertyName' => 'tags',
- 'through' => 'SiteArticlesTags',
- 'foreignKey' => ['article_id', 'site_id'],
- 'targetForeignKey' => ['tag_id', 'site_id']
- ]);
- $data = [
- 'title' => 'Haz tags',
- 'body' => 'Some content here',
- 'tags' => ['_ids' => [[1, 1], [2, 2], [3, 1]]]
- ];
- $marshall = new Marshaller($articles);
- $result = $marshall->one($data, ['associated' => ['SiteTags']]);
- $this->assertCount(3, $result->tags);
- $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
- $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
- $this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
- $data = [
- 'title' => 'Haz tags',
- 'body' => 'Some content here',
- 'tags' => ['_ids' => [1, 2, 3]]
- ];
- $marshall = new Marshaller($articles);
- $result = $marshall->one($data, ['associated' => ['SiteTags']]);
- $this->assertEmpty($result->tags);
- }
- /**
- * Tests find('list') with composite keys
- *
- * @return void
- */
- public function testFindListCompositeKeys()
- {
- $table = new Table([
- 'table' => 'site_authors',
- 'connection' => $this->connection,
- ]);
- $table->displayField('name');
- $query = $table->find('list')
- ->hydrate(false)
- ->order('id');
- $expected = [
- '1;1' => 'mark',
- '2;2' => 'juan',
- '3;2' => 'jose',
- '4;1' => 'andy'
- ];
- $this->assertEquals($expected, $query->toArray());
- $table->displayField(['name', 'site_id']);
- $query = $table->find('list')
- ->hydrate(false)
- ->order('id');
- $expected = [
- '1;1' => 'mark;1',
- '2;2' => 'juan;2',
- '3;2' => 'jose;2',
- '4;1' => 'andy;1'
- ];
- $this->assertEquals($expected, $query->toArray());
- $query = $table->find('list', ['groupField' => ['site_id', 'site_id']])
- ->hydrate(false)
- ->order('id');
- $expected = [
- '1;1' => [
- '1;1' => 'mark;1',
- '4;1' => 'andy;1'
- ],
- '2;2' => [
- '2;2' => 'juan;2',
- '3;2' => 'jose;2'
- ]
- ];
- $this->assertEquals($expected, $query->toArray());
- }
- /**
- * Tests find('threaded') with composite keys
- *
- * @return void
- */
- public function testFindThreadedCompositeKeys()
- {
- $table = TableRegistry::get('SiteAuthors');
- $query = $this->getMock(
- '\Cake\ORM\Query',
- ['_addDefaultFields', 'execute'],
- [null, $table]
- );
- $items = new \Cake\Datasource\ResultSetDecorator([
- ['id' => 1, 'name' => 'a', 'site_id' => 1, 'parent_id' => null],
- ['id' => 2, 'name' => 'a', 'site_id' => 2, 'parent_id' => null],
- ['id' => 3, 'name' => 'a', 'site_id' => 1, 'parent_id' => 1],
- ['id' => 4, 'name' => 'a', 'site_id' => 2, 'parent_id' => 2],
- ['id' => 5, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
- ['id' => 6, 'name' => 'a', 'site_id' => 1, 'parent_id' => 2],
- ['id' => 7, 'name' => 'a', 'site_id' => 1, 'parent_id' => 3],
- ['id' => 8, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
- ]);
- $query->find('threaded', ['parentField' => ['parent_id', 'site_id']]);
- $formatter = $query->formatResults()[0];
- $expected = [
- [
- 'id' => 1,
- 'name' => 'a',
- 'site_id' => 1,
- 'parent_id' => null,
- 'children' => [
- [
- 'id' => 3,
- 'name' => 'a',
- 'site_id' => 1,
- 'parent_id' => 1,
- 'children' => [
- [
- 'id' => 7,
- 'name' => 'a',
- 'site_id' => 1,
- 'parent_id' => 3,
- 'children' => []
- ]
- ]
- ]
- ]
- ],
- [
- 'id' => 2,
- 'name' => 'a',
- 'site_id' => 2,
- 'parent_id' => null,
- 'children' => [
- [
- 'id' => 4,
- 'name' => 'a',
- 'site_id' => 2,
- 'parent_id' => 2,
- 'children' => [
- [
- 'id' => 5,
- 'name' => 'a',
- 'site_id' => 2,
- 'parent_id' => 4,
- 'children' => []
- ],
- [
- 'id' => 8,
- 'name' => 'a',
- 'site_id' => 2,
- 'parent_id' => 4,
- 'children' => []
- ]
- ]
- ]
- ]
- ],
- [
- 'id' => 6,
- 'name' => 'a',
- 'site_id' => 1,
- 'parent_id' => 2,
- 'children' => []
- ]
- ];
- $this->assertEquals($expected, $formatter($items)->toArray());
- }
- }
|