function SmartDefaultSettingsTest::setUp

Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Kernel\SmartDefaultSettingsTest::setUp()
  2. 10 core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php \Drupal\Tests\ckeditor5\Kernel\SmartDefaultSettingsTest::setUp()

Overrides KernelTestBase::setUp

File

core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php, line 89

Class

SmartDefaultSettingsTest
@covers <a href="/api/drupal/core%21modules%21ckeditor5%21src%21SmartDefaultSettings.php/class/SmartDefaultSettings/11.x" title="Generates CKEditor 5 settings for existing text editors/formats." class="local">\Drupal\ckeditor5\SmartDefaultSettings</a>::computeSmartDefaultSettings @group ckeditor5 @internal

Namespace

Drupal\Tests\ckeditor5\Kernel

Code

protected function setUp() : void {
    parent::setUp();
    $this->manager = $this->container
        ->get('plugin.manager.ckeditor5.plugin');
    $this->typedConfig = $this->container
        ->get('config.typed');
    $this->smartDefaultSettings = $this->container
        ->get('ckeditor5.smart_default_settings');
    $this->database = $this->container
        ->get('database');
    $this->installSchema('dblog', [
        'watchdog',
    ]);
    FilterFormat::create([
        'format' => 'minimal_ckeditor_wrong_allowed_html',
        'name' => 'Most basic HTML, but with allowed_html misconfigured',
        'filters' => [
            'filter_html' => [
                'status' => 1,
                'settings' => [
                    // Misconfiguration aspects:
                    // 1. `<a>`, not `<a href>`, while `DrupalLink` is enabled
                    // 2. `<p style>` even though `style` is globally disallowed by
                    //    filter_html
                    // 3. `<a onclick>` even though `on*` is globally disallowed by
                    //    filter_html
'allowed_html' => '<p style> <br> <a onclick>',
                ],
            ],
        ],
    ])->save();
    Editor::create([
        'format' => 'minimal_ckeditor_wrong_allowed_html',
        'editor' => 'ckeditor',
        'image_upload' => [
            'status' => FALSE,
        ],
        'settings' => [
            'toolbar' => [
                'rows' => [
                    0 => [
                        [
                            'name' => 'Basic Formatting',
                            'items' => [
                                'DrupalLink',
                            ],
                        ],
                    ],
                ],
            ],
            'plugins' => [],
        ],
    ])->save();
    FilterFormat::create(Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/filter.format.full_html.yml'))->save();
    Editor::create(Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.full_html.yml'))->save();
    $basic_html_format = Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/filter.format.basic_html.yml');
    FilterFormat::create($basic_html_format)->save();
    Editor::create(Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    FilterFormat::create(Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/filter.format.restricted_html.yml'))->save();
    $basic_html_format_without_image_uploads = $basic_html_format;
    $basic_html_format_without_image_uploads['name'] .= ' (without image uploads)';
    $basic_html_format_without_image_uploads['format'] = 'basic_html_without_image_uploads';
    FilterFormat::create($basic_html_format_without_image_uploads)->save();
    Editor::create([
        'format' => 'basic_html_without_image_uploads',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->setImageUploadSettings([
        'status' => FALSE,
    ])
        ->save();
    $allowed_html_parents = [
        'filters',
        'filter_html',
        'settings',
        'allowed_html',
    ];
    $current_value = NestedArray::getValue($basic_html_format, $allowed_html_parents);
    $new_value = str_replace([
        '<h4 id> ',
        '<h6 id> ',
    ], '', $current_value);
    $basic_html_format_without_h4_h6 = $basic_html_format;
    $basic_html_format_without_h4_h6['name'] .= ' (without H4 and H6)';
    $basic_html_format_without_h4_h6['format'] = 'basic_html_without_h4_h6';
    NestedArray::setValue($basic_html_format_without_h4_h6, $allowed_html_parents, $new_value);
    FilterFormat::create($basic_html_format_without_h4_h6)->save();
    Editor::create([
        'format' => 'basic_html_without_h4_h6',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    $new_value = str_replace([
        '<h2 id> ',
        '<h3 id> ',
        '<h4 id> ',
        '<h5 id> ',
        '<h6 id> ',
    ], '', $current_value);
    $basic_html_format_without_headings = $basic_html_format;
    $basic_html_format_without_headings['name'] .= ' (without H*)';
    $basic_html_format_without_headings['format'] = 'basic_html_without_headings';
    NestedArray::setValue($basic_html_format_without_headings, $allowed_html_parents, $new_value);
    FilterFormat::create($basic_html_format_without_headings)->save();
    Editor::create([
        'format' => 'basic_html_without_headings',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    $basic_html_format_with_pre = $basic_html_format;
    $basic_html_format_with_pre['name'] .= ' (with <pre>)';
    $basic_html_format_with_pre['format'] = 'basic_html_with_pre';
    NestedArray::setValue($basic_html_format_with_pre, $allowed_html_parents, $current_value . ' <pre>');
    FilterFormat::create($basic_html_format_with_pre)->save();
    Editor::create([
        'format' => 'basic_html_with_pre',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    $basic_html_format_with_h1 = $basic_html_format;
    $basic_html_format_with_h1['name'] .= ' (with <h1>)';
    $basic_html_format_with_h1['format'] = 'basic_html_with_h1';
    NestedArray::setValue($basic_html_format_with_h1, $allowed_html_parents, $current_value . ' <h1>');
    FilterFormat::create($basic_html_format_with_h1)->save();
    Editor::create([
        'format' => 'basic_html_with_h1',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    $new_value = str_replace('<p>', '<p class="text-align-center text-align-justify">', $current_value);
    $basic_html_format_with_alignable_p = $basic_html_format;
    $basic_html_format_with_alignable_p['name'] .= ' (with alignable paragraph support)';
    $basic_html_format_with_alignable_p['format'] = 'basic_html_with_alignable_p';
    NestedArray::setValue($basic_html_format_with_alignable_p, $allowed_html_parents, $new_value);
    FilterFormat::create($basic_html_format_with_alignable_p)->save();
    Editor::create([
        'format' => 'basic_html_with_alignable_p',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    $basic_html_format_with_media_embed = $basic_html_format;
    $basic_html_format_with_media_embed['name'] .= ' (with Media Embed support)';
    $basic_html_format_with_media_embed['format'] = 'basic_html_with_media_embed';
    // Add media_embed filter, update filter_html filter settings.
    $basic_html_format_with_media_embed['filters']['media_embed'] = [
        'status' => TRUE,
    ];
    $new_value = $current_value . ' <drupal-media data-entity-type data-entity-uuid data-align data-caption alt>';
    NestedArray::setValue($basic_html_format_with_media_embed, $allowed_html_parents, $new_value);
    FilterFormat::create($basic_html_format_with_media_embed)->save();
    $basic_html_editor_with_media_embed = Editor::create([
        'format' => 'basic_html_with_media_embed',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'));
    $settings = $basic_html_editor_with_media_embed->getSettings();
    // Add "insert media from library" button to CKEditor 4 configuration, the
    // pre-existing toolbar item group labeled "Media".
    $settings['toolbar']['rows'][0][3]['items'][] = 'DrupalMediaLibrary';
    $basic_html_editor_with_media_embed->setSettings($settings);
    $basic_html_editor_with_media_embed->save();
    $basic_html_format_with_media_embed_view_mode_invalid = $basic_html_format_with_media_embed;
    $basic_html_format_with_media_embed_view_mode_invalid['name'] = ' (with Media Embed support, view mode enabled but no view modes configured)';
    $basic_html_format_with_media_embed_view_mode_invalid['format'] = 'basic_html_with_media_embed_view_mode_enabled_no_view_modes_configured';
    $current_value_media_embed = NestedArray::getValue($basic_html_format_with_media_embed, $allowed_html_parents);
    $new_value = str_replace('<drupal-media data-entity-type data-entity-uuid data-align data-caption alt>', '<drupal-media data-entity-type data-entity-uuid data-align data-caption alt data-view-mode>', $current_value_media_embed);
    NestedArray::setValue($basic_html_format_with_media_embed_view_mode_invalid, $allowed_html_parents, $new_value);
    FilterFormat::create($basic_html_format_with_media_embed_view_mode_invalid)->save();
    $basic_html_editor_with_media_embed_view_mode_enabled_no_view_modes_configured = Editor::create([
        'format' => 'basic_html_with_media_embed_view_mode_enabled_no_view_modes_configured',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'));
    $settings = $basic_html_editor_with_media_embed_view_mode_enabled_no_view_modes_configured->getSettings();
    // Add "insert media from library" button to CKEditor 4 configuration, the
    // pre-existing toolbar item group labeled "Media".
    $settings['toolbar']['rows'][0][3]['items'][] = 'DrupalMediaLibrary';
    $basic_html_editor_with_media_embed_view_mode_enabled_no_view_modes_configured->setSettings($settings);
    $basic_html_editor_with_media_embed_view_mode_enabled_no_view_modes_configured->save();
    $new_value = str_replace('<img src alt height width data-entity-type data-entity-uuid data-align data-caption>', '<img src alt height width data-*>', $current_value);
    $basic_html_format_with_any_data_attr = $basic_html_format;
    $basic_html_format_with_any_data_attr['name'] .= ' (with any data-* attribute on images)';
    $basic_html_format_with_any_data_attr['format'] = 'basic_html_with_any_data_attr';
    NestedArray::setValue($basic_html_format_with_any_data_attr, $allowed_html_parents, $new_value);
    FilterFormat::create($basic_html_format_with_any_data_attr)->save();
    Editor::create([
        'format' => 'basic_html_with_any_data_attr',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'))->save();
    $basic_html_format_with_media_embed_view_mode_enabled_two_view_modes_configured = $basic_html_format_with_media_embed_view_mode_invalid;
    $basic_html_format_with_media_embed_view_mode_enabled_two_view_modes_configured['name'] = ' (with Media Embed support, view mode enabled and two view modes configured )';
    $basic_html_format_with_media_embed_view_mode_enabled_two_view_modes_configured['format'] = 'basic_html_with_media_embed_view_mode_enabled_two_view_modes_configured';
    FilterFormat::create($basic_html_format_with_media_embed_view_mode_enabled_two_view_modes_configured)->save();
    $basic_html_editor_with_media_embed_view_mode_enabled_two_view_modes_configured = Editor::create([
        'format' => 'basic_html_with_media_embed_view_mode_enabled_two_view_modes_configured',
    ] + Yaml::parseFile('core/modules/ckeditor5/tests/fixtures/ckeditor4_config/editor.editor.basic_html.yml'));
    $settings = $basic_html_editor_with_media_embed_view_mode_enabled_two_view_modes_configured->getSettings();
    // Add "insert media from library" button to CKEditor 4 configuration, the
    // pre-existing toolbar item group labeled "Media".
    $settings['toolbar']['rows'][0][3]['items'][] = 'DrupalMediaLibrary';
    $basic_html_editor_with_media_embed_view_mode_enabled_two_view_modes_configured->setSettings($settings);
    $basic_html_editor_with_media_embed_view_mode_enabled_two_view_modes_configured->save();
    EntityViewMode::create([
        'id' => 'media.view_mode_1',
        'targetEntityType' => 'media',
        'status' => TRUE,
        'enabled' => TRUE,
        'label' => 'View Mode 1',
    ])->save();
    EntityViewMode::create([
        'id' => 'media.view_mode_2',
        'targetEntityType' => 'media',
        'status' => TRUE,
        'enabled' => TRUE,
        'label' => 'View Mode 2',
    ])->save();
    $filter_format = FilterFormat::load('basic_html_with_media_embed_view_mode_enabled_two_view_modes_configured');
    $filter_format->setFilterConfig('media_embed', [
        'status' => TRUE,
        'settings' => [
            'default_view_mode' => 'view_mode_1',
            'allowed_media_types' => [],
            'allowed_view_modes' => [
                'view_mode_1' => 'view_mode_1',
                'view_mode_2' => 'view_mode_2',
            ],
        ],
    ])
        ->save();
    $filter_plugin_manager = $this->container
        ->get('plugin.manager.filter');
    FilterFormat::create([
        'format' => 'filter_only__filter_html',
        'name' => 'Only the "filter_html" filter and its default settings',
        'filters' => [
            'filter_html' => [
                'status' => 1,
                'settings' => $filter_plugin_manager->getDefinition('filter_html')['settings'],
            ],
        ],
    ])
        ->save();
    FilterFormat::create([
        'format' => 'cke4_stylescombo_span',
        'name' => 'A CKEditor 4 configured to have span styles',
        'filters' => [
            'filter_html' => [
                'status' => 1,
                'settings' => [
                    'allowed_html' => '<p> <br> <span class="llama">',
                ] + $filter_plugin_manager->getDefinition('filter_html')['settings'],
            ],
        ],
    ])
        ->save();
    Editor::create([
        'format' => 'cke4_stylescombo_span',
        'editor' => 'ckeditor',
        'image_upload' => [
            'status' => FALSE,
        ],
        'settings' => [
            'toolbar' => [
                'rows' => [
                    0 => [
                        [
                            'name' => 'Whatever',
                            'items' => [
                                'Styles',
                            ],
                        ],
                    ],
                ],
            ],
            'plugins' => [
                'stylescombo' => [
                    'styles' => "span.llama|Llama span",
                ],
            ],
        ],
    ])->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.