You are here

public function AjaxTest::testCheckoutPaneAjax in Ubercart 8.4

Tests Ajax on the checkout panes.

File

uc_store/tests/src/FunctionalJavascript/AjaxTest.php, line 155

Class

AjaxTest
Tests Ajax updating of checkout and order pages.

Namespace

Drupal\Tests\uc_store\FunctionalJavascript

Code

public function testCheckoutPaneAjax() {

  /** @var \Behat\Mink\Element\DocumentElement $page */
  $page = $this
    ->getSession()
    ->getPage();

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // Create two shipping quote methods.
  $quote1 = $this
    ->createQuote();
  $quote2 = $this
    ->createQuote();

  // Create two unique policy messages for our two payment methods.
  // Use randomMachineName() as randomString() has escaping problems when
  // sent over Ajax.
  // @see https://www.drupal.org/node/2664320
  $policy1 = $this
    ->randomMachineName();
  $policy2 = $this
    ->randomMachineName();

  // Add first Cash-On-Delivery payment method.
  $payment1 = $this
    ->createPaymentMethod('cod', [
    'settings[policy]' => $policy1,
  ]);

  // Add second COD method, with different policy message.
  $payment2 = $this
    ->createPaymentMethod('cod', [
    'settings[policy]' => $policy2,
  ]);

  // Add a shippable product to the cart.
  $product = $this
    ->createProduct([
    'shippable' => 1,
  ]);
  $this
    ->addToCart($product);

  // Can't set a number form element with  fillField() or drupalPostForm().
  // $this->drupalPostForm('cart', ['items[0][qty]' => 1], 'Checkout');
  $this
    ->drupalGet('cart');

  // $page->fillField('items[0][qty]',  1);
  $page
    ->findButton('Checkout')
    ->press();
  $assert
    ->assertWaitOnAjaxRequest();

  //
  // Changing the payment method.
  //
  // Change the payment method to $payment1. This should trigger Ajax
  // to change the payment pane and show the correct policy message.
  $page
    ->findField('panes[payment][payment_method]')
    ->selectOption($payment1['id']);
  $assert
    ->assertWaitOnAjaxRequest();
  $field = $page
    ->findField('panes[payment][payment_method]');
  $this
    ->assertNotEmpty($field);
  $this
    ->assertEquals($field
    ->getValue(), $payment1['id']);

  // Check that the payment method detail div changes.
  $assert
    ->pageTextContains($policy1, 'After changing the payment method, the payment method policy string is updated.');

  // Now change the payment method to $payment2. This should trigger Ajax
  // to change the payment pane and show the correct policy message.
  $page
    ->findField('panes[payment][payment_method]')
    ->selectOption($payment2['id']);
  $assert
    ->assertWaitOnAjaxRequest();
  $field = $page
    ->findField('panes[payment][payment_method]');
  $this
    ->assertNotEmpty($field);
  $this
    ->assertEquals($field
    ->getValue(), $payment2['id']);

  // Check that the payment method detail div changes.
  $assert
    ->pageTextContains($policy2, 'After changing again the payment method, the payment method policy string is updated.');

  //
  // Changing the shipping method.
  //
  // Change the shipping quote to $quote1. This should trigger Ajax
  // to change the order total pane to show the quote.
  $page
    ->findField('panes[quotes][quotes][quote_option]')
    ->selectOption($quote1
    ->id() . '---0');
  $assert
    ->assertWaitOnAjaxRequest();
  $field = $page
    ->findField('panes[quotes][quotes][quote_option]');
  $this
    ->assertNotEmpty($field);
  $this
    ->assertEquals($field
    ->getValue(), $quote1
    ->id() . '---0');

  // Check that the shipping line item in the payment pane shows the correct
  // quote method title and price.
  $this
    ->assertEquals($page
    ->find('css', 'tr.line-item-shipping td.title')
    ->getHtml(), $quote1
    ->label() . ':');
  $config = $quote1
    ->getPluginConfiguration();
  $rate = (double) $config['base_rate'] + (double) $config['product_rate'];
  $this
    ->assertEquals($page
    ->find('css', 'tr.line-item-shipping td.price')
    ->getHtml(), uc_currency_format($rate));

  // Change the shipping quote to $quote2. This should trigger Ajax
  // to change the order total pane to show the quote.
  $page
    ->findField('panes[quotes][quotes][quote_option]')
    ->selectOption($quote2
    ->id() . '---0');
  $assert
    ->assertWaitOnAjaxRequest();
  $field = $page
    ->findField('panes[quotes][quotes][quote_option]');
  $this
    ->assertNotEmpty($field);
  $this
    ->assertEquals($field
    ->getValue(), $quote2
    ->id() . '---0');

  // Check that the shipping line item in the payment pane shows the correct
  // quote method title and price.
  $this
    ->assertEquals($page
    ->find('css', 'tr.line-item-shipping td.title')
    ->getHtml(), $quote2
    ->label() . ':');
  $config = $quote2
    ->getPluginConfiguration();
  $rate = (double) $config['base_rate'] + (double) $config['product_rate'];
  $this
    ->assertEquals($page
    ->find('css', 'tr.line-item-shipping td.price')
    ->getHtml(), uc_currency_format($rate));
}