annotate core/modules/quickedit/src/Tests/QuickEditLoadingTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\quickedit\Tests;
Chris@0 4
Chris@0 5 use Drupal\Component\Serialization\Json;
Chris@0 6 use Drupal\block_content\Entity\BlockContent;
Chris@0 7 use Drupal\field\Entity\FieldConfig;
Chris@0 8 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 9 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
Chris@0 10 use Drupal\Core\Url;
Chris@0 11 use Drupal\node\Entity\Node;
Chris@0 12 use Drupal\node\Entity\NodeType;
Chris@0 13 use Drupal\simpletest\WebTestBase;
Chris@0 14 use Drupal\filter\Entity\FilterFormat;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Tests loading of in-place editing functionality and lazy loading of its
Chris@0 18 * in-place editors.
Chris@0 19 *
Chris@0 20 * @group quickedit
Chris@0 21 */
Chris@0 22 class QuickEditLoadingTest extends WebTestBase {
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Modules to enable.
Chris@0 26 *
Chris@0 27 * @var array
Chris@0 28 */
Chris@0 29 public static $modules = [
Chris@0 30 'contextual',
Chris@0 31 'quickedit',
Chris@0 32 'filter',
Chris@0 33 'node',
Chris@0 34 'image',
Chris@0 35 ];
Chris@0 36
Chris@0 37 /**
Chris@0 38 * An user with permissions to create and edit articles.
Chris@0 39 *
Chris@0 40 * @var \Drupal\user\UserInterface
Chris@0 41 */
Chris@0 42 protected $authorUser;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * A test node.
Chris@0 46 *
Chris@0 47 * @var \Drupal\node\NodeInterface
Chris@0 48 */
Chris@0 49 protected $testNode;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * A author user with permissions to access in-place editor.
Chris@0 53 *
Chris@0 54 * @var \Drupal\user\UserInterface
Chris@0 55 */
Chris@0 56 protected $editorUser;
Chris@0 57
Chris@0 58 protected function setUp() {
Chris@0 59 parent::setUp();
Chris@0 60
Chris@0 61 // Create a text format.
Chris@0 62 $filtered_html_format = FilterFormat::create([
Chris@0 63 'format' => 'filtered_html',
Chris@0 64 'name' => 'Filtered HTML',
Chris@0 65 'weight' => 0,
Chris@0 66 'filters' => [],
Chris@0 67 ]);
Chris@0 68 $filtered_html_format->save();
Chris@0 69
Chris@0 70 // Create a node type.
Chris@0 71 $this->drupalCreateContentType([
Chris@0 72 'type' => 'article',
Chris@0 73 'name' => 'Article',
Chris@0 74 ]);
Chris@0 75
Chris@0 76 // Set the node type to initially not have revisions.
Chris@0 77 // Testing with revisions will be done later.
Chris@0 78 $node_type = NodeType::load('article');
Chris@0 79 $node_type->setNewRevision(FALSE);
Chris@0 80 $node_type->save();
Chris@0 81
Chris@0 82 // Create one node of the above node type using the above text format.
Chris@0 83 $this->testNode = $this->drupalCreateNode([
Chris@0 84 'type' => 'article',
Chris@0 85 'body' => [
Chris@0 86 0 => [
Chris@0 87 'value' => '<p>How are you?</p>',
Chris@0 88 'format' => 'filtered_html',
Chris@4 89 ],
Chris@0 90 ],
Chris@0 91 'revision_log' => $this->randomString(),
Chris@0 92 ]);
Chris@0 93
Chris@0 94 // Create 2 users, the only difference being the ability to use in-place
Chris@0 95 // editing
Chris@0 96 $basic_permissions = ['access content', 'create article content', 'edit any article content', 'use text format filtered_html', 'access contextual links'];
Chris@0 97 $this->authorUser = $this->drupalCreateUser($basic_permissions);
Chris@0 98 $this->editorUser = $this->drupalCreateUser(array_merge($basic_permissions, ['access in-place editing']));
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * Test the loading of Quick Edit when a user doesn't have access to it.
Chris@0 103 */
Chris@0 104 public function testUserWithoutPermission() {
Chris@0 105 $this->drupalLogin($this->authorUser);
Chris@0 106 $this->drupalGet('node/1');
Chris@0 107
Chris@0 108 // Library and in-place editors.
Chris@0 109 $this->assertNoRaw('core/modules/quickedit/js/quickedit.js', 'Quick Edit library not loaded.');
Chris@0 110 $this->assertNoRaw('core/modules/quickedit/js/editors/formEditor.js', "'form' in-place editor not loaded.");
Chris@0 111
Chris@0 112 // HTML annotation and title class does not exist for users without
Chris@0 113 // permission to in-place edit.
Chris@0 114 $this->assertNoRaw('data-quickedit-entity-id="node/1"');
Chris@0 115 $this->assertNoRaw('data-quickedit-field-id="node/1/body/en/full"');
Chris@0 116 $this->assertNoFieldByXPath('//h1[contains(@class, "js-quickedit-page-title")]');
Chris@0 117
Chris@0 118 // Retrieving the metadata should result in an empty 403 response.
Chris@0 119 $post = ['fields[0]' => 'node/1/body/en/full'];
Chris@0 120 $response = $this->drupalPostWithFormat(Url::fromRoute('quickedit.metadata'), 'json', $post);
Chris@0 121 $this->assertIdentical(Json::encode(['message' => "The 'access in-place editing' permission is required."]), $response);
Chris@0 122 $this->assertResponse(403);
Chris@0 123
Chris@0 124 // Quick Edit's JavaScript would never hit these endpoints if the metadata
Chris@0 125 // was empty as above, but we need to make sure that malicious users aren't
Chris@0 126 // able to use any of the other endpoints either.
Chris@0 127 $post = ['editors[0]' => 'form'] + $this->getAjaxPageStatePostData();
Chris@0 128 $response = $this->drupalPost('quickedit/attachments', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 129 $message = Json::encode(['message' => "The 'access in-place editing' permission is required."]);
Chris@0 130 $this->assertIdentical($message, $response);
Chris@0 131 $this->assertResponse(403);
Chris@0 132 $post = ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData();
Chris@0 133 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 134 $this->assertIdentical($message, $response);
Chris@0 135 $this->assertResponse(403);
Chris@0 136 $edit = [];
Chris@0 137 $edit['form_id'] = 'quickedit_field_form';
Chris@0 138 $edit['form_token'] = 'xIOzMjuc-PULKsRn_KxFn7xzNk5Bx7XKXLfQfw1qOnA';
Chris@0 139 $edit['form_build_id'] = 'form-kVmovBpyX-SJfTT5kY0pjTV35TV-znor--a64dEnMR8';
Chris@0 140 $edit['body[0][summary]'] = '';
Chris@0 141 $edit['body[0][value]'] = '<p>Malicious content.</p>';
Chris@0 142 $edit['body[0][format]'] = 'filtered_html';
Chris@0 143 $edit['op'] = t('Save');
Chris@0 144 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $edit, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 145 $this->assertIdentical($message, $response);
Chris@0 146 $this->assertResponse(403);
Chris@0 147 $post = ['nocssjs' => 'true'];
Chris@0 148 $response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
Chris@0 149 $this->assertIdentical(Json::encode(['message' => "The 'access in-place editing' permission is required."]), $response);
Chris@0 150 $this->assertResponse(403);
Chris@0 151 }
Chris@0 152
Chris@0 153 /**
Chris@0 154 * Tests the loading of Quick Edit when a user does have access to it.
Chris@0 155 *
Chris@0 156 * Also ensures lazy loading of in-place editors works.
Chris@0 157 */
Chris@0 158 public function testUserWithPermission() {
Chris@0 159 $this->drupalLogin($this->editorUser);
Chris@0 160 $this->drupalGet('node/1');
Chris@0 161
Chris@0 162 // Library and in-place editors.
Chris@0 163 $settings = $this->getDrupalSettings();
Chris@0 164 $libraries = explode(',', $settings['ajaxPageState']['libraries']);
Chris@0 165 $this->assertTrue(in_array('quickedit/quickedit', $libraries), 'Quick Edit library loaded.');
Chris@0 166 $this->assertFalse(in_array('quickedit/quickedit.inPlaceEditor.form', $libraries), "'form' in-place editor not loaded.");
Chris@0 167
Chris@0 168 // HTML annotation and title class must always exist (to not break the
Chris@0 169 // render cache).
Chris@0 170 $this->assertRaw('data-quickedit-entity-id="node/1"');
Chris@0 171 $this->assertRaw('data-quickedit-field-id="node/1/body/en/full"');
Chris@0 172 $this->assertFieldByXPath('//h1[contains(@class, "js-quickedit-page-title")]');
Chris@0 173
Chris@0 174 // There should be only one revision so far.
Chris@0 175 $node = Node::load(1);
Chris@0 176 $vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
Chris@0 177 $this->assertIdentical(1, count($vids), 'The node has only one revision.');
Chris@0 178 $original_log = $node->revision_log->value;
Chris@0 179
Chris@0 180 // Retrieving the metadata should result in a 200 JSON response.
Chris@0 181 $htmlPageDrupalSettings = $this->drupalSettings;
Chris@0 182 $post = ['fields[0]' => 'node/1/body/en/full'];
Chris@0 183 $response = $this->drupalPostWithFormat('quickedit/metadata', 'json', $post);
Chris@0 184 $this->assertResponse(200);
Chris@0 185 $expected = [
Chris@0 186 'node/1/body/en/full' => [
Chris@0 187 'label' => 'Body',
Chris@0 188 'access' => TRUE,
Chris@0 189 'editor' => 'form',
Chris@4 190 ],
Chris@0 191 ];
Chris@0 192 $this->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
Chris@0 193 // Restore drupalSettings to build the next requests; simpletest wipes them
Chris@0 194 // after a JSON response.
Chris@0 195 $this->drupalSettings = $htmlPageDrupalSettings;
Chris@0 196
Chris@0 197 // Retrieving the attachments should result in a 200 response, containing:
Chris@0 198 // 1. a settings command with useless metadata: AjaxController is dumb
Chris@0 199 // 2. an insert command that loads the required in-place editors
Chris@0 200 $post = ['editors[0]' => 'form'] + $this->getAjaxPageStatePostData();
Chris@0 201 $response = $this->drupalPost('quickedit/attachments', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 202 $ajax_commands = Json::decode($response);
Chris@0 203 $this->assertIdentical(2, count($ajax_commands), 'The attachments HTTP request results in two AJAX commands.');
Chris@0 204 // First command: settings.
Chris@0 205 $this->assertIdentical('settings', $ajax_commands[0]['command'], 'The first AJAX command is a settings command.');
Chris@0 206 // Second command: insert libraries into DOM.
Chris@0 207 $this->assertIdentical('insert', $ajax_commands[1]['command'], 'The second AJAX command is an append command.');
Chris@0 208 $this->assertTrue(in_array('quickedit/quickedit.inPlaceEditor.form', explode(',', $ajax_commands[0]['settings']['ajaxPageState']['libraries'])), 'The quickedit.inPlaceEditor.form library is loaded.');
Chris@0 209
Chris@0 210 // Retrieving the form for this field should result in a 200 response,
Chris@0 211 // containing only a quickeditFieldForm command.
Chris@0 212 $post = ['nocssjs' => 'true', 'reset' => 'true'] + $this->getAjaxPageStatePostData();
Chris@0 213 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 214 $this->assertResponse(200);
Chris@0 215 $ajax_commands = Json::decode($response);
Chris@0 216 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 217 $this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
Chris@4 218 $this->assertIdentical('<form ', mb_substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
Chris@0 219
Chris@0 220 // Prepare form values for submission. drupalPostAjaxForm() is not suitable
Chris@0 221 // for handling pages with JSON responses, so we need our own solution here.
Chris@0 222 $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
Chris@0 223 $this->assertTrue($form_tokens_found, 'Form tokens found in output.');
Chris@0 224
Chris@0 225 if ($form_tokens_found) {
Chris@0 226 $edit = [
Chris@0 227 'body[0][summary]' => '',
Chris@0 228 'body[0][value]' => '<p>Fine thanks.</p>',
Chris@0 229 'body[0][format]' => 'filtered_html',
Chris@0 230 'op' => t('Save'),
Chris@0 231 ];
Chris@0 232 $post = [
Chris@0 233 'form_id' => 'quickedit_field_form',
Chris@0 234 'form_token' => $token_match[1],
Chris@0 235 'form_build_id' => $build_id_match[1],
Chris@0 236 ];
Chris@0 237 $post += $edit + $this->getAjaxPageStatePostData();
Chris@0 238
Chris@0 239 // Submit field form and check response. This should store the updated
Chris@0 240 // entity in PrivateTempStore on the server.
Chris@0 241 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 242 $this->assertResponse(200);
Chris@0 243 $ajax_commands = Json::decode($response);
Chris@0 244 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 245 $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
Chris@0 246 $this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
Chris@0 247 $this->assertIdentical($ajax_commands[0]['other_view_modes'], [], 'Field was not rendered in any other view mode.');
Chris@0 248
Chris@0 249 // Ensure the text on the original node did not change yet.
Chris@0 250 $this->drupalGet('node/1');
Chris@0 251 $this->assertText('How are you?');
Chris@0 252
Chris@0 253 // Save the entity by moving the PrivateTempStore values to entity storage.
Chris@0 254 $post = ['nocssjs' => 'true'];
Chris@0 255 $response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
Chris@0 256 $this->assertResponse(200);
Chris@0 257 $ajax_commands = Json::decode($response);
Chris@0 258 $this->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
Chris@0 259 $this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditEntitySaved command.');
Chris@0 260 $this->assertIdentical($ajax_commands[0]['data']['entity_type'], 'node', 'Saved entity is of type node.');
Chris@0 261 $this->assertIdentical($ajax_commands[0]['data']['entity_id'], '1', 'Entity id is 1.');
Chris@0 262
Chris@0 263 // Ensure the text on the original node did change.
Chris@0 264 $this->drupalGet('node/1');
Chris@0 265 $this->assertText('Fine thanks.');
Chris@0 266
Chris@0 267 // Ensure no new revision was created and the log message is unchanged.
Chris@0 268 $node = Node::load(1);
Chris@0 269 $vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
Chris@0 270 $this->assertIdentical(1, count($vids), 'The node has only one revision.');
Chris@0 271 $this->assertIdentical($original_log, $node->revision_log->value, 'The revision log message is unchanged.');
Chris@0 272
Chris@0 273 // Now configure this node type to create new revisions automatically,
Chris@0 274 // then again retrieve the field form, fill it, submit it (so it ends up
Chris@0 275 // in PrivateTempStore) and then save the entity. Now there should be two
Chris@0 276 // revisions.
Chris@0 277 $node_type = NodeType::load('article');
Chris@0 278 $node_type->setNewRevision(TRUE);
Chris@0 279 $node_type->save();
Chris@0 280
Chris@0 281 // Retrieve field form.
Chris@0 282 $post = ['nocssjs' => 'true', 'reset' => 'true'];
Chris@0 283 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 284 $this->assertResponse(200);
Chris@0 285 $ajax_commands = Json::decode($response);
Chris@0 286 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 287 $this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
Chris@4 288 $this->assertIdentical('<form ', mb_substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
Chris@0 289
Chris@0 290 // Submit field form.
Chris@0 291 preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match);
Chris@0 292 preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
Chris@0 293 $edit['body[0][value]'] = '<p>kthxbye</p>';
Chris@0 294 $post = [
Chris@0 295 'form_id' => 'quickedit_field_form',
Chris@0 296 'form_token' => $token_match[1],
Chris@0 297 'form_build_id' => $build_id_match[1],
Chris@0 298 ];
Chris@0 299 $post += $edit + $this->getAjaxPageStatePostData();
Chris@0 300 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 301 $this->assertResponse(200);
Chris@0 302 $ajax_commands = Json::decode($response);
Chris@0 303 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 304 $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is an quickeditFieldFormSaved command.');
Chris@0 305 $this->assertTrue(strpos($ajax_commands[0]['data'], 'kthxbye'), 'Form value saved and printed back.');
Chris@0 306
Chris@0 307 // Save the entity.
Chris@0 308 $post = ['nocssjs' => 'true'];
Chris@0 309 $response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
Chris@0 310 $this->assertResponse(200);
Chris@0 311 $ajax_commands = Json::decode($response);
Chris@0 312 $this->assertIdentical(1, count($ajax_commands));
Chris@0 313 $this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is an quickeditEntitySaved command.');
Chris@0 314 $this->assertEqual($ajax_commands[0]['data'], ['entity_type' => 'node', 'entity_id' => 1], 'Updated entity type and ID returned');
Chris@0 315
Chris@0 316 // Test that a revision was created with the correct log message.
Chris@0 317 $vids = \Drupal::entityManager()->getStorage('node')->revisionIds(Node::load(1));
Chris@0 318 $this->assertIdentical(2, count($vids), 'The node has two revisions.');
Chris@0 319 $revision = node_revision_load($vids[0]);
Chris@0 320 $this->assertIdentical($original_log, $revision->revision_log->value, 'The first revision log message is unchanged.');
Chris@0 321 $revision = node_revision_load($vids[1]);
Chris@0 322 $this->assertIdentical('Updated the <em class="placeholder">Body</em> field through in-place editing.', $revision->revision_log->value, 'The second revision log message was correctly generated by Quick Edit module.');
Chris@0 323 }
Chris@0 324 }
Chris@0 325
Chris@0 326 /**
Chris@0 327 * Test quickedit does not appear for entities with pending revisions.
Chris@0 328 */
Chris@0 329 public function testWithPendingRevision() {
Chris@0 330 $this->drupalLogin($this->editorUser);
Chris@0 331
Chris@0 332 // Verify that the preview is loaded correctly.
Chris@0 333 $this->drupalPostForm('node/add/article', ['title[0][value]' => 'foo'], 'Preview');
Chris@0 334 $this->assertResponse(200);
Chris@0 335 // Verify that quickedit is not active on preview.
Chris@0 336 $this->assertNoRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
Chris@0 337 $this->assertNoRaw('data-quickedit-field-id="node/' . $this->testNode->id() . '/title/' . $this->testNode->language()->getId() . '/full"');
Chris@0 338
Chris@0 339 $this->drupalGet('node/' . $this->testNode->id());
Chris@0 340 $this->assertRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
Chris@0 341 $this->assertRaw('data-quickedit-field-id="node/' . $this->testNode->id() . '/title/' . $this->testNode->language()->getId() . '/full"');
Chris@0 342
Chris@0 343 $this->testNode->title = 'Updated node';
Chris@0 344 $this->testNode->setNewRevision(TRUE);
Chris@0 345 $this->testNode->isDefaultRevision(FALSE);
Chris@0 346 $this->testNode->save();
Chris@0 347
Chris@0 348 $this->drupalGet('node/' . $this->testNode->id());
Chris@0 349 $this->assertResponse(200);
Chris@0 350 $this->assertNoRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
Chris@0 351 $this->assertNoRaw('data-quickedit-field-id="node/' . $this->testNode->id() . '/title/' . $this->testNode->language()->getId() . '/full"');
Chris@0 352 }
Chris@0 353
Chris@0 354 /**
Chris@0 355 * Tests the loading of Quick Edit for the title base field.
Chris@0 356 */
Chris@0 357 public function testTitleBaseField() {
Chris@0 358 $this->drupalLogin($this->editorUser);
Chris@0 359 $this->drupalGet('node/1');
Chris@0 360
Chris@0 361 // Ensure that the full page title is actually in-place editable
Chris@0 362 $node = Node::load(1);
Chris@0 363 $elements = $this->xpath('//h1/span[@data-quickedit-field-id="node/1/title/en/full" and normalize-space(text())=:title]', [':title' => $node->label()]);
Chris@0 364 $this->assertTrue(!empty($elements), 'Title with data-quickedit-field-id attribute found.');
Chris@0 365
Chris@0 366 // Retrieving the metadata should result in a 200 JSON response.
Chris@0 367 $htmlPageDrupalSettings = $this->drupalSettings;
Chris@0 368 $post = ['fields[0]' => 'node/1/title/en/full'];
Chris@0 369 $response = $this->drupalPostWithFormat('quickedit/metadata', 'json', $post);
Chris@0 370 $this->assertResponse(200);
Chris@0 371 $expected = [
Chris@0 372 'node/1/title/en/full' => [
Chris@0 373 'label' => 'Title',
Chris@0 374 'access' => TRUE,
Chris@0 375 'editor' => 'plain_text',
Chris@4 376 ],
Chris@0 377 ];
Chris@0 378 $this->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
Chris@0 379 // Restore drupalSettings to build the next requests; simpletest wipes them
Chris@0 380 // after a JSON response.
Chris@0 381 $this->drupalSettings = $htmlPageDrupalSettings;
Chris@0 382
Chris@0 383 // Retrieving the form for this field should result in a 200 response,
Chris@0 384 // containing only a quickeditFieldForm command.
Chris@0 385 $post = ['nocssjs' => 'true', 'reset' => 'true'] + $this->getAjaxPageStatePostData();
Chris@0 386 $response = $this->drupalPost('quickedit/form/' . 'node/1/title/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 387 $this->assertResponse(200);
Chris@0 388 $ajax_commands = Json::decode($response);
Chris@0 389 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 390 $this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
Chris@4 391 $this->assertIdentical('<form ', mb_substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
Chris@0 392
Chris@0 393 // Prepare form values for submission. drupalPostAjaxForm() is not suitable
Chris@0 394 // for handling pages with JSON responses, so we need our own solution
Chris@0 395 // here.
Chris@0 396 $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
Chris@0 397 $this->assertTrue($form_tokens_found, 'Form tokens found in output.');
Chris@0 398
Chris@0 399 if ($form_tokens_found) {
Chris@0 400 $edit = [
Chris@0 401 'title[0][value]' => 'Obligatory question',
Chris@0 402 'op' => t('Save'),
Chris@0 403 ];
Chris@0 404 $post = [
Chris@0 405 'form_id' => 'quickedit_field_form',
Chris@0 406 'form_token' => $token_match[1],
Chris@0 407 'form_build_id' => $build_id_match[1],
Chris@0 408 ];
Chris@0 409 $post += $edit + $this->getAjaxPageStatePostData();
Chris@0 410
Chris@0 411 // Submit field form and check response. This should store the
Chris@0 412 // updated entity in PrivateTempStore on the server.
Chris@0 413 $response = $this->drupalPost('quickedit/form/' . 'node/1/title/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 414 $this->assertResponse(200);
Chris@0 415 $ajax_commands = Json::decode($response);
Chris@0 416 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 417 $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
Chris@0 418 $this->assertTrue(strpos($ajax_commands[0]['data'], 'Obligatory question'), 'Form value saved and printed back.');
Chris@0 419
Chris@0 420 // Ensure the text on the original node did not change yet.
Chris@0 421 $this->drupalGet('node/1');
Chris@0 422 $this->assertNoText('Obligatory question');
Chris@0 423
Chris@0 424 // Save the entity by moving the PrivateTempStore values to entity storage.
Chris@0 425 $post = ['nocssjs' => 'true'];
Chris@0 426 $response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
Chris@0 427 $this->assertResponse(200);
Chris@0 428 $ajax_commands = Json::decode($response);
Chris@0 429 $this->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
Chris@0 430 $this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is n quickeditEntitySaved command.');
Chris@0 431 $this->assertIdentical($ajax_commands[0]['data']['entity_type'], 'node', 'Saved entity is of type node.');
Chris@0 432 $this->assertIdentical($ajax_commands[0]['data']['entity_id'], '1', 'Entity id is 1.');
Chris@0 433
Chris@0 434 // Ensure the text on the original node did change.
Chris@0 435 $this->drupalGet('node/1');
Chris@0 436 $this->assertText('Obligatory question');
Chris@0 437 }
Chris@0 438 }
Chris@0 439
Chris@0 440 /**
Chris@0 441 * Tests that Quick Edit doesn't make fields rendered with display options
Chris@0 442 * editable.
Chris@0 443 */
Chris@0 444 public function testDisplayOptions() {
Chris@0 445 $node = Node::load('1');
Chris@0 446 $display_settings = [
Chris@0 447 'label' => 'inline',
Chris@0 448 ];
Chris@0 449 $build = $node->body->view($display_settings);
Chris@0 450 $output = \Drupal::service('renderer')->renderRoot($build);
Chris@0 451 $this->assertFalse(strpos($output, 'data-quickedit-field-id'), 'data-quickedit-field-id attribute not added when rendering field using dynamic display options.');
Chris@0 452 }
Chris@0 453
Chris@0 454 /**
Chris@0 455 * Tests that Quick Edit works with custom render pipelines.
Chris@0 456 */
Chris@0 457 public function testCustomPipeline() {
Chris@0 458 \Drupal::service('module_installer')->install(['quickedit_test']);
Chris@0 459
Chris@0 460 $custom_render_url = 'quickedit/form/node/1/body/en/quickedit_test-custom-render-data';
Chris@0 461 $this->drupalLogin($this->editorUser);
Chris@0 462
Chris@0 463 // Request editing to render results with the custom render pipeline.
Chris@0 464 $post = ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData();
Chris@0 465 $response = $this->drupalPost($custom_render_url, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 466 $ajax_commands = Json::decode($response);
Chris@0 467
Chris@0 468 // Prepare form values for submission. drupalPostAJAX() is not suitable for
Chris@0 469 // handling pages with JSON responses, so we need our own solution here.
Chris@0 470 $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
Chris@0 471 $this->assertTrue($form_tokens_found, 'Form tokens found in output.');
Chris@0 472
Chris@0 473 if ($form_tokens_found) {
Chris@0 474 $post = [
Chris@0 475 'form_id' => 'quickedit_field_form',
Chris@0 476 'form_token' => $token_match[1],
Chris@0 477 'form_build_id' => $build_id_match[1],
Chris@0 478 'body[0][summary]' => '',
Chris@0 479 'body[0][value]' => '<p>Fine thanks.</p>',
Chris@0 480 'body[0][format]' => 'filtered_html',
Chris@0 481 'op' => t('Save'),
Chris@0 482 ];
Chris@0 483 // Assume there is another field on this page, which doesn't use a custom
Chris@0 484 // render pipeline, but the default one, and it uses the "full" view mode.
Chris@0 485 $post += ['other_view_modes[]' => 'full'];
Chris@0 486
Chris@0 487 // Submit field form and check response. Should render with the custom
Chris@0 488 // render pipeline.
Chris@0 489 $response = $this->drupalPost($custom_render_url, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 490 $this->assertResponse(200);
Chris@0 491 $ajax_commands = Json::decode($response);
Chris@0 492 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
Chris@0 493 $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
Chris@0 494 $this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
Chris@0 495 $this->assertTrue(strpos($ajax_commands[0]['data'], '<div class="quickedit-test-wrapper">') !== FALSE, 'Custom render pipeline used to render the value.');
Chris@0 496 $this->assertIdentical(array_keys($ajax_commands[0]['other_view_modes']), ['full'], 'Field was also rendered in the "full" view mode.');
Chris@0 497 $this->assertTrue(strpos($ajax_commands[0]['other_view_modes']['full'], 'Fine thanks.'), '"full" version of field contains the form value.');
Chris@0 498 }
Chris@0 499 }
Chris@0 500
Chris@0 501 /**
Chris@0 502 * Tests Quick Edit on a node that was concurrently edited on the full node
Chris@0 503 * form.
Chris@0 504 */
Chris@0 505 public function testConcurrentEdit() {
Chris@0 506 $this->drupalLogin($this->editorUser);
Chris@0 507
Chris@0 508 $post = ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData();
Chris@0 509 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 510 $this->assertResponse(200);
Chris@0 511 $ajax_commands = Json::decode($response);
Chris@0 512
Chris@0 513 // Prepare form values for submission. drupalPostAJAX() is not suitable for
Chris@0 514 // handling pages with JSON responses, so we need our own solution here.
Chris@0 515 $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
Chris@0 516 $this->assertTrue($form_tokens_found, 'Form tokens found in output.');
Chris@0 517
Chris@0 518 if ($form_tokens_found) {
Chris@0 519 $post = [
Chris@0 520 'nocssjs' => 'true',
Chris@0 521 'form_id' => 'quickedit_field_form',
Chris@0 522 'form_token' => $token_match[1],
Chris@0 523 'form_build_id' => $build_id_match[1],
Chris@0 524 'body[0][summary]' => '',
Chris@0 525 'body[0][value]' => '<p>Fine thanks.</p>',
Chris@0 526 'body[0][format]' => 'filtered_html',
Chris@0 527 'op' => t('Save'),
Chris@0 528 ];
Chris@0 529
Chris@0 530 // Save the node on the regular node edit form.
Chris@0 531 $this->drupalPostForm('node/1/edit', [], t('Save'));
Chris@0 532 // Ensure different save timestamps for field editing.
Chris@0 533 sleep(2);
Chris@0 534
Chris@0 535 // Submit field form and check response. Should throw a validation error
Chris@0 536 // because the node was changed in the meantime.
Chris@0 537 $response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 538 $this->assertResponse(200);
Chris@0 539 $ajax_commands = Json::decode($response);
Chris@0 540 $this->assertIdentical(2, count($ajax_commands), 'The field form HTTP request results in two AJAX commands.');
Chris@0 541 $this->assertIdentical('quickeditFieldFormValidationErrors', $ajax_commands[1]['command'], 'The second AJAX command is a quickeditFieldFormValidationErrors command.');
Chris@0 542 $this->assertTrue(strpos($ajax_commands[1]['data'], 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'), 'Error message returned to user.');
Chris@0 543 }
Chris@0 544 }
Chris@0 545
Chris@0 546 /**
Chris@0 547 * Tests that Quick Edit's data- attributes are present for content blocks.
Chris@0 548 */
Chris@0 549 public function testContentBlock() {
Chris@0 550 \Drupal::service('module_installer')->install(['block_content']);
Chris@0 551
Chris@0 552 // Create and place a content_block block.
Chris@0 553 $block = BlockContent::create([
Chris@0 554 'info' => $this->randomMachineName(),
Chris@0 555 'type' => 'basic',
Chris@0 556 'langcode' => 'en',
Chris@0 557 ]);
Chris@0 558 $block->save();
Chris@0 559 $this->drupalPlaceBlock('block_content:' . $block->uuid());
Chris@0 560
Chris@0 561 // Check that the data- attribute is present.
Chris@0 562 $this->drupalLogin($this->editorUser);
Chris@0 563 $this->drupalGet('');
Chris@0 564 $this->assertRaw('data-quickedit-entity-id="block_content/1"');
Chris@0 565 }
Chris@0 566
Chris@0 567 /**
Chris@0 568 * Tests that Quick Edit can handle an image field.
Chris@0 569 */
Chris@0 570 public function testImageField() {
Chris@0 571 // Add an image field to the content type.
Chris@0 572 FieldStorageConfig::create([
Chris@0 573 'field_name' => 'field_image',
Chris@0 574 'type' => 'image',
Chris@0 575 'entity_type' => 'node',
Chris@0 576 ])->save();
Chris@0 577 FieldConfig::create([
Chris@0 578 'field_name' => 'field_image',
Chris@0 579 'field_type' => 'image',
Chris@0 580 'label' => t('Image'),
Chris@0 581 'entity_type' => 'node',
Chris@0 582 'bundle' => 'article',
Chris@0 583 ])->save();
Chris@0 584 entity_get_form_display('node', 'article', 'default')
Chris@0 585 ->setComponent('field_image', [
Chris@0 586 'type' => 'image_image',
Chris@0 587 ])
Chris@0 588 ->save();
Chris@0 589
Chris@0 590 // Add an image to the node.
Chris@0 591 $this->drupalLogin($this->editorUser);
Chris@0 592 $image = $this->drupalGetTestFiles('image')[0];
Chris@0 593 $this->drupalPostForm('node/1/edit', [
Chris@0 594 'files[field_image_0]' => $image->uri,
Chris@0 595 ], t('Upload'));
Chris@0 596 $this->drupalPostForm(NULL, [
Chris@0 597 'field_image[0][alt]' => 'Vivamus aliquet elit',
Chris@0 598 ], t('Save'));
Chris@0 599
Chris@0 600 // The image field form should load normally.
Chris@0 601 $response = $this->drupalPost('quickedit/form/node/1/field_image/en/full', '', ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData(), ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 602 $this->assertResponse(200);
Chris@0 603 $ajax_commands = Json::decode($response);
Chris@4 604 $this->assertIdentical('<form ', mb_substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
Chris@0 605 }
Chris@0 606
Chris@0 607 }