Skip to content

Commit 04c6599

Browse files
committed
Merge remote-tracking branch 'remotes/mainline/2.3.3-develop' into MC-15972-squashed
2 parents 61b0be7 + 7d30371 commit 04c6599

File tree

6 files changed

+174
-71
lines changed

6 files changed

+174
-71
lines changed

app/code/Magento/Email/Block/Adminhtml/Template/Preview.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,26 @@ public function __construct(
5555
* Prepare html output
5656
*
5757
* @return string
58+
* @throws \Magento\Framework\Exception\LocalizedException
5859
*/
5960
protected function _toHtml()
6061
{
62+
$request = $this->getRequest();
63+
64+
if (!$request instanceof \Magento\Framework\App\RequestSafetyInterface || !$request->isSafeMethod()) {
65+
throw new \Magento\Framework\Exception\LocalizedException(__('Wrong request.'));
66+
}
67+
6168
$storeId = $this->getAnyStoreView()->getId();
6269
/** @var $template \Magento\Email\Model\Template */
6370
$template = $this->_emailFactory->create();
6471

65-
if ($id = (int)$this->getRequest()->getParam('id')) {
72+
if ($id = (int)$request->getParam('id')) {
6673
$template->load($id);
6774
} else {
68-
$template->setTemplateType($this->getRequest()->getParam('type'));
69-
$template->setTemplateText($this->getRequest()->getParam('text'));
70-
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
75+
$template->setTemplateType($request->getParam('type'));
76+
$template->setTemplateText($request->getParam('text'));
77+
$template->setTemplateStyles($request->getParam('styles'));
7178
}
7279

7380
\Magento\Framework\Profiler::start($this->profilerName);

app/code/Magento/Email/Test/Unit/Block/Adminhtml/Template/PreviewTest.php

Lines changed: 124 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,105 +18,175 @@ class PreviewTest extends \PHPUnit\Framework\TestCase
1818

1919
const MALICIOUS_TEXT = 'test malicious';
2020

21+
/**
22+
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $request;
25+
26+
/**
27+
* @var \Magento\Email\Block\Adminhtml\Template\Preview
28+
*/
29+
protected $preview;
30+
31+
/**
32+
* @var \Magento\Framework\Filter\Input\MaliciousCode|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $maliciousCode;
35+
36+
/**
37+
* @var \Magento\Email\Model\Template|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $template;
40+
41+
/**
42+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $storeManager;
45+
2146
/**
2247
* Init data
2348
*/
2449
protected function setUp()
2550
{
2651
$this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
27-
}
2852

29-
/**
30-
* Check of processing email templates
31-
*
32-
* @param array $requestParamMap
33-
*
34-
* @dataProvider toHtmlDataProvider
35-
* @param $requestParamMap
36-
*/
37-
public function testToHtml($requestParamMap)
38-
{
3953
$storeId = 1;
40-
$template = $this->getMockBuilder(\Magento\Email\Model\Template::class)
41-
->setMethods([
42-
'setDesignConfig',
43-
'getDesignConfig',
44-
'__wakeup',
45-
'getProcessedTemplate',
46-
'getAppState',
47-
'revertDesign'
48-
])
54+
$designConfigData = [];
55+
56+
$this->template = $this->getMockBuilder(\Magento\Email\Model\Template::class)
57+
->setMethods(
58+
[
59+
'setDesignConfig',
60+
'getDesignConfig',
61+
'__wakeup',
62+
'getProcessedTemplate',
63+
'getAppState',
64+
'revertDesign'
65+
]
66+
)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
70+
$this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
4971
->disableOriginalConstructor()
5072
->getMock();
51-
$template->expects($this->once())
73+
74+
$this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
75+
76+
$this->maliciousCode = $this->createPartialMock(
77+
\Magento\Framework\Filter\Input\MaliciousCode::class,
78+
['filter']
79+
);
80+
81+
$this->template->expects($this->once())
5282
->method('getProcessedTemplate')
5383
->with($this->equalTo([]))
5484
->willReturn(self::MALICIOUS_TEXT);
55-
$designConfigData = [];
56-
$template->expects($this->atLeastOnce())
57-
->method('getDesignConfig')
58-
->willReturn(new \Magento\Framework\DataObject(
59-
$designConfigData
60-
));
85+
86+
$this->template->method('getDesignConfig')
87+
->willReturn(new \Magento\Framework\DataObject($designConfigData));
88+
6189
$emailFactory = $this->createPartialMock(\Magento\Email\Model\TemplateFactory::class, ['create']);
6290
$emailFactory->expects($this->any())
6391
->method('create')
64-
->willReturn($template);
92+
->willReturn($this->template);
6593

66-
$request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
67-
$request->expects($this->any())->method('getParam')->willReturnMap($requestParamMap);
6894
$eventManage = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
6995
$scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
7096
$design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
7197
$store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId', '__wakeup']);
72-
$store->expects($this->any())->method('getId')->willReturn($storeId);
73-
$storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
74-
->disableOriginalConstructor()
75-
->getMock();
76-
$storeManager->expects($this->atLeastOnce())
77-
->method('getDefaultStoreView')
98+
99+
$store->expects($this->any())
100+
->method('getId')
101+
->willReturn($storeId);
102+
103+
$this->storeManager->method('getDefaultStoreView')
78104
->willReturn($store);
79-
$storeManager->expects($this->any())->method('getDefaultStoreView')->willReturn(null);
80-
$storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
105+
106+
$this->storeManager->expects($this->any())->method('getDefaultStoreView')->willReturn(null);
107+
$this->storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
81108
$appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
82-
->setConstructorArgs([
83-
$scopeConfig
84-
])
109+
->setConstructorArgs(
110+
[
111+
$scopeConfig
112+
]
113+
)
85114
->setMethods(['emulateAreaCode'])
86115
->disableOriginalConstructor()
87116
->getMock();
88117
$appState->expects($this->any())
89118
->method('emulateAreaCode')
90-
->with(\Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA, [$template, 'getProcessedTemplate'])
91-
->willReturn($template->getProcessedTemplate());
119+
->with(
120+
\Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA,
121+
[$this->template, 'getProcessedTemplate']
122+
)
123+
->willReturn($this->template->getProcessedTemplate());
92124

93125
$context = $this->createPartialMock(
94126
\Magento\Backend\Block\Template\Context::class,
95127
['getRequest', 'getEventManager', 'getScopeConfig', 'getDesignPackage', 'getStoreManager', 'getAppState']
96128
);
97-
$context->expects($this->any())->method('getRequest')->willReturn($request);
129+
$context->expects($this->any())->method('getRequest')->willReturn($this->request);
98130
$context->expects($this->any())->method('getEventManager')->willReturn($eventManage);
99131
$context->expects($this->any())->method('getScopeConfig')->willReturn($scopeConfig);
100132
$context->expects($this->any())->method('getDesignPackage')->willReturn($design);
101-
$context->expects($this->any())->method('getStoreManager')->willReturn($storeManager);
133+
$context->expects($this->any())->method('getStoreManager')->willReturn($this->storeManager);
102134
$context->expects($this->once())->method('getAppState')->willReturn($appState);
103135

104-
$maliciousCode = $this->createPartialMock(\Magento\Framework\Filter\Input\MaliciousCode::class, ['filter']);
105-
$maliciousCode->expects($this->once())
106-
->method('filter')
107-
->with($this->equalTo($requestParamMap[1][2]))
108-
->willReturn(self::MALICIOUS_TEXT);
109-
110136
/** @var \Magento\Email\Block\Adminhtml\Template\Preview $preview */
111-
$preview = $this->objectManagerHelper->getObject(
137+
$this->preview = $this->objectManagerHelper->getObject(
112138
\Magento\Email\Block\Adminhtml\Template\Preview::class,
113139
[
114140
'context' => $context,
115-
'maliciousCode' => $maliciousCode,
141+
'maliciousCode' => $this->maliciousCode,
116142
'emailFactory' => $emailFactory
117143
]
118144
);
119-
$this->assertEquals(self::MALICIOUS_TEXT, $preview->toHtml());
145+
}
146+
147+
/**
148+
* Check of processing email templates
149+
*
150+
* @param array $requestParamMap
151+
* @dataProvider toHtmlDataProvider
152+
*/
153+
public function testToHtml($requestParamMap)
154+
{
155+
$this->request->expects($this->atLeastOnce())
156+
->method('isSafeMethod')
157+
->willReturn(true);
158+
$this->request->expects($this->any())
159+
->method('getParam')
160+
->willReturnMap($requestParamMap);
161+
$this->template
162+
->expects($this->atLeastOnce())
163+
->method('getDesignConfig');
164+
$this->storeManager->expects($this->atLeastOnce())
165+
->method('getDefaultStoreView');
166+
$this->maliciousCode->expects($this->once())
167+
->method('filter')
168+
->with($this->equalTo($requestParamMap[1][2]))
169+
->willReturn(self::MALICIOUS_TEXT);
170+
171+
$this->assertEquals(self::MALICIOUS_TEXT, $this->preview->toHtml());
172+
}
173+
174+
/**
175+
* @expectedException \Magento\Framework\Exception\LocalizedException
176+
*/
177+
public function testToHtmlWithException()
178+
{
179+
$this->request->expects($this->atLeastOnce())
180+
->method('isSafeMethod')
181+
->willReturn(false);
182+
$this->template
183+
->expects($this->never())
184+
->method('getDesignConfig');
185+
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
186+
$this->expectExceptionMessage(
187+
(string)__('Wrong request.')
188+
);
189+
$this->preview->toHtml();
120190
}
121191

122192
/**

app/code/Magento/ImportExport/Model/Import.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,15 @@ public function uploadSource()
568568
$entity = $this->getEntity();
569569
/** @var $uploader Uploader */
570570
$uploader = $this->_uploaderFactory->create(['fileId' => self::FIELD_NAME_SOURCE_FILE]);
571+
$uploader->setAllowedExtensions(['csv', 'zip']);
571572
$uploader->skipDbProcessing(true);
572573
$fileName = $this->random->getRandomString(32) . '.' . $uploader->getFileExtension();
573-
$result = $uploader->save($this->getWorkingDir(), $fileName);
574+
try {
575+
$result = $uploader->save($this->getWorkingDir(), $fileName);
576+
} catch (\Exception $e) {
577+
throw new LocalizedException(__('The file cannot be uploaded.'));
578+
}
579+
574580
// phpcs:disable Magento2.Functions.DiscouragedFunction.Discouraged
575581
$extension = pathinfo($result['file'], PATHINFO_EXTENSION);
576582

app/code/Magento/Theme/Model/Design/BackendModelFactory.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Theme\Model\Design;
710

811
use Magento\Framework\App\Config\Value;
@@ -11,6 +14,9 @@
1114
use Magento\Theme\Model\Design\Config\MetadataProvider;
1215
use Magento\Theme\Model\ResourceModel\Design\Config\CollectionFactory;
1316

17+
/**
18+
* Class BackendModelFactory
19+
*/
1420
class BackendModelFactory extends ValueFactory
1521
{
1622
/**
@@ -58,13 +64,15 @@ public function __construct(
5864
*/
5965
public function create(array $data = [])
6066
{
67+
$storedData = $this->getStoredData($data['scope'], $data['scopeId'], $data['config']['path']);
68+
6169
$backendModelData = array_replace_recursive(
62-
$this->getStoredData($data['scope'], $data['scopeId'], $data['config']['path']),
70+
$storedData,
6371
[
6472
'path' => $data['config']['path'],
6573
'scope' => $data['scope'],
6674
'scope_id' => $data['scopeId'],
67-
'field_config' => $data['config'],
75+
'field_config' => $data['config']
6876
]
6977
);
7078

@@ -76,6 +84,13 @@ public function create(array $data = [])
7684
$backendModel = $this->getNewBackendModel($backendType, $backendModelData);
7785
$backendModel->setValue($data['value']);
7886

87+
if ($storedData) {
88+
foreach ($storedData as $key => $value) {
89+
$backendModel->setOrigData($key, $value);
90+
}
91+
$backendModel->setOrigData('field_config', $data['config']);
92+
}
93+
7994
return $backendModel;
8095
}
8196

@@ -166,9 +181,12 @@ protected function getMetadata()
166181
{
167182
if (!$this->metadata) {
168183
$this->metadata = $this->metadataProvider->get();
169-
array_walk($this->metadata, function (&$value) {
170-
$value = $value['path'];
171-
});
184+
array_walk(
185+
$this->metadata,
186+
function (&$value) {
187+
$value = $value['path'];
188+
}
189+
);
172190
}
173191
return $this->metadata;
174192
}

app/code/Magento/Theme/Model/Design/Config/ValueChecker.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Framework\App\Config as AppConfig;
99
use Magento\Framework\App\ScopeFallbackResolverInterface;
1010

11+
/**
12+
* Class ValueChecker
13+
*/
1114
class ValueChecker
1215
{
1316
/**
@@ -61,7 +64,7 @@ public function isDifferentFromDefault($value, $scope, $scopeId, array $fieldCon
6164
$fieldConfig
6265
),
6366
$this->valueProcessor->process(
64-
$this->appConfig->getValue($fieldConfig['path'], $scope, $scopeId),
67+
($this->appConfig->getValue($fieldConfig['path'], $scope, $scopeId) ?? ""),
6568
$scope,
6669
$scopeId,
6770
$fieldConfig
@@ -80,12 +83,11 @@ public function isDifferentFromDefault($value, $scope, $scopeId, array $fieldCon
8083
*/
8184
protected function isEqual($value, $defaultValue)
8285
{
83-
switch (gettype($value)) {
84-
case 'array':
85-
return $this->isEqualArrays($value, $defaultValue);
86-
default:
87-
return $value === $defaultValue;
86+
if (is_array($value)) {
87+
return $this->isEqualArrays($value, $defaultValue);
8888
}
89+
90+
return $value === $defaultValue;
8991
}
9092

9193
/**

dev/tests/integration/testsuite/Magento/ImportExport/Controller/Adminhtml/Import/ValidateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function validationDataProvider(): array
9393
[
9494
'file_name' => 'test.txt',
9595
'mime-type' => 'text/csv',
96-
'message' => '\'txt\' file extension is not supported',
96+
'message' => 'The file cannot be uploaded.',
9797
'delimiter' => ',',
9898
],
9999
[

0 commit comments

Comments
 (0)