EditorAdminTest.php 7.05 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
    $edit = $this->selectUnicornEditor();
    // 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 87 88 89 90 91 92

    // 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.");
93
  }
94

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

  /**
   * Enables the unicorn editor.
   */
  protected function enableUnicornEditor() {
116
    \Drupal::service('module_installer')->install(array('editor_test'));
117
  }
118

119 120 121 122 123 124 125
  /**
   * Tests and selects the unicorn editor.
   *
   * @return array
   *   Returns an edit array containing the values to be posted.
   */
  protected function selectUnicornEditor() {
126
    // Verify the <select> when a text editor is available.
127 128 129
    $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');
130 131 132
    $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.');
133 134
    $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".');
135
    $this->assertTrue(((string) $options[0]['selected']) === 'selected', 'Option 1 ("None") is selected.');
136
    // Ensure the none option is selected.
137 138 139 140
    $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(
141
      'editor[editor]' => 'unicorn',
142
    );
143
    $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
144 145
    $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.");
146

147 148
    return $edit;
  }
149

150 151 152 153 154
  /**
   * Verifies unicorn editor configuration.
   *
   * @param string $format_id
   *   The format machine name.
155 156
   * @param boolean $ponies_too
   *   The expected value of the ponies_too setting.
157
   */
158
  protected function verifyUnicornEditorConfiguration($format_id, $ponies_too = TRUE) {
159
    $editor = editor_load($format_id);
160 161
    $settings = $editor->getSettings();
    $this->assertIdentical($editor->getEditor(), 'unicorn', 'The text editor is configured correctly.');
162
    $this->assertIdentical($settings['ponies_too'], $ponies_too, 'The text editor settings are stored correctly.');
163
    $this->drupalGet('admin/config/content/formats/manage/'. $format_id);
164 165 166
    $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');
167 168 169 170 171
    $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.');
  }
172

173
}