function EditorManagerTest::testManager

Same name in other branches
  1. 9 core/modules/editor/tests/src/Kernel/EditorManagerTest.php \Drupal\Tests\editor\Kernel\EditorManagerTest::testManager()
  2. 8.9.x core/modules/editor/tests/src/Kernel/EditorManagerTest.php \Drupal\Tests\editor\Kernel\EditorManagerTest::testManager()
  3. 10 core/modules/editor/tests/src/Kernel/EditorManagerTest.php \Drupal\Tests\editor\Kernel\EditorManagerTest::testManager()

Tests the configurable text editor manager.

File

core/modules/editor/tests/src/Kernel/EditorManagerTest.php, line 58

Class

EditorManagerTest
Tests detection of text editors and correct generation of attachments.

Namespace

Drupal\Tests\editor\Kernel

Code

public function testManager() : void {
    $this->editorManager = $this->container
        ->get('plugin.manager.editor');
    // Case 1: no text editor available:
    // - listOptions() should return an empty list of options
    // - getAttachments() should return an empty #attachments array (and not
    //   a JS settings structure that is empty)
    $this->assertSame([], $this->editorManager
        ->listOptions(), 'When no text editor is enabled, the manager works correctly.');
    $this->assertSame([], $this->editorManager
        ->getAttachments([]), 'No attachments when no text editor is enabled and retrieving attachments for zero text formats.');
    $this->assertSame([], $this->editorManager
        ->getAttachments([
        'filtered_html',
        'full_html',
    ]), 'No attachments when no text editor is enabled and retrieving attachments for multiple text formats.');
    // Enable the Text Editor Test module, which has the Unicorn Editor and
    // clear the editor manager's cache so it is picked up.
    $this->enableModules([
        'editor_test',
    ]);
    $this->editorManager = $this->container
        ->get('plugin.manager.editor');
    $this->editorManager
        ->clearCachedDefinitions();
    // Case 2: a text editor available.
    $this->assertSame('Unicorn Editor', (string) $this->editorManager
        ->listOptions()['unicorn'], 'When some text editor is enabled, the manager works correctly.');
    // Case 3: a text editor available & associated (but associated only with
    // the 'Full HTML' text format).
    $unicorn_plugin = $this->editorManager
        ->createInstance('unicorn');
    $editor = Editor::create([
        'format' => 'full_html',
        'editor' => 'unicorn',
        'image_upload' => [
            'status' => FALSE,
        ],
    ]);
    $editor->save();
    $this->assertSame([], $this->editorManager
        ->getAttachments([]), 'No attachments when one text editor is enabled and retrieving attachments for zero text formats.');
    $expected = [
        'library' => [
            0 => 'editor_test/unicorn',
        ],
        'drupalSettings' => [
            'editor' => [
                'formats' => [
                    'full_html' => [
                        'format' => 'full_html',
                        'editor' => 'unicorn',
                        'editorSettings' => $unicorn_plugin->getJSSettings($editor),
                        'editorSupportsContentFiltering' => TRUE,
                        'isXssSafe' => FALSE,
                    ],
                ],
            ],
        ],
    ];
    $this->assertSame($expected, $this->editorManager
        ->getAttachments([
        'filtered_html',
        'full_html',
    ]), 'Correct attachments when one text editor is enabled and retrieving attachments for multiple text formats.');
    // Case 4: a text editor available associated, but now with its JS settings
    // being altered via hook_editor_js_settings_alter().
    \Drupal::state()->set('editor_test_js_settings_alter_enabled', TRUE);
    $expected['drupalSettings']['editor']['formats']['full_html']['editorSettings']['ponyModeEnabled'] = FALSE;
    $this->assertSame($expected, $this->editorManager
        ->getAttachments([
        'filtered_html',
        'full_html',
    ]), 'hook_editor_js_settings_alter() works correctly.');
}

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