Skip to content

Commit 4630f5d

Browse files
committed
ACP2E-4156: For Shipping Information Server Side Validation is not Working using REST API
1 parent babb02e commit 4630f5d

File tree

12 files changed

+236
-65
lines changed

12 files changed

+236
-65
lines changed

app/code/Magento/Checkout/Model/GuestShippingInformationManagement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private function validateAddressAttributes(AddressInterface $address, string $ad
105105
}
106106
}
107107
}
108+
$customerAddress->setSkipRequiredValidation(true);
108109
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
109110
if (!$validator->isValid($customerAddress)) {
110111
$this->throwValidationException($validator->getMessages(), $addressType);

app/code/Magento/Eav/Model/Attribute/Data/Date.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Eav\Model\Attribute\Data;
77

88
use Magento\Framework\App\RequestInterface;
9+
use Magento\Framework\Exception\LocalizedException;
910

1011
/**
1112
* EAV Entity Attribute Date Data Model
12-
*
13-
* @author Magento Core Team <[email protected]>
1413
*/
1514
class Date extends \Magento\Eav\Model\Attribute\Data\AbstractData
1615
{
@@ -28,12 +27,14 @@ public function extractValue(RequestInterface $request)
2827

2928
/**
3029
* Validate data
30+
*
3131
* Return true or array of errors
3232
*
3333
* @param array|string $value
3434
* @return bool|array
3535
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
3636
* @SuppressWarnings(PHPMD.NPathComplexity)
37+
* @throws LocalizedException
3738
*/
3839
public function validateValue($value)
3940
{
@@ -45,6 +46,10 @@ public function validateValue($value)
4546
$value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
4647
}
4748

49+
if ((!$attribute->getIsRequired() || ($this->getEntity()?->getSkipRequiredValidation())) && empty($value)) {
50+
return true;
51+
}
52+
4853
if ($attribute->getIsRequired() && empty($value)) {
4954
$label = __($attribute->getStoreLabel());
5055
$errors[] = __('"%1" is a required value.', $label);

app/code/Magento/Eav/Model/Attribute/Data/File.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Eav\Model\Attribute\Data;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\App\RequestInterface;
1010
use Magento\Framework\Filesystem\Io\File as FileIo;
11+
use Magento\Framework\Exception\LocalizedException;
1112

1213
/**
1314
* EAV Entity Attribute File Data Model
@@ -182,6 +183,7 @@ protected function _validateByRules($value)
182183
* @return bool
183184
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
184185
* @SuppressWarnings(PHPMD.NPathComplexity)
186+
* @throws LocalizedException
185187
*/
186188
public function validateValue($value)
187189
{
@@ -210,7 +212,9 @@ public function validateValue($value)
210212
return true;
211213
}
212214

213-
if (!$attribute->getIsRequired() && !$toUpload) {
215+
if ((!$attribute->getIsRequired() || ($this->getEntity()?->getSkipRequiredValidation()))
216+
&& !$toUpload
217+
) {
214218
return true;
215219
}
216220

app/code/Magento/Eav/Model/Attribute/Data/Multiline.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Eav\Model\Attribute\Data;
77

88
use Magento\Framework\App\RequestInterface;
9+
use Magento\Framework\Exception\LocalizedException;
910

1011
/**
1112
* EAV Entity Attribute Multiply line Data Model
12-
*
13-
* @author Magento Core Team <[email protected]>
1413
*/
1514
class Multiline extends \Magento\Eav\Model\Attribute\Data\Text
1615
{
@@ -38,13 +37,20 @@ public function extractValue(RequestInterface $request)
3837
*
3938
* @param array|string $value
4039
* @return bool|array
40+
* @throws LocalizedException
41+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
42+
* @SuppressWarnings(PHPMD.NPathComplexity)
4143
*/
4244
public function validateValue($value)
4345
{
4446
$errors = [];
4547
$lines = $this->processValue($value);
4648
$attribute = $this->getAttribute();
4749

50+
if ((!$attribute->getIsRequired() || ($this->getEntity()?->getSkipRequiredValidation())) && empty($lines)) {
51+
return true;
52+
}
53+
4854
if ($attribute->getIsRequired() && empty($lines)) {
4955
$attributeLabel = __($attribute->getStoreLabel());
5056
$errors[] = __('"%1" is a required value.', $attributeLabel);

app/code/Magento/Eav/Model/Attribute/Data/Multiselect.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Eav\Model\Attribute\Data;
77

88
use Magento\Framework\App\RequestInterface;
99

1010
/**
1111
* EAV Entity Attribute Multiply select Data Model
12-
*
13-
* @author Magento Core Team <[email protected]>
1412
*/
1513
class Multiselect extends AbstractData
1614
{
@@ -81,7 +79,11 @@ public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::O
8179
}
8280

8381
/**
84-
* @inheritdoc
82+
* Validate the data
83+
*
84+
* @param array|string $value
85+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
86+
* @SuppressWarnings(PHPMD.NPathComplexity)
8587
*/
8688
public function validateValue($value)
8789
{
@@ -93,6 +95,10 @@ public function validateValue($value)
9395
$value = $this->getEntity()->getData($attribute->getAttributeCode());
9496
}
9597

98+
if ((!$attribute->getIsRequired() || ($this->getEntity()?->getSkipRequiredValidation())) && empty($value)) {
99+
return true;
100+
}
101+
96102
if ($attribute->getIsRequired() && empty($value) && $value != '0') {
97103
$label = __($attribute->getStoreLabel());
98104
$errors[] = __('"%1" is a required value.', $label);

app/code/Magento/Eav/Model/Attribute/Data/Select.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Eav\Model\Attribute\Data;
77

88
use Magento\Framework\App\RequestInterface;
9+
use Magento\Framework\Exception\LocalizedException;
910

1011
/**
1112
* EAV Entity Attribute Select Data Model
12-
*
13-
* @author Magento Core Team <[email protected]>
1413
*/
1514
class Select extends \Magento\Eav\Model\Attribute\Data\AbstractData
1615
{
@@ -32,6 +31,9 @@ public function extractValue(RequestInterface $request)
3231
*
3332
* @param array|string $value
3433
* @return bool|array
34+
* @throws LocalizedException
35+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
36+
* @SuppressWarnings(PHPMD.NPathComplexity)
3537
*/
3638
public function validateValue($value)
3739
{
@@ -43,6 +45,10 @@ public function validateValue($value)
4345
$value = $this->getEntity()->getData($attribute->getAttributeCode());
4446
}
4547

48+
if ((!$attribute->getIsRequired() || ($this->getEntity()?->getSkipRequiredValidation())) && empty($value)) {
49+
return true;
50+
}
51+
4652
if ($attribute->getIsRequired() && empty($value) && $value != '0') {
4753
$label = __($attribute->getStoreLabel());
4854
$errors[] = __('"%1" is a required value.', $label);

app/code/Magento/Eav/Model/Attribute/Data/Text.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Eav\Model\Attribute\Data;
@@ -17,7 +17,6 @@
1717
/**
1818
* EAV Entity Attribute Text Data Model
1919
*
20-
* @author Magento Core Team <[email protected]>
2120
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2221
*/
2322
class Text extends \Magento\Eav\Model\Attribute\Data\AbstractData
@@ -63,7 +62,7 @@ public function extractValue(RequestInterface $request)
6362
*
6463
* @param array|string $value
6564
* @return bool|array
66-
* @throws \Magento\Framework\Exception\LocalizedException
65+
* @throws LocalizedException
6766
*/
6867
public function validateValue($value)
6968
{
@@ -75,7 +74,7 @@ public function validateValue($value)
7574
$value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
7675
}
7776

78-
if (!$attribute->getIsRequired() && empty($value)) {
77+
if ((!$attribute->getIsRequired() || ($this->getEntity()?->getSkipRequiredValidation())) && empty($value)) {
7978
return true;
8079
}
8180

app/code/Magento/Eav/Test/Unit/Model/Attribute/Data/DateTest.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -96,14 +96,27 @@ public static function outputValueDataProvider()
9696
* @param array $rules
9797
* @param mixed $originalValue
9898
* @param bool $isRequired
99+
* @param bool $skipRequiredValidation
99100
* @param array $expectedResult
100101
* @dataProvider validateValueDataProvider
101102
*/
102-
public function testValidateValue($value, $rules, $originalValue, $isRequired, $expectedResult)
103-
{
104-
$entityMock = $this->createMock(AbstractModel::class);
103+
public function testValidateValue(
104+
$value,
105+
$rules,
106+
$originalValue,
107+
$isRequired,
108+
$skipRequiredValidation,
109+
$expectedResult
110+
) {
111+
$entityMock = $this->getMockBuilder(AbstractModel::class)
112+
->disableOriginalConstructor()
113+
->onlyMethods(['getDataUsingMethod'])
114+
->addMethods(['getSkipRequiredValidation'])
115+
->getMock();
105116
$entityMock->expects($this->any())->method('getDataUsingMethod')->willReturn($originalValue);
106-
117+
$entityMock->expects($this->any())
118+
->method('getSkipRequiredValidation')
119+
->willReturn($skipRequiredValidation);
107120
$attributeMock = $this->createMock(Attribute::class);
108121
$attributeMock->expects($this->any())->method('getStoreLabel')->willReturn('Label');
109122
$attributeMock->expects($this->any())->method('getIsRequired')->willReturn($isRequired);
@@ -125,41 +138,55 @@ public static function validateValueDataProvider()
125138
'rules' => [],
126139
'originalValue' => false,
127140
'isRequired' => true,
141+
'skipRequiredValidation' => false,
128142
'expectedResult' => ['"Label" is a required value.'],
129143
],
130144
[
131145
'value' => 'value',
132146
'rules' => [],
133147
'originalValue' => 'value',
134148
'isRequired' => false,
149+
'skipRequiredValidation' => false,
150+
'expectedResult' => true,
151+
],
152+
[
153+
'value' => false,
154+
'rules' => [],
155+
'originalValue' => '',
156+
'isRequired' => true,
157+
'skipRequiredValidation' => true,
135158
'expectedResult' => true,
136159
],
137160
[
138161
'value' => null,
139162
'rules' => [],
140163
'originalValue' => '',
141164
'isRequired' => false,
165+
'skipRequiredValidation' => false,
142166
'expectedResult' => true,
143167
],
144168
[
145169
'value' => '2000-01-01',
146170
'rules' => ['date_range_min' => strtotime('2001-01-01'),'date_range_max' => strtotime('2002-01-01')],
147171
'originalValue' => '',
148172
'isRequired' => false,
173+
'skipRequiredValidation' => false,
149174
'expectedResult' => ['Please enter a valid date between 01/01/2001 and 01/01/2002 at Label.'],
150175
],
151176
[
152177
'value' => '2000-01-01',
153178
'rules' => ['date_range_min' => strtotime('2001-01-01')],
154179
'originalValue' => '',
155180
'isRequired' => false,
181+
'skipRequiredValidation' => false,
156182
'expectedResult' => ['Please enter a valid date equal to or greater than 01/01/2001 at Label.'],
157183
],
158184
[
159185
'value' => '2010-01-01',
160186
'rules' => ['date_range_max' => strtotime('2001-01-01')],
161187
'originalValue' => '',
162188
'isRequired' => false,
189+
'skipRequiredValidation' => false,
163190
'expectedResult' => ['Please enter a valid date less than or equal to 01/01/2001 at Label.'],
164191
],
165192
];

0 commit comments

Comments
 (0)