Skip to content

Commit 48f0e47

Browse files
committed
IBX-8784: Created RichText configuration REST endpoint
1 parent 0a3b830 commit 48f0e47

File tree

13 files changed

+239
-0
lines changed

13 files changed

+239
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"ibexa/search": "~4.6.x-dev",
3939
"ibexa/solr": "~4.6.x-dev",
4040
"ibexa/test-core": "~4.6.x-dev",
41+
"ibexa/test-rest": "~4.6.x-dev",
4142
"phpstan/phpstan": "^1.9",
4243
"phpstan/phpstan-symfony": "^1.2",
4344
"phpstan/phpstan-phpunit": "^1.3",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Bundle\FieldTypeRichText\Controller;
10+
11+
use Ibexa\Contracts\FieldTypeRichText\Configuration\ProviderService;
12+
use Ibexa\FieldTypeRichText\REST\Value\RichTextConfig;
13+
use Ibexa\Rest\Server\Controller;
14+
15+
final class RichTextConfigController extends Controller
16+
{
17+
private ProviderService $providerService;
18+
19+
public function __construct(ProviderService $providerService)
20+
{
21+
$this->providerService = $providerService;
22+
}
23+
24+
public function loadConfigAction(): RichTextConfig
25+
{
26+
return new RichTextConfig($this->providerService->getConfiguration());
27+
}
28+
}

src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function load(array $configs, ContainerBuilder $container)
8282
$loader->load('configuration.yaml');
8383
$loader->load('api.yaml');
8484
$loader->load('command.yaml');
85+
$loader->load('controller.yaml');
8586

8687
$configuration = $this->getConfiguration($configs, $container);
8788
$config = $this->processConfiguration($configuration, $configs);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
_defaults:
3+
autoconfigure: true
4+
autowire: true
5+
public: false
6+
7+
Ibexa\Bundle\FieldTypeRichText\Controller\RichTextConfigController:
8+
tags:
9+
- controller.service_arguments

src/bundle/Resources/config/rest.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ services:
99
- '@Ibexa\FieldTypeRichText\RichText\Converter\Html5Edit'
1010
tags:
1111
- { name: ibexa.rest.field_type.processor, alias: ezrichtext }
12+
13+
Ibexa\FieldTypeRichText\REST\Output\ValueObjectVisitor\RichTextConfigVisitor:
14+
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
15+
tags:
16+
- { name: ibexa.rest.output.value_object.visitor, type: Ibexa\FieldTypeRichText\REST\Value\RichTextConfig }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ibexa.rest.richtext_config:
2+
path: /richtext-config
3+
controller: 'Ibexa\Bundle\FieldTypeRichText\Controller\RichTextConfigController::loadConfigAction'
4+
methods: [GET]
5+
options:
6+
expose: true
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\FieldTypeRichText\REST\Output\ValueObjectVisitor;
10+
11+
use Ibexa\Contracts\Rest\Output\Generator;
12+
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
13+
use Ibexa\Contracts\Rest\Output\Visitor;
14+
15+
final class RichTextConfigVisitor extends ValueObjectVisitor
16+
{
17+
public function visit(Visitor $visitor, Generator $generator, $data): void
18+
{
19+
$generator->startObjectElement('RichTextConfig');
20+
$visitor->setHeader('Content-Type', $generator->getMediaType('RichTextConfig'));
21+
22+
foreach ($data->getConfig() as $namespace => $config) {
23+
$generator->generateFieldTypeHash($namespace, $config);
24+
}
25+
26+
$generator->endObjectElement('RichTextConfig');
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\FieldTypeRichText\REST\Value;
10+
11+
use Ibexa\Rest\Value;
12+
13+
final class RichTextConfig extends Value
14+
{
15+
/** @var array<string, mixed> */
16+
private array $config;
17+
18+
/**
19+
* @param array<string, mixed> $config
20+
*/
21+
public function __construct(array $config)
22+
{
23+
$this->config = $config;
24+
}
25+
26+
/**
27+
* @return array<string, mixed>
28+
*/
29+
public function getConfig(): array
30+
{
31+
return $this->config;
32+
}
33+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Integration\FieldTypeRichText\REST;
10+
11+
use Ibexa\Contracts\Test\Rest\WebTestCase;
12+
13+
final class RichTextConfigTest extends WebTestCase
14+
{
15+
/**
16+
* @dataProvider provideForTestConfig
17+
*/
18+
public function testConfig(
19+
string $type,
20+
string $acceptHeader,
21+
string $snapshot
22+
): void {
23+
$this->client->request('GET', '/api/ibexa/v2/richtext-config', [], [], [
24+
'HTTP_ACCEPT' => $acceptHeader,
25+
]);
26+
27+
$this->assertResponseIsSuccessful();
28+
$response = $this->client->getResponse();
29+
$content = $response->getContent();
30+
self::assertIsString($content);
31+
32+
if ($type === 'xml') {
33+
self::assertSame('application/vnd.ibexa.api.RichTextConfig+xml', $response->headers->get('Content-Type'));
34+
self::assertResponseMatchesXmlSnapshot($content, $snapshot);
35+
} elseif ($type === 'json') {
36+
self::assertSame('application/vnd.ibexa.api.RichTextConfig+json', $response->headers->get('Content-Type'));
37+
self::assertResponseMatchesJsonSnapshot($content, $snapshot);
38+
} else {
39+
throw new \LogicException(sprintf(
40+
'Unknown type: "%s". Expected one of: "%s"',
41+
$type,
42+
implode('", "', ['json', 'xml']),
43+
));
44+
}
45+
}
46+
47+
/**
48+
* @return iterable<array{
49+
* 'xml'|'json',
50+
* non-empty-string,
51+
* non-empty-string,
52+
* }>
53+
*/
54+
public static function provideForTestConfig(): iterable
55+
{
56+
yield 'application/vnd.ibexa.api.RichTextConfig+json' => [
57+
'json',
58+
'application/vnd.ibexa.api.RichTextConfig+json',
59+
__DIR__ . '/_output/RichTextConfig.json',
60+
];
61+
62+
yield 'application/json' => [
63+
'json',
64+
'application/json',
65+
__DIR__ . '/_output/RichTextConfig.json',
66+
];
67+
68+
yield 'application/vnd.ibexa.api.RichTextConfig+xml' => [
69+
'xml',
70+
'application/vnd.ibexa.api.RichTextConfig+xml',
71+
__DIR__ . '/_output/RichTextConfig.xml',
72+
];
73+
74+
yield 'application/xml' => [
75+
'xml',
76+
'application/xml',
77+
__DIR__ . '/_output/RichTextConfig.xml',
78+
];
79+
}
80+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"RichTextConfig": {
3+
"_media-type": "application\/vnd.ibexa.api.RichTextConfig+json",
4+
"customStyles": [],
5+
"customTags": [],
6+
"alloyEditor": {
7+
"extraPlugins": [],
8+
"toolbars": [],
9+
"classes": [],
10+
"attributes": [],
11+
"nativeAttributes": {
12+
"table": [
13+
"border"
14+
]
15+
}
16+
},
17+
"CKEditor": {
18+
"toolbar": []
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)