You are here

public function AttachedAssetsTest::testAlter in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testAlter()
  2. 10 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testAlter()

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

See also

simpletest_js_alter()

File

core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php, line 388

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public 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.');
}