View source
<?php
namespace Drupal\Tests\editor\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
class EditorAdminTest extends BrowserTestBase {
public static $modules = [
'filter',
'editor',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() {
parent::setUp();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [],
]);
$filtered_html_format
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer filters',
]);
}
public function testNoEditorAvailable() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$raw_content = $this
->getSession()
->getPage()
->getContent();
$roles_pos = strpos($raw_content, 'Roles');
$editor_pos = strpos($raw_content, 'Text editor');
$filters_pos = strpos($raw_content, 'Enabled filters');
$this
->assertTrue($roles_pos < $editor_pos && $editor_pos < $filters_pos, '"Text Editor" select appears in the correct location of the text format configuration UI.');
$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
->assertCount(1, $select, 'The Text Editor select exists.');
$this
->assertCount(1, $select_is_disabled, 'The Text Editor select is disabled.');
$this
->assertCount(1, $options, 'The Text Editor select has only one option.');
$this
->assertTrue($options[0]
->getText() === 'None', 'Option 1 in the Text Editor select is "None".');
$this
->assertRaw('This option is disabled because no modules that provide a text editor are currently enabled.', 'Description for select present that tells users to install a text editor module.');
}
public function testAddEditorToExistingFormat() {
$this
->enableUnicornEditor();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$edit = $this
->selectUnicornEditor();
$edit['editor[settings][ponies_too]'] = FALSE;
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this
->verifyUnicornEditorConfiguration('filtered_html', FALSE);
$edit = [
'editor[editor]' => '',
];
$this
->drupalPostForm(NULL, $edit, 'Configure');
$unicorn_setting = $this
->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
$this
->assertCount(0, $unicorn_setting, "Unicorn Editor's settings form is no longer present.");
}
public function testAddEditorToNewFormat() {
$this
->addEditorToNewFormat('monoceros', 'Monoceros');
$this
->verifyUnicornEditorConfiguration('monoceros');
}
public function testDisableFormatWithEditor() {
$formats = [
'monoceros' => 'Monoceros',
'tattoo' => 'Tattoo',
];
$this->container
->get('module_installer')
->install([
'node',
]);
$this
->resetAll();
$node_type = NodeType::create([
'type' => mb_strtolower($this
->randomMachineName()),
]);
$node_type
->save();
node_add_body_field($node_type, $this
->randomString());
$permissions = [
'administer filters',
"edit any {$node_type->id()} content",
];
foreach ($formats as $format => $name) {
$this
->addEditorToNewFormat($format, $name);
$permissions[] = "use text format {$format}";
}
$node = Node::create([
'type' => $node_type
->id(),
'title' => $this
->randomString(),
]);
$node->body->value = $this
->randomString(100);
$node->body->format = 'monoceros';
$node
->save();
$account = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($account);
$text = (string) new FormattableMarkup('<em>Edit @type</em> @title', [
'@type' => $node_type
->label(),
'@title' => $node
->label(),
]);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertRaw($text);
FilterFormat::load('monoceros')
->disable()
->save();
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertRaw($text);
}
public function testSwitchEditorToNone() {
$this
->enableUnicornEditor();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$edit = $this
->selectUnicornEditor();
$edit = [
'editor[editor]' => '',
];
$this
->submitForm($edit, 'Configure');
$this
->submitForm($edit, 'Save configuration');
}
protected function addEditorToNewFormat($format_id, $format_name) {
$this
->enableUnicornEditor();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/formats/add');
$edit = [
'name' => $format_name,
'format' => $format_id,
];
$edit += $this
->selectUnicornEditor();
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
}
protected function enableUnicornEditor() {
if (!$this->container
->get('module_handler')
->moduleExists('editor_test')) {
$this->container
->get('module_installer')
->install([
'editor_test',
]);
}
}
protected function selectUnicornEditor() {
$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
->assertCount(1, $select, 'The Text Editor select exists.');
$this
->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this
->assertCount(2, $options, 'The Text Editor select has two options.');
$this
->assertTrue($options[0]
->getText() === 'None', 'Option 1 in the Text Editor select is "None".');
$this
->assertTrue($options[1]
->getText() === 'Unicorn Editor', 'Option 2 in the Text Editor select is "Unicorn Editor".');
$this
->assertTrue($options[0]
->hasAttribute('selected'), 'Option 1 ("None") is selected.');
$this
->assertNoRaw('This option is disabled because no modules that provide a text editor are currently enabled.', 'Description for select absent that tells users to install a text editor module.');
$edit = [
'editor[editor]' => 'unicorn',
];
$this
->drupalPostForm(NULL, $edit, 'Configure');
$unicorn_setting = $this
->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
$this
->assertCount(1, $unicorn_setting, "Unicorn Editor's settings form is present.");
return $edit;
}
protected function verifyUnicornEditorConfiguration($format_id, $ponies_too = TRUE) {
$editor = editor_load($format_id);
$settings = $editor
->getSettings();
$this
->assertIdentical($editor
->getEditor(), 'unicorn', 'The text editor is configured correctly.');
$this
->assertIdentical($settings['ponies_too'], $ponies_too, 'The text editor settings are stored correctly.');
$this
->drupalGet('admin/config/content/formats/manage/' . $format_id);
$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
->assertCount(1, $select, 'The Text Editor select exists.');
$this
->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this
->assertCount(2, $options, 'The Text Editor select has two options.');
$this
->assertTrue($options[1]
->hasAttribute('selected'), 'Option 2 ("Unicorn Editor") is selected.');
}
}