Skip to content

Commit a30e6d9

Browse files
committed
Merge remote-tracking branch 'origin/MC-30103' into 2.4-develop-pr8
2 parents 6b4a82b + 9a31aa2 commit a30e6d9

File tree

3 files changed

+104
-27
lines changed

3 files changed

+104
-27
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,11 @@ public function getThumbnailPath($filePath, $checkFile = false)
570570
$mediaRootDir = $this->_cmsWysiwygImages->getStorageRoot();
571571

572572
if (strpos($filePath, (string) $mediaRootDir) === 0) {
573-
$thumbPath = $this->getThumbnailRoot() . substr($filePath, strlen($mediaRootDir));
573+
$relativeFilePath = substr($filePath, strlen($mediaRootDir));
574+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
575+
$thumbPath = $relativeFilePath === basename($filePath)
576+
? $this->getThumbnailRoot() . DIRECTORY_SEPARATOR . $relativeFilePath
577+
: $this->getThumbnailRoot() . $relativeFilePath;
574578

575579
if (!$checkFile || $this->_directory->isExist($this->_directory->getRelativePath($thumbPath))) {
576580
return $thumbPath;
@@ -589,21 +593,12 @@ public function getThumbnailPath($filePath, $checkFile = false)
589593
*/
590594
public function getThumbnailUrl($filePath, $checkFile = false)
591595
{
592-
$mediaRootDir = $this->_cmsWysiwygImages->getStorageRoot();
593-
594-
if (strpos($filePath, (string) $mediaRootDir) === 0) {
595-
$thumbSuffix = self::THUMBS_DIRECTORY_NAME . substr($filePath, strlen($mediaRootDir));
596-
if (!$checkFile || $this->_directory->isExist(
597-
$this->_directory->getRelativePath($mediaRootDir . '/' . $thumbSuffix)
598-
)
599-
) {
600-
$thumbSuffix = substr(
601-
$mediaRootDir,
602-
strlen($this->_directory->getAbsolutePath())
603-
) . '/' . $thumbSuffix;
604-
$randomIndex = '?rand=' . time();
605-
return str_replace('\\', '/', $this->_cmsWysiwygImages->getBaseUrl() . $thumbSuffix) . $randomIndex;
606-
}
596+
$thumbPath = $this->getThumbnailPath($filePath, $checkFile);
597+
if ($thumbPath) {
598+
$thumbRelativePath = ltrim($this->_directory->getRelativePath($thumbPath), '/\\');
599+
$baseUrl = rtrim($this->_cmsWysiwygImages->getBaseUrl(), '/');
600+
$randomIndex = '?rand=' . time();
601+
return str_replace('\\', '/', $baseUrl . '/' . $thumbRelativePath) . $randomIndex;
607602
}
608603

609604
return false;
@@ -666,11 +661,13 @@ public function resizeOnTheFly($filename)
666661
*/
667662
public function getThumbsPath($filePath = false)
668663
{
669-
$mediaRootDir = $this->_cmsWysiwygImages->getStorageRoot();
670664
$thumbnailDir = $this->getThumbnailRoot();
671665

672-
if ($filePath && strpos($filePath, (string) $mediaRootDir) === 0) {
673-
$thumbnailDir .= $this->file->getParentDirectory(substr($filePath, strlen($mediaRootDir)));
666+
if ($filePath) {
667+
$thumbPath = $this->getThumbnailPath($filePath, false);
668+
if ($thumbPath) {
669+
$thumbnailDir = $this->file->getParentDirectory($thumbPath);
670+
}
674671
}
675672

676673
return $thumbnailDir;

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class StorageTest extends \PHPUnit\Framework\TestCase
131131
*/
132132
protected function setUp()
133133
{
134+
$this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
134135
$this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
135136
$this->driverMock = $this->getMockBuilder(\Magento\Framework\Filesystem\DriverInterface::class)
136137
->setMethods(['getRealPathSafety'])
@@ -159,10 +160,7 @@ protected function setUp()
159160
$this->returnValue($this->directoryMock)
160161
);
161162

162-
$this->fileMock = $this->createPartialMock(
163-
\Magento\Framework\Filesystem\Driver\File::class,
164-
['getParentDirectory']
165-
);
163+
$this->fileMock = $this->objectManagerHelper->getObject(\Magento\Framework\Filesystem\Driver\File::class);
166164
$this->ioFileMock = $this->createPartialMock(\Magento\Framework\Filesystem\Io\File::class, ['getPathInfo']);
167165
$this->ioFileMock->expects(
168166
$this->any()
@@ -233,8 +231,6 @@ function ($path) {
233231
'image_allowed' => $this->allowedImageExtensions,
234232
];
235233

236-
$this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
237-
238234
$this->imagesStorage = $this->objectManagerHelper->getObject(
239235
\Magento\Cms\Model\Wysiwyg\Images\Storage::class,
240236
[
@@ -525,8 +521,6 @@ public function testUploadFile()
525521
]
526522
);
527523

528-
$this->fileMock->expects($this->any())->method('getParentDirectory')->willReturn($path);
529-
530524
$image = $this->getMockBuilder(\Magento\Catalog\Model\Product\Image::class)
531525
->disableOriginalConstructor()
532526
->setMethods(['open', 'keepAspectRatio', 'resize', 'save'])

dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
use Magento\Framework\App\Filesystem\DirectoryList;
1010

1111
/**
12+
* Test methods of class Storage
1213
*
1314
* @SuppressWarnings(PHPMD.LongVariable)
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
1518
*/
1619
class StorageTest extends \PHPUnit\Framework\TestCase
1720
{
@@ -260,4 +263,87 @@ public function testUploadFileWithWrongFile(): void
260263
$this->assertFalse(is_file(self::$_baseDir . DIRECTORY_SEPARATOR . $fileName));
261264
// phpcs:enable
262265
}
266+
267+
/**
268+
* Test that getThumbnailUrl() returns correct URL for root folder or sub-folders images
269+
*
270+
* @param string $directory
271+
* @param string $filename
272+
* @param string $expectedUrl
273+
* @return void
274+
* @magentoAppIsolation enabled
275+
* @magentoAppArea adminhtml
276+
* @dataProvider getThumbnailUrlDataProvider
277+
*/
278+
public function testGetThumbnailUrl(string $directory, string $filename, string $expectedUrl): void
279+
{
280+
$root = $this->storage->getCmsWysiwygImages()->getStorageRoot();
281+
$directory = implode('/', array_filter([rtrim($root, '/'), trim($directory, '/')]));
282+
$path = $directory . '/' . $filename;
283+
$this->generateImage($path);
284+
$this->storage->resizeFile($path);
285+
$collection = $this->storage->getFilesCollection($directory, 'image');
286+
$paths = [];
287+
foreach ($collection as $item) {
288+
$paths[] = parse_url($item->getThumbUrl(), PHP_URL_PATH);
289+
}
290+
$this->assertEquals([$expectedUrl], $paths);
291+
$this->storage->deleteFile($path);
292+
}
293+
294+
/**
295+
* Provide scenarios for testing getThumbnailUrl()
296+
*
297+
* @return array
298+
*/
299+
public function getThumbnailUrlDataProvider(): array
300+
{
301+
return [
302+
[
303+
'/',
304+
'image1.png',
305+
'/pub/media/.thumbs/image1.png'
306+
],
307+
[
308+
'/cms',
309+
'image2.png',
310+
'/pub/media/.thumbscms/image2.png'
311+
],
312+
[
313+
'/cms/pages',
314+
'image3.png',
315+
'/pub/media/.thumbscms/pages/image3.png'
316+
]
317+
];
318+
}
319+
320+
/**
321+
* Generate a dummy image of the given width and height.
322+
*
323+
* @param string $path
324+
* @param int $width
325+
* @param int $height
326+
* @return string
327+
*/
328+
private function generateImage(string $path, int $width = 1024, int $height = 768)
329+
{
330+
$dir = dirname($path);
331+
if (!file_exists($dir)) {
332+
mkdir($dir, 0777, true);
333+
}
334+
$file = fopen($path, 'wb');
335+
$filename = basename($path);
336+
ob_start();
337+
$image = imagecreatetruecolor($width, $height);
338+
switch (substr($filename, strrpos($filename, '.'))) {
339+
case '.jpeg':
340+
imagejpeg($image);
341+
break;
342+
case '.png':
343+
imagepng($image);
344+
break;
345+
}
346+
fwrite($file, ob_get_clean());
347+
return $path;
348+
}
263349
}

0 commit comments

Comments
 (0)