Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 
  • In Drupal7, the single DrupalWebTestCase::drupalPostAJAX() method manages both sending an AJAX request, and updating the current HTML content upon return.
  • In Drupal 8, we now have the option to post AJAX requests that aren't processed by a form controller, through a new drupalPost() method. This method however is NOT updating the current HTML content.
  • A new drupalProcessAjaxResponse() method has been introduced to process AJAX response in the current content. This method is used by drupalPostAjaxForm() itself, but can also be used when it is necessary to update page content after a non-form initiated AJAX request.

Code example - from the views preview UI test:

  /**
   * Mimic clicking on a preview link.
   *
   * @param string $url
   *   The url to navigate to.
   * @param int $row_count
   *   The expected number of rows in the preview.
   */
  protected function clickPreviewLinkAJAX($url, $row_count) {
    $content = $this->content;
    $drupal_settings = $this->drupalSettings;
    $ajax_settings = array(
      'wrapper' => 'views-preview-wrapper',
      'method' => 'replaceWith',
    );
    $url = $this->getAbsoluteUrl($url);
    $post = array('js' => 'true') + $this->getAjaxPageStatePostData();
    $result = drupal_json_decode($this->drupalPost($url, 'application/vnd.drupal-ajax', $post));
    if (!empty($result)) {
      $this->drupalProcessAjaxResponse($content, $result, $ajax_settings, $drupal_settings);
    }
    $this->assertPreviewAJAX($result, $row_count);
  }

Note: content and drupalSettings need to be stored away before invoking drupalPost(), and then passed back to drupalProcessAjaxResponse(). This because drupalPost() replaces current content completely with the content returned by the AJAX controller, which is a sequence of AJAX commands (not HTML).

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done