Commit bc5bfc91 authored by alexpott's avatar alexpott

Issue #2207777 by Wim Leers, pjonckiere, Rajesh Ashok | alexpott: Can not...

Issue #2207777 by Wim Leers, pjonckiere, Rajesh Ashok | alexpott: Can not configure editor whilst creating a new text format.
parent fe26eb63
......@@ -49,6 +49,10 @@ function ckeditor_theme() {
* Implements hook_ckeditor_css_alter().
*/
function ckeditor_ckeditor_css_alter(array &$css, Editor $editor) {
if (!$editor->hasAssociatedFilterFormat()) {
return;
}
// Add the filter caption CSS if the text format associated with this text
// editor uses the filter_caption filter. This is used by the included
// CKEditor DrupalImageCaption plugin.
......
......@@ -68,6 +68,10 @@ public function getConfig(Editor $editor) {
* {@inheritdoc}
*/
function isEnabled(Editor $editor) {
if (!$editor->hasAssociatedFilterFormat()) {
return FALSE;
}
// Automatically enable this plugin if the text format associated with this
// text editor uses the filter_caption filter and the DrupalImage button is
// enabled.
......
......@@ -289,9 +289,16 @@ protected function generateFormatTagsSetting(Editor $editor) {
* array or FALSE. The latter indicates that nothing is disallowed.
*/
protected function generateACFSettings(Editor $editor) {
// When nothing is disallowed, set allowedContent to true.
// When no text format is associated yet, assume nothing is disallowed, so
// set allowedContent to true.
if (!$editor->hasAssociatedFilterFormat()) {
return TRUE;
}
$format = $editor->getFilterFormat();
$filter_types = $format->getFilterTypes();
// When nothing is disallowed, set allowedContent to true.
if (!in_array(FilterInterface::TYPE_HTML_RESTRICTOR, $filter_types)) {
return array(TRUE, FALSE);
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\ckeditor\Tests;
use Drupal\editor\Entity\Editor;
use Drupal\filter\FilterFormatInterface;
use Drupal\simpletest\WebTestBase;
/**
......@@ -46,7 +47,10 @@ function setUp() {
$this->admin_user = $this->drupalCreateUser(array('administer filters'));
}
function testAdmin() {
/**
* Tests configuring a text editor for an existing text format.
*/
function testExistingFormat() {
$ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
$this->drupalLogin($this->admin_user);
......@@ -187,4 +191,66 @@ function testAdmin() {
$this->assertIdentical($expected_settings, $editor->getSettings());
}
/**
* Tests configuring a text editor for a new text format.
*
* This test only needs to ensure that the basics of the CKEditor
* configuration form work; details are tested in testExistingFormat().
*/
function testNewFormat() {
$this->drupalLogin($this->admin_user);
$this->drupalGet('admin/config/content/formats/add');
// Verify the "Text Editor" <select> when a text editor is available.
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
$this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
$this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
$this->assertTrue(((string) $options[0]) === 'None', 'Option 1 in the Text Editor select is "None".');
$this->assertTrue(((string) $options[1]) === 'CKEditor', 'Option 2 in the Text Editor select is "CKEditor".');
$this->assertTrue(((string) $options[0]['selected']) === 'selected', 'Option 1 ("None") is selected.');
// Name our fancy new text format, select the "CKEditor" editor and click
// the "Configure" button.
$edit = array(
'name' => 'My amazing text format',
'format' => 'amazing_format',
'editor[editor]' => 'ckeditor',
);
$this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
$filter_format = entity_load('filter_format', 'amazing_format');
$this->assertFalse($filter_format, 'No FilterFormat config entity exists yet.');
$editor = entity_load('editor', 'amazing_format');
$this->assertFalse($editor, 'No Editor config entity exists yet.');
// Ensure the toolbar buttons configuration value is initialized to the
// default value.
$ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
$default_settings = $ckeditor->getDefaultSettings();
$expected_buttons_value = json_encode($default_settings['toolbar']['rows']);
$this->assertFieldByName('editor[settings][toolbar][button_groups]', $expected_buttons_value);
// Ensure the styles textarea exists and is initialized empty.
$styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
$this->assertFieldByXPath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]', '', 'The styles textarea exists and is empty.');
$this->assertTrue(count($styles_textarea) === 1, 'The "styles" textarea exists.');
// Submit the form to create both a new text format and an associated text
// editor.
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
// Ensure a FilterFormat object exists now.
$filter_format = entity_load('filter_format', 'amazing_format');
$this->assertTrue($filter_format instanceof FilterFormatInterface, 'A FilterFormat config entity exists now.');
// Ensure an Editor object exists now, with the proper settings.
$expected_settings = $default_settings;
$expected_settings['plugins']['stylescombo']['styles'] = '';
$editor = entity_load('editor', 'amazing_format');
$this->assertTrue($editor instanceof Editor, 'An Editor config entity exists now.');
$this->assertIdentical($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
}
}
......@@ -14,10 +14,24 @@
*/
interface EditorInterface extends ConfigEntityInterface {
/**
* Returns whether this text editor has an associated filter format.
*
* A text editor may be created at the same time as the filter format it's
* going to be associated with; in that case, no filter format object is
* available yet.
*
* @return bool
*/
public function hasAssociatedFilterFormat();
/**
* Returns the filter format this text editor is associated with.
*
* @return \Drupal\filter\FilterFormatInterface
* This could be NULL if the associated filter format is still being created.
* @see hasAssociatedFilterFormat()
*
* @return \Drupal\filter\FilterFormatInterface|null
*/
public function getFilterFormat();
......
......@@ -102,6 +102,13 @@ public function calculateDependencies() {
return $this->dependencies;
}
/**
* {@inheritdoc}
*/
public function hasAssociatedFilterFormat() {
return $this->format !== NULL;
}
/**
* {@inheritdoc}
*/
......
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\filter\Entity\FilterFormatInterface.
* Contains \Drupal\filter\FilterFormatInterface.
*/
namespace Drupal\filter;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment