function CKEditor5CKEditor4Compatibility::setUp

Overrides BrowserTestBase::setUp

File

core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditor5CKEditor4Compatibility.php, line 41

Class

CKEditor5CKEditor4Compatibility
Ensures that CKEditor 5 can be used on the same page with CKEditor 4.

Namespace

Drupal\Tests\ckeditor\FunctionalJavascript

Code

protected function setUp() : void {
    parent::setUp();
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    $this->drupalLogin($this->drupalCreateUser([
        'administer filters',
        'create page content',
        'edit own page content',
    ]));
    $current_user_roles = $this->loggedInUser
        ->getRoles(TRUE);
    // Create text format, text editor and text fields for CKEditor 5 and 4.
    foreach ([
        5 => 'ckeditor5',
        4 => 'ckeditor',
    ] as $version => $text_editor_plugin_id) {
        $format_id = sprintf('test_format_for_ckeditor%d', $version);
        $field_name = sprintf('field_text_ckeditor%d', $version);
        FilterFormat::create([
            'format' => $format_id,
            'name' => sprintf('CKEditor %d editor', $version),
            'roles' => $current_user_roles,
            'filters' => [
                'filter_html' => [
                    'status' => TRUE,
                    'settings' => [
                        'allowed_html' => '<p> <br> <h2> <h3> <h4> <h5> <h6> <strong> <em>',
                    ],
                ],
            ],
        ])->save();
        Editor::create([
            'editor' => $text_editor_plugin_id,
            'format' => $format_id,
            'settings' => $version === 4 ? [] : [
                'toolbar' => [
                    'items' => [
                        'heading',
                        'bold',
                        'italic',
                    ],
                ],
                'plugins' => [
                    'ckeditor5_heading' => [
                        'enabled_headings' => [
                            'heading2',
                            'heading3',
                            'heading4',
                            'heading5',
                            'heading6',
                        ],
                    ],
                ],
            ],
            'image_upload' => [
                'status' => FALSE,
            ],
        ])->save();
        if ($version === 5) {
            $this->assertSame([], array_map(function (ConstraintViolation $v) {
                return (string) $v->getMessage();
            }, iterator_to_array(CKEditor5::validatePair(Editor::load($format_id), FilterFormat::load($format_id)))));
        }
        $field_storage = FieldStorageConfig::create([
            'field_name' => $field_name,
            'entity_type' => 'node',
            'type' => 'text_long',
        ]);
        $field_storage->save();
        FieldConfig::create([
            'field_storage' => $field_storage,
            'entity_type' => 'node',
            'bundle' => 'page',
        ])->save();
        // Add the new field to the default form display.
        EntityFormDisplay::load('node.page.default')->setComponent($field_name, [
            'type' => 'text_textarea',
        ])
            ->save();
    }
}

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