Skip to content

Set media storage root to wysiwyg directory in pub/media #22681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/code/Magento/Cms/Helper/Wysiwyg/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* Wysiwyg Images Helper.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Images extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand Down Expand Up @@ -118,7 +119,7 @@ public function getStorageRoot()
*/
public function getStorageRootSubpath()
{
return '';
return \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY;
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -294,6 +294,7 @@ public function idEncode($string)
public function idDecode($string)
{
$string = strtr($string, ':_-', '+/=');
// phpcs:ignore Magento2.Functions.DiscouragedFunction
return base64_decode($string);
}

Expand All @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ protected function setUp()
->setConstructorArgs(['path' => $this->path])
->disableOriginalConstructor()
->getMock();

$this->directoryWriteMock->expects($this->any())
->method('getAbsolutePath')
->willReturnMap(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -317,20 +318,23 @@ 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;
}
);
}

/**
* @return array
*/
public function providerIsUsingStaticUrlsAllowed()
{
return [
return
[
[true],
[false],
];
];
}

/**
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,5 @@
<actionGroup ref="DeleteImageFromStorageActionGroup" stepKey="deleteImageFromStorage">
<argument name="Image" value="ImageUpload3"/>
</actionGroup>
<actionGroup ref="NavigateToFaviconMediaFolderActionGroup" stepKey="navigateToFolder3">
<argument name="StoreFolder" value="{{AdminDesignConfigSection.storeLink}}"/>
</actionGroup>
<actionGroup ref="DeleteImageFromStorageActionGroup" stepKey="deleteImageFromStorage2">
<argument name="Image" value="ImageUpload3"/>
</actionGroup>
</test>
</tests>
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -141,18 +139,21 @@ 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';
$this->model->getRequest()->setMethod('POST')
->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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -109,14 +110,19 @@ 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();

$this->assertFileExists($this->fullDirectoryPath . $directoryName);
}

/**
* phpcs:disable Magento2.Functions.StaticFunction
* @inheritdoc
*/
public static function tearDownAfterClass()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\TestFramework\ObjectManager;

/**
* Image test.
*/
class ImagesTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -82,22 +85,22 @@ public function testGetImageHtmlDeclaration(
public function providerGetImageHtmlDeclaration()
{
return [
[true, 'wysiwyg/hello.png', true, '<img src="http://example.com/pub/media/wysiwyg/hello.png" alt="" />'],
[true, 'hello.png', true, '<img src="http://example.com/pub/media/wysiwyg/hello.png" alt="" />'],
[
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, '<img src="{{media url=&quot;wysiwyg/hello.png&quot;}}" alt="" />'],
[true, 'hello.png', false, 'http://example.com/pub/media/wysiwyg/hello.png'],
[false, 'hello.png', true, '<img src="{{media url=&quot;wysiwyg/hello.png&quot;}}" alt="" />'],
];
}

Expand All @@ -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);

Expand Down