You are here

public function EventTrackerServicePurchaseTest::testPurchaseOrderWithTaxExclusiveOnOrderItems in Commerce Google Tag Manager 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/EventTrackerServicePurchaseTest.php \Drupal\Tests\commerce_google_tag_manager\Kernel\EventTrackerServicePurchaseTest::testPurchaseOrderWithTaxExclusiveOnOrderItems()

@covers ::purchase

Test handling Taxes on Order item which are excluded from the price.

File

tests/src/Kernel/EventTrackerServicePurchaseTest.php, line 540

Class

EventTrackerServicePurchaseTest
@coversDefaultClass \Drupal\commerce_google_tag_manager\EventTrackerService

Namespace

Drupal\Tests\commerce_google_tag_manager\Kernel

Code

public function testPurchaseOrderWithTaxExclusiveOnOrderItems() {
  $this->store
    ->set('prices_include_tax', FALSE);
  $this->store
    ->save();
  $this
    ->assertEquals(new Price('11.00', 'USD'), $this->orderItem
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('11.00', 'USD'), $this->orderItem
    ->getAdjustedUnitPrice());
  $this
    ->assertEquals(new Price('11.00', 'USD'), $this->orderItemAlt
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('11.00', 'USD'), $this->orderItemAlt
    ->getAdjustedUnitPrice());

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = Order::create([
    'type' => 'default',
    'state' => 'draft',
    'mail' => $this->user
      ->getEmail(),
    'uid' => $this->user
      ->id(),
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'billing_profile' => $this->profile,
    'store_id' => $this->store
      ->id(),
    'order_items' => [
      $this->orderItem,
      $this->orderItemAlt,
    ],
  ]);
  $order
    ->save();
  $this->orderItem
    ->addAdjustment(new Adjustment([
    'type' => 'tax',
    'label' => 'Tax',
    // 2.75 USD.
    'amount' => $this->orderItem
      ->getAdjustedUnitPrice()
      ->multiply('0.25'),
    'percentage' => '0.25',
    'source_id' => '1',
    'included' => FALSE,
  ]));
  $this->orderItem
    ->save();
  $this
    ->assertEquals(new Price('13.75', 'USD'), $this->orderItem
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('13.75', 'USD'), $this->orderItem
    ->getAdjustedUnitPrice());
  $this->orderItemAlt
    ->addAdjustment(new Adjustment([
    'type' => 'tax',
    'label' => 'Tax',
    // 6.05 USD.
    'amount' => $this->orderItemAlt
      ->getAdjustedUnitPrice()
      ->multiply('0.55'),
    'percentage' => '0.55',
    'source_id' => '1',
    'included' => FALSE,
  ]));
  $this->orderItemAlt
    ->save();
  $this
    ->assertEquals(new Price('17.05', 'USD'), $this->orderItemAlt
    ->getAdjustedTotalPrice());
  $this
    ->assertEquals(new Price('17.05', 'USD'), $this->orderItemAlt
    ->getAdjustedUnitPrice());

  // Only order-item taxes will be collected.
  $this
    ->assertCount(2, $order
    ->collectAdjustments());
  $this
    ->assertEquals(new Price('26.4', 'USD'), $order
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('22', 'USD'), $order
    ->getSubtotalPrice());
  $order
    ->recalculateTotalPrice();
  $this
    ->assertEquals(new Price('30.8', 'USD'), $order
    ->getTotalPrice());
  $this
    ->assertEquals(new Price('22', 'USD'), $order
    ->getSubtotalPrice());
  $this
    ->invokeMethod($this->eventTracker, 'purchase', [
    $order,
  ]);
  $data = $this->tempStore
    ->get('events');
  $this
    ->assertSame([
    'e4c5391625f71636d7ff637b725a4d8f' => [
      'event' => 'purchase',
      'ecommerce' => [
        'purchase' => [
          'actionField' => [
            'id' => '6',
            'affiliation' => 'Default store',
            'revenue' => '30.80',
            'shipping' => '0',
            'tax' => '8.80',
            'coupon' => '',
          ],
          'products' => [
            0 => [
              'name' => 'Lorem Ipsum',
              'id' => '1',
              'price' => '11.00',
              'variant' => 'Lorem Ipsum',
              'quantity' => 1,
            ],
            1 => [
              'name' => 'Lorem Ipsum',
              'id' => '1',
              'price' => '11.00',
              'variant' => 'Lorem Ipsum',
              'quantity' => 1,
            ],
          ],
        ],
      ],
    ],
  ], $data);
}