You are here

public function AttachedAssetsTest::testAlter in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testAlter()
  2. 9 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

common_test_js_alter()

File

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

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 alter.js.
  $build['#attached']['library'][] = 'core/drupal.tableselect';
  $build['#attached']['library'][] = 'common_test/hook_js_alter';
  $assets = AttachedAssets::createFromRenderArray($build);

  // Render the JavaScript, testing if alter.js was altered to be before
  // tableselect.js. See common_test_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);

  // Verify that JavaScript weight is correctly altered by the alter hook.
  $this
    ->assertLessThan(strpos($rendered_js, 'core/misc/tableselect.js'), strpos($rendered_js, 'alter.js'));
}