Skip to content

Commit 004019c

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-56156' into BUGS
2 parents 8cfa3c6 + ef9c0c9 commit 004019c

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

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

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

265-
if (!\Zend_Validate::is($taxRate->getRate(), 'NotEmpty')) {
265+
if (!\Zend_Validate::is($taxRate->getRate(), 'Float')) {
266266
$exception->addError(__('%fieldName is a required field.', ['fieldName' => 'percentage_rate']));
267267
}
268268

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,42 @@ public function testValidate()
434434
);
435435
$this->model->save($rateMock);
436436
}
437+
438+
/**
439+
* @expectedException \Magento\Framework\Exception\InputException
440+
* @expectedExceptionMessage percentage_rate is a required field.
441+
*/
442+
public function testValidateWithNoRate()
443+
{
444+
$rateTitles = ['Label 1', 'Label 2'];
445+
446+
$countryCode = 'US';
447+
$countryMock = $this->getMock(\Magento\Directory\Model\Country::class, [], [], '', false);
448+
$countryMock->expects($this->any())->method('getId')->will($this->returnValue(1));
449+
$countryMock->expects($this->any())->method('loadByCode')->with($countryCode)->will($this->returnSelf());
450+
$this->countryFactoryMock->expects($this->once())->method('create')->will($this->returnValue($countryMock));
451+
452+
$regionId = 2;
453+
$regionMock = $this->getMock(\Magento\Directory\Model\Region::class, [], [], '', false);
454+
$regionMock->expects($this->any())->method('getId')->will($this->returnValue($regionId));
455+
$regionMock->expects($this->any())->method('load')->with($regionId)->will($this->returnSelf());
456+
$this->regionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($regionMock));
457+
458+
$rateMock = $this->getTaxRateMock(
459+
[
460+
'id' => null,
461+
'tax_country_id' => $countryCode,
462+
'tax_region_id' => $regionId,
463+
'region_name' => null,
464+
'tax_postcode' => null,
465+
'zip_is_range' => true,
466+
'zip_from' => 90000,
467+
'zip_to' => 90005,
468+
'rate' => '',
469+
'code' => 'Tax Rate Code',
470+
'titles' => $rateTitles,
471+
]
472+
);
473+
$this->model->save($rateMock);
474+
}
437475
}

dev/tests/api-functional/testsuite/Magento/Tax/Api/TaxRateRepositoryTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,44 @@ public function testCreateTaxRateExistingCode()
138138
}
139139
}
140140

141+
public function testCreateTaxRateWithoutValue()
142+
{
143+
$data = [
144+
'tax_rate' => [
145+
'tax_country_id' => 'US',
146+
'tax_region_id' => 12,
147+
'tax_postcode' => '*',
148+
'code' => 'US-CA-*-Rate 1',
149+
],
150+
];
151+
152+
$serviceInfo = [
153+
'rest' => [
154+
'resourcePath' => self::RESOURCE_PATH,
155+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
156+
],
157+
'soap' => [
158+
'service' => self::SERVICE_NAME,
159+
'serviceVersion' => self::SERVICE_VERSION,
160+
'operation' => self::SERVICE_NAME . 'Save',
161+
],
162+
];
163+
try {
164+
$this->_webApiCall($serviceInfo, $data);
165+
$this->fail('Expected exception was not raised');
166+
} catch (\SoapFault $e) {
167+
$this->assertContains(
168+
'SOAP-ERROR: Encoding: object has no \'rate\' property',
169+
$e->getMessage(),
170+
'SoapFault does not contain expected message.'
171+
);
172+
} catch (\Exception $e) {
173+
$errorObj = $this->processRestExceptionResult($e);
174+
$this->assertEquals('%fieldName is a required field.', $errorObj['message']);
175+
$this->assertEquals(['fieldName' => 'percentage_rate'], $errorObj['parameters']);
176+
}
177+
}
178+
141179
public function testCreateTaxRate()
142180
{
143181
$data = [
@@ -207,6 +245,41 @@ public function testCreateTaxRateWithZipRange()
207245
$taxRate->delete();
208246
}
209247

248+
public function testCreateTaxRateWithZeroValue()
249+
{
250+
$data = [
251+
'tax_rate' => [
252+
'tax_country_id' => 'US',
253+
'tax_region_id' => 12,
254+
'tax_postcode' => '*',
255+
'code' => 'Test Tax Rate ' . microtime(),
256+
'rate' => '0.0',
257+
],
258+
];
259+
260+
$serviceInfo = [
261+
'rest' => [
262+
'resourcePath' => self::RESOURCE_PATH,
263+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
264+
],
265+
'soap' => [
266+
'service' => self::SERVICE_NAME,
267+
'serviceVersion' => self::SERVICE_VERSION,
268+
'operation' => self::SERVICE_NAME . 'Save',
269+
],
270+
];
271+
$result = $this->_webApiCall($serviceInfo, $data);
272+
$this->assertArrayHasKey('id', $result);
273+
$taxRateId = $result['id'];
274+
/** Ensure that tax rate was actually created in DB */
275+
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
276+
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
277+
$taxModel = $taxRate->load($taxRateId);
278+
$this->assertEquals($taxRateId, $taxModel->getId(), 'Tax rate was not created in DB.');
279+
$this->assertEquals(0, $taxModel->getRate(), 'Tax rate value is wrong.');
280+
$taxRate->delete();
281+
}
282+
210283
/**
211284
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
212285
*/

dev/tests/integration/testsuite/Magento/Tax/Model/Calculation/RateRepositoryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,38 @@ public function testSave()
104104
$this->assertNotNull($taxRateServiceData->getId());
105105
}
106106

107+
/**
108+
* @magentoDbIsolation enabled
109+
*/
110+
public function testSaveWithZeroValue()
111+
{
112+
$taxData = [
113+
'tax_country_id' => 'US',
114+
'tax_region_id' => '8',
115+
'rate' => '0',
116+
'code' => 'US-CA-*-Rate' . rand(),
117+
'zip_is_range' => true,
118+
'zip_from' => 78765,
119+
'zip_to' => 78780,
120+
];
121+
// Tax rate data object created
122+
$taxRate = $this->taxRateFactory->create();
123+
$this->dataObjectHelper->populateWithArray($taxRate, $taxData, \Magento\Tax\Api\Data\TaxRateInterface::class);
124+
//Tax rate service call
125+
$taxRateServiceData = $this->rateRepository->save($taxRate);
126+
127+
//Assertions
128+
$this->assertInstanceOf(\Magento\Tax\Api\Data\TaxRateInterface::class, $taxRateServiceData);
129+
$this->assertEquals($taxData['tax_country_id'], $taxRateServiceData->getTaxCountryId());
130+
$this->assertEquals($taxData['tax_region_id'], $taxRateServiceData->getTaxRegionId());
131+
$this->assertEquals($taxData['rate'], $taxRateServiceData->getRate());
132+
$this->assertEquals($taxData['code'], $taxRateServiceData->getCode());
133+
$this->assertEquals($taxData['zip_from'], $taxRateServiceData->getZipFrom());
134+
$this->assertEquals($taxData['zip_to'], $taxRateServiceData->getZipTo());
135+
$this->assertEquals('78765-78780', $taxRateServiceData->getTaxPostcode());
136+
$this->assertNotNull($taxRateServiceData->getId());
137+
}
138+
107139
/**
108140
* @magentoDbIsolation enabled
109141
* @magentoDataFixture Magento/Store/_files/store.php

0 commit comments

Comments
 (0)