Skip to content

Commit 36413b8

Browse files
author
Cristian Partica
committed
MAGETWO-36706: [GITHUB] Won't accept valid Brazil monetary value as input #1192
- unit tests
1 parent 18bb7bb commit 36413b8

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class HelperTest extends \PHPUnit_Framework_TestCase
4444
*/
4545
protected $websiteMock;
4646

47+
/**
48+
* @var \PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
protected $dateFilterMock;
51+
4752
/**
4853
* @var int
4954
*/
@@ -66,6 +71,7 @@ protected function setUp()
6671
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
6772
$this->websiteMock = $this->getMock('Magento\Store\Model\Website', [], [], '', false);
6873
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
74+
$this->dateFilterMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\Filter\Date', [], [], '', false);
6975

7076
$this->stockFilterMock = $this->getMock(
7177
'Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter',
@@ -91,6 +97,7 @@ protected function setUp()
9197
'setWebsiteIds',
9298
'isLockedAttribute',
9399
'lockAttribute',
100+
'getAttributes',
94101
'unlockAttribute',
95102
'getOptionsReadOnly',
96103
'setProductOptions',
@@ -129,14 +136,45 @@ public function testInitialize()
129136
$this->storeManagerMock,
130137
$this->stockFilterMock,
131138
$this->productLinksMock,
132-
$this->jsHelperMock
139+
$this->jsHelperMock,
140+
$this->dateFilterMock
133141
);
134142

135143
$productData = [
136144
'stock_data' => ['stock_data'],
137145
'options' => ['option1', 'option2']
138146
];
139147

148+
$attributeNonDate = $this->getMock('Magento\Catalog\Model\Resource\Eav\Attribute', [], [], '', false);
149+
$attributeDate = $this->getMock('Magento\Catalog\Model\Resource\Eav\Attribute', [], [], '', false);
150+
151+
$attributeNonDateBackEnd =
152+
$this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend', [], [], '', false);
153+
$attributeDateBackEnd =
154+
$this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\Datetime', [], [], '', false);
155+
156+
$attributeNonDate->expects($this->any())
157+
->method('getBackend')
158+
->will($this->returnValue($attributeNonDateBackEnd));
159+
160+
$attributeDate->expects($this->any())
161+
->method('getBackend')
162+
->will($this->returnValue($attributeDateBackEnd));
163+
164+
165+
$attributeNonDateBackEnd->expects($this->any())
166+
->method('getType')
167+
->will($this->returnValue('non-datetime'));
168+
169+
$attributeDateBackEnd->expects($this->any())
170+
->method('getType')
171+
->will($this->returnValue('datetime'));
172+
173+
$attributesArray = [
174+
$attributeNonDate,
175+
$attributeDate
176+
];
177+
140178
$useDefaults = ['attributeCode1', 'attributeCode2'];
141179

142180
$this->requestMock->expects($this->at(0))
@@ -186,6 +224,10 @@ public function testInitialize()
186224
->method('lockAttribute')
187225
->with('media');
188226

227+
$this->productMock->expects($this->once())
228+
->method('getAttributes')
229+
->will($this->returnValue($attributesArray));
230+
189231
$productData['category_ids'] = [];
190232
$productData['website_ids'] = [];
191233
$this->productMock->expects($this->once())
@@ -256,7 +298,8 @@ public function testMergeProductOptions($productOptions, $defaultOptions, $expec
256298
$this->storeManagerMock,
257299
$this->stockFilterMock,
258300
$this->productLinksMock,
259-
$this->jsHelperMock
301+
$this->jsHelperMock,
302+
$this->dateFilterMock
260303
);
261304
$result = $this->helper->mergeProductOptions($productOptions, $defaultOptions);
262305
$this->assertEquals($expectedResults, $result);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Backend;
7+
8+
class WeightTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Catalog\Model\Product\Attribute\Backend\Weight
12+
*/
13+
protected $model;
14+
15+
protected function setUp()
16+
{
17+
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
18+
19+
// we want to use an actual implementation of \Magento\Framework\Locale\FormatInterface
20+
$scopeResolver = $this->getMockForAbstractClass('\Magento\Framework\App\ScopeResolverInterface', [], '', false);
21+
$localeResolver = $this->getMockForAbstractClass('\Magento\Framework\Locale\ResolverInterface', [], '', false);
22+
$currencyFactory = $this->getMock('\Magento\Directory\Model\CurrencyFactory', [], [], '', false);
23+
$localeFormat = $objectHelper->getObject(
24+
'Magento\Framework\Locale\Format',
25+
[
26+
'scopeResolver' => $scopeResolver,
27+
'localeResolver' => $localeResolver,
28+
'currencyFactory' => $currencyFactory,
29+
]
30+
);
31+
32+
// the model we are testing
33+
$this->model = $objectHelper->getObject(
34+
'Magento\Catalog\Model\Product\Attribute\Backend\Weight',
35+
['localeFormat' => $localeFormat]
36+
);
37+
38+
$attribute = $this->getMockForAbstractClass(
39+
'\Magento\Eav\Model\Entity\Attribute\AbstractAttribute',
40+
[],
41+
'',
42+
false
43+
);
44+
$this->model->setAttribute($attribute);
45+
}
46+
47+
/**
48+
* Tests for the cases that expect to pass validation
49+
*
50+
* @dataProvider dataProviderValidate
51+
*/
52+
public function testValidate($value)
53+
{
54+
$object = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
55+
$object->expects($this->once())->method('getData')->willReturn($value);
56+
57+
$this->assertTrue($this->model->validate($object));
58+
}
59+
60+
/**
61+
* @return array
62+
*/
63+
public function dataProviderValidate()
64+
{
65+
return [
66+
'US simple' => ['1234.56'],
67+
'US full' => ['123,456.78'],
68+
'Brazil' => ['123.456,78'],
69+
'India' => ['1,23,456.78'],
70+
'Lebanon' => ['1 234'],
71+
'zero' => ['0.00'],
72+
'NaN becomes zero' => ['kiwi'],
73+
];
74+
}
75+
76+
/**
77+
* Tests for the cases that expect to fail validation
78+
*
79+
* @expectedException \Magento\Framework\Exception\LocalizedException
80+
* @dataProvider dataProviderValidateForFailure
81+
*/
82+
public function testValidateForFailure($value)
83+
{
84+
$object = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
85+
$object->expects($this->once())->method('getData')->willReturn($value);
86+
87+
$this->model->validate($object);
88+
$this->fail('Expected the following value to NOT validate: ' . $value);
89+
}
90+
91+
/**
92+
* @return array
93+
*/
94+
public function dataProviderValidateForFailure()
95+
{
96+
return [
97+
'negative US simple' => ['-1234.56'],
98+
'negative US full' => ['-123,456.78'],
99+
'negative Brazil' => ['-123.456,78'],
100+
'negative India' => ['-1,23,456.78'],
101+
'negative Lebanon' => ['-1 234'],
102+
];
103+
}
104+
}

0 commit comments

Comments
 (0)