Verified Commit b638924e authored by Alex Pott's avatar Alex Pott
Browse files

task: #3634418 Implement better messaging for Assert for css in libraries discovery

By: tregonia
By: leandro713
(cherry picked from commit 813e696a)
parent 4ca1eee3
Loading
Loading
Loading
Loading
Loading
+5
Changes for core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -126,6 +126,7 @@ 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(static::validateCssLibrary($library[$type]) !== 3, 'CSS file options must be an array. Use {} when no options are needed. See https://www.drupal.org/node/2274843.');
          assert(static::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(static::validateCssLibrary($library[$type]) === 0, 'CSS must be nested under a category. See https://www.drupal.org/node/2274843.');
          foreach ($library[$type] as $category => $files) {
@@ -590,6 +591,7 @@ protected function resolveThemeAssetPath($theme_path, $overriding_asset) {
   *     - 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
   *     - 3 if the library definition is missing options for a file
   */
  public static function validateCssLibrary($library) {
    $categories = [];
@@ -600,6 +602,9 @@ public static function validateCssLibrary($library) {
      }
      $categories[] = $category;
      foreach ($files as $options) {
        if ($options === NULL) {
          return 3;
        }
        if (!is_array($options)) {
          return 1;
        }
+4
Changes for core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -781,6 +781,10 @@ public static function providerTestCssAssert(): array {
        'css_bad_nesting_array',
        'CSS files should be specified as key/value pairs, where the values are configuration options. See https://www.drupal.org/node/2274843.',
      ],
      'Missing CSS options' => [
        'css_missing_options',
        'CSS file options must be an array. Use {} when no options are needed. See https://www.drupal.org/node/2274843.',
      ],
    ];
  }

+6
Changes for core/tests/Drupal/Tests/Core/Asset/library_test_files/css_missing_options.libraries.yml: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
missing_options:
  css:
    component:
      # Explicit null reproduces an omitted options value without an empty mapping value.
      css/styles.css: null
      css/other.css: {}