You are here

function AttachedAssetsTest::testLibraryAlter in Zircon Profile 8.0

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

Adds a JavaScript library to the page and alters it.

See also

common_test_library_info_alter()

File

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

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testLibraryAlter() {

  // Verify that common_test altered the title of Farbtastic.

  /** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery */
  $library_discovery = \Drupal::service('library.discovery');
  $library = $library_discovery
    ->getLibraryByName('core', 'jquery.farbtastic');
  $this
    ->assertEqual($library['version'], '0.0', 'Registered libraries were altered.');

  // common_test_library_info_alter() also added a dependency on jQuery Form.
  $build['#attached']['library'][] = 'core/jquery.farbtastic';
  $assets = AttachedAssets::createFromRenderArray($build);
  $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, 'core/assets/vendor/jquery-form/jquery.form.min.js'), 'Altered library dependencies are added to the page.');
}