Skip to content

Commit 0f53208

Browse files
author
Sergey Shvets
committed
MAGETWO-82132: Comma special character in cart price rule condition value results in incorrect rule
1 parent b23ddd7 commit 0f53208

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

app/code/Magento/SalesRule/Model/Rule/Condition/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
5252

5353
$attrCode = $this->getAttribute();
5454

55-
if ('category_ids' == $attrCode) {
55+
if ($attrCode === 'category_ids') {
5656
return $this->validateAttribute($this->_getAvailableInCategories($product->getId()));
5757
}
5858

app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\SalesRule\Test\Unit\Model\Rule\Condition;
78

9+
use Magento\Directory\Model\CurrencyFactory;
10+
use Magento\Framework\App\ScopeResolverInterface;
811
use \Magento\Framework\DB\Adapter\AdapterInterface;
912
use \Magento\Framework\DB\Select;
10-
use \Magento\Framework\Model\AbstractModel;
13+
use Magento\Framework\Locale\Format;
14+
use Magento\Framework\Locale\ResolverInterface;
1115
use Magento\Quote\Model\Quote\Item\AbstractItem;
1216
use \Magento\Rule\Model\Condition\Context;
1317
use \Magento\Backend\Helper\Data;
@@ -130,8 +134,12 @@ protected function setUp()
130134
$this->collectionMock = $this->getMockBuilder(Collection::class)
131135
->disableOriginalConstructor()
132136
->getMock();
133-
$this->formatMock = $this->getMockBuilder(FormatInterface::class)
134-
->getMockForAbstractClass();
137+
$this->formatMock = new Format(
138+
$this->getMockBuilder(ScopeResolverInterface::class)->disableOriginalConstructor()->getMock(),
139+
$this->getMockBuilder(ResolverInterface::class)->disableOriginalConstructor()->getMock(),
140+
$this->getMockBuilder(CurrencyFactory::class)->disableOriginalConstructor()->getMock()
141+
);
142+
135143
$this->model = new SalesRuleProduct(
136144
$this->contextMock,
137145
$this->backendHelperMock,
@@ -231,4 +239,83 @@ public function testValidateCategoriesIgnoresVisibility()
231239

232240
$this->model->validate($item);
233241
}
242+
243+
/**
244+
* @param boolean $isValid
245+
* @param string $conditionValue
246+
* @param string $operator
247+
* @param double $productPrice
248+
* @dataProvider localisationProvider
249+
*/
250+
public function testQuoteLocaleFormatPrice($isValid, $conditionValue, $operator = '>=', $productPrice = 2000.00)
251+
{
252+
$attr = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class)
253+
->disableOriginalConstructor()
254+
->setMethods(['getAttribute'])
255+
->getMockForAbstractClass();
256+
257+
$attr->expects($this->any())
258+
->method('getAttribute')
259+
->willReturn('');
260+
261+
/* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */
262+
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
263+
->disableOriginalConstructor()
264+
->setMethods(['setQuoteItemPrice', 'getResource', 'hasData', 'getData',])
265+
->getMock();
266+
267+
$product->expects($this->any())
268+
->method('setQuoteItemPrice')
269+
->willReturnSelf();
270+
271+
$product->expects($this->any())
272+
->method('getResource')
273+
->willReturn($attr);
274+
275+
$product->expects($this->any())
276+
->method('hasData')
277+
->willReturn(true);
278+
279+
$product->expects($this->any())
280+
->method('getData')
281+
->with('quote_item_price')
282+
->willReturn($productPrice);
283+
284+
/* @var AbstractItem|\PHPUnit_Framework_MockObject_MockObject $item */
285+
$item = $this->getMockBuilder(AbstractItem::class)
286+
->disableOriginalConstructor()
287+
->setMethods(['getPrice', 'getProduct',])
288+
->getMockForAbstractClass();
289+
290+
$item->expects($this->any())
291+
->method('getPrice')
292+
->willReturn($productPrice);
293+
294+
$item->expects($this->any())
295+
->method('getProduct')
296+
->willReturn($product);
297+
298+
$this->model->setAttribute('quote_item_price')
299+
->setOperator($operator);
300+
301+
$this->assertEquals($isValid, $this->model->setValue($conditionValue)->validate($item));
302+
}
303+
304+
305+
/**
306+
* DataProvider for testQuoteLocaleFormatPrice
307+
*
308+
* @return array
309+
*/
310+
public function localisationProvider(): array
311+
{
312+
return [
313+
'number' => [true, 500.01],
314+
'locale' => [true, '1,500.03'],
315+
'operation' => [true, '1,500.03', '!='],
316+
'stringOperation' => [false, '1,500.03', '{}'],
317+
'smallPrice' => [false, '1,500.03', '>=', 1000],
318+
];
319+
}
320+
234321
}

0 commit comments

Comments
 (0)