From 36413b83c6badc4ad7dd0df5dcdd365439bcd593 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 6 Jul 2015 14:56:21 -0500 Subject: [PATCH] MAGETWO-36706: [GITHUB] Won't accept valid Brazil monetary value as input #1192 - unit tests --- .../Product/Initialization/HelperTest.php | 47 +++++++- .../Product/Attribute/Backend/WeightTest.php | 104 ++++++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Backend/WeightTest.php diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php index 78cd0113a6afd..58c02ff2b0de2 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php @@ -44,6 +44,11 @@ class HelperTest extends \PHPUnit_Framework_TestCase */ protected $websiteMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $dateFilterMock; + /** * @var int */ @@ -66,6 +71,7 @@ protected function setUp() $this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false); $this->websiteMock = $this->getMock('Magento\Store\Model\Website', [], [], '', false); $this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface'); + $this->dateFilterMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\Filter\Date', [], [], '', false); $this->stockFilterMock = $this->getMock( 'Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter', @@ -91,6 +97,7 @@ protected function setUp() 'setWebsiteIds', 'isLockedAttribute', 'lockAttribute', + 'getAttributes', 'unlockAttribute', 'getOptionsReadOnly', 'setProductOptions', @@ -129,7 +136,8 @@ public function testInitialize() $this->storeManagerMock, $this->stockFilterMock, $this->productLinksMock, - $this->jsHelperMock + $this->jsHelperMock, + $this->dateFilterMock ); $productData = [ @@ -137,6 +145,36 @@ public function testInitialize() 'options' => ['option1', 'option2'] ]; + $attributeNonDate = $this->getMock('Magento\Catalog\Model\Resource\Eav\Attribute', [], [], '', false); + $attributeDate = $this->getMock('Magento\Catalog\Model\Resource\Eav\Attribute', [], [], '', false); + + $attributeNonDateBackEnd = + $this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend', [], [], '', false); + $attributeDateBackEnd = + $this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\Datetime', [], [], '', false); + + $attributeNonDate->expects($this->any()) + ->method('getBackend') + ->will($this->returnValue($attributeNonDateBackEnd)); + + $attributeDate->expects($this->any()) + ->method('getBackend') + ->will($this->returnValue($attributeDateBackEnd)); + + + $attributeNonDateBackEnd->expects($this->any()) + ->method('getType') + ->will($this->returnValue('non-datetime')); + + $attributeDateBackEnd->expects($this->any()) + ->method('getType') + ->will($this->returnValue('datetime')); + + $attributesArray = [ + $attributeNonDate, + $attributeDate + ]; + $useDefaults = ['attributeCode1', 'attributeCode2']; $this->requestMock->expects($this->at(0)) @@ -186,6 +224,10 @@ public function testInitialize() ->method('lockAttribute') ->with('media'); + $this->productMock->expects($this->once()) + ->method('getAttributes') + ->will($this->returnValue($attributesArray)); + $productData['category_ids'] = []; $productData['website_ids'] = []; $this->productMock->expects($this->once()) @@ -256,7 +298,8 @@ public function testMergeProductOptions($productOptions, $defaultOptions, $expec $this->storeManagerMock, $this->stockFilterMock, $this->productLinksMock, - $this->jsHelperMock + $this->jsHelperMock, + $this->dateFilterMock ); $result = $this->helper->mergeProductOptions($productOptions, $defaultOptions); $this->assertEquals($expectedResults, $result); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Backend/WeightTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Backend/WeightTest.php new file mode 100644 index 0000000000000..2b8ca53742888 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Backend/WeightTest.php @@ -0,0 +1,104 @@ +getMockForAbstractClass('\Magento\Framework\App\ScopeResolverInterface', [], '', false); + $localeResolver = $this->getMockForAbstractClass('\Magento\Framework\Locale\ResolverInterface', [], '', false); + $currencyFactory = $this->getMock('\Magento\Directory\Model\CurrencyFactory', [], [], '', false); + $localeFormat = $objectHelper->getObject( + 'Magento\Framework\Locale\Format', + [ + 'scopeResolver' => $scopeResolver, + 'localeResolver' => $localeResolver, + 'currencyFactory' => $currencyFactory, + ] + ); + + // the model we are testing + $this->model = $objectHelper->getObject( + 'Magento\Catalog\Model\Product\Attribute\Backend\Weight', + ['localeFormat' => $localeFormat] + ); + + $attribute = $this->getMockForAbstractClass( + '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute', + [], + '', + false + ); + $this->model->setAttribute($attribute); + } + + /** + * Tests for the cases that expect to pass validation + * + * @dataProvider dataProviderValidate + */ + public function testValidate($value) + { + $object = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); + $object->expects($this->once())->method('getData')->willReturn($value); + + $this->assertTrue($this->model->validate($object)); + } + + /** + * @return array + */ + public function dataProviderValidate() + { + return [ + 'US simple' => ['1234.56'], + 'US full' => ['123,456.78'], + 'Brazil' => ['123.456,78'], + 'India' => ['1,23,456.78'], + 'Lebanon' => ['1 234'], + 'zero' => ['0.00'], + 'NaN becomes zero' => ['kiwi'], + ]; + } + + /** + * Tests for the cases that expect to fail validation + * + * @expectedException \Magento\Framework\Exception\LocalizedException + * @dataProvider dataProviderValidateForFailure + */ + public function testValidateForFailure($value) + { + $object = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); + $object->expects($this->once())->method('getData')->willReturn($value); + + $this->model->validate($object); + $this->fail('Expected the following value to NOT validate: ' . $value); + } + + /** + * @return array + */ + public function dataProviderValidateForFailure() + { + return [ + 'negative US simple' => ['-1234.56'], + 'negative US full' => ['-123,456.78'], + 'negative Brazil' => ['-123.456,78'], + 'negative India' => ['-1,23,456.78'], + 'negative Lebanon' => ['-1 234'], + ]; + } +}