Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / editor / tests / src / Functional / EditorAdminTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Functional;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Tests\BrowserTestBase;
10
11 /**
12  * Tests administration of text editors.
13  *
14  * @group editor
15  */
16 class EditorAdminTest extends BrowserTestBase {
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     $raw_content = $this->getSession()->getPage()->getContent();
57     $roles_pos = strpos($raw_content, 'Roles');
58     $editor_pos = strpos($raw_content, 'Text editor');
59     $filters_pos = strpos($raw_content, 'Enabled filters');
60     $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
62     // Verify the <select>.
63     $select = $this->xpath('//select[@name="editor[editor]"]');
64     $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
65     $options = $this->xpath('//select[@name="editor[editor]"]/option');
66     $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
67     $this->assertTrue(count($select_is_disabled) === 1, 'The Text Editor select is disabled.');
68     $this->assertTrue(count($options) === 1, 'The Text Editor select has only one option.');
69     $this->assertTrue(($options[0]->getText()) === 'None', 'Option 1 in the Text Editor select is "None".');
70     $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.');
71   }
72
73   /**
74    * Tests adding a text editor to an existing text format.
75    */
76   public function testAddEditorToExistingFormat() {
77     $this->enableUnicornEditor();
78     $this->drupalLogin($this->adminUser);
79     $this->drupalGet('admin/config/content/formats/manage/filtered_html');
80     $edit = $this->selectUnicornEditor();
81     // Configure Unicorn Editor's setting to another value.
82     $edit['editor[settings][ponies_too]'] = FALSE;
83     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
84     $this->verifyUnicornEditorConfiguration('filtered_html', FALSE);
85
86     // Switch back to 'None' and check the Unicorn Editor's settings are gone.
87     $edit = [
88       'editor[editor]' => '',
89     ];
90     $this->drupalPostForm(NULL, $edit, 'Configure');
91     $unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
92     $this->assertTrue(count($unicorn_setting) === 0, "Unicorn Editor's settings form is no longer present.");
93   }
94
95   /**
96    * Tests adding a text editor to a new text format.
97    */
98   public function testAddEditorToNewFormat() {
99     $this->addEditorToNewFormat('monocerus', 'Monocerus');
100     $this->verifyUnicornEditorConfiguration('monocerus');
101   }
102
103   /**
104    * Tests format disabling.
105    */
106   public function testDisableFormatWithEditor() {
107     $formats = ['monocerus' => 'Monocerus', 'tattoo' => 'Tattoo'];
108
109     // Install the node module.
110     $this->container->get('module_installer')->install(['node']);
111     $this->resetAll();
112     // Create a new node type and attach the 'body' field to it.
113     $node_type = NodeType::create(['type' => mb_strtolower($this->randomMachineName())]);
114     $node_type->save();
115     node_add_body_field($node_type, $this->randomString());
116
117     $permissions = ['administer filters', "edit any {$node_type->id()} content"];
118     foreach ($formats as $format => $name) {
119       // Create a format and add an editor to this format.
120       $this->addEditorToNewFormat($format, $name);
121       // Add permission for this format.
122       $permissions[] = "use text format $format";
123     }
124
125     // Create a node having the body format value 'moncerus'.
126     $node = Node::create([
127       'type' => $node_type->id(),
128       'title' => $this->randomString(),
129     ]);
130     $node->body->value = $this->randomString(100);
131     $node->body->format = 'monocerus';
132     $node->save();
133
134     // Log in as an user able to use both formats and edit nodes of created type.
135     $account = $this->drupalCreateUser($permissions);
136     $this->drupalLogin($account);
137
138     // The node edit page header.
139     $text = (string) new FormattableMarkup('<em>Edit @type</em> @title', ['@type' => $node_type->label(), '@title' => $node->label()]);
140
141     // Go to node edit form.
142     $this->drupalGet('node/' . $node->id() . '/edit');
143     $this->assertRaw($text);
144
145     // Disable the format assigned to the 'body' field of the node.
146     FilterFormat::load('monocerus')->disable()->save();
147
148     // Edit again the node.
149     $this->drupalGet('node/' . $node->id() . '/edit');
150     $this->assertRaw($text);
151   }
152
153   /**
154    * Adds an editor to a new format using the UI.
155    *
156    * @param string $format_id
157    *   The format id.
158    * @param string $format_name
159    *   The format name.
160    */
161   protected function addEditorToNewFormat($format_id, $format_name) {
162     $this->enableUnicornEditor();
163     $this->drupalLogin($this->adminUser);
164     $this->drupalGet('admin/config/content/formats/add');
165     // Configure the text format name.
166     $edit = [
167       'name' => $format_name,
168       'format' => $format_id,
169     ];
170     $edit += $this->selectUnicornEditor();
171     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
172   }
173
174   /**
175    * Enables the unicorn editor.
176    */
177   protected function enableUnicornEditor() {
178     if (!$this->container->get('module_handler')->moduleExists('editor_test')) {
179       $this->container->get('module_installer')->install(['editor_test']);
180     }
181   }
182
183   /**
184    * Tests and selects the unicorn editor.
185    *
186    * @return array
187    *   Returns an edit array containing the values to be posted.
188    */
189   protected function selectUnicornEditor() {
190     // Verify the <select> when a text editor is available.
191     $select = $this->xpath('//select[@name="editor[editor]"]');
192     $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
193     $options = $this->xpath('//select[@name="editor[editor]"]/option');
194     $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
195     $this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
196     $this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
197     $this->assertTrue(($options[0]->getText()) === 'None', 'Option 1 in the Text Editor select is "None".');
198     $this->assertTrue(($options[1]->getText()) === 'Unicorn Editor', 'Option 2 in the Text Editor select is "Unicorn Editor".');
199     $this->assertTrue($options[0]->hasAttribute('selected'), 'Option 1 ("None") is selected.');
200     // Ensure the none option is selected.
201     $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.');
202
203     // Select the "Unicorn Editor" editor and click the "Configure" button.
204     $edit = [
205       'editor[editor]' => 'unicorn',
206     ];
207     $this->drupalPostForm(NULL, $edit, 'Configure');
208     $unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
209     $this->assertTrue(count($unicorn_setting), "Unicorn Editor's settings form is present.");
210
211     return $edit;
212   }
213
214   /**
215    * Verifies unicorn editor configuration.
216    *
217    * @param string $format_id
218    *   The format machine name.
219    * @param bool $ponies_too
220    *   The expected value of the ponies_too setting.
221    */
222   protected function verifyUnicornEditorConfiguration($format_id, $ponies_too = TRUE) {
223     $editor = editor_load($format_id);
224     $settings = $editor->getSettings();
225     $this->assertIdentical($editor->getEditor(), 'unicorn', 'The text editor is configured correctly.');
226     $this->assertIdentical($settings['ponies_too'], $ponies_too, 'The text editor settings are stored correctly.');
227     $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
228     $select = $this->xpath('//select[@name="editor[editor]"]');
229     $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
230     $options = $this->xpath('//select[@name="editor[editor]"]/option');
231     $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
232     $this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
233     $this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
234     $this->assertTrue($options[1]->hasAttribute('selected'), 'Option 2 ("Unicorn Editor") is selected.');
235   }
236
237 }