diff --git a/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php b/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php new file mode 100644 index 0000000000..fbc83e1c37 --- /dev/null +++ b/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php @@ -0,0 +1,142 @@ +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); + } +} diff --git a/src/lib/UniversalDiscovery/Event/ConfigResolveEvent.php b/src/lib/UniversalDiscovery/Event/ConfigResolveEvent.php index 50a93665fa..c7ef8a62fd 100644 --- a/src/lib/UniversalDiscovery/Event/ConfigResolveEvent.php +++ b/src/lib/UniversalDiscovery/Event/ConfigResolveEvent.php @@ -12,16 +12,16 @@ class ConfigResolveEvent extends Event { - const NAME = 'udw.resolve.config'; + public const NAME = 'udw.resolve.config'; /** @var string */ protected $configName; /** @var array */ - protected $config; + protected $config = []; /** @var array */ - protected $context; + protected $context = []; /** * @return string diff --git a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php index 03ae04e571..9d0083cc08 100644 --- a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php +++ b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php @@ -10,74 +10,60 @@ 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 Symfony\Component\EventDispatcher\EventSubscriberInterface; -class RichTextEmbedAllowedContentTypes implements EventSubscriberInterface +final class RichTextEmbedAllowedContentTypes implements EventSubscriberInterface { - /** @var string[] */ - private $allowedContentTypesIdentifiers; + /** @var \eZ\Publish\API\Repository\PermissionResolver */ + private $permissionResolver; + + /** @var \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface */ + private $permissionChecker; + + /** @var \eZ\Publish\API\Repository\ContentTypeService */ + private $contentTypeService; + + /** @var string[]|null */ + private $allowedContentTypesIdentifiers = null; - /** - * @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver - * @param \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface $permissionChecker - * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService - * - * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException - */ public function __construct( PermissionResolver $permissionResolver, PermissionCheckerInterface $permissionChecker, ContentTypeService $contentTypeService ) { - $this->allowedContentTypesIdentifiers = $this->getAllowedContentTypesIdentifiers( - $permissionResolver, - $permissionChecker, - $contentTypeService - ); + $this->permissionResolver = $permissionResolver; + $this->permissionChecker = $permissionChecker; + $this->contentTypeService = $contentTypeService; } /** - * @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver - * @param \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface $permissionChecker - * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService - * * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException */ - private function getAllowedContentTypesIdentifiers( - PermissionResolver $permissionResolver, - PermissionCheckerInterface $permissionChecker, - ContentTypeService $contentTypeService - ): array { - $access = $permissionResolver->hasAccess('content', 'read'); + private function getAllowedContentTypesIdentifiers(): array + { + $access = $this->permissionResolver->hasAccess('content', 'read'); if (!\is_array($access)) { return []; } - $restrictedContentTypesIds = $permissionChecker->getRestrictions($access, ContentTypeLimitation::class); - + $restrictedContentTypesIds = $this->permissionChecker->getRestrictions($access, ContentTypeLimitation::class); if (empty($restrictedContentTypesIds)) { return []; } - $restrictedContentTypes = $contentTypeService->loadContentTypeList($restrictedContentTypesIds); + $allowedContentTypesIdentifiers = []; + + $restrictedContentTypes = $this->contentTypeService->loadContentTypeList($restrictedContentTypesIds); + foreach ($restrictedContentTypes as $contentType) { + $allowedContentTypesIdentifiers[] = $contentType->identifier; + } - return array_values( - array_map( - static function (ContentType $contentType): string { - return $contentType->identifier; - }, - iterator_to_array($restrictedContentTypes) - ) - ); + return $allowedContentTypesIdentifiers; } - /** - * {@inheritdoc} - */ public static function getSubscribedEvents(): array { return [ @@ -85,9 +71,6 @@ public static function getSubscribedEvents(): array ]; } - /** - * @param \EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\ConfigResolveEvent $event - */ public function onUdwConfigResolve(ConfigResolveEvent $event): void { $config = $event->getConfig(); @@ -96,7 +79,11 @@ public function onUdwConfigResolve(ConfigResolveEvent $event): void return; } - $config['allowed_content_types'] = !empty($this->allowedContentTypesIdentifiers) ? $this->restrictedContentTypesIdentifier : null; + if ($this->allowedContentTypesIdentifiers === null) { + $this->allowedContentTypesIdentifiers = $this->getAllowedContentTypesIdentifiers(); + } + + $config['allowed_content_types'] = !empty($this->allowedContentTypesIdentifiers) ? $this->allowedContentTypesIdentifiers : null; $event->setConfig($config); }