function EditorManagerTest::testManager

Tests the configurable text editor manager.

File

drupal/core/modules/editor/lib/Drupal/editor/Tests/EditorManagerTest.php, line 68
Definition of \Drupal\editor\Tests\EditorManagerTest.

Class

EditorManagerTest
Unit tests for the configurable text editor manager.

Namespace

Drupal\editor\Tests

Code

function testManager() {
  $this->editorManager = new EditorManager($this->container
    ->get('container.namespaces'));

  // 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
    ->assertIdentical(array(), $this->editorManager
    ->listOptions(), 'When no text editor is enabled, the manager works correctly.');
  $this
    ->assertIdentical(array(), $this->editorManager
    ->getAttachments(array()), 'No attachments when no text editor is enabled and retrieving attachments for zero text formats.');
  $this
    ->assertIdentical(array(), $this->editorManager
    ->getAttachments(array(
    '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(array(
    'editor_test',
  ));
  $this->editorManager
    ->clearCachedDefinitions();

  // Case 2: a text editor available.
  $this
    ->assertIdentical(array(
    'unicorn' => 'Unicorn Editor',
  ), $this->editorManager
    ->listOptions(), '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 = entity_create('editor', array(
    'format' => 'full_html',
    'editor' => 'unicorn',
  ));
  $editor
    ->save();
  $this
    ->assertIdentical(array(), $this->editorManager
    ->getAttachments(array()), 'No attachments when one text editor is enabled and retrieving attachments for zero text formats.');
  $expected = array(
    'library' => array(
      0 => array(
        'edit_test',
        'unicorn',
      ),
    ),
    'js' => array(
      0 => array(
        'type' => 'setting',
        'data' => array(
          'editor' => array(
            'formats' => array(
              'full_html' => array(
                'editor' => 'unicorn',
                'editorSettings' => $unicorn_plugin
                  ->getJSSettings($editor),
              ),
            ),
          ),
        ),
      ),
    ),
  );
  $this
    ->assertIdentical($expected, $this->editorManager
    ->getAttachments(array(
    '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['js'][0]['data']['editor']['formats']['full_html']['editorSettings']['ponyModeEnabled'] = FALSE;
  $this
    ->assertIdentical($expected, $this->editorManager
    ->getAttachments(array(
    'filtered_html',
    'full_html',
  )), 'hook_editor_js_settings_alter() works correctly.');
}