QuickEditLoadingTest.php 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace Drupal\Tests\quickedit\FunctionalJavascript;

use Behat\Mink\Session;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests loading of in-place editing functionality and lazy loading of its
 * in-place editors.
 *
 * @group quickedit
 */
class QuickEditLoadingTest extends WebDriverTestBase {

  use ContextualLinkClickTrait;

  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
  }

  /**
   * Modules to enable.
   *
   * @var array
   */
36
  protected static $modules = [
37
38
39
40
41
42
43
    'contextual',
    'quickedit',
    'filter',
    'node',
    'image',
  ];

44
45
46
47
48
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  /**
   * An user with permissions to create and edit articles.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $authorUser;

  /**
   * A test node.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $testNode;

  /**
   * A author user with permissions to access in-place editor.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $editorUser;

  /**
   * {@inheritdoc}
   */
73
  protected function setUp(): void {
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    parent::setUp();

    // Create a text format.
    $filtered_html_format = FilterFormat::create([
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => [],
    ]);
    $filtered_html_format->save();

    // Create a node type.
    $this->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);

    // Set the node type to initially not have revisions.
    // Testing with revisions will be done later.
    $node_type = NodeType::load('article');
    $node_type->setNewRevision(FALSE);
    $node_type->save();

    // Create one node of the above node type using the above text format.
    $this->testNode = $this->drupalCreateNode([
      'type' => 'article',
      'body' => [
        0 => [
          'value' => '<p>How are you?</p>',
          'format' => 'filtered_html',
        ],
      ],
      'revision_log' => $this->randomString(),
    ]);

    // Create 2 users, the only difference being the ability to use in-place
    // editing
    $basic_permissions = [
      'access content',
      'create article content',
      'edit any article content',
      'use text format filtered_html',
      'access contextual links',
    ];
    $this->authorUser = $this->drupalCreateUser($basic_permissions);
    $this->editorUser = $this->drupalCreateUser(array_merge($basic_permissions, ['access in-place editing']));
  }

  /**
   * Test the loading of Quick Edit with different permissions.
   */
  public function testUserPermissions() {
    $assert = $this->assertSession();
    $this->drupalLogin($this->authorUser);
    $this->drupalGet('node/1');

    // Library and in-place editors.
    $this->assertNoRaw('core/modules/quickedit/js/quickedit.js', 'Quick Edit library not loaded.');
    $this->assertNoRaw('core/modules/quickedit/js/editors/formEditor.js', "'form' in-place editor not loaded.");

    // HTML annotation and title class does not exist for users without
    // permission to in-place edit.
    $this->assertNoRaw('data-quickedit-entity-id="node/1"');
    $this->assertNoRaw('data-quickedit-field-id="node/1/body/en/full"');
    $this->assertNoFieldByXPath('//h1[contains(@class, "js-quickedit-page-title")]');
    $assert->linkNotExists('Quick edit');

    // Tests the loading of Quick Edit when a user does have access to it.
    // Also ensures lazy loading of in-place editors works.
    $nid = $this->testNode->id();
    // There should be only one revision so far.
    $node = Node::load($nid);
146
    $vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    $this->assertCount(1, $vids, 'The node has only one revision.');
    $original_log = $node->revision_log->value;

    $this->drupalLogin($this->editorUser);
    $this->drupalGet('node/' . $nid);
    $page = $this->getSession()->getPage();

    // Wait "Quick edit" button for node.
    $assert->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');
    // Click by "Quick edit".
    $this->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
    // Switch to body field.
    $page->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')->click();
    $assert->assertWaitOnAjaxRequest();

    // Wait and update body field.
    $body_field_locator = '[name="body[0][value]"]';
    $body_text = 'Fine thanks.';
    $assert->waitForElementVisible('css', $body_field_locator)->setValue('<p>' . $body_text . '</p>');

    // Wait and click by "Save" button after body field was changed.
    $assert->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
    $assert->assertWaitOnAjaxRequest();

    $node = Node::load($nid);
172
    $vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    $this->assertCount(1, $vids, 'The node has only one revision.');
    $this->assertSame($original_log, $node->revision_log->value, 'The revision log message is unchanged.');

    // Ensure that the changes take effect.
    $assert->responseMatches("|\s*$body_text\s*|");

    // Reload the page and check for updated body.
    $this->drupalGet('node/' . $nid);
    $assert->pageTextContains($body_text);
  }

  /**
   * Test Quick Edit does not appear for entities with pending revisions.
   */
  public function testWithPendingRevision() {
    $this->drupalLogin($this->editorUser);

    // Verify that the preview is loaded correctly.
    $this->drupalPostForm('node/add/article', ['title[0][value]' => 'foo'], 'Preview');
    // Verify that quickedit is not active on preview.
    $this->assertNoRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
    $this->assertNoRaw('data-quickedit-field-id="node/' . $this->testNode->id() . '/title/' . $this->testNode->language()->getId() . '/full"');

    $this->drupalGet('node/' . $this->testNode->id());
    $this->assertRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
    $this->assertRaw('data-quickedit-field-id="node/' . $this->testNode->id() . '/title/' . $this->testNode->language()->getId() . '/full"');

    // Wait for the page to completely load before making any changes to the
    // node. This allows Quick Edit to fetch the metadata without causing
    // database locks on SQLite.
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->testNode->title = 'Updated node';
    $this->testNode->setNewRevision(TRUE);
    $this->testNode->isDefaultRevision(FALSE);
    $this->testNode->save();

    $this->drupalGet('node/' . $this->testNode->id());
    $this->assertNoRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
    $this->assertNoRaw('data-quickedit-field-id="node/' . $this->testNode->id() . '/title/' . $this->testNode->language()->getId() . '/full"');
  }

  /**
   * Tests the loading of Quick Edit for the title base field.
   */
  public function testTitleBaseField() {
    $page = $this->getSession()->getPage();
    $assert = $this->assertSession();
    $nid = $this->testNode->id();

    $this->drupalLogin($this->editorUser);
    $this->drupalGet('node/' . $nid);

    // Wait "Quick edit" button for node.
    $assert->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');
    // Click by "Quick edit".
    $this->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
    // Switch to title field.
    $page->find('css', '[data-quickedit-field-id="node/' . $nid . '/title/en/full"]')->click();
    $assert->assertWaitOnAjaxRequest();

    // Wait and update title field.
    $field_locator = '.field--name-title';
    $text_new = 'Obligatory question';
    $assert->waitForElementVisible('css', $field_locator)->setValue($text_new);

    // Wait and click by "Save" button after title field was changed.
    $this->assertSession()->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
    $assert->assertWaitOnAjaxRequest();

    // Ensure that the changes take effect.
    $assert->responseMatches("|\s*$text_new\s*|");

    // Reload the page and check for updated title.
    $this->drupalGet('node/' . $nid);
    $assert->pageTextContains($text_new);
  }

  /**
   * Tests that Quick Edit doesn't make fields rendered with display options
   * editable.
   */
  public function testDisplayOptions() {
    $node = Node::load('1');
    $display_settings = [
      'label' => 'inline',
    ];
    $build = $node->body->view($display_settings);
    $output = \Drupal::service('renderer')->renderRoot($build);
    $this->assertFalse(strpos($output, 'data-quickedit-field-id'), 'data-quickedit-field-id attribute not added when rendering field using dynamic display options.');
  }

  /**
   * Tests Quick Edit on a node that was concurrently edited on the full node
   * form.
   */
  public function testConcurrentEdit() {
    $nid = $this->testNode->id();
    $this->drupalLogin($this->authorUser);

    // Open the edit page in the default session.
    $this->drupalGet('node/' . $nid . '/edit');

    // Switch to a concurrent session and save a quick edit change.
    // We need to do some bookkeeping to keep track of the logged in user.
    $logged_in_user = $this->loggedInUser;
    $this->loggedInUser = FALSE;
    // Register a session to preform concurrent editing.
    $driver = $this->getDefaultDriverInstance();
    $session = new Session($driver);
    $this->mink->registerSession('concurrent', $session);
    $this->mink->setDefaultSessionName('concurrent');
    $this->initFrontPage();
    $this->drupalLogin($this->editorUser);
    $this->drupalGet('node/' . $nid);

    $assert = $this->assertSession();
    $page = $this->getSession()->getPage();

    // Wait "Quick edit" button for node.
    $assert->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');
    // Click by "Quick edit".
    $this->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
    // Switch to body field.
    $page->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')->click();
    $assert->assertWaitOnAjaxRequest();

    // Wait and update body field.
    $body_field_locator = '[name="body[0][value]"]';
    $body_text = 'Fine thanks.';
    $assert->waitForElementVisible('css', $body_field_locator)->setValue('<p>' . $body_text . '</p>');

    // Wait and click by "Save" button after body field was changed.
    $assert->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
    $assert->assertWaitOnAjaxRequest();

    // Ensure that the changes take effect.
    $assert->responseMatches("|\s*$body_text\s*|");

    // Switch back to the default session.
    $this->mink->setDefaultSessionName('default');
    $this->loggedInUser = $logged_in_user;
    // Ensure different save timestamps for field editing.
    sleep(2);
    $this->drupalPostForm(NULL, ['body[0][value]' => '<p>Concurrent edit!</p>'], 'Save');

    $this->getSession()->getPage()->hasContent('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');
  }

  /**
   * Tests that Quick Edit's data- attributes are present for content blocks.
   */
  public function testContentBlock() {
    \Drupal::service('module_installer')->install(['block_content']);

    // Create and place a content_block block.
    $block = BlockContent::create([
      'info' => $this->randomMachineName(),
      'type' => 'basic',
      'langcode' => 'en',
    ]);
    $block->save();
    $this->drupalPlaceBlock('block_content:' . $block->uuid());

    // Check that the data- attribute is present.
    $this->drupalLogin($this->editorUser);
    $this->drupalGet('');
    $this->assertRaw('data-quickedit-entity-id="block_content/1"');
  }

  /**
   * Tests that Quick Edit can handle an image field.
   */
  public function testImageField() {
    $page = $this->getSession()->getPage();
    $assert = $this->assertSession();

    // Add an image field to the content type.
    FieldStorageConfig::create([
      'field_name' => 'field_image',
      'type' => 'image',
      'entity_type' => 'node',
    ])->save();
    FieldConfig::create([
      'field_name' => 'field_image',
      'field_type' => 'image',
      'label' => t('Image'),
      'entity_type' => 'node',
      'bundle' => 'article',
    ])->save();
362
    \Drupal::service('entity_display.repository')->getFormDisplay('node', 'article', 'default')
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
      ->setComponent('field_image', [
        'type' => 'image_image',
      ])
      ->save();
    $display = EntityViewDisplay::load('node.article.default');
    $display->setComponent('field_image', [
      'type' => 'image',
    ])->save();

    // Add an image to the node.
    $this->drupalLogin($this->editorUser);
    $this->drupalGet('node/1/edit');
    $image = $this->drupalGetTestFiles('image')[0];
    $image_path = $this->container->get('file_system')->realpath($image->uri);
    $page->attachFileToField('files[field_image_0]', $image_path);
    $alt_field = $assert->waitForField('field_image[0][alt]');
    $this->assertNotEmpty($alt_field);
    $this->drupalPostForm(NULL, [
      'field_image[0][alt]' => 'Vivamus aliquet elit',
    ], t('Save'));

    // The image field form should load normally.
    // Wait "Quick edit" button for node.
    $assert->waitForElement('css', '[data-quickedit-entity-id="node/1"] .contextual .quickedit');
    // Click by "Quick edit".
    $this->clickContextualLink('[data-quickedit-entity-id="node/1"]', 'Quick edit');
    // Switch to body field.
    $assert->waitForElement('css', '[data-quickedit-field-id="node/1/field_image/en/full"]')->click();
    $assert->assertWaitOnAjaxRequest();

    $field_locator = '.field--name-field-image';
    $assert->waitForElementVisible('css', $field_locator);
  }

}