Skip to content

Commit adc798e

Browse files
karyna-tandrewbess
authored andcommitted
CR fixes
1 parent b1e56f0 commit adc798e

File tree

8 files changed

+80
-138
lines changed

8 files changed

+80
-138
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,20 @@ public function execute()
9898
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
9999
);
100100

101-
if ($result === false) {
102-
throw new LocalizedException(__('Something went wrong while saving the file(s).'));
103-
}
104-
$this->_eventManager->dispatch(
105-
'catalog_product_gallery_upload_image_after',
106-
['result' => $result, 'action' => $this]
107-
);
101+
if (is_array($result)) {
102+
$this->_eventManager->dispatch(
103+
'catalog_product_gallery_upload_image_after',
104+
['result' => $result, 'action' => $this]
105+
);
108106

109-
unset($result['tmp_name']);
110-
unset($result['path']);
107+
unset($result['tmp_name']);
108+
unset($result['path']);
111109

112-
$result['url'] = $this->productMediaConfig->getTmpMediaUrl($result['file']);
113-
$result['file'] = $result['file'] . '.tmp';
110+
$result['url'] = $this->productMediaConfig->getTmpMediaUrl($result['file']);
111+
$result['file'] = $result['file'] . '.tmp';
112+
} else {
113+
$result = ['error' => 'Something went wrong while saving the file(s).'];
114+
}
114115
} catch (LocalizedException $e) {
115116
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
116117
} catch (\Throwable $e) {

app/code/Magento/CatalogImportExport/Model/Import/Uploader.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,16 @@ public function move($fileName, $renameFileOff = false)
201201
$destDir = $rootDirectory->getAbsolutePath($this->getDestDir());
202202
$result = $this->save($destDir);
203203

204-
if ($result) {
204+
if (\is_array($result)) {
205205
unset($result['path']);
206206
$result['name'] = self::getCorrectFileName($result['name']);
207-
}
208207

209-
// Directory and filename must be no more than 255 characters in length
210-
if (strlen($result['file']) > $this->maxFilenameLength) {
211-
throw new \LengthException(
212-
__('Filename is too long; must be %1 characters or less', $this->maxFilenameLength)
213-
);
208+
// Directory and filename must be no more than 255 characters in length
209+
if (strlen($result['file']) > $this->maxFilenameLength) {
210+
throw new \LengthException(
211+
__('Filename is too long; must be %1 characters or less', $this->maxFilenameLength)
212+
);
213+
}
214214
}
215215

216216
return $result;

app/code/Magento/Config/Model/Config/Backend/File.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public function __construct(
8585
*
8686
* @return $this
8787
* @throws LocalizedException
88-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8988
*/
9089
public function beforeSave()
9190
{
@@ -104,8 +103,8 @@ public function beforeSave()
104103
} catch (Exception $e) {
105104
throw new LocalizedException(__('%1', $e->getMessage()));
106105
}
107-
$filename = ($result !== false) ? $result['file'] : '';
108-
if ($filename) {
106+
if ($result !== false) {
107+
$filename = $result['file'];
109108
if ($this->_addWhetherScopeInfo()) {
110109
$filename = $this->_prependScopeInfo($filename);
111110
}

app/code/Magento/Downloadable/Helper/File.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ public function uploadFromTmp($tmpPath, Uploader $uploader)
8585
$uploader->setAllowRenameFiles(true);
8686
$uploader->setFilesDispersion(true);
8787
$absoluteTmpPath = $this->_mediaDirectory->getAbsolutePath($tmpPath);
88-
$result = $uploader->save($absoluteTmpPath) ?: [];
89-
unset($result['path']);
88+
$result = $uploader->save($absoluteTmpPath);
89+
if (is_array($result)) {
90+
unset($result['path']);
91+
}
9092

9193
return $result;
9294
}

app/code/Magento/Downloadable/Test/Unit/Helper/FileTest.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@ class FileTest extends TestCase
2525
private $file;
2626

2727
/**
28-
* Core file storage database
29-
*
3028
* @var Database|MockObject
3129
*/
3230
private $coreFileStorageDatabase;
3331

3432
/**
35-
* Filesystem object.
36-
*
3733
* @var \Magento\Framework\Filesystem|MockObject
3834
*/
3935
private $filesystem;
@@ -50,6 +46,14 @@ class FileTest extends TestCase
5046
*/
5147
private $appContext;
5248

49+
/**
50+
* @var Uploader|MockObject
51+
*/
52+
private $uploader;
53+
54+
/**
55+
* @inheritDoc
56+
*/
5357
protected function setUp(): void
5458
{
5559
$this->mediaDirectory = $this->getMockBuilder(WriteInterface::class)
@@ -91,21 +95,49 @@ protected function setUp(): void
9195
$this->coreFileStorageDatabase,
9296
$this->filesystem
9397
);
98+
99+
$this->uploader = $this->getMockBuilder(Uploader::class)
100+
->disableOriginalConstructor()
101+
->getMock();
94102
}
95103

104+
/**
105+
* @return void
106+
*/
96107
public function testUploadFromTmp()
97108
{
98-
$uploaderMock = $this->getMockBuilder(Uploader::class)
99-
->disableOriginalConstructor()
100-
->getMock();
101-
$uploaderMock->expects($this->once())->method('setAllowRenameFiles');
102-
$uploaderMock->expects($this->once())->method('setFilesDispersion');
109+
$this->uploader->expects($this->once())->method('setAllowRenameFiles');
110+
$this->uploader->expects($this->once())->method('setFilesDispersion');
103111
$this->mediaDirectory->expects($this->once())->method('getAbsolutePath')->willReturn('absPath');
104-
$uploaderMock->expects($this->once())->method('save')->with('absPath')
112+
$this->uploader->expects($this->once())->method('save')->with('absPath')
105113
->willReturn(['file' => 'file.jpg', 'path' => 'absPath']);
106114

107-
$result = $this->file->uploadFromTmp('tmpPath', $uploaderMock);
115+
$result = $this->file->uploadFromTmp('tmpPath', $this->uploader);
108116

109117
$this->assertArrayNotHasKey('path', $result);
110118
}
119+
120+
/**
121+
* @return void
122+
*/
123+
public function testUploadFromTmpSuccess(): void
124+
{
125+
$tmpPath = __DIR__;
126+
$data = ['path' => __DIR__ . DIRECTORY_SEPARATOR . 'media', 'file' => 'test_image.jpg'];
127+
$this->uploader->method('save')->willReturn($data);
128+
$result = $this->file->uploadFromTmp($tmpPath, $this->uploader);
129+
$this->assertEquals('test_image.jpg', $result['file']);
130+
$this->assertEquals(1, count($result));
131+
}
132+
133+
/**
134+
* @return void
135+
*/
136+
public function testUploadFromTmpFail(): void
137+
{
138+
$tmpPath = __DIR__;
139+
$this->uploader->expects($this->once())->method('save')->willReturn(false);
140+
$result = $this->file->uploadFromTmp($tmpPath, $this->uploader);
141+
$this->assertEquals(false, $result);
142+
}
111143
}

app/code/Magento/Theme/Model/Wysiwyg/Storage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ public function getFilesCollection()
298298
if (self::TYPE_IMAGE == $storageType) {
299299
$requestParams['file'] = $fileName;
300300
$file['thumbnailParams'] = $requestParams;
301-
//phpcs:ignore Generic.PHP.NoSilencedErrors
302-
$size = @getimagesizefromstring($path);
301+
// phpcs:ignore Generic.PHP.NoSilencedErrors, Magento2.Functions.DiscouragedFunction
302+
$size = @getimagesize($path);
303303
if (is_array($size)) {
304304
$file['width'] = $size[0];
305305
$file['height'] = $size[1];

dev/tests/integration/testsuite/Magento/Downloadable/Helper/FileTest.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/Downloadable/Model/File/ContentUploaderTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Downloadable\Model\File;
99

10+
use Magento\Downloadable\Api\Data\File\ContentInterface;
1011
use Magento\Downloadable\Api\Data\File\ContentInterfaceFactory;
1112
use Magento\Downloadable\Model\Link;
1213
use Magento\Downloadable\Model\Sample;
@@ -17,18 +18,19 @@
1718
use Magento\MediaStorage\Helper\File\Storage\Database;
1819
use Magento\MediaStorage\Model\File\Validator\NotProtectedExtension;
1920
use Magento\TestFramework\Helper\Bootstrap;
20-
use PHPUnit\Framework\MockObject\MockObject;
2121
use PHPUnit\Framework\TestCase;
2222

2323
/**
2424
* Integration test for \Magento\Downloadable\Model\File\ContentUploader class
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2527
*/
2628
class ContentUploaderTest extends TestCase
2729
{
2830
/**
29-
* @var MockObject
31+
* @var ContentInterface
3032
*/
31-
private $fileContentMock;
33+
private $fileContent;
3234

3335
/**
3436
* @var string
@@ -51,11 +53,11 @@ class ContentUploaderTest extends TestCase
5153
protected function setUp(): void
5254
{
5355
$this->objectManager = Bootstrap::getObjectManager();
54-
$this->fileContentMock = $this->objectManager->create(ContentInterfaceFactory::class)->create();
56+
$this->fileContent = $this->objectManager->create(ContentInterfaceFactory::class)->create();
5557
$fixtureDir = realpath(__DIR__ . '/../../_files');
5658
$this->filePath = $fixtureDir . DIRECTORY_SEPARATOR . 'test_image.jpg';
57-
$this->fileContentMock->setFileData(base64_encode(file_get_contents($this->filePath)));
58-
$this->fileContentMock->setName('test_image.jpg');
59+
$this->fileContent->setFileData(base64_encode(file_get_contents($this->filePath)));
60+
$this->fileContent->setName('test_image.jpg');
5961

6062
$database = $this->getMockBuilder(Database::class)
6163
->disableOriginalConstructor()
@@ -104,7 +106,7 @@ public function testUploadWithSuccessSave()
104106
{
105107
$data = ['path' => $this->filePath, 'file' => 'test_image.jpg'];
106108
$this->model->expects($this->once())->method('save')->willReturn($data);
107-
$result = $this->model->upload($this->fileContentMock, 'sample');
109+
$result = $this->model->upload($this->fileContent, 'sample');
108110
$this->assertIsArray($result);
109111
$this->assertArrayHasKey('status', $result);
110112
$this->assertArrayHasKey('name', $result);
@@ -113,6 +115,6 @@ public function testUploadWithSuccessSave()
113115
public function testUploadWithFalseSave()
114116
{
115117
$this->model->expects($this->once())->method('save')->willReturn(false);
116-
$this->assertFalse($this->model->upload($this->fileContentMock, 'sample'));
118+
$this->assertFalse($this->model->upload($this->fileContent, 'sample'));
117119
}
118120
}

0 commit comments

Comments
 (0)