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 all 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 @@ -60,31 +60,30 @@ public function isValidTitleDataProvider()
{
$mess = ['option required fields' => 'Missed values for option required fields'];
return [
['option_title', 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
['option_title', 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 0]), [], true],
[null, 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
[null, 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 0]), $mess, false],
['option_title', 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
['option_title', 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 0]), [], true],
[null, 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
[null, 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 0]), $mess, false],
];
}

/**
* @param $title
* @param $type
* @param $priceType
* @param $price
* @param $product
* @param $messages
* @param $result
* @dataProvider isValidTitleDataProvider
*/
public function testIsValidTitle($title, $type, $priceType, $price, $product, $messages, $result)
public function testIsValidTitle($title, $type, $priceType, $product, $messages, $result)
{
$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->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');
$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 @@ -126,37 +125,72 @@ public function testIsValidFail($product)
}

/**
* Data provider for testValidationNegativePrice
* Data provider for testValidPriceTypeIsValid
* @return array
*/
public function validationNegativePriceDataProvider()
public function validPriceTypeIsValidDataProvider()
{
return [
['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 1])],
['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 0])],
['option_title', 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 1])],
['option_title', 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 0])],
];
}

/**
*
*
* @param $title
* @param $type
* @param $priceType
* @param $price
* @param $product
* @dataProvider validationNegativePriceDataProvider
* @dataProvider validPriceTypeIsValidDataProvider
*/
public function testValidationNegativePrice($title, $type, $priceType, $price, $product)
public function testValidPriceTypeIsValid($title, $type, $priceType, $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');
$valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));

$messages = [];
$this->assertTrue($this->validator->isValid($valueMock));
$this->assertEquals($messages, $this->validator->getMessages());
}

/**
* Data provider for testInvalidPriceTypeIsFail
* @return array
*/
public function invalidPriceTypeIsFailDataProvider()
{
return [
['option_title', 'name 1.1', 'notexisting', new \Magento\Framework\DataObject(['store_id' => 1])],
['option_title', 'name 1.1', 'wrongtype', new \Magento\Framework\DataObject(['store_id' => 0])],
];
}

/**
* @param $title
* @param $type
* @param $priceType
* @param $product
* @dataProvider invalidPriceTypeIsFailDataProvider
*/
public function testInvalidPriceTypeIsFail($title, $type, $priceType, $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->never())->method('getPrice');
$valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));

$messages = [
'option values' => 'Invalid option value',
'option values' => 'Invalid option value'
];
$this->assertFalse($this->validator->isValid($valueMock));
$this->assertEquals($messages, $this->validator->getMessages());
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('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