diff --git a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php index 07e365b..57e3d60 100644 --- a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php @@ -34,7 +34,7 @@ public function getOwnerName($uri = NULL) { $uri_parts = explode('://', $uri, 2); if (count($uri_parts) === 1) { // The delimiter ('://') was not found in $uri, malformed $uri passed. - throw new \InvalidArgumentException('Malformed $uri parameter passed: %s', $uri); + throw new \InvalidArgumentException(sprintf('Malformed uri parameter passed: %s', $uri)); } else { list($scheme, $target) = $uri_parts; diff --git a/core/modules/system/src/Tests/File/SystemStreamUnitTest.php b/core/modules/system/src/Tests/File/SystemStreamUnitTest.php index bb27b4c..691f309 100644 --- a/core/modules/system/src/Tests/File/SystemStreamUnitTest.php +++ b/core/modules/system/src/Tests/File/SystemStreamUnitTest.php @@ -41,16 +41,11 @@ public static function getInfo() { public function setUp() { parent::setUp(); drupal_static_reset('file_get_stream_wrappers'); + // Add default profile for the purposes of this test new Settings(Settings::getAll() + array( 'install_profile' => 'standard' )); - - // Drupal::container() doesn't contain theme state info which is already - // saved in KernelTestBase::container. Need to transfer this across. - \Drupal::state()->set('system.theme.data', $this->container->get('state')->get('system.theme.data')); - \Drupal::state()->set('system.theme.files', $this->container->get('state')->get('system.theme.files')); - \Drupal::service('theme_handler')->enable(array('stark', 'bartik', 'seven')); } /** @@ -61,6 +56,37 @@ public function tearDown() { } /** + * Test Invalid stream uri. + */ + public function testInvalidStreamUriException() { + $bad_uris = array( + 'invalid/uri', + 'invalid_uri', + 'module/invalid/uri', + 'module/invalid_uri', + 'module:invalid_uri', + 'module::/invalid/uri', + 'module::/invalid_uri', + 'module//:invalid/uri', + 'module//invalid_uri', + 'module//invalid/uri', + ); + + /** @var \Drupal\Core\StreamWrapper\ModuleStream $instance */ + $instance = file_stream_wrapper_get_instance_by_scheme('module'); + + foreach ($bad_uris as $bad_uri) { + try { + $instance->getOwnerName($bad_uri); + $this->fail(format_string('Invalid uri %uri not detected.', array('%uri' => $bad_uri))); + } + catch (\InvalidArgumentException $e) { + $this->pass(format_string('Throw exception on invalid uri %uri supplied.', array('%uri' => $bad_uri))); + } + } + } + + /** * Test the Module stream wrapper functions. */ public function testModuleStream() { @@ -161,21 +187,24 @@ public function testProfileStream() { * Test the Theme stream wrapper functions. */ public function testThemeStream() { - // Disable Bartik theme. - $system_theme_disabled = \Drupal::config('system.theme.disabled'); - $this->assertNotIdentical($system_theme_disabled->get('bartik'), 0, format_string('%bartik theme was enabled.', array('%bartik' => 'Bartik'))); - $system_theme_disabled->set('bartik', 0)->save(); - $this->assertIdentical($system_theme_disabled->get('bartik'), 0, format_string('Now disable %bartik theme.', array('%bartik' => 'Bartik'))); + /** @var \Drupal\Core\Extension\ThemeHandler $theme_handler */ + $theme_handler = \Drupal::service('theme_handler'); + + // Enable Stark, Bartik and Seven themes. + $theme_handler->enable(array('bartik', 'seven', 'stark')); // Set admin theme to Seven. $system_theme = \Drupal::config('system.theme'); $this->assertNull($system_theme->get('admin'), 'No admin theme was set.'); $system_theme->set('admin', 'seven')->save(); - $this->assertEqual($system_theme->get('admin'), 'seven', format_string('Now make %seven the admin theme.', array('%seven' => 'Seven'))); + $this->assertEqual($system_theme->get('admin'), 'seven', format_string('Make %seven the admin theme.', array('%seven' => 'Seven'))); // Set default theme to Bartik. - $system_theme->set('default', 'bartik')->save(); - $this->assertEqual($system_theme->get('default'), 'bartik', format_string('Now make %bartik the default theme.', array('%bartik' => 'Bartik'))); + $theme_handler->setDefault('bartik'); + $this->assertEqual($theme_handler->getDefault(), 'bartik', format_string('Make %bartik the default theme.', array('%bartik' => 'Bartik'))); + + // Disable Stark theme. + $theme_handler->disable(array('stark')); $uri1 = 'theme://seven'; $uri2 = 'theme://seven/style.css'; @@ -187,6 +216,7 @@ public function testThemeStream() { $uri8 = 'theme://default/bartik.info.yml'; $uri9 = 'theme://admin'; $uri10 = 'theme://admin/fake.info.yml'; + $uri11 = 'theme://stark/stark.info.yml'; /** @var \Drupal\Core\StreamWrapper\ThemeStream $instance */ $instance = file_stream_wrapper_get_instance_by_scheme('theme'); @@ -202,6 +232,7 @@ public function testThemeStream() { $this->assertEqual($instance->getOwnerName($uri8), 'bartik', format_string('Lookup real name of %default for a resource.', array('%default' => 'theme://default'))); $this->assertEqual($instance->getOwnerName($uri9), 'seven', format_string('Lookup real name of %admin for a partial URI.', array('%admin' => 'theme://admin'))); $this->assertEqual($instance->getOwnerName($uri10), 'seven', format_string('Lookup real name of %admin even for a non-existing resource.', array('%admin' => 'theme://admin'))); + $this->assertFalse($instance->getOwnerName($uri11), "Fail returning a disabled theme's name."); // getTarget() $this->assertEqual($instance->getTarget($uri1), '', 'Return empty target for a partial URI giving only the theme.'); @@ -214,6 +245,7 @@ public function testThemeStream() { $this->assertEqual($instance->getTarget($uri8), 'bartik.info.yml', format_string("Extract target from a resource located in a subdirectory of %default.", array('%default' => 'theme://default')) . $instance->getTarget($uri8)); $this->assertEqual($instance->getTarget($uri9), '', format_string('Return empty target for a partial URI giving only %admin.', array('%admin' => 'theme://admin'))); $this->assertFalse($instance->getTarget($uri10), format_string("Fail returning target for a non-existing resource within %admin.", array('%admin' => 'theme://admin'))); + $this->assertFalse($instance->getTarget($uri11), "Fail returning a target for a disabled theme."); // getDirectoryPath() $this->assertEqual($instance->getDirectoryPath($uri1), 'core/themes/seven', "Lookup theme's directory path for a partial URI."); @@ -226,6 +258,7 @@ public function testThemeStream() { $this->assertEqual($instance->getDirectoryPath($uri8), 'core/themes/bartik', format_string('Lookup real directory path of %default for a resource.', array('%default' => 'theme://default'))); $this->assertEqual($instance->getDirectoryPath($uri9), 'core/themes/seven', format_string('Lookup real directory path of %admin for a partial URI.', array('%admin' => 'theme://admin'))); $this->assertEqual($instance->getDirectoryPath($uri10), 'core/themes/seven', format_string('Lookup real directory path of %admin for a resource.', array('%admin' => 'theme://admin'))); + $this->assertFalse($instance->getDirectoryPath($uri11), "Fail returning a disabled theme's directory path."); // getExternalUrl() $base_url = \Drupal::request()->getUri(); @@ -239,6 +272,7 @@ public function testThemeStream() { $this->assertEqual($instance->getExternalUrl($uri8), $base_url . 'core/themes/bartik/bartik.info.yml', format_string('Lookup external url of %default for a resource.', array('%default' => 'theme://default'))); $this->assertEqual($instance->getExternalUrl($uri9), $base_url . 'core/themes/seven', format_string('Lookup external url of %admin for a partial URI.', array('%admin' => 'theme://admin'))); $this->assertEqual($instance->getExternalUrl($uri10), $base_url . 'core/themes/seven/fake.info.yml', format_string('Lookup external url of %admin for a resource.', array('%admin' => 'theme://admin'))); + $this->assertFalse($instance->getExternalUrl($uri11), "Fail returning a disabled theme's external url."); } }