View source
<?php
namespace Drupal\Tests\commerce_tax\Kernel\Plugin\Commerce\TaxType;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderItemType;
use Drupal\commerce_price\Price;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_tax\Entity\TaxType;
use Drupal\commerce_tax\TaxableType;
use Drupal\profile\Entity\Profile;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
class EuropeanUnionVatTest extends OrderKernelTestBase {
protected $taxType;
protected $user;
public static $modules = [
'commerce_tax',
'commerce_tax_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig('commerce_tax');
OrderItemType::create([
'id' => 'test_physical',
'label' => 'Test (Physical)',
'orderType' => 'default',
'third_party_settings' => [
'commerce_tax' => [
'taxable_type' => TaxableType::PHYSICAL_GOODS,
],
],
])
->save();
OrderItemType::create([
'id' => 'test_digital',
'label' => 'Test (Digital)',
'orderType' => 'default',
'third_party_settings' => [
'commerce_tax' => [
'taxable_type' => TaxableType::DIGITAL_GOODS,
],
],
])
->save();
$user = $this
->createUser();
$this->user = $this
->reloadEntity($user);
$this->taxType = TaxType::create([
'id' => 'european_union_vat',
'label' => 'EU VAT',
'plugin' => 'european_union_vat',
'configuration' => [
'display_inclusive' => TRUE,
],
'status' => FALSE,
]);
$this->taxType
->save();
}
public function testApplication() {
$plugin = $this->taxType
->getPlugin();
$order = $this
->buildOrder('DE', 'FR', 'DE123456789');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|ic|zero', $adjustment
->getSourceId());
$order = $this
->buildOrder('FR', 'FR', 'FR00123456789');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|fr|standard', $adjustment
->getSourceId());
$order = $this
->buildOrder('DE', 'FR');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|fr|standard', $adjustment
->getSourceId());
$order = $this
->buildOrder('GB', 'FR');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$this
->assertCount(0, $adjustments);
$order = $this
->buildOrder('DE', 'FR', '', [
'DE',
]);
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|de|standard', $adjustment
->getSourceId());
$order = $this
->buildOrder('DE', 'FR', '', [], TRUE);
$order
->setPlacedTime(mktime(1, 1, 1, 1, 1, 2014));
$order
->save();
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|fr|standard', $adjustment
->getSourceId());
$order = $this
->buildOrder('DE', 'FR', '', [], TRUE);
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|de|standard', $adjustment
->getSourceId());
$order = $this
->buildOrder('DE', 'US', '', [
'FR',
], TRUE);
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|de|standard', $adjustment
->getSourceId());
$order = $this
->buildOrder('DE', 'US', 'DE123456789', [
'FR',
], TRUE);
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('european_union_vat|ic|zero', $adjustment
->getSourceId());
$order = $this
->buildOrder('RS', 'FR');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$this
->assertCount(0, $order
->collectAdjustments());
$order = $this
->buildOrder('FR', 'RS');
$this
->assertFalse($plugin
->applies($order));
}
public function testGetZones() {
$plugin = $this->taxType
->getPlugin();
$zones = $plugin
->getZones();
$this
->assertArrayHasKey('be', $zones);
$this
->assertArrayHasKey('es', $zones);
$this
->assertArrayHasKey('fr', $zones);
$this
->assertArrayHasKey('it', $zones);
$this
->assertArrayHasKey('de', $zones);
$germany_tax_rates = $zones['de']
->getRates();
$standard_rate = $germany_tax_rates['standard']
->toArray();
$fake_percentage = end($standard_rate['percentages']);
$this
->assertEquals([
'number' => '0.25',
'start_date' => '2041-01-01',
], $fake_percentage);
}
protected function buildOrder($customer_country, $store_country, $tax_number = '', array $store_registrations = [], $digital = FALSE) {
$store = Store::create([
'type' => 'default',
'label' => 'My store',
'address' => [
'country_code' => $store_country,
],
'prices_include_tax' => FALSE,
'tax_registrations' => $store_registrations,
]);
$store
->save();
$customer_profile = Profile::create([
'type' => 'customer',
'uid' => $this->user
->id(),
'address' => [
'country_code' => $customer_country,
],
]);
if ($tax_number) {
$customer_profile
->set('tax_number', [
'type' => 'european_union_vat',
'value' => $tax_number,
'verification_state' => 'success',
]);
}
$customer_profile
->save();
$order_item = OrderItem::create([
'type' => $digital ? 'test_digital' : 'test_physical',
'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
->save();
return $order;
}
}