Skip to content

Allow negative price for custom option: fixes #7333 in GitHub (internal MAGETWO-60573) #13393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function validateOptionType(Option $option)
*/
protected function validateOptionValue(Option $option)
{
return $this->isInRange($option->getPriceType(), $this->priceTypes) && !$this->isNegative($option->getPrice());
return $this->isInRange($option->getPriceType(), $this->priceTypes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function isValidOptionPrice($priceType, $price, $storeId)
if (!$priceType && !$price) {
return true;
}
if (!$this->isInRange($priceType, $this->priceTypes) || $this->isNegative($price)) {
if (!$this->isInRange($priceType, $this->priceTypes)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testIsValidTitle($title, $type, $priceType, $price, $product, $m
$valueMock->expects($this->once())->method('getTitle')->will($this->returnValue($title));
$valueMock->expects($this->any())->method('getType')->will($this->returnValue($type));
$valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue($priceType));
$valueMock->expects($this->once())->method('getPrice')->will($this->returnValue($price));
$valueMock->expects($this->never())->method('getPrice')->will($this->returnValue($price));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the method should never be called, the will is not necessary (and will probably confuse the reader). 

$valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
$this->assertEquals($result, $this->validator->isValid($valueMock));
$this->assertEquals($messages, $this->validator->getMessages());
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testIsValidFail($product)
* Data provider for testValidationNegativePrice
* @return array
*/
public function validationNegativePriceDataProvider()
public function negativePriceIsValidDataProvider()
{
return [
['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 1])],
Expand All @@ -143,22 +143,22 @@ public function validationNegativePriceDataProvider()
* @param $priceType
* @param $price
* @param $product
* @dataProvider validationNegativePriceDataProvider
* @dataProvider negativePriceIsValidDataProvider
*/
public function testValidationNegativePrice($title, $type, $priceType, $price, $product)
public function testNegativePriceIsValid($title, $type, $priceType, $price, $product)
{
$methods = ['getTitle', 'getType', 'getPriceType', 'getPrice', '__wakeup', 'getProduct'];
$valueMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Option::class, $methods);
$valueMock->expects($this->once())->method('getTitle')->will($this->returnValue($title));
$valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue($type));
$valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue($priceType));
$valueMock->expects($this->once())->method('getPrice')->will($this->returnValue($price));
$valueMock->expects($this->never())->method('getPrice')->will($this->returnValue($price));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here.

$valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));

$messages = [
'option values' => 'Invalid option value',
];
$this->assertFalse($this->validator->isValid($valueMock));
$this->assertEquals($messages, $this->validator->getMessages());
$this->assertTrue($this->validator->isValid($valueMock));
$this->assertNotEquals($messages, $this->validator->getMessages());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose making the check more specific, i.e., to check for the expected messages instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your feedback! Will make the proposed changes and improve the tests.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testIsValidSuccess()
$this->valueMock->expects($this->once())->method('getTitle')->will($this->returnValue('option_title'));
$this->valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue('name 1.1'));
$this->valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('fixed'));
$this->valueMock->expects($this->once())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->never())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getImageSizeX')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getImageSizeY')->will($this->returnValue(15));
$this->assertEmpty($this->validator->getMessages());
Expand All @@ -71,7 +71,7 @@ public function testIsValidWithNegativeImageSize()
$this->valueMock->expects($this->once())->method('getTitle')->will($this->returnValue('option_title'));
$this->valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue('name 1.1'));
$this->valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('fixed'));
$this->valueMock->expects($this->once())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->never())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getImageSizeX')->will($this->returnValue(-10));
$this->valueMock->expects($this->never())->method('getImageSizeY');
$messages = [
Expand All @@ -86,7 +86,7 @@ public function testIsValidWithNegativeImageSizeY()
$this->valueMock->expects($this->once())->method('getTitle')->will($this->returnValue('option_title'));
$this->valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue('name 1.1'));
$this->valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('fixed'));
$this->valueMock->expects($this->once())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->never())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getImageSizeX')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getImageSizeY')->will($this->returnValue(-10));
$messages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function isValidSuccessDataProvider()
]
],
[
false,
true,
[
'title' => 'Some Title',
'price_type' => 'fixed',
Expand Down Expand Up @@ -157,7 +157,6 @@ public function testIsValidateWithInvalidData($priceType, $price, $title)
public function isValidateWithInvalidDataDataProvider()
{
return [
'invalid_price' => ['fixed', -10, 'Title'],
'invalid_price_type' => ['some_value', '10', 'Title'],
'empty_title' => ['fixed', 10, null]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testIsValidSuccess()
$this->valueMock->expects($this->once())->method('getTitle')->will($this->returnValue('option_title'));
$this->valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue('name 1.1'));
$this->valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('fixed'));
$this->valueMock->expects($this->once())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->never())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getMaxCharacters')->will($this->returnValue(10));
$this->assertTrue($this->validator->isValid($this->valueMock));
$this->assertEmpty($this->validator->getMessages());
Expand All @@ -70,7 +70,7 @@ public function testIsValidWithNegativeMaxCharacters()
$this->valueMock->expects($this->once())->method('getTitle')->will($this->returnValue('option_title'));
$this->valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue('name 1.1'));
$this->valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('fixed'));
$this->valueMock->expects($this->once())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->never())->method('getPrice')->will($this->returnValue(10));
$this->valueMock->expects($this->once())->method('getMaxCharacters')->will($this->returnValue(-10));
$messages = [
'option values' => 'Invalid option value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ protected function getPriceFieldConfig($sortOrder)
'addbeforePool' => $this->productOptionsPrice->prefixesToOptionArray(),
'sortOrder' => $sortOrder,
'validation' => [
'validate-zero-or-greater' => true
'validate-zero-or-greater' => false
],
],
],
Expand Down
4 changes: 3 additions & 1 deletion app/code/Magento/Catalog/view/base/web/js/price-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ define([
optionConfig: {},
optionHandlers: {},
optionTemplate: '<%= data.label %>' +
'<% if (data.finalPrice.value) { %>' +
'<% if (data.finalPrice.value > 0) { %>' +
' +<%- data.finalPrice.formatted %>' +
'<% } else if (data.finalPrice.value) { %>' +
' <%- data.finalPrice.formatted %>' +
'<% } %>',
controlContainer: 'dd'
};
Expand Down