Skip to content

Commit 24d56dd

Browse files
committed
Merge remote-tracking branch 'origin/MC-18099' into 2.3.3-develop-pr58
2 parents 81fa344 + 0a7e677 commit 24d56dd

File tree

5 files changed

+265
-19
lines changed
  • app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/File
  • dev/tests
    • functional/tests/app/Magento/Checkout/Test/Block/Cart/Sidebar
    • integration/testsuite/Magento
  • lib/internal/Magento/Framework/File

5 files changed

+265
-19
lines changed

app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/File/Upload.php

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

88
use Magento\Framework\App\Action\HttpPostActionInterface;
99
use Magento\Framework\Controller\ResultFactory;
10+
use Magento\Framework\Exception\FileSystemException;
11+
use Magento\Framework\Exception\LocalizedException;
1012

1113
/**
1214
* Class Upload
@@ -76,23 +78,27 @@ public function __construct(
7678
*/
7779
public function execute()
7880
{
79-
$type = $this->getRequest()->getParam('type');
80-
$tmpPath = '';
81-
if ($type == 'samples') {
82-
$tmpPath = $this->_sample->getBaseTmpPath();
83-
} elseif ($type == 'links') {
84-
$tmpPath = $this->_link->getBaseTmpPath();
85-
} elseif ($type == 'link_samples') {
86-
$tmpPath = $this->_link->getBaseSampleTmpPath();
87-
}
88-
8981
try {
82+
$type = $this->getRequest()->getParam('type');
83+
$tmpPath = '';
84+
if ($type === 'samples') {
85+
$tmpPath = $this->_sample->getBaseTmpPath();
86+
} elseif ($type === 'links') {
87+
$tmpPath = $this->_link->getBaseTmpPath();
88+
} elseif ($type === 'link_samples') {
89+
$tmpPath = $this->_link->getBaseSampleTmpPath();
90+
} else {
91+
throw new LocalizedException(__('Upload type can not be determined.'));
92+
}
93+
9094
$uploader = $this->uploaderFactory->create(['fileId' => $type]);
9195

9296
$result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader);
9397

9498
if (!$result) {
95-
throw new \Exception('File can not be moved from temporary folder to the destination folder.');
99+
throw new FileSystemException(
100+
__('File can not be moved from temporary folder to the destination folder.')
101+
);
96102
}
97103

98104
unset($result['tmp_name'], $result['path']);
@@ -101,7 +107,7 @@ public function execute()
101107
$relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/');
102108
$this->storageDatabase->saveFile($relativePath);
103109
}
104-
} catch (\Exception $e) {
110+
} catch (\Throwable $e) {
105111
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
106112
}
107113

dev/tests/functional/tests/app/Magento/Checkout/Test/Block/Cart/Sidebar/Item.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Item extends Sidebar
6262
*/
6363
public function removeItemFromMiniCart()
6464
{
65+
$this->waitForDeleteButtonVisible();
6566
$this->_rootElement->find($this->removeItem)->click();
6667
$element = $this->browser->find($this->confirmModal);
6768
/** @var \Magento\Ui\Test\Block\Adminhtml\Modal $modal */
@@ -70,6 +71,23 @@ public function removeItemFromMiniCart()
7071
$modal->waitModalWindowToDisappear();
7172
}
7273

74+
/**
75+
* Wait for Delete button is visible in the block.
76+
*
77+
* @return bool|null
78+
*/
79+
private function waitForDeleteButtonVisible()
80+
{
81+
$rootElement = $this->_rootElement;
82+
$deleteButtonSelector = $this->removeItem;
83+
return $rootElement->waitUntil(
84+
function () use ($rootElement, $deleteButtonSelector) {
85+
$element = $rootElement->find($deleteButtonSelector);
86+
return $element->isVisible() ? true : null;
87+
}
88+
);
89+
}
90+
7391
/**
7492
* Click "Edit item" button.
7593
*

dev/tests/integration/testsuite/Magento/Downloadable/Controller/Adminhtml/Downloadable/FileTest.php

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
11
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
27
namespace Magento\Downloadable\Controller\Adminhtml\Downloadable;
38

49
use Magento\Framework\Serialize\Serializer\Json;
5-
use Magento\TestFramework\Helper\Bootstrap;
610

711
/**
812
* Magento\Downloadable\Controller\Adminhtml\Downloadable\File
913
*
10-
* Copyright © Magento, Inc. All rights reserved.
11-
* See COPYING.txt for license details.
1214
* @magentoAppArea adminhtml
1315
*/
1416
class FileTest extends \Magento\TestFramework\TestCase\AbstractBackendController
1517
{
18+
/**
19+
* @var Json
20+
*/
21+
private $jsonSerializer;
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected function setUp()
27+
{
28+
parent::setUp();
29+
30+
$this->jsonSerializer = $this->_objectManager->get(Json::class);
31+
}
32+
1633
/**
1734
* @inheritdoc
1835
*/
1936
protected function tearDown()
2037
{
38+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
2139
$filePath = dirname(__DIR__) . '/_files/sample.tmp';
40+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
2241
if (is_file($filePath)) {
42+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
2343
unlink($filePath);
2444
}
2545
}
2646

2747
public function testUploadAction()
2848
{
49+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
2950
copy(dirname(__DIR__) . '/_files/sample.txt', dirname(__DIR__) . '/_files/sample.tmp');
51+
// phpcs:ignore Magento2.Security.Superglobal
3052
$_FILES = [
3153
'samples' => [
3254
'name' => 'sample.txt',
3355
'type' => 'text/plain',
56+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
3457
'tmp_name' => dirname(__DIR__) . '/_files/sample.tmp',
3558
'error' => 0,
3659
'size' => 0,
@@ -40,7 +63,7 @@ public function testUploadAction()
4063
$this->getRequest()->setMethod('POST');
4164
$this->dispatch('backend/admin/downloadable_file/upload/type/samples');
4265
$body = $this->getResponse()->getBody();
43-
$result = Bootstrap::getObjectManager()->get(Json::class)->unserialize($body);
66+
$result = $this->jsonSerializer->unserialize($body);
4467
$this->assertEquals(0, $result['error']);
4568
}
4669

@@ -52,9 +75,11 @@ public function testUploadAction()
5275
*/
5376
public function testUploadProhibitedExtensions($fileName)
5477
{
78+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
5579
$path = dirname(__DIR__) . '/_files/';
80+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
5681
copy($path . 'sample.txt', $path . 'sample.tmp');
57-
82+
// phpcs:ignore Magento2.Security.Superglobal
5883
$_FILES = [
5984
'samples' => [
6085
'name' => $fileName,
@@ -68,7 +93,7 @@ public function testUploadProhibitedExtensions($fileName)
6893
$this->getRequest()->setMethod('POST');
6994
$this->dispatch('backend/admin/downloadable_file/upload/type/samples');
7095
$body = $this->getResponse()->getBody();
71-
$result = Bootstrap::getObjectManager()->get(Json::class)->unserialize($body);
96+
$result = $this->jsonSerializer->unserialize($body);
7297

7398
self::assertArrayHasKey('errorcode', $result);
7499
self::assertEquals(0, $result['errorcode']);
@@ -90,4 +115,37 @@ public function extensionsDataProvider()
90115
['sample.php7'],
91116
];
92117
}
118+
119+
/**
120+
* @dataProvider uploadWrongUploadTypeDataProvider
121+
* @return void
122+
*/
123+
public function testUploadWrongUploadType($postData): void
124+
{
125+
$this->getRequest()->setPostValue($postData);
126+
$this->getRequest()->setMethod('POST');
127+
128+
$this->dispatch('backend/admin/downloadable_file/upload');
129+
130+
$body = $this->getResponse()->getBody();
131+
$result = $this->jsonSerializer->unserialize($body);
132+
$this->assertEquals('Upload type can not be determined.', $result['error']);
133+
$this->assertEquals(0, $result['errorcode']);
134+
}
135+
136+
public function uploadWrongUploadTypeDataProvider(): array
137+
{
138+
return [
139+
[
140+
['type' => 'test'],
141+
],
142+
[
143+
[
144+
'type' => [
145+
'type1' => 'test',
146+
],
147+
],
148+
],
149+
];
150+
}
93151
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\File;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
12+
/**
13+
* Test for \Magento\Framework\File\Uploader
14+
*/
15+
class UploaderTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* @var \Magento\MediaStorage\Model\File\UploaderFactory
19+
*/
20+
private $uploaderFactory;
21+
22+
/**
23+
* @var \Magento\Framework\Filesystem
24+
*/
25+
private $filesystem;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function setUp()
31+
{
32+
$this->uploaderFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
33+
->get(\Magento\MediaStorage\Model\File\UploaderFactory::class);
34+
35+
$this->filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
36+
->get(\Magento\Framework\Filesystem::class);
37+
}
38+
39+
/**
40+
* @return void
41+
*/
42+
public function testUploadFileFromAllowedFolder(): void
43+
{
44+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
45+
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
46+
47+
$fileName = 'text.txt';
48+
$tmpDir = 'tmp';
49+
$filePath = $tmpDirectory->getAbsolutePath($fileName);
50+
51+
$tmpDirectory->writeFile($fileName, 'just a text');
52+
53+
$type = [
54+
'tmp_name' => $filePath,
55+
'name' => $fileName,
56+
];
57+
58+
$uploader = $this->uploaderFactory->create(['fileId' => $type]);
59+
$uploader->save($mediaDirectory->getAbsolutePath($tmpDir));
60+
61+
$this->assertTrue($mediaDirectory->isFile($tmpDir . DIRECTORY_SEPARATOR . $fileName));
62+
}
63+
64+
/**
65+
* @expectedException \InvalidArgumentException
66+
* @expectedExceptionMessage Invalid parameter given. A valid $fileId[tmp_name] is expected.
67+
*
68+
* @return void
69+
*/
70+
public function testUploadFileFromNotAllowedFolder(): void
71+
{
72+
$fileName = 'text.txt';
73+
$tmpDir = 'tmp';
74+
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::LOG);
75+
$filePath = $tmpDirectory->getAbsolutePath() . $tmpDir . DIRECTORY_SEPARATOR . $fileName;
76+
77+
$tmpDirectory->writeFile($tmpDir . DIRECTORY_SEPARATOR . $fileName, 'just a text');
78+
79+
$type = [
80+
'tmp_name' => $filePath,
81+
'name' => $fileName,
82+
];
83+
84+
$this->uploaderFactory->create(['fileId' => $type]);
85+
}
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
protected function tearDown()
91+
{
92+
parent::tearDown();
93+
94+
$tmpDir = 'tmp';
95+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
96+
$mediaDirectory->delete($tmpDir);
97+
98+
$logDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::LOG);
99+
$logDirectory->delete($tmpDir);
100+
}
101+
}

0 commit comments

Comments
 (0)