Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 17012d3

Browse files
Merge pull request #2814 from magento-qwerty/2.3-bugfixes-290618
Fixed issues: - MAGETWO-88600: Error while saving Theme design configuration - MAGETWO-88642: Sort Order Field Values
2 parents 6178786 + 74101a8 commit 17012d3

File tree

5 files changed

+95
-22
lines changed

5 files changed

+95
-22
lines changed

app/code/Magento/Theme/Controller/Adminhtml/Design/Config/Save.php

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

88
use Magento\Backend\App\Action;
99
use Magento\Framework\App\Request\DataPersistorInterface;
10+
use Magento\Framework\Exception\NotFoundException;
1011
use Magento\Theme\Model\DesignConfigRepository;
1112
use Magento\Backend\App\Action\Context;
1213
use Magento\Framework\Exception\LocalizedException;
@@ -62,9 +63,15 @@ protected function _isAllowed()
6263

6364
/**
6465
* @return \Magento\Framework\Controller\Result\Redirect
66+
*
67+
* @throws NotFoundException
6568
*/
6669
public function execute()
6770
{
71+
if (!$this->getRequest()->isPost()) {
72+
throw new NotFoundException(__('Page not found.'));
73+
}
74+
6875
$resultRedirect = $this->resultRedirectFactory->create();
6976
$scope = $this->getRequest()->getParam('scope');
7077
$scopeId = (int)$this->getRequest()->getParam('scope_id');
@@ -73,7 +80,7 @@ public function execute()
7380
try {
7481
$designConfigData = $this->configFactory->create($scope, $scopeId, $data);
7582
$this->designConfigRepository->save($designConfigData);
76-
$this->messageManager->addSuccess(__('You saved the configuration.'));
83+
$this->messageManager->addSuccessMessage(__('You saved the configuration.'));
7784

7885
$this->dataPersistor->clear('theme_design_config');
7986

@@ -86,10 +93,10 @@ public function execute()
8693
} catch (LocalizedException $e) {
8794
$messages = explode("\n", $e->getMessage());
8895
foreach ($messages as $message) {
89-
$this->messageManager->addError(__('%1', $message));
96+
$this->messageManager->addErrorMessage(__('%1', $message));
9097
}
9198
} catch (\Exception $e) {
92-
$this->messageManager->addException(
99+
$this->messageManager->addExceptionMessage(
93100
$e,
94101
__('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()
95102
);

app/code/Magento/Theme/Test/Unit/Controller/Adminhtml/Design/Config/SaveTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,13 @@ public function setUp()
6262
'',
6363
false
6464
);
65-
$this->request = $this->getMockForAbstractClass(
66-
\Magento\Framework\App\RequestInterface::class,
67-
[],
68-
'',
69-
false,
70-
false,
71-
true,
72-
['getFiles', 'getParam', 'getParams']
73-
);
65+
$this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
66+
->disableOriginalConstructor()->getMock();
67+
68+
$this->request->expects($this->atLeastOnce())
69+
->method('isPost')
70+
->willReturn(true);
71+
7472
$this->context = $objectManager->getObject(
7573
\Magento\Backend\App\Action\Context::class,
7674
[
@@ -138,7 +136,7 @@ public function testSave()
138136
->method('save')
139137
->with($this->designConfig);
140138
$this->messageManager->expects($this->once())
141-
->method('addSuccess')
139+
->method('addSuccessMessage')
142140
->with(__('You saved the configuration.'));
143141
$this->dataPersistor->expects($this->once())
144142
->method('clear')
@@ -194,7 +192,7 @@ public function testSaveWithLocalizedException()
194192
->with($this->designConfig)
195193
->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Exception message')));
196194
$this->messageManager->expects($this->once())
197-
->method('addError')
195+
->method('addErrorMessage')
198196
->with(__('Exception message')->render());
199197

200198
$this->dataPersistor->expects($this->once())
@@ -249,7 +247,7 @@ public function testSaveWithException()
249247
->with($this->designConfig)
250248
->willThrowException($exception);
251249
$this->messageManager->expects($this->once())
252-
->method('addException')
250+
->method('addExceptionMessage')
253251
->with($exception, 'Something went wrong while saving this configuration: Exception message');
254252

255253
$this->dataPersistor->expects($this->once())

dev/tests/integration/testsuite/Magento/Theme/Controller/Adminhtml/System/Design/Config/SaveTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
class SaveTest extends AbstractBackendController
1616
{
17+
/**
18+
* @var FormKey
19+
*/
20+
private $formKey;
21+
1722
/**
1823
* @inheritdoc
1924
*/
@@ -24,6 +29,15 @@ class SaveTest extends AbstractBackendController
2429
*/
2530
protected $uri = 'backend/theme/design_config/save';
2631

32+
protected function setUp()
33+
{
34+
parent::setUp();
35+
36+
$this->formKey = $this->_objectManager->get(
37+
FormKey::class
38+
);
39+
}
40+
2741
/**
2842
* Test design configuration save valid values.
2943
*
@@ -89,7 +103,22 @@ private function getRequestParams()
89103
'watermark_swatch_image_imageOpacity' => '',
90104
'watermark_swatch_image_position' => 'stretch',
91105
'scope' => 'default',
92-
'form_key' => $this->_objectManager->get(FormKey::class)->getFormKey(),
106+
'form_key' => $this->formKey->getFormKey(),
93107
];
94108
}
109+
110+
public function testAclHasAccess()
111+
{
112+
$this->getRequest()->setMethod(
113+
\Zend\Http\Request::METHOD_POST
114+
);
115+
116+
$this->getRequest()->setParams(
117+
[
118+
'form_key' => $this->formKey->getFormKey()
119+
]
120+
);
121+
122+
parent::testAclHasAccess();
123+
}
95124
}

lib/internal/Magento/Framework/Api/SortOrder.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ class SortOrder extends AbstractSimpleObject
2525
* Initialize object and validate sort direction
2626
*
2727
* @param array $data
28+
* @throws InputException
2829
*/
2930
public function __construct(array $data = [])
3031
{
3132
parent::__construct($data);
3233
if (null !== $this->getDirection()) {
3334
$this->validateDirection($this->getDirection());
3435
}
36+
if ($this->getField() !== null) {
37+
$this->validateField($this->getField());
38+
}
3539
}
3640

3741
/**
@@ -48,10 +52,14 @@ public function getField()
4852
* Set sorting field.
4953
*
5054
* @param string $field
55+
* @throws InputException
56+
*
5157
* @return $this
5258
*/
5359
public function setField($field)
5460
{
61+
$this->validateField($field);
62+
5563
return $this->setData(SortOrder::FIELD, $field);
5664
}
5765

@@ -69,6 +77,8 @@ public function getDirection()
6977
* Set sorting direction.
7078
*
7179
* @param string $direction
80+
* @throws InputException
81+
*
7282
* @return $this
7383
*/
7484
public function setDirection($direction)
@@ -81,10 +91,10 @@ public function setDirection($direction)
8191
* Validate direction argument ASC or DESC
8292
*
8393
* @param mixed $direction
84-
* @return null
94+
* @return void
8595
* @throws InputException
8696
*/
87-
private function validateDirection($direction)
97+
private function validateDirection($direction): void
8898
{
8999
$this->validateDirectionIsString($direction);
90100
$this->validateDirectionIsAscOrDesc($direction);
@@ -93,9 +103,9 @@ private function validateDirection($direction)
93103
/**
94104
* @param string $direction
95105
* @throws InputException
96-
* @return null
106+
* @return void
97107
*/
98-
private function validateDirectionIsString($direction)
108+
private function validateDirectionIsString($direction): void
99109
{
100110
if (!is_string($direction)) {
101111
throw new InputException(new Phrase(
@@ -108,9 +118,9 @@ private function validateDirectionIsString($direction)
108118
/**
109119
* @param string $direction
110120
* @throws InputException
111-
* @return null
121+
* @return void
112122
*/
113-
private function validateDirectionIsAscOrDesc($direction)
123+
private function validateDirectionIsAscOrDesc($direction): void
114124
{
115125
$normalizedDirection = $this->normalizeDirectionInput($direction);
116126
if (!in_array($normalizedDirection, [SortOrder::SORT_ASC, SortOrder::SORT_DESC], true)) {
@@ -129,4 +139,23 @@ private function normalizeDirectionInput($direction)
129139
{
130140
return strtoupper($direction);
131141
}
142+
143+
/**
144+
* Check if given value can be used as sorting field.
145+
*
146+
* @param string $field
147+
* @return void
148+
* @throws InputException
149+
*/
150+
private function validateField(string $field): void
151+
{
152+
if (preg_match('/[^a-z0-9\_]/i', $field)) {
153+
throw new InputException(
154+
new Phrase(
155+
'Sort order field %1 contains restricted symbols',
156+
[$field]
157+
)
158+
);
159+
}
160+
}
132161
}

lib/internal/Magento/Framework/Api/Test/Unit/SortOrderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,14 @@ public function testItValidatesADirectionAssignedDuringInstantiation()
9292
SortOrder::DIRECTION => 'not-asc-or-desc'
9393
]);
9494
}
95+
96+
/**
97+
* @expectedException \Magento\Framework\Exception\InputException
98+
*/
99+
public function testValidateField()
100+
{
101+
$this->sortOrder = new SortOrder([
102+
SortOrder::FIELD => 'invalid field (value);'
103+
]);
104+
}
95105
}

0 commit comments

Comments
 (0)