Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 223dba5

Browse files
committed
MAGETWO-70705: [GitHub] Installation error in Tax module #10138
1 parent 14669ad commit 223dba5

File tree

6 files changed

+77
-24
lines changed

6 files changed

+77
-24
lines changed

app/code/Magento/Tax/Block/Adminhtml/Rate/Grid/Renderer/Data.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@ class Data extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRe
2222
* @param \Magento\Framework\DataObject $row
2323
* @return int|string
2424
* @since 2.0.0
25+
* @deprecated since it doesn't bring any value anymore
2526
*/
2627
protected function _getValue(\Magento\Framework\DataObject $row)
2728
{
28-
$data = parent::_getValue($row);
29-
if (intval($data) == $data) {
30-
return (string)number_format($data, 2);
31-
}
32-
if ($data !== null) {
33-
return $data * 1;
34-
}
35-
return $this->getColumn()->getDefault();
29+
return parent::_getValue($row);
3630
}
3731
}

app/code/Magento/Tax/Model/Calculation/Rate/Converter.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Tax\Model\Calculation\Rate;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Locale\FormatInterface;
10+
811
/**
912
* Tax Rate Model converter.
1013
*
@@ -25,17 +28,26 @@ class Converter
2528
*/
2629
protected $taxRateTitleDataObjectFactory;
2730

31+
/**
32+
* @var FormatInterface|null
33+
* @since 2.2.0
34+
*/
35+
private $format;
36+
2837
/**
2938
* @param \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory
30-
* @param \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory,
39+
* @param \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory ,
40+
* @param FormatInterface|null $format
3141
* @since 2.0.0
3242
*/
3343
public function __construct(
3444
\Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory,
35-
\Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory
45+
\Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory,
46+
FormatInterface $format = null
3647
) {
3748
$this->taxRateDataObjectFactory = $taxRateDataObjectFactory;
3849
$this->taxRateTitleDataObjectFactory = $taxRateTitleDataObjectFactory;
50+
$this->format = $format ?: ObjectManager::getInstance()->get(FormatInterface::class);
3951
}
4052

4153
/**
@@ -131,7 +143,7 @@ public function populateTaxRateData($formData)
131143
->setTaxRegionId($this->extractFormData($formData, 'tax_region_id'))
132144
->setTaxPostcode($this->extractFormData($formData, 'tax_postcode'))
133145
->setCode($this->extractFormData($formData, 'code'))
134-
->setRate($this->extractFormData($formData, 'rate'));
146+
->setRate($this->format->getNumber($this->extractFormData($formData, 'rate')));
135147
if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
136148
$taxRate->setZipFrom($this->extractFormData($formData, 'zip_from'))
137149
->setZipTo($this->extractFormData($formData, 'zip_to'))->setZipIsRange(1);

app/code/Magento/Tax/Model/Calculation/RateRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private function validate(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
283283
);
284284
}
285285

286-
if (!\Zend_Validate::is($taxRate->getRate(), 'Float')) {
286+
if (!is_numeric($taxRate->getRate()) || $taxRate->getRate() < 0) {
287287
$exception->addError(__('%fieldName is a required field.', ['fieldName' => 'percentage_rate']));
288288
}
289289

app/code/Magento/Tax/Setup/UpgradeData.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,13 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
9393
$taxRateList = $this->taxRateRepository->getList($this->searchCriteriaFactory->create());
9494
/** @var \Magento\Tax\Api\Data\TaxRateInterface $taxRateData */
9595
foreach ($taxRateList->getItems() as $taxRateData) {
96-
if (!empty($taxRateData->getData('percentage_rate'))) {
97-
$regionCode = $this->parseRegionFromTaxCode($taxRateData->getCode());
98-
if ($regionCode) {
99-
/** @var \Magento\Directory\Model\Region $region */
100-
$region = $this->directoryRegionFactory->create();
101-
$region->loadByCode($regionCode, $taxRateData->getTaxCountryId());
102-
$taxRateData->setTaxRegionId($region->getRegionId());
103-
$this->taxRateRepository->save($taxRateData);
104-
}
96+
$regionCode = $this->parseRegionFromTaxCode($taxRateData->getCode());
97+
if ($regionCode) {
98+
/** @var \Magento\Directory\Model\Region $region */
99+
$region = $this->directoryRegionFactory->create();
100+
$region->loadByCode($regionCode, $taxRateData->getTaxCountryId());
101+
$taxRateData->setTaxRegionId($region->getRegionId());
102+
$this->taxRateRepository->save($taxRateData);
105103
}
106104
}
107105
}

app/code/Magento/Tax/Test/Unit/Model/Calculation/Rate/ConverterTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ class ConverterTest extends \PHPUnit_Framework_TestCase
1515
protected $converter;
1616

1717
/**
18-
* @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory
18+
* @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
1919
*/
2020
protected $taxRateDataObjectFactory;
2121

2222
/**
23-
* @var \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory
23+
* @var \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
2424
*/
2525
protected $taxRateTitleDataObjectFactory;
2626

27+
/**
28+
* @var \Magento\Framework\Locale\FormatInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $format;
31+
2732
/**
2833
* @var \Magento\Framework\TestFramework\Unit\Helper
2934
*/
@@ -45,12 +50,16 @@ protected function setUp()
4550
->setMethods(['create'])
4651
->getMock();
4752

53+
$this->format = $this->getMockBuilder(\Magento\Framework\Locale\FormatInterface::class)
54+
->getMock();
55+
4856
$this->objectManager = new ObjectManager($this);
4957
$this->converter = $this->objectManager->getObject(
5058
\Magento\Tax\Model\Calculation\Rate\Converter::class,
5159
[
5260
'taxRateDataObjectFactory' => $this->taxRateDataObjectFactory,
5361
'taxRateTitleDataObjectFactory' => $this->taxRateTitleDataObjectFactory,
62+
'format' => $this->format,
5463
]
5564
);
5665
}
@@ -109,12 +118,14 @@ public function testPopulateTaxRateData()
109118
$taxRate = $this->objectManager->getObject(
110119
\Magento\Tax\Model\Calculation\Rate::class,
111120
[
112-
'data' =>$dataArray,
121+
'data' => $dataArray,
113122
]
114123
);
115124

116125
$this->taxRateDataObjectFactory->expects($this->once())->method('create')->willReturn($taxRate);
117126

127+
$this->format->expects($this->once())->method('getNumber')->willReturnArgument(0);
128+
118129
$this->assertSame($taxRate, $this->converter->populateTaxRateData($dataArray));
119130
$this->assertEquals($taxRate->getTitles(), $rateTitles);
120131
}

app/code/Magento/Tax/Test/Unit/Model/Calculation/RateRepositoryTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,42 @@ public function testValidateWithNoRate()
474474
);
475475
$this->model->save($rateMock);
476476
}
477+
478+
/**
479+
* @expectedException \Magento\Framework\Exception\InputException
480+
* @expectedExceptionMessage percentage_rate is a required field.
481+
*/
482+
public function testValidateWithWrongRate()
483+
{
484+
$rateTitles = ['Label 1', 'Label 2'];
485+
486+
$countryCode = 'US';
487+
$countryMock = $this->getMock(\Magento\Directory\Model\Country::class, [], [], '', false);
488+
$countryMock->expects($this->any())->method('getId')->will($this->returnValue(1));
489+
$countryMock->expects($this->any())->method('loadByCode')->with($countryCode)->will($this->returnSelf());
490+
$this->countryFactoryMock->expects($this->once())->method('create')->will($this->returnValue($countryMock));
491+
492+
$regionId = 2;
493+
$regionMock = $this->getMock(\Magento\Directory\Model\Region::class, [], [], '', false);
494+
$regionMock->expects($this->any())->method('getId')->will($this->returnValue($regionId));
495+
$regionMock->expects($this->any())->method('load')->with($regionId)->will($this->returnSelf());
496+
$this->regionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($regionMock));
497+
498+
$rateMock = $this->getTaxRateMock(
499+
[
500+
'id' => null,
501+
'tax_country_id' => $countryCode,
502+
'tax_region_id' => $regionId,
503+
'region_name' => null,
504+
'tax_postcode' => null,
505+
'zip_is_range' => true,
506+
'zip_from' => 90000,
507+
'zip_to' => 90005,
508+
'rate' => '7,9',
509+
'code' => 'Tax Rate Code',
510+
'titles' => $rateTitles,
511+
]
512+
);
513+
$this->model->save($rateMock);
514+
}
477515
}

0 commit comments

Comments
 (0)