|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\commerce_tax\Kernel; |
| 4 | + |
| 5 | +use Drupal\commerce_order\Entity\Order; |
| 6 | +use Drupal\commerce_order\Entity\OrderItem; |
| 7 | +use Drupal\commerce_order\Entity\OrderItemType; |
| 8 | +use Drupal\commerce_price\Price; |
| 9 | +use Drupal\commerce_tax\Entity\TaxType; |
| 10 | +use Drupal\profile\Entity\Profile; |
| 11 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests integration with orders. |
| 15 | + * |
| 16 | + * @group commerce |
| 17 | + */ |
| 18 | +class OrderIntegrationTest extends CommerceKernelTestBase { |
| 19 | + |
| 20 | + /** |
| 21 | + * A sample order. |
| 22 | + * |
| 23 | + * @var \Drupal\commerce_order\Entity\OrderInterface |
| 24 | + */ |
| 25 | + protected $order; |
| 26 | + |
| 27 | + /** |
| 28 | + * Modules to enable. |
| 29 | + * |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + public static $modules = [ |
| 33 | + 'entity_reference_revisions', |
| 34 | + 'path', |
| 35 | + 'profile', |
| 36 | + 'state_machine', |
| 37 | + 'commerce_order', |
| 38 | + 'commerce_tax', |
| 39 | + ]; |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + protected function setUp() { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->installEntitySchema('profile'); |
| 48 | + $this->installEntitySchema('commerce_order'); |
| 49 | + $this->installEntitySchema('commerce_order_item'); |
| 50 | + $this->installConfig(['commerce_order']); |
| 51 | + $user = $this->createUser(['mail' => $this->randomString() . '@example.com']); |
| 52 | + |
| 53 | + $this->store->set('prices_include_tax', TRUE); |
| 54 | + $this->store->save(); |
| 55 | + |
| 56 | + // The default store is US-WI, so imagine that the US has VAT. |
| 57 | + TaxType::create([ |
| 58 | + 'id' => 'us_vat', |
| 59 | + 'label' => 'US VAT', |
| 60 | + 'plugin' => 'custom', |
| 61 | + 'configuration' => [ |
| 62 | + 'display_inclusive' => TRUE, |
| 63 | + 'rates' => [ |
| 64 | + [ |
| 65 | + 'id' => 'standard', |
| 66 | + 'label' => 'Standard', |
| 67 | + 'amount' => '0.2', |
| 68 | + ], |
| 69 | + ], |
| 70 | + 'territories' => [ |
| 71 | + ['country_code' => 'US', 'administrative_area' => 'WI'], |
| 72 | + ['country_code' => 'US', 'administrative_area' => 'SC'], |
| 73 | + ], |
| 74 | + ], |
| 75 | + ])->save(); |
| 76 | + OrderItemType::create([ |
| 77 | + 'id' => 'test', |
| 78 | + 'label' => 'Test', |
| 79 | + 'orderType' => 'default', |
| 80 | + ])->save(); |
| 81 | + $order = Order::create([ |
| 82 | + 'type' => 'default', |
| 83 | + 'store_id' => $this->store->id(), |
| 84 | + 'state' => 'draft', |
| 85 | + 'mail' => $user->getEmail(), |
| 86 | + 'uid' => $user->id(), |
| 87 | + 'ip_address' => '127.0.0.1', |
| 88 | + 'order_number' => '6', |
| 89 | + ]); |
| 90 | + $order->save(); |
| 91 | + $this->order = $this->reloadEntity($order); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Tests that the store address is used as a default for new orders. |
| 96 | + */ |
| 97 | + public function testDefaultProfile() { |
| 98 | + $order_item = OrderItem::create([ |
| 99 | + 'type' => 'test', |
| 100 | + 'quantity' => '1', |
| 101 | + 'unit_price' => new Price('12.00', 'USD'), |
| 102 | + ]); |
| 103 | + $order_item->save(); |
| 104 | + $this->order->addItem($order_item); |
| 105 | + $this->order->save(); |
| 106 | + $adjustments = $this->order->collectAdjustments(); |
| 107 | + $adjustment = reset($adjustments); |
| 108 | + $this->assertCount(1, $adjustments); |
| 109 | + $this->assertEquals(new Price('2.00', 'USD'), $adjustment->getAmount()); |
| 110 | + $this->assertEquals('us_vat|default|standard', $adjustment->getSourceId()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Tests the usage of default billing profile. |
| 115 | + */ |
| 116 | + public function testBillingProfile() { |
| 117 | + $profile = Profile::create([ |
| 118 | + 'type' => 'customer', |
| 119 | + 'address' => [ |
| 120 | + 'country_code' => 'US', |
| 121 | + 'administrative_area' => 'SC', |
| 122 | + ], |
| 123 | + ]); |
| 124 | + $profile->save(); |
| 125 | + $order_item = OrderItem::create([ |
| 126 | + 'type' => 'test', |
| 127 | + 'quantity' => '1', |
| 128 | + 'unit_price' => new Price('12.00', 'USD'), |
| 129 | + ]); |
| 130 | + $order_item->save(); |
| 131 | + $this->order->addItem($order_item); |
| 132 | + $this->order->setBillingProfile($profile); |
| 133 | + $this->order->save(); |
| 134 | + $adjustments = $this->order->collectAdjustments(); |
| 135 | + $adjustment = reset($adjustments); |
| 136 | + $this->assertCount(1, $adjustments); |
| 137 | + $this->assertEquals(new Price('2.00', 'USD'), $adjustment->getAmount()); |
| 138 | + $this->assertEquals('us_vat|default|standard', $adjustment->getSourceId()); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments