Skip to content

Commit 73dfa18

Browse files
committed
Merge branch 'B2B-2049' into B2B-2069
2 parents 9a132c5 + 45a2af6 commit 73dfa18

File tree

3 files changed

+114
-82
lines changed
  • app/code/Magento/Customer
  • dev/tests/integration/testsuite/Magento/Customer/Model/Metadata/Form

3 files changed

+114
-82
lines changed

app/code/Magento/Customer/Model/Metadata/Form/Image.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Framework\Exception\LocalizedException;
2323
use Magento\Framework\File\UploaderFactory;
2424
use Magento\Framework\Filesystem;
25+
use Magento\Framework\Filesystem\Directory\ReadInterface;
2526
use Magento\Framework\Filesystem\Directory\WriteFactory;
2627
use Magento\Framework\Filesystem\Directory\WriteInterface;
2728
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
@@ -48,10 +49,15 @@ class Image extends File
4849
*/
4950
private $ioFileSystem;
5051

52+
/**
53+
* @var ReadInterface
54+
*/
55+
private $mediaEntityTmpReadDirectory;
56+
5157
/**
5258
* @var WriteInterface
5359
*/
54-
private $mediaEntityTmpDirectory;
60+
private $mediaWriteDirectory;
5561

5662
/**
5763
* @param TimezoneInterface $localeDate
@@ -71,6 +77,7 @@ class Image extends File
7177
* @param DirectoryList|null $directoryList
7278
* @param WriteFactory|null $writeFactory
7379
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
80+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7481
* @throws FileSystemException
7582
*/
7683
public function __construct(
@@ -109,12 +116,10 @@ public function __construct(
109116
->get(ImageContentInterfaceFactory::class);
110117
$this->ioFileSystem = $ioFileSystem ?: ObjectManager::getInstance()
111118
->get(IoFileSystem::class);
112-
$writeFactory = $writeFactory ?? ObjectManager::getInstance()->get(WriteFactory::class);
113-
$directoryList = $directoryList ?? ObjectManager::getInstance()->get(DirectoryList::class);
114-
$this->mediaEntityTmpDirectory = $writeFactory->create(
115-
$directoryList->getPath($directoryList::MEDIA)
116-
. '/' . $this->_entityTypeCode
117-
. '/' . FileProcessor::TMP_DIR
119+
$this->mediaWriteDirectory = $fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
120+
$this->mediaEntityTmpReadDirectory = $fileSystem->getDirectoryReadByPath(
121+
$this->mediaWriteDirectory->getAbsolutePath() . $this->_entityTypeCode
122+
. DIRECTORY_SEPARATOR . FileProcessor::TMP_DIR . DIRECTORY_SEPARATOR
118123
);
119124
}
120125

@@ -224,18 +229,18 @@ protected function processUiComponentValue(array $value)
224229
*/
225230
protected function processCustomerAddressValue(array $value)
226231
{
227-
$fileName = $this->mediaEntityTmpDirectory
232+
$fileName = $this->mediaWriteDirectory
228233
->getDriver()
229234
->getRealPathSafety(
230-
$this->mediaEntityTmpDirectory->getAbsolutePath(
235+
$this->mediaEntityTmpReadDirectory->getAbsolutePath(
231236
ltrim(
232237
$value['file'],
233238
'/'
234239
)
235240
)
236241
);
237242
return $this->getFileProcessor()->moveTemporaryFile(
238-
$this->mediaEntityTmpDirectory->getRelativePath($fileName)
243+
$this->mediaEntityTmpReadDirectory->getRelativePath($fileName)
239244
);
240245
}
241246

@@ -249,7 +254,7 @@ protected function processCustomerAddressValue(array $value)
249254
protected function processCustomerValue(array $value)
250255
{
251256
$file = ltrim($value['file'], '/');
252-
if ($this->mediaEntityTmpDirectory->isExist($file)) {
257+
if ($this->mediaEntityTmpReadDirectory->isExist($file)) {
253258
$temporaryFile = FileProcessor::TMP_DIR . '/' . $file;
254259
$base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
255260
/** @var ImageContentInterface $imageContentDataObject */

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/ImageTest.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
use Magento\Customer\Model\Metadata\Form\Image;
1616
use Magento\Framework\Api\Data\ImageContentInterface;
1717
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
18-
use Magento\Framework\App\Filesystem\DirectoryList;
1918
use Magento\Framework\App\Request\Http;
2019
use Magento\Framework\Exception\FileSystemException;
2120
use Magento\Framework\Exception\LocalizedException;
2221
use Magento\Framework\File\UploaderFactory;
2322
use Magento\Framework\Filesystem;
24-
use Magento\Framework\Filesystem\Directory\WriteFactory;
23+
use Magento\Framework\Filesystem\Directory\ReadInterface;
2524
use Magento\Framework\Filesystem\Directory\Write;
2625
use Magento\Framework\Filesystem\Driver\File as Driver;
2726
use Magento\Framework\Filesystem\Io\File;
@@ -82,19 +81,14 @@ class ImageTest extends AbstractFormTestCase
8281
private $ioFileSystemMock;
8382

8483
/**
85-
* @var DirectoryList|\PHPUnit\Framework\MockObject\MockObject
84+
* @var ReadInterface|\PHPUnit\Framework\MockObject\MockObject
8685
*/
87-
private $directoryListMock;
88-
89-
/**
90-
* @var WriteFactory|\PHPUnit\Framework\MockObject\MockObject
91-
*/
92-
private $writeFactoryMock;
86+
private $readDirectoryMock;
9387

9488
/**
9589
* @var Write|\PHPUnit\Framework\MockObject\MockObject
9690
*/
97-
private $mediaEntityTmpDirectoryMock;
91+
private $mediaWriteDirectoryMock;
9892

9993
/**
10094
* @var Driver|\PHPUnit\Framework\MockObject\MockObject
@@ -140,23 +134,24 @@ protected function setUp(): void
140134
$this->ioFileSystemMock = $this->getMockBuilder(File::class)
141135
->disableOriginalConstructor()
142136
->getMock();
143-
$this->directoryListMock = $this->getMockBuilder(DirectoryList::class)
144-
->disableOriginalConstructor()
145-
->getMock();
146-
$this->writeFactoryMock = $this->getMockBuilder(WriteFactory::class)
147-
->setMethods(['create'])
148-
->disableOriginalConstructor()
149-
->getMock();
150-
$this->mediaEntityTmpDirectoryMock = $this->getMockBuilder(Write::class)
137+
138+
$this->readDirectoryMock = $this->getMockBuilder(ReadInterface::class)
139+
->getMockForAbstractClass();
140+
$this->fileSystemMock->expects($this->once())
141+
->method('getDirectoryReadByPath')
142+
->willReturn($this->readDirectoryMock);
143+
144+
$this->mediaWriteDirectoryMock = $this->getMockBuilder(Write::class)
151145
->disableOriginalConstructor()
152146
->getMock();
147+
$this->fileSystemMock->expects($this->once())
148+
->method('getDirectoryWrite')
149+
->willReturn($this->mediaWriteDirectoryMock);
150+
153151
$this->driverMock = $this->getMockBuilder(Driver::class)
154152
->disableOriginalConstructor()
155153
->getMock();
156-
$this->writeFactoryMock->expects($this->any())
157-
->method('create')
158-
->willReturn($this->mediaEntityTmpDirectoryMock);
159-
$this->mediaEntityTmpDirectoryMock->expects($this->any())
154+
$this->mediaWriteDirectoryMock->expects($this->any())
160155
->method('getDriver')
161156
->willReturn($this->driverMock);
162157
}
@@ -184,9 +179,7 @@ private function initialize(array $data): Image
184179
$this->uploaderFactoryMock,
185180
$this->fileProcessorFactoryMock,
186181
$this->imageContentFactory,
187-
$this->ioFileSystemMock,
188-
$this->directoryListMock,
189-
$this->writeFactoryMock
182+
$this->ioFileSystemMock
190183
);
191184
}
192185

@@ -470,10 +463,10 @@ public function testCompactValueUiComponentAddress()
470463
->method('getRealPathSafety')
471464
->with($value['file'])
472465
->willReturn($value['file']);
473-
$this->mediaEntityTmpDirectoryMock->expects($this->once())
466+
$this->readDirectoryMock->expects($this->once())
474467
->method('getAbsolutePath')
475468
->willReturn($value['file']);
476-
$this->mediaEntityTmpDirectoryMock->expects($this->once())
469+
$this->readDirectoryMock->expects($this->once())
477470
->method('getRelativePath')
478471
->willReturn($value['file']);
479472
$this->fileProcessorMock->expects($this->once())
@@ -505,7 +498,7 @@ public function testCompactValueUiComponentCustomer()
505498

506499
$base64EncodedData = 'encoded_data';
507500

508-
$this->mediaEntityTmpDirectoryMock->expects($this->once())
501+
$this->readDirectoryMock->expects($this->once())
509502
->method('isExist')
510503
->with($value['file'])
511504
->willReturn(true);
@@ -561,7 +554,7 @@ public function testCompactValueUiComponentCustomerNotExists()
561554
'type' => 'image',
562555
];
563556

564-
$this->mediaEntityTmpDirectoryMock->expects($this->once())
557+
$this->readDirectoryMock->expects($this->once())
565558
->method('isExist')
566559
->with($value['file'])
567560
->willReturn(false);

dev/tests/integration/testsuite/Magento/Customer/Model/Metadata/Form/ImageTest.php

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,10 @@ public function setUp(): void
7373
*/
7474
public function testProcessCustomerAddressValue()
7575
{
76-
$this->mediaDirectory->delete('customer_address');
77-
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath('customer_address/tmp/'));
78-
$tmpFilePath = $this->mediaDirectory->getAbsolutePath('customer_address/tmp/' . $this->fileName);
79-
$this->copyFile($this->imageFixtureDir . DIRECTORY_SEPARATOR . $this->fileName, $tmpFilePath);
76+
$entityTypeCode = 'customer_address';
77+
$tmpFilePath = $this->prepareImageForTest($entityTypeCode);
8078

81-
$imageFile = [
82-
'name' => $this->fileName,
83-
'type' => 'image/jpeg',
84-
'tmp_name' => $this->fileName,
85-
'file' => $this->fileName,
86-
'error' => 0,
87-
'size' => 12500,
88-
'previewType' => 'image',
89-
];
79+
$imageFile = $this->getImageValues();
9080

9181
$params = [
9282
'entityTypeCode' => 'customer_address',
@@ -119,23 +109,12 @@ public function testProcessCustomerAddressValue()
119109
*/
120110
public function testProcessCustomerValue()
121111
{
122-
$this->mediaDirectory->delete('customer');
123-
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath('customer/tmp/'));
124-
$tmpFilePath = $this->mediaDirectory->getAbsolutePath('customer/tmp/' . $this->fileName);
125-
$this->copyFile($this->imageFixtureDir . DIRECTORY_SEPARATOR . $this->fileName, $tmpFilePath);
126-
127-
$imageFile = [
128-
'name' => $this->fileName,
129-
'type' => 'image/jpeg',
130-
'tmp_name' => $this->fileName,
131-
'file' => $this->fileName,
132-
'error' => 0,
133-
'size' => 12500,
134-
'previewType' => 'image',
135-
];
112+
$entityTypeCode = 'customer';
113+
$tmpFilePath = $this->prepareImageForTest($entityTypeCode);
114+
$imageFile = $this->getImageValues();
136115

137116
$params = [
138-
'entityTypeCode' => 'customer',
117+
'entityTypeCode' => $entityTypeCode,
139118
'formCode' => 'customer_edit',
140119
'isAjax' => false,
141120
'value' => $imageFile
@@ -167,23 +146,14 @@ public function testProcessCustomerInvalidValue()
167146
\Magento\Framework\Exception\ValidatorException::class
168147
);
169148

170-
$this->mediaDirectory->delete('customer');
171-
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath('customer/tmp/'));
172-
$tmpFilePath = $this->mediaDirectory->getAbsolutePath('customer/tmp/' . $this->fileName);
173-
$this->copyFile($this->imageFixtureDir . DIRECTORY_SEPARATOR . $this->fileName, $tmpFilePath);
149+
$entityTypeCode = 'customer';
150+
$this->prepareImageForTest($entityTypeCode);
174151

175-
$imageFile = [
176-
'name' => $this->fileName,
177-
'type' => 'image/jpeg',
178-
'tmp_name' => $this->fileName,
179-
'file' => $this->invalidFileName,
180-
'error' => 0,
181-
'size' => 12500,
182-
'previewType' => 'image',
183-
];
152+
$imageFile = $this->getImageValues();
153+
$imageFile['file'] = $this->invalidFileName;
184154

185155
$params = [
186-
'entityTypeCode' => 'customer',
156+
'entityTypeCode' => $entityTypeCode,
187157
'formCode' => 'customer_edit',
188158
'isAjax' => false,
189159
'value' => $imageFile
@@ -199,6 +169,36 @@ public function testProcessCustomerInvalidValue()
199169
$processCustomerAddressValueMethod->invoke($image, $imageFile);
200170
}
201171

172+
/**
173+
* Test for _validateByRules method with not existed file
174+
*
175+
* @magentoAppIsolation enabled
176+
*
177+
* @throws FileSystemException
178+
* @throws \ReflectionException
179+
*/
180+
public function testValidateByRulesWithNotValidFile()
181+
{
182+
$entityTypeCode = 'customer';
183+
$this->mediaDirectory->delete($entityTypeCode);
184+
$imageFile = $this->getImageValues();
185+
$params = [
186+
'entityTypeCode' => $entityTypeCode,
187+
'formCode' => 'customer_edit',
188+
'isAjax' => false,
189+
'value' => $imageFile
190+
];
191+
192+
$image = $this->objectManager->create(\Magento\Customer\Model\Metadata\Form\Image::class, $params);
193+
$processValidateMethod = new \ReflectionMethod(
194+
\Magento\Customer\Model\Metadata\Form\Image::class,
195+
'_validateByRules'
196+
);
197+
$processValidateMethod->setAccessible(true);
198+
$validationResult = $processValidateMethod->invoke($image, $imageFile);
199+
$this->assertEquals('"' . $this->fileName .'" is not a valid file.', $validationResult[0]->__toString());
200+
}
201+
202202
/**
203203
* @inheritdoc
204204
* @throws FileSystemException
@@ -226,4 +226,38 @@ private function copyFile(string $source, string $destination)
226226
$driver->createDirectory(dirname($destination));
227227
$driver->filePutContents($destination, file_get_contents($source));
228228
}
229+
230+
/**
231+
* Returns image values
232+
*
233+
* @return array
234+
*/
235+
private function getImageValues(): array
236+
{
237+
return [
238+
'name' => $this->fileName,
239+
'type' => 'image/jpeg',
240+
'tmp_name' => $this->fileName,
241+
'file' => $this->fileName,
242+
'error' => 0,
243+
'size' => 12500,
244+
'previewType' => 'image',
245+
];
246+
}
247+
248+
/**
249+
* Copies image from fixture to necessary for test dir
250+
*
251+
* @param string $entityTypeCode
252+
* @return string
253+
* @throws FileSystemException
254+
*/
255+
private function prepareImageForTest(string $entityTypeCode): string
256+
{
257+
$this->mediaDirectory->delete($entityTypeCode);
258+
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($entityTypeCode . '/tmp/'));
259+
$tmpFilePath = $this->mediaDirectory->getAbsolutePath($entityTypeCode . '/tmp/' . $this->fileName);
260+
$this->copyFile($this->imageFixtureDir . DIRECTORY_SEPARATOR . $this->fileName, $tmpFilePath);
261+
return $tmpFilePath;
262+
}
229263
}

0 commit comments

Comments
 (0)