You are here

public function PanelizerQuickEditTest::testPanelizerQuickEdit in Panelizer 8.4

Same name and namespace in other branches
  1. 8.5 panelizer_quickedit/tests/src/FunctionalJavascript/PanelizerQuickEditTest.php \Drupal\Tests\panelizer_quickedit\FunctionalJavascript\PanelizerQuickEditTest::testPanelizerQuickEdit()
  2. 8.3 panelizer_quickedit/tests/src/FunctionalJavascript/PanelizerQuickEditTest.php \Drupal\Tests\panelizer_quickedit\FunctionalJavascript\PanelizerQuickEditTest::testPanelizerQuickEdit()

Tests Quick Editing a Panelized Node.

File

panelizer_quickedit/tests/src/FunctionalJavascript/PanelizerQuickEditTest.php, line 89

Class

PanelizerQuickEditTest
Tests that a Panelized Node can be Quick-Edited.

Namespace

Drupal\Tests\panelizer_quickedit\FunctionalJavascript

Code

public function testPanelizerQuickEdit() {

  /** @var \Drupal\panelizer\PanelizerInterface $panelizer */
  $panelizer = \Drupal::service('panelizer');
  $displays = $panelizer
    ->getDefaultPanelsDisplays('node', 'page', 'default');
  $display = $displays['default'];

  // Find the "test_field" block.
  $block_id = FALSE;
  foreach ($display
    ->getConfiguration()['blocks'] as $block) {
    if ($block['id'] === 'entity_field:node:test_field') {
      $block_id = $block['uuid'];
    }
  }

  // Make sure we found a valid UUID.
  $this
    ->assertNotFalse($block_id);

  // Create an Article.
  $node = $this
    ->drupalCreateNode([
    'type' => 'page',
    'test_field' => [
      'value' => 'Change me',
    ],
  ]);

  // Visit the new node.
  $this
    ->drupalGet($node
    ->toUrl());

  // This is the unique ID we append to normal Quick Edit field IDs.
  $panelizer_id = 'panelizer-full-block-id-' . $block_id;

  // Assemble common CSS selectors.
  $entity_selector = '[data-quickedit-entity-id="node/' . $node
    ->id() . '"]';
  $field_selector = '[data-quickedit-field-id="node/' . $node
    ->id() . '/test_field/' . $node
    ->language()
    ->getId() . '/' . $panelizer_id . '"]';

  // Wait until Quick Edit loads.
  $condition = "jQuery('" . $entity_selector . " .quickedit').length > 0";
  $this
    ->assertJsCondition($condition, 10000);

  // Initiate Quick Editing.
  $this
    ->triggerClick($entity_selector . ' [data-contextual-id] > button');
  $this
    ->click($entity_selector . ' [data-contextual-id] .quickedit > a');
  $this
    ->triggerClick($field_selector);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Trigger an edit with Javascript (this is a "contenteditable" element).
  $this
    ->getSession()
    ->executeScript("jQuery('" . $field_selector . "').text('Hello world').trigger('keyup');");

  // To prevent 403s on save, we re-set our request (cookie) state.
  $this
    ->prepareRequest();

  // Save the change.
  $this
    ->triggerClick('.quickedit-button.action-save');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Re-visit the node to make sure the edit worked.
  $this
    ->drupalGet($node
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains('Hello world');
}