Newer
Older
Angie Byron
committed
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\editor\Tests\EditorManagerTest.
Angie Byron
committed
*/
namespace Drupal\editor\Tests;
use Drupal\editor\Entity\Editor;
use Drupal\simpletest\KernelTestBase;
use Drupal\filter\Entity\FilterFormat;
Angie Byron
committed
/**
* Tests detection of text editors and correct generation of attachments.
*
* @group editor
Angie Byron
committed
*/
class EditorManagerTest extends KernelTestBase {
Angie Byron
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system', 'user', 'filter', 'editor');
Angie Byron
committed
/**
* The manager for text editor plugins.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $editorManager;
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
parent::setUp();
// Install the Filter module.
Angie Byron
committed
$this->installSchema('system', 'url_alias');
Angie Byron
committed
// Add text formats.
$filtered_html_format = FilterFormat::create(array(
Angie Byron
committed
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
Angie Byron
committed
));
$filtered_html_format->save();
$full_html_format = FilterFormat::create(array(
Angie Byron
committed
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
Angie Byron
committed
));
$full_html_format->save();
Angie Byron
committed
}
/**
* Tests the configurable text editor manager.
*/
Angie Byron
committed
public function testManager() {
Alex Pott
committed
$this->editorManager = $this->container->get('plugin.manager.editor');
Angie Byron
committed
// 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 = $this->container->get('plugin.manager.editor');
Angie Byron
committed
$this->editorManager->clearCachedDefinitions();
// Case 2: a text editor available.
$this->assertIdentical('Unicorn Editor', (string) $this->editorManager->listOptions()['unicorn'], 'When some text editor is enabled, the manager works correctly.');
Angie Byron
committed
// 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([
Angie Byron
committed
'format' => 'full_html',
'editor' => 'unicorn',
Angie Byron
committed
$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 => 'editor_test/unicorn',
Angie Byron
committed
),
'drupalSettings' => [
'editor' => [
'formats' => [
'full_html' => [
Dries Buytaert
committed
'format' => 'full_html',
Angie Byron
committed
'editor' => 'unicorn',
'editorSettings' => $unicorn_plugin->getJSSettings($editor),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
],
],
],
],
Angie Byron
committed
);
$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.');
Angie Byron
committed
// 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;
Angie Byron
committed
$this->assertIdentical($expected, $this->editorManager->getAttachments(array('filtered_html', 'full_html')), 'hook_editor_js_settings_alter() works correctly.');