View source
<?php
namespace Drupal\Tests\commerce_tax\Kernel\Plugin\Commerce\TaxType;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_tax\Entity\TaxType;
use Drupal\commerce_tax\Plugin\Commerce\TaxType\Custom;
use Drupal\profile\Entity\Profile;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
class CustomTest extends OrderKernelTestBase {
protected $taxType;
protected $user;
public static $modules = [
'commerce_tax',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'commerce_tax',
]);
$user = $this
->createUser();
$this->user = $this
->reloadEntity($user);
$this->taxType = TaxType::create([
'id' => 'serbian_vat',
'label' => 'Serbian VAT',
'plugin' => 'custom',
'configuration' => [
'display_inclusive' => TRUE,
'display_label' => 'vat',
'round' => TRUE,
'rates' => [
[
'id' => 'standard',
'label' => 'Standard',
'percentage' => '0.2',
],
[
'id' => 'reduced',
'label' => 'Reduced',
'percentage' => '0.1',
],
],
'territories' => [
[
'country_code' => 'RS',
],
],
],
'status' => FALSE,
]);
$this->taxType
->save();
}
public function testGetters() {
$plugin = $this->taxType
->getPlugin();
assert($plugin instanceof Custom);
$this
->assertEquals(0, $plugin
->getWeight());
$this
->assertTrue($plugin
->isDisplayInclusive());
$this
->assertTrue($plugin
->shouldRound());
$zones = $plugin
->getZones();
$zone = reset($zones);
$rates = $zone
->getRates();
$this
->assertCount(1, $zones);
$this
->assertCount(2, $rates);
$this
->assertArrayHasKey('standard', $rates);
$this
->assertArrayHasKey('reduced', $rates);
$this
->assertEquals('standard', $rates['standard']
->getId());
$this
->assertEquals('Standard', $rates['standard']
->getLabel());
$this
->assertEquals('0.2', $rates['standard']
->getPercentage()
->getNumber());
$this
->assertTrue($rates['standard']
->isDefault());
$this
->assertEquals('reduced', $rates['reduced']
->getId());
$this
->assertEquals('Reduced', $rates['reduced']
->getLabel());
$this
->assertEquals('0.1', $rates['reduced']
->getPercentage()
->getNumber());
$this
->assertFalse($rates['reduced']
->isDefault());
}
public function testApplication() {
$plugin = $this->taxType
->getPlugin();
$order = $this
->buildOrder('US', 'US');
$this
->assertFalse($plugin
->applies($order));
$order = $this
->buildOrder('US', 'US', [
'RS',
]);
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$this
->assertCount(0, $order
->collectAdjustments());
$order = $this
->buildOrder('RS', 'US', [
'RS',
]);
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$this
->assertCount(1, $order
->collectAdjustments());
$order = $this
->buildOrder('US', 'RS');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$this
->assertCount(0, $order
->collectAdjustments());
$order = $this
->buildOrder('RS', 'RS');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$this
->assertCount(1, $order
->collectAdjustments());
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertEquals(new Price('12.40', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals('tax', $adjustment
->getType());
$this
->assertEquals(t('VAT'), $adjustment
->getLabel());
$this
->assertEquals(new Price('2.07', 'USD'), $adjustment
->getAmount());
$this
->assertEquals('0.2', $adjustment
->getPercentage());
$this
->assertEquals('serbian_vat|default|standard', $adjustment
->getSourceId());
$this
->assertTrue($adjustment
->isIncluded());
$order = $this
->buildOrder('RS', 'RS', [], TRUE);
$plugin
->apply($order);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertEquals(new Price('10.33', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('1.72', 'USD'), $adjustment
->getAmount());
$this
->assertEquals('0.2', $adjustment
->getPercentage());
$this
->assertTrue($adjustment
->isIncluded());
$configuration = $plugin
->getConfiguration();
$configuration['display_inclusive'] = FALSE;
$plugin
->setConfiguration($configuration);
$order = $this
->buildOrder('RS', 'RS', [], TRUE);
$plugin
->apply($order);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertEquals(new Price('8.61', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('1.72', 'USD'), $adjustment
->getAmount());
$this
->assertEquals('0.2', $adjustment
->getPercentage());
$this
->assertFalse($adjustment
->isIncluded());
}
public function testDiscountedPrices() {
$plugin = $this->taxType
->getPlugin();
$order = $this
->buildOrder('RS', 'RS', [], TRUE);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$order_item
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => t('Discount'),
'amount' => new Price('-1', 'USD'),
]));
$plugin
->apply($order);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$tax_adjustment = end($adjustments);
$this
->assertEquals(new Price('10.33', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('9.33', 'USD'), $order_item
->getAdjustedUnitPrice());
$this
->assertCount(2, $adjustments);
$this
->assertEquals('tax', $tax_adjustment
->getType());
$this
->assertEquals(t('VAT'), $tax_adjustment
->getLabel());
$this
->assertEquals(new Price('1.56', 'USD'), $tax_adjustment
->getAmount());
$this
->assertEquals('0.2', $tax_adjustment
->getPercentage());
$order = $this
->buildOrder('RS', 'RS', []);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$order_item
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => t('Discount'),
'amount' => new Price('-1', 'USD'),
]));
$plugin
->apply($order);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$tax_adjustment = end($adjustments);
$this
->assertEquals(new Price('12.40', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('11.40', 'USD'), $order_item
->getAdjustedUnitPrice());
$this
->assertCount(2, $adjustments);
$this
->assertEquals('tax', $tax_adjustment
->getType());
$this
->assertEquals(t('VAT'), $tax_adjustment
->getLabel());
$this
->assertEquals(new Price('1.90', 'USD'), $tax_adjustment
->getAmount());
$this
->assertEquals('0.2', $tax_adjustment
->getPercentage());
$configuration = $plugin
->getConfiguration();
$configuration['display_inclusive'] = FALSE;
$plugin
->setConfiguration($configuration);
$order = $this
->buildOrder('RS', 'RS', []);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$order_item
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => t('Discount'),
'amount' => new Price('-1', 'USD'),
]));
$plugin
->apply($order);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$tax_adjustment = end($adjustments);
$this
->assertEquals(new Price('10.33', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('11.20', 'USD'), $order_item
->getAdjustedUnitPrice());
$this
->assertCount(2, $adjustments);
$this
->assertEquals('tax', $tax_adjustment
->getType());
$this
->assertEquals(t('VAT'), $tax_adjustment
->getLabel());
$this
->assertEquals(new Price('1.87', 'USD'), $tax_adjustment
->getAmount());
$this
->assertEquals('0.2', $tax_adjustment
->getPercentage());
$configuration = $plugin
->getConfiguration();
$configuration['display_inclusive'] = FALSE;
$plugin
->setConfiguration($configuration);
$order = $this
->buildOrder('RS', 'RS', [], TRUE);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$order_item
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => t('Discount'),
'amount' => new Price('-1', 'USD'),
]));
$plugin
->apply($order);
$order_items = $order
->getItems();
$order_item = reset($order_items);
$adjustments = $order
->collectAdjustments();
$tax_adjustment = end($adjustments);
$this
->assertEquals(new Price('8.61', 'USD'), $order_item
->getUnitPrice());
$this
->assertEquals(new Price('9.13', 'USD'), $order_item
->getAdjustedUnitPrice());
$this
->assertCount(2, $adjustments);
$this
->assertEquals('tax', $tax_adjustment
->getType());
$this
->assertEquals(t('VAT'), $tax_adjustment
->getLabel());
$this
->assertEquals(new Price('1.52', 'USD'), $tax_adjustment
->getAmount());
$this
->assertEquals('0.2', $tax_adjustment
->getPercentage());
}
protected function buildOrder($customer_country, $store_country, array $store_registrations = [], $prices_include_tax = FALSE) {
$store = Store::create([
'type' => 'default',
'label' => 'My store',
'address' => [
'country_code' => $store_country,
],
'prices_include_tax' => $prices_include_tax,
'tax_registrations' => $store_registrations,
]);
$store
->save();
$customer_profile = Profile::create([
'type' => 'customer',
'uid' => $this->user
->id(),
'address' => [
'country_code' => $customer_country,
],
]);
$customer_profile
->save();
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'unit_price' => new Price('10.33', 'USD'),
]);
$order_item
->save();
$order = Order::create([
'type' => 'default',
'uid' => $this->user
->id(),
'store_id' => $store,
'billing_profile' => $customer_profile,
'order_items' => [
$order_item,
],
]);
$order
->setRefreshState(Order::REFRESH_SKIP);
$order
->save();
return $order;
}
}