/**
  * Returns the library definitions for a given extension.
  *
  * This also implements libraries-overrides for entire libraries that have
  * been specified by the LibraryDiscoveryParser.
  *
  * @param string $extension
  *   The name of the extension for which library definitions will be returned.
  *
  * @return array
  *   The library definitions for $extension with overrides applied.
  *
  * @throws \Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException
  */
 protected function getLibraryDefinitions($extension)
 {
     $libraries = $this->discoveryParser->buildByExtension($extension);
     foreach ($libraries as $name => $definition) {
         // Handle libraries that are marked for override or removal.
         // @see \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride()
         if (isset($definition['override'])) {
             if ($definition['override'] === FALSE) {
                 // Remove the library definition if FALSE is given.
                 unset($libraries[$name]);
             } else {
                 // Otherwise replace with existing library definition if it exists.
                 // Throw an exception if it doesn't.
                 list($replacement_extension, $replacement_name) = explode('/', $definition['override']);
                 $replacement_definition = $this->get($replacement_extension);
                 if (isset($replacement_definition[$replacement_name])) {
                     $libraries[$name] = $replacement_definition[$replacement_name];
                 } else {
                     throw new InvalidLibrariesOverrideSpecificationException(sprintf('The specified library %s does not exist.', $definition['override']));
                 }
             }
         } else {
             // If libraries are not overridden, then apply libraries-extend.
             $libraries[$name] = $this->applyLibrariesExtend($extension, $name, $definition);
         }
     }
     return $libraries;
 }
 /**
  * Tests the destruct method.
  *
  * @covers ::destruct
  */
 public function testDestruct()
 {
     $this->libraryDiscoveryParser->expects($this->once())->method('buildByExtension')->with('test')->will($this->returnValue($this->libraryData));
     $lock_key = 'library_info:Drupal\\Core\\Cache\\CacheCollector';
     $this->lock->expects($this->once())->method('acquire')->with($lock_key)->will($this->returnValue(TRUE));
     $this->cache->expects($this->exactly(2))->method('get')->with('library_info')->will($this->returnValue(FALSE));
     $this->cache->expects($this->once())->method('set')->with('library_info', array('test' => $this->libraryData), Cache::PERMANENT, array('library_info'));
     $this->lock->expects($this->once())->method('release')->with($lock_key);
     // This should get data and persist the key.
     $this->libraryDiscoveryCollector->get('test');
     $this->libraryDiscoveryCollector->destruct();
 }
 /**
  * Tests the destruct method.
  *
  * @covers ::destruct
  */
 public function testDestruct()
 {
     $this->activeTheme = $this->getMockBuilder('Drupal\\Core\\Theme\\ActiveTheme')->disableOriginalConstructor()->getMock();
     $this->themeManager->expects($this->once())->method('getActiveTheme')->willReturn($this->activeTheme);
     $this->activeTheme->expects($this->once())->method('getName')->willReturn('kitten_theme');
     $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
     $this->libraryDiscoveryParser->expects($this->once())->method('buildByExtension')->with('test')->willReturn($this->libraryData);
     $lock_key = 'library_info:kitten_theme:Drupal\\Core\\Cache\\CacheCollector';
     $this->lock->expects($this->once())->method('acquire')->with($lock_key)->will($this->returnValue(TRUE));
     $this->cache->expects($this->exactly(2))->method('get')->with('library_info:kitten_theme')->willReturn(FALSE);
     $this->cache->expects($this->once())->method('set')->with('library_info:kitten_theme', array('test' => $this->libraryData), Cache::PERMANENT, ['library_info']);
     $this->lock->expects($this->once())->method('release')->with($lock_key);
     // This should get data and persist the key.
     $this->libraryDiscoveryCollector->get('test');
     $this->libraryDiscoveryCollector->destruct();
 }
 /**
  * Tests a library with various licenses, some GPL-compatible, some not.
  *
  * @covers ::buildByExtension
  */
 public function testLibraryWithLicenses()
 {
     $this->moduleHandler->expects($this->atLeastOnce())->method('moduleExists')->with('licenses')->will($this->returnValue(TRUE));
     $path = __DIR__ . '/library_test_files';
     $path = substr($path, strlen($this->root) + 1);
     $this->libraryDiscoveryParser->setPaths('module', 'licenses', $path);
     $libraries = $this->libraryDiscoveryParser->buildByExtension('licenses');
     // For libraries without license info, the default license is applied.
     $library = $libraries['no-license-info'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $this->assertTrue(isset($library['license']));
     $default_license = array('name' => 'GNU-GPL-2.0-or-later', 'url' => 'https://www.drupal.org/licensing/faq', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $default_license);
     // GPL2-licensed libraries.
     $library = $libraries['gpl2'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'gpl2', 'url' => 'https://url-to-gpl2-license', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $expected_license);
     // MIT-licensed libraries.
     $library = $libraries['mit'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'MIT', 'url' => 'https://url-to-mit-license', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $expected_license);
     // Libraries in the Public Domain.
     $library = $libraries['public-domain'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'Public Domain', 'url' => 'https://url-to-public-domain-license', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $expected_license);
     // Apache-licensed libraries.
     $library = $libraries['apache'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'apache', 'url' => 'https://url-to-apache-license', 'gpl-compatible' => FALSE);
     $this->assertEquals($library['license'], $expected_license);
     // Copyrighted libraries.
     $library = $libraries['copyright'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => '© Some company', 'gpl-compatible' => FALSE);
     $this->assertEquals($library['license'], $expected_license);
 }
 /**
  * {@inheritdoc}
  */
 protected function resolveCacheMiss($key)
 {
     $this->storage[$key] = $this->discoveryParser->buildByExtension($key);
     $this->persist($key);
     return $this->storage[$key];
 }