Skip to content

Commit

Permalink
[Tests] Added test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Sep 4, 2023
1 parent a4d2cca commit 571974c
Showing 1 changed file with 223 additions and 0 deletions.
223 changes: 223 additions & 0 deletions tests/lib/Permission/LimitationResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?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 PHPUnit\Framework\TestCase;

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
): void {
$this->mockPermissionResolverLookupLimitations(
[
'eng-GB',
'ger-DE',
],
$versionInfo->getContentInfo(),
$location,
new LookupLimitationResult(true)
);
self::assertEquals(
$expected,
$this->limitationResolver->getLanguageLimitations(
$versionInfo,
$location
)
);
}

/**
* @return iterable<array{
* array<array{
* languageCode: string,
* name: string,
* hasAccess: bool,
* }>,
* 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<string> $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);
}
}

0 comments on commit 571974c

Please sign in to comment.