View source
<?php
namespace Drupal\Tests\uc_tax\Functional;
use Drupal\uc_order\Entity\Order;
class StoredTaxTest extends TaxTestBase {
public function testTaxDisplay() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->createPaymentMethod('check');
$rate = (object) [
'name' => $this
->randomMachineName(8),
'rate' => 0.2,
'taxed_product_types' => [
'product',
],
'taxed_line_items' => [],
'weight' => 0,
'shippable' => 0,
'display_include' => 1,
'inclusion_text' => '',
];
uc_tax_rate_save($rate);
$this
->drupalGet('admin/store/config/taxes');
$assert
->pageTextContains($rate->name);
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$assert
->pageTextContains('Enter your billing address and information here.');
$assert
->responseContains($rate->name);
$assert
->responseContains(uc_currency_format($rate->rate * $this->product->price->value));
$edit = $this
->populateCheckoutForm();
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$assert
->responseContains('Your order is almost complete.');
$assert
->responseContains($rate->name);
$assert
->responseContains(uc_currency_format($rate->rate * $this->product->price->value));
$this
->submitForm([], 'Submit order');
$order_ids = \Drupal::entityQuery('uc_order')
->condition('delivery_first_name', $edit['panes[delivery][first_name]'])
->execute();
$order_id = reset($order_ids);
if ($order_id) {
$this
->assertTrue(TRUE, 'Order ' . $order_id . ' has been created');
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->assertTaxLineCorrect($this
->loadTaxLine($order_id), $rate->rate, 'on initial order load');
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->submitForm([], 'Save changes');
$assert
->pageTextContains('Order changes saved.');
$this
->assertTaxLineCorrect($this
->loadTaxLine($order_id), $rate->rate, 'after saving order');
$oldrate = $rate->rate;
$rate->rate = 0.1;
$rate = uc_tax_rate_save($rate);
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->submitForm([], 'Save changes');
$assert
->pageTextContains('Order changes saved.');
$this
->assertTaxLineCorrect($this
->loadTaxLine($order_id), $oldrate, 'after rate change');
$class = $this
->createProductClass();
$rate->taxed_product_types = [
$class
->getEntityTypeId(),
];
uc_tax_rate_save($rate);
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->submitForm([], 'Save changes');
$assert
->pageTextContains('Order changes saved.');
$this
->assertTaxLineCorrect($this
->loadTaxLine($order_id), $oldrate, 'after applicable product change');
Order::load($order_id)
->setStatusId('in_checkout')
->save();
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->submitForm([], 'Save changes');
$assert
->pageTextContains('Order changes saved.');
$this
->assertFalse($this
->loadTaxLine($order_id), 'The tax line was removed from the order when order status changed back to in_checkout.');
$rate->taxed_product_types = [
'product',
];
uc_tax_rate_save($rate);
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->submitForm([], 'Save changes');
$assert
->pageTextContains('Order changes saved.');
$this
->assertTaxLineCorrect($this
->loadTaxLine($order_id), $rate->rate, 'when order status changed back to in_checkout');
}
else {
$this
->fail('No order was created.');
}
}
protected function loadTaxLine($order_id) {
\Drupal::entityTypeManager()
->getStorage('uc_order')
->resetCache([
$order_id,
]);
$order = Order::load($order_id);
foreach ($order->line_items as $line) {
if ($line['type'] == 'tax') {
return $line;
}
}
return FALSE;
}
protected function assertTaxLineCorrect($line, $rate, $when) {
$assert = $this
->assertSession();
$this
->assertTrue($line, 'The tax line item was saved to the order ' . $when);
$this
->assertEquals(number_format($rate * $this->product->price->value, 2), number_format($line['amount'], 2), 'Stored tax line item has the correct amount ' . $when);
$assert
->hiddenFieldValueEquals('line_items[' . $line['line_item_id'] . '][li_id]', $line['line_item_id']);
$assert
->pageTextContains($line['title']);
$this
->assertTrue(TRUE, 'Found the tax title ' . $when);
$assert
->pageTextContains(uc_currency_format($line['amount']));
$this
->assertTrue(TRUE, 'Tax display has the correct amount ' . $when);
}
}