diff --git a/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php b/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php index 31a0c0f..bf2cdfc 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php @@ -46,5 +46,4 @@ public function getOwnerName($uri = NULL) { public function getDirectoryPath($uri = NULL) { return drupal_get_path('profile', $this->getOwnerName($uri)); } - } diff --git a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php index cb8267e..289583a 100644 --- a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php @@ -48,6 +48,20 @@ public function getOwnerName($uri = NULL) { * {@inheritdoc} */ public function getTarget($uri = NULL) { + $target = $this->extractTarget($uri); + return file_exists($this->getDirectoryPath($uri) . '/' . $target) ? $target : NULL; + } + + /** + * Returns the local target of the resource, regardless of whether it exists. + * + * @param string $uri + * Optional URI. + * + * @return bool|string + * A path to the local target. + */ + protected function extractTarget($uri = NULL) { // If the owner doesn't exist at all, we don't extract anything. if ($this->getOwnerName($uri) === FALSE) { return FALSE; @@ -56,8 +70,7 @@ public function getTarget($uri = NULL) { // Remove the preceding owner name including slash from the path. $start = strpos($target, '/'); $target = ($start === FALSE) ? '' : substr($target, $start + 1); - // Return the target if it exists in filesystem. - return file_exists($this->getDirectoryPath($uri) . '/' . $target) ? $target : FALSE; + return $target; } /** @@ -69,9 +82,8 @@ public function getExternalUrl($uri = NULL) { return FALSE; } - $path = str_replace('\\', '/', $this->getTarget($uri)); - return \Drupal::request()->getBaseUrl() . '/' . $dir . '/' . UrlHelper::encodePath($path); - + $target = $this->extractTarget($uri); + $path = $target != '' ? '/' . UrlHelper::encodePath(str_replace('\\', '/', $target)) : ''; + return \Drupal::request()->getBaseUrl() . '/' . $dir . $path; } - } diff --git a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php index 24e925b..29ac6b9 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php @@ -35,7 +35,7 @@ public function getOwnerName($uri = NULL) { return \Drupal::config('system.theme')->get('admin'); default: // Return name only for enabled and admin themes. - return drupal_theme_access($name) ? $name : FALSE; + return \Drupal::service('access_check.theme')->checkAccess($name) ? $name : FALSE; } } diff --git a/core/modules/system/src/Tests/File/SystemStreamUnitTest.php b/core/modules/system/src/Tests/File/SystemStreamUnitTest.php index 9b2a326..45cd6fa 100644 --- a/core/modules/system/src/Tests/File/SystemStreamUnitTest.php +++ b/core/modules/system/src/Tests/File/SystemStreamUnitTest.php @@ -2,17 +2,20 @@ /** * @file - * Definition of Drupal\system\Tests\File\StreamWrapperTest. + * Contains \Drupal\system\Tests\File\SystemStreamUnitTest. */ namespace Drupal\system\Tests\File; -use Drupal\simpletest\DrupalUnitTestBase; +use Drupal\Core\Site\Settings; +use Drupal\simpletest\KernelTestBase; /** - * Tests stream wrapper functions. + * Unit tests for system stream wrapper functions. + * + * @group system */ -class SystemStreamUnitTest extends DrupalUnitTestBase { +class SystemStreamUnitTest extends KernelTestBase { /** * Modules to enable. @@ -21,28 +24,29 @@ class SystemStreamUnitTest extends DrupalUnitTestBase { */ public static $modules = array('system', 'file', 'file_test'); - public static function getInfo() { - return array( - 'name' => 'System Stream wrappers', - 'description' => 'Unit tests system stream wrapper functions.', - 'group' => 'File API', - ); - } - - function setUp() { + /** + * {@inheritdoc} + */ + public function setUp() { parent::setUp(); drupal_static_reset('file_get_stream_wrappers'); + new Settings(Settings::getAll() + array( + 'install_profile' => 'standard', + )); } - function tearDown() { + /** + * {@inheritdoc} + */ + public function tearDown() { parent::tearDown(); } /** * Test the Module stream wrapper functions. */ - function testModuleStream() { - // Generate a module stream wrapper instance + public function testModuleStream() { + // Generate a module stream wrapper instance. $uri1 = 'module://system'; $uri2 = 'module://system/css/system.admin.css'; $uri3 = 'module://file_test/file_test.dummy.inc'; @@ -50,6 +54,7 @@ function testModuleStream() { $uri5 = 'module://ckeditor/ckeditor.info.yml'; $uri6 = 'module://foo_bar/foo.bar.js'; $instance = file_stream_wrapper_get_instance_by_scheme('module'); + /* @var $instance \Drupal\Core\StreamWrapper\ModuleStream */ // getOwnerName() $this->assertEqual($instance->getOwnerName($uri1), 'system', 'Extract module name from a partial URI.'); @@ -81,21 +86,22 @@ function testModuleStream() { $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), $base_url . "Fail returning a disabled module's directory path"); - $this->assertFalse($instance->getExternalUrl($uri6), $base_url . "Fail returning a non-existing module's directory path."); + $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."); } /** * Test the Profile stream wrapper functions. */ - function testProfileStream() { + public function testProfileStream() { $uri1 = 'profile://minimal'; - $uri2 = 'profile://minimal/config/block.block.stark.login.yml'; - $uri3 = 'profile://minimal/config/node.type.article.yml'; + $uri2 = 'profile://minimal/config/install/block.block.stark_login.yml'; + $uri3 = 'profile://minimal/config/install/node.type.article.yml'; $uri4 = 'profile://foo_bar/'; $uri5 = 'profile://current'; $uri6 = 'profile://current/standard.info.yml'; $instance = file_stream_wrapper_get_instance_by_scheme('profile'); + /* @var $instance \Drupal\Core\StreamWrapper\ProfileStream */ // getOwnerName() $this->assertEqual($instance->getOwnerName($uri1), 'minimal', "Extract profile's name from a partial URI."); @@ -107,7 +113,7 @@ function testProfileStream() { // getTarget() $this->assertEqual($instance->getTarget($uri1), '', 'Return empty target for a partial URI giving only the profile.'); - $this->assertEqual($instance->getTarget($uri2), 'config/block.block.stark.login.yml', 'Extract target for a resource located in a subdirectory.'); + $this->assertEqual($instance->getTarget($uri2), 'config/install/block.block.stark_login.yml', 'Extract target for a resource located in a subdirectory.'); $this->assertFalse($instance->getTarget($uri3), 'Fail returning a target for a non-existing resource.'); $this->assertFalse($instance->getTarget($uri4), 'Fail returning a target within a non-existing profile.'); $this->assertEqual($instance->getTarget($uri5), '', format_string('Return empty target for a partial URI giving only %current.', array('%current' => 'profile://current'))); @@ -122,26 +128,27 @@ 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() - $this->assertEqual($instance->getExternalUrl($uri1), 'core/profiles/minimal', "Lookup profile's directory path for a partial URI."); - $this->assertEqual($instance->getExternalUrl($uri2), 'core/profiles/minimal', "Lookup profile's directory path for a resource located in a subdirectory."); - $this->assertEqual($instance->getExternalUrl($uri3), 'core/profiles/minimal', "Lookup profile's directory path even for a non-existing resource, as long as the profile exists."); + $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), 'core/profiles/standard', format_string('Lookup real directory path of %current for a partial URI.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getExternalUrl($uri6), 'core/profiles/standard', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'profile://current'))); + $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'))); } /** * Test the Theme stream wrapper functions. */ - function testThemeStream() { - // Disable Bartik theme - $system_theme_disabled = config('system.theme.disabled'); - $this->assertNotIdentical($system_theme_disabled->get('bartik'), '0', format_string('%bartik theme was enabled.', array('%bartik' => 'Bartik'))); + 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'))); + $this->assertIdentical($system_theme_disabled->get('bartik'), 0, format_string('Now disable %bartik theme.', array('%bartik' => 'Bartik'))); - // Set admin theme to Seven - $system_theme = config('system.theme'); + // 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(); @@ -158,6 +165,7 @@ function testThemeStream() { $uri9 = 'theme://admin'; $uri10 = 'theme://admin/stark.info.yml'; $instance = file_stream_wrapper_get_instance_by_scheme('theme'); + /* @var $instance \Drupal\Core\StreamWrapper\ThemeStream */ // getOwnerName() $this->assertEqual($instance->getOwnerName($uri1), 'seven', "Extract theme's name from a partial URI."); @@ -196,16 +204,16 @@ function testThemeStream() { $this->assertEqual($instance->getDirectoryPath($uri10), 'core/themes/seven', format_string('Lookup real directory path of %admin for a resource.', array('%admin' => 'profile://admin'))); // getExternalUrl() - $this->assertEqual($instance->getExternalUrl($uri1), 'core/themes/seven', "Lookup theme's directory path for a partial URI."); - $this->assertEqual($instance->getExternalUrl($uri2), 'core/themes/seven', "Lookup theme's directory path for a resource located in a subdirectory."); - $this->assertEqual($instance->getExternalUrl($uri3), 'core/themes/bartik', "Lookup theme's directory path for a resource."); + $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), 'core/themes/standard', format_string('Lookup real directory path of %current for a partial URI.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getExternalUrl($uri6), 'core/themes/standard', format_string('Lookup real directory path of %current for a resource.', array('%current' => 'profile://current'))); - $this->assertEqual($instance->getExternalUrl($uri7), 'core/themes/stark', format_string('Lookup real directory path of %default for a partial URI.', array('%default' => 'profile://default'))); - $this->assertEqual($instance->getExternalUrl($uri8), 'core/themes/stark', format_string('Lookup real directory path of %default for a resource.', array('%default' => 'profile://default'))); - $this->assertEqual($instance->getExternalUrl($uri9), 'core/themes/seven', format_string('Lookup real directory path of %admin for a partial URI.', array('%admin' => 'profile://admin'))); - $this->assertEqual($instance->getExternalUrl($uri10), 'core/themes/seven', format_string('Lookup real directory path of %admin for a resource.', array('%admin' => 'profile://admin'))); - + $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'))); } }