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

/**
 * @file
5
 * Contains \Drupal\editor\Tests\EditorAdminTest.
6 7 8 9 10 11 12 13
 */

namespace Drupal\editor\Tests;

use Drupal\simpletest\WebTestBase;

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

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

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

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

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

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

49 50 51
  /**
   * Tests an existing format without any editors available.
   */
52
  public function testNoEditorAvailable() {
53
    $this->drupalLogin($this->adminUser);
54
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
55 56 57 58 59 60 61 62

    // Ensure the form field order is correct.
    $roles_pos = strpos($this->drupalGetContent(), 'Roles');
    $editor_pos = strpos($this->drupalGetContent(), 'Text editor');
    $filters_pos = strpos($this->drupalGetContent(), '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.');

    // Verify the <select>.
63 64 65
    $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');
66 67 68
    $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.');
69
    $this->assertTrue(((string) $options[0]) === 'None', 'Option 1 in the Text Editor select is "None".');
70
    $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.');
71 72 73 74 75
  }

  /**
   * Tests adding a text editor to an existing text format.
   */
76
  public function testAddEditorToExistingFormat() {
77
    $this->enableUnicornEditor();
78
    $this->drupalLogin($this->adminUser);
79
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
80 81 82
    $edit = $this->selectUnicornEditor();
    // Configure Unicorn Editor's setting to another value.
    $edit['editor[settings][foo]'] = 'baz';
83
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
84 85
    $this->verifyUnicornEditorConfiguration('filtered_html', 'baz');
  }
86

87 88 89
  /**
   * Tests adding a text editor to a new text format.
   */
90
  public function testAddEditorToNewFormat() {
91
    $this->enableUnicornEditor();
92
    $this->drupalLogin($this->adminUser);
93 94 95 96 97 98 99
    $this->drupalGet('admin/config/content/formats/add');
    // Configure the text format name.
    $edit = array(
      'name' => 'Monocerus',
      'format' => 'monocerus',
    );
    $edit += $this->selectUnicornEditor();
100
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
101 102 103 104 105 106 107
    $this->verifyUnicornEditorConfiguration($edit['format']);
  }

  /**
   * Enables the unicorn editor.
   */
  protected function enableUnicornEditor() {
108
    \Drupal::service('module_installer')->install(array('editor_test'));
109 110
    $this->rebuildContainer();
    $this->resetAll();
111
  }
112

113 114 115 116 117 118 119
  /**
   * Tests and selects the unicorn editor.
   *
   * @return array
   *   Returns an edit array containing the values to be posted.
   */
  protected function selectUnicornEditor() {
120
    // Verify the <select> when a text editor is available.
121 122 123
    $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');
124 125 126
    $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.');
127 128
    $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".');
129 130 131 132 133 134
    $this->assertTrue(((string) $options[0]['selected']) === 'selected', 'Option 1 ("None") is selected.');
    // Ensure the none option is selected
    $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(
135
      'editor[editor]' => 'unicorn',
136
    );
137
    $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
138
    $unicorn_setting_foo = $this->xpath('//input[@name="editor[settings][foo]" and @type="text" and @value="bar"]');
139 140
    $this->assertTrue(count($unicorn_setting_foo), "Unicorn Editor's settings form is present.");

141 142
    return $edit;
  }
143

144 145 146 147 148 149 150 151 152 153
  /**
   * Verifies unicorn editor configuration.
   *
   * @param string $format_id
   *   The format machine name.
   * @param string $foo
   *   The expected value of the foo setting.
   */
  protected function verifyUnicornEditorConfiguration($format_id, $foo = 'bar') {
    $editor = editor_load($format_id);
154 155 156
    $settings = $editor->getSettings();
    $this->assertIdentical($editor->getEditor(), 'unicorn', 'The text editor is configured correctly.');
    $this->assertIdentical($settings['foo'], $foo, 'The text editor settings are stored correctly.');
157
    $this->assertIdentical($settings['ponies_too'], true, 'The text editor defaults are retrieved correctly.');
158 159
    $this->assertIdentical($settings['rainbows'], true, 'The text editor defaults added by hook_editor_settings_defaults() are retrieved correctly.');
    $this->assertIdentical($settings['sparkles'], false, 'The text editor defaults modified by hook_editor_settings_defaults_alter() are retrieved correctly.');
160
    $this->drupalGet('admin/config/content/formats/manage/'. $format_id);
161 162 163
    $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');
164 165 166 167 168
    $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.');
  }
169

170
}