function AjaxTest::testGlobalEvents

Same name in other branches
  1. 10 core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxTest::testGlobalEvents()
  2. 11.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxTest::testGlobalEvents()

Tests that jQuery's global Ajax events are triggered at the correct time.

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php, line 159

Class

AjaxTest
Tests AJAX responses.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testGlobalEvents() {
    $session = $this->getSession();
    $assert = $this->assertSession();
    $expected_event_order = implode('', [
        'ajaxSuccess',
        'ajaxComplete',
        'ajaxStop',
    ]);
    $this->drupalGet('ajax-test/global-events');
    // Ensure that a non-Drupal Ajax request triggers the expected events, in
    // the correct order, a single time.
    $session->executeScript('jQuery.get(Drupal.url("core/COPYRIGHT.txt"))');
    $assert->assertWaitOnAjaxRequest();
    $assert->elementTextEquals('css', '#test_global_events_log', $expected_event_order);
    $assert->elementTextEquals('css', '#test_global_events_log2', $expected_event_order);
    // Ensure that an Ajax request to a Drupal Ajax response, but that was not
    // initiated with Drupal.Ajax(), triggers the expected events, in the
    // correct order, a single time. We expect $expected_event_order to appear
    // twice in each log element, because Drupal Ajax response commands (such
    // as the one to clear the log element) are only executed for requests
    // initiated with Drupal.Ajax(), and these elements already contain the
    // text that was added above.
    $session->executeScript('jQuery.get(Drupal.url("ajax-test/global-events/clear-log"))');
    $assert->assertWaitOnAjaxRequest();
    $assert->elementTextEquals('css', '#test_global_events_log', str_repeat($expected_event_order, 2));
    $assert->elementTextEquals('css', '#test_global_events_log2', str_repeat($expected_event_order, 2));
    // Ensure that a Drupal Ajax request triggers the expected events, in the
    // correct order, a single time.
    // - We expect the first log element to list the events exactly once,
    //   because the Ajax response clears it, and we expect the events to be
    //   triggered after the commands are executed.
    // - We expect the second log element to list the events exactly three
    //   times, because it already contains the two from the code that was
    //   already executed above. This additional log element that isn't cleared
    //   by the response's command ensures that the events weren't triggered
    //   additional times before the response commands were executed.
    $this->click('#test_global_events_drupal_ajax_link');
    $assert->assertWaitOnAjaxRequest();
    $assert->elementTextEquals('css', '#test_global_events_log', $expected_event_order);
    $assert->elementTextEquals('css', '#test_global_events_log2', str_repeat($expected_event_order, 3));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.