You are here

public function FrameworkTest::testOrder in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Ajax/FrameworkTest.php \Drupal\system\Tests\Ajax\FrameworkTest::testOrder()

Tests AjaxResponse::prepare() AJAX commands ordering.

File

core/modules/system/src/Tests/Ajax/FrameworkTest.php, line 37
Contains \Drupal\system\Tests\Ajax\FrameworkTest.

Class

FrameworkTest
Performs tests on AJAX framework functions.

Namespace

Drupal\system\Tests\Ajax

Code

public function testOrder() {
  $expected_commands = array();

  // 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');
  $expected_commands[0] = new SettingsCommand(array(
    'ajax' => 'test',
  ), TRUE);
  $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
  // AjaxResponse::ajaxRender().
  // @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. JavaScript settings
  // 2. CSS files
  // 3. JavaScript files in the header
  // 4. JavaScript files in the footer
  // 5. Any other AJAX commands, in whatever order they were added.
  $commands = $this
    ->drupalPostAjaxForm(NULL, array(), NULL, 'ajax-test/order', array(), array(), NULL, array());
  $this
    ->assertCommand(array_slice($commands, 0, 1), $expected_commands[0]
    ->render(), 'Settings command is first.');
  $this
    ->assertCommand(array_slice($commands, 1, 1), $expected_commands[1]
    ->render(), 'CSS command is second (and CSS files are ordered correctly).');
  $this
    ->assertCommand(array_slice($commands, 2, 1), $expected_commands[2]
    ->render(), 'Header JS command is third.');
  $this
    ->assertCommand(array_slice($commands, 3, 1), $expected_commands[3]
    ->render(), 'Footer JS command is fourth.');
  $this
    ->assertCommand(array_slice($commands, 4, 1), $expected_commands[4]
    ->render(), 'HTML command is fifth.');
}