-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1539 from ezsystems/ezp_31783_follow_up
EZP-31783: Fixed RichTextEmbedAllowedContentTypes subscriber implementation
- Loading branch information
Showing
3 changed files
with
176 additions
and
47 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php
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,142 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformAdminUi\Tests\UniversalDiscovery\Event\Subscriber; | ||
|
||
use eZ\Publish\API\Repository\ContentTypeService; | ||
use eZ\Publish\API\Repository\PermissionResolver; | ||
use eZ\Publish\API\Repository\Values\ContentType\ContentType; | ||
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation; | ||
use EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface; | ||
use EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\ConfigResolveEvent; | ||
use EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\Subscriber\RichTextEmbedAllowedContentTypes; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RichTextEmbedAllowedContentTypesTest extends TestCase | ||
{ | ||
private const EXAMPLE_LIMITATIONS = [/* Some limitations */]; | ||
|
||
private const SUPPORTED_CONFIG_NAMES = ['richtext_embed', 'richtext_embed_image']; | ||
|
||
private const ALLOWED_CONTENT_TYPES_IDS = [2, 4]; | ||
private const ALLOWED_CONTENT_TYPES = ['article', 'folder']; | ||
|
||
/** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */ | ||
private $permissionResolver; | ||
|
||
/** @var \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface|\PHPUnit\Framework\MockObject\MockObject */ | ||
private $permissionChecker; | ||
|
||
/** @var \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject */ | ||
private $contentTypeService; | ||
|
||
/** @var \EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\Subscriber\RichTextEmbedAllowedContentTypes */ | ||
private $subscriber; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->permissionResolver = $this->createMock(PermissionResolver::class); | ||
$this->permissionChecker = $this->createMock(PermissionCheckerInterface::class); | ||
$this->contentTypeService = $this->createMock(ContentTypeService::class); | ||
|
||
$this->subscriber = new RichTextEmbedAllowedContentTypes( | ||
$this->permissionResolver, | ||
$this->permissionChecker, | ||
$this->contentTypeService | ||
); | ||
} | ||
|
||
public function testUdwConfigResolveOnUnsupportedConfigName(): void | ||
{ | ||
$this->permissionResolver->expects($this->never())->method('hasAccess'); | ||
$this->permissionChecker->expects($this->never())->method('getRestrictions'); | ||
$this->contentTypeService->expects($this->never())->method('loadContentTypeList'); | ||
|
||
$event = $this->createConfigResolveEvent('unsupported_config_name'); | ||
|
||
$this->subscriber->onUdwConfigResolve($event); | ||
|
||
$this->assertEquals([], $event->getConfig()); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForUdwConfigResolveWhenThereIsNoContentReadLimitations | ||
*/ | ||
public function testUdwConfigResolveWhenThereIsNoContentReadLimitations(bool $hasAccess): void | ||
{ | ||
$this->permissionResolver->method('hasAccess')->with('content', 'read')->willReturn($hasAccess); | ||
$this->permissionChecker->expects($this->never())->method('getRestrictions'); | ||
$this->contentTypeService->expects($this->never())->method('loadContentTypeList'); | ||
|
||
$this->assertConfigurationResolvingResult([ | ||
'allowed_content_types' => null, | ||
]); | ||
} | ||
|
||
public function dataProviderForUdwConfigResolveWhenThereIsNoContentReadLimitations(): iterable | ||
{ | ||
return [ | ||
['hasAccess' => false], | ||
['hasAccess' => true], | ||
]; | ||
} | ||
|
||
public function testUdwConfigResolveWhenThereAreContentReadLimitations(): void | ||
{ | ||
$this->permissionResolver | ||
->method('hasAccess') | ||
->with('content', 'read') | ||
->willReturn(self::EXAMPLE_LIMITATIONS); | ||
|
||
$this->permissionChecker | ||
->method('getRestrictions') | ||
->with(self::EXAMPLE_LIMITATIONS, ContentTypeLimitation::class) | ||
->willReturn(self::ALLOWED_CONTENT_TYPES_IDS); | ||
|
||
$this->contentTypeService | ||
->method('loadContentTypeList') | ||
->with(self::ALLOWED_CONTENT_TYPES_IDS) | ||
->willReturn($this->createContentTypeListMock(self::ALLOWED_CONTENT_TYPES)); | ||
|
||
$this->assertConfigurationResolvingResult([ | ||
'allowed_content_types' => self::ALLOWED_CONTENT_TYPES, | ||
]); | ||
} | ||
|
||
private function assertConfigurationResolvingResult(?array $expectedConfiguration): void | ||
{ | ||
foreach (self::SUPPORTED_CONFIG_NAMES as $configName) { | ||
$event = $this->createConfigResolveEvent($configName); | ||
|
||
$this->subscriber->onUdwConfigResolve($event); | ||
|
||
$this->assertEquals( | ||
$expectedConfiguration, | ||
$event->getConfig() | ||
); | ||
} | ||
} | ||
|
||
private function createConfigResolveEvent(string $configName = 'richtext_embed'): ConfigResolveEvent | ||
{ | ||
$event = new ConfigResolveEvent(); | ||
$event->setConfigName($configName); | ||
|
||
return $event; | ||
} | ||
|
||
private function createContentTypeListMock(array $identifiers): array | ||
{ | ||
return array_map(function (string $identifier) { | ||
$contentType = $this->createMock(ContentType::class); | ||
$contentType->method('__get')->with('identifier')->willReturn($identifier); | ||
|
||
return $contentType; | ||
}, $identifiers); | ||
} | ||
} |
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