3 namespace Drupal\editor\Tests;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\simpletest\WebTestBase;
12 * Tests administration of text editors.
16 class EditorAdminTest extends WebTestBase {
23 public static $modules = ['filter', 'editor'];
26 * A user with the 'administer filters' permission.
28 * @var \Drupal\user\UserInterface
32 protected function setUp() {
36 $filtered_html_format = FilterFormat::create([
37 'format' => 'filtered_html',
38 'name' => 'Filtered HTML',
42 $filtered_html_format->save();
45 $this->adminUser = $this->drupalCreateUser(['administer filters']);
49 * Tests an existing format without any editors available.
51 public function testNoEditorAvailable() {
52 $this->drupalLogin($this->adminUser);
53 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
55 // Ensure the form field order is correct.
56 $roles_pos = strpos($this->getRawContent(), 'Roles');
57 $editor_pos = strpos($this->getRawContent(), 'Text editor');
58 $filters_pos = strpos($this->getRawContent(), 'Enabled filters');
59 $this->assertTrue($roles_pos < $editor_pos && $editor_pos < $filters_pos, '"Text Editor" select appears in the correct location of the text format configuration UI.');
61 // Verify the <select>.
62 $select = $this->xpath('//select[@name="editor[editor]"]');
63 $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
64 $options = $this->xpath('//select[@name="editor[editor]"]/option');
65 $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
66 $this->assertTrue(count($select_is_disabled) === 1, 'The Text Editor select is disabled.');
67 $this->assertTrue(count($options) === 1, 'The Text Editor select has only one option.');
68 $this->assertTrue(((string) $options[0]) === 'None', 'Option 1 in the Text Editor select is "None".');
69 $this->assertRaw(t('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.');
73 * Tests adding a text editor to an existing text format.
75 public function testAddEditorToExistingFormat() {
76 $this->enableUnicornEditor();
77 $this->drupalLogin($this->adminUser);
78 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
79 $edit = $this->selectUnicornEditor();
80 // Configure Unicorn Editor's setting to another value.
81 $edit['editor[settings][ponies_too]'] = FALSE;
82 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
83 $this->verifyUnicornEditorConfiguration('filtered_html', FALSE);
85 // Switch back to 'None' and check the Unicorn Editor's settings are gone.
87 'editor[editor]' => '',
89 $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
90 $unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
91 $this->assertTrue(count($unicorn_setting) === 0, "Unicorn Editor's settings form is no longer present.");
95 * Tests adding a text editor to a new text format.
97 public function testAddEditorToNewFormat() {
98 $this->addEditorToNewFormat('monocerus', 'Monocerus');
99 $this->verifyUnicornEditorConfiguration('monocerus');
103 * Tests format disabling.
105 public function testDisableFormatWithEditor() {
106 $formats = ['monocerus' => 'Monocerus', 'tattoo' => 'Tattoo'];
108 // Install the node module.
109 $this->container->get('module_installer')->install(['node']);
111 // Create a new node type and attach the 'body' field to it.
112 $node_type = NodeType::create(['type' => Unicode::strtolower($this->randomMachineName())]);
114 node_add_body_field($node_type, $this->randomString());
116 $permissions = ['administer filters', "edit any {$node_type->id()} content"];
117 foreach ($formats as $format => $name) {
118 // Create a format and add an editor to this format.
119 $this->addEditorToNewFormat($format, $name);
120 // Add permission for this format.
121 $permissions[] = "use text format $format";
124 // Create a node having the body format value 'moncerus'.
125 $node = Node::create([
126 'type' => $node_type->id(),
127 'title' => $this->randomString(),
129 $node->body->value = $this->randomString(100);
130 $node->body->format = 'monocerus';
133 // Log in as an user able to use both formats and edit nodes of created type.
134 $account = $this->drupalCreateUser($permissions);
135 $this->drupalLogin($account);
137 // The node edit page header.
138 $text = t('<em>Edit @type</em> @title', ['@type' => $node_type->label(), '@title' => $node->label()]);
140 // Go to node edit form.
141 $this->drupalGet('node/' . $node->id() . '/edit');
142 $this->assertRaw($text);
144 // Disable the format assigned to the 'body' field of the node.
145 FilterFormat::load('monocerus')->disable()->save();
147 // Edit again the node.
148 $this->drupalGet('node/' . $node->id() . '/edit');
149 $this->assertRaw($text);
153 * Adds an editor to a new format using the UI.
155 * @param string $format_id
157 * @param string $format_name
160 protected function addEditorToNewFormat($format_id, $format_name) {
161 $this->enableUnicornEditor();
162 $this->drupalLogin($this->adminUser);
163 $this->drupalGet('admin/config/content/formats/add');
164 // Configure the text format name.
166 'name' => $format_name,
167 'format' => $format_id,
169 $edit += $this->selectUnicornEditor();
170 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
174 * Enables the unicorn editor.
176 protected function enableUnicornEditor() {
177 if (!$this->container->get('module_handler')->moduleExists('editor_test')) {
178 $this->container->get('module_installer')->install(['editor_test']);
183 * Tests and selects the unicorn editor.
186 * Returns an edit array containing the values to be posted.
188 protected function selectUnicornEditor() {
189 // Verify the <select> when a text editor is available.
190 $select = $this->xpath('//select[@name="editor[editor]"]');
191 $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
192 $options = $this->xpath('//select[@name="editor[editor]"]/option');
193 $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
194 $this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
195 $this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
196 $this->assertTrue(((string) $options[0]) === 'None', 'Option 1 in the Text Editor select is "None".');
197 $this->assertTrue(((string) $options[1]) === 'Unicorn Editor', 'Option 2 in the Text Editor select is "Unicorn Editor".');
198 $this->assertTrue(((string) $options[0]['selected']) === 'selected', 'Option 1 ("None") is selected.');
199 // Ensure the none option is selected.
200 $this->assertNoRaw(t('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.');
202 // Select the "Unicorn Editor" editor and click the "Configure" button.
204 'editor[editor]' => 'unicorn',
206 $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
207 $unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
208 $this->assertTrue(count($unicorn_setting), "Unicorn Editor's settings form is present.");
214 * Verifies unicorn editor configuration.
216 * @param string $format_id
217 * The format machine name.
218 * @param bool $ponies_too
219 * The expected value of the ponies_too setting.
221 protected function verifyUnicornEditorConfiguration($format_id, $ponies_too = TRUE) {
222 $editor = editor_load($format_id);
223 $settings = $editor->getSettings();
224 $this->assertIdentical($editor->getEditor(), 'unicorn', 'The text editor is configured correctly.');
225 $this->assertIdentical($settings['ponies_too'], $ponies_too, 'The text editor settings are stored correctly.');
226 $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
227 $select = $this->xpath('//select[@name="editor[editor]"]');
228 $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
229 $options = $this->xpath('//select[@name="editor[editor]"]/option');
230 $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
231 $this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
232 $this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
233 $this->assertTrue(((string) $options[1]['selected']) === 'selected', 'Option 2 ("Unicorn Editor") is selected.');