Skip to content

Commit 7b76b65

Browse files
committed
AC-5905: Customer API improvements
1 parent b2bfd05 commit 7b76b65

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-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+
8+
namespace Magento\Customer\Plugin\Webapi\Controller\Rest;
9+
10+
use Magento\Webapi\Controller\Rest\ParamsOverrider;
11+
12+
/**
13+
* Validates Customer Data
14+
*/
15+
class ValidateCustomerData
16+
{
17+
private const CUSTOMER_KEY = 'customer';
18+
19+
/**
20+
* Before Overriding to validate data
21+
*
22+
* @param ParamsOverrider $subject
23+
* @param array $inputData
24+
* @param array $parameters
25+
* @return array[]
26+
*
27+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
28+
*/
29+
public function beforeOverride(ParamsOverrider $subject, array $inputData, array $parameters): array
30+
{
31+
if (isset($inputData[self:: CUSTOMER_KEY])) {
32+
$inputData[self:: CUSTOMER_KEY] = $this->validateInputData($inputData[self:: CUSTOMER_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: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\Customer\Test\Unit\Plugin\Webapi\Controller\Rest;
9+
10+
use Exception;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Customer\Plugin\Webapi\Controller\Rest\ValidateCustomerData;
13+
use PHPUnit\Framework\TestCase;
14+
use ReflectionClass;
15+
16+
/**
17+
* Unit test for ValidateCustomerData plugin
18+
*/
19+
class ValidateCustomerDataTest extends TestCase
20+
{
21+
22+
/**
23+
* @var ValidateCustomerData
24+
*/
25+
private $validateCustomerDataObject;
26+
27+
/**
28+
* @var ReflectionClass
29+
*
30+
*/
31+
private $reflectionObject;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp(): void
37+
{
38+
$this->validateCustomerDataObject = ObjectManager::getInstance()->get(ValidateCustomerData::class);
39+
$this->reflectionObject = new ReflectionClass(get_class($this->validateCustomerDataObject));
40+
}
41+
42+
/**
43+
* Test if the customer Info is valid
44+
*
45+
* @param array $customerInfo
46+
* @param array $result
47+
* @dataProvider dataProviderInputData
48+
* @throws Exception
49+
*/
50+
public function testValidateInputData(array $customerInfo, array $result)
51+
{
52+
$this->assertEquals(
53+
$result,
54+
$this->invokeValidateInputData('validateInputData', [$customerInfo])
55+
);
56+
}
57+
58+
/**
59+
* @param string $methodName
60+
* @param array $arguments
61+
* @return mixed
62+
* @throws Exception
63+
*/
64+
private function invokeValidateInputData(string $methodName, array $arguments = [])
65+
{
66+
$validateInputDataMethod = $this->reflectionObject->getMethod($methodName);
67+
$validateInputDataMethod->setAccessible(true);
68+
return $validateInputDataMethod->invokeArgs($this->validateCustomerDataObject, $arguments);
69+
}
70+
71+
/**
72+
* @return array
73+
*/
74+
public function dataProviderInputData(): array
75+
{
76+
return [
77+
[
78+
['customer' =>
79+
[
80+
'id' => -1,
81+
'Id' => 1,
82+
'name' =>
83+
[
84+
'firstName' => 'Test',
85+
'LastName' => 'user'
86+
],
87+
'isHavingOwnHouse' => 1,
88+
'address' =>
89+
[
90+
'street' => '1st Street',
91+
'Street' => '3rd Street',
92+
'city' => 'London'
93+
],
94+
]
95+
],
96+
['customer' =>
97+
[
98+
'id' => -1,
99+
'name' =>
100+
[
101+
'firstName' => 'Test',
102+
'LastName' => 'user'
103+
],
104+
'isHavingOwnHouse' => 1,
105+
'address' =>
106+
[
107+
'street' => '1st Street',
108+
'city' => 'London'
109+
],
110+
]
111+
],
112+
]
113+
];
114+
}
115+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
</argument>
3232
</arguments>
3333
</type>
34+
<type name="Magento\Webapi\Controller\Rest\ParamsOverrider">
35+
<plugin name="validateCustomerData" type="Magento\Customer\Plugin\Webapi\Controller\Rest\ValidateCustomerData" sortOrder="1" disabled="false" />
36+
</type>
3437
<preference for="Magento\Customer\Api\AccountManagementInterface"
3538
type="Magento\Customer\Model\AccountManagementApi" />
3639
</config>

0 commit comments

Comments
 (0)