diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-11-30 15:38:43 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-11-30 15:38:43 +0100 |
commit | 08248f66ba28db3f6e3328fc489fa52432c73544 (patch) | |
tree | 62e54500319f7aadd973f0f75e6aeba84ffdbb3b /tests | |
parent | fd79511d68b970b980bd65bb6537df9642f3de3e (diff) | |
parent | b8b0ae094432ca093022896e1b57db91400f245a (diff) | |
download | nextcloud-server-08248f66ba28db3f6e3328fc489fa52432c73544.tar.gz nextcloud-server-08248f66ba28db3f6e3328fc489fa52432c73544.zip |
Merge pull request #20650 from owncloud/systemtags-core
Implement systemtag managers and mapper
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/systemtag/systemtagmanagertest.php | 405 | ||||
-rw-r--r-- | tests/lib/systemtag/systemtagobjectmappertest.php | 335 |
2 files changed, 740 insertions, 0 deletions
diff --git a/tests/lib/systemtag/systemtagmanagertest.php b/tests/lib/systemtag/systemtagmanagertest.php new file mode 100644 index 00000000000..0a192f01f41 --- /dev/null +++ b/tests/lib/systemtag/systemtagmanagertest.php @@ -0,0 +1,405 @@ +<?php + +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * +*/ + +namespace Test\SystemTag; + +use OC\SystemTag\SystemTagManager; +use OC\SystemTag\SystemTagObjectMapper; +use OCP\IDBConnection; +use OCP\SystemTag\ISystemTag; +use OCP\SystemTag\ISystemTagManager; +use Test\TestCase; + +/** + * Class TestSystemTagManager + * + * @group DB + * @package Test\SystemTag + */ +class SystemTagManagerTest extends TestCase { + + /** + * @var ISystemTagManager + **/ + private $tagManager; + + /** + * @var IDBConnection + */ + private $connection; + + public function setUp() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + $this->tagManager = new SystemTagManager($this->connection); + } + + public function tearDown() { + $query = $this->connection->getQueryBuilder(); + $query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute(); + $query->delete(SystemTagManager::TAG_TABLE)->execute(); + } + + public function getAllTagsDataProvider() { + return [ + [ + // no tags at all + [] + ], + [ + // simple + [ + ['one', false, false], + ['two', false, false], + ] + ], + [ + // duplicate names, different flags + [ + ['one', false, false], + ['one', true, false], + ['one', false, true], + ['one', true, true], + ['two', false, false], + ['two', false, true], + ] + ] + ]; + } + + /** + * @dataProvider getAllTagsDataProvider + */ + public function testGetAllTags($testTags) { + $testTagsById = []; + foreach ($testTags as $testTag) { + $tag = $this->tagManager->createTag($testTag[0], $testTag[1], $testTag[2]); + $testTagsById[$tag->getId()] = $tag; + } + + $tagList = $this->tagManager->getAllTags(); + + $this->assertCount(count($testTags), $tagList); + + foreach ($testTagsById as $testTagId => $testTag) { + $this->assertTrue(isset($tagList[$testTagId])); + $this->assertSameTag($tagList[$testTagId], $testTag); + } + } + + public function getAllTagsFilteredDataProvider() { + return [ + [ + [ + // no tags at all + ], + null, + null, + [] + ], + // filter by visibile only + [ + // none visible + [ + ['one', false, false], + ['two', false, false], + ], + true, + null, + [] + ], + [ + // one visible + [ + ['one', true, false], + ['two', false, false], + ], + true, + null, + [ + ['one', true, false], + ] + ], + [ + // one invisible + [ + ['one', true, false], + ['two', false, false], + ], + false, + null, + [ + ['two', false, false], + ] + ], + // filter by name pattern + [ + [ + ['one', true, false], + ['one', false, false], + ['two', true, false], + ], + null, + 'on', + [ + ['one', true, false], + ['one', false, false], + ] + ], + // filter by name pattern and visibility + [ + // one visible + [ + ['one', true, false], + ['two', true, false], + ['one', false, false], + ], + true, + 'on', + [ + ['one', true, false], + ] + ], + // filter by name pattern in the middle + [ + // one visible + [ + ['abcdefghi', true, false], + ['two', true, false], + ], + null, + 'def', + [ + ['abcdefghi', true, false], + ] + ] + ]; + } + + /** + * @dataProvider getAllTagsFilteredDataProvider + */ + public function testGetAllTagsFiltered($testTags, $visibilityFilter, $nameSearch, $expectedResults) { + foreach ($testTags as $testTag) { + $this->tagManager->createTag($testTag[0], $testTag[1], $testTag[2]); + } + + $testTagsById = []; + foreach ($expectedResults as $expectedTag) { + $tag = $this->tagManager->getTag($expectedTag[0], $expectedTag[1], $expectedTag[2]); + $testTagsById[$tag->getId()] = $tag; + } + + $tagList = $this->tagManager->getAllTags($visibilityFilter, $nameSearch); + + $this->assertCount(count($testTagsById), $tagList); + + foreach ($testTagsById as $testTagId => $testTag) { + $this->assertTrue(isset($tagList[$testTagId])); + $this->assertSameTag($tagList[$testTagId], $testTag); + } + } + + public function oneTagMultipleFlagsProvider() { + return [ + ['one', false, false], + ['one', true, false], + ['one', false, true], + ['one', true, true], + ]; + } + + /** + * @dataProvider oneTagMultipleFlagsProvider + * @expectedException \OCP\SystemTag\TagAlreadyExistsException + */ + public function testCreateDuplicate($name, $userVisible, $userAssignable) { + try { + $this->tagManager->createTag($name, $userVisible, $userAssignable); + } catch (\Exception $e) { + $this->assertTrue(false, 'No exception thrown for the first create call'); + } + $this->tagManager->createTag($name, $userVisible, $userAssignable); + } + + /** + * @dataProvider oneTagMultipleFlagsProvider + */ + public function testGetExistingTag($name, $userVisible, $userAssignable) { + $tag1 = $this->tagManager->createTag($name, $userVisible, $userAssignable); + $tag2 = $this->tagManager->getTag($name, $userVisible, $userAssignable); + + $this->assertSameTag($tag1, $tag2); + } + + public function testGetExistingTagById() { + $tag1 = $this->tagManager->createTag('one', true, false); + $tag2 = $this->tagManager->createTag('two', false, true); + + $tagList = $this->tagManager->getTagsById([$tag1->getId(), $tag2->getId()]); + + $this->assertCount(2, $tagList); + + $this->assertSameTag($tag1, $tagList[$tag1->getId()]); + $this->assertSameTag($tag2, $tagList[$tag2->getId()]); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testGetNonExistingTag() { + $this->tagManager->getTag('nonexist', false, false); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testGetNonExistingTagsById() { + $tag1 = $this->tagManager->createTag('one', true, false); + $this->tagManager->getTagsById([$tag1->getId(), 100, 101]); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testGetInvalidTagIdFormat() { + $tag1 = $this->tagManager->createTag('one', true, false); + $this->tagManager->getTagsById([$tag1->getId() . 'suffix']); + } + + public function updateTagProvider() { + return [ + [ + // update name + ['one', true, true], + ['two', true, true] + ], + [ + // update one flag + ['one', false, true], + ['one', true, true] + ], + [ + // update all flags + ['one', false, false], + ['one', true, true] + ], + [ + // update all + ['one', false, false], + ['two', true, true] + ], + ]; + } + + /** + * @dataProvider updateTagProvider + */ + public function testUpdateTag($tagCreate, $tagUpdated) { + $tag1 = $this->tagManager->createTag( + $tagCreate[0], + $tagCreate[1], + $tagCreate[2] + ); + $this->tagManager->updateTag( + $tag1->getId(), + $tagUpdated[0], + $tagUpdated[1], + $tagUpdated[2] + ); + $tag2 = $this->tagManager->getTag( + $tagUpdated[0], + $tagUpdated[1], + $tagUpdated[2] + ); + + $this->assertEquals($tag2->getId(), $tag1->getId()); + $this->assertEquals($tag2->getName(), $tagUpdated[0]); + $this->assertEquals($tag2->isUserVisible(), $tagUpdated[1]); + $this->assertEquals($tag2->isUserAssignable(), $tagUpdated[2]); + } + + /** + * @dataProvider updateTagProvider + * @expectedException \OCP\SystemTag\TagAlreadyExistsException + */ + public function testUpdateTagDuplicate($tagCreate, $tagUpdated) { + $this->tagManager->createTag( + $tagCreate[0], + $tagCreate[1], + $tagCreate[2] + ); + $tag2 = $this->tagManager->createTag( + $tagUpdated[0], + $tagUpdated[1], + $tagUpdated[2] + ); + + // update to match the first tag + $this->tagManager->updateTag( + $tag2->getId(), + $tagCreate[0], + $tagCreate[1], + $tagCreate[2] + ); + } + + public function testDeleteTags() { + $tag1 = $this->tagManager->createTag('one', true, false); + $tag2 = $this->tagManager->createTag('two', false, true); + + $this->tagManager->deleteTags([$tag1->getId(), $tag2->getId()]); + + $this->assertEmpty($this->tagManager->getAllTags()); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testDeleteNonExistingTag() { + $this->tagManager->deleteTags([100]); + } + + public function testDeleteTagRemovesRelations() { + $tag1 = $this->tagManager->createTag('one', true, false); + $tag2 = $this->tagManager->createTag('two', true, true); + + $tagMapper = new SystemTagObjectMapper($this->connection, $this->tagManager); + + $tagMapper->assignTags(1, 'testtype', $tag1->getId()); + $tagMapper->assignTags(1, 'testtype', $tag2->getId()); + $tagMapper->assignTags(2, 'testtype', $tag1->getId()); + + $this->tagManager->deleteTags($tag1->getId()); + + $tagIdMapping = $tagMapper->getTagIdsForObjects( + [1, 2], + 'testtype' + ); + + $this->assertEquals([ + 1 => [$tag2->getId()], + 2 => [], + ], $tagIdMapping); + } + + /** + * @param ISystemTag $tag1 + * @param ISystemTag $tag2 + */ + private function assertSameTag($tag1, $tag2) { + $this->assertEquals($tag1->getId(), $tag2->getId()); + $this->assertEquals($tag1->getName(), $tag2->getName()); + $this->assertEquals($tag1->isUserVisible(), $tag2->isUserVisible()); + $this->assertEquals($tag1->isUserAssignable(), $tag2->isUserAssignable()); + } + +} diff --git a/tests/lib/systemtag/systemtagobjectmappertest.php b/tests/lib/systemtag/systemtagobjectmappertest.php new file mode 100644 index 00000000000..a4312fa722d --- /dev/null +++ b/tests/lib/systemtag/systemtagobjectmappertest.php @@ -0,0 +1,335 @@ +<?php + +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * +*/ + +namespace Test\SystemTag; + +use OC\SystemTag\SystemTagManager; +use OC\SystemTag\SystemTagObjectMapper; +use \OCP\SystemTag\ISystemTag; +use \OCP\SystemTag\ISystemTagManager; +use \OCP\SystemTag\ISystemTagObjectMapper; +use \OCP\SystemTag\TagNotFoundException; +use \OCP\IDBConnection; +use \OC\SystemTag\SystemTag; +use Test\TestCase; + +/** + * Class TestSystemTagObjectMapper + * + * @group DB + * @package Test\SystemTag + */ +class SystemTagObjectMapperTest extends TestCase { + + /** + * @var ISystemTagManager + **/ + private $tagManager; + + /** + * @var ISystemTagObjectMapper + **/ + private $tagMapper; + + /** + * @var IDBConnection + */ + private $connection; + + /** + * @var ISystemTag + */ + private $tag1; + + /** + * @var ISystemTag + */ + private $tag2; + + /** + * @var ISystemTag + */ + private $tag3; + + public function setUp() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + + $this->tagManager = $this->getMockBuilder('OCP\SystemTag\ISystemTagManager') + ->getMock(); + + $this->tagMapper = new SystemTagObjectMapper($this->connection, $this->tagManager); + + $this->tag1 = new SystemTag(1, 'testtag1', false, false); + $this->tag2 = new SystemTag(2, 'testtag2', true, false); + $this->tag3 = new SystemTag(3, 'testtag3', false, false); + + $this->tagManager->expects($this->any()) + ->method('getTagsById') + ->will($this->returnCallback(function($tagIds) { + $result = []; + if (in_array(1, $tagIds)) { + $result[1] = $this->tag1; + } + if (in_array(2, $tagIds)) { + $result[2] = $this->tag2; + } + if (in_array(3, $tagIds)) { + $result[3] = $this->tag3; + } + return $result; + })); + + $this->tagMapper->assignTags(1, 'testtype', $this->tag1->getId()); + $this->tagMapper->assignTags(1, 'testtype', $this->tag2->getId()); + $this->tagMapper->assignTags(2, 'testtype', $this->tag1->getId()); + $this->tagMapper->assignTags(3, 'anothertype', $this->tag1->getId()); + } + + public function tearDown() { + $query = $this->connection->getQueryBuilder(); + $query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute(); + $query->delete(SystemTagManager::TAG_TABLE)->execute(); + } + + public function testGetTagsForObjects() { + $tagIdMapping = $this->tagMapper->getTagIdsForObjects( + [1, 2, 3, 4], + 'testtype' + ); + + $this->assertEquals([ + 1 => [$this->tag1->getId(), $this->tag2->getId()], + 2 => [$this->tag1->getId()], + 3 => [], + 4 => [], + ], $tagIdMapping); + } + + public function testGetObjectsForTags() { + $objectIds = $this->tagMapper->getObjectIdsForTags( + [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()], + 'testtype' + ); + + $this->assertEquals([ + 1, + 2, + ], $objectIds); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testGetObjectsForNonExistingTag() { + $this->tagMapper->getObjectIdsForTags( + [100], + 'testtype' + ); + } + + public function testAssignUnassignTags() { + $this->tagMapper->unassignTags(1, 'testtype', [$this->tag1->getId()]); + + $tagIdMapping = $this->tagMapper->getTagIdsForObjects(1, 'testtype'); + $this->assertEquals([ + 1 => [$this->tag2->getId()], + ], $tagIdMapping); + + $this->tagMapper->assignTags(1, 'testtype', [$this->tag1->getId()]); + $this->tagMapper->assignTags(1, 'testtype', $this->tag3->getId()); + + $tagIdMapping = $this->tagMapper->getTagIdsForObjects(1, 'testtype'); + + $this->assertEquals([ + 1 => [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()], + ], $tagIdMapping); + } + + public function testReAssignUnassignTags() { + // reassign tag1 + $this->tagMapper->assignTags(1, 'testtype', [$this->tag1->getId()]); + + // tag 3 was never assigned + $this->tagMapper->unassignTags(1, 'testtype', [$this->tag3->getId()]); + + $this->assertTrue(true, 'No error when reassigning/unassigning'); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testAssignNonExistingTags() { + $this->tagMapper->assignTags(1, 'testtype', [100]); + } + + public function testAssignNonExistingTagInArray() { + $caught = false; + try { + $this->tagMapper->assignTags(1, 'testtype', [100, $this->tag3->getId()]); + } catch (TagNotFoundException $e) { + $caught = true; + } + + $this->assertTrue($caught, 'Exception thrown'); + + $tagIdMapping = $this->tagMapper->getTagIdsForObjects( + [1], + 'testtype' + ); + + $this->assertEquals([ + 1 => [$this->tag1->getId(), $this->tag2->getId()], + ], $tagIdMapping, 'None of the tags got assigned'); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testUnassignNonExistingTags() { + $this->tagMapper->unassignTags(1, 'testtype', [100]); + } + + public function testUnassignNonExistingTagsInArray() { + $caught = false; + try { + $this->tagMapper->unassignTags(1, 'testtype', [100, $this->tag1->getId()]); + } catch (TagNotFoundException $e) { + $caught = true; + } + + $this->assertTrue($caught, 'Exception thrown'); + + $tagIdMapping = $this->tagMapper->getTagIdsForObjects( + [1], + 'testtype' + ); + + $this->assertEquals([ + 1 => [$this->tag1->getId(), $this->tag2->getId()], + ], $tagIdMapping, 'None of the tags got unassigned'); + } + + public function testHaveTagAllMatches() { + $this->assertTrue( + $this->tagMapper->haveTag( + [1], + 'testtype', + $this->tag1->getId(), + true + ), + 'object 1 has the tag tag1' + ); + + $this->assertTrue( + $this->tagMapper->haveTag( + [1, 2], + 'testtype', + $this->tag1->getId(), + true + ), + 'object 1 and object 2 ALL have the tag tag1' + ); + + $this->assertFalse( + $this->tagMapper->haveTag( + [1, 2], + 'testtype', + $this->tag2->getId(), + true + ), + 'object 1 has tag2 but not object 2, so not ALL of them' + ); + + $this->assertFalse( + $this->tagMapper->haveTag( + [2], + 'testtype', + $this->tag2->getId(), + true + ), + 'object 2 does not have tag2' + ); + + $this->assertFalse( + $this->tagMapper->haveTag( + [3], + 'testtype', + $this->tag2->getId(), + true + ), + 'object 3 does not have tag1 due to different type' + ); + } + + public function testHaveTagAtLeastOneMatch() { + $this->assertTrue( + $this->tagMapper->haveTag( + [1], + 'testtype', + $this->tag1->getId(), + false + ), + 'object1 has the tag tag1' + ); + + $this->assertTrue( + $this->tagMapper->haveTag( + [1, 2], + 'testtype', + $this->tag1->getId(), + false + ), + 'object 1 and object 2 both the tag tag1' + ); + + $this->assertTrue( + $this->tagMapper->haveTag( + [1, 2], + 'testtype', + $this->tag2->getId(), + false + ), + 'at least object 1 has the tag tag2' + ); + + $this->assertFalse( + $this->tagMapper->haveTag( + [2], + 'testtype', + $this->tag2->getId(), + false + ), + 'object 2 does not have tag2' + ); + + $this->assertFalse( + $this->tagMapper->haveTag( + [3], + 'testtype', + $this->tag2->getId(), + false + ), + 'object 3 does not have tag1 due to different type' + ); + } + + /** + * @expectedException \OCP\SystemTag\TagNotFoundException + */ + public function testHaveTagNonExisting() { + $this->tagMapper->haveTag( + [1], + 'testtype', + 100 + ); + } +} |