Skip to content

Commit

Permalink
Merge branch '2.0' into 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Oct 13, 2020
2 parents 365e07b + e376b33 commit 82a53c4
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 20 deletions.
87 changes: 85 additions & 2 deletions src/bundle/DependencyInjection/EzPlatformRichTextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class EzPlatformRichTextExtension extends Extension implements PrependExtensionI
const RICHTEXT_ALLOY_EDITOR_PARAMETER = 'ezplatform.ezrichtext.alloy_editor';
public const RICHTEXT_CONFIGURATION_PROVIDER_TAG = 'ezplatform.ezrichtext.configuration.provider';

private const RICHTEXT_TEXT_TOOLBAR_NAME = 'text';

public function getAlias()
{
return 'ezrichtext';
Expand Down Expand Up @@ -81,7 +83,7 @@ public function load(array $configs, ContainerBuilder $container)
* @param array $config
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
private function registerRichTextConfiguration(array $config, ContainerBuilder $container)
private function registerRichTextConfiguration(array $config, ContainerBuilder $container): void
{
$customTagsConfig = $config['custom_tags'] ?? [];
$customStylesConfig = $config['custom_styles'] ?? [];
Expand All @@ -98,6 +100,11 @@ private function registerRichTextConfiguration(array $config, ContainerBuilder $
'Tag',
$container
);
$this->validateInlineCustomTagToolbarsConfig(
$availableSiteAccesses,
$customTagsConfig,
$container,
);
$this->validateCustomTemplatesConfig(
$availableSiteAccesses,
$customStylesConfig,
Expand Down Expand Up @@ -188,7 +195,7 @@ private function validateCustomTemplatesConfig(
string $nodeName,
string $type,
ContainerBuilder $container
) {
): void {
$namespace = 'ezsettings';
$definedCustomTemplates = array_keys($config);
// iterate manually through available Scopes as scope context is not available
Expand All @@ -207,4 +214,80 @@ private function validateCustomTemplatesConfig(
}
}
}

/**
* Validate presence of inline Custom Tags in Toolbars.
*
* @param array $availableSiteAccesses a list of available SiteAccesses
* @param array $customTagsConfig Custom Tags configuration
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
private function validateInlineCustomTagToolbarsConfig(
array $availableSiteAccesses,
array $customTagsConfig,
ContainerBuilder $container
): void {
$customTags = $this->getInlineCustomTags($customTagsConfig);
foreach ($this->getToolbarsBySiteAccess($availableSiteAccesses, $container) as $siteAccess => $toolbar) {
foreach ($toolbar as $toolbarName => $toolbarContent) {
$this->checkForInlineTagsInToolbar($toolbarName, $toolbarContent, $customTags, $siteAccess);
}
}
}

/**
* @return iterable<array> Iterable containing arrays with toolbars and their buttons
*/
private function getToolbarsBySiteAccess(array $availableSiteAccesses, ContainerBuilder $container): iterable
{
foreach ($availableSiteAccesses as $siteAccessName) {
$paramName = "ezsettings.{$siteAccessName}.fieldtypes.ezrichtext.toolbars";
if (!$container->hasParameter($paramName)) {
continue;
}

yield $paramName => $container->getParameter($paramName);
}
}

/**
* @return string[]
*/
private function getInlineCustomTags(array $customTagsConfig): array
{
$customTags = array_filter(
$customTagsConfig,
static function (array $customTag): bool {
return $customTag['is_inline'] ?? false;
}
);

return array_keys($customTags);
}

private function checkForInlineTagsInToolbar(
string $toolbarName,
array $toolbarContent,
array $customTags,
string $siteAccess
): void {
// "text" toolbar is the only one that can contain inline tags
if (self::RICHTEXT_TEXT_TOOLBAR_NAME === $toolbarName) {
return;
}

foreach ($toolbarContent['buttons'] as $buttonName => $buttonConfig) {
if (in_array($buttonName, $customTags, true)) {
throw new InvalidConfigurationException(
sprintf(
"Toolbar '%s' configured in the '%s' scope cannot contain Custom Tag '%s'. Inline Custom Tags are not allowed in Toolbars other than '%s'.",
$toolbarName,
$siteAccess,
$buttonName,
self::RICHTEXT_TEXT_TOOLBAR_NAME
)
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class EzBtnInlineCustomTagUpdate extends EzBtnCustomTagUpdate {

if (createNewTag) {
const firstChild = selection.getFirst();
const isNodeElement = firstChild.type === CKEDITOR.NODE_ELEMENT;
const isNodeElement = firstChild && firstChild.type === CKEDITOR.NODE_ELEMENT;
const content = isNodeElement && firstChild.is('table') ? selection.$.textContent : selection.getHtml();

widget.setName(this.customTagName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,20 @@

use EzSystems\EzPlatformRichTextBundle\DependencyInjection\EzPlatformRichTextExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Yaml\Yaml;

class EzPlatformRichTextExtensionTest extends AbstractExtensionTestCase
{
/**
* @var \EzSystems\EzPlatformRichTextBundle\DependencyInjection\EzPlatformRichTextExtension
*/
private $extension;

protected function setUp(): void
{
$this->extension = new EzPlatformRichTextExtension();

parent::setUp();
}

protected function getContainerExtensions(): array
{
return [$this->extension];
return [new EzPlatformRichTextExtension()];
}

/**
* Test RichText Semantic Configuration.
*/
public function testRichTextConfiguration()
public function testRichTextConfiguration(): void
{
$config = Yaml::parse(
file_get_contents(__DIR__ . '/Fixtures/ezrichtext.yaml')
Expand All @@ -43,7 +32,7 @@ public function testRichTextConfiguration()

// Validate Custom Tags
$this->assertTrue(
$this->container->hasParameter($this->extension::RICHTEXT_CUSTOM_TAGS_PARAMETER)
$this->container->hasParameter(EzPlatformRichTextExtension::RICHTEXT_CUSTOM_TAGS_PARAMETER)
);
$expectedCustomTagsConfig = [
'video' => [
Expand Down Expand Up @@ -90,7 +79,7 @@ public function testRichTextConfiguration()

$this->assertSame(
$expectedCustomTagsConfig,
$this->container->getParameter($this->extension::RICHTEXT_CUSTOM_TAGS_PARAMETER)
$this->container->getParameter(EzPlatformRichTextExtension::RICHTEXT_CUSTOM_TAGS_PARAMETER)
);
}

Expand All @@ -99,7 +88,7 @@ public function testRichTextConfiguration()
*
* @see \EzSystems\EzPlatformRichTextBundle\DependencyInjection\EzPlatformRichTextExtension::prepend
*/
public function testPrepend()
public function testPrepend(): void
{
$this->load([]);

Expand Down Expand Up @@ -127,4 +116,45 @@ public function testPrepend()
$actualPrependedConfig['system']['default']
);
}

/**
* @dataProvider inlineTagDataProvider
*/
public function testCheckingInlineCustomTagsInToolbars(string $toolbarName, ?string $expectedException): void
{
$config = Yaml::parse(
file_get_contents(__DIR__ . '/Fixtures/ezrichtext.yaml')
);
$config['custom_tags']['video']['is_inline'] = true;
$this->container->setParameter('ezpublish.siteaccess.list', ['admin_group']);
$this->container->setParameter('ezsettings.admin_group.fieldtypes.ezrichtext.toolbars', [
$toolbarName => [
'buttons' => [
'video' => [
'priority' => 5,
'visible' => true,
],
],
],
]);

if (is_string($expectedException)) {
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage($expectedException);
}
$this->load($config);
}

public function inlineTagDataProvider(): iterable
{
yield 'Inline tag in normal toolbar' => [
'foo',
"Toolbar 'foo' configured in the 'ezsettings.admin_group.fieldtypes.ezrichtext.toolbars' scope cannot contain Custom Tag 'video'. Inline Custom Tags are not allowed in Toolbars other than 'text'.",
];

yield 'Inline tag in text toolbar' => [
'text',
null,
];
}
}

0 comments on commit 82a53c4

Please sign in to comment.