You are here

public function QuickEditAutocompleteTermTest::testAutocompleteQuickEdit in Zircon Profile 8.0

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

Tests Quick Edit autocomplete term behavior.

File

core/modules/quickedit/src/Tests/QuickEditAutocompleteTermTest.php, line 131
Contains \Drupal\quickedit\Tests\QuickEditAutocompleteTermTest.

Class

QuickEditAutocompleteTermTest
Tests in-place editing of autocomplete tags.

Namespace

Drupal\quickedit\Tests

Code

public function testAutocompleteQuickEdit() {
  $this
    ->drupalLogin($this->editorUser);
  $quickedit_uri = 'quickedit/form/node/' . $this->node
    ->id() . '/' . $this->fieldName . '/' . $this->node
    ->language()
    ->getId() . '/full';
  $post = array(
    'nocssjs' => 'true',
  ) + $this
    ->getAjaxPageStatePostData();
  $response = $this
    ->drupalPost($quickedit_uri, '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],
      $this->fieldName . '[target_id]' => implode(', ', array(
        $this->term1
          ->getName(),
        'new term',
        $this->term2
          ->getName(),
      )),
      'op' => t('Save'),
    );

    // Submit field form and check response. Should render back all the terms.
    $response = $this
      ->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = Json::decode($response);
    $this
      ->setRawContent($ajax_commands[0]['data']);
    $this
      ->assertLink($this->term1
      ->getName());
    $this
      ->assertLink($this->term2
      ->getName());
    $this
      ->assertText('new term');
    $this
      ->assertNoLink('new term');

    // Load the form again, which should now get it back from
    // PrivateTempStore.
    $quickedit_uri = 'quickedit/form/node/' . $this->node
      ->id() . '/' . $this->fieldName . '/' . $this->node
      ->language()
      ->getId() . '/full';
    $post = array(
      'nocssjs' => 'true',
    ) + $this
      ->getAjaxPageStatePostData();
    $response = $this
      ->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post);
    $ajax_commands = Json::decode($response);

    // The AjaxResponse's first command is an InsertCommand which contains
    // the form to edit the taxonomy term field, it should contain all three
    // taxonomy terms, including the one that has just been newly created and
    // which is not yet stored.
    $this
      ->setRawContent($ajax_commands[0]['data']);
    $expected = array(
      $this->term1
        ->getName() . ' (' . $this->term1
        ->id() . ')',
      'new term',
      $this->term2
        ->getName() . ' (' . $this->term2
        ->id() . ')',
    );
    $this
      ->assertFieldByName($this->fieldName . '[target_id]', implode(', ', $expected));

    // Save the entity.
    $post = array(
      'nocssjs' => 'true',
    );
    $response = $this
      ->drupalPostWithFormat('quickedit/entity/node/' . $this->node
      ->id(), 'json', $post);
    $this
      ->assertResponse(200);

    // The full node display should now link to all entities, with the new
    // one created in the database as well.
    $this
      ->drupalGet('node/' . $this->node
      ->id());
    $this
      ->assertLink($this->term1
      ->getName());
    $this
      ->assertLink($this->term2
      ->getName());
    $this
      ->assertLink('new term');
  }
}