View source
<?php
namespace Drupal\uc_quote\Tests;
use Drupal\uc_country\Entity\Country;
use Drupal\uc_order\Entity\Order;
use Drupal\uc_quote\Entity\ShippingQuoteMethod;
use Drupal\uc_store\Tests\UbercartTestBase;
class QuoteTest extends UbercartTestBase {
public static $modules = [
'uc_payment',
'uc_payment_pack',
'uc_quote',
];
public static $adminPermissions = [
'configure quotes',
];
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this
->createPaymentMethod('check');
Country::load('US')
->enable()
->save();
Country::load('CA')
->enable()
->save();
\Drupal::configFactory()
->getEditable('uc_store.settings')
->set('address.country', 'US')
->save();
}
protected function createQuote(array $edit = [], $condition = FALSE) {
$edit += [
'id' => $this
->randomMachineName(),
'label' => $this
->randomMachineName(),
'status' => 1,
'weight' => 0,
'plugin' => 'flatrate',
'settings' => [
'base_rate' => mt_rand(1, 10),
'product_rate' => mt_rand(1, 10),
],
];
$method = ShippingQuoteMethod::create($edit);
$method
->save();
return $method;
}
protected function selectCountry($country_id) {
$country_name = \Drupal::service('country_manager')
->getCountry($country_id)->name;
$dom = new \DOMDocument();
$dom
->loadHTML($this->content);
$parent = $dom
->getElementById('edit-panes-delivery-delivery-country');
$options = $parent
->getElementsByTagName('option');
for ($i = 0; $i < $options->length; $i++) {
if ($options
->item($i)->textContent == $country_name) {
$options
->item($i)
->setAttribute('selected', 'selected');
}
else {
$options
->item($i)
->removeAttribute('selected');
}
}
$this
->setRawContent($dom
->saveHTML());
return $this
->drupalPostAjaxForm(NULL, [], 'panes[delivery][country]');
}
protected function selectQuote($n) {
$xpath = '//*[@name="panes[quotes][quotes][quote_option]"]';
$elements = $this
->xpath($xpath);
$vals = [];
foreach ($elements as $element) {
$vals[(string) $element['id']] = (string) $element['value'];
}
$dom = new \DOMDocument();
$dom
->loadHTML($this->content);
$i = 0;
$selected = '';
foreach ($vals as $id => $value) {
if ($i == $n) {
$dom
->getElementById($id)
->setAttribute('checked', 'checked');
$selected = $value;
}
else {
$dom
->getElementById($id)
->removeAttribute('checked');
}
$i++;
}
$this
->setRawContent($dom
->saveHTML());
$option = [
'panes[quotes][quotes][quote_option]' => $selected,
];
return $this
->drupalPostAjaxForm(NULL, [], $option);
}
public function testNoQuote() {
$product = $this
->createProduct([
'shippable' => 0,
]);
$quote = $this
->createQuote();
$this
->addToCart($product);
$this
->drupalPostForm('cart', [
'items[0][qty]' => 1,
], t('Checkout'));
$this
->assertNoText(t('Calculate shipping cost'), 'Shipping pane is not present with no shippable item.');
}
public function testQuote() {
$product = $this
->createProduct();
$quote1 = $this
->createQuote();
$quote2 = $this
->createQuote([
'weight' => 1,
], [
'LABEL' => 'quote_conditions',
'PLUGIN' => 'and',
'REQUIRES' => [
'rules',
],
'USES VARIABLES' => [
'order' => [
'type' => 'uc_order',
'label' => 'Order',
],
],
'AND' => [
[
'data_is' => [
'data' => [
'order:delivery-address:country',
],
'value' => 'US',
],
],
],
]);
$qty = mt_rand(2, 100);
foreach ([
$quote1,
$quote2,
] as $quote) {
$configuration = $quote
->getPluginConfiguration();
$quote->amount = uc_currency_format($configuration['base_rate'] + $configuration['product_rate'] * $qty);
$quote->option_text = $quote
->label() . ': ' . $quote->amount;
$quote->total = uc_currency_format($product->price->value * $qty + $configuration['base_rate'] + $configuration['product_rate'] * $qty);
}
$this
->addToCart($product);
$this
->drupalPostForm('cart', [
'items[0][qty]' => $qty,
], t('Checkout'));
$this
->assertText($quote1
->label(), 'The default quote option is available');
$this
->assertText($quote2
->label(), 'The second quote option is available');
$this
->assertText($quote1->total, 'Order total includes the default quote.');
$edit = [
'panes[quotes][quotes][quote_option]' => $quote2
->id() . '---0',
];
$edit = $this
->populateCheckoutForm($edit);
$result = $this
->ucPostAjax(NULL, $edit, $edit);
$this
->assertText($quote2->total, 'The order total includes the selected quote.');
$edit['panes[quotes][quotes][quote_option]'] = $quote1
->id() . '---0';
$edit = $this
->populateCheckoutForm($edit);
$this
->drupalPostForm(NULL, $edit, t('Review order'));
$this
->assertRaw(t('Your order is almost complete.'));
$this
->assertText($quote1->total, 'The total is correct on the order review page.');
$this
->drupalPostForm(NULL, [], t('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) {
$order = Order::load($order_id);
foreach ($order->line_items as $line) {
if ($line['type'] == 'shipping') {
break;
}
}
$this
->assertEqual($line['type'], 'shipping', 'The shipping line item was saved to the order.');
$this
->assertEqual($quote1->amount, uc_currency_format($line['amount']), 'Stored shipping line item has the correct amount.');
$this
->drupalGet('admin/store/orders/' . $order_id);
$this
->assertText($quote1->total, 'The total is correct on the order admin view page.');
$this
->drupalGet('admin/store/orders/' . $order_id . '/edit');
$this
->assertFieldByName('line_items[' . $line['line_item_id'] . '][title]', $quote1
->label(), 'Found the correct shipping line item title.');
$this
->assertFieldByName('line_items[' . $line['line_item_id'] . '][amount]', substr($quote1->amount, 1), 'Found the correct shipping line item title.');
$result = $this
->ucPostAjax('admin/store/orders/' . $order_id . '/edit', [], [
'op' => t('Get shipping quotes'),
]);
$this
->assertText($quote1->option_text, 'The default quote is available on the order-edit page.');
$this
->assertText($quote2->option_text, 'The second quote is available on the order-edit page.');
}
else {
$this
->fail('No order was created.');
}
}
}