Skip to content

Commit 1808301

Browse files
committed
Merge remote-tracking branch 'origin/MC-32593' into 2.4-develop-pr19
2 parents 1a77b70 + fc034eb commit 1808301

File tree

2 files changed

+80
-43
lines changed

2 files changed

+80
-43
lines changed

app/code/Magento/MediaStorage/App/Media.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Framework\AppInterface;
2121
use Magento\Framework\Filesystem;
2222
use Magento\Framework\Filesystem\Directory\WriteInterface;
23+
use Magento\Framework\Filesystem\Driver\File;
2324
use Magento\MediaStorage\Model\File\Storage\Config;
2425
use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
2526
use Magento\MediaStorage\Model\File\Storage\Response;
@@ -28,7 +29,7 @@
2829
use Magento\MediaStorage\Service\ImageResize;
2930

3031
/**
31-
* Media Storage
32+
* The class resize original images
3233
*
3334
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3435
*/
@@ -70,7 +71,12 @@ class Media implements AppInterface
7071
/**
7172
* @var WriteInterface
7273
*/
73-
private $directory;
74+
private $directoryPub;
75+
76+
/**
77+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
78+
*/
79+
private $directoryMedia;
7480

7581
/**
7682
* @var ConfigFactory
@@ -109,6 +115,7 @@ class Media implements AppInterface
109115
* @param PlaceholderFactory $placeholderFactory
110116
* @param State $state
111117
* @param ImageResize $imageResize
118+
* @param File $file
112119
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
113120
*/
114121
public function __construct(
@@ -122,15 +129,17 @@ public function __construct(
122129
Filesystem $filesystem,
123130
PlaceholderFactory $placeholderFactory,
124131
State $state,
125-
ImageResize $imageResize
132+
ImageResize $imageResize,
133+
File $file
126134
) {
127135
$this->response = $response;
128136
$this->isAllowed = $isAllowed;
129-
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::PUB);
137+
$this->directoryPub = $filesystem->getDirectoryWrite(DirectoryList::PUB);
138+
$this->directoryMedia = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
130139
$mediaDirectory = trim($mediaDirectory);
131140
if (!empty($mediaDirectory)) {
132141
// phpcs:ignore Magento2.Functions.DiscouragedFunction
133-
$this->mediaDirectoryPath = str_replace('\\', '/', realpath($mediaDirectory));
142+
$this->mediaDirectoryPath = str_replace('\\', '/', $file->getRealPath($mediaDirectory));
134143
}
135144
$this->configCacheFile = $configCacheFile;
136145
$this->relativeFileName = $relativeFileName;
@@ -151,7 +160,7 @@ public function launch(): ResponseInterface
151160
{
152161
$this->appState->setAreaCode(Area::AREA_GLOBAL);
153162

154-
if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) {
163+
if ($this->checkMediaDirectoryChanged()) {
155164
// Path to media directory changed or absent - update the config
156165
/** @var Config $config */
157166
$config = $this->configFactory->create(['cacheFile' => $this->configCacheFile]);
@@ -166,11 +175,11 @@ public function launch(): ResponseInterface
166175

167176
try {
168177
/** @var Synchronization $sync */
169-
$sync = $this->syncFactory->create(['directory' => $this->directory]);
178+
$sync = $this->syncFactory->create(['directory' => $this->directoryPub]);
170179
$sync->synchronize($this->relativeFileName);
171180
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
172-
if ($this->directory->isReadable($this->relativeFileName)) {
173-
$this->response->setFilePath($this->directory->getAbsolutePath($this->relativeFileName));
181+
if ($this->directoryPub->isReadable($this->relativeFileName)) {
182+
$this->response->setFilePath($this->directoryPub->getAbsolutePath($this->relativeFileName));
174183
} else {
175184
$this->setPlaceholderImage();
176185
}
@@ -182,7 +191,17 @@ public function launch(): ResponseInterface
182191
}
183192

184193
/**
185-
* Set Placeholder as a response
194+
* Check if media directory changed
195+
*
196+
* @return bool
197+
*/
198+
private function checkMediaDirectoryChanged(): bool
199+
{
200+
return rtrim($this->mediaDirectoryPath, '/') !== rtrim($this->directoryMedia->getAbsolutePath(), '/');
201+
}
202+
203+
/**
204+
* Set placeholder image into response
186205
*
187206
* @return void
188207
*/

app/code/Magento/MediaStorage/Test/Unit/App/MediaTest.php

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Filesystem;
1616
use Magento\Framework\Filesystem\Directory\Read;
1717
use Magento\Framework\Filesystem\Directory\WriteInterface;
18+
use Magento\Framework\Filesystem\DriverPool;
1819
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1920
use Magento\MediaStorage\App\Media;
2021
use Magento\MediaStorage\Model\File\Storage\Config;
@@ -74,7 +75,12 @@ class MediaTest extends TestCase
7475
/**
7576
* @var Read|MockObject
7677
*/
77-
private $directoryMock;
78+
private $directoryMediaMock;
79+
80+
/**
81+
* @var \Magento\Framework\Filesystem\Directory\Read|\PHPUnit_Framework_MockObject_MockObject
82+
*/
83+
private $directoryPubMock;
7884

7985
protected function setUp()
8086
{
@@ -96,12 +102,30 @@ protected function setUp()
96102
->will($this->returnValue($this->sync));
97103

98104
$this->filesystemMock = $this->createMock(Filesystem::class);
99-
$this->directoryMock = $this->getMockForAbstractClass(WriteInterface::class);
100-
105+
$this->directoryPubMock = $this->getMockForAbstractClass(
106+
WriteInterface::class,
107+
[],
108+
'',
109+
false,
110+
true,
111+
true,
112+
['isReadable', 'getAbsolutePath']
113+
);
114+
$this->directoryMediaMock = $this->getMockForAbstractClass(
115+
WriteInterface::class,
116+
[],
117+
'',
118+
false,
119+
true,
120+
true,
121+
['getAbsolutePath']
122+
);
101123
$this->filesystemMock->expects($this->any())
102124
->method('getDirectoryWrite')
103-
->with(DirectoryList::PUB)
104-
->will($this->returnValue($this->directoryMock));
125+
->willReturnMap([
126+
[DirectoryList::PUB, DriverPool::FILE, $this->directoryPubMock],
127+
[DirectoryList::MEDIA, DriverPool::FILE, $this->directoryMediaMock],
128+
]);
105129

106130
$this->responseMock = $this->createMock(Response::class);
107131
}
@@ -116,17 +140,17 @@ public function testProcessRequestCreatesConfigFileMediaDirectoryIsNotProvided()
116140
$this->mediaModel = $this->getMediaModel();
117141

118142
$filePath = '/absolute/path/to/test/file.png';
119-
$this->directoryMock->expects($this->any())
143+
$this->directoryMediaMock->expects($this->once())
144+
->method('getAbsolutePath')
145+
->with(null)
146+
->will($this->returnValue(self::MEDIA_DIRECTORY));
147+
$this->directoryPubMock->expects($this->once())
120148
->method('getAbsolutePath')
121-
->will($this->returnValueMap(
122-
[
123-
[null, self::MEDIA_DIRECTORY],
124-
[self::RELATIVE_FILE_PATH, $filePath],
125-
]
126-
));
149+
->with(self::RELATIVE_FILE_PATH)
150+
->will($this->returnValue($filePath));
127151
$this->configMock->expects($this->once())->method('save');
128152
$this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
129-
$this->directoryMock->expects($this->once())
153+
$this->directoryPubMock->expects($this->once())
130154
->method('isReadable')
131155
->with(self::RELATIVE_FILE_PATH)
132156
->will($this->returnValue(true));
@@ -140,18 +164,18 @@ public function testProcessRequestReturnsFileIfItsProperlySynchronized()
140164

141165
$filePath = '/absolute/path/to/test/file.png';
142166
$this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
143-
$this->directoryMock->expects($this->once())
167+
$this->directoryMediaMock->expects($this->once())
168+
->method('getAbsolutePath')
169+
->with(null)
170+
->will($this->returnValue(self::MEDIA_DIRECTORY));
171+
$this->directoryPubMock->expects($this->once())
144172
->method('isReadable')
145173
->with(self::RELATIVE_FILE_PATH)
146174
->will($this->returnValue(true));
147-
$this->directoryMock->expects($this->any())
175+
$this->directoryPubMock->expects($this->once())
148176
->method('getAbsolutePath')
149-
->will($this->returnValueMap(
150-
[
151-
[null, self::MEDIA_DIRECTORY],
152-
[self::RELATIVE_FILE_PATH, $filePath],
153-
]
154-
));
177+
->with(self::RELATIVE_FILE_PATH)
178+
->will($this->returnValue($filePath));
155179
$this->responseMock->expects($this->once())->method('setFilePath')->with($filePath);
156180
$this->assertSame($this->responseMock, $this->mediaModel->launch());
157181
}
@@ -161,11 +185,11 @@ public function testProcessRequestReturnsNotFoundIfFileIsNotSynchronized()
161185
$this->mediaModel = $this->getMediaModel();
162186

163187
$this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
164-
$this->directoryMock->expects($this->once())
188+
$this->directoryMediaMock->expects($this->once())
165189
->method('getAbsolutePath')
166-
->with()
190+
->with(null)
167191
->will($this->returnValue(self::MEDIA_DIRECTORY));
168-
$this->directoryMock->expects($this->once())
192+
$this->directoryPubMock->expects($this->once())
169193
->method('isReadable')
170194
->with(self::RELATIVE_FILE_PATH)
171195
->will($this->returnValue(false));
@@ -205,16 +229,10 @@ public function testCatchException($isDeveloper, $setBodyCalls)
205229
public function testExceptionWhenIsAllowedReturnsFalse()
206230
{
207231
$this->mediaModel = $this->getMediaModel(false);
208-
209-
$filePath = '/absolute/path/to/test/file.png';
210-
$this->directoryMock->expects($this->any())
232+
$this->directoryMediaMock->expects($this->once())
211233
->method('getAbsolutePath')
212-
->will($this->returnValueMap(
213-
[
214-
[null, self::MEDIA_DIRECTORY],
215-
[self::RELATIVE_FILE_PATH, $filePath],
216-
]
217-
));
234+
->with(null)
235+
->will($this->returnValue(self::MEDIA_DIRECTORY));
218236
$this->configMock->expects($this->once())->method('save');
219237

220238
$this->expectException(LogicException::class);

0 commit comments

Comments
 (0)