QuickEditIntegrationTest.php 7.97 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\editor\Kernel;
4

5
use Drupal\Component\Serialization\Json;
6
use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
7
use Drupal\Core\Language\LanguageInterface;
8
use Drupal\editor\Entity\Editor;
9
use Drupal\entity_test\Entity\EntityTest;
10
use Drupal\quickedit\MetadataGenerator;
11
use Drupal\Tests\quickedit\Kernel\QuickEditTestBase;
12
use Drupal\quickedit_test\MockQuickEditEntityFieldAccessCheck;
13 14
use Drupal\editor\EditorController;
use Symfony\Component\HttpFoundation\Request;
15 16
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
17
use Drupal\filter\Entity\FilterFormat;
18 19 20

/**
 * Tests Edit module integration (Editor module's inline editing support).
21 22
 *
 * @group editor
23
 */
24
class QuickEditIntegrationTest extends QuickEditTestBase {
25

26 27 28
  /**
   * {@inheritdoc}
   */
29
  public static $modules = ['editor', 'editor_test'];
30

31
  /**
32
   * The manager for editor plug-ins.
33 34 35 36 37 38 39 40
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $editorManager;

  /**
   * The metadata generator object to be tested.
   *
41
   * @var \Drupal\quickedit\MetadataGeneratorInterface
42 43 44 45 46 47
   */
  protected $metadataGenerator;

  /**
   * The editor selector object to be used by the metadata generator object.
   *
48
   * @var \Drupal\quickedit\EditorSelectorInterface
49 50 51 52 53 54
   */
  protected $editorSelector;

  /**
   * The access checker object to be used by the metadata generator object.
   *
55
   * @var \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheckInterface
56 57 58 59 60 61 62 63
   */
  protected $accessChecker;

  /**
   * The name of the field ued for tests.
   *
   * @var string
   */
64
  protected $fieldName;
65

66
  protected function setUp() {
67 68 69 70 71
    parent::setUp();

    // Install the Filter module.

    // Create a field.
72
    $this->fieldName = 'field_textarea';
73
    $this->createFieldWithStorage(
74
      $this->fieldName, 'text', 1, 'Long text field',
75
      // Instance settings.
76
      [],
77 78
      // Widget type & settings.
      'text_textarea',
79
      ['size' => 42],
80 81
      // 'default' formatter type & settings.
      'text_default',
82
      []
83 84 85
    );

    // Create text format.
86
    $full_html_format = FilterFormat::create([
87 88 89
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
90 91
      'filters' => [],
    ]);
92 93 94
    $full_html_format->save();

    // Associate text editor with text format.
95
    $editor = Editor::create([
96
      'format' => $full_html_format->id(),
97
      'editor' => 'unicorn',
98
    ]);
99
    $editor->save();
100 101

    // Also create a text format without an associated text editor.
102
    FilterFormat::create([
103 104 105
      'format' => 'no_editor',
      'name' => 'No Text Editor',
      'weight' => 2,
106 107
      'filters' => [],
    ])->save();
108 109 110
  }

  /**
111 112 113 114 115 116 117 118 119 120 121
   * Returns the in-place editor that quickedit selects.
   *
   * @param int $entity_id
   *   An entity ID.
   * @param string $field_name
   *   A field name.
   * @param string $view_mode
   *   A view mode.
   *
   * @return string
   *   Returns the selected in-place editor.
122
   */
123
  protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
124 125 126
    $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
    $storage->resetCache([$entity_id]);
    $entity = $storage->load($entity_id);
127
    $items = $entity->get($field_name);
128
    $options = entity_get_display('entity_test', 'entity_test', $view_mode)->getComponent($field_name);
129
    return $this->editorSelector->getEditor($options['type'], $items);
130 131 132 133 134
  }

  /**
   * Tests editor selection when the Editor module is present.
   *
135
   * Tests a textual field, with text filtering, with cardinality 1 and >1,
136 137 138
   * always with a ProcessedTextEditor plug-in present, but with varying text
   * format compatibility.
   */
139
  public function testEditorSelection() {
140 141
    $this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
    $this->editorSelector = $this->container->get('quickedit.editor.selector');
142

143
    // Create an entity with values for this text field.
144
    $entity = EntityTest::create();
145 146
    $entity->{$this->fieldName}->value = 'Hello, world!';
    $entity->{$this->fieldName}->format = 'filtered_html';
147
    $entity->save();
148 149

    // Editor selection w/ cardinality 1, text format w/o associated text editor.
150
    $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $this->fieldName), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected.");
151 152

    // Editor selection w/ cardinality 1, text format w/ associated text editor.
153
    $entity->{$this->fieldName}->format = 'full_html';
154
    $entity->save();
155
    $this->assertEqual('editor', $this->getSelectedEditor($entity->id(), $this->fieldName), "With cardinality 1, and the full_html text format, the 'editor' editor is selected.");
156 157

    // Editor selection with text processing, cardinality >1
158
    $this->fields->field_textarea_field_storage->setCardinality(2);
159
    $this->fields->field_textarea_field_storage->save();
160
    $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $this->fieldName), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
161 162 163
  }

  /**
164
   * Tests (custom) metadata when the formatted text editor is used.
165
   */
166
  public function testMetadata() {
167
    $this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
168
    $this->accessChecker = new MockQuickEditEntityFieldAccessCheck();
169
    $this->editorSelector = $this->container->get('quickedit.editor.selector');
170 171 172
    $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);

    // Create an entity with values for the field.
173
    $entity = EntityTest::create();
174 175
    $entity->{$this->fieldName}->value = 'Test';
    $entity->{$this->fieldName}->format = 'full_html';
176
    $entity->save();
177
    $entity = EntityTest::load($entity->id());
178 179

    // Verify metadata.
180
    $items = $entity->get($this->fieldName);
181
    $metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
182
    $expected = [
183 184 185
      'access' => TRUE,
      'label' => 'Long text field',
      'editor' => 'editor',
186
      'custom' => [
187 188
        'format' => 'full_html',
        'formatHasTransformations' => FALSE,
189 190
      ],
    ];
191 192 193
    $this->assertEqual($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
  }

194 195 196 197 198 199
  /**
   * Tests in-place editor attachments when the Editor module is present.
   */
  public function testAttachments() {
    $this->editorSelector = $this->container->get('quickedit.editor.selector');

200
    $editors = ['editor'];
201
    $attachments = $this->editorSelector->getEditorAttachments($editors);
202
    $this->assertIdentical($attachments, ['library' => ['editor/quickedit.inPlaceEditor.formattedText']], "Expected attachments for Editor module's in-place editor found.");
203 204
  }

205 206 207
  /**
   * Tests GetUntransformedTextCommand AJAX command.
   */
208
  public function testGetUntransformedTextCommand() {
209
    // Create an entity with values for the field.
210
    $entity = EntityTest::create();
211 212
    $entity->{$this->fieldName}->value = 'Test';
    $entity->{$this->fieldName}->format = 'full_html';
213
    $entity->save();
214
    $entity = EntityTest::load($entity->id());
215 216 217 218

    // Verify AJAX response.
    $controller = new EditorController();
    $request = new Request();
219
    $response = $controller->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_DEFAULT, 'default');
220 221
    $expected = [
      [
222 223
        'command' => 'editorGetUntransformedText',
        'data' => 'Test',
224
      ],
225
    ];
226 227 228 229 230 231 232 233 234 235 236 237

    $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
    $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
    $event = new FilterResponseEvent(
      \Drupal::service('http_kernel'),
      $request,
      HttpKernelInterface::MASTER_REQUEST,
      $response
    );
    $subscriber->onResponse($event);

    $this->assertEqual(Json::encode($expected), $response->getContent(), 'The GetUntransformedTextCommand AJAX command works correctly.');
238
  }
239

240
}