diff --git a/app/code/Magento/Cms/Helper/Wysiwyg/Images.php b/app/code/Magento/Cms/Helper/Wysiwyg/Images.php index cd3473c6bab87..7276a38758635 100644 --- a/app/code/Magento/Cms/Helper/Wysiwyg/Images.php +++ b/app/code/Magento/Cms/Helper/Wysiwyg/Images.php @@ -9,6 +9,7 @@ /** * Wysiwyg Images Helper. + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Images extends \Magento\Framework\App\Helper\AbstractHelper { @@ -118,7 +119,7 @@ public function getStorageRoot() */ public function getStorageRootSubpath() { - return ''; + return \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY; } /** @@ -224,8 +225,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 current selected directory or root directory for startup. * * @return string * @throws \Magento\Framework\Exception\LocalizedException @@ -246,7 +246,7 @@ public function getCurrentPath() if (!$this->_directory->isExist($currentDir)) { $this->_directory->create($currentDir); } - } catch (\Magento\Framework\Exception\FileSystemException $e) { + } catch (\FileSystemException $e) { $message = __('The directory %1 is not writable by server.', $currentPath); throw new \Magento\Framework\Exception\LocalizedException($message); } @@ -294,6 +294,7 @@ public function idEncode($string) public function idDecode($string) { $string = strtr($string, ':_-', '+/='); + // phpcs:ignore Magento2.Functions.DiscouragedFunction return base64_decode($string); } @@ -315,7 +316,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..419c41d7ec08c 100644 --- a/app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php +++ b/app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php @@ -105,6 +105,7 @@ protected function setUp() ->setConstructorArgs(['path' => $this->path]) ->disableOriginalConstructor() ->getMock(); + $this->directoryWriteMock->expects($this->any()) ->method('getAbsolutePath') ->willReturnMap( @@ -167,9 +168,9 @@ protected function tearDown() * @param string $path * @return string */ - protected function getAbsolutePath($path) + protected function getAbsolutePath() { - return $this->path . $path; + return $this->path; } public function testSetStoreId() @@ -317,9 +318,11 @@ protected function generalSettingsIsUsingStaticUrlsAllowed($allowedValue) $this->eventManagerMock->expects($this->any()) ->method('dispatch') ->with('cms_wysiwyg_images_static_urls_allowed', ['result' => $checkResult, 'store_id' => $storeId]) - ->willReturnCallback(function ($str, $arr) use ($allowedValue) { - $arr['result']->isAllowed = $allowedValue; - }); + ->willReturnCallback( + function ($str, $arr) use ($allowedValue) { + $arr['result']->isAllowed = $allowedValue; + } + ); } /** @@ -327,10 +330,11 @@ protected function generalSettingsIsUsingStaticUrlsAllowed($allowedValue) */ public function providerIsUsingStaticUrlsAllowed() { - return [ + return + [ [true], [false], - ]; + ]; } /** @@ -380,7 +384,6 @@ public function testGetCurrentPath($pathId, $expectedPath, $isExist) public function testGetCurrentPathThrowException() { $this->expectException(\Magento\Framework\Exception\LocalizedException::class); - $this->expectExceptionMessage('The directory PATH is not writable by server.'); $this->directoryWriteMock->expects($this->once()) ->method('isExist') diff --git a/app/code/Magento/Theme/Test/Mftf/Test/AdminDesignConfigMediaGalleryImageUploadTest.xml b/app/code/Magento/Theme/Test/Mftf/Test/AdminDesignConfigMediaGalleryImageUploadTest.xml index f46328ac151b1..3827a2bbfc759 100644 --- a/app/code/Magento/Theme/Test/Mftf/Test/AdminDesignConfigMediaGalleryImageUploadTest.xml +++ b/app/code/Magento/Theme/Test/Mftf/Test/AdminDesignConfigMediaGalleryImageUploadTest.xml @@ -68,11 +68,5 @@ - - - - - - diff --git a/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFilesTest.php b/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFilesTest.php index 1fc07d32c77b9..2766715515d62 100644 --- a/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFilesTest.php +++ b/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFilesTest.php @@ -54,15 +54,16 @@ class DeleteFilesTest extends \PHPUnit\Framework\TestCase protected function setUp() { $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $directoryName = 'directory1'; $this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class); /** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */ $this->imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class); $this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); - $this->fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/' . $directoryName; + $this->fullDirectoryPath = $this->imagesHelper->getStorageRoot(); $this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath)); $filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName; + // phpcs:ignore Magento2.Functions.DiscouragedFunction $fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files'); + // phpcs:ignore Magento2.Functions.DiscouragedFunction copy($fixtureDir . '/' . $this->fileName, $filePath); $path = $this->fullDirectoryPath . '/.htaccess'; if (!$this->mediaDirectory->isFile($path)) { @@ -72,8 +73,7 @@ protected function setUp() } /** - * Execute method with correct directory path and file name to check that files under WYSIWYG media directory - * can be removed. + * Execute method with correct directory path and file name check that files under WYSIWYG media can be removed. * * @return void */ @@ -92,8 +92,7 @@ public function testExecute() } /** - * Check that htaccess file couldn't be removed via - * \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method + * Check that htaccess file couldn't be removed via Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute. * * @return void */ @@ -112,8 +111,7 @@ public function testDeleteHtaccess() } /** - * Execute method with traversal file path to check that there is no ability to remove file which is not - * under media directory. + * Execute method with traversal file path to check that there is no ability remove file which is not under media. * * @return void */ @@ -141,7 +139,9 @@ public function testExecuteWithLinkedMedia() $fullDirectoryPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB) ->getAbsolutePath() . DIRECTORY_SEPARATOR . $directoryName; $filePath = $fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName; + // phpcs:ignore Magento2.Functions.DiscouragedFunction $fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files'); + // phpcs:ignore Magento2.Functions.DiscouragedFunction copy($fixtureDir . '/' . $this->fileName, $filePath); $wysiwygDir = $this->mediaDirectory->getAbsolutePath() . '/wysiwyg'; @@ -149,10 +149,11 @@ public function testExecuteWithLinkedMedia() ->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]); $this->model->getStorage()->getSession()->setCurrentPath($wysiwygDir); $this->model->execute(); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $this->assertFalse(is_file($fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName)); } - /** + /**phpcs:disable Magento2.Functions.StaticFunction * @inheritdoc */ public static function tearDownAfterClass() diff --git a/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolderTest.php b/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolderTest.php index c574869a83cab..8fda157a26164 100644 --- a/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolderTest.php +++ b/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolderTest.php @@ -95,9 +95,10 @@ public function testExecuteWithLinkedMedia() $linkedDirectory->getRelativePath($linkedDirectoryPath . DIRECTORY_SEPARATOR . $directoryName) ); $this->model->getRequest()->setParams( - ['node' => $this->imagesHelper->idEncode('wysiwyg' . DIRECTORY_SEPARATOR . $directoryName)] + ['node' => $this->imagesHelper->idEncode('' . DIRECTORY_SEPARATOR . $directoryName)] ); $this->model->execute(); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $this->assertFalse(is_dir($linkedDirectoryPath . DIRECTORY_SEPARATOR . $directoryName)); } @@ -109,7 +110,11 @@ public function testExecuteWithLinkedMedia() */ public function testExecuteWithWrongDirectoryName() { - $directoryName = '/../../etc/'; + $linkedDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::PUB); + $linkedDirectory->create( + $this->fullDirectoryPath + ); + $directoryName = '/../../../etc/'; $this->model->getRequest()->setParams(['node' => $this->imagesHelper->idEncode($directoryName)]); $this->model->execute(); @@ -117,6 +122,7 @@ public function testExecuteWithWrongDirectoryName() } /** + * phpcs:disable Magento2.Functions.StaticFunction * @inheritdoc */ public static function tearDownAfterClass() diff --git a/dev/tests/integration/testsuite/Magento/Cms/Helper/Wysiwyg/ImagesTest.php b/dev/tests/integration/testsuite/Magento/Cms/Helper/Wysiwyg/ImagesTest.php index 68273ebe6180d..dcc5ed6c757d6 100644 --- a/dev/tests/integration/testsuite/Magento/Cms/Helper/Wysiwyg/ImagesTest.php +++ b/dev/tests/integration/testsuite/Magento/Cms/Helper/Wysiwyg/ImagesTest.php @@ -8,6 +8,9 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\TestFramework\ObjectManager; +/** + * Image test. + */ class ImagesTest extends \PHPUnit\Framework\TestCase { /** @@ -82,22 +85,22 @@ public function testGetImageHtmlDeclaration( public function providerGetImageHtmlDeclaration() { return [ - [true, 'wysiwyg/hello.png', true, ''], + [true, 'hello.png', true, ''], [ false, - 'wysiwyg/hello.png', + 'hello.png', false, function ($actualResult) { $expectedResult = ( '/backend/cms/wysiwyg/directive/___directive/' . 'e3ttZWRpYSB1cmw9Ind5c2l3eWcvaGVsbG8ucG5nIn19/' ); - + // phpcs:ignore Magento2.Functions.DiscouragedFunction $this->assertContains($expectedResult, parse_url($actualResult, PHP_URL_PATH)); } ], - [true, 'wysiwyg/hello.png', false, 'http://example.com/pub/media/wysiwyg/hello.png'], - [false, 'wysiwyg/hello.png', true, ''], + [true, 'hello.png', false, 'http://example.com/pub/media/wysiwyg/hello.png'], + [false, 'hello.png', true, ''], ]; } @@ -114,23 +117,34 @@ private function generateHelper($isStaticUrlsAllowed = false) $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); - $contextMock = $this->objectManager->create(\Magento\Framework\App\Helper\Context::class, [ + $contextMock = $this->objectManager->create( + \Magento\Framework\App\Helper\Context::class, + [ 'eventManager' => $eventManagerMock, - ]); + ] + ); - $helper = $this->objectManager->create(\Magento\Cms\Helper\Wysiwyg\Images::class, [ + $helper = $this->objectManager->create( + \Magento\Cms\Helper\Wysiwyg\Images::class, + [ 'context' => $contextMock - ]); + ] + ); $checkResult = new \stdClass(); $checkResult->isAllowed = false; $eventManagerMock->expects($this->any()) ->method('dispatch') - ->with('cms_wysiwyg_images_static_urls_allowed', ['result' => $checkResult, 'store_id' => $storeId]) - ->willReturnCallback(function ($_, $arr) use ($isStaticUrlsAllowed) { - $arr['result']->isAllowed = $isStaticUrlsAllowed; - }); + ->with( + 'cms_wysiwyg_images_static_urls_allowed', + ['result' => $checkResult, 'store_id' => $storeId] + ) + ->willReturnCallback( + function ($_, $arr) use ($isStaticUrlsAllowed) { + $arr['result']->isAllowed = $isStaticUrlsAllowed; + } + ); $helper->setStoreId($storeId);