annotate core/modules/quickedit/src/Tests/QuickEditLoadingTest.php @ 0:c75dbcec494b

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