diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php new file mode 100644 index 0000000..56ea60a --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php @@ -0,0 +1,132 @@ + array( + 'js' => array(), + 'css' => array(), + ), + 'test_2' => array( + 'js' => array(), + 'css' => array(), + ), + ); + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Tests \Drupal\Core\Asset\LibraryDiscoveryCollector', + 'description' => '', + 'group' => 'Asset handling', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); + $this->libraryDiscoveryParser = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscoveryParser') + ->disableOriginalConstructor() + ->getMock(); + + $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser); + } + + /** + * Tests the resolve cache miss function. + * + * @covers ::resolveCacheMiss + */ + public function testResolveCacheMiss() { + $this->libraryDiscoveryParser->expects($this->once()) + ->method('buildLibrariesByExtension') + ->with('test') + ->will($this->returnValue($this->libraryData)); + + $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector->get('test')); + $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector->get('test')); + } + + /** + * Tests the destruct method. + * + * @covers ::destruct + */ + public function testDestruct() { + $this->libraryDiscoveryParser->expects($this->once()) + ->method('buildLibrariesByExtension') + ->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' => array(TRUE))); + $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(); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTestParser.php similarity index 62% rename from core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php rename to core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTestParser.php index 52f38b5..ea80cb9 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTestParser.php @@ -7,8 +7,7 @@ namespace Drupal\Tests\Core\Asset; -use Drupal\Core\Asset\LibraryDiscovery; -use Drupal\Core\Cache\Cache; +use Drupal\Core\Asset\LibraryDiscoveryParser; use Drupal\Tests\UnitTestCase; if (!defined('DRUPAL_ROOT')) { @@ -30,18 +29,18 @@ } /** - * Tests the library discovery. + * Tests the library discovery parser. * - * @coversDefaultClass \Drupal\Core\Asset\LibraryDiscovery + * @coversDefaultClass \Drupal\Core\Asset\LibraryDiscoveryParser */ -class LibraryDiscoveryTest extends UnitTestCase { +class LibraryDiscoveryParserTest extends UnitTestCase { /** * The tested library provider. * - * @var \Drupal\Core\Asset\LibraryDiscovery|\Drupal\Tests\Core\Asset\TestLibraryDiscovery + * @var \Drupal\Core\Asset\LibraryDiscoveryParser|\Drupal\Tests\Core\Asset\TestLibraryDiscoveryParser */ - protected $libraryDiscovery; + protected $libraryDiscoveryParser; /** * The mocked cache backend. @@ -69,7 +68,7 @@ class LibraryDiscoveryTest extends UnitTestCase { */ public static function getInfo() { return array( - 'name' => 'Tests \Drupal\Core\Asset\LibraryProvider', + 'name' => 'Tests \Drupal\Core\Asset\LibraryDiscoveryParser', 'description' => '', 'group' => 'Asset handling', ); @@ -79,19 +78,16 @@ public static function getInfo() { * {@inheritdoc} */ protected function setUp() { - $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); - $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); - $this->libraryDiscovery = new TestLibraryDiscovery($this->cache, $this->moduleHandler, $this->lock); + $this->libraryDiscoveryParser = new TestLibraryDiscoveryParser($this->moduleHandler); } /** * Tests that basic functionality works for getLibraryByName. * - * @covers ::getLibraryByName() * @covers ::buildLibrariesByExtension() */ - public function testGetLibraryByNameSimple() { + public function testBuildLibrariesByExtensionSimple() { $this->moduleHandler->expects($this->atLeastOnce()) ->method('moduleExists') ->with('example_module') @@ -99,9 +95,11 @@ public function testGetLibraryByNameSimple() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'example_module', $path); + $this->libraryDiscoveryParser->setPaths('module', 'example_module', $path); + + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('example_module', 'example'); + $library = $libraries['example']; - $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example'); $this->assertCount(0, $library['js']); $this->assertCount(1, $library['css']); $this->assertCount(0, $library['dependencies']); @@ -112,33 +110,11 @@ public function testGetLibraryByNameSimple() { } /** - * Tests that basic functionality works for getLibrariesByExtension. - * - * @covers ::getLibrariesByExtension() - * @covers ::buildLibrariesByExtension() - */ - public function testGetLibrariesByExtensionSimple() { - $this->moduleHandler->expects($this->atLeastOnce()) - ->method('moduleExists') - ->with('example_module') - ->will($this->returnValue(TRUE)); - - $path = __DIR__ . '/library_test_files'; - $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'example_module', $path); - - $libraries = $this->libraryDiscovery->getLibrariesByExtension('example_module', 'example'); - $this->assertCount(1, $libraries); - $this->assertEquals($path . '/css/example.css', $libraries['example']['css'][0]['data']); - } - - /** * Tests that a theme can be used instead of a module. * - * @covers ::getLibraryByName() * @covers ::buildLibrariesByExtension() */ - public function testGetLibraryByNameWithTheme() { + public function testBuildLibrariesByExtensionWithTheme() { $this->moduleHandler->expects($this->atLeastOnce()) ->method('moduleExists') ->with('example_theme') @@ -146,9 +122,11 @@ public function testGetLibraryByNameWithTheme() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('theme', 'example_theme', $path); + $this->libraryDiscoveryParser->setPaths('theme', 'example_theme', $path); + + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('example_theme'); + $library = $libraries['example']; - $library = $this->libraryDiscovery->getLibraryByName('example_theme', 'example'); $this->assertCount(0, $library['js']); $this->assertCount(1, $library['css']); $this->assertCount(0, $library['dependencies']); @@ -158,11 +136,9 @@ public function testGetLibraryByNameWithTheme() { /** * Tests that a module with a missing library file results in FALSE. * - * @covers ::getLibraryByName() - * @covers ::getLibrariesByExtension() * @covers ::buildLibrariesByExtension() */ - public function testGetLibraryWithMissingLibraryFile() { + public function testBuildLibrariesByExtensionWithMissingLibraryFile() { $this->moduleHandler->expects($this->atLeastOnce()) ->method('moduleExists') ->with('example_module') @@ -170,10 +146,9 @@ public function testGetLibraryWithMissingLibraryFile() { $path = __DIR__ . '/library_test_files_not_existing'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'example_module', $path); + $this->libraryDiscoveryParser->setPaths('module', 'example_module', $path); - $this->assertFalse($this->libraryDiscovery->getLibraryByName('example_module', 'example')); - $this->assertFalse($this->libraryDiscovery->getLibrariesByExtension('example_module')); + $this->assertSame($this->libraryDiscoveryParser->buildLibrariesByExtension('example_module'), array()); } /** @@ -191,9 +166,9 @@ public function testInvalidLibrariesFile() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'invalid_file', $path); + $this->libraryDiscoveryParser->setPaths('module', 'invalid_file', $path); - $this->libraryDiscovery->getLibrariesByExtension('invalid_file'); + $this->libraryDiscoveryParser->buildLibrariesByExtension('invalid_file'); } /** @@ -204,7 +179,7 @@ public function testInvalidLibrariesFile() { * * @covers ::buildLibrariesByExtension() */ - public function testGetLibraryWithMissingInformation() { + public function testBuildLibrariesByExtensionWithMissingInformation() { $this->moduleHandler->expects($this->atLeastOnce()) ->method('moduleExists') ->with('example_module_missing_information') @@ -212,9 +187,9 @@ public function testGetLibraryWithMissingInformation() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'example_module_missing_information', $path); + $this->libraryDiscoveryParser->setPaths('module', 'example_module_missing_information', $path); - $this->libraryDiscovery->getLibrariesByExtension('example_module_missing_information'); + $this->libraryDiscoveryParser->buildLibrariesByExtension('example_module_missing_information'); } /** @@ -230,9 +205,11 @@ public function testExternalLibraries() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'external', $path); + $this->libraryDiscoveryParser->setPaths('module', 'external', $path); + + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('external', 'example_external'); + $library = $libraries['example_external']; - $library = $this->libraryDiscovery->getLibraryByName('external', 'example_external'); $this->assertEquals($path . '/css/example_external.css', $library['css'][0]['data']); $this->assertEquals('3.14', $library['version']); } @@ -250,9 +227,10 @@ public function testDefaultCssWeights() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'css_weights', $path); + $this->libraryDiscoveryParser->setPaths('module', 'css_weights', $path); - $library = $this->libraryDiscovery->getLibraryByName('css_weights', 'example'); + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('css_weights'); + $library = $libraries['example']; $css = $library['css']; $this->assertCount(10, $css); @@ -289,9 +267,9 @@ public function testJsWithPositiveWeight() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'js_positive_weight', $path); + $this->libraryDiscoveryParser->setPaths('module', 'js_positive_weight', $path); - $this->libraryDiscovery->getLibrariesByExtension('js_positive_weight'); + $this->libraryDiscoveryParser->buildLibrariesByExtension('js_positive_weight'); } /** @@ -307,9 +285,10 @@ public function testLibraryWithCssJsSetting() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'css_js_settings', $path); + $this->libraryDiscoveryParser->setPaths('module', 'css_js_settings', $path); - $library = $this->libraryDiscovery->getLibraryByName('css_js_settings', 'example'); + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('css_js_settings'); + $library = $libraries['example']; // Ensures that the group and type are set automatically. $this->assertEquals(-100, $library['js'][0]['group']); @@ -337,9 +316,11 @@ public function testLibraryWithDependencies() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'dependencies', $path); + $this->libraryDiscoveryParser->setPaths('module', 'dependencies', $path); + + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('dependencies'); + $library = $libraries['example']; - $library = $this->libraryDiscovery->getLibraryByName('dependencies', 'example'); $this->assertCount(2, $library['dependencies']); $this->assertEquals('external/example_external', $library['dependencies'][0]); $this->assertEquals('example_module/example', $library['dependencies'][1]); @@ -358,12 +339,14 @@ public function testLibraryWithDataTypes() { $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'data_types', $path); + $this->libraryDiscoveryParser->setPaths('module', 'data_types', $path); - $this->libraryDiscovery->setFileValidUri('public://test.css', TRUE); - $this->libraryDiscovery->setFileValidUri('public://test2.css', FALSE); + $this->libraryDiscoveryParser->setFileValidUri('public://test.css', TRUE); + $this->libraryDiscoveryParser->setFileValidUri('public://test2.css', FALSE); + + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('data_types'); + $library = $libraries['example']; - $library = $this->libraryDiscovery->getLibraryByName('data_types', 'example'); $this->assertCount(5, $library['css']); $this->assertEquals('external', $library['css'][0]['type']); $this->assertEquals('http://example.com/test.css', $library['css'][0]['data']); @@ -385,82 +368,17 @@ public function testStaticCache() { ->method('moduleExists') ->with('example_module') ->will($this->returnValue(TRUE)); - $this->cache->expects($this->once()) - ->method('get') - ->with('library:info:' . 'example_module') - ->will($this->returnValue(NULL)); - - $path = __DIR__ . '/library_test_files'; - $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'example_module', $path); - - $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example'); - $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']); - - $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example'); - $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']); - } - - /** - * Tests the external cache. - * - * @covers ::getCache() - */ - public function testExternalCache() { - // Ensure that the module handler does not need to be touched. - $this->moduleHandler->expects($this->never()) - ->method('moduleExists'); $path = __DIR__ . '/library_test_files'; $path = substr($path, strlen(DRUPAL_ROOT) + 1); + $this->libraryDiscoveryParser->setPaths('module', 'example_module', $path); - // Setup a cache entry which will be retrieved, but just once, so the static - // cache still works. - $this->cache->expects($this->once()) - ->method('get') - ->with('library:info:' . 'example_module') - ->will($this->returnValue((object) array( - 'data' => array( - 'example' => array( - 'css' => array( - array( - 'data' => $path . '/css/example.css', - ), - ), - ), - ) - ))); - - $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example'); - $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']); - - $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example'); + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('example_module'); + $library = $libraries['example']; $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']); - } - - /** - * Tests setting the external cache. - * - * @covers ::setCache() - */ - public function testSetCache() { - $this->moduleHandler->expects($this->once()) - ->method('moduleExists') - ->with('example_module') - ->will($this->returnValue(TRUE)); - - $path = __DIR__ . '/library_test_files'; - $path = substr($path, strlen(DRUPAL_ROOT) + 1); - $this->libraryDiscovery->setPaths('module', 'example_module', $path); - - $this->cache->expects($this->once()) - ->method('set') - ->with('library:info:example_module', $this->isType('array'), Cache::PERMANENT, array( - 'extension' => array(TRUE, 'example_module'), - 'library_info' => array(TRUE), - )); - $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example'); + $libraries = $this->libraryDiscoveryParser->buildLibrariesByExtension('example_module'); + $library = $libraries['example']; $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']); } @@ -469,7 +387,7 @@ public function testSetCache() { /** * Wraps the tested class to mock the external dependencies. */ -class TestLibraryDiscovery extends LibraryDiscovery { +class TestLibraryDiscoveryParser extends LibraryDiscoveryParser { protected $paths;