function AttachedAssetsTest::testAlter

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testAlter()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testAlter()
  3. 11.x 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 379

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testAlter() : void {
    // 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, \Drupal::languageManager()->getCurrentLanguage())[1];
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_js = (string) $this->renderer
        ->renderInIsolation($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'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.