Skip to content

Commit 0c885b4

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-91771' into chaika_august2
2 parents 353b4b6 + 0c3e397 commit 0c885b4

File tree

6 files changed

+183
-28
lines changed

6 files changed

+183
-28
lines changed

app/code/Magento/ConfigurableProduct/Test/Unit/Block/Product/View/Type/ConfigurableTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\ConfigurableProduct\Test\Unit\Block\Product\View\Type;
78

89
use Magento\Customer\Model\Session;
@@ -83,6 +84,9 @@ class ConfigurableTest extends \PHPUnit\Framework\TestCase
8384
*/
8485
private $variationPricesMock;
8586

87+
/**
88+
* {@inheritDoc}
89+
*/
8690
protected function setUp()
8791
{
8892
$this->mockContextObject();
@@ -174,7 +178,7 @@ protected function setUp()
174178
*
175179
* @return array
176180
*/
177-
public function cacheKeyProvider() : array
181+
public function cacheKeyProvider(): array
178182
{
179183
return [
180184
'without_currency_and_customer_group' => [
@@ -313,11 +317,7 @@ public function testGetJsonConfig()
313317
$this->localeFormat->expects($this->atLeastOnce())->method('getPriceFormat')->willReturn([]);
314318
$this->localeFormat->expects($this->any())
315319
->method('getNumber')
316-
->willReturnMap([
317-
[$amount, $amount],
318-
[$priceQty, $priceQty],
319-
[$percentage, $percentage],
320-
]);
320+
->willReturnArgument(0);
321321

322322
$this->variationPricesMock->expects($this->once())
323323
->method('getFormattedPrices')
@@ -349,13 +349,13 @@ public function testGetJsonConfig()
349349
/**
350350
* Retrieve array with expected parameters for method getJsonConfig()
351351
*
352-
* @param $productId
353-
* @param $amount
354-
* @param $priceQty
355-
* @param $percentage
352+
* @param int $productId
353+
* @param double $amount
354+
* @param int $priceQty
355+
* @param int $percentage
356356
* @return array
357357
*/
358-
private function getExpectedArray($productId, $amount, $priceQty, $percentage)
358+
private function getExpectedArray($productId, $amount, $priceQty, $percentage): array
359359
{
360360
$expectedArray = [
361361
'attributes' => [],

app/code/Magento/Rule/Model/Condition/AbstractCondition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function isArrayOperatorType()
380380
}
381381

382382
/**
383-
* @return array
383+
* @return mixed
384384
*/
385385
public function getValue()
386386
{

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

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

89
/**
@@ -51,10 +52,17 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
5152

5253
$attrCode = $this->getAttribute();
5354

54-
if ('category_ids' == $attrCode) {
55+
if ($attrCode === 'category_ids') {
5556
return $this->validateAttribute($this->_getAvailableInCategories($product->getId()));
5657
}
5758

59+
if ($attrCode === 'quote_item_price') {
60+
$numericOperations = $this->getDefaultOperatorInputByType()['numeric'];
61+
if (in_array($this->getOperator(), $numericOperations)) {
62+
$this->setData('value', $this->getFormattedPrice($this->getValue()));
63+
}
64+
}
65+
5866
return parent::validate($product);
5967
}
6068

@@ -79,4 +87,23 @@ public function getValueElementChooserUrl()
7987
}
8088
return $url !== false ? $this->_backendData->getUrl($url) : '';
8189
}
90+
91+
/**
92+
* @param string $value
93+
* @return float|null
94+
*/
95+
private function getFormattedPrice($value)
96+
{
97+
$value = preg_replace('/[^0-9^\^.,-]/m', '', $value);
98+
99+
/**
100+
* If the comma is the third symbol in the number, we consider it to be a decimal separator
101+
*/
102+
$separatorComa = strpos($value, ',');
103+
$separatorDot = strpos($value, '.');
104+
if ($separatorComa !== false && $separatorDot === false && preg_match('/,\d{3}$/m', $value) === 1) {
105+
$value .= '.00';
106+
}
107+
return $this->_localeFormat->getNumber($value);
108+
}
82109
}

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

Lines changed: 95 additions & 7 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;
@@ -50,8 +54,8 @@ class ProductTest extends \PHPUnit\Framework\TestCase
5054
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject */
5155
protected $collectionMock;
5256

53-
/** @var FormatInterface|\PHPUnit_Framework_MockObject_MockObject */
54-
protected $formatMock;
57+
/** @var FormatInterface */
58+
protected $format;
5559

5660
/** @var AttributeLoaderInterface|\PHPUnit_Framework_MockObject_MockObject */
5761
protected $attributeLoaderInterfaceMock;
@@ -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->format = 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,
@@ -140,7 +148,7 @@ protected function setUp()
140148
$this->productRepositoryMock,
141149
$this->productMock,
142150
$this->collectionMock,
143-
$this->formatMock
151+
$this->format
144152
);
145153
}
146154

@@ -199,7 +207,10 @@ public function testGetValueElementChooserUrl($attribute, $url, $jsObject = '')
199207
$this->assertEquals($url, $this->model->getValueElementChooserUrl());
200208
}
201209

202-
public function testValidateCategoriesIgnoresVisibility()
210+
/**
211+
* test ValidateCategoriesIgnoresVisibility
212+
*/
213+
public function testValidateCategoriesIgnoresVisibility(): void
203214
{
204215
/* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */
205216
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
@@ -231,4 +242,81 @@ public function testValidateCategoriesIgnoresVisibility()
231242

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

lib/internal/Magento/Framework/Locale/Format.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getNumber($value)
6565
}
6666

6767
//trim spaces and apostrophes
68-
$value = str_replace(['\'', ' '], '', $value);
68+
$value = preg_replace('/[^0-9^\^.,-]/m', '', $value);
6969

7070
$separatorComa = strpos($value, ',');
7171
$separatorDot = strpos($value, '.');

lib/internal/Magento/Framework/Locale/Test/Unit/FormatTest.php

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class FormatTest extends \PHPUnit\Framework\TestCase
3333
*/
3434
protected $currency;
3535

36+
/**
37+
* {@inheritDoc}
38+
*/
3639
protected function setUp()
3740
{
3841
$this->currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
@@ -41,9 +44,7 @@ protected function setUp()
4144
$this->scope = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)
4245
->setMethods(['getCurrentCurrency'])
4346
->getMockForAbstractClass();
44-
$this->scope->expects($this->once())
45-
->method('getCurrentCurrency')
46-
->willReturn($this->currency);
47+
4748
$this->scopeResolver = $this->getMockBuilder(\Magento\Framework\App\ScopeResolverInterface::class)
4849
->setMethods(['getScope'])
4950
->getMockForAbstractClass();
@@ -52,7 +53,10 @@ protected function setUp()
5253
->willReturn($this->scope);
5354
$this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
5455
->getMock();
56+
57+
/** @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject $currencyFactory */
5558
$currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
59+
->disableOriginalConstructor()
5660
->getMock();
5761

5862
$this->formatModel = new \Magento\Framework\Locale\Format(
@@ -63,21 +67,26 @@ protected function setUp()
6367
}
6468

6569
/**
66-
* @param $localeCode
67-
* @param $expectedResult
70+
* @param string $localeCode
71+
* @param array $expectedResult
6872
* @dataProvider getPriceFormatDataProvider
6973
*/
70-
public function testGetPriceFormat($localeCode, $expectedResult)
74+
public function testGetPriceFormat($localeCode, array $expectedResult): void
7175
{
76+
$this->scope->expects($this->once())
77+
->method('getCurrentCurrency')
78+
->willReturn($this->currency);
79+
7280
$result = $this->formatModel->getPriceFormat($localeCode);
7381
$intersection = array_intersect_assoc($result, $expectedResult);
7482
$this->assertCount(count($expectedResult), $intersection);
7583
}
7684

7785
/**
86+
*
7887
* @return array
7988
*/
80-
public function getPriceFormatDataProvider()
89+
public function getPriceFormatDataProvider(): array
8190
{
8291
return [
8392
['en_US', ['decimalSymbol' => '.', 'groupSymbol' => ',']],
@@ -86,4 +95,35 @@ public function getPriceFormatDataProvider()
8695
['uk_UA', ['decimalSymbol' => ',', 'groupSymbol' => ' ']]
8796
];
8897
}
98+
99+
/**
100+
*
101+
* @param mixed $value
102+
* @param float $expected
103+
* @dataProvider provideNumbers
104+
*/
105+
public function testGetNumber($value, $expected): void
106+
{
107+
$this->assertEquals($expected, $this->formatModel->getNumber($value));
108+
}
109+
110+
/**
111+
*
112+
* @return array
113+
*/
114+
public function provideNumbers(): array
115+
{
116+
return [
117+
[' 2345.4356,1234', 23454356.1234],
118+
['+23,3452.123', 233452.123],
119+
['12343', 12343],
120+
['-9456km', -9456],
121+
['0', 0],
122+
['2 054,10', 2054.1],
123+
['2046,45', 2046.45],
124+
['2 054.52', 2054.52],
125+
['2,46 GB', 2.46],
126+
['2,054.00', 2054],
127+
];
128+
}
89129
}

0 commit comments

Comments
 (0)