function JqueryUiLibraryAssetsTest::testLibraryAssetLoadingOrder

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalTests/Libraries/JqueryUiLibraryAssetsTest.php \Drupal\FunctionalTests\Libraries\JqueryUiLibraryAssetsTest::testLibraryAssetLoadingOrder()
  2. 11.x 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 221

Class

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

Namespace

Drupal\FunctionalTests\Libraries

Code

public function testLibraryAssetLoadingOrder($library, array $expected_css, array $expected_js) : void {
    $this->drupalGet("jqueryui_library_assets_test/{$library}");
    $this->assertSession()
        ->statusCodeEquals(200);
    // A pipe character in $libraries is delimiting multiple library names.
    $libraries = str_contains($library, '|') ? 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 (str_contains($asset['data'], 'jquery.ui')) {
                    $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);
}

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