EditorAdminTest.php 9.26 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\editor\Tests;

5 6 7 8
use Drupal\Component\Utility\Unicode;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
9 10 11 12
use Drupal\simpletest\WebTestBase;

/**
 * Tests administration of text editors.
13 14
 *
 * @group editor
15 16 17 18 19 20 21 22 23 24
 */
class EditorAdminTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('filter', 'editor');

25 26 27 28 29 30 31
  /**
   * A user with the 'administer filters' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

32
  protected function setUp() {
33 34
    parent::setUp();

35
    // Add text format.
36
    $filtered_html_format = FilterFormat::create(array(
37 38 39 40
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(),
41 42
    ));
    $filtered_html_format->save();
43 44

    // Create admin user.
45
    $this->adminUser = $this->drupalCreateUser(array('administer filters'));
46 47
  }

48 49 50
  /**
   * 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');
54 55

    // Ensure the form field order is correct.
56 57 58
    $roles_pos = strpos($this->getRawContent(), 'Roles');
    $editor_pos = strpos($this->getRawContent(), 'Text editor');
    $filters_pos = strpos($this->getRawContent(), 'Enabled filters');
59 60 61
    $this->assertTrue($roles_pos < $editor_pos && $editor_pos < $filters_pos, '"Text Editor" select appears in the correct location of the text format configuration UI.');

    // Verify the <select>.
62 63 64
    $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');
65 66 67
    $this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
    $this->assertTrue(count($select_is_disabled) === 1, 'The Text Editor select is disabled.');
    $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 74
  }

  /**
   * 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 80
    $edit = $this->selectUnicornEditor();
    // 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 86 87 88 89 90 91

    // Switch back to 'None' and check the Unicorn Editor's settings are gone.
    $edit = array(
      'editor[editor]' => '',
    );
    $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
    $unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
    $this->assertTrue(count($unicorn_setting) === 0, "Unicorn Editor's settings form is no longer present.");
92
  }
93

94 95 96
  /**
   * Tests adding a text editor to a new text format.
   */
97
  public function testAddEditorToNewFormat() {
98 99 100 101 102
    $this->addEditorToNewFormat('monocerus', 'Monocerus');
    $this->verifyUnicornEditorConfiguration('monocerus');
  }

  /**
103
   * Tests format disabling.
104 105
   */
  public function testDisableFormatWithEditor() {
106
    $formats = ['monocerus' => 'Monocerus', 'tattoo' => 'Tattoo'];
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    // Install the node module.
    $this->container->get('module_installer')->install(['node']);
    $this->resetAll();
    // Create a new node type and attach the 'body' field to it.
    $node_type = NodeType::create(['type' => Unicode::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) {
      // Create a format and add an editor to this format.
      $this->addEditorToNewFormat($format, $name);
      // Add permission for this format.
      $permissions[] = "use text format $format";
    }

    // Create a node having the body format value 'moncerus'.
    $node = Node::create([
      'type' => $node_type->id(),
      'title' => $this->randomString(),
    ]);
    $node->body->value = $this->randomString(100);
    $node->body->format = 'monocerus';
    $node->save();

133
    // Log in as an user able to use both formats and edit nodes of created type.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    $account = $this->drupalCreateUser($permissions);
    $this->drupalLogin($account);

    // The node edit page header.
    $text = t('<em>Edit @type</em> @title', array('@type' => $node_type->label(), '@title' => $node->label()));

    // Go to node edit form.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertRaw($text);

    // Disable the format assigned to the 'body' field of the node.
    FilterFormat::load('monocerus')->disable()->save();

    // Edit again the node.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertRaw($text);
  }

  /**
   * Adds an editor to a new format using the UI.
   *
   * @param string $format_id
   *   The format id.
   * @param string $format_name
   *   The format name.
   */
  protected function addEditorToNewFormat($format_id, $format_name) {
161
    $this->enableUnicornEditor();
162
    $this->drupalLogin($this->adminUser);
163 164 165
    $this->drupalGet('admin/config/content/formats/add');
    // Configure the text format name.
    $edit = array(
166 167
      'name' => $format_name,
      'format' => $format_id,
168 169
    );
    $edit += $this->selectUnicornEditor();
170
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
171 172 173 174 175 176
  }

  /**
   * Enables the unicorn editor.
   */
  protected function enableUnicornEditor() {
177 178 179
    if (!$this->container->get('module_handler')->moduleExists('editor_test')) {
      $this->container->get('module_installer')->install(array('editor_test'));
    }
180
  }
181

182 183 184 185 186 187 188
  /**
   * Tests and selects the unicorn editor.
   *
   * @return array
   *   Returns an edit array containing the values to be posted.
   */
  protected function selectUnicornEditor() {
189
    // Verify the <select> when a text editor is available.
190 191 192
    $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');
193 194 195
    $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.');
196 197
    $this->assertTrue(((string) $options[0]) === 'None', 'Option 1 in the Text Editor select is "None".');
    $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 201 202 203
    $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.');

    // Select the "Unicorn Editor" editor and click the "Configure" button.
    $edit = array(
204
      'editor[editor]' => 'unicorn',
205
    );
206
    $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
207 208
    $unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
    $this->assertTrue(count($unicorn_setting), "Unicorn Editor's settings form is present.");
209

210 211
    return $edit;
  }
212

213 214 215 216 217
  /**
   * Verifies unicorn editor configuration.
   *
   * @param string $format_id
   *   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 224
    $settings = $editor->getSettings();
    $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 228 229
    $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');
230 231 232 233 234
    $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[1]['selected']) === 'selected', 'Option 2 ("Unicorn Editor") is selected.');
  }
235

236
}