diff --git a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php index 289583a..07e365b 100644 --- a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php @@ -84,6 +84,6 @@ public function getExternalUrl($uri = NULL) { $target = $this->extractTarget($uri); $path = $target != '' ? '/' . UrlHelper::encodePath(str_replace('\\', '/', $target)) : ''; - return \Drupal::request()->getBaseUrl() . '/' . $dir . $path; + return \Drupal::request()->getUri() . $dir . $path; } } diff --git a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php index 29ac6b9..e59efd0 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php @@ -24,19 +24,20 @@ class ThemeStream extends SystemStream { * {@inheritdoc} */ public function getOwnerName($uri = NULL) { - global $theme_key; $name = parent::getOwnerName($uri); switch ($name) { case 'current': - return $theme_key; + $name = $this->getActiveTheme(); + break; case 'default': - return \Drupal::config('system.theme')->get('default'); + $name = \Drupal::config('system.theme')->get('default'); + break; case 'admin': - return \Drupal::config('system.theme')->get('admin'); - default: - // Return name only for enabled and admin themes. - return \Drupal::service('access_check.theme')->checkAccess($name) ? $name : FALSE; + $name = \Drupal::config('system.theme')->get('admin'); + break; } + // Return name only for enabled themes. + return array_key_exists($name, \Drupal::service('theme_handler')->listInfo()) ? $name : FALSE; } /** @@ -52,4 +53,10 @@ public function getDirectoryPath($uri = NULL) { return drupal_get_path('theme', $this->getOwnerName($uri)); } + /** + * Gets the currently active theme. + */ + protected function getActiveTheme() { + return \Drupal::service('theme.negotiator')->determineActiveTheme(\Drupal::service('current_route_match')); + } } diff --git a/core/modules/simpletest/src/KernelTestBase.php b/core/modules/simpletest/src/KernelTestBase.php index 616d064..f60ab48 100644 --- a/core/modules/simpletest/src/KernelTestBase.php +++ b/core/modules/simpletest/src/KernelTestBase.php @@ -61,6 +61,7 @@ private $moduleFiles; private $themeFiles; + private $themeData; /** * The configuration directories for this test run. @@ -101,6 +102,7 @@ protected function beforePrepareEnvironment() { if (!isset($this->moduleFiles)) { $this->moduleFiles = \Drupal::state()->get('system.module.files') ?: array(); $this->themeFiles = \Drupal::state()->get('system.theme.files') ?: array(); + $this->themeData = \Drupal::state()->get('system.theme.data') ?: array(); } } @@ -170,6 +172,7 @@ protected function setUp() { $this->container->get('state')->set('system.module.files', $this->moduleFiles); $this->container->get('state')->set('system.theme.files', $this->themeFiles); + $this->container->get('state')->set('system.theme.data', $this->themeData); // Create a minimal core.extension configuration object so that the list of // enabled modules can be maintained allowing diff --git a/core/modules/system/src/Tests/File/SystemStreamUnitTest.php b/core/modules/system/src/Tests/File/SystemStreamUnitTest.php index 45cd6fa..062a64b 100644 --- a/core/modules/system/src/Tests/File/SystemStreamUnitTest.php +++ b/core/modules/system/src/Tests/File/SystemStreamUnitTest.php @@ -25,20 +25,57 @@ class SystemStreamUnitTest extends KernelTestBase { public static $modules = array('system', 'file', 'file_test'); /** + * Original test environment settings. + * + * @var array + */ + protected $originalSettings; + + /** + /** + * Original theme files list. + * + * @var array + */ + protected $originalThemeFiles; + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'System Stream wrappers', + 'description' => 'Unit tests system stream wrapper functions.', + 'group' => 'File API', + ); + } + + /** * {@inheritdoc} */ public function setUp() { parent::setUp(); drupal_static_reset('file_get_stream_wrappers'); - new Settings(Settings::getAll() + array( - 'install_profile' => 'standard', - )); + // Add default profile for the purposes of this test + $this->originalSettings = Settings::getAll(); + new Settings(array_merge(array('install_profile' => 'standard'), $this->originalSettings)); + + // Drupal::container() doesn't contain theme state info which is already + // saved in KernelTestBase::container. Need to transfer this across. + $this->originalThemeData = \Drupal::state()->get('system.theme.data'); + \Drupal::state()->set('system.theme.data', $this->container->get('state')->get('system.theme.data')); +// $this->originalThemeFiles = \Drupal::state()->get('system.theme.files'); +// \Drupal::state()->set('system.theme.files', $this->container->get('state')->get('system.theme.files')); } /** * {@inheritdoc} */ public function tearDown() { + // Restore changes made in setUp(). + new Settings($this->originalSettings); + \Drupal::state()->set('system.theme.data', $this->originalThemeData); + \Drupal::state()->set('system.theme.files', $this->originalThemeFiles); parent::tearDown(); } @@ -81,13 +118,13 @@ public function testModuleStream() { $this->assertFalse($instance->getDirectoryPath($uri6), "Fail returning a non-existing module's directory path."); // getExternalUrl() - $base_url = \Drupal::request()->getBaseUrl() . '/'; - $this->assertEqual($instance->getExternalUrl($uri1), $base_url . 'core/modules/system', "Lookup module's directory path for a partial URI."); - $this->assertEqual($instance->getExternalUrl($uri2), $base_url . 'core/modules/system/css/system.admin.css', "Lookup module's directory path for a resource located in a subdirectory."); - $this->assertEqual($instance->getExternalUrl($uri3), $base_url . 'core/modules/file/tests/file_test/file_test.dummy.inc', "Lookup module's directory path for a module located in a subdirectory."); - $this->assertEqual($instance->getExternalUrl($uri4), $base_url . 'core/modules/file/tests/file_test/includes/file_test.dummy.inc', "Lookup module's directory path even for a non-existing resource, as long as the module exists."); - $this->assertFalse($instance->getExternalUrl($uri5), "Fail returning a disabled module's directory path"); - $this->assertFalse($instance->getExternalUrl($uri6), "Fail returning a non-existing module's directory path."); + $base_url = \Drupal::request()->getUri(); + $this->assertEqual($instance->getExternalUrl($uri1), $base_url . 'core/modules/system', "Return the external url for the module directory path."); + $this->assertEqual($instance->getExternalUrl($uri2), $base_url . 'core/modules/system/css/system.admin.css', "Return the external url of a resource located in a subdirectory."); + $this->assertEqual($instance->getExternalUrl($uri3), $base_url . 'core/modules/file/tests/file_test/file_test.dummy.inc', "Return the external url of an include file located in a subdirectory."); + $this->assertEqual($instance->getExternalUrl($uri4), $base_url . 'core/modules/file/tests/file_test/includes/file_test.dummy.inc', "Return the external url even for a non-existing resource, as long as the module exists."); + $this->assertFalse($instance->getExternalUrl($uri5), "Fail returning the external uri for resources in a disabled module."); + $this->assertFalse($instance->getExternalUrl($uri6), "Fail returning the external uri for resources in a non-existing module."); } /** @@ -128,13 +165,13 @@ public function testProfileStream() { $this->assertEqual($instance->getDirectoryPath($uri6), 'core/profiles/standard', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'profile://current'))); // getExternalUrl() - $base_url = \Drupal::request()->getBaseUrl() . '/'; - $this->assertEqual($instance->getExternalUrl($uri1), $base_url . 'core/profiles/minimal', "Lookup profile's directory path for a partial URI."); - $this->assertEqual($instance->getExternalUrl($uri2), $base_url . 'core/profiles/minimal/config/install/block.block.stark_login.yml', "Lookup profile's directory path for a resource located in a subdirectory."); - $this->assertEqual($instance->getExternalUrl($uri3), $base_url . 'core/profiles/minimal/config/install/node.type.article.yml', "Lookup profile's directory path even for a non-existing resource, as long as the profile exists."); - $this->assertFalse($instance->getExternalUrl($uri4), "Fail returning a non-existing profile's directory path."); - $this->assertEqual($instance->getExternalUrl($uri5), $base_url . 'core/profiles/standard', format_string('Lookup real directory path of %current for a partial URI.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getExternalUrl($uri6), $base_url . 'core/profiles/standard/standard.info.yml', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'profile://current'))); + $base_url = \Drupal::request()->getUri(); + $this->assertEqual($instance->getExternalUrl($uri1), $base_url . 'core/profiles/minimal', "Return the external url for the profile directory path."); + $this->assertEqual($instance->getExternalUrl($uri2), $base_url . 'core/profiles/minimal/config/install/block.block.stark_login.yml', "Return the external url of a file located in a subdirectory."); + $this->assertEqual($instance->getExternalUrl($uri3), $base_url . 'core/profiles/minimal/config/install/node.type.article.yml', "Return the external url of a non-existing file located in a subdirectory."); + $this->assertFalse($instance->getExternalUrl($uri4), "Fail returning the external uri for a disabled profile."); + $this->assertEqual($instance->getExternalUrl($uri5), $base_url . 'core/profiles/standard', format_string('Lookup the external url of %current for a partial URI.', array('%current' => 'profile://current'))); + $this->assertEqual($instance->getExternalUrl($uri6), $base_url . 'core/profiles/standard/standard.info.yml', format_string('Lookup the external url of %current for a resource.', array('%current' => 'profile://current'))); } /** @@ -150,10 +187,13 @@ public function testThemeStream() { // Set admin theme to Seven. $system_theme = \Drupal::config('system.theme'); $this->assertNull($system_theme->get('admin'), 'No admin theme was set.'); - file_put_contents('admin.theme', print_r($system_theme->get('admin'), TRUE)); $system_theme->set('admin', 'seven')->save(); $this->assertEqual($system_theme->get('admin'), 'seven', format_string('Now 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'))); + $uri1 = 'theme://seven'; $uri2 = 'theme://seven/style.css'; $uri3 = 'theme://bartik/color/preview.js'; @@ -161,9 +201,9 @@ public function testThemeStream() { $uri5 = 'theme://current'; $uri6 = 'theme://current/logo.png'; $uri7 = 'theme://default'; - $uri8 = 'theme://default/stark.info.yml'; + $uri8 = 'theme://default/bartik.info.yml'; $uri9 = 'theme://admin'; - $uri10 = 'theme://admin/stark.info.yml'; + $uri10 = 'theme://admin/fake.info.yml'; $instance = file_stream_wrapper_get_instance_by_scheme('theme'); /* @var $instance \Drupal\Core\StreamWrapper\ThemeStream */ @@ -172,22 +212,22 @@ public function testThemeStream() { $this->assertEqual($instance->getOwnerName($uri2), 'seven', "Extract theme's name from a full URI."); $this->assertEqual($instance->getOwnerName($uri3), 'bartik', "Extract theme's name from a full URI with subdirectory."); $this->assertFalse($instance->getOwnerName($uri4), "Fail returning a non-existing theme's name."); - $this->assertEqual($instance->getOwnerName($uri5), 'bla', format_string('Lookup real name of %current for a partial URI.', array('%current' => 'theme://current')) . $instance->getOwnerName($uri5)); - $this->assertEqual($instance->getOwnerName($uri6), 'bla', format_string('Lookup real name of %current for a resource.', array('%current' => 'theme://current')) . $instance->getOwnerName($uri6)); - $this->assertEqual($instance->getOwnerName($uri7), 'stark', format_string('Lookup real name of %default for a partial URI.', array('%default' => 'theme://default'))); - $this->assertEqual($instance->getOwnerName($uri8), 'stark', format_string('Lookup real name of %default for a resource.', array('%default' => 'theme://default'))); + $this->assertEqual($instance->getOwnerName($uri5), 'bartik', format_string('Lookup real name of %current for a partial URI.', array('%current' => 'theme://current')) . $instance->getOwnerName($uri5)); + $this->assertEqual($instance->getOwnerName($uri6), 'bartik', format_string('Lookup real name of %current for a resource.', array('%current' => 'theme://current')) . $instance->getOwnerName($uri6)); + $this->assertEqual($instance->getOwnerName($uri7), 'bartik', format_string('Lookup real name of %default for a partial URI.', array('%default' => 'theme://default'))); + $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'))); // getTarget() $this->assertEqual($instance->getTarget($uri1), '', 'Return empty target for a partial URI giving only the theme.'); - $this->assertEqual($instance->getTarget($uri2), 'style.css', 'Extract target for a resource.' . $instance->getTarget($uri3)); + $this->assertFalse($instance->getTarget($uri2), 'Fail returning a target for a non-existent resource.'); $this->assertEqual($instance->getTarget($uri3), 'color/preview.js', 'Extract target for a resource located in a subdirectory.'); $this->assertFalse($instance->getTarget($uri4), 'Fail returning a target within a non-existing theme.'); $this->assertEqual($instance->getTarget($uri5), '', format_string('Return empty target for a partial URI giving only %current.', array('%current' => 'theme://current'))); $this->assertEqual($instance->getTarget($uri6), 'logo.png', format_string("Extract target from a resource within %current.", array('%current' => 'theme://current'))); $this->assertEqual($instance->getTarget($uri7), '', format_string('Return empty target for a partial URI giving only %default.', array('%default' => 'theme://default'))); - $this->assertEqual($instance->getTarget($uri8), 'stark.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($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'))); @@ -196,24 +236,25 @@ public function testThemeStream() { $this->assertEqual($instance->getDirectoryPath($uri2), 'core/themes/seven', "Lookup theme's directory path for a resource located in a subdirectory."); $this->assertEqual($instance->getDirectoryPath($uri3), 'core/themes/bartik', "Lookup theme's directory path for a resource."); $this->assertFalse($instance->getDirectoryPath($uri4), "Fail returning a non-existing theme's directory path."); - $this->assertEqual($instance->getDirectoryPath($uri5), 'core/themes/standard', format_string('Lookup real directory path of %current for a partial URI.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getDirectoryPath($uri6), 'core/themes/standard', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getDirectoryPath($uri7), 'core/themes/stark', format_string('Lookup real directory path of %default for a partial URI.', array('%default' => 'profile://default'))); - $this->assertEqual($instance->getDirectoryPath($uri8), 'core/themes/stark', format_string('Lookup real directory path of %default for a resource.', array('%default' => 'profile://default'))); - $this->assertEqual($instance->getDirectoryPath($uri9), 'core/themes/seven', format_string('Lookup real directory path of %admin for a partial URI.', array('%admin' => 'profile://admin'))); - $this->assertEqual($instance->getDirectoryPath($uri10), 'core/themes/seven', format_string('Lookup real directory path of %admin for a resource.', array('%admin' => 'profile://admin'))); + $this->assertEqual($instance->getDirectoryPath($uri5), 'core/themes/bartik', format_string('Lookup real directory path of %current for a partial URI.', array('%current' => 'theme://current'))); + $this->assertEqual($instance->getDirectoryPath($uri6), 'core/themes/bartik', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'theme://current'))); + $this->assertEqual($instance->getDirectoryPath($uri7), 'core/themes/bartik', format_string('Lookup real directory path of %default for a partial URI.', array('%default' => 'theme://default'))); + $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'))); // getExternalUrl() - $base_url = \Drupal::request()->getBaseUrl() . '/'; - $this->assertEqual($instance->getExternalUrl($uri1), $base_url . 'core/themes/seven', "Lookup theme's directory path for a partial URI."); - $this->assertEqual($instance->getExternalUrl($uri2), $base_url . 'core/themes/seven', "Lookup theme's directory path for a resource located in a subdirectory."); - $this->assertEqual($instance->getExternalUrl($uri3), $base_url . 'core/themes/bartik', "Lookup theme's directory path for a resource."); - $this->assertFalse($instance->getExternalUrl($uri4), "Fail returning a non-existing theme's directory path."); - $this->assertEqual($instance->getExternalUrl($uri5), $base_url . 'core/themes/standard', format_string('Lookup real directory path of %current for a partial URI.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getExternalUrl($uri6), $base_url . 'core/themes/standard', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getExternalUrl($uri7), $base_url . 'core/themes/stark', format_string('Lookup real directory path of %default for a partial URI.', array('%default' => 'profile://default'))); - $this->assertEqual($instance->getExternalUrl($uri8), $base_url . 'core/themes/stark', format_string('Lookup real directory path of %default for a resource.', array('%default' => 'profile://default'))); - $this->assertEqual($instance->getExternalUrl($uri9), $base_url . 'core/themes/seven', format_string('Lookup real directory path of %admin for a partial URI.', array('%admin' => 'profile://admin'))); - $this->assertEqual($instance->getExternalUrl($uri10), $base_url . 'core/themes/seven', format_string('Lookup real directory path of %admin for a resource.', array('%admin' => 'profile://admin'))); + $base_url = \Drupal::request()->getUri(); + $this->assertEqual($instance->getExternalUrl($uri1), $base_url . 'core/themes/seven', "Lookup theme's external url for a partial URI."); + $this->assertEqual($instance->getExternalUrl($uri2), $base_url . 'core/themes/seven/style.css', "Lookup theme's external url for a resource located in a subdirectory."); + $this->assertEqual($instance->getExternalUrl($uri3), $base_url . 'core/themes/bartik/color/preview.js', "Lookup theme's external url for a resource."); + $this->assertFalse($instance->getExternalUrl($uri4), "Fail returning a non-existing theme's external url."); + $this->assertEqual($instance->getExternalUrl($uri5), $base_url . 'core/themes/bartik', format_string('Lookup real external url of %current for a partial URI.', array('%current' => 'theme://current'))); + $this->assertEqual($instance->getExternalUrl($uri6), $base_url . 'core/themes/bartik/logo.png', format_string('Lookup external url of %current for a resource.', array('%current' => 'theme://current'))); + $this->assertEqual($instance->getExternalUrl($uri7), $base_url . 'core/themes/bartik', format_string('Lookup external url of %default for a partial URI.', array('%default' => 'theme://default'))); + $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'))); + } }