You are here

public function ThemesNotUsingClassyTemplatesTest::testThemesTemplatesNotClassy in Drupal 8

Ensures that themes are not inheriting templates from Classy.

@dataProvider providerTestThemesTemplatesNotClassy

Parameters

string $theme: The theme to test.

string[] $templates_to_skip: Templates that will not be tested.

File

core/tests/Drupal/KernelTests/Core/Theme/ThemesNotUsingClassyTemplatesTest.php, line 121

Class

ThemesNotUsingClassyTemplatesTest
Tests that themes do not depend on Classy templates.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testThemesTemplatesNotClassy($theme, array $templates_to_skip) {

  // Get every template available to the theme being tested.
  $theme_registry = new Registry($this->root, \Drupal::cache(), \Drupal::lock(), \Drupal::moduleHandler(), $this->themeHandler, \Drupal::service('theme.initialization'), $theme);
  $theme_registry
    ->setThemeManager(\Drupal::theme());
  $theme_registry_full = $theme_registry
    ->get();

  // Add views-form-views-form to the skipped templates array. It is
  // registered via views_theme() in views.module, but does not represent an
  // actual template.
  $templates_to_skip[] = 'views-form-views-form';

  // Loop through every template available to the current theme, confirm it
  // does not come from Classy, does not attach Classy libraries, and does not
  // extend or include Classy templates.
  foreach ($theme_registry_full as $info) {
    if (isset($info['template'])) {
      $template_name = $info['template'];
      if (in_array($template_name, $templates_to_skip) || in_array($template_name, $this->templatesSkippableBecauseIdenticalToStable)) {
        continue;
      }
      $template_contents = file_get_contents("{$this->root}/{$info['path']}/{$template_name}.html.twig");

      // Confirm template does not come from Classy.
      $this
        ->assertFalse($info['theme path'] === 'core/themes/classy', "{$theme} is inheriting {$template_name} from Classy.");

      // Confirm template does not include or extend Classy templates.
      preg_match_all('/(extends|include)\\s+(\'|")@classy/', $template_contents, $classy_extend_include_matches);
      $this
        ->assertEmpty($classy_extend_include_matches[0], "The template: '{$template_name}' in the theme: '{$theme}' includes or extends a Classy template.");

      // Confirm template does not attach a Classy library.
      preg_match_all('/attach_library\\((\'|")classy\\/.+(\'|")\\)/', $template_contents, $classy_extend_library_matches);
      $this
        ->assertEmpty($classy_extend_library_matches[0], "The template: '{$template_name}' in the theme: '{$theme}' attaches a Classy library.");
    }
  }
}