function FrameworkTest::testOrder

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php \Drupal\Tests\system\Functional\Ajax\FrameworkTest::testOrder()
  2. 10 core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php \Drupal\Tests\system\Functional\Ajax\FrameworkTest::testOrder()
  3. 11.x core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php \Drupal\Tests\system\Functional\Ajax\FrameworkTest::testOrder()

Tests AjaxResponse::prepare() AJAX commands ordering.

File

core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php, line 46

Class

FrameworkTest
Performs tests on AJAX framework functions.

Namespace

Drupal\Tests\system\Functional\Ajax

Code

public function testOrder() {
    $expected_commands = [];
    // Expected commands, in a very specific order.
    $asset_resolver = \Drupal::service('asset.resolver');
    $css_collection_renderer = \Drupal::service('asset.css.collection_renderer');
    $js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
    $renderer = \Drupal::service('renderer');
    $build['#attached']['library'][] = 'ajax_test/order-css-command';
    $assets = AttachedAssets::createFromRenderArray($build);
    $css_render_array = $css_collection_renderer->render($asset_resolver->getCssAssets($assets, FALSE));
    $expected_commands[1] = new AddCssCommand($renderer->renderRoot($css_render_array));
    $build['#attached']['library'][] = 'ajax_test/order-header-js-command';
    $build['#attached']['library'][] = 'ajax_test/order-footer-js-command';
    $assets = AttachedAssets::createFromRenderArray($build);
    list($js_assets_header, $js_assets_footer) = $asset_resolver->getJsAssets($assets, FALSE);
    $js_header_render_array = $js_collection_renderer->render($js_assets_header);
    $js_footer_render_array = $js_collection_renderer->render($js_assets_footer);
    $expected_commands[2] = new PrependCommand('head', $js_header_render_array);
    $expected_commands[3] = new AppendCommand('body', $js_footer_render_array);
    $expected_commands[4] = new HtmlCommand('body', 'Hello, world!');
    // Load any page with at least one CSS file, at least one JavaScript file
    // and at least one #ajax-powered element. The latter is an assumption of
    // drupalPostAjaxForm(), the two former are assumptions of the Ajax
    // renderer.
    // @todo refactor AJAX Framework + tests to make less assumptions.
    $this->drupalGet('ajax_forms_test_lazy_load_form');
    // Verify AJAX command order — this should always be the order:
    // 1. CSS files
    // 2. JavaScript files in the header
    // 3. JavaScript files in the footer
    // 4. Any other AJAX commands, in whatever order they were added.
    $commands = $this->drupalGetAjax('ajax-test/order');
    $this->assertCommand(array_slice($commands, 0, 1), $expected_commands[1]->render());
    $this->assertCommand(array_slice($commands, 1, 1), $expected_commands[2]->render());
    $this->assertCommand(array_slice($commands, 2, 1), $expected_commands[3]->render());
    $this->assertCommand(array_slice($commands, 3, 1), $expected_commands[4]->render());
}

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