You are here

function AttachedAssetsTest::testAlter in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Common/AttachedAssetsTest.php \Drupal\system\Tests\Common\AttachedAssetsTest::testAlter()

Tests altering a JavaScript's weight via hook_js_alter().

See also

simpletest_js_alter()

File

core/modules/system/src/Tests/Common/AttachedAssetsTest.php, line 392
Contains \Drupal\system\Tests\Common\AttachedAssetsTest.

Class

AttachedAssetsTest
Tests #attached assets: attached asset libraries and JavaScript settings.

Namespace

Drupal\system\Tests\Common

Code

function testAlter() {

  // Add both tableselect.js and simpletest.js.
  $build['#attached']['library'][] = 'core/drupal.tableselect';
  $build['#attached']['library'][] = 'simpletest/drupal.simpletest';
  $assets = AttachedAssets::createFromRenderArray($build);

  // Render the JavaScript, testing if simpletest.js was altered to be before
  // tableselect.js. See simpletest_js_alter() to see where this alteration
  // takes place.
  $js = $this->assetResolver
    ->getJsAssets($assets, FALSE)[1];
  $js_render_array = \Drupal::service('asset.js.collection_renderer')
    ->render($js);
  $rendered_js = $this->renderer
    ->renderPlain($js_render_array);
  $this
    ->assertTrue(strpos($rendered_js, 'simpletest.js') < strpos($rendered_js, 'core/misc/tableselect.js'), 'Altering JavaScript weight through the alter hook.');
}