diff --git a/core/modules/image/src/Plugin/ImageDerivative/Core.php b/core/modules/image/src/Plugin/ImageDerivative/Core.php index 8b154a784b..8cb305008a 100644 --- a/core/modules/image/src/Plugin/ImageDerivative/Core.php +++ b/core/modules/image/src/Plugin/ImageDerivative/Core.php @@ -226,7 +226,10 @@ public function buildDerivativeUri() { // source files not stored in the default scheme. if ($source_scheme != $default_scheme) { $class = $this->streamWrapperManager->getClass($source_scheme); - $is_writable = $class::getType() & StreamWrapperInterface::WRITE; + $is_writable = NULL; + if ($class) { + $is_writable = $class::getType() & StreamWrapperInterface::WRITE; + } // Compute the derivative URI scheme. Derivatives created from writable // source stream wrappers will inherit the scheme. Derivatives created @@ -258,7 +261,7 @@ public function buildDerivativeUrl($clean_urls = NULL) { $token_query = []; if (!$this->configFactory->get('image.settings')->get('suppress_itok_output')) { // The sourceUri property can be either a relative path or a full URI. - $original_uri = file_uri_scheme($this->getVariable('sourceUri')) ? file_stream_wrapper_uri_normalize($this->getVariable('sourceUri')) : file_build_uri($this->getVariable('sourceUri')); + $original_uri = $this->streamWrapperManager->getScheme($this->getVariable('sourceUri')) ? $this->streamWrapperManager->normalizeUri($this->getVariable('sourceUri')) : file_build_uri($this->getVariable('sourceUri')); $token_query = [IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri)]; } @@ -277,9 +280,9 @@ public function buildDerivativeUrl($clean_urls = NULL) { // ensure that it is included. Once the file exists it's fine to fall back // to the actual file path, this avoids bootstrapping PHP once the files are // built. - if ($clean_urls === FALSE && file_uri_scheme($derivative_uri) == 'public' && !file_exists($derivative_uri)) { + if ($clean_urls === FALSE && $this->streamWrapperManager->getScheme($derivative_uri) == 'public' && !file_exists($derivative_uri)) { $directory_path = $this->streamWrapperManager->getViaUri($derivative_uri)->getDirectoryPath(); - return Url::fromUri('base:' . $directory_path . '/' . file_uri_target($derivative_uri), ['absolute' => TRUE, 'query' => $token_query])->toString(); + return Url::fromUri('base:' . $directory_path . '/' . $this->streamWrapperManager->getTarget($derivative_uri), ['absolute' => TRUE, 'query' => $token_query])->toString(); } $file_url = file_create_url($derivative_uri); @@ -297,7 +300,7 @@ public function buildDerivativeUrl($clean_urls = NULL) { public function removeSourceUriDerivative() { $derivative_uri = $this->buildDerivativeUri(); if (file_exists($derivative_uri)) { - file_unmanaged_delete($derivative_uri); + $this->fileSystem->delete($derivative_uri); } return TRUE; } @@ -310,7 +313,7 @@ public function removeAllImageStyleDerivatives() { $wrappers = $this->streamWrapperManager->getWrappers(StreamWrapperInterface::WRITE_VISIBLE); foreach ($wrappers as $wrapper => $wrapper_data) { if (file_exists($directory = $wrapper . '://styles/' . $this->getVariable('imageStyle')->id())) { - file_unmanaged_delete_recursive($directory); + $this->fileSystem->deleteRecursive($directory); } } @@ -354,7 +357,7 @@ public function saveImage($uri) { $directory = $this->fileSystem->dirname($uri); // Build the destination folder tree if it doesn't already exist. - if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { + if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) { $this->logger->error('Failed to create style directory: %directory', ['%directory' => $directory]); return FALSE; } @@ -454,16 +457,12 @@ protected function addExtension($path) { * @param string $uri * A stream, referenced as "scheme://target" or "data:target". * - * @see file_uri_target() - * - * @todo: Remove when https://www.drupal.org/node/2050759 is in. - * * @return string * A string containing the name of the scheme, or FALSE if none. For * example, the URI "public://example.txt" would return "public". */ protected function fileUriScheme($uri) { - return file_uri_scheme($uri); + return $this->streamWrapperManager->getScheme($uri); } /** @@ -474,17 +473,13 @@ protected function fileUriScheme($uri) { * @param string $uri * A stream, referenced as "scheme://target" or "data:target". * - * @see file_uri_scheme() - * - * @todo: Convert file_uri_target() into a proper injectable service. - * * @return string|bool * A string containing the target (path), or FALSE if none. * For example, the URI "public://sample/test.txt" would return * "sample/test.txt". */ protected function fileUriTarget($uri) { - return file_uri_target($uri); + return $this->streamWrapperManager->getTarget($uri); } /** @@ -492,13 +487,11 @@ protected function fileUriTarget($uri) { * * Gets the default file stream implementation. * - * @todo: Convert file_default_scheme() into a proper injectable service. - * * @return string * 'public', 'private' or any other file scheme defined as the default. */ protected function fileDefaultScheme() { - return file_default_scheme(); + return $this->configFactory->get('system.file')->get('default_scheme'); } } diff --git a/core/modules/image/tests/src/Kernel/ImageStyleTest.php b/core/modules/image/tests/src/Kernel/ImageStyleTest.php index 2c5f610050..3051e0ebad 100644 --- a/core/modules/image/tests/src/Kernel/ImageStyleTest.php +++ b/core/modules/image/tests/src/Kernel/ImageStyleTest.php @@ -4,6 +4,7 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\image\Entity\ImageStyle; +use Drupal\image\ImageStyleInterface; /** * Legacy test for deprecated ImageStyle methods. @@ -47,7 +48,7 @@ protected function setUp() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::buildUri method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testBuildUri() { - $this->imageStyle->buildUri('public://test.png'); + $this->assertNotEmpty($this->imageStyle->buildUri('public://test.png')); } /** @@ -55,7 +56,7 @@ public function testBuildUri() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::buildUrl method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testBuildUrl() { - $this->imageStyle->buildUrl('public://test.png'); + $this->assertNotEmpty($this->imageStyle->buildUrl('public://test.png')); } /** @@ -63,7 +64,7 @@ public function testBuildUrl() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::flush method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testFlush() { - $this->imageStyle->flush(); + $this->assertInstanceOf(ImageStyleInterface::class, $this->imageStyle->flush()); } /** @@ -71,7 +72,7 @@ public function testFlush() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::createDerivative method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testCreateDerivative() { - $this->imageStyle->createDerivative('public://test.png', 'public://test_derivative.png'); + $this->assertInternalType('bool', $this->imageStyle->createDerivative('public://test.png', 'public://test_derivative.png')); } /** @@ -80,7 +81,7 @@ public function testCreateDerivative() { */ public function testTransformDimensions() { $dimensions = ['width' => 100, 'height' => 200]; - $this->imageStyle->transformDimensions($dimensions, 'public://test.png'); + $this->assertNull($this->imageStyle->transformDimensions($dimensions, 'public://test.png')); } /** @@ -88,7 +89,7 @@ public function testTransformDimensions() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::getDerivativeExtension method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testGetDerivativeExtension() { - $this->imageStyle->getDerivativeExtension('png'); + $this->assertNotEmpty($this->imageStyle->getDerivativeExtension('png')); } /** @@ -96,7 +97,7 @@ public function testGetDerivativeExtension() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::getPathToken method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testGetPathToken() { - $this->imageStyle->getPathToken('public://test.png'); + $this->assertNotEmpty($this->imageStyle->getPathToken('public://test.png')); } /** @@ -104,7 +105,7 @@ public function testGetPathToken() { * @expectedDeprecation The Drupal\image\Entity\ImageStyle::supportsUri method is deprecated since version 9.x.x and will be removed in y.y.y. */ public function testSupportsUri() { - $this->imageStyle->supportsUri('public://test.png'); + $this->assertInternalType('bool', $this->imageStyle->supportsUri('public://test.png')); } } diff --git a/core/modules/image/tests/src/Unit/CoreImageDerivativePluginTest.php b/core/modules/image/tests/src/Unit/CoreImageDerivativePluginTest.php index 031ebf5d76..a5934a5d25 100644 --- a/core/modules/image/tests/src/Unit/CoreImageDerivativePluginTest.php +++ b/core/modules/image/tests/src/Unit/CoreImageDerivativePluginTest.php @@ -86,13 +86,13 @@ protected function getCoreImageDerivativePluginMock($stubs = []) { 'core', [], $image_factory, - $this->getMock('\Drupal\Core\StreamWrapper\StreamWrapperManagerInterface'), + $this->createMock('\Drupal\Core\StreamWrapper\StreamWrapperManagerInterface'), $private_key, - $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface'), - $this->getMock('\Drupal\Core\Config\ConfigFactoryInterface'), - $this->getMock('\Symfony\Component\HttpFoundation\RequestStack'), - $this->getMock('\Psr\Log\LoggerInterface'), - $this->getMock('\Drupal\Core\File\FileSystemInterface'), + $this->createMock('\Drupal\Core\Extension\ModuleHandlerInterface'), + $this->createMock('\Drupal\Core\Config\ConfigFactoryInterface'), + $this->createMock('\Symfony\Component\HttpFoundation\RequestStack'), + $this->createMock('\Psr\Log\LoggerInterface'), + $this->createMock('\Drupal\Core\File\FileSystemInterface'), ]) ->setMethods(array_merge($default_stubs, $stubs)) ->getMock();