function FrameworkTest::testOrder

Tests AjaxResponse::prepare() AJAX commands ordering.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Ajax/FrameworkTest.php, line 43
Contains \Drupal\system\Tests\Ajax\FrameworkTest.

Class

FrameworkTest
Tests primary Ajax framework functions.

Namespace

Drupal\system\Tests\Ajax

Code

function testOrder() {
  $path = drupal_get_path('module', 'system');
  $expected_commands = array();

  // Expected commands, in a very specific order.
  $expected_commands[0] = new SettingsCommand(array(
    'ajax' => 'test',
  ), TRUE);
  drupal_static_reset('drupal_add_css');
  drupal_add_css($path . '/css/system.admin.css');
  drupal_add_css($path . '/css/system.maintenance.css');
  $expected_commands[1] = new AddCssCommand(drupal_get_css(drupal_add_css(), TRUE));
  drupal_static_reset('drupal_add_js');
  drupal_add_js($path . '/system.js');
  $expected_commands[2] = new PrependCommand('head', drupal_get_js('header', drupal_add_js(), TRUE));
  drupal_static_reset('drupal_add_js');
  drupal_add_js($path . '/system.modules.js', array(
    'scope' => 'footer',
  ));
  $expected_commands[3] = new AppendCommand('body', drupal_get_js('footer', drupal_add_js(), TRUE));
  $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
  // drupalPostAJAX(), the two former are assumptions of
  // AjaxReponse::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
    ->drupalPostAJAX(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.');
}