diff --git a/composer.json b/composer.json index 1265bcbd..f54cc31b 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,7 @@ "ibexa/search": "~4.6.x-dev", "ibexa/solr": "~4.6.x-dev", "ibexa/test-core": "~4.6.x-dev", + "ibexa/test-rest": "~4.6.x-dev", "phpstan/phpstan": "^1.9", "phpstan/phpstan-symfony": "^1.2", "phpstan/phpstan-phpunit": "^1.3", diff --git a/src/bundle/Controller/RichTextConfigController.php b/src/bundle/Controller/RichTextConfigController.php new file mode 100644 index 00000000..3c571ba7 --- /dev/null +++ b/src/bundle/Controller/RichTextConfigController.php @@ -0,0 +1,28 @@ +providerService = $providerService; + } + + public function loadConfigAction(): RichTextConfig + { + return new RichTextConfig($this->providerService->getConfiguration()); + } +} diff --git a/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php b/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php index 0da004ce..957e42d7 100644 --- a/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php +++ b/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php @@ -82,6 +82,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('configuration.yaml'); $loader->load('api.yaml'); $loader->load('command.yaml'); + $loader->load('controller.yaml'); $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); diff --git a/src/bundle/Resources/config/controller.yaml b/src/bundle/Resources/config/controller.yaml new file mode 100644 index 00000000..d2442f95 --- /dev/null +++ b/src/bundle/Resources/config/controller.yaml @@ -0,0 +1,9 @@ +services: + _defaults: + autoconfigure: true + autowire: true + public: false + + Ibexa\Bundle\FieldTypeRichText\Controller\RichTextConfigController: + tags: + - controller.service_arguments diff --git a/src/bundle/Resources/config/rest.yaml b/src/bundle/Resources/config/rest.yaml index 7aae8148..4fe9b4f2 100644 --- a/src/bundle/Resources/config/rest.yaml +++ b/src/bundle/Resources/config/rest.yaml @@ -9,3 +9,8 @@ services: - '@Ibexa\FieldTypeRichText\RichText\Converter\Html5Edit' tags: - { name: ibexa.rest.field_type.processor, alias: ezrichtext } + + Ibexa\FieldTypeRichText\REST\Output\ValueObjectVisitor\RichTextConfigVisitor: + parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor + tags: + - { name: ibexa.rest.output.value_object.visitor, type: Ibexa\FieldTypeRichText\REST\Value\RichTextConfig } diff --git a/src/bundle/Resources/config/routing_rest.yaml b/src/bundle/Resources/config/routing_rest.yaml new file mode 100644 index 00000000..341d2594 --- /dev/null +++ b/src/bundle/Resources/config/routing_rest.yaml @@ -0,0 +1,6 @@ +ibexa.rest.richtext_config: + path: /richtext-config + controller: 'Ibexa\Bundle\FieldTypeRichText\Controller\RichTextConfigController::loadConfigAction' + methods: [GET] + options: + expose: true diff --git a/src/lib/REST/Output/ValueObjectVisitor/RichTextConfigVisitor.php b/src/lib/REST/Output/ValueObjectVisitor/RichTextConfigVisitor.php new file mode 100644 index 00000000..d758d960 --- /dev/null +++ b/src/lib/REST/Output/ValueObjectVisitor/RichTextConfigVisitor.php @@ -0,0 +1,28 @@ +startObjectElement('RichTextConfig'); + $visitor->setHeader('Content-Type', $generator->getMediaType('RichTextConfig')); + + foreach ($data->getConfig() as $namespace => $config) { + $generator->generateFieldTypeHash($namespace, $config); + } + + $generator->endObjectElement('RichTextConfig'); + } +} diff --git a/src/lib/REST/Value/RichTextConfig.php b/src/lib/REST/Value/RichTextConfig.php new file mode 100644 index 00000000..b771788e --- /dev/null +++ b/src/lib/REST/Value/RichTextConfig.php @@ -0,0 +1,33 @@ + */ + private array $config; + + /** + * @param array $config + */ + public function __construct(array $config) + { + $this->config = $config; + } + + /** + * @return array + */ + public function getConfig(): array + { + return $this->config; + } +} diff --git a/tests/integration/REST/RichTextConfigTest.php b/tests/integration/REST/RichTextConfigTest.php new file mode 100644 index 00000000..9055424a --- /dev/null +++ b/tests/integration/REST/RichTextConfigTest.php @@ -0,0 +1,80 @@ +client->request('GET', '/api/ibexa/v2/richtext-config', [], [], [ + 'HTTP_ACCEPT' => $acceptHeader, + ]); + + $this->assertResponseIsSuccessful(); + $response = $this->client->getResponse(); + $content = $response->getContent(); + self::assertIsString($content); + + if ($type === 'xml') { + self::assertSame('application/vnd.ibexa.api.RichTextConfig+xml', $response->headers->get('Content-Type')); + self::assertResponseMatchesXmlSnapshot($content, $snapshot); + } elseif ($type === 'json') { + self::assertSame('application/vnd.ibexa.api.RichTextConfig+json', $response->headers->get('Content-Type')); + self::assertResponseMatchesJsonSnapshot($content, $snapshot); + } else { + throw new \LogicException(sprintf( + 'Unknown type: "%s". Expected one of: "%s"', + $type, + implode('", "', ['json', 'xml']), + )); + } + } + + /** + * @return iterable + */ + public static function provideForTestConfig(): iterable + { + yield 'application/vnd.ibexa.api.RichTextConfig+json' => [ + 'json', + 'application/vnd.ibexa.api.RichTextConfig+json', + __DIR__ . '/_output/RichTextConfig.json', + ]; + + yield 'application/json' => [ + 'json', + 'application/json', + __DIR__ . '/_output/RichTextConfig.json', + ]; + + yield 'application/vnd.ibexa.api.RichTextConfig+xml' => [ + 'xml', + 'application/vnd.ibexa.api.RichTextConfig+xml', + __DIR__ . '/_output/RichTextConfig.xml', + ]; + + yield 'application/xml' => [ + 'xml', + 'application/xml', + __DIR__ . '/_output/RichTextConfig.xml', + ]; + } +} diff --git a/tests/integration/REST/_output/RichTextConfig.json b/tests/integration/REST/_output/RichTextConfig.json new file mode 100644 index 00000000..13cf6351 --- /dev/null +++ b/tests/integration/REST/_output/RichTextConfig.json @@ -0,0 +1,21 @@ +{ + "RichTextConfig": { + "_media-type": "application\/vnd.ibexa.api.RichTextConfig+json", + "customStyles": [], + "customTags": [], + "alloyEditor": { + "extraPlugins": [], + "toolbars": [], + "classes": [], + "attributes": [], + "nativeAttributes": { + "table": [ + "border" + ] + } + }, + "CKEditor": { + "toolbar": [] + } + } +} \ No newline at end of file diff --git a/tests/integration/REST/_output/RichTextConfig.xml b/tests/integration/REST/_output/RichTextConfig.xml new file mode 100644 index 00000000..d0fba021 --- /dev/null +++ b/tests/integration/REST/_output/RichTextConfig.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + border + + + + + + + diff --git a/tests/integration/Resources/config.yaml b/tests/integration/Resources/config.yaml index 60ae5ec2..f2352ffc 100644 --- a/tests/integration/Resources/config.yaml +++ b/tests/integration/Resources/config.yaml @@ -15,6 +15,10 @@ ibexa: engine: '%env(SEARCH_ENGINE)%' connection: default + siteaccess: + groups: + admin_group: [] + system: default: languages: diff --git a/tests/integration/Resources/routing.yaml b/tests/integration/Resources/routing.yaml index 2956445d..10db3109 100644 --- a/tests/integration/Resources/routing.yaml +++ b/tests/integration/Resources/routing.yaml @@ -1,2 +1,6 @@ ibexa.user.default_profile_image.initials: path: /user/default_profile_image/initials.svg + +ibexa.fieldtype_richtext.rest: + resource: '@IbexaFieldTypeRichTextBundle/Resources/config/routing_rest.yaml' + prefix: '%ibexa.rest.path_prefix%'