4315a6dabd859cc93398f324b98741941f2792b2
[yaffs-website] / web / core / modules / editor / src / Tests / EditorAdminTest.php
1 <?php
2
3 namespace Drupal\editor\Tests;
4
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;
10
11 /**
12  * Tests administration of text editors.
13  *
14  * @group editor
15  */
16 class EditorAdminTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['filter', 'editor'];
24
25   /**
26    * A user with the 'administer filters' permission.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   protected function setUp() {
33     parent::setUp();
34
35     // Add text format.
36     $filtered_html_format = FilterFormat::create([
37       'format' => 'filtered_html',
38       'name' => 'Filtered HTML',
39       'weight' => 0,
40       'filters' => [],
41     ]);
42     $filtered_html_format->save();
43
44     // Create admin user.
45     $this->adminUser = $this->drupalCreateUser(['administer filters']);
46   }
47
48   /**
49    * Tests an existing format without any editors available.
50    */
51   public function testNoEditorAvailable() {
52     $this->drupalLogin($this->adminUser);
53     $this->drupalGet('admin/config/content/formats/manage/filtered_html');
54
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.');
60
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.');
70   }
71
72   /**
73    * Tests adding a text editor to an existing text format.
74    */
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);
84
85     // Switch back to 'None' and check the Unicorn Editor's settings are gone.
86     $edit = [
87       'editor[editor]' => '',
88     ];
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.");
92   }
93
94   /**
95    * Tests adding a text editor to a new text format.
96    */
97   public function testAddEditorToNewFormat() {
98     $this->addEditorToNewFormat('monocerus', 'Monocerus');
99     $this->verifyUnicornEditorConfiguration('monocerus');
100   }
101
102   /**
103    * Tests format disabling.
104    */
105   public function testDisableFormatWithEditor() {
106     $formats = ['monocerus' => 'Monocerus', 'tattoo' => 'Tattoo'];
107
108     // Install the node module.
109     $this->container->get('module_installer')->install(['node']);
110     $this->resetAll();
111     // Create a new node type and attach the 'body' field to it.
112     $node_type = NodeType::create(['type' => Unicode::strtolower($this->randomMachineName())]);
113     $node_type->save();
114     node_add_body_field($node_type, $this->randomString());
115
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";
122     }
123
124     // Create a node having the body format value 'moncerus'.
125     $node = Node::create([
126       'type' => $node_type->id(),
127       'title' => $this->randomString(),
128     ]);
129     $node->body->value = $this->randomString(100);
130     $node->body->format = 'monocerus';
131     $node->save();
132
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);
136
137     // The node edit page header.
138     $text = t('<em>Edit @type</em> @title', ['@type' => $node_type->label(), '@title' => $node->label()]);
139
140     // Go to node edit form.
141     $this->drupalGet('node/' . $node->id() . '/edit');
142     $this->assertRaw($text);
143
144     // Disable the format assigned to the 'body' field of the node.
145     FilterFormat::load('monocerus')->disable()->save();
146
147     // Edit again the node.
148     $this->drupalGet('node/' . $node->id() . '/edit');
149     $this->assertRaw($text);
150   }
151
152   /**
153    * Adds an editor to a new format using the UI.
154    *
155    * @param string $format_id
156    *   The format id.
157    * @param string $format_name
158    *   The format name.
159    */
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.
165     $edit = [
166       'name' => $format_name,
167       'format' => $format_id,
168     ];
169     $edit += $this->selectUnicornEditor();
170     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
171   }
172
173   /**
174    * Enables the unicorn editor.
175    */
176   protected function enableUnicornEditor() {
177     if (!$this->container->get('module_handler')->moduleExists('editor_test')) {
178       $this->container->get('module_installer')->install(['editor_test']);
179     }
180   }
181
182   /**
183    * Tests and selects the unicorn editor.
184    *
185    * @return array
186    *   Returns an edit array containing the values to be posted.
187    */
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.');
201
202     // Select the "Unicorn Editor" editor and click the "Configure" button.
203     $edit = [
204       'editor[editor]' => 'unicorn',
205     ];
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.");
209
210     return $edit;
211   }
212
213   /**
214    * Verifies unicorn editor configuration.
215    *
216    * @param string $format_id
217    *   The format machine name.
218    * @param bool $ponies_too
219    *   The expected value of the ponies_too setting.
220    */
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.');
234   }
235
236 }