Skip to content

Commit

Permalink
IBX-8784: Created RichText configuration REST endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Aug 26, 2024
1 parent 0a3b830 commit 48f0e47
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions src/bundle/Controller/RichTextConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Bundle\FieldTypeRichText\Controller;

use Ibexa\Contracts\FieldTypeRichText\Configuration\ProviderService;
use Ibexa\FieldTypeRichText\REST\Value\RichTextConfig;
use Ibexa\Rest\Server\Controller;

final class RichTextConfigController extends Controller
{
private ProviderService $providerService;

public function __construct(ProviderService $providerService)
{
$this->providerService = $providerService;
}

public function loadConfigAction(): RichTextConfig
{
return new RichTextConfig($this->providerService->getConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/bundle/Resources/config/controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
autoconfigure: true
autowire: true
public: false

Ibexa\Bundle\FieldTypeRichText\Controller\RichTextConfigController:
tags:
- controller.service_arguments
5 changes: 5 additions & 0 deletions src/bundle/Resources/config/rest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/routing_rest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ibexa.rest.richtext_config:
path: /richtext-config
controller: 'Ibexa\Bundle\FieldTypeRichText\Controller\RichTextConfigController::loadConfigAction'
methods: [GET]
options:
expose: true
28 changes: 28 additions & 0 deletions src/lib/REST/Output/ValueObjectVisitor/RichTextConfigVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\FieldTypeRichText\REST\Output\ValueObjectVisitor;

use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
use Ibexa\Contracts\Rest\Output\Visitor;

final class RichTextConfigVisitor extends ValueObjectVisitor
{
public function visit(Visitor $visitor, Generator $generator, $data): void
{
$generator->startObjectElement('RichTextConfig');
$visitor->setHeader('Content-Type', $generator->getMediaType('RichTextConfig'));

foreach ($data->getConfig() as $namespace => $config) {
$generator->generateFieldTypeHash($namespace, $config);
}

$generator->endObjectElement('RichTextConfig');
}
}
33 changes: 33 additions & 0 deletions src/lib/REST/Value/RichTextConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\FieldTypeRichText\REST\Value;

use Ibexa\Rest\Value;

final class RichTextConfig extends Value
{
/** @var array<string, mixed> */
private array $config;

/**
* @param array<string, mixed> $config
*/
public function __construct(array $config)
{
$this->config = $config;
}

/**
* @return array<string, mixed>
*/
public function getConfig(): array
{
return $this->config;
}
}
80 changes: 80 additions & 0 deletions tests/integration/REST/RichTextConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\Integration\FieldTypeRichText\REST;

use Ibexa\Contracts\Test\Rest\WebTestCase;

final class RichTextConfigTest extends WebTestCase
{
/**
* @dataProvider provideForTestConfig
*/
public function testConfig(
string $type,
string $acceptHeader,
string $snapshot
): void {
$this->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<array{
* 'xml'|'json',
* non-empty-string,
* non-empty-string,
* }>
*/
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',
];
}
}
21 changes: 21 additions & 0 deletions tests/integration/REST/_output/RichTextConfig.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
}
19 changes: 19 additions & 0 deletions tests/integration/REST/_output/RichTextConfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<RichTextConfig media-type="application/vnd.ibexa.api.RichTextConfig+xml">
<customStyles/>
<customTags/>
<alloyEditor>
<value key="extraPlugins"/>
<value key="toolbars"/>
<value key="classes"/>
<value key="attributes"/>
<value key="nativeAttributes">
<value key="table">
<value>border</value>
</value>
</value>
</alloyEditor>
<CKEditor>
<value key="toolbar"/>
</CKEditor>
</RichTextConfig>
4 changes: 4 additions & 0 deletions tests/integration/Resources/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ ibexa:
engine: '%env(SEARCH_ENGINE)%'
connection: default

siteaccess:
groups:
admin_group: []

system:
default:
languages:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/Resources/routing.yaml
Original file line number Diff line number Diff line change
@@ -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%'

0 comments on commit 48f0e47

Please sign in to comment.