diff options
Diffstat (limited to 'tests/lib/SystemTag')
-rw-r--r-- | tests/lib/SystemTag/SystemTagManagerTest.php | 277 | ||||
-rw-r--r-- | tests/lib/SystemTag/SystemTagObjectMapperTest.php | 46 |
2 files changed, 188 insertions, 135 deletions
diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index 5029eaa7df6..b443d7222b2 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -11,11 +11,16 @@ namespace Test\SystemTag; use OC\SystemTag\SystemTagManager; use OC\SystemTag\SystemTagObjectMapper; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IAppConfig; use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IUser; +use OCP\IUserSession; +use OCP\Server; use OCP\SystemTag\ISystemTag; use OCP\SystemTag\ISystemTagManager; +use OCP\SystemTag\TagAlreadyExistsException; +use OCP\SystemTag\TagNotFoundException; use Test\TestCase; /** @@ -25,44 +30,36 @@ use Test\TestCase; * @package Test\SystemTag */ class SystemTagManagerTest extends TestCase { - /** - * @var ISystemTagManager - **/ - private $tagManager; - - /** - * @var IDBConnection - */ - private $connection; - - /** - * @var IGroupManager - */ - private $groupManager; - - /** - * @var IEventDispatcher - */ - private $dispatcher; + private ISystemTagManager $tagManager; + private IDBConnection $connection; + private IGroupManager $groupManager; + private IUserSession $userSession; + private IAppConfig $appConfig; + private IEventDispatcher $dispatcher; protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->groupManager = $this->createMock(IGroupManager::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->appConfig = $this->createMock(IAppConfig::class); $this->tagManager = new SystemTagManager( $this->connection, $this->groupManager, - $this->dispatcher + $this->dispatcher, + $this->userSession, + $this->appConfig, ); $this->pruneTagsTables(); } protected function tearDown(): void { $this->pruneTagsTables(); + \OC::$CLI = true; parent::tearDown(); } @@ -72,7 +69,7 @@ class SystemTagManagerTest extends TestCase { $query->delete(SystemTagManager::TAG_TABLE)->execute(); } - public function getAllTagsDataProvider() { + public static function getAllTagsDataProvider(): array { return [ [ // no tags at all @@ -85,24 +82,11 @@ class SystemTagManagerTest extends TestCase { ['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) { + #[\PHPUnit\Framework\Attributes\DataProvider('getAllTagsDataProvider')] + public function testGetAllTags($testTags): void { $testTagsById = []; foreach ($testTags as $testTag) { $tag = $this->tagManager->createTag($testTag[0], $testTag[1], $testTag[2]); @@ -119,7 +103,7 @@ class SystemTagManagerTest extends TestCase { } } - public function getAllTagsFilteredDataProvider() { + public static function getAllTagsFilteredDataProvider(): array { return [ [ [ @@ -168,14 +152,14 @@ class SystemTagManagerTest extends TestCase { [ [ ['one', true, false], - ['one', false, false], + ['one_different', false, false], ['two', true, false], ], null, 'on', [ ['one', true, false], - ['one', false, false], + ['one_different', false, false], ] ], // filter by name pattern and visibility @@ -184,7 +168,7 @@ class SystemTagManagerTest extends TestCase { [ ['one', true, false], ['two', true, false], - ['one', false, false], + ['one_different', false, false], ], true, 'on', @@ -208,10 +192,8 @@ class SystemTagManagerTest extends TestCase { ]; } - /** - * @dataProvider getAllTagsFilteredDataProvider - */ - public function testGetAllTagsFiltered($testTags, $visibilityFilter, $nameSearch, $expectedResults) { + #[\PHPUnit\Framework\Attributes\DataProvider('getAllTagsFilteredDataProvider')] + public function testGetAllTagsFiltered($testTags, $visibilityFilter, $nameSearch, $expectedResults): void { foreach ($testTags as $testTag) { $this->tagManager->createTag($testTag[0], $testTag[1], $testTag[2]); } @@ -232,7 +214,7 @@ class SystemTagManagerTest extends TestCase { } } - public function oneTagMultipleFlagsProvider() { + public static function oneTagMultipleFlagsProvider(): array { return [ ['one', false, false], ['one', true, false], @@ -241,11 +223,9 @@ class SystemTagManagerTest extends TestCase { ]; } - /** - * @dataProvider oneTagMultipleFlagsProvider - */ - public function testCreateDuplicate($name, $userVisible, $userAssignable) { - $this->expectException(\OCP\SystemTag\TagAlreadyExistsException::class); + #[\PHPUnit\Framework\Attributes\DataProvider('oneTagMultipleFlagsProvider')] + public function testCreateDuplicate($name, $userVisible, $userAssignable): void { + $this->expectException(TagAlreadyExistsException::class); try { $this->tagManager->createTag($name, $userVisible, $userAssignable); @@ -255,22 +235,29 @@ class SystemTagManagerTest extends TestCase { $this->tagManager->createTag($name, $userVisible, $userAssignable); } - public function testCreateOverlongName() { + public function testCreateDuplicateWithDifferentFlags(): void { + $this->expectException(TagAlreadyExistsException::class); + + // Create a tag with specific flags + $this->tagManager->createTag('duplicate', true, false); + // Try to create a tag with the same name but different flags - should fail + $this->tagManager->createTag('duplicate', false, true); + } + + public function testCreateOverlongName(): void { $tag = $this->tagManager->createTag('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas, Salão de Física, Torre Sineira, Paço Velho e Jardim Botânico)', true, true); $this->assertSame('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas', $tag->getName()); // 63 characters but 64 bytes due to "á" } - /** - * @dataProvider oneTagMultipleFlagsProvider - */ - public function testGetExistingTag($name, $userVisible, $userAssignable) { + #[\PHPUnit\Framework\Attributes\DataProvider('oneTagMultipleFlagsProvider')] + public function testGetExistingTag($name, $userVisible, $userAssignable): void { $tag1 = $this->tagManager->createTag($name, $userVisible, $userAssignable); $tag2 = $this->tagManager->getTag($name, $userVisible, $userAssignable); $this->assertSameTag($tag1, $tag2); } - public function testGetExistingTagById() { + public function testGetExistingTagById(): void { $tag1 = $this->tagManager->createTag('one', true, false); $tag2 = $this->tagManager->createTag('two', false, true); @@ -283,107 +270,101 @@ class SystemTagManagerTest extends TestCase { } - public function testGetNonExistingTag() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testGetNonExistingTag(): void { + $this->expectException(TagNotFoundException::class); $this->tagManager->getTag('nonexist', false, false); } - public function testGetNonExistingTagsById() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testGetNonExistingTagsById(): void { + $this->expectException(TagNotFoundException::class); $tag1 = $this->tagManager->createTag('one', true, false); $this->tagManager->getTagsByIds([$tag1->getId(), 100, 101]); } - public function testGetInvalidTagIdFormat() { + public function testGetInvalidTagIdFormat(): void { $this->expectException(\InvalidArgumentException::class); $tag1 = $this->tagManager->createTag('one', true, false); $this->tagManager->getTagsByIds([$tag1->getId() . 'suffix']); } - public function updateTagProvider() { + public static function updateTagProvider(): array { return [ [ // update name - ['one', true, true], - ['two', true, true] + ['one', true, true, '0082c9'], + ['two', true, true, '0082c9'] ], [ // update one flag - ['one', false, true], - ['one', true, true] + ['one', false, true, null], + ['one', true, true, '0082c9'] ], [ // update all flags - ['one', false, false], - ['one', true, true] + ['one', false, false, '0082c9'], + ['one', true, true, null] ], [ // update all - ['one', false, false], - ['two', true, true] + ['one', false, false, '0082c9'], + ['two', true, true, '0082c9'] ], ]; } - /** - * @dataProvider updateTagProvider - */ - public function testUpdateTag($tagCreate, $tagUpdated) { + #[\PHPUnit\Framework\Attributes\DataProvider('updateTagProvider')] + public function testUpdateTag($tagCreate, $tagUpdated): void { $tag1 = $this->tagManager->createTag( $tagCreate[0], $tagCreate[1], - $tagCreate[2] + $tagCreate[2], + $tagCreate[3], ); $this->tagManager->updateTag( $tag1->getId(), $tagUpdated[0], $tagUpdated[1], - $tagUpdated[2] + $tagUpdated[2], + $tagUpdated[3], ); $tag2 = $this->tagManager->getTag( $tagUpdated[0], $tagUpdated[1], - $tagUpdated[2] + $tagUpdated[2], + $tagUpdated[3], ); $this->assertEquals($tag2->getId(), $tag1->getId()); $this->assertEquals($tag2->getName(), $tagUpdated[0]); $this->assertEquals($tag2->isUserVisible(), $tagUpdated[1]); $this->assertEquals($tag2->isUserAssignable(), $tagUpdated[2]); + $this->assertEquals($tag2->getColor(), $tagUpdated[3]); + } - /** - * @dataProvider updateTagProvider - */ - public function testUpdateTagDuplicate($tagCreate, $tagUpdated) { - $this->expectException(\OCP\SystemTag\TagAlreadyExistsException::class); + public function testUpdateTagToExistingName(): void { + $this->expectException(TagAlreadyExistsException::class); - $this->tagManager->createTag( - $tagCreate[0], - $tagCreate[1], - $tagCreate[2] - ); - $tag2 = $this->tagManager->createTag( - $tagUpdated[0], - $tagUpdated[1], - $tagUpdated[2] - ); + // Create two different tags + $tag1 = $this->tagManager->createTag('first', true, true); + $tag2 = $this->tagManager->createTag('second', false, false); - // update to match the first tag + // Try to update tag2 to have the same name as tag1 - should fail $this->tagManager->updateTag( $tag2->getId(), - $tagCreate[0], - $tagCreate[1], - $tagCreate[2] + 'first', + false, + false, + null ); } - public function testDeleteTags() { + public function testDeleteTags(): void { $tag1 = $this->tagManager->createTag('one', true, false); $tag2 = $this->tagManager->createTag('two', false, true); @@ -393,13 +374,13 @@ class SystemTagManagerTest extends TestCase { } - public function testDeleteNonExistingTag() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testDeleteNonExistingTag(): void { + $this->expectException(TagNotFoundException::class); $this->tagManager->deleteTags([100]); } - public function testDeleteTagRemovesRelations() { + public function testDeleteTagRemovesRelations(): void { $tag1 = $this->tagManager->createTag('one', true, false); $tag2 = $this->tagManager->createTag('two', true, true); @@ -422,7 +403,7 @@ class SystemTagManagerTest extends TestCase { ], $tagIdMapping); } - public function visibilityCheckProvider() { + public static function visibilityCheckProvider(): array { return [ [false, false, false, false], [true, false, false, true], @@ -431,10 +412,8 @@ class SystemTagManagerTest extends TestCase { ]; } - /** - * @dataProvider visibilityCheckProvider - */ - public function testVisibilityCheck($userVisible, $userAssignable, $isAdmin, $expectedResult) { + #[\PHPUnit\Framework\Attributes\DataProvider('visibilityCheckProvider')] + public function testVisibilityCheck($userVisible, $userAssignable, $isAdmin, $expectedResult): void { $user = $this->getMockBuilder(IUser::class)->getMock(); $user->expects($this->any()) ->method('getUID') @@ -449,7 +428,7 @@ class SystemTagManagerTest extends TestCase { $this->assertEquals($expectedResult, $this->tagManager->canUserSeeTag($tag1, $user)); } - public function assignabilityCheckProvider() { + public static function assignabilityCheckProvider(): array { return [ // no groups [false, false, false, false], @@ -478,10 +457,8 @@ class SystemTagManagerTest extends TestCase { ]; } - /** - * @dataProvider assignabilityCheckProvider - */ - public function testAssignabilityCheck($userVisible, $userAssignable, $isAdmin, $expectedResult, $userGroupIds = [], $tagGroupIds = []) { + #[\PHPUnit\Framework\Attributes\DataProvider('assignabilityCheckProvider')] + public function testAssignabilityCheck($userVisible, $userAssignable, $isAdmin, $expectedResult, $userGroupIds = [], $tagGroupIds = []): void { $user = $this->getMockBuilder(IUser::class)->getMock(); $user->expects($this->any()) ->method('getUID') @@ -501,7 +478,7 @@ class SystemTagManagerTest extends TestCase { $this->assertEquals($expectedResult, $this->tagManager->canUserAssignTag($tag1, $user)); } - public function testTagGroups() { + public function testTagGroups(): void { $tag1 = $this->tagManager->createTag('tag1', true, false); $tag2 = $this->tagManager->createTag('tag2', true, false); $this->tagManager->setTagGroups($tag1, ['group1', 'group2']); @@ -521,12 +498,86 @@ class SystemTagManagerTest extends TestCase { /** * empty groupIds should be ignored */ - public function testEmptyTagGroup() { + public function testEmptyTagGroup(): void { $tag1 = $this->tagManager->createTag('tag1', true, false); $this->tagManager->setTagGroups($tag1, ['']); $this->assertEquals([], $this->tagManager->getTagGroups($tag1)); } + public static function allowedToCreateProvider(): array { + return [ + [true, null, true], + [true, null, false], + [false, true, true], + [false, true, false], + [false, false, false], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('allowedToCreateProvider')] + public function testAllowedToCreateTag(bool $isCli, ?bool $isAdmin, bool $isRestricted): void { + $oldCli = \OC::$CLI; + \OC::$CLI = $isCli; + + $user = $this->getMockBuilder(IUser::class)->getMock(); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('test'); + $this->userSession->expects($this->any()) + ->method('getUser') + ->willReturn($isAdmin === null ? null : $user); + $this->groupManager->expects($this->any()) + ->method('isAdmin') + ->with('test') + ->willReturn($isAdmin); + $this->appConfig->expects($this->any()) + ->method('getValueBool') + ->with('systemtags', 'restrict_creation_to_admin') + ->willReturn($isRestricted); + + $name = uniqid('tag_', true); + $tag = $this->tagManager->createTag($name, true, true); + $this->assertEquals($tag->getName(), $name); + $this->tagManager->deleteTags($tag->getId()); + + \OC::$CLI = $oldCli; + } + + public static function disallowedToCreateProvider(): array { + return [ + [false], + [null], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('disallowedToCreateProvider')] + public function testDisallowedToCreateTag(?bool $isAdmin): void { + $oldCli = \OC::$CLI; + \OC::$CLI = false; + + $user = $this->getMockBuilder(IUser::class)->getMock(); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('test'); + $this->userSession->expects($this->any()) + ->method('getUser') + ->willReturn($isAdmin === null ? null : $user); + $this->groupManager->expects($this->any()) + ->method('isAdmin') + ->with('test') + ->willReturn($isAdmin); + $this->appConfig->expects($this->any()) + ->method('getValueBool') + ->with('systemtags', 'restrict_creation_to_admin') + ->willReturn(true); + + $this->expectException(\Exception::class); + $tag = $this->tagManager->createTag(uniqid('tag_', true), true, true); + + \OC::$CLI = $oldCli; + } + + /** * @param ISystemTag $tag1 * @param ISystemTag $tag2 diff --git a/tests/lib/SystemTag/SystemTagObjectMapperTest.php b/tests/lib/SystemTag/SystemTagObjectMapperTest.php index c9aff9f5183..a43bda3b659 100644 --- a/tests/lib/SystemTag/SystemTagObjectMapperTest.php +++ b/tests/lib/SystemTag/SystemTagObjectMapperTest.php @@ -13,6 +13,7 @@ use OC\SystemTag\SystemTagManager; use OC\SystemTag\SystemTagObjectMapper; use OCP\EventDispatcher\IEventDispatcher; use OCP\IDBConnection; +use OCP\Server; use OCP\SystemTag\ISystemTag; use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagObjectMapper; @@ -64,7 +65,7 @@ class SystemTagObjectMapperTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); $this->pruneTagsTables(); $this->tagManager = $this->createMock(ISystemTagManager::class); @@ -113,7 +114,7 @@ class SystemTagObjectMapperTest extends TestCase { $query->delete(SystemTagManager::TAG_TABLE)->execute(); } - public function testGetTagIdsForObjects() { + public function testGetTagIdsForObjects(): void { $tagIdMapping = $this->tagMapper->getTagIdsForObjects( ['1', '2', '3', '4'], 'testtype' @@ -127,7 +128,7 @@ class SystemTagObjectMapperTest extends TestCase { ], $tagIdMapping); } - public function testGetTagIdsForNoObjects() { + public function testGetTagIdsForNoObjects(): void { $tagIdMapping = $this->tagMapper->getTagIdsForObjects( [], 'testtype' @@ -136,7 +137,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertEquals([], $tagIdMapping); } - public function testGetTagIdsForALotOfObjects() { + public function testGetTagIdsForALotOfObjects(): void { $ids = range(1, 10500); $tagIdMapping = $this->tagMapper->getTagIdsForObjects( $ids, @@ -147,11 +148,12 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertEquals([$this->tag1->getId(), $this->tag2->getId()], $tagIdMapping[1]); } - public function testGetObjectsForTags() { + public function testGetObjectsForTags(): void { $objectIds = $this->tagMapper->getObjectIdsForTags( [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()], 'testtype' ); + sort($objectIds); $this->assertEquals([ '1', @@ -159,7 +161,7 @@ class SystemTagObjectMapperTest extends TestCase { ], $objectIds); } - public function testGetObjectsForTagsLimit() { + public function testGetObjectsForTagsLimit(): void { $objectIds = $this->tagMapper->getObjectIdsForTags( [$this->tag1->getId()], 'testtype', @@ -172,7 +174,7 @@ class SystemTagObjectMapperTest extends TestCase { } - public function testGetObjectsForTagsLimitWithMultipleTags() { + public function testGetObjectsForTagsLimitWithMultipleTags(): void { $this->expectException(\InvalidArgumentException::class); $this->tagMapper->getObjectIdsForTags( @@ -182,7 +184,7 @@ class SystemTagObjectMapperTest extends TestCase { ); } - public function testGetObjectsForTagsLimitOffset() { + public function testGetObjectsForTagsLimitOffset(): void { $objectIds = $this->tagMapper->getObjectIdsForTags( [$this->tag1->getId()], 'testtype', @@ -196,8 +198,8 @@ class SystemTagObjectMapperTest extends TestCase { } - public function testGetObjectsForNonExistingTag() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testGetObjectsForNonExistingTag(): void { + $this->expectException(TagNotFoundException::class); $this->tagMapper->getObjectIdsForTags( [100], @@ -205,7 +207,7 @@ class SystemTagObjectMapperTest extends TestCase { ); } - public function testAssignUnassignTags() { + public function testAssignUnassignTags(): void { $this->tagMapper->unassignTags('1', 'testtype', [$this->tag1->getId()]); $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype'); @@ -223,7 +225,7 @@ class SystemTagObjectMapperTest extends TestCase { ], $tagIdMapping); } - public function testReAssignUnassignTags() { + public function testReAssignUnassignTags(): void { // reassign tag1 $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]); @@ -234,13 +236,13 @@ class SystemTagObjectMapperTest extends TestCase { } - public function testAssignNonExistingTags() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testAssignNonExistingTags(): void { + $this->expectException(TagNotFoundException::class); $this->tagMapper->assignTags('1', 'testtype', [100]); } - public function testAssignNonExistingTagInArray() { + public function testAssignNonExistingTagInArray(): void { $caught = false; try { $this->tagMapper->assignTags('1', 'testtype', [100, $this->tag3->getId()]); @@ -261,13 +263,13 @@ class SystemTagObjectMapperTest extends TestCase { } - public function testUnassignNonExistingTags() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testUnassignNonExistingTags(): void { + $this->expectException(TagNotFoundException::class); $this->tagMapper->unassignTags('1', 'testtype', [100]); } - public function testUnassignNonExistingTagsInArray() { + public function testUnassignNonExistingTagsInArray(): void { $caught = false; try { $this->tagMapper->unassignTags('1', 'testtype', [100, $this->tag1->getId()]); @@ -287,7 +289,7 @@ class SystemTagObjectMapperTest extends TestCase { ], $tagIdMapping, 'None of the tags got unassigned'); } - public function testHaveTagAllMatches() { + public function testHaveTagAllMatches(): void { $this->assertTrue( $this->tagMapper->haveTag( ['1'], @@ -339,7 +341,7 @@ class SystemTagObjectMapperTest extends TestCase { ); } - public function testHaveTagAtLeastOneMatch() { + public function testHaveTagAtLeastOneMatch(): void { $this->assertTrue( $this->tagMapper->haveTag( ['1'], @@ -392,8 +394,8 @@ class SystemTagObjectMapperTest extends TestCase { } - public function testHaveTagNonExisting() { - $this->expectException(\OCP\SystemTag\TagNotFoundException::class); + public function testHaveTagNonExisting(): void { + $this->expectException(TagNotFoundException::class); $this->tagMapper->haveTag( ['1'], |