Skip to content

Commit 545311b

Browse files
committed
Merge remote-tracking branch 'tier4/ACP2E-3100' into T4-PR-07-02-2024
2 parents ccadc89 + 9c63d29 commit 545311b

File tree

4 files changed

+113
-22
lines changed

4 files changed

+113
-22
lines changed

app/code/Magento/Catalog/Model/View/Asset/Placeholder.php

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
namespace Magento\Catalog\Model\View\Asset;
88

99
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\DriverPool;
1014
use Magento\Framework\View\Asset\ContextInterface;
1115
use Magento\Framework\View\Asset\File\NotFoundException;
1216
use Magento\Framework\View\Asset\LocalInterface;
1317
use Magento\Framework\View\Asset\Repository;
18+
use Magento\Catalog\Model\Product\Media\ConfigInterface;
1419

1520
/**
1621
* A locally available image placeholder file asset that can be referred with a file type
@@ -53,28 +58,49 @@ class Placeholder implements LocalInterface
5358
*/
5459
private $scopeConfig;
5560

61+
/**
62+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
63+
*/
64+
private $directoryMedia;
65+
66+
/**
67+
* @var ConfigInterface
68+
*/
69+
private $mediaConfig;
70+
5671
/**
5772
* Placeholder constructor.
5873
*
5974
* @param ContextInterface $context
6075
* @param ScopeConfigInterface $scopeConfig
6176
* @param Repository $assetRepo
6277
* @param string $type
78+
* @param Filesystem|null $filesystem
79+
* @param ConfigInterface|null $mediaConfig
80+
*
6381
*/
6482
public function __construct(
6583
ContextInterface $context,
6684
ScopeConfigInterface $scopeConfig,
6785
Repository $assetRepo,
68-
$type
86+
$type,
87+
?Filesystem $filesystem = null,
88+
?ConfigInterface $mediaConfig = null
6989
) {
7090
$this->context = $context;
7191
$this->scopeConfig = $scopeConfig;
7292
$this->assetRepo = $assetRepo;
7393
$this->type = $type;
94+
$filesystem = $filesystem ?? ObjectManager::getInstance()->get(Filesystem::class);
95+
$this->mediaConfig = $mediaConfig ?? ObjectManager::getInstance()->get(ConfigInterface::class);
96+
$this->directoryMedia = $filesystem->getDirectoryWrite(
97+
DirectoryList::MEDIA,
98+
DriverPool::FILE
99+
);
74100
}
75101

76102
/**
77-
* {@inheritdoc}
103+
* @inheritdoc
78104
*/
79105
public function getUrl()
80106
{
@@ -88,20 +114,20 @@ public function getUrl()
88114
}
89115

90116
/**
91-
* {@inheritdoc}
117+
* @inheritdoc
92118
*/
93119
public function getContentType()
94120
{
95121
return $this->contentType;
96122
}
97123

98124
/**
99-
* {@inheritdoc}
125+
* @inheritdoc
100126
*/
101127
public function getPath()
102128
{
103129
if ($this->getFilePath() !== null) {
104-
$result = $this->getContext()->getPath()
130+
$result = $this->getLocalMediaPath()
105131
. DIRECTORY_SEPARATOR . $this->getModule()
106132
. DIRECTORY_SEPARATOR . $this->getFilePath();
107133
} else {
@@ -119,7 +145,35 @@ public function getPath()
119145
}
120146

121147
/**
122-
* {@inheritdoc}
148+
* Get path for local media
149+
*
150+
* @return string
151+
*/
152+
private function getLocalMediaPath()
153+
{
154+
return $this->directoryMedia->getAbsolutePath($this->mediaConfig->getBaseMediaPath());
155+
}
156+
157+
/**
158+
* Get relative placeholder path
159+
*
160+
* @return string|null
161+
*/
162+
public function getRelativePath()
163+
{
164+
$result = null;
165+
//will use system placeholder unless another specified in the config
166+
if ($this->getFilePath() !== null) {
167+
$result = DIRECTORY_SEPARATOR . DirectoryList::MEDIA
168+
. DIRECTORY_SEPARATOR . $this->directoryMedia->getRelativePath($this->mediaConfig->getBaseMediaPath())
169+
. DIRECTORY_SEPARATOR . $this->getModule()
170+
. DIRECTORY_SEPARATOR . $this->getFilePath();
171+
}
172+
return $result;
173+
}
174+
175+
/**
176+
* @inheritdoc
123177
*/
124178
public function getSourceFile()
125179
{
@@ -137,15 +191,15 @@ public function getSourceContentType()
137191
}
138192

139193
/**
140-
* {@inheritdoc}
194+
* @inheritdoc
141195
*/
142196
public function getContent()
143197
{
144198
return null;
145199
}
146200

147201
/**
148-
* {@inheritdoc}
202+
* @inheritdoc
149203
*/
150204
public function getFilePath()
151205
{
@@ -163,7 +217,8 @@ public function getFilePath()
163217
}
164218

165219
/**
166-
* {@inheritdoc}
220+
* @inheritdoc
221+
*
167222
* @return ContextInterface
168223
*/
169224
public function getContext()
@@ -172,7 +227,7 @@ public function getContext()
172227
}
173228

174229
/**
175-
* {@inheritdoc}
230+
* @inheritdoc
176231
*/
177232
public function getModule()
178233
{

app/code/Magento/Catalog/Test/Unit/Model/View/Asset/PlaceholderTest.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,40 @@
1515
use Magento\Store\Model\ScopeInterface;
1616
use PHPUnit\Framework\MockObject\MockObject;
1717
use PHPUnit\Framework\TestCase;
18+
use Magento\Framework\Filesystem;
19+
use Magento\Catalog\Model\Product\Media\ConfigInterface;
1820

1921
class PlaceholderTest extends TestCase
2022
{
2123
/**
2224
* @var Placeholder
2325
*/
24-
protected $model;
26+
private $model;
2527

2628
/**
2729
* @var ScopeConfigInterface|MockObject
2830
*/
29-
protected $scopeConfig;
31+
private $scopeConfig;
3032

3133
/**
3234
* @var Repository|MockObject
3335
*/
34-
protected $repository;
36+
private $repository;
3537

3638
/**
3739
* @var ContextInterface|MockObject
3840
*/
39-
protected $imageContext;
41+
private $imageContext;
42+
43+
/**
44+
* @var Filesystem|MockObject
45+
*/
46+
private $filesystem;
47+
48+
/**
49+
* @var ConfigInterface|MockObject
50+
*/
51+
private $mediaConfig;
4052

4153
protected function setUp(): void
4254
{
@@ -47,11 +59,18 @@ protected function setUp(): void
4759
$this->repository = $this->getMockBuilder(Repository::class)
4860
->disableOriginalConstructor()
4961
->getMock();
62+
$this->filesystem = $this->createMock(Filesystem::class);
63+
$this->filesystem->expects($this->any())
64+
->method('getDirectoryWrite')
65+
->willReturn($this->createMock(\Magento\Framework\Filesystem\Directory\WriteInterface::class));
66+
$this->mediaConfig = $this->createMock(ConfigInterface::class);
5067
$this->model = new Placeholder(
5168
$this->imageContext,
5269
$this->scopeConfig,
5370
$this->repository,
54-
'thumbnail'
71+
'thumbnail',
72+
$this->filesystem,
73+
$this->mediaConfig
5574
);
5675
}
5776

@@ -87,7 +106,9 @@ public function testGetPathAndGetSourceFile($imageType, $placeholderPath)
87106
$this->imageContext,
88107
$this->scopeConfig,
89108
$this->repository,
90-
$imageType
109+
$imageType,
110+
$this->filesystem,
111+
$this->mediaConfig
91112
);
92113
$absolutePath = '/var/www/html/magento2ce/pub/media/catalog/product';
93114

@@ -108,8 +129,7 @@ public function testGetPathAndGetSourceFile($imageType, $placeholderPath)
108129
$this->repository->expects($this->any())->method('createAsset')->willReturn($assetMock);
109130
} else {
110131
$this->imageContext->expects($this->any())->method('getPath')->willReturn($absolutePath);
111-
$expectedResult = $absolutePath
112-
. DIRECTORY_SEPARATOR . $imageModel->getModule()
132+
$expectedResult = DIRECTORY_SEPARATOR . $imageModel->getModule()
113133
. DIRECTORY_SEPARATOR . $placeholderPath;
114134
}
115135

@@ -128,7 +148,9 @@ public function testGetUrl($imageType, $placeholderPath)
128148
$this->imageContext,
129149
$this->scopeConfig,
130150
$this->repository,
131-
$imageType
151+
$imageType,
152+
$this->filesystem,
153+
$this->mediaConfig
132154
);
133155

134156
$this->scopeConfig->expects($this->any())

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ private function createLocalCopy(): void
227227
}
228228
}
229229

230+
/**
231+
* Create local vopy for the placeholder
232+
*
233+
* @param ?string $relativeFileName
234+
* @return void
235+
*/
236+
private function createPlaceholderLocalCopy(?string $relativeFileName): void
237+
{
238+
$synchronizer = $this->syncFactory->create(['directory' => $this->directoryMedia]);
239+
$synchronizer->synchronize($relativeFileName);
240+
}
241+
230242
/**
231243
* Check if media directory changed
232244
*
@@ -248,6 +260,7 @@ private function checkMediaDirectoryChanged(): bool
248260
private function setPlaceholderImage(): void
249261
{
250262
$placeholder = $this->placeholderFactory->create(['type' => 'image']);
263+
$this->createPlaceholderLocalCopy($placeholder->getRelativePath());
251264
$this->response->setFilePath($placeholder->getPath());
252265
}
253266

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ public function testProcessRequestReturnsNotFoundIfFileIsNotSynchronized(): void
175175
{
176176
$this->mediaModel = $this->createMediaModel();
177177

178-
$this->sync->expects(self::once())
179-
->method('synchronize')
178+
$this->sync->method('synchronize')
180179
->with(self::RELATIVE_FILE_PATH);
181180
$this->directoryMediaMock->expects(self::once())
182181
->method('getAbsolutePath')
@@ -270,9 +269,11 @@ protected function createMediaModel(bool $isAllowed = true): Media
270269

271270
$driverFile = $this->createMock(Filesystem\Driver\File::class);
272271
$driverFile->method('getRealPath')->willReturn('');
272+
$placeholder = $this->createMock(Placeholder::class);
273+
$placeholder->method('getRelativePath')->willReturn(self::RELATIVE_FILE_PATH);
273274
$placeholderFactory = $this->createMock(PlaceholderFactory::class);
274275
$placeholderFactory->method('create')
275-
->willReturn($this->createMock(Placeholder::class));
276+
->willReturn($placeholder);
276277

277278
return new Media(
278279
$this->configFactoryMock,

0 commit comments

Comments
 (0)