-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
321 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,315 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\AdminUi\Permission; | ||
|
||
use Ibexa\AdminUi\Permission\LimitationResolver; | ||
use Ibexa\AdminUi\Permission\LimitationResolverInterface; | ||
use Ibexa\AdminUi\Permission\LookupLimitationsTransformer; | ||
use Ibexa\Contracts\Core\Limitation\Target\Builder\VersionBuilder; | ||
use Ibexa\Contracts\Core\Repository\ContentService; | ||
use Ibexa\Contracts\Core\Repository\ContentTypeService; | ||
use Ibexa\Contracts\Core\Repository\LanguageService; | ||
use Ibexa\Contracts\Core\Repository\LocationService; | ||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Language; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Location; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
use Ibexa\Contracts\Core\Repository\Values\User\Limitation; | ||
use Ibexa\Contracts\Core\Repository\Values\User\LookupLimitationResult; | ||
use Ibexa\Contracts\Core\Repository\Values\User\LookupPolicyLimitations; | ||
use Ibexa\Contracts\Core\Repository\Values\User\Policy; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Ibexa\AdminUi\Permission\LimitationResolver | ||
*/ | ||
final class LimitationResolverTest extends TestCase | ||
{ | ||
/** @var \Ibexa\Contracts\Core\Repository\ContentService&\PHPUnit\Framework\MockObject\MockObject */ | ||
private ContentService $contentService; | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService&\PHPUnit\Framework\MockObject\MockObject */ | ||
private ContentTypeService $contentTypeService; | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\LanguageService&\PHPUnit\Framework\MockObject\MockObject */ | ||
private LanguageService $languageService; | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\LocationService&\PHPUnit\Framework\MockObject\MockObject */ | ||
private LocationService $locationService; | ||
|
||
private LookupLimitationsTransformer $lookupLimitationsTransformer; | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\PermissionResolver&\PHPUnit\Framework\MockObject\MockObject */ | ||
private PermissionResolver $permissionResolver; | ||
|
||
private LimitationResolverInterface $limitationResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->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<array{ | ||
* languageCode: string, | ||
* name: string, | ||
* hasAccess: bool, | ||
* }> $expected | ||
*/ | ||
public function testGetLanguageLimitations( | ||
array $expected, | ||
VersionInfo $versionInfo, | ||
Location $location, | ||
LookupLimitationResult $lookupLimitationResult | ||
): void { | ||
$this->mockPermissionResolverLookupLimitations( | ||
[ | ||
'eng-GB', | ||
'ger-DE', | ||
], | ||
$versionInfo->getContentInfo(), | ||
$location, | ||
$lookupLimitationResult | ||
); | ||
self::assertEquals( | ||
$expected, | ||
$this->limitationResolver->getLanguageLimitations( | ||
$versionInfo, | ||
$location | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* array<array{ | ||
* languageCode: string, | ||
* name: string, | ||
* hasAccess: bool, | ||
* }>, | ||
* \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo, | ||
* \Ibexa\Contracts\Core\Repository\Values\Content\Location, | ||
* \Ibexa\Contracts\Core\Repository\Values\User\LookupLimitationResult, | ||
* }> | ||
*/ | ||
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'); | ||
$versionInfo = $this->createVersionInfo( | ||
$this->createContentInfo(), | ||
[ | ||
$english, | ||
$german, | ||
$french, | ||
] | ||
); | ||
$location = $this->createLocation(); | ||
|
||
yield 'No access to all languages' => [ | ||
[ | ||
$this->getLanguageAccessData(false, $english), | ||
$this->getLanguageAccessData(false, $german), | ||
$this->getLanguageAccessData(false, $french), | ||
], | ||
$versionInfo, | ||
$location, | ||
new LookupLimitationResult(false), | ||
]; | ||
|
||
yield 'Has access to all enabled languages' => [ | ||
[ | ||
$this->getLanguageAccessData(true, $english), | ||
$this->getLanguageAccessData(true, $german), | ||
$this->getLanguageAccessData(false, $french), | ||
], | ||
$versionInfo, | ||
$location, | ||
new LookupLimitationResult(true), | ||
]; | ||
|
||
yield 'Has limited access to English language by policy limitation' => [ | ||
[ | ||
$this->getLanguageAccessData(true, $english), | ||
$this->getLanguageAccessData(false, $german), | ||
$this->getLanguageAccessData(false, $french), | ||
], | ||
$versionInfo, | ||
$location, | ||
new LookupLimitationResult( | ||
true, | ||
[], | ||
[ | ||
new LookupPolicyLimitations( | ||
$this->createMock(Policy::class), | ||
[ | ||
$this->createLanguageLimitation(['eng-GB']), | ||
] | ||
), | ||
] | ||
), | ||
]; | ||
|
||
yield 'Has limited access to German language by role limitation' => [ | ||
[ | ||
$this->getLanguageAccessData(false, $english), | ||
$this->getLanguageAccessData(true, $german), | ||
$this->getLanguageAccessData(false, $french), | ||
], | ||
$versionInfo, | ||
$location, | ||
new LookupLimitationResult( | ||
true, | ||
[ | ||
$this->createLanguageLimitation(['ger-DE']), | ||
], | ||
), | ||
]; | ||
|
||
yield 'Has limited access to English and German languages by role and policy limitations' => [ | ||
[ | ||
$this->getLanguageAccessData(true, $english), | ||
$this->getLanguageAccessData(true, $german), | ||
$this->getLanguageAccessData(false, $french), | ||
], | ||
$versionInfo, | ||
$location, | ||
new LookupLimitationResult( | ||
true, | ||
[ | ||
$this->createLanguageLimitation(['eng-GB', 'fra-FR']), | ||
], | ||
[ | ||
new LookupPolicyLimitations( | ||
$this->createMock(Policy::class), | ||
[ | ||
$this->createLanguageLimitation(['ger-DE', 'fra-FR']), | ||
] | ||
), | ||
] | ||
), | ||
]; | ||
} | ||
|
||
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 | ||
->method('getContentInfo') | ||
->willReturn($contentInfo); | ||
$versionInfo | ||
->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<string> $limitationValues | ||
*/ | ||
private function createLanguageLimitation(array $limitationValues): Limitation\LanguageLimitation | ||
{ | ||
return new Limitation\LanguageLimitation( | ||
[ | ||
'limitationValues' => $limitationValues, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param array<string> $languageCodes | ||
*/ | ||
private function mockPermissionResolverLookupLimitations( | ||
array $languageCodes, | ||
ContentInfo $contentInfo, | ||
Location $location, | ||
LookupLimitationResult $lookupLimitationResult | ||
): void { | ||
$this->permissionResolver | ||
->method('lookupLimitations') | ||
->with( | ||
'content', | ||
'edit', | ||
$contentInfo, | ||
[ | ||
(new VersionBuilder())->translateToAnyLanguageOf($languageCodes)->build(), | ||
$location, | ||
], | ||
[Limitation::LANGUAGE], | ||
) | ||
->willReturn($lookupLimitationResult); | ||
} | ||
} |