Skip to content

Commit 7297b00

Browse files
author
Stanislav Idolov
committed
magento-commerce/magento2ce#7218: Fixed tests
1 parent 4ad3e94 commit 7297b00

File tree

2 files changed

+185
-63
lines changed

2 files changed

+185
-63
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*
6+
*/
7+
namespace Magento\Cms\Model\Wysiwyg\Images;
8+
9+
use Magento\Cms\Model\Wysiwyg\Images\Storage\Collection;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\DataObject;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Driver\File;
15+
use Magento\Framework\Filesystem\DriverInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
18+
/**
19+
* Test methods of class Storage
20+
*
21+
* @SuppressWarnings(PHPMD.LongVariable)
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
25+
*/
26+
class GetThumbnailUrlsTest extends \PHPUnit\Framework\TestCase
27+
{
28+
private const MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH
29+
= 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders';
30+
31+
/**
32+
* @var \Magento\Framework\ObjectManagerInterface
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var Filesystem
38+
*/
39+
private $filesystem;
40+
41+
/**
42+
* @var Storage
43+
*/
44+
private $storage;
45+
46+
/**
47+
* @var DriverInterface
48+
*/
49+
private $driver;
50+
51+
/**
52+
* @var array
53+
*/
54+
private $origConfigValue;
55+
56+
/**
57+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
58+
*/
59+
private $mediaDirectory;
60+
61+
/**
62+
* @var string
63+
*/
64+
private $fullDirectoryPath;
65+
66+
/**
67+
* @var \Magento\Cms\Helper\Wysiwyg\Images
68+
*/
69+
private $imagesHelper;
70+
71+
/**
72+
* @inheritdoc
73+
*/
74+
protected function setUp(): void
75+
{
76+
$this->objectManager = Bootstrap::getObjectManager();
77+
$this->filesystem = $this->objectManager->get(Filesystem::class);
78+
$this->imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
79+
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
80+
$this->fullDirectoryPath = rtrim($this->imagesHelper->getStorageRoot(), '/')
81+
. '/MagentoCmsModelWysiwygImagesStorageTest';
82+
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
83+
$config = $this->objectManager->get(ScopeConfigInterface::class);
84+
$this->origConfigValue = $config->getValue(
85+
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
86+
'default'
87+
);
88+
$scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
89+
$scopeConfig->setValue(
90+
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
91+
array_merge($this->origConfigValue, ['MagentoCmsModelWysiwygImagesStorageTest']),
92+
);
93+
$this->storage = $this->objectManager->create(Storage::class);
94+
$this->driver = $this->mediaDirectory->getDriver();
95+
}
96+
97+
protected function tearDown(): void
98+
{
99+
$this->mediaDirectory->delete($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
100+
$scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
101+
$scopeConfig->setValue(
102+
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
103+
$this->origConfigValue
104+
);
105+
}
106+
107+
/**
108+
* Test that getThumbnailUrl() returns correct URL for root folder or sub-folders images
109+
*
110+
* @param string $directory
111+
* @param string $filename
112+
* @param array $expectedUrls
113+
* @return void
114+
* @magentoAppIsolation enabled
115+
* @magentoAppArea adminhtml
116+
* @dataProvider getThumbnailUrlDataProvider
117+
*/
118+
public function testGetThumbnailUrl(string $directory, string $filename, array $expectedUrls): void
119+
{
120+
$root = $this->storage->getCmsWysiwygImages()->getStorageRoot();
121+
$directory = implode('/', array_filter([rtrim($root, '/'), trim($directory, '/')]));
122+
$path = $directory . '/' . $filename;
123+
$this->generateImage($path);
124+
$this->storage->resizeFile($path);
125+
$collection = $this->storage->getFilesCollection($directory, 'image');
126+
$paths = [];
127+
foreach ($collection as $item) {
128+
$paths[] = parse_url($item->getThumbUrl(), PHP_URL_PATH);
129+
}
130+
$this->assertEquals($expectedUrls, $paths);
131+
$this->driver->deleteFile($path);
132+
}
133+
134+
/**
135+
* Provide scenarios for testing getThumbnailUrl()
136+
*
137+
* @return array
138+
*/
139+
public function getThumbnailUrlDataProvider(): array
140+
{
141+
return [
142+
[
143+
'/',
144+
'image1.png',
145+
[]
146+
],
147+
[
148+
'/cms',
149+
'image2.png',
150+
[]
151+
],
152+
[
153+
'/cms/pages',
154+
'image3.png',
155+
[]
156+
],
157+
[
158+
'/MagentoCmsModelWysiwygImagesStorageTest',
159+
'image2.png',
160+
['/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/image2.png']
161+
],
162+
[
163+
'/MagentoCmsModelWysiwygImagesStorageTest/pages',
164+
'image3.png',
165+
['/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/pages/image3.png']
166+
]
167+
];
168+
}
169+
170+
/**
171+
* Generate a dummy image of the given width and height.
172+
*
173+
* @param string $path
174+
* @return string
175+
*/
176+
private function generateImage(string $path)
177+
{
178+
$this->mediaDirectory->create(dirname($this->mediaDirectory->getRelativePath($path)));
179+
ob_start();
180+
$image = imagecreatetruecolor(1024, 768);
181+
imagepng($image);
182+
$this->driver->filePutContents($path, ob_get_clean());
183+
return $path;
184+
}
185+
}

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

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -312,33 +312,6 @@ public function testUploadFileWithWrongFile(): void
312312
// phpcs:enable
313313
}
314314

315-
/**
316-
* Test that getThumbnailUrl() returns correct URL for root folder or sub-folders images
317-
*
318-
* @param string $directory
319-
* @param string $filename
320-
* @param array $expectedUrls
321-
* @return void
322-
* @magentoAppIsolation enabled
323-
* @magentoAppArea adminhtml
324-
* @dataProvider getThumbnailUrlDataProvider
325-
*/
326-
public function testGetThumbnailUrl(string $directory, string $filename, array $expectedUrls): void
327-
{
328-
$root = $this->storage->getCmsWysiwygImages()->getStorageRoot();
329-
$directory = implode('/', array_filter([rtrim($root, '/'), trim($directory, '/')]));
330-
$path = $directory . '/' . $filename;
331-
$this->generateImage($path);
332-
$this->storage->resizeFile($path);
333-
$collection = $this->storage->getFilesCollection($directory, 'image');
334-
$paths = [];
335-
foreach ($collection as $item) {
336-
$paths[] = parse_url($item->getThumbUrl(), PHP_URL_PATH);
337-
}
338-
$this->assertEquals($expectedUrls, $paths);
339-
$this->driver->deleteFile($path);
340-
}
341-
342315
/**
343316
* Verify thumbnail generation for diferent sizes
344317
*
@@ -391,42 +364,6 @@ public function getThumbnailsSizes(): array
391364
];
392365
}
393366

394-
/**
395-
* Provide scenarios for testing getThumbnailUrl()
396-
*
397-
* @return array
398-
*/
399-
public function getThumbnailUrlDataProvider(): array
400-
{
401-
return [
402-
[
403-
'/',
404-
'image1.png',
405-
[]
406-
],
407-
[
408-
'/cms',
409-
'image2.png',
410-
[]
411-
],
412-
[
413-
'/cms/pages',
414-
'image3.png',
415-
[]
416-
],
417-
[
418-
'/MagentoCmsModelWysiwygImagesStorageTest',
419-
'image2.png',
420-
['/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/image2.png']
421-
],
422-
[
423-
'/MagentoCmsModelWysiwygImagesStorageTest/pages',
424-
'image3.png',
425-
['/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/pages/image3.png']
426-
]
427-
];
428-
}
429-
430367
/**
431368
* Generate a dummy image of the given width and height.
432369
*

0 commit comments

Comments
 (0)