From 571974c91e1918fd86419f922bc36ae5a9225af1 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Mon, 4 Sep 2023 10:29:48 +0200 Subject: [PATCH] [Tests] Added test coverage --- .../lib/Permission/LimitationResolverTest.php | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 tests/lib/Permission/LimitationResolverTest.php diff --git a/tests/lib/Permission/LimitationResolverTest.php b/tests/lib/Permission/LimitationResolverTest.php new file mode 100644 index 0000000000..55e0be99d3 --- /dev/null +++ b/tests/lib/Permission/LimitationResolverTest.php @@ -0,0 +1,223 @@ +contentService = $this->createMock(ContentService::class); + $this->contentTypeService = $this->createMock(ContentTypeService::class); + $this->languageService = $this->createMock(LanguageService::class); + $this->locationService = $this->createMock(LocationService::class); + $this->lookupLimitationsTransformer = new LookupLimitationsTransformer(); + $this->permissionResolver = $this->createMock(PermissionResolver::class); + + $this->limitationResolver = new LimitationResolver( + $this->contentService, + $this->contentTypeService, + $this->languageService, + $this->locationService, + $this->lookupLimitationsTransformer, + $this->permissionResolver + ); + } + + /** + * @dataProvider provideDataForTestGetLanguageLimitations + * + * @param array $expected + */ + public function testGetLanguageLimitations( + array $expected, + VersionInfo $versionInfo, + Location $location + ): void { + $this->mockPermissionResolverLookupLimitations( + [ + 'eng-GB', + 'ger-DE', + ], + $versionInfo->getContentInfo(), + $location, + new LookupLimitationResult(true) + ); + self::assertEquals( + $expected, + $this->limitationResolver->getLanguageLimitations( + $versionInfo, + $location + ) + ); + } + + /** + * @return iterable, + * VersionInfo, + * Location, + * }> + */ + public function provideDataForTestGetLanguageLimitations(): iterable + { + $english = $this->createLanguage(1, true, 'eng-GB', 'English'); + $german = $this->createLanguage(2, true, 'ger-DE', 'German'); + $french = $this->createLanguage(3, false, 'fra-FR', 'French'); + + yield 'Has access to all enabled languages' => [ + [ + $this->getLanguageAccessData(true, $english), + $this->getLanguageAccessData(true, $german), + $this->getLanguageAccessData(false, $french), + ], + $this->createVersionInfo( + $this->createContentInfo(), + [ + $english, + $german, + $french, + ] + ), + $this->createLocation(), + ]; + } + + private function createContentInfo(): ContentInfo + { + return $this->createMock(ContentInfo::class); + } + + /** + * @param iterable<\Ibexa\Contracts\Core\Repository\Values\Content\Language> $languages + */ + private function createVersionInfo( + ContentInfo $contentInfo, + iterable $languages + ): VersionInfo { + $versionInfo = $this->createMock(VersionInfo::class); + $versionInfo + ->expects(self::atLeastOnce()) + ->method('getContentInfo') + ->willReturn($contentInfo); + $versionInfo + ->expects(self::atLeastOnce()) + ->method('getLanguages') + ->willReturn($languages); + + return $versionInfo; + } + + private function createLocation(): Location + { + return $this->createMock(Location::class); + } + + private function createLanguage( + int $id, + bool $enabled, + string $languageCode, + string $name + ): Language { + return new Language( + [ + 'id' => $id, + 'enabled' => $enabled, + 'languageCode' => $languageCode, + 'name' => $name, + ] + ); + } + + /** + * @return array{ + * languageCode: string, + * name: string, + * hasAccess: bool, + * } + */ + private function getLanguageAccessData( + bool $hasAccess, + Language $language + ): array { + return [ + 'languageCode' => $language->getLanguageCode(), + 'name' => $language->getName(), + 'hasAccess' => $hasAccess, + ]; + } + + /** + * @param array $languageCodes + */ + private function mockPermissionResolverLookupLimitations( + array $languageCodes, + ContentInfo $contentInfo, + Location $location, + LookupLimitationResult $lookupLimitationResult + ): void { + $this->permissionResolver + ->expects(self::once()) + ->method('lookupLimitations') + ->with( + 'content', + 'edit', + $contentInfo, + [ + (new VersionBuilder())->translateToAnyLanguageOf($languageCodes)->build(), + $location, + ], + [Limitation::LANGUAGE], + ) + ->willReturn($lookupLimitationResult); + } +}