ResolvedLibraryDefinitionsFilesMatchTest.php in Zircon Profile 8        
                          
                  
                        
  
  
  
File
  core/modules/system/src/Tests/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php
  
    View source  
  <?php
namespace Drupal\system\Tests\Asset;
use Drupal\simpletest\KernelTestBase;
class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase {
  
  protected $themeHandler;
  
  protected $themeInitialization;
  
  protected $themeManager;
  
  protected $libraryDiscovery;
  
  protected $allModules;
  
  protected $allThemes = [
    'bartik',
    'classy',
    'seven',
    'stable',
    'stark',
  ];
  
  protected $librariesToSkip = [
    
    'locale/translations',
  ];
  
  protected $pathsChecked;
  
  public function setUp() {
    parent::setUp();
    $this->themeHandler = $this->container
      ->get('theme_handler');
    $this->themeInitialization = $this->container
      ->get('theme.initialization');
    $this->themeManager = $this->container
      ->get('theme.manager');
    $this->libraryDiscovery = $this->container
      ->get('library.discovery');
    
    sort($this->allThemes);
    $this->container
      ->get('theme_installer')
      ->install($this->allThemes);
    
    $all_modules = system_rebuild_module_data();
    $all_modules = array_filter($all_modules, function ($module) {
      
      if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
        return FALSE;
      }
      return TRUE;
    });
    $this->allModules = array_keys($all_modules);
    sort($this->allModules);
    $this
      ->enableModules($this->allModules);
  }
  
  public function testCoreLibraryCompleteness() {
    
    $this
      ->verifyLibraryFilesExist($this
      ->getAllLibraries());
    
    foreach ($this->allThemes as $theme) {
      $this->themeManager
        ->setActiveTheme($this->themeInitialization
        ->getActiveThemeByName($theme));
      $this->libraryDiscovery
        ->clearCachedDefinitions();
      $this
        ->verifyLibraryFilesExist($this
        ->getAllLibraries());
    }
  }
  
  protected function verifyLibraryFilesExist($library_definitions) {
    $root = \Drupal::root();
    foreach ($library_definitions as $extension => $libraries) {
      foreach ($libraries as $library_name => $library) {
        if (in_array("{$extension}/{$library_name}", $this->librariesToSkip)) {
          continue;
        }
        
        foreach ([
          'css',
          'js',
        ] as $asset_type) {
          foreach ($library[$asset_type] as $asset) {
            $file = $asset['data'];
            $path = $root . '/' . $file;
            
            if (!isset($this->pathsChecked[$path])) {
              $this
                ->assertTrue(is_file($path), "{$file} file referenced from the {$extension}/{$library_name} library exists.");
              $this->pathsChecked[$path] = TRUE;
            }
          }
        }
      }
    }
  }
  
  protected function getAllLibraries() {
    $modules = \Drupal::moduleHandler()
      ->getModuleList();
    $extensions = $modules;
    $module_list = array_keys($modules);
    sort($module_list);
    $this
      ->assertEqual($this->allModules, $module_list, 'All core modules are installed.');
    $themes = $this->themeHandler
      ->listInfo();
    $extensions += $themes;
    $theme_list = array_keys($themes);
    sort($theme_list);
    $this
      ->assertEqual($this->allThemes, $theme_list, 'All core themes are installed.');
    $libraries['core'] = $this->libraryDiscovery
      ->getLibrariesByExtension('core');
    $root = \Drupal::root();
    foreach ($extensions as $extension_name => $extension) {
      $library_file = $extension
        ->getPath() . '/' . $extension_name . '.libraries.yml';
      if (is_file($root . '/' . $library_file)) {
        $libraries[$extension_name] = $this->libraryDiscovery
          ->getLibrariesByExtension($extension_name);
      }
    }
    return $libraries;
  }
}