Skip to content

Commit

Permalink
[Tests] Added tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Sep 6, 2023
1 parent a4d2cca commit bef583b
Show file tree
Hide file tree
Showing 3 changed files with 321 additions and 2 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8630,6 +8630,11 @@ parameters:
count: 1
path: src/lib/Pagination/Pagerfanta/URLWildcardAdapter.php

-
message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\LookupLimitationResult\\:\\:\\$hasAccess\\.$#"
count: 1
path: src/lib/Permission/LimitationResolver.php

-
message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\LookupLimitationResult\\:\\:\\$lookupPolicyLimitations\\.$#"
count: 2
Expand Down
3 changes: 1 addition & 2 deletions src/lib/Permission/LimitationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,11 @@ public function getLanguageLimitations(
$limitationLanguageCodes = $this->lookupLimitationsTransformer->getFlattenedLimitationsValues($lookupLimitations);

$languageLimitations = [];

foreach ($languages as $language) {
$languageLimitations[] = [
'languageCode' => $language->getLanguageCode(),
'name' => $language->getName(),
'hasAccess' => $this->hasAccessToLanguage($language, $limitationLanguageCodes),
'hasAccess' => $lookupLimitations->hasAccess && $this->hasAccessToLanguage($language, $limitationLanguageCodes),
];
}

Expand Down
315 changes: 315 additions & 0 deletions tests/lib/Permission/LimitationResolverTest.php
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);
}
}

0 comments on commit bef583b

Please sign in to comment.