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 CanadianSalesTaxTest extends OrderKernelTestBase {
protected $taxType;
protected $user;
public static $modules = [
'commerce_tax',
];
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();
$user = $this
->createUser();
$this->user = $this
->reloadEntity($user);
$this->taxType = TaxType::create([
'id' => 'canadian_sales_tax',
'label' => 'Canadian Sales Tax',
'plugin' => 'canadian_sales_tax',
'configuration' => [
'display_inclusive' => FALSE,
],
'status' => FALSE,
]);
$this->taxType
->save();
}
public function testApplication() {
$plugin = $this->taxType
->getPlugin();
$order = $this
->buildOrder('CA', 'BC');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$this
->assertCount(2, $adjustments);
$this
->assertEquals('canadian_sales_tax|ca|gst', $adjustments[0]
->getSourceId());
$this
->assertEquals('canadian_sales_tax|bc|pst', $adjustments[1]
->getSourceId());
$this
->assertEquals(t('GST'), $adjustments[0]
->getLabel());
$this
->assertEquals(t('PST'), $adjustments[1]
->getLabel());
$this
->assertEquals(new Price('0.5', 'USD'), $adjustments[0]
->getAmount());
$this
->assertEquals(new Price('0.7', 'USD'), $adjustments[1]
->getAmount());
$order = $this
->buildOrder('CA', 'AB');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('canadian_sales_tax|ca|gst', $adjustment
->getSourceId());
$order = $this
->buildOrder('CA', 'ON');
$this
->assertTrue($plugin
->applies($order));
$plugin
->apply($order);
$adjustments = $order
->collectAdjustments();
$adjustment = reset($adjustments);
$this
->assertCount(1, $adjustments);
$this
->assertEquals('canadian_sales_tax|on|hst', $adjustment
->getSourceId());
$order = $this
->buildOrder('US', 'SC');
$plugin
->apply($order);
$this
->assertCount(0, $order
->collectAdjustments());
}
public function testGetZones() {
$plugin = $this->taxType
->getPlugin();
$zones = $plugin
->getZones();
$this
->assertArrayHasKey('ca', $zones);
$this
->assertArrayHasKey('bc', $zones);
$this
->assertArrayHasKey('mb', $zones);
$this
->assertArrayHasKey('nb', $zones);
}
protected function buildOrder($customer_country, $customer_province) {
$store = Store::create([
'type' => 'default',
'label' => 'My store',
'address' => [
'country_code' => 'CA',
],
'prices_include_tax' => FALSE,
]);
$store
->save();
$customer_profile = Profile::create([
'type' => 'customer',
'uid' => $this->user
->id(),
'address' => [
'country_code' => $customer_country,
'administrative_area' => $customer_province,
],
]);
$customer_profile
->save();
$order_item = OrderItem::create([
'type' => 'test_physical',
'quantity' => 1,
'unit_price' => new Price('10.00', '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;
}
}