Skip to content

Commit dc4de31

Browse files
committed
Fix #7333 Unable to set negative custom option fixed price in admin view.
1 parent 4832a75 commit dc4de31

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

app/code/Magento/Catalog/Model/Product/Option/Validator/DefaultValidator.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ protected function validateOptionType(Option $option)
132132
*/
133133
protected function validateOptionValue(Option $option)
134134
{
135-
return $this->isInRange($option->getPriceType(), $this->priceTypes) && !$this->isNegative($option->getPrice());
135+
return $this->isInRange($option->getPriceType(), $this->priceTypes) && $this->isNumber($option->getPrice());
136136
}
137137

138138
/**
139-
* Check whether value is empty
139+
* Check whether the value is empty
140140
*
141141
* @param mixed $value
142142
* @return bool
@@ -147,7 +147,7 @@ protected function isEmpty($value)
147147
}
148148

149149
/**
150-
* Check whether value is in range
150+
* Check whether the value is in range
151151
*
152152
* @param string $value
153153
* @param array $range
@@ -159,7 +159,7 @@ protected function isInRange($value, array $range)
159159
}
160160

161161
/**
162-
* Check whether value is not negative
162+
* Check whether the value is negative
163163
*
164164
* @param string $value
165165
* @return bool
@@ -168,4 +168,15 @@ protected function isNegative($value)
168168
{
169169
return intval($value) < 0;
170170
}
171+
172+
/**
173+
* Check whether the value is a number
174+
*
175+
* @param string $value
176+
* @return bool
177+
*/
178+
protected function isNumber($value)
179+
{
180+
return is_numeric($value);
181+
}
171182
}

app/code/Magento/Catalog/Model/Product/Option/Validator/Select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function isValidOptionPrice($priceType, $price, $storeId)
8383
if (!$priceType && !$price) {
8484
return true;
8585
}
86-
if (!$this->isInRange($priceType, $this->priceTypes) || $this->isNegative($price)) {
86+
if (!$this->isInRange($priceType, $this->priceTypes) || !$this->isNumber($price)) {
8787
return false;
8888
}
8989

app/code/Magento/Catalog/Test/Unit/Model/Product/Option/Validator/DefaultValidatorTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,13 @@ public function testIsValidFail($product)
129129
* Data provider for testValidationNegativePrice
130130
* @return array
131131
*/
132-
public function validationNegativePriceDataProvider()
132+
public function validationPriceDataProvider()
133133
{
134134
return [
135135
['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 1])],
136136
['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 0])],
137+
['option_title', 'name 1.1', 'fixed', 12, new \Magento\Framework\DataObject(['store_id' => 1])],
138+
['option_title', 'name 1.1', 'fixed', 12, new \Magento\Framework\DataObject(['store_id' => 0])]
137139
];
138140
}
139141

@@ -143,9 +145,9 @@ public function validationNegativePriceDataProvider()
143145
* @param $priceType
144146
* @param $price
145147
* @param $product
146-
* @dataProvider validationNegativePriceDataProvider
148+
* @dataProvider validationPriceDataProvider
147149
*/
148-
public function testValidationNegativePrice($title, $type, $priceType, $price, $product)
150+
public function testValidationPrice($title, $type, $priceType, $price, $product)
149151
{
150152
$methods = ['getTitle', 'getType', 'getPriceType', 'getPrice', '__wakeup', 'getProduct'];
151153
$valueMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Option::class, $methods);
@@ -155,10 +157,8 @@ public function testValidationNegativePrice($title, $type, $priceType, $price, $
155157
$valueMock->expects($this->once())->method('getPrice')->will($this->returnValue($price));
156158
$valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
157159

158-
$messages = [
159-
'option values' => 'Invalid option value',
160-
];
161-
$this->assertFalse($this->validator->isValid($valueMock));
160+
$messages = [];
161+
$this->assertTrue($this->validator->isValid($valueMock));
162162
$this->assertEquals($messages, $this->validator->getMessages());
163163
}
164164
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Option/Validator/SelectTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function isValidSuccessDataProvider()
8787
]
8888
],
8989
[
90-
false,
90+
true,
9191
[
9292
'title' => 'Some Title',
9393
'price_type' => 'fixed',
@@ -157,7 +157,6 @@ public function testIsValidateWithInvalidData($priceType, $price, $title)
157157
public function isValidateWithInvalidDataDataProvider()
158158
{
159159
return [
160-
'invalid_price' => ['fixed', -10, 'Title'],
161160
'invalid_price_type' => ['some_value', '10', 'Title'],
162161
'empty_title' => ['fixed', 10, null]
163162
];

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ protected function getPriceFieldConfig($sortOrder)
923923
'addbeforePool' => $this->productOptionsPrice->prefixesToOptionArray(),
924924
'sortOrder' => $sortOrder,
925925
'validation' => [
926-
'validate-zero-or-greater' => true
926+
'validate-number' => true
927927
],
928928
],
929929
],

0 commit comments

Comments
 (0)