function MediaLibraryJavascriptOrderTest::testJsScriptOrder

Same name and namespace in other branches
  1. main core/themes/default_admin/tests/src/Kernel/MediaLibraryJavascriptOrderTest.php \Drupal\Tests\default_admin\Kernel\MediaLibraryJavascriptOrderTest::testJsScriptOrder()

Tests that media library JS is after its dependencies in the page.

File

core/themes/default_admin/tests/src/Kernel/MediaLibraryJavascriptOrderTest.php, line 112

Class

MediaLibraryJavascriptOrderTest
Tests that media library UI JS is not added before its dependencies.

Namespace

Drupal\Tests\default_admin\Kernel

Code

public function testJsScriptOrder() : void {
  // View the entity_test add entity form as a user who can create entity_test
  // content and view media, so that the media library is visible.
  $this->setUpCurrentUser(permissions: [
    'administer entity_test content',
    'view media',
    'view the administration theme',
  ]);
  $this->drupalGet('entity_test/add');
  $text = $this->getSession()
    ->getPage()
    ->getContent();
  $scripts = [
    '<script src="/core/assets/vendor/jquery/jquery.min.js',
    '<script src="/core/assets/vendor/once/once.min.js',
    '<script src="/core/misc/drupal.js',
    '<script src="/core/misc/drupal.init.js',
    '<script src="/core/themes/default_admin/js/media-library.ui.js',
  ];
  // Track the positions of the script strings in the page text.
  $positions = [];
  // Track the largest position for a script string found in the page text.
  $maxPosition = -1;
  foreach ($scripts as $script) {
    $position = strpos($text, $script);
    $this->assertNotFalse($position);
    $maxPosition = $position > $maxPosition ? $position : $maxPosition;
    // Each position should be unique.
    $this->assertNotContains($position, $positions);
    $positions[$script] = $position;
  }
  // The large position should be the one for the media library script.
  $this->assertSame($maxPosition, $positions['<script src="/core/themes/default_admin/js/media-library.ui.js']);
}

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