EditorSelectionTest.php 6.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\edit\Tests\EditorSelectionTest.
 */

namespace Drupal\edit\Tests;

use Drupal\edit\Plugin\ProcessedTextEditorManager;
use Drupal\edit\EditorSelector;

/**
 * Test in-place field editor selection.
 */
16
class EditorSelectionTest extends EditTestBase {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  /**
   * The editor selector object to be tested.
   *
   * @var \Drupal\edit\EditorSelectorInterface
   */
  protected $editorSelector;

  public static function getInfo() {
    return array(
      'name' => 'In-place field editor selection',
      'description' => 'Tests in-place field editor selection.',
      'group' => 'Edit',
    );
  }

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

    // @todo Rather than using the real ProcessedTextEditorManager, which can
    //   find all text editor plugins in the codebase, create a mock one for
    //   testing that is populated with only the ones we want to test.
    $text_editor_manager = new ProcessedTextEditorManager();

    $this->editorSelector = new EditorSelector($text_editor_manager);
  }

  /**
   * Retrieves the FieldInstance object for the given field and returns the
   * editor that Edit selects.
   */
48
  protected function getSelectedEditor($items, $field_name, $view_mode = 'default') {
49
    $options = entity_get_display('test_entity', 'test_bundle', $view_mode)->getComponent($field_name);
50
    $field_instance = field_info_instance('test_entity', $field_name, 'test_bundle');
51
    return $this->editorSelector->getEditor($options['type'], $field_instance, $items);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 133 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 161 162 163 164 165 166 167 168
  }

  /**
   * Tests a textual field, without/with text processing, with cardinality 1 and
   * >1, always without a WYSIWYG editor present.
   */
  function testText() {
    $field_name = 'field_text';
    $this->createFieldWithInstance(
      $field_name, 'text', 1, 'Simple text field',
      // Instance settings.
      array('text_processing' => 0),
      // Widget type & settings.
      'text_textfield',
      array('size' => 42),
      // 'default' formatter type & settings.
      'text_default',
      array()
    );

    // Pretend there is an entity with these items for the field.
    $items = array(array('value' => 'Hello, world!', 'format' => 'full_html'));

    // Editor selection without text processing, with cardinality 1.
    $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality 1, the 'direct' editor is selected.");

    // Editor selection with text processing, cardinality 1.
    $this->field_text_instance['settings']['text_processing'] = 1;
    field_update_instance($this->field_text_instance);
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality 1, the 'form' editor is selected.");

    // Editor selection without text processing, cardinality 1 (again).
    $this->field_text_instance['settings']['text_processing'] = 0;
    field_update_instance($this->field_text_instance);
    $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing again, cardinality 1, the 'direct' editor is selected.");

    // Editor selection without text processing, cardinality >1
    $this->field_text_field['cardinality'] = 2;
    field_update_field($this->field_text_field);
    $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected.");

    // Editor selection with text processing, cardinality >1
    $this->field_text_instance['settings']['text_processing'] = 1;
    field_update_instance($this->field_text_instance);
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality >1, the 'form' editor is selected.");
  }

  /**
   * Tests a textual field, with text processing, with cardinality 1 and >1,
   * always with a ProcessedTextEditor plug-in present, but with varying text
   * format compatibility.
   */
  function testTextWysiwyg() {
    $field_name = 'field_textarea';
    $this->createFieldWithInstance(
      $field_name, 'text', 1, 'Long text field',
      // Instance settings.
      array('text_processing' => 1),
      // Widget type & settings.
      'text_textarea',
      array('size' => 42),
      // 'default' formatter type & settings.
      'text_default',
      array()
    );

    // ProcessedTextEditor plug-in compatible with the full_html text format.
    state()->set('edit_test.compatible_format', 'full_html');

    // Pretend there is an entity with these items for the field.
    $items = array(array('value' => 'Hello, world!', 'format' => 'filtered_html'));

    // Editor selection with cardinality 1, without compatible text format.
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without cardinality 1, and the filtered_html text format, the 'form' editor is selected.");

    // Editor selection with cardinality 1, with compatible text format.
    $items[0]['format'] = 'full_html';
    $this->assertEqual('direct-with-wysiwyg', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the full_html text format, the 'direct-with-wysiwyg' editor is selected.");

    // Editor selection with text processing, cardinality >1
    $this->field_textarea_field['cardinality'] = 2;
    field_update_field($this->field_textarea_field);
    $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
  }

  /**
   * Tests a number field, with cardinality 1 and >1.
   */
  function testNumber() {
    $field_name = 'field_nr';
    $this->createFieldWithInstance(
      $field_name, 'number_integer', 1, 'Simple number field',
      // Instance settings.
      array(),
      // Widget type & settings.
      'number',
      array(),
      // 'default' formatter type & settings.
      'number_integer',
      array()
    );

    // Pretend there is an entity with these items for the field.
    $items = array(42, 43);

    // Editor selection with cardinality 1.
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, the 'form' editor is selected.");

    // Editor selection with cardinality >1.
    $this->field_nr_field['cardinality'] = 2;
    field_update_field($this->field_nr_field);
    $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, the 'form' editor is selected.");
  }

}