diff --git a/app/code/Magento/Cms/Helper/Wysiwyg/Images.php b/app/code/Magento/Cms/Helper/Wysiwyg/Images.php index cd3473c6bab87..c634910f4468f 100644 --- a/app/code/Magento/Cms/Helper/Wysiwyg/Images.php +++ b/app/code/Magento/Cms/Helper/Wysiwyg/Images.php @@ -224,8 +224,7 @@ public function getImageHtmlDeclaration($filename, $renderAsTag = false) } /** - * Return path of the current selected directory or root directory for startup - * Try to create target directory if it doesn't exist + * Return path of the root directory for startup. Also try to create target directory if it doesn't exist * * @return string * @throws \Magento\Framework\Exception\LocalizedException @@ -241,18 +240,34 @@ public function getCurrentPath() $currentPath = $path; } } + + $currentTreePath = $this->_getRequest()->getParam('current_tree_path'); + if ($currentTreePath) { + $currentTreePath = $this->convertIdToPath($currentTreePath); + $this->createSubDirIfNotExist($currentTreePath); + } + + $this->_currentPath = $currentPath; + } + + return $this->_currentPath; + } + + private function createSubDirIfNotExist(string $absPath) + { + $relPath = $this->_directory->getRelativePath($absPath); + if (!$this->_directory->isExist($relPath)) { try { - $currentDir = $this->_directory->getRelativePath($currentPath); - if (!$this->_directory->isExist($currentDir)) { - $this->_directory->create($currentDir); - } + $this->_directory->create($relPath); } catch (\Magento\Framework\Exception\FileSystemException $e) { - $message = __('The directory %1 is not writable by server.', $currentPath); + $message = __( + 'Can\'t create %1 as subdirectory of %2, you might have some permission issue.', + $relPath, + $this->_directory->getAbsolutePath() + ); throw new \Magento\Framework\Exception\LocalizedException($message); } - $this->_currentPath = $currentPath; } - return $this->_currentPath; } /** @@ -294,6 +309,8 @@ public function idEncode($string) public function idDecode($string) { $string = strtr($string, ':_-', '+/='); + + // phpcs:ignore Magento2.Functions.DiscouragedFunction return base64_decode($string); } @@ -315,7 +332,7 @@ public function getShortFilename($filename, $maxLength = 20) /** * Set user-traversable image directory subpath relative to media directory and relative to nested storage root * - * @var string $subpath + * @param string $subpath * @return void */ public function setImageDirectorySubpath($subpath) diff --git a/app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php b/app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php index d13b4f47a85e7..4acef951d8f4a 100644 --- a/app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php +++ b/app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php @@ -339,13 +339,14 @@ public function providerIsUsingStaticUrlsAllowed() * @param bool $isExist * @dataProvider providerGetCurrentPath */ - public function testGetCurrentPath($pathId, $expectedPath, $isExist) + public function testGetCurrentPath($pathId, $subDir, $expectedPath, $isExist) { $this->requestMock->expects($this->any()) ->method('getParam') ->willReturnMap( [ ['node', null, $pathId], + ['current_tree_path', null, $subDir], ] ); @@ -367,21 +368,33 @@ public function testGetCurrentPath($pathId, $expectedPath, $isExist) ['PATH', '.'], ] ); - $this->directoryWriteMock->expects($this->once()) - ->method('isExist') - ->willReturn($isExist); - $this->directoryWriteMock->expects($this->any()) - ->method('create') - ->with($this->directoryWriteMock->getRelativePath($expectedPath)); + + if ($subDir) { + $this->directoryWriteMock->expects($this->once()) + ->method('isExist') + ->willReturn($isExist); + $this->directoryWriteMock->expects($this->any()) + ->method('create') + ->with($this->directoryWriteMock->getRelativePath($expectedPath)); + } $this->assertEquals($expectedPath, $this->imagesHelper->getCurrentPath()); } public function testGetCurrentPathThrowException() { + $this->requestMock->expects($this->any()) + ->method('getParam') + ->willReturn('PATH'); + $this->expectException(\Magento\Framework\Exception\LocalizedException::class); - $this->expectExceptionMessage('The directory PATH is not writable by server.'); + $this->expectExceptionMessage( + 'Can\'t create SUBDIR as subdirectory of PATH, you might have some permission issue.' + ); + $this->directoryWriteMock->expects($this->any()) + ->method('getRelativePath') + ->willReturn('SUBDIR'); $this->directoryWriteMock->expects($this->once()) ->method('isExist') ->willReturn(false); @@ -402,12 +415,12 @@ public function testGetCurrentPathThrowException() public function providerGetCurrentPath() { return [ - ['L3Rlc3RfcGF0aA--', 'PATH/test_path', true], - ['L215LmpwZw--', 'PATH', true], - [null, 'PATH', true], - ['L3Rlc3RfcGF0aA--', 'PATH/test_path', false], - ['L215LmpwZw--', 'PATH', false], - [null, 'PATH', false], + ['L3Rlc3RfcGF0aA--', 'L3Rlc3RfcGF0aA--', 'PATH/test_path', true], + ['L215LmpwZw--', '', 'PATH', true], + [null, '', 'PATH', true], + ['L3Rlc3RfcGF0aA--', 'L3Rlc3RfcGF0aA--', 'PATH/test_path', false], + ['L215LmpwZw--', '', 'PATH', false], + [null, '', 'PATH', false], ]; }