-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7841 from magento-cia/cia-2.4.6-bugfixes-08182022
cia-2.4.6-develop-bugfixes-08182022
- Loading branch information
Showing
7 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
app/code/Magento/Customer/Plugin/Webapi/Controller/Rest/ValidateCustomerData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Customer\Plugin\Webapi\Controller\Rest; | ||
|
||
use Magento\Webapi\Controller\Rest\ParamsOverrider; | ||
|
||
/** | ||
* Validates Customer Data | ||
*/ | ||
class ValidateCustomerData | ||
{ | ||
private const CUSTOMER_KEY = 'customer'; | ||
|
||
/** | ||
* Before Overriding to validate data | ||
* | ||
* @param ParamsOverrider $subject | ||
* @param array $inputData | ||
* @param array $parameters | ||
* @return array[] | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeOverride(ParamsOverrider $subject, array $inputData, array $parameters): array | ||
{ | ||
if (isset($inputData[self:: CUSTOMER_KEY])) { | ||
$inputData[self:: CUSTOMER_KEY] = $this->validateInputData($inputData[self:: CUSTOMER_KEY]); | ||
} | ||
return [$inputData, $parameters]; | ||
} | ||
|
||
/** | ||
* Validates InputData | ||
* | ||
* @param array $inputData | ||
* @return array | ||
*/ | ||
private function validateInputData(array $inputData): array | ||
{ | ||
$result = []; | ||
|
||
$data = array_filter($inputData, function ($k) use (&$result) { | ||
$key = is_string($k) ? strtolower($k) : $k; | ||
return !isset($result[$key]) && ($result[$key] = true); | ||
}, ARRAY_FILTER_USE_KEY); | ||
|
||
return array_map(function ($value) { | ||
return is_array($value) ? $this->validateInputData($value) : $value; | ||
}, $data); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...ode/Magento/Customer/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateCustomerDataTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Customer\Test\Unit\Plugin\Webapi\Controller\Rest; | ||
|
||
use Exception; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Customer\Plugin\Webapi\Controller\Rest\ValidateCustomerData; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Unit test for ValidateCustomerData plugin | ||
*/ | ||
class ValidateCustomerDataTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var ValidateCustomerData | ||
*/ | ||
private $validateCustomerDataObject; | ||
|
||
/** | ||
* @var ReflectionClass | ||
* | ||
*/ | ||
private $reflectionObject; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->validateCustomerDataObject = ObjectManager::getInstance()->get(ValidateCustomerData::class); | ||
$this->reflectionObject = new ReflectionClass(get_class($this->validateCustomerDataObject)); | ||
} | ||
|
||
/** | ||
* Test if the customer Info is valid | ||
* | ||
* @param array $customerInfo | ||
* @param array $result | ||
* @dataProvider dataProviderInputData | ||
* @throws Exception | ||
*/ | ||
public function testValidateInputData(array $customerInfo, array $result) | ||
{ | ||
$this->assertEquals( | ||
$result, | ||
$this->invokeValidateInputData('validateInputData', [$customerInfo]) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $methodName | ||
* @param array $arguments | ||
* @return mixed | ||
* @throws Exception | ||
*/ | ||
private function invokeValidateInputData(string $methodName, array $arguments = []) | ||
{ | ||
$validateInputDataMethod = $this->reflectionObject->getMethod($methodName); | ||
$validateInputDataMethod->setAccessible(true); | ||
return $validateInputDataMethod->invokeArgs($this->validateCustomerDataObject, $arguments); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function dataProviderInputData(): array | ||
{ | ||
return [ | ||
[ | ||
['customer' => | ||
[ | ||
'id' => -1, | ||
'Id' => 1, | ||
'name' => | ||
[ | ||
'firstName' => 'Test', | ||
'LastName' => 'user' | ||
], | ||
'isHavingOwnHouse' => 1, | ||
'address' => | ||
[ | ||
'street' => '1st Street', | ||
'Street' => '3rd Street', | ||
'city' => 'London' | ||
], | ||
] | ||
], | ||
['customer' => | ||
[ | ||
'id' => -1, | ||
'name' => | ||
[ | ||
'firstName' => 'Test', | ||
'LastName' => 'user' | ||
], | ||
'isHavingOwnHouse' => 1, | ||
'address' => | ||
[ | ||
'street' => '1st Street', | ||
'city' => 'London' | ||
], | ||
] | ||
], | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters