Skip to content

Commit 3023c43

Browse files
committed
Merge branch 'AC-1997' of github.com:magento-cia/magento2ce into 2.4.5-bugfixes-020822
2 parents f403415 + 2a0838e commit 3023c43

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Quote\Plugin\Webapi\Controller\Rest;
8+
9+
use Magento\Webapi\Controller\Rest\ParamsOverrider;
10+
11+
/**
12+
* Validates Quote Data
13+
*/
14+
class ValidateQuoteData
15+
{
16+
private const QUOTE_KEY = 'quote';
17+
18+
/**
19+
* Before Overriding to validate data
20+
*
21+
* @param ParamsOverrider $subject
22+
* @param array $inputData
23+
* @param array $parameters
24+
* @return array[]
25+
*
26+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
27+
*/
28+
29+
public function beforeOverride(ParamsOverrider $subject, array $inputData, array $parameters): array
30+
{
31+
if (isset($inputData[self:: QUOTE_KEY])) {
32+
$inputData[self:: QUOTE_KEY] = $this->validateInputData($inputData[self:: QUOTE_KEY]);
33+
};
34+
return [$inputData, $parameters];
35+
}
36+
37+
/**
38+
* Validates InputData
39+
*
40+
* @param array $inputData
41+
* @return array
42+
*/
43+
private function validateInputData(array $inputData): array
44+
{
45+
$result = [];
46+
47+
$data = array_filter($inputData, function ($k) use (&$result) {
48+
$key = is_string($k) ? strtolower($k) : $k;
49+
return !isset($result[$key]) && ($result[$key] = true);
50+
}, ARRAY_FILTER_USE_KEY);
51+
52+
return array_map(function ($value) {
53+
return is_array($value) ? $this->validateInputData($value) : $value;
54+
}, $data);
55+
}
56+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Test\Unit\Plugin\Webapi\Controller\Rest;
8+
9+
use Exception;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Quote\Plugin\Webapi\Controller\Rest\ValidateQuoteData;
12+
use PHPUnit\Framework\TestCase;
13+
use ReflectionClass;
14+
15+
/**
16+
* Unit test for ValidateQuoteData plugin
17+
*/
18+
class ValidateQuoteDataTest extends TestCase
19+
{
20+
21+
/**
22+
* @var ValidateQuoteData
23+
*/
24+
private $validateQuoteDataObject;
25+
26+
/**
27+
* @var ReflectionClass
28+
*
29+
*/
30+
private $reflectionObject;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp(): void
36+
{
37+
$this->validateQuoteDataObject = ObjectManager::getInstance()->get(ValidateQuoteData::class);
38+
$this->reflectionObject = new ReflectionClass(get_class($this->validateQuoteDataObject));
39+
}
40+
/**
41+
* Test if the quote array is valid
42+
*
43+
* @param array $array
44+
* @param array $result
45+
* @dataProvider dataProviderInputData
46+
* @throws Exception
47+
*/
48+
public function testValidateInputData(array $array, array $result)
49+
{
50+
$this->assertEquals(
51+
$result,
52+
$this->invokeValidateInputData('validateInputData', [$array])
53+
);
54+
}
55+
56+
/**
57+
* @param string $methodName
58+
* @param array $arguments
59+
* @return mixed
60+
* @throws Exception
61+
*/
62+
private function invokeValidateInputData(string $methodName, array $arguments = [])
63+
{
64+
$validateInputDataMethod = $this->reflectionObject->getMethod($methodName);
65+
$validateInputDataMethod->setAccessible(true);
66+
return $validateInputDataMethod->invokeArgs($this->validateQuoteDataObject, $arguments);
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
public function dataProviderInputData(): array
73+
{
74+
return [
75+
[
76+
['person' =>
77+
[
78+
'id' => -1,
79+
'Id' => 1,
80+
'name' =>
81+
[
82+
'firstName' => 'John',
83+
'LastName' => 'S'
84+
],
85+
'isHavingVehicle' => 1,
86+
'address' =>
87+
[
88+
'street' => '4th Street',
89+
'Street' => '2nd Street',
90+
'city' => 'Atlanta'
91+
],
92+
]
93+
],
94+
['person' =>
95+
[
96+
'id' => -1,
97+
'name' =>
98+
[
99+
'firstName' => 'John',
100+
'LastName' => 'S'
101+
],
102+
'isHavingVehicle' => 1,
103+
'address' =>
104+
[
105+
'street' => '4th Street',
106+
'city' => 'Atlanta'
107+
],
108+
]
109+
],
110+
]
111+
];
112+
}
113+
}

app/code/Magento/Quote/etc/webapi_rest/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
<type name="Magento\Quote\Model\Quote">
2020
<plugin name="updateQuoteStoreId" type="Magento\Quote\Model\Quote\Plugin\UpdateQuoteStoreId" />
2121
</type>
22+
<type name="Magento\Webapi\Controller\Rest\ParamsOverrider">
23+
<plugin name="validateQuoteData" type="Magento\Quote\Plugin\Webapi\Controller\Rest\ValidateQuoteData" sortOrder="1" disabled="false" />
24+
</type>
2225
</config>

0 commit comments

Comments
 (0)