3 namespace Drupal\Tests\editor\Kernel;
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\MockEditEntityFieldAccessCheck;
13 use Drupal\editor\EditorController;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16 use Symfony\Component\HttpKernel\HttpKernelInterface;
17 use Drupal\filter\Entity\FilterFormat;
20 * Tests Edit module integration (Editor module's inline editing support).
24 class QuickEditIntegrationTest extends QuickEditTestBase {
29 public static $modules = ['editor', 'editor_test'];
32 * The manager for editor plug-ins.
34 * @var \Drupal\Component\Plugin\PluginManagerInterface
36 protected $editorManager;
39 * The metadata generator object to be tested.
41 * @var \Drupal\quickedit\MetadataGeneratorInterface.php
43 protected $metadataGenerator;
46 * The editor selector object to be used by the metadata generator object.
48 * @var \Drupal\quickedit\EditorSelectorInterface
50 protected $editorSelector;
53 * The access checker object to be used by the metadata generator object.
55 * @var \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface
57 protected $accessChecker;
60 * The name of the field ued for tests.
66 protected function setUp() {
69 // Install the Filter module.
72 $this->fieldName = 'field_textarea';
73 $this->createFieldWithStorage(
74 $this->fieldName, 'text', 1, 'Long text field',
77 // Widget type & settings.
80 // 'default' formatter type & settings.
85 // Create text format.
86 $full_html_format = FilterFormat::create([
87 'format' => 'full_html',
88 'name' => 'Full HTML',
92 $full_html_format->save();
94 // Associate text editor with text format.
95 $editor = Editor::create([
96 'format' => $full_html_format->id(),
97 'editor' => 'unicorn',
101 // Also create a text format without an associated text editor.
102 FilterFormat::create([
103 'format' => 'no_editor',
104 'name' => 'No Text Editor',
111 * Returns the in-place editor that quickedit selects.
113 * @param int $entity_id
115 * @param string $field_name
117 * @param string $view_mode
121 * Returns the selected in-place editor.
123 protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
124 $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
125 $storage->resetCache([$entity_id]);
126 $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);
133 * Tests editor selection when the Editor module is present.
135 * Tests a textual field, with text filtering, with cardinality 1 and >1,
136 * always with a ProcessedTextEditor plug-in present, but with varying text
137 * format compatibility.
139 public function testEditorSelection() {
140 $this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
141 $this->editorSelector = $this->container->get('quickedit.editor.selector');
143 // Create an entity with values for this text field.
144 $entity = EntityTest::create();
145 $entity->{$this->fieldName}->value = 'Hello, world!';
146 $entity->{$this->fieldName}->format = 'filtered_html';
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.");
152 // Editor selection w/ cardinality 1, text format w/ associated text editor.
153 $entity->{$this->fieldName}->format = 'full_html';
155 $this->assertEqual('editor', $this->getSelectedEditor($entity->id(), $this->fieldName), "With cardinality 1, and the full_html text format, the 'editor' editor is selected.");
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.");
164 * Tests (custom) metadata when the formatted text editor is used.
166 public function testMetadata() {
167 $this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
168 $this->accessChecker = new MockEditEntityFieldAccessCheck();
169 $this->editorSelector = $this->container->get('quickedit.editor.selector');
170 $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
172 // Create an entity with values for the field.
173 $entity = EntityTest::create();
174 $entity->{$this->fieldName}->value = 'Test';
175 $entity->{$this->fieldName}->format = 'full_html';
177 $entity = EntityTest::load($entity->id());
180 $items = $entity->get($this->fieldName);
181 $metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
184 'label' => 'Long text field',
185 'editor' => 'editor',
187 'format' => 'full_html',
188 'formatHasTransformations' => FALSE,
191 $this->assertEqual($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
195 * Tests in-place editor attachments when the Editor module is present.
197 public function testAttachments() {
198 $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.");
206 * Tests GetUntransformedTextCommand AJAX command.
208 public function testGetUntransformedTextCommand() {
209 // Create an entity with values for the field.
210 $entity = EntityTest::create();
211 $entity->{$this->fieldName}->value = 'Test';
212 $entity->{$this->fieldName}->format = 'full_html';
214 $entity = EntityTest::load($entity->id());
216 // Verify AJAX response.
217 $controller = new EditorController();
218 $request = new Request();
219 $response = $controller->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_DEFAULT, 'default');
222 'command' => 'editorGetUntransformedText',
227 $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
228 $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
229 $event = new FilterResponseEvent(
230 \Drupal::service('http_kernel'),
232 HttpKernelInterface::MASTER_REQUEST,
235 $subscriber->onResponse($event);
237 $this->assertEqual(Json::encode($expected), $response->getContent(), 'The GetUntransformedTextCommand AJAX command works correctly.');