You are here

public function JqueryUiLibraryAssetsTest::testLibraryAssetLoadingOrder in Drupal 10

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/Libraries/JqueryUiLibraryAssetsTest.php \Drupal\FunctionalTests\Libraries\JqueryUiLibraryAssetsTest::testLibraryAssetLoadingOrder()

Confirms that jQuery UI assets load on the page in the configured order.

@dataProvider providerTestAssetLoading

File

core/tests/Drupal/FunctionalTests/Libraries/JqueryUiLibraryAssetsTest.php, line 218

Class

JqueryUiLibraryAssetsTest
Tests the loading of jQuery UI CSS and JS assets.

Namespace

Drupal\FunctionalTests\Libraries

Code

public function testLibraryAssetLoadingOrder($library) {
  $this
    ->drupalGet("jqueryui_library_assets_test/{$library}");
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // A pipe character in $libraries is delimiting multiple library names.
  $libraries = strpos($library, '|') !== FALSE ? explode('|', $library) : [
    $library,
  ];
  $files_to_check = [];

  // Populate an array with the filenames of every jQuery UI asset in the
  // libraries being tested. This will be used to confirm the configured
  // assets actually load on the test page.
  foreach ($libraries as $library_name) {
    foreach ([
      'css',
      'js',
    ] as $type) {
      $assets = $this->coreLibrariesWithJqueryUiAssets[$library_name][$type];
      foreach ($assets as $asset) {
        if (strpos($asset['data'], 'jquery.ui') !== FALSE) {
          $files_to_check[$asset['data']] = TRUE;
        }
      }
    }
  }
  $this
    ->assertNotEmpty($files_to_check);

  // Find all jQuery UI CSS files loaded to the page, and compare their
  // loading order to the weights configured in core.libraries.yml.
  $css = $this
    ->getSession()
    ->getPage()
    ->findAll('css', 'link[href*="jquery.ui"]');
  $css_weight = -100;
  foreach ($css as $item) {
    $file = $this
      ->trimFilePath($item
      ->getAttribute('href'));
    $found = FALSE;
    foreach ($this->weightGroupedAssets as $key => $array) {
      if (in_array($file, $array)) {
        $found = TRUE;
        $this
          ->assertGreaterThanOrEqual($css_weight, $key, "The file {$file} not loading in the expected order based on its weight value.");
        $css_weight = $key;
        unset($files_to_check[$file]);
      }
    }
    $this
      ->assertTrue($found, "A jQuery UI file: {$file} was loaded by the page, but is not taken into account by the test.");
  }
  $this
    ->assertGreaterThan(-100, $css_weight);
  $js_weight = -100;
  $js = $this
    ->getSession()
    ->getPage()
    ->findAll('css', 'script[src*="jquery.ui"]');
  foreach ($js as $item) {
    $file = $this
      ->trimFilePath($item
      ->getAttribute('src'));
    $found = FALSE;
    foreach ($this->weightGroupedAssets as $key => $array) {
      if (in_array($file, $array)) {
        $found = TRUE;
        $this
          ->assertGreaterThanOrEqual($js_weight, $key, "The file {$file} not loading in the expected order based on its weight value.");
        $js_weight = $key;
        unset($files_to_check[$file]);
      }
    }
    $this
      ->assertTrue($found, "A jQuery UI file: {$file} was loaded by the page, but is not taken into account by the test.");
  }
  $this
    ->assertGreaterThan(-100, $js_weight);
  $this
    ->assertEmpty($files_to_check);
}