|
6 | 6 |
|
7 | 7 | namespace Magento\Quote\Model;
|
8 | 8 |
|
| 9 | +use Magento\Customer\Model\Vat; |
| 10 | +use Magento\Store\Model\ScopeInterface; |
| 11 | +use Magento\Tax\Model\Config as TaxConfig; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Customer\Api\Data\GroupInterface; |
| 16 | +use Magento\Customer\Api\GroupRepositoryInterface; |
| 17 | +use Magento\Tax\Model\ClassModel; |
| 18 | +use Magento\Framework\App\Config\MutableScopeConfigInterface; |
| 19 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 20 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 21 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 22 | +use Magento\Quote\Api\ShippingMethodManagementInterface; |
| 23 | +use Magento\Customer\Api\Data\AddressInterface; |
| 24 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 25 | +use Magento\Tax\Api\TaxClassRepositoryInterface; |
| 26 | +use Magento\Tax\Api\Data\TaxClassInterface; |
| 27 | + |
9 | 28 | /**
|
10 |
| - * Class ShippingMethodManagementTest |
| 29 | + * Test for shipping methods management |
11 | 30 | *
|
12 | 31 | * @magentoDbIsolation enabled
|
| 32 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
13 | 33 | */
|
14 | 34 | class ShippingMethodManagementTest extends \PHPUnit\Framework\TestCase
|
15 | 35 | {
|
| 36 | + /** @var ObjectManagerInterface $objectManager */ |
| 37 | + private $objectManager; |
| 38 | + |
| 39 | + /** @var GroupRepositoryInterface $groupRepository */ |
| 40 | + private $groupRepository; |
| 41 | + |
| 42 | + /** @var TaxClassRepositoryInterface $taxClassRepository */ |
| 43 | + private $taxClassRepository; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 51 | + $this->groupRepository = $this->objectManager->get(GroupRepositoryInterface::class); |
| 52 | + $this->taxClassRepository = $this->objectManager->get(TaxClassRepositoryInterface::class); |
| 53 | + } |
| 54 | + |
16 | 55 | /**
|
17 | 56 | * @magentoDataFixture Magento/SalesRule/_files/cart_rule_100_percent_off.php
|
18 | 57 | * @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
|
@@ -173,4 +212,130 @@ private function executeTestFlow($flatRateAmount, $tableRateAmount)
|
173 | 212 | $this->assertEquals($expectedResult[$rate->getCarrierCode()]['method_code'], $rate->getMethodCode());
|
174 | 213 | }
|
175 | 214 | }
|
| 215 | + |
| 216 | + /** |
| 217 | + * Test for estimate shipping with tax and changed VAT customer group |
| 218 | + * |
| 219 | + * @magentoDbIsolation disabled |
| 220 | + * @magentoDataFixture Magento/Tax/_files/tax_classes_de.php |
| 221 | + * @magentoDataFixture Magento/Sales/_files/quote_with_customer.php |
| 222 | + * @magentoDataFixture Magento/Customer/_files/customer_group.php |
| 223 | + * @magentoDataFixture Magento/Customer/_files/customer_address.php |
| 224 | + * @magentoConfigFixture current_store customer/create_account/tax_calculation_address_type shipping |
| 225 | + * @magentoConfigFixture current_store customer/create_account/default_group 1 |
| 226 | + * @magentoConfigFixture current_store customer/create_account/auto_group_assign 1 |
| 227 | + * @magentoConfigFixture current_store tax/calculation/price_includes_tax 1 |
| 228 | + * @magentoConfigFixture current_store tax/calculation/shipping_includes_tax 1 |
| 229 | + */ |
| 230 | + public function testEstimateByAddressWithInclExclTaxAndVATGroup() |
| 231 | + { |
| 232 | + /** @var CustomerRepositoryInterface $customerRepository */ |
| 233 | + $customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class); |
| 234 | + $customer = $customerRepository->get('customer@example.com'); |
| 235 | + |
| 236 | + /** @var GroupInterface $customerGroup */ |
| 237 | + $customerGroup = $this->findCustomerGroupByCode('custom_group'); |
| 238 | + $customerGroup->setTaxClassId($this->getTaxClass('CustomerTaxClass')->getClassId()); |
| 239 | + $this->groupRepository->save($customerGroup); |
| 240 | + |
| 241 | + $customer->setGroupId($customerGroup->getId()); |
| 242 | + $customer->setTaxvat('12'); |
| 243 | + $customerRepository->save($customer); |
| 244 | + $this->setConfig($customerGroup->getId(), $this->getTaxClass('ProductTaxClass')->getClassId()); |
| 245 | + $this->changeCustomerAddress($customer->getDefaultShipping()); |
| 246 | + |
| 247 | + $quote = $this->objectManager->get(GetQuoteByReservedOrderId::class)->execute('test01'); |
| 248 | + |
| 249 | + /** @var ShippingMethodManagementInterface $shippingEstimation */ |
| 250 | + $shippingEstimation = $this->objectManager->get(ShippingMethodManagementInterface::class); |
| 251 | + $result = $shippingEstimation->estimateByAddressId($quote->getId(), $customer->getDefaultShipping()); |
| 252 | + |
| 253 | + $this->assertEquals(6.05, $result[0]->getPriceInclTax()); |
| 254 | + $this->assertEquals(5.0, $result[0]->getPriceExclTax()); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Find the group with a given code. |
| 259 | + * |
| 260 | + * @param string $code |
| 261 | + * |
| 262 | + * @return GroupInterface |
| 263 | + */ |
| 264 | + protected function findCustomerGroupByCode(string $code): ?GroupInterface |
| 265 | + { |
| 266 | + /** @var SearchCriteriaBuilder $searchBuilder */ |
| 267 | + $searchBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 268 | + $searchCriteria = $searchBuilder->addFilter('code', $code) |
| 269 | + ->create(); |
| 270 | + $groups = $this->groupRepository->getList($searchCriteria) |
| 271 | + ->getItems(); |
| 272 | + |
| 273 | + return array_shift($groups); |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Change customer address |
| 278 | + * |
| 279 | + * @param int $customerAddressId |
| 280 | + * |
| 281 | + * @return AddressInterface |
| 282 | + */ |
| 283 | + private function changeCustomerAddress(int $customerAddressId): AddressInterface |
| 284 | + { |
| 285 | + $addressRepository = $this->objectManager->get(AddressRepositoryInterface::class); |
| 286 | + $address = $addressRepository->getById($customerAddressId); |
| 287 | + $address->setVatId(12345); |
| 288 | + $address->setCountryId('DE'); |
| 289 | + $address->setRegionId(0); |
| 290 | + $address->setPostcode(10178); |
| 291 | + |
| 292 | + return $addressRepository->save($address); |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Get tax class. |
| 297 | + * |
| 298 | + * @param string $name |
| 299 | + * |
| 300 | + * @return TaxClassInterface |
| 301 | + */ |
| 302 | + private function getTaxClass(string $name): ?TaxClassInterface |
| 303 | + { |
| 304 | + /** @var SearchCriteriaBuilder $searchBuilder */ |
| 305 | + $searchBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 306 | + $searchCriteria = $searchBuilder->addFilter(ClassModel::KEY_NAME, $name) |
| 307 | + ->create(); |
| 308 | + $searchResults = $this->taxClassRepository->getList($searchCriteria) |
| 309 | + ->getItems(); |
| 310 | + |
| 311 | + return array_shift($searchResults); |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * Set the configuration. |
| 316 | + * |
| 317 | + * @param int $customerGroupId |
| 318 | + * @param int $productTaxClassId |
| 319 | + * |
| 320 | + * @return void |
| 321 | + */ |
| 322 | + private function setConfig(int $customerGroupId, int $productTaxClassId): void |
| 323 | + { |
| 324 | + $configData = [ |
| 325 | + [ |
| 326 | + 'path' => Vat::XML_PATH_CUSTOMER_VIV_INVALID_GROUP, |
| 327 | + 'value' => $customerGroupId, |
| 328 | + 'scope' => ScopeInterface::SCOPE_STORE, |
| 329 | + ], |
| 330 | + [ |
| 331 | + 'path' => TaxConfig::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, |
| 332 | + 'value' => $productTaxClassId, |
| 333 | + 'scope' => ScopeInterface::SCOPE_STORE, |
| 334 | + ], |
| 335 | + ]; |
| 336 | + $config = $this->objectManager->get(MutableScopeConfigInterface::class); |
| 337 | + foreach ($configData as $data) { |
| 338 | + $config->setValue($data['path'], $data['value'], $data['scope']); |
| 339 | + } |
| 340 | + } |
176 | 341 | }
|
0 commit comments