ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,326 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tax\Api;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Tax\Api\Data\TaxClassInterfaceFactory;
use Magento\Tax\Model\ClassModel;
use Magento\Tax\Model\ClassModelRegistry;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
/**
* Tests for tax class service.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TaxClassRepositoryTest extends WebapiAbstract
{
const SERVICE_NAME = 'taxTaxClassRepositoryV1';
const SERVICE_VERSION = 'V1';
const RESOURCE_PATH = '/V1/taxClasses';
/** @var SearchCriteriaBuilder */
private $searchCriteriaBuilder;
/** @var FilterBuilder */
private $filterBuilder;
/** @var SortOrderBuilder */
private $sortOrderBuilder;
/** @var TaxClassInterfaceFactory */
private $taxClassFactory;
/** @var TaxClassRepositoryInterface */
private $taxClassRepository;
/** @var ClassModelRegistry */
private $taxClassRegistry;
const SAMPLE_TAX_CLASS_NAME = 'Wholesale Customer';
/**
* Execute per test initialization.
*/
protected function setUp(): void
{
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(
\Magento\Framework\Api\SearchCriteriaBuilder::class
);
$this->filterBuilder = Bootstrap::getObjectManager()->create(
\Magento\Framework\Api\FilterBuilder::class
);
$this->taxClassFactory = Bootstrap::getObjectManager()->create(
\Magento\Tax\Api\Data\TaxClassInterfaceFactory::class
);
$this->taxClassRegistry = Bootstrap::getObjectManager()->create(
\Magento\Tax\Model\ClassModelRegistry::class
);
$this->taxClassRepository = Bootstrap::getObjectManager()->create(
\Magento\Tax\Model\TaxClass\Repository::class,
['classModelRegistry' => $this->taxClassRegistry]
);
$this->sortOrderBuilder = Bootstrap::getObjectManager()->create(
\Magento\Framework\Api\SortOrderBuilder::class
);
}
/**
* Test create Data\TaxClassInterface
*/
public function testCreateTaxClass()
{
$taxClassName = self::SAMPLE_TAX_CLASS_NAME . uniqid();
/** @var \Magento\Tax\Api\Data\TaxClassInterface $taxClassDataObject */
$taxClassDataObject = $this->taxClassFactory->create();
$taxClassDataObject->setClassName($taxClassName)
->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$requestData = ['taxClass' => [
'class_id' => $taxClassDataObject->getClassId(),
'class_name' => $taxClassDataObject->getClassName(),
'class_type' => $taxClassDataObject->getClassType(),
],
];
$taxClassId = $this->_webApiCall($serviceInfo, $requestData);
$this->assertNotNull($taxClassId);
//Verify by getting the Data\TaxClassInterface
$taxClassData = $this->taxClassRepository->get($taxClassId);
$this->assertEquals($taxClassData->getClassName(), $taxClassName);
$this->assertEquals($taxClassData->getClassType(), TaxClassManagementInterface::TYPE_CUSTOMER);
}
/**
* Test create Data\TaxClassInterface
*/
public function testUpdateTaxClass()
{
//Create Tax Class
$taxClassDataObject = $this->taxClassFactory->create();
$taxClassDataObject->setClassName(self::SAMPLE_TAX_CLASS_NAME . uniqid())
->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
$taxClassId = $this->taxClassRepository->save($taxClassDataObject);
$this->assertNotNull($taxClassId);
//Update Tax Class
$updatedTaxClassName = self::SAMPLE_TAX_CLASS_NAME . uniqid();
$updatedTaxClassDataObject = $taxClassDataObject;
$updatedTaxClassDataObject->setClassName($updatedTaxClassName);
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/' . $taxClassId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$taxClass = [
'class_id' => $updatedTaxClassDataObject->getClassId(),
'class_name' => $updatedTaxClassDataObject->getClassName(),
'class_type' => $updatedTaxClassDataObject->getClassType(),
];
$requestData = ['taxClass' => $taxClass, 'ClassId' => $taxClassId];
$this->assertEquals($taxClassId, $this->_webApiCall($serviceInfo, $requestData));
//Verify by getting the Data\TaxClassInterface
$this->taxClassRegistry->remove($taxClassId);
$taxClassData = $this->taxClassRepository->get($taxClassId);
$this->assertEquals($taxClassData->getClassName(), $updatedTaxClassName);
}
public function testGetTaxClass()
{
//Create Tax Class
$taxClassName = self::SAMPLE_TAX_CLASS_NAME . uniqid();
$taxClassDataObject = $this->taxClassFactory->create();
$taxClassDataObject->setClassName($taxClassName)
->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
$taxClassId = $this->taxClassRepository->save($taxClassDataObject);
$this->assertNotNull($taxClassId);
//Verify by getting the Data\TaxClassInterface
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/' . $taxClassId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$requestData = ['taxClassId' => $taxClassId];
$taxClassData = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals($taxClassData[ClassModel::KEY_NAME], $taxClassName);
$this->assertEquals(
$taxClassData[ClassModel::KEY_TYPE],
TaxClassManagementInterface::TYPE_CUSTOMER
);
}
/**
* Test delete Tax class
*/
public function testDeleteTaxClass()
{
$taxClassDataObject = $this->taxClassFactory->create();
$taxClassDataObject->setClassName(self::SAMPLE_TAX_CLASS_NAME . uniqid())
->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
$taxClassId = $this->taxClassRepository->save($taxClassDataObject);
$this->assertNotNull($taxClassId);
//Verify by getting the Data\TaxClassInterface
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/' . $taxClassId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'DeleteById',
],
];
$requestData = ['taxClassId' => $taxClassId];
$result = $this->_webApiCall($serviceInfo, $requestData);
$this->assertTrue($result);
try {
$this->taxClassRegistry->remove($taxClassId);
$this->taxClassRepository->get($taxClassId);
$this->fail("Tax class was not expected to be returned after being deleted.");
} catch (NoSuchEntityException $e) {
$this->assertEquals('No such entity with class_id = ' . $taxClassId, $e->getMessage());
}
}
/**
* Test with a single filter
*/
public function testSearchTaxClass()
{
$taxClassName = 'Retail Customer';
$taxClassNameField = ClassModel::KEY_NAME;
$filter = $this->filterBuilder->setField($taxClassNameField)
->setValue($taxClassName)
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchData = $this->searchCriteriaBuilder->create()->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$this->assertEquals($taxClassName, $searchResults['items'][0][$taxClassNameField]);
}
/**
* Test using multiple filters
*/
public function testSearchTaxClassMultipleFilterGroups()
{
$productTaxClass = [
ClassModel::KEY_NAME => 'Taxable Goods',
ClassModel::KEY_TYPE => 'PRODUCT',
];
$customerTaxClass = [ClassModel::KEY_NAME => 'Retail Customer',
ClassModel::KEY_TYPE => 'CUSTOMER', ];
$filter1 = $this->filterBuilder->setField(ClassModel::KEY_NAME)
->setValue($productTaxClass[ClassModel::KEY_NAME])
->create();
$filter2 = $this->filterBuilder->setField(ClassModel::KEY_NAME)
->setValue($customerTaxClass[ClassModel::KEY_NAME])
->create();
$filter3 = $this->filterBuilder->setField(ClassModel::KEY_TYPE)
->setValue($productTaxClass[ClassModel::KEY_TYPE])
->create();
$filter4 = $this->filterBuilder->setField(ClassModel::KEY_TYPE)
->setValue($customerTaxClass[ClassModel::KEY_TYPE])
->create();
$sortOrder = $this->sortOrderBuilder->setField("class_type")
->setDirection("ASC")->create();
/**
* (class_name == 'Retail Customer' || class_name == 'Taxable Goods)
* && ( class_type == 'CUSTOMER' || class_type == 'PRODUCT')
*/
$this->searchCriteriaBuilder->addFilters([$filter1, $filter2]);
$this->searchCriteriaBuilder->addFilters([$filter3, $filter4]);
$this->searchCriteriaBuilder->addSortOrder($sortOrder);
$searchCriteria = $this->searchCriteriaBuilder->setCurrentPage(1)->setPageSize(10)->create();
$searchData = $searchCriteria->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(2, $searchResults['total_count']);
$this->assertEquals(
$customerTaxClass[ClassModel::KEY_NAME],
$searchResults['items'][0][ClassModel::KEY_NAME]
);
$this->assertEquals(
$productTaxClass[ClassModel::KEY_NAME],
$searchResults['items'][1][ClassModel::KEY_NAME]
);
/** class_name == 'Retail Customer' && ( class_type == 'CUSTOMER' || class_type == 'PRODUCT') */
$this->searchCriteriaBuilder->addFilters([$filter2]);
$this->searchCriteriaBuilder->addFilters([$filter3, $filter4]);
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchData = $searchCriteria->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo['rest']['resourcePath'] = self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData);
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$this->assertEquals(
$customerTaxClass[ClassModel::KEY_NAME],
$searchResults['items'][0][ClassModel::KEY_NAME]
);
}
}
@@ -0,0 +1,750 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tax\Api;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Tax\Model\Calculation\Rate;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TaxRateRepositoryTest extends WebapiAbstract
{
const SERVICE_NAME = "taxTaxRateRepositoryV1";
const SERVICE_VERSION = "V1";
const RESOURCE_PATH = "/V1/taxRates";
/** @var \Magento\Tax\Model\Calculation\Rate[] */
private $fixtureTaxRates;
/** @var \Magento\Tax\Model\ClassModel[] */
private $fixtureTaxClasses;
/** @var \Magento\Tax\Model\Calculation\Rule[] */
private $fixtureTaxRules;
/**
* @var \Magento\Tax\Api\TaxRateRepositoryInterface
*/
private $taxRateService;
/** @var FilterBuilder */
private $filterBuilder;
/** @var SearchCriteriaBuilder */
private $searchCriteriaBuilder;
/** @var SortOrderBuilder */
private $sortOrderBuilder;
/**
* Other rates created during tests, to be deleted in tearDown()
*
* @var \Magento\Tax\Model\Calculation\Rate[]
*/
private $otherRates = [];
/**
* Execute per test initialization.
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->taxRateService = $objectManager->get(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
$this->searchCriteriaBuilder = $objectManager->create(
\Magento\Framework\Api\SearchCriteriaBuilder::class
);
$this->filterBuilder = $objectManager->create(
\Magento\Framework\Api\FilterBuilder::class
);
$this->sortOrderBuilder = $objectManager->create(
\Magento\Framework\Api\SortOrderBuilder::class
);
/** Initialize tax classes, tax rates and tax rules defined in fixture Magento/Tax/_files/tax_classes.php */
$this->getFixtureTaxRates();
$this->getFixtureTaxClasses();
$this->getFixtureTaxRules();
}
protected function tearDown(): void
{
$taxRules = $this->getFixtureTaxRules();
if (count($taxRules)) {
$taxRates = $this->getFixtureTaxRates();
$taxClasses = $this->getFixtureTaxClasses();
foreach ($taxRules as $taxRule) {
$taxRule->delete();
}
foreach ($taxRates as $taxRate) {
$taxRate->delete();
}
foreach ($taxClasses as $taxClass) {
$taxClass->delete();
}
}
if (count($this->otherRates)) {
foreach ($this->otherRates as $taxRate) {
$taxRate->delete();
}
}
}
public function testCreateTaxRateExistingCode()
{
$expectedMessage = '%1 already exists.';
$data = [
'tax_rate' => [
'tax_country_id' => 'US',
'tax_region_id' => 12,
'tax_postcode' => '*',
'code' => 'US-CA-*-Rate 1',
'rate' => '8.2501',
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
try {
$this->_webApiCall($serviceInfo, $data);
$this->fail('Expected exception was not raised');
} catch (\SoapFault $e) {
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
'SoapFault does not contain expected message.'
);
} catch (\Exception $e) {
$errorObj = $this->processRestExceptionResult($e);
$this->assertEquals($expectedMessage, $errorObj['message']);
$this->assertEquals(['Code'], $errorObj['parameters']);
}
}
public function testCreateTaxRateWithoutValue()
{
$data = [
'tax_rate' => [
'tax_country_id' => 'US',
'tax_region_id' => 12,
'tax_postcode' => '*',
'code' => 'US-CA-*-Rate 1',
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
try {
$this->_webApiCall($serviceInfo, $data);
$this->fail('Expected exception was not raised');
} catch (\SoapFault $e) {
$this->assertStringContainsString(
'SOAP-ERROR: Encoding: object has no \'rate\' property',
$e->getMessage(),
'SoapFault does not contain expected message.'
);
} catch (\Exception $e) {
$errorObj = $this->processRestExceptionResult($e);
$this->assertEquals('"%fieldName" is required. Enter and try again.', $errorObj['message']);
$this->assertEquals(['fieldName' => 'percentage_rate'], $errorObj['parameters']);
}
}
public function testCreateTaxRate()
{
$data = [
'tax_rate' => [
'tax_country_id' => 'US',
'tax_region_id' => 12,
'tax_postcode' => '*',
'code' => 'Test Tax Rate ' . microtime(),
'rate' => '8.2501',
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$result = $this->_webApiCall($serviceInfo, $data);
$this->assertArrayHasKey('id', $result);
$taxRateId = $result['id'];
/** Ensure that tax rate was actually created in DB */
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$this->assertEquals($taxRateId, $taxRate->load($taxRateId)->getId(), 'Tax rate was not created in DB.');
$taxRate->delete();
}
public function testCreateTaxRateWithZipRange()
{
$data = [
'tax_rate' => [
'tax_country_id' => 'US',
'tax_region_id' => 12,
'code' => 'Test Tax Rate ' . microtime(),
'rate' => '8.2501',
'zip_is_range' => 1,
'zip_from' => 17,
'zip_to' => 25,
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$result = $this->_webApiCall($serviceInfo, $data);
$this->assertArrayHasKey('id', $result);
$taxRateId = $result['id'];
/** Ensure that tax rate was actually created in DB */
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$this->assertEquals($taxRateId, $taxRate->load($taxRateId)->getId(), 'Tax rate was not created in DB.');
$this->assertEquals('17-25', $taxRate->getTaxPostcode(), 'Zip range is not saved in DB.');
$taxRate->delete();
}
public function testCreateTaxRateWithZeroValue()
{
$data = [
'tax_rate' => [
'tax_country_id' => 'US',
'tax_region_id' => 12,
'tax_postcode' => '*',
'code' => 'Test Tax Rate ' . microtime(),
'rate' => '0.0',
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$result = $this->_webApiCall($serviceInfo, $data);
$this->assertArrayHasKey('id', $result);
$taxRateId = $result['id'];
/** Ensure that tax rate was actually created in DB */
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$taxModel = $taxRate->load($taxRateId);
$this->assertEquals($taxRateId, $taxModel->getId(), 'Tax rate was not created in DB.');
$this->assertEquals(0, $taxModel->getRate(), 'Tax rate value is wrong.');
$taxRate->delete();
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testUpdateTaxRate()
{
$fixtureRate = $this->getFixtureTaxRates()[0];
$data = [
'tax_rate' => [
'id' => $fixtureRate->getId(),
'tax_region_id' => 43,
'tax_country_id' => 'US',
'tax_postcode' => '07400',
'code' => 'Test Tax Rate ' . microtime(),
'rate' => 3.456,
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$this->_webApiCall($serviceInfo, $data);
$expectedRateData = $data['tax_rate'];
/** Ensure that tax rate was actually updated in DB */
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$taxRateModel = $taxRate->load($fixtureRate->getId());
$this->assertEquals($expectedRateData['id'], $taxRateModel->getId(), 'Tax rate was not updated in DB.');
$this->assertEquals(
$expectedRateData['tax_region_id'],
$taxRateModel->getTaxRegionId(),
'Tax rate was not updated in DB.'
);
$this->assertEquals(
$expectedRateData['tax_country_id'],
$taxRateModel->getTaxCountryId(),
'Tax rate was not updated in DB.'
);
$this->assertEquals(
$expectedRateData['tax_postcode'],
$taxRateModel->getTaxPostcode(),
'Tax rate was not updated in DB.'
);
$this->assertEquals($expectedRateData['code'], $taxRateModel->getCode(), 'Tax rate was not updated in DB.');
$this->assertEquals(
$expectedRateData['rate'],
$taxRateModel->getRate(),
'Tax rate was not updated in DB.'
);
}
public function testUpdateTaxRateNotExisting()
{
$data = [
'tax_rate' => [
'id' => 555,
'tax_region_id' => 43,
'tax_country_id' => 'US',
'tax_postcode' => '07400',
'code' => 'Test Tax Rate ' . microtime(),
'rate' => 3.456,
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
try {
$this->_webApiCall($serviceInfo, $data);
$this->fail('Expected exception was not raised');
} catch (\Exception $e) {
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
"Exception does not contain expected message."
);
}
}
public function testGetTaxRate()
{
$taxRateId = 2;
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$result = $this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
$expectedRateData = [
'id' => 2,
'tax_country_id' => 'US',
'tax_region_id' => 43,
'tax_postcode' => '*',
'code' => 'US-NY-*-Rate 1',
'rate' => 8.375,
'titles' => [],
'region_name' => 'NY',
];
$this->assertEquals($expectedRateData, $result);
}
public function testGetTaxRateNotExist()
{
$taxRateId = 37865;
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
try {
$this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
$this->fail('Expected exception was not raised');
} catch (\Exception $e) {
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
"Exception does not contain expected message."
);
}
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testDeleteTaxRate()
{
/** Tax rules must be deleted since tax rate cannot be deleted if there are any tax rules associated with it */
$taxRules = $this->getFixtureTaxRules();
foreach ($taxRules as $taxRule) {
$taxRule->delete();
}
$fixtureRate = $this->getFixtureTaxRates()[0];
$taxRateId = $fixtureRate->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'DeleteById',
],
];
$result = $this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
$this->assertTrue($result);
/** Ensure that tax rate was actually removed from DB */
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$this->assertNull($taxRate->load($taxRateId)->getId(), 'Tax rate was not deleted from DB.');
}
/**
* Insure that tax rate cannot be deleted if it is used for a tax rule.
*
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testCannotDeleteTaxRate()
{
$fixtureRate = $this->getFixtureTaxRates()[0];
$taxRateId = $fixtureRate->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRateId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'DeleteById',
],
];
try {
$this->_webApiCall($serviceInfo, ['rateId' => $taxRateId]);
$this->fail('Expected exception was not raised');
} catch (\Exception $e) {
$expectedMessage = "The tax rate can't be removed because it exists in a tax rule.";
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
"Exception does not contain expected message."
);
}
}
public function testSearchTaxRates()
{
$rates = $this->setupTaxRatesForSearch();
// Find rates whose code is 'codeUs12'
$filter = $this->filterBuilder->setField(Rate::KEY_CODE)
->setValue('codeUs12')
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchData = $this->searchCriteriaBuilder->create()->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
/** @var \Magento\Framework\Api\SearchResults $searchResults */
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$expectedRuleData = [
[
'id' => (int)$rates['codeUs12']->getId(),
'tax_country_id' => $rates['codeUs12']->getTaxCountryId(),
'tax_region_id' => (int)$rates['codeUs12']->getTaxRegionId(),
'region_name' => 'CA',
'tax_postcode' => $rates['codeUs12']->getTaxPostcode(),
'code' => $rates['codeUs12']->getCode(),
'rate' => ((float) $rates['codeUs12']->getRate()),
'titles' => [],
],
];
$this->assertEquals($expectedRuleData, $searchResults['items']);
}
public function testSearchTaxRatesCz()
{
// TODO: This test fails in SOAP, a generic bug searching in SOAP
$this->_markTestAsRestOnly();
$rates = $this->setupTaxRatesForSearch();
$filterBR = $this->filterBuilder->setField(Rate::KEY_COUNTRY_ID)
->setValue('BR')
->create();
$filterUS = $this->filterBuilder->setField(Rate::KEY_COUNTRY_ID)
->setValue('US')
->create();
// Find rates which country id 'CZ'
$filterCZ = $this->filterBuilder->setField(Rate::KEY_COUNTRY_ID)
->setValue('CZ')
->create();
$sortOrder = $this->sortOrderBuilder
->setField(Rate::KEY_POSTCODE)
->setDirection(SortOrder::SORT_DESC)
->create();
$filterRate = $this->filterBuilder->setField(Rate::KEY_PERCENTAGE_RATE)
->setValue('2.2000')
->create();
$this->searchCriteriaBuilder->addFilters([$filterBR, $filterUS, $filterCZ]);
// Order them by descending postcode (not the default order)
$this->searchCriteriaBuilder->addFilters([$filterCZ, $filterRate])
->addSortOrder($sortOrder);
$searchData = $this->searchCriteriaBuilder->create()->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
/** @var \Magento\Framework\Api\SearchResults $searchResults */
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(2, $searchResults['total_count']);
$expectedRuleData = [
[
'id' => (int)$rates['codeCz2']->getId(),
'tax_country_id' => $rates['codeCz2']->getTaxCountryId(),
'tax_postcode' => $rates['codeCz2']->getTaxPostcode(),
'code' => $rates['codeCz2']->getCode(),
'rate' => ((float) $rates['codeCz2']->getRate()),
'tax_region_id' => 0,
'titles' => [],
],
[
'id' => (int)$rates['codeCz1']->getId(),
'tax_country_id' => $rates['codeCz1']->getTaxCountryId(),
'tax_postcode' => $rates['codeCz1']->getTaxPostcode(),
'code' => $rates['codeCz1']->getCode(),
'rate' => ((float) $rates['codeCz1']->getRate()),
'tax_region_id' => 0,
'titles' => [],
],
];
$this->assertEquals($expectedRuleData, $searchResults['items']);
}
/**
* Get tax rates created in Magento\Tax\_files\tax_classes.php
*
* @return \Magento\Tax\Model\Calculation\Rate[]
*/
private function getFixtureTaxRates()
{
if ($this->fixtureTaxRates === null) {
$this->fixtureTaxRates = [];
if ($this->getFixtureTaxRules()) {
$taxRateIds = (array)$this->getFixtureTaxRules()[0]->getRates();
foreach ($taxRateIds as $taxRateId) {
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$this->fixtureTaxRates[] = $taxRate->load($taxRateId);
}
}
}
return $this->fixtureTaxRates;
}
/**
* Get tax classes created in Magento\Tax\_files\tax_classes.php
*
* @return \Magento\Tax\Model\ClassModel[]
*/
private function getFixtureTaxClasses()
{
if ($this->fixtureTaxClasses === null) {
$this->fixtureTaxClasses = [];
if ($this->getFixtureTaxRules()) {
$taxClassIds = array_merge(
(array)$this->getFixtureTaxRules()[0]->getCustomerTaxClasses(),
(array)$this->getFixtureTaxRules()[0]->getProductTaxClasses()
);
foreach ($taxClassIds as $taxClassId) {
/** @var \Magento\Tax\Model\ClassModel $taxClass */
$taxClass = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\ClassModel::class);
$this->fixtureTaxClasses[] = $taxClass->load($taxClassId);
}
}
}
return $this->fixtureTaxClasses;
}
/**
* Get tax rule created in Magento\Tax\_files\tax_classes.php
*
* @return \Magento\Tax\Model\Calculation\Rule[]
*/
private function getFixtureTaxRules()
{
if ($this->fixtureTaxRules === null) {
$this->fixtureTaxRules = [];
$taxRuleCodes = ['Test Rule Duplicate', 'Test Rule'];
foreach ($taxRuleCodes as $taxRuleCode) {
/** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
$taxRule = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rule::class);
$taxRule->load($taxRuleCode, 'code');
if ($taxRule->getId()) {
$this->fixtureTaxRules[] = $taxRule;
}
}
}
return $this->fixtureTaxRules;
}
/**
* Creates rates for search tests.
*
* @return \Magento\Tax\Model\Calculation\Rate[]
*/
private function setupTaxRatesForSearch()
{
$objectManager = Bootstrap::getObjectManager();
$taxRateUs12 = [
'tax_country_id' => 'US',
'tax_region_id' => 12,
'tax_postcode' => '*',
'code' => 'codeUs12',
'rate' => 22,
'region_name' => 'CA',
];
$rates['codeUs12'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
->setData($taxRateUs12)
->save();
$taxRateUs14 = [
'tax_country_id' => 'US',
'tax_region_id' => 14,
'tax_postcode' => '*',
'code' => 'codeUs14',
'rate' => 22,
];
$rates['codeUs14'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
->setData($taxRateUs14)
->save();
$taxRateBr13 = [
'tax_country_id' => 'BR',
'tax_region_id' => 13,
'tax_postcode' => '*',
'code' => 'codeBr13',
'rate' => 7.5,
];
$rates['codeBr13'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
->setData($taxRateBr13)
->save();
$taxRateCz1 = [
'tax_country_id' => 'CZ',
'tax_postcode' => '110 00',
'code' => 'codeCz1',
'rate' => 1.1,
];
$rates['codeCz1'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
->setData($taxRateCz1)
->save();
$taxRateCz2 = [
'tax_country_id' => 'CZ',
'tax_postcode' => '250 00',
'code' => 'codeCz2',
'rate' => 2.2,
];
$rates['codeCz2'] = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
->setData($taxRateCz2)
->save();
// Set class variable so rates will be deleted on tearDown()
$this->otherRates = $rates;
return $rates;
}
}
@@ -0,0 +1,654 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tax\Api;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
use Magento\Webapi\Model\Rest\Config as HttpConstants;
/**
* Tax Rule Repository API test
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TaxRuleRepositoryInterfaceTest extends WebapiAbstract
{
const SERVICE_NAME = "taxTaxRuleRepositoryV1";
const SERVICE_VERSION = "V1";
const RESOURCE_PATH = "/V1/taxRules";
/** @var \Magento\Tax\Model\Calculation\Rate[] */
private $fixtureTaxRates;
/** @var \Magento\Tax\Model\ClassModel[] */
private $fixtureTaxClasses;
/** @var \Magento\Tax\Model\Calculation\Rule[] */
private $fixtureTaxRules;
/** @var FilterBuilder */
private $filterBuilder;
/** @var SearchCriteriaBuilder */
private $searchCriteriaBuilder;
/** @var \Magento\Tax\Api\Data\TaxClassInterface | \Magento\Tax\Model\ClassModel */
private $taxClass;
/** @var \Magento\Tax\Api\Data\TaxRateInterface | \Magento\Tax\Model\Calculation\Rate */
private $taxRate;
/** @var SortOrderBuilder */
private $sortOrderBuilder;
/**
* Execute per test initialization.
*/
protected function setUp(): void
{
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(
\Magento\Framework\Api\SearchCriteriaBuilder::class
);
$this->filterBuilder = Bootstrap::getObjectManager()->create(
\Magento\Framework\Api\FilterBuilder::class
);
$objectManager = Bootstrap::getObjectManager();
$this->searchCriteriaBuilder = $objectManager->create(
\Magento\Framework\Api\SearchCriteriaBuilder::class
);
$this->filterBuilder = $objectManager->create(
\Magento\Framework\Api\FilterBuilder::class
);
$this->sortOrderBuilder = $objectManager->create(
SortOrderBuilder::class
);
$this->taxClass = $objectManager->create(
\Magento\Tax\Api\Data\TaxClassInterface::class
);
$this->taxRate = $objectManager->create(
\Magento\Tax\Api\Data\TaxRateInterface::class
);
/** Initialize tax classes, tax rates and tax rules defined in fixture Magento/Tax/_files/tax_classes.php */
$this->getFixtureTaxRates();
$this->getFixtureTaxClasses();
$this->getFixtureTaxRules();
}
protected function tearDown(): void
{
$taxRules = $this->getFixtureTaxRules();
if (count($taxRules)) {
$taxRates = $this->getFixtureTaxRates();
$taxClasses = $this->getFixtureTaxClasses();
foreach ($taxRules as $taxRule) {
$taxRule->delete();
}
foreach ($taxRates as $taxRate) {
$taxRate->delete();
}
foreach ($taxClasses as $taxClass) {
$taxClass->delete();
}
}
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testDeleteTaxRule()
{
$fixtureRule = $this->getFixtureTaxRules()[0];
$taxRuleId = $fixtureRule->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRuleId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'DeleteById',
],
];
$requestData = ['ruleId' => $taxRuleId];
$result = $this->_webApiCall($serviceInfo, $requestData);
$this->assertTrue($result);
/** Ensure that tax rule was actually removed from DB */
/** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$this->assertNull($taxRate->load($taxRuleId)->getId(), 'Tax rule was not deleted from DB.');
}
public function testCreateTaxRule()
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => HttpConstants::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$requestData = [
'rule' => [
'code' => 'Test Rule ' . microtime(),
'position' => 10,
'priority' => 5,
'customer_tax_class_ids' => [3],
'product_tax_class_ids' => [2],
'tax_rate_ids' => [1, 2],
'calculate_subtotal' => 1,
],
];
$taxRuleData = $this->_webApiCall($serviceInfo, $requestData);
$this->assertArrayHasKey('id', $taxRuleData, "Tax rule ID is expected");
$this->assertGreaterThan(0, $taxRuleData['id']);
$taxRuleId = $taxRuleData['id'];
unset($taxRuleData['id']);
$this->assertEquals($requestData['rule'], $taxRuleData, "Tax rule is created with invalid data.");
/** Ensure that tax rule was actually created in DB */
/** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
$taxRule = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rule::class);
$this->assertEquals($taxRuleId, $taxRule->load($taxRuleId)->getId(), 'Tax rule was not created in DB.');
$taxRule->delete();
}
public function testCreateTaxRuleInvalidTaxClassIds()
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => HttpConstants::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$requestData = [
'rule' => [
'code' => 'Test Rule ' . microtime(),
'position' => 10,
'priority' => 5,
'customer_tax_class_ids' => [2],
'product_tax_class_ids' => [3],
'tax_rate_ids' => [1, 2],
'calculate_subtotal' => 1,
],
];
try {
$this->_webApiCall($serviceInfo, $requestData);
$this->fail('Did not throw expected InputException');
} catch (\SoapFault $e) {
$this->assertStringContainsString(
'No such entity with customer_tax_class_ids = %fieldValue',
$e->getMessage()
);
} catch (\Exception $e) {
$this->assertStringContainsString(
'No such entity with customer_tax_class_ids = %fieldValue',
$e->getMessage()
);
}
}
public function testCreateTaxRuleExistingCode()
{
$expectedMessage = '%1 already exists.';
$requestData = [
'rule' => [
'code' => 'Test Rule ' . microtime(),
'position' => 10,
'priority' => 5,
'customer_tax_class_ids' => [3],
'product_tax_class_ids' => [2],
'tax_rate_ids' => [1, 2],
'calculate_subtotal' => 0,
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$newTaxRuleData = $this->_webApiCall($serviceInfo, $requestData);
try {
$this->_webApiCall($serviceInfo, $requestData);
$this->fail('Expected exception was not raised');
} catch (\SoapFault $e) {
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
'SoapFault does not contain expected message.'
);
} catch (\Exception $e) {
$errorObj = $this->processRestExceptionResult($e);
$this->assertEquals($expectedMessage, $errorObj['message']);
$this->assertEquals(['Code'], $errorObj['parameters']);
}
// Clean up the new tax rule so it won't affect other tests
/** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
$taxRule = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rule::class);
$taxRule->load($newTaxRuleData['id']);
$taxRule->delete();
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testGetTaxRule()
{
$fixtureRule = $this->getFixtureTaxRules()[0];
$taxRuleId = $fixtureRule->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRuleId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$expectedRuleData = [
'id' => $taxRuleId,
'code' => 'Test Rule Duplicate',
'priority' => '0',
'position' => '0',
'customer_tax_class_ids' => array_values(array_unique($fixtureRule->getCustomerTaxClasses())),
'product_tax_class_ids' => array_values(array_unique($fixtureRule->getProductTaxClasses())),
'tax_rate_ids' => array_values(array_unique($fixtureRule->getRates())),
'calculate_subtotal' => false,
];
$requestData = ['ruleId' => $taxRuleId];
$result = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals($expectedRuleData, $result);
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testSearchTaxRulesSimple()
{
// Find rules whose code is 'Test Rule'
$filter = $this->filterBuilder->setField('code')
->setValue('Test Rule')
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$fixtureRule = $this->getFixtureTaxRules()[1];
$searchData = $this->searchCriteriaBuilder->create()->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
/** @var \Magento\Framework\Api\SearchResults $searchResults */
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$expectedRuleData = [
[
'id' => $fixtureRule->getId(),
'code' => 'Test Rule',
'priority' => 0,
'position' => 0,
'calculate_subtotal' => 0,
'customer_tax_class_ids' => array_values(array_unique($fixtureRule->getCustomerTaxClasses())),
'product_tax_class_ids' => array_values(array_unique($fixtureRule->getProductTaxClasses())),
'tax_rate_ids' => array_values(array_unique($fixtureRule->getRates())),
],
];
$this->assertEquals($expectedRuleData, $searchResults['items']);
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testSearchTaxRulesCodeWithJoins()
{
$customerTaxClass = clone $this->taxClass->load('CustomerTaxClass2', 'class_name');
$productTaxClass = clone $this->taxClass->load('Taxable Goods', 'class_name');
$taxRate = $this->taxRate->load('*', 'code');
$filter2 = $this->filterBuilder
->setField('customer_tax_class_ids')
->setValue($customerTaxClass->getClassId())
->create();
$filter3 = $this->filterBuilder
->setField('product_tax_class_ids')
->setValue($productTaxClass->getClassId())
->create();
$filter4 = $this->filterBuilder
->setField('tax_calculation_rate_id')
->setValue($taxRate->getId())
->create();
$sortOrder = $this->sortOrderBuilder
->setField('code')
->setDirection('DESC')
->create();
$this->searchCriteriaBuilder->addFilters([$filter2, $filter3]);
$this->searchCriteriaBuilder->addFilters([$filter4]);
$this->searchCriteriaBuilder->addSortOrder($sortOrder);
$fixtureRule = $this->getFixtureTaxRules()[1];
$searchData = $this->searchCriteriaBuilder->create()->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
/** @var \Magento\Framework\Api\SearchResults $searchResults */
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(2, $searchResults['total_count']);
$expectedRuleData = [
[
'id' => $this->getFixtureTaxRules()[0]->getId(),
'code' => 'Test Rule Duplicate',
'priority' => 0,
'position' => 0,
'calculate_subtotal' => 0,
'customer_tax_class_ids' => array_values(array_unique($fixtureRule->getCustomerTaxClasses())),
'product_tax_class_ids' => array_values(array_unique($fixtureRule->getProductTaxClasses())),
'tax_rate_ids' => array_values(array_unique($fixtureRule->getRates()))
],
[
'id' => $fixtureRule->getId(),
'code' => 'Test Rule',
'priority' => 0,
'position' => 0,
'calculate_subtotal' => 0,
'customer_tax_class_ids' => array_values(array_unique($fixtureRule->getCustomerTaxClasses())),
'product_tax_class_ids' => array_values(array_unique($fixtureRule->getProductTaxClasses())),
'tax_rate_ids' => array_values(array_unique($fixtureRule->getRates())),
],
];
$this->assertEquals($expectedRuleData, $searchResults['items']);
}
public function testGetTaxRuleNotExist()
{
$taxRuleId = 37865;
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/$taxRuleId",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$requestData = ['ruleId' => $taxRuleId];
try {
$this->_webApiCall($serviceInfo, $requestData);
$this->fail('Expected exception was not raised');
} catch (\Exception $e) {
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
"Exception does not contain expected message."
);
}
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testUpdateTaxRule()
{
$fixtureRule = $this->getFixtureTaxRules()[0];
$requestData = [
'rule' => [
'id' => $fixtureRule->getId(),
'code' => 'Test Rule ' . microtime(),
'position' => 10,
'priority' => 5,
'customer_tax_class_ids' => [3],
'product_tax_class_ids' => [2],
'tax_rate_ids' => [1, 2],
'calculate_subtotal' => 1,
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$this->_webApiCall($serviceInfo, $requestData);
$expectedRuleData = $requestData['rule'];
/** Ensure that tax rule was actually updated in DB */
/** @var \Magento\Tax\Model\Calculation $taxCalculation */
$taxCalculation = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation::class);
/** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
$taxRule = Bootstrap::getObjectManager()->create(
\Magento\Tax\Model\Calculation\Rule::class,
['calculation' => $taxCalculation]
);
$taxRuleModel = $taxRule->load($fixtureRule->getId());
$this->assertEquals($expectedRuleData['id'], $taxRuleModel->getId(), 'Tax rule was not updated in DB.');
$this->assertEquals(
$expectedRuleData['code'],
$taxRuleModel->getCode(),
'Tax rule code was updated incorrectly.'
);
$this->assertEquals(
$expectedRuleData['position'],
$taxRuleModel->getPosition(),
'Tax rule sort order was updated incorrectly.'
);
$this->assertEquals(
$expectedRuleData['priority'],
$taxRuleModel->getPriority(),
'Tax rule priority was updated incorrectly.'
);
$this->assertEquals(
$expectedRuleData['customer_tax_class_ids'],
array_values(array_unique($taxRuleModel->getCustomerTaxClasses())),
'Customer Tax classes were updated incorrectly'
);
$this->assertEquals(
$expectedRuleData['product_tax_class_ids'],
array_values(array_unique($taxRuleModel->getProductTaxClasses())),
'Product Tax classes were updated incorrectly.'
);
$this->assertEquals(
$expectedRuleData['tax_rate_ids'],
array_values(array_unique($taxRuleModel->getRates())),
'Tax rates were updated incorrectly.'
);
}
public function testUpdateTaxRuleNotExisting()
{
$requestData = [
'rule' => [
'id' => 12345,
'code' => 'Test Rule ' . microtime(),
'position' => 10,
'priority' => 5,
'customer_tax_class_ids' => [3],
'product_tax_class_ids' => [2],
'tax_rate_ids' => [1, 2],
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
try {
$this->_webApiCall($serviceInfo, $requestData);
$this->fail('Expected exception was not raised');
} catch (\Exception $e) {
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
"Exception does not contain expected message."
);
}
}
/**
* @magentoApiDataFixture Magento/Tax/_files/tax_classes.php
*/
public function testSearchTaxRule()
{
$fixtureRule = $this->getFixtureTaxRules()[0];
$filter = $this->filterBuilder->setField('code')
->setValue($fixtureRule->getCode())
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchData = $this->searchCriteriaBuilder->create()->__toArray();
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$this->assertEquals($fixtureRule->getId(), $searchResults['items'][0]["id"]);
$this->assertEquals($fixtureRule->getCode(), $searchResults['items'][0]['code']);
}
/**
* Get tax rates created in Magento\Tax\_files\tax_classes.php
*
* @return \Magento\Tax\Model\Calculation\Rate[]
*/
private function getFixtureTaxRates()
{
if ($this->fixtureTaxRates === null) {
$this->fixtureTaxRates = [];
if ($this->getFixtureTaxRules()) {
$taxRateIds = (array)$this->getFixtureTaxRules()[0]->getRates();
foreach ($taxRateIds as $taxRateId) {
/** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
$taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
$this->fixtureTaxRates[] = $taxRate->load($taxRateId);
}
}
}
return $this->fixtureTaxRates;
}
/**
* Get tax classes created in Magento\Tax\_files\tax_classes.php
*
* @return \Magento\Tax\Model\ClassModel[]
*/
private function getFixtureTaxClasses()
{
if ($this->fixtureTaxClasses === null) {
$this->fixtureTaxClasses = [];
if ($this->getFixtureTaxRules()) {
$taxClassIds = array_merge(
(array)$this->getFixtureTaxRules()[0]->getCustomerTaxClasses(),
(array)$this->getFixtureTaxRules()[0]->getProductTaxClasses()
);
foreach ($taxClassIds as $taxClassId) {
/** @var \Magento\Tax\Model\ClassModel $taxClass */
$taxClass = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\ClassModel::class);
$this->fixtureTaxClasses[] = $taxClass->load($taxClassId);
}
}
}
return $this->fixtureTaxClasses;
}
/**
* Get tax rule created in Magento\Tax\_files\tax_classes.php
*
* @return \Magento\Tax\Model\Calculation\Rule[]
*/
private function getFixtureTaxRules()
{
if ($this->fixtureTaxRules === null) {
$this->fixtureTaxRules = [];
$taxRuleCodes = ['Test Rule Duplicate', 'Test Rule'];
foreach ($taxRuleCodes as $taxRuleCode) {
/** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
$taxRule = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rule::class);
$taxRule->load($taxRuleCode, 'code');
if ($taxRule->getId()) {
$this->fixtureTaxRules[] = $taxRule;
}
}
}
return $this->fixtureTaxRules;
}
}