Commit 2f26241c authored by Gábor Hojtsy's avatar Gábor Hojtsy
Browse files

Issue #3306543 by Gábor Hojtsy, lauriii: Add warning for custom off-canvas CSS

parent 07eb96f5
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\upgrade_status;

use Drupal\Core\Extension\Extension;

/**
 * The route deprecation analyzer.
 */
final class CSSDeprecationAnalyzer {

  /**
    * Analyzes usages of deprecated CSS selectors in an extension.
   *
   * @param \Drupal\Core\Extension\Extension $extension
   *  The extension to be analyzed.
   *
   * @return \Drupal\upgrade_status\DeprecationMessage[]
   *   A list of deprecation messages.
   *
   * @throws \Exception
   */
  public function analyze(Extension $extension): array {
    $deprecations = [];
    $css_files = $this->getAllCSSFiles(DRUPAL_ROOT . '/' . $extension->getPath());
    foreach ($css_files as $css_file) {
      $content = file_get_contents($css_file);
      // Remove valid selectors for this check.
      $content = str_replace('#drupal-off-canvas:not(.drupal-off-canvas-reset)', 'removed', $content);
      $content = str_replace('#drupal-off-canvas-wrapper', 'removed', $content);
      if (strpos($content, '#drupal-off-canvas')) {
        $deprecations[] = new DeprecationMessage('The #drupal-off-canvas selector is deprecated in drupal:9.5.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3305664.', $css_file, 0);
      }
    }
    return $deprecations;
  }

  /**
   * Finds all .css files for non-test extensions under a path.
   *
   * @param string $path
   *   Base path to find all .css files in.
   *
   * @return array
   *   A list of paths to .css files found under the base path.
   */
  private function getAllCSSFiles(string $path) {
    $files = [];
    foreach(glob($path . '/*.css') as $file) {
      $files[] = $file;
    }
    foreach (glob($path . '/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
      $files = array_merge($files, $this->getAllCSSFiles($dir));
    }
    return $files;
  }

}
+18 −1
Original line number Diff line number Diff line
@@ -112,6 +112,13 @@ final class DeprecationAnalyzer {
   */
  protected $extensionMetadataDeprecationAnalyzer;

  /**
   * The CSS deprecation analyzer.
   *
   * @var \Drupal\upgrade_status\CSSDeprecationAnalyzer
   */
  protected $CSSDeprecationAnalyzer;

  /**
   * The time service.
   *
@@ -154,6 +161,8 @@ final class DeprecationAnalyzer {
   *   The route deprecation analyzer.
   * @param \Drupal\upgrade_status\ExtensionMetadataDeprecationAnalyzer $extension_metadata_analyzer
   *   The extension metadata analyzer.
   * @param \Drupal\upgrade_status\CSSDeprecationAnalyzer $css_deprecation_analyzer
   *   The CSS deprecation analyzer.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
@@ -167,6 +176,7 @@ final class DeprecationAnalyzer {
    ThemeFunctionDeprecationAnalyzer $theme_function_deprecation_analyzer,
    RouteDeprecationAnalyzer $route_deprecation_analyzer,
    ExtensionMetadataDeprecationAnalyzer $extension_metadata_analyzer,
    CSSDeprecationAnalyzer $css_deprecation_analyzer,
    TimeInterface $time
  ) {
    $this->scanResultStorage = $key_value_factory->get('upgrade_status_scan_results');
@@ -178,6 +188,7 @@ final class DeprecationAnalyzer {
    $this->themeFunctionDeprecationAnalyzer = $theme_function_deprecation_analyzer;
    $this->routeDeprecationAnalyzer = $route_deprecation_analyzer;
    $this->extensionMetadataDeprecationAnalyzer = $extension_metadata_analyzer;
    $this->CSSDeprecationAnalyzer = $css_deprecation_analyzer;
    $this->time = $time;
  }

@@ -397,9 +408,15 @@ final class DeprecationAnalyzer {
      $this->twigDeprecationAnalyzer->analyze($extension),
      $this->libraryDeprecationAnalyzer->analyze($extension),
      $this->themeFunctionDeprecationAnalyzer->analyze($extension),
      (projectCollector::getDrupalCoreMajorVersion() > 8) ? $this->routeDeprecationAnalyzer->analyze($extension) : [],
      $metadataDeprecations,
    );
    if (projectCollector::getDrupalCoreMajorVersion() > 8) {
      $more_deprecations = array_merge($more_deprecations,
        $this->routeDeprecationAnalyzer->analyze($extension),
        $this->CSSDeprecationAnalyzer->analyze($extension)
      );
    }

    foreach ($more_deprecations as $one_deprecation) {
      $result['data']['files'][$one_deprecation->getFile()]['messages'][] = [
        'message' => $one_deprecation->getMessage(),
+11 −0
Original line number Diff line number Diff line
#drupal-off-canvas:not(.drupal-off-canvas-reset) {
  /* this will not be detected */
}

#drupal-off-canvas-wrapper {
  /* this will also not be detected */
}

#drupal-off-canvas {
  /* THIS will be detected however */
}
+12 −7
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ class UpgradeStatusAnalyzeTest extends UpgradeStatusTestBase {

    $report = $key_value->get('upgrade_status_test_error');
    $this->assertNotEmpty($report);
    $this->assertEquals($this->getDrupalCoreMajorVersion() < 9 ? 5 : 6, $report['data']['totals']['file_errors']);
    $this->assertCount($this->getDrupalCoreMajorVersion() < 9 ? 5 : 6, $report['data']['files']);
    $this->assertEquals($this->getDrupalCoreMajorVersion() < 9 ? 5 : 7, $report['data']['totals']['file_errors']);
    $this->assertCount($this->getDrupalCoreMajorVersion() < 9 ? 5 : 7, $report['data']['files']);
    $file = reset($report['data']['files']);
    $message = $file['messages'][0];
    $this->assertEquals('fatal.php', basename(key($report['data']['files'])));
@@ -58,18 +58,23 @@ class UpgradeStatusAnalyzeTest extends UpgradeStatusTestBase {
    $message = $file['messages'][0];
    $this->assertEquals("Configuration entity must define a `config_export` key. See https://www.drupal.org/node/2481909", $message['message']);
    $this->assertEquals(15, $message['line']);
    $file = next($report['data']['files']);
    $this->assertEquals('upgrade_status_test_error.info.yml', basename(key($report['data']['files'])));
    $message = $file['messages'][0];
    $this->assertEquals("Add core_version_requirement: ^8 || ^9 to designate that the extension is compatible with Drupal 9. See https://drupal.org/node/3070687.", $message['message']);
    $this->assertEquals(0, $message['line']);
    if ($this->getDrupalCoreMajorVersion() > 8) {
      $file = next($report['data']['files']);
      $this->assertEquals('upgrade_status_test_error.routing.yml', basename(key($report['data']['files'])));
      $message = $file['messages'][0];
      $this->assertEquals("The _access_node_revision routing requirement is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use _entity_access instead. See https://www.drupal.org/node/3161210.", $message['message']);
      $this->assertEquals(0, $message['line']);
    }
      $file = next($report['data']['files']);
    $this->assertEquals('upgrade_status_test_error.info.yml', basename(key($report['data']['files'])));
      $this->assertEquals('upgrade_status_test_error.css', basename(key($report['data']['files'])));
      $message = $file['messages'][0];
    $this->assertEquals("Add core_version_requirement: ^8 || ^9 to designate that the extension is compatible with Drupal 9. See https://drupal.org/node/3070687.", $message['message']);
      $this->assertEquals("The #drupal-off-canvas selector is deprecated in drupal:9.5.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3305664.", $message['message']);
      $this->assertEquals(0, $message['line']);
    }

    // The Drupal 9 compatible test modules are not Drupal 10 compatible.
    $test_9_compatibles = [
+5 −5
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ class UpgradeStatusUiTest extends UpgradeStatusTestBase {
    $assert_session->buttonExists('Export selected as HTML');

    // Error and no-error test module results should show.
    $this->assertSame($this->getDrupalCoreMajorVersion() < 9 ? '5 problems' : '6 problems', strip_tags($page->find('css', 'tr.project-upgrade_status_test_error td.scan-result')->getHtml()));
    $this->assertSame($this->getDrupalCoreMajorVersion() < 9 ? '5 problems' : '7 problems', strip_tags($page->find('css', 'tr.project-upgrade_status_test_error td.scan-result')->getHtml()));
    $this->assertSame($this->getDrupalCoreMajorVersion() < 9 ? 'No problems found' : '1 problem', strip_tags($page->find('css', 'tr.project-upgrade_status_test_9_compatible td.scan-result')->getHtml()));
    $this->assertSame('No problems found', strip_tags($page->find('css', 'tr.project-upgrade_status_test_10_compatible td.scan-result')->getHtml()));

@@ -69,7 +69,7 @@ class UpgradeStatusUiTest extends UpgradeStatusTestBase {
    // Check UI of results for the custom project.
    $this->drupalGet('/admin/reports/upgrade-status/project/upgrade_status_test_error');
    $this->assertText('Upgrade status test error');
    $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '4' . ' warnings found.');
    $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '5' . ' warnings found.');
    $this->assertText('Syntax error, unexpected T_STRING on line 3');

    // Go forward to the export page and assert that still contains the results
@@ -79,7 +79,7 @@ class UpgradeStatusUiTest extends UpgradeStatusTestBase {
    $this->assertText('Upgrade status test error');
    $this->assertText('Custom projects');
    $this->assertNoText('Contributed projects');
    $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '4' . ' warnings found.');
    $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '5' . ' warnings found.');
    $this->assertText('Syntax error, unexpected T_STRING on line 3');

    // Go back to the results page and click over to exporting in single ASCII.
@@ -88,7 +88,7 @@ class UpgradeStatusUiTest extends UpgradeStatusTestBase {
    $this->assertText('Upgrade status test error');
    $this->assertText('CUSTOM PROJECTS');
    $this->assertNoText('CONTRIBUTED PROJECTS');
    $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '4' . ' warnings found.');
    $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '5' . ' warnings found.');
    $this->assertText('Syntax error, unexpected T_STRING on line 3');

    // Run partial export of multiple projects.
@@ -110,7 +110,7 @@ class UpgradeStatusUiTest extends UpgradeStatusTestBase {
      $this->assertText('Upgrade status test error');
      $this->assertNoText('Upgrade status test root module');
      $this->assertNoText('Upgrade status test contrib 9 compatbile');
      $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '4' . ' warnings found.');
      $this->assertText('2 errors found. ' . $this->getDrupalCoreMajorVersion() < 9 ? '3' : '5' . ' warnings found.');
      $this->assertText('Syntax error, unexpected T_STRING on line 3');
    }
  }
Loading