Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / editor / tests / src / Kernel / QuickEditIntegrationTest.php
1 <?php
2
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 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;
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    * {@inheritdoc}
28    */
29   public static $modules = ['editor', 'editor_test'];
30
31   /**
32    * The manager for editor plug-ins.
33    *
34    * @var \Drupal\Component\Plugin\PluginManagerInterface
35    */
36   protected $editorManager;
37
38   /**
39    * The metadata generator object to be tested.
40    *
41    * @var \Drupal\quickedit\MetadataGeneratorInterface.php
42    */
43   protected $metadataGenerator;
44
45   /**
46    * The editor selector object to be used by the metadata generator object.
47    *
48    * @var \Drupal\quickedit\EditorSelectorInterface
49    */
50   protected $editorSelector;
51
52   /**
53    * The access checker object to be used by the metadata generator object.
54    *
55    * @var \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheckInterface
56    */
57   protected $accessChecker;
58
59   /**
60    * The name of the field ued for tests.
61    *
62    * @var string
63    */
64   protected $fieldName;
65
66   protected function setUp() {
67     parent::setUp();
68
69     // Install the Filter module.
70
71     // Create a field.
72     $this->fieldName = 'field_textarea';
73     $this->createFieldWithStorage(
74       $this->fieldName, 'text', 1, 'Long text field',
75       // Instance settings.
76       [],
77       // Widget type & settings.
78       'text_textarea',
79       ['size' => 42],
80       // 'default' formatter type & settings.
81       'text_default',
82       []
83     );
84
85     // Create text format.
86     $full_html_format = FilterFormat::create([
87       'format' => 'full_html',
88       'name' => 'Full HTML',
89       'weight' => 1,
90       'filters' => [],
91     ]);
92     $full_html_format->save();
93
94     // 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       'format' => 'no_editor',
104       'name' => 'No Text Editor',
105       'weight' => 2,
106       'filters' => [],
107     ])->save();
108   }
109
110   /**
111    * Returns the in-place editor that quickedit selects.
112    *
113    * @param int $entity_id
114    *   An entity ID.
115    * @param string $field_name
116    *   A field name.
117    * @param string $view_mode
118    *   A view mode.
119    *
120    * @return string
121    *   Returns the selected in-place editor.
122    */
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);
130   }
131
132   /**
133    * Tests editor selection when the Editor module is present.
134    *
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.
138    */
139   public function testEditorSelection() {
140     $this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
141     $this->editorSelector = $this->container->get('quickedit.editor.selector');
142
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';
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     $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
171
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';
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       'access' => TRUE,
184       'label' => 'Long text field',
185       'editor' => 'editor',
186       'custom' => [
187         'format' => 'full_html',
188         'formatHasTransformations' => FALSE,
189       ],
190     ];
191     $this->assertEqual($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
192   }
193
194   /**
195    * Tests in-place editor attachments when the Editor module is present.
196    */
197   public function testAttachments() {
198     $this->editorSelector = $this->container->get('quickedit.editor.selector');
199
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    * Tests GetUntransformedTextCommand AJAX command.
207    */
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';
213     $entity->save();
214     $entity = EntityTest::load($entity->id());
215
216     // Verify AJAX response.
217     $controller = new EditorController();
218     $request = new Request();
219     $response = $controller->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_DEFAULT, 'default');
220     $expected = [
221       [
222         'command' => 'editorGetUntransformedText',
223         'data' => 'Test',
224       ]
225     ];
226
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'),
231       $request,
232       HttpKernelInterface::MASTER_REQUEST,
233       $response
234     );
235     $subscriber->onResponse($event);
236
237     $this->assertEqual(Json::encode($expected), $response->getContent(), 'The GetUntransformedTextCommand AJAX command works correctly.');
238   }
239
240 }