You are here

public function CustomTest::testDiscountedPrices in Commerce Core 8.2

@covers ::apply

File

modules/tax/tests/src/Kernel/Plugin/Commerce/TaxType/CustomTest.php, line 194

Class

CustomTest
@coversDefaultClass \Drupal\commerce_tax\Plugin\Commerce\TaxType\Custom @group commerce

Namespace

Drupal\Tests\commerce_tax\Kernel\Plugin\Commerce\TaxType

Code

public function testDiscountedPrices() {
  $plugin = $this->taxType
    ->getPlugin();

  // Tax-inclusive prices + display-inclusive taxes.
  // A 10.33 USD price with a 1.00 USD discount should have a 9.33 USD total.
  $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());

  // Non-tax-inclusive prices + display-inclusive taxes.
  // A 10.33 USD price is 12.40 USD with 20% tax included.
  // A 1.00 USD discount should result in a 11.40 USD total.
  $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());

  // Non-tax-inclusive prices + non-display-inclusive taxes.
  // A 10.33 USD price with a 1.00 USD discount is 9.33 USD.
  // And 9.33 USD plus 20% tax is 11.20 USD.
  $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());

  // Tax-inclusive prices + non-display-inclusive taxes.
  // A 10.33 USD price is 8.61 USD once the 20% tax is removed.
  // A 1.00 USD discount gives 7.61 USD + 20% VAT -> 8.88 USD.
  $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());
}