You are here

public function QuickEditLoadingTest::testCustomPipeline in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/quickedit/src/Tests/QuickEditLoadingTest.php \Drupal\quickedit\Tests\QuickEditLoadingTest::testCustomPipeline()

Tests that Quick Edit works with custom render pipelines.

File

core/modules/quickedit/src/Tests/QuickEditLoadingTest.php, line 416
Contains \Drupal\quickedit\Tests\QuickEditLoadingTest.

Class

QuickEditLoadingTest
Tests loading of in-place editing functionality and lazy loading of its in-place editors.

Namespace

Drupal\quickedit\Tests

Code

public function testCustomPipeline() {
  \Drupal::service('module_installer')
    ->install(array(
    'quickedit_test',
  ));
  $custom_render_url = 'quickedit/form/node/1/body/en/quickedit_test-custom-render-data';
  $this
    ->drupalLogin($this->editorUser);

  // Request editing to render results with the custom render pipeline.
  $post = array(
    'nocssjs' => 'true',
  ) + $this
    ->getAjaxPageStatePostData();
  $response = $this
    ->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
  $ajax_commands = Json::decode($response);

  // Prepare form values for submission. drupalPostAJAX() is not suitable for
  // handling pages with JSON responses, so we need our own solution here.
  $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);
  $this
    ->assertTrue($form_tokens_found, 'Form tokens found in output.');
  if ($form_tokens_found) {
    $post = array(
      'form_id' => 'quickedit_field_form',
      'form_token' => $token_match[1],
      'form_build_id' => $build_id_match[1],
      'body[0][summary]' => '',
      'body[0][value]' => '<p>Fine thanks.</p>',
      'body[0][format]' => 'filtered_html',
      'op' => t('Save'),
    );

    // Assume there is another field on this page, which doesn't use a custom
    // render pipeline, but the default one, and it uses the "full" view mode.
    $post += array(
      'other_view_modes[]' => 'full',
    );

    // Submit field form and check response. Should render with the custom
    // render pipeline.
    $response = $this
      ->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = Json::decode($response);
    $this
      ->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
    $this
      ->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
    $this
      ->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
    $this
      ->assertTrue(strpos($ajax_commands[0]['data'], '<div class="quickedit-test-wrapper">') !== FALSE, 'Custom render pipeline used to render the value.');
    $this
      ->assertIdentical(array_keys($ajax_commands[0]['other_view_modes']), array(
      'full',
    ), 'Field was also rendered in the "full" view mode.');
    $this
      ->assertTrue(strpos($ajax_commands[0]['other_view_modes']['full'], 'Fine thanks.'), '"full" version of field contains the form value.');
  }
}