EditorAdminTest.php 7.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php

/**
 * @file
 * Definition of \Drupal\editor\Tests\EditorAdminTest.
 */

namespace Drupal\editor\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests administration of text editors.
 */
class EditorAdminTest extends WebTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'Text editor administration',
      'description' => 'Tests administration of text editors.',
      'group' => 'Text Editor',
    );
  }

  function setUp() {
    parent::setUp();

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

    // Create admin user.
    $this->admin_user = $this->drupalCreateUser(array('administer filters'));
  }

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

    // 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>.
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 75 76 77
  }

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

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

  /**
   * Enables the unicorn editor.
   */
  protected function enableUnicornEditor() {
107 108 109
    module_enable(array('editor_test'));
    $this->rebuildContainer();
    $this->resetAll();
110
  }
111

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

140 141
    return $edit;
  }
142

143 144 145 146 147 148 149 150 151 152
  /**
   * 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);
153
    $this->assertIdentical($editor->editor, 'unicorn', 'The text editor is configured correctly.');
154
    $this->assertIdentical($editor->settings['foo'], $foo, 'The text editor settings are stored correctly.');
155 156 157
    $this->assertIdentical($editor->settings['ponies too'], true, 'The text editor defaults are retrieved correctly.');
    $this->assertIdentical($editor->settings['rainbows'], true, 'The text editor defaults added by hook_editor_settings_defaults() are retrieved correctly.');
    $this->assertIdentical($editor->settings['sparkles'], false, 'The text editor defaults modified by hook_editor_settings_defaults_alter() are retrieved correctly.');
158
    $this->drupalGet('admin/config/content/formats/manage/'. $format_id);
159 160 161
    $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');
162 163 164 165 166
    $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.');
  }
167

168
}