diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php index 88aef27..5d85e33 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php @@ -129,7 +129,8 @@ public function buildByExtension($extension) { // properly resolve dependencies for all (css) libraries per category, // and only once prior to rendering out an HTML page. if ($type == 'css' && !empty($library[$type])) { - assert('\Drupal\Core\Asset\LibraryDiscoveryParser::validateCssLibrary($library[$type])', 'CSS must be nested under a category. See https://www.drupal.org/node/2274843.'); + assert('\Drupal\Core\Asset\LibraryDiscoveryParser::validateCssLibrary($library[$type]) < 2', 'CSS files should be specified as key/value pairs, where the values are configuration options. See https://www.drupal.org/node/2274843.'); + assert('\Drupal\Core\Asset\LibraryDiscoveryParser::validateCssLibrary($library[$type]) === 0', 'CSS must be nested under a category. See https://www.drupal.org/node/2274843.'); foreach ($library[$type] as $category => $files) { $category_weight = 'CSS_' . strtoupper($category); assert('defined($category_weight)', 'Invalid CSS category: ' . $category . '. See https://www.drupal.org/node/2274843.'); @@ -469,23 +470,28 @@ protected function resolveThemeAssetPath($theme_path, $overriding_asset) { * @param array $library * The library definition array. * - * @return bool - * Returns TRUE for a valid library. Proper category checking is not done - * here, as that is captured by a separate assertion. + * @return int + * Returns based on validity: + * - 0 if the library definition is valid + * - 1 if the library definition has improper nesting + * - 2 if the library definition specifies files as an array */ public static function validateCssLibrary($library) { $categories = []; // Verify options first and return early if invalid. foreach ($library as $category => $files) { + if (!is_array($files)) { + return 2; + } $categories[] = $category; foreach ($files as $source => $options) { if (!is_array($options)) { - return FALSE; + return 1; } } } - return TRUE; + return 0; } } diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php index e18b0ae..efce82f 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php @@ -535,9 +535,6 @@ public function testLibraryWithLicenses() { /** * Verify an assertion fails if CSS declarations have non-existent categories. - * - * @expectedException \AssertionError - * @expectedExceptionMessage Invalid CSS category: bad_category. See https://www.drupal.org/node/2274843. */ public function testCssCategoryAssert() { $this->moduleHandler->expects($this->atLeastOnce()) @@ -551,14 +548,12 @@ public function testCssCategoryAssert() { // This will fail since the CSS declaration isn't properly nested under // a category. + $this->setExpectedException(\AssertionError::class, 'Invalid CSS category: bad_category. See https://www.drupal.org/node/2274843.'); $this->libraryDiscoveryParser->buildByExtension('css_bad_category'); } /** * Verify an assertion fails if CSS declarations aren't properly nested. - * - * @expectedException \AssertionError - * @expectedExceptionMessage CSS must be nested under a category. See https://www.drupal.org/node/2274843. */ public function testCssNestingAssert() { $this->moduleHandler->expects($this->atLeastOnce()) @@ -572,9 +567,29 @@ public function testCssNestingAssert() { // This will fail since the CSS declaration isn't properly nested under // a category. + $this->setExpectedException(\AssertionError::class, 'CSS must be nested under a category. See https://www.drupal.org/node/2274843.'); $this->libraryDiscoveryParser->buildByExtension('css_bad_nesting'); } + /** + * Verify an assertion fails if CSS declarations are a simple array of files. + */ + public function testCssNestingArrayAssert() { + $this->moduleHandler->expects($this->atLeastOnce()) + ->method('moduleExists') + ->with('css_bad_nesting_array') + ->will($this->returnValue(TRUE)); + + $path = __DIR__ . '/library_test_files'; + $path = substr($path, strlen($this->root) + 1); + $this->libraryDiscoveryParser->setPaths('module', 'css_bad_nesting_array', $path); + + // This will fail since the CSS declaration isn't properly nested under + // a category. + $this->setExpectedException(\AssertionError::class, 'CSS files should be specified as key/value pairs, where the values are configuration options. See https://www.drupal.org/node/2274843.'); + $this->libraryDiscoveryParser->buildByExtension('css_bad_nesting_array'); + } + } /**