diff --git a/core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php b/core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php index f58b0ff..c0ff68c 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php +++ b/core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php @@ -63,7 +63,7 @@ protected function getTarget($uri = NULL) { public function getExternalUrl() { $dir = $this->getDirectoryPath(); if (empty($dir)) { - throw new \InvalidArgumentException("Extension directory for {$this->uri} does not exist."); + throw new \RuntimeException("Extension directory for {$this->uri} does not exist."); } $path = rtrim(base_path() . $dir . '/' . $this->getTarget(), '/'); return $this->getRequest()->getUriForPath($path); diff --git a/core/lib/Drupal/Core/StreamWrapper/LibraryStream.php b/core/lib/Drupal/Core/StreamWrapper/LibraryStream.php index a529535..ee6294d 100644 --- a/core/lib/Drupal/Core/StreamWrapper/LibraryStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/LibraryStream.php @@ -24,7 +24,7 @@ protected function getOwnerName() { $library_discovery = new LibraryDiscovery($this->getDrupalRoot()); $files = $library_discovery->scan($name); if (!isset($files[$name])) { - throw new \InvalidArgumentException("Library $name does not exist"); + throw new \RuntimeException("Library $name does not exist"); } return $name; diff --git a/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php b/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php index 75230d9..d88a3c0 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php @@ -21,7 +21,7 @@ protected function getOwnerName() { $name = parent::getOwnerName(); if (!$this->getModuleHandler()->moduleExists($name)) { // The module does not exist or is not installed. - throw new \InvalidArgumentException("Module $name does not exist or is not installed"); + throw new \RuntimeException("Module $name does not exist or is not installed"); } return $name; } diff --git a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php index 00cfa50..baf65ac 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php @@ -21,7 +21,7 @@ protected function getOwnerName() { $name = parent::getOwnerName(); if (!$this->getThemeHandler()->themeExists($name)) { // The theme does not exist or is not installed. - throw new \InvalidArgumentException("Theme $name does not exist or is not installed"); + throw new \RuntimeException("Theme $name does not exist or is not installed"); } return $name; } diff --git a/core/modules/system/tests/src/Kernel/File/ExtensionStreamTest.php b/core/modules/system/tests/src/Kernel/File/ExtensionStreamTest.php index bbba29c..27d5a41 100644 --- a/core/modules/system/tests/src/Kernel/File/ExtensionStreamTest.php +++ b/core/modules/system/tests/src/Kernel/File/ExtensionStreamTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Site\Settings; use Drupal\KernelTests\KernelTestBase; +use Symfony\Component\Filesystem\Filesystem; /** * Tests system stream wrapper functions. @@ -34,6 +35,13 @@ class ExtensionStreamTest extends KernelTestBase { public static $modules = ['system']; /** + * The path to the stub library for testing the library wrapper. + * + * @var string + */ + protected $stubLibraryPath; + + /** * {@inheritdoc} */ public function setUp() { @@ -45,7 +53,7 @@ public function setUp() { /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */ $stream_wrapper_manager = $this->container->get('stream_wrapper_manager'); // Get stream wrapper instances. - foreach (['module', 'theme', 'profile'] as $scheme) { + foreach (['module', 'theme', 'profile', 'library'] as $scheme) { $this->streamWrappers[$scheme] = $stream_wrapper_manager->getViaScheme($scheme); } @@ -65,6 +73,21 @@ public function setUp() { $theme_installer = $this->container->get('theme_installer'); // Install Bartik and Seven themes. $theme_installer->install(['bartik', 'seven']); + + // Create a stub library for testing. + $this->stubLibraryPath = \Drupal::service('site.path') . '/libraries/extension-stream-test'; + $f = new Filesystem(); + $f->mkdir([$this->stubLibraryPath, $this->stubLibraryPath . '/subdirectory']); + $f->touch([$this->stubLibraryPath . '/empty', $this->stubLibraryPath . '/subdirectory/empty']); + } + + /** + * {@inheritdoc} + */ + protected function tearDown() { + parent::tearDown(); + $f = new Filesystem(); + $f->remove($this->stubLibraryPath); } /** @@ -112,11 +135,11 @@ public function providerInvalidUris() { * * @param string $uri * The uri to be tested. - * @param string|\InvalidArgumentException $dirname + * @param string|\RuntimeException|\InvalidArgumentException $dirname * The expectation for dirname() method. - * @param string|\InvalidArgumentException $realpath + * @param string|\RuntimeException|\InvalidArgumentException $realpath * The expectation for realpath() method. - * @param string|\InvalidArgumentException $getExternalUrl + * @param string|\RuntimeException|\InvalidArgumentException $getExternalUrl * The expectation for getExternalUrl() method. * * @dataProvider providerStreamWrapperMethods @@ -126,7 +149,7 @@ public function testStreamWrapperMethods($uri, $dirname, $realpath, $getExternal $realpath = is_string($realpath) ? DRUPAL_ROOT . $realpath : $realpath; // Prefix getExternalUrl() expected value with base url. $getExternalUrl = is_string($getExternalUrl) ? "{$this->baseUrl}$getExternalUrl" : $getExternalUrl; - $case = compact($dirname, $realpath, $getExternalUrl); + $case = compact('dirname', 'realpath', 'getExternalUrl'); foreach ($case as $method => $expected) { list($scheme, ) = explode('://', $uri); @@ -142,6 +165,17 @@ public function testStreamWrapperMethods($uri, $dirname, $realpath, $getExternal $this->assertSame($expected->getMessage(), $e->getMessage(), $message); } } + else if ($expected instanceof \RuntimeException) { + /** @var \RuntimeException $expected */ + $message = sprintf('Exception thrown: \RuntimeException("%s").', $expected->getMessage()); + try { + $this->streamWrappers[$scheme]->$method(); + $this->fail($message); + } + catch (\RuntimeException $e) { + $this->assertSame($expected->getMessage(), $e->getMessage(), $message); + } + } elseif (is_string($expected)) { $this->assertSame($expected, $this->streamWrappers[$scheme]->$method()); } @@ -196,15 +230,15 @@ public function providerStreamWrapperMethods() { ], [ 'module://ckeditor/ckeditor.info.yml', - new \InvalidArgumentException('Module ckeditor does not exist or is not installed'), - new \InvalidArgumentException('Module ckeditor does not exist or is not installed'), - new \InvalidArgumentException('Module ckeditor does not exist or is not installed'), + new \RuntimeException('Module ckeditor does not exist or is not installed'), + new \RuntimeException('Module ckeditor does not exist or is not installed'), + new \RuntimeException('Module ckeditor does not exist or is not installed'), ], [ 'module://foo_bar/foo.bar.js', - new \InvalidArgumentException('Module foo_bar does not exist or is not installed'), - new \InvalidArgumentException('Module foo_bar does not exist or is not installed'), - new \InvalidArgumentException('Module foo_bar does not exist or is not installed'), + new \RuntimeException('Module foo_bar does not exist or is not installed'), + new \RuntimeException('Module foo_bar does not exist or is not installed'), + new \RuntimeException('Module foo_bar does not exist or is not installed'), ], // Cases for theme:// stream wrapper. [ @@ -227,15 +261,15 @@ public function providerStreamWrapperMethods() { ], [ 'theme://fifteen/screenshot.png', - new \InvalidArgumentException('Theme fifteen does not exist or is not installed'), - new \InvalidArgumentException('Theme fifteen does not exist or is not installed'), - new \InvalidArgumentException('Theme fifteen does not exist or is not installed'), + new \RuntimeException('Theme fifteen does not exist or is not installed'), + new \RuntimeException('Theme fifteen does not exist or is not installed'), + new \RuntimeException('Theme fifteen does not exist or is not installed'), ], [ 'theme://stark/stark.info.yml', - new \InvalidArgumentException('Theme stark does not exist or is not installed'), - new \InvalidArgumentException('Theme stark does not exist or is not installed'), - new \InvalidArgumentException('Theme stark does not exist or is not installed'), + new \RuntimeException('Theme stark does not exist or is not installed'), + new \RuntimeException('Theme stark does not exist or is not installed'), + new \RuntimeException('Theme stark does not exist or is not installed'), ], // Cases for profile:// stream wrapper. [ @@ -262,6 +296,37 @@ public function providerStreamWrapperMethods() { '/core/profiles/minimal/minimal.info.yml', 'core/profiles/minimal/minimal.info.yml', ], + // Cases for library:// stream wrapper. + [ + 'library://extension-stream-test', + 'library://extension-stream-test', + '/libraries/extension-stream-test', + 'libraries/extension-stream-test', + ], + [ + 'library://extension-stream-test/empty', + 'library://extension-stream-test', + '/libraries/extension-stream-test/empty', + 'libraries/extension-stream-test/empty', + ], + [ + 'library://extension-stream-test/subdirectory/empty', + 'library://extension-stream-test/subdirectory', + '/libraries/extension-stream-test/subdirectory/empty', + 'libraries/extension-stream-test/subdirectory/empty', + ], + [ + 'library://does-not-exist', + new \RuntimeException('Library does-not-exist does not exist'), + new \RuntimeException('Library does-not-exist does not exist'), + new \RuntimeException('Library does-not-exist does not exist'), + ], + [ + 'library://does-not-exist/does-not-exist.js', + new \RuntimeException('Library does-not-exist does not exist'), + new \RuntimeException('Library does-not-exist does not exist'), + new \RuntimeException('Library does-not-exist does not exist'), + ], ]; }