You are here

public function AjaxTest::testCheckoutAjax in Ubercart 8.4

Tests Ajax on the checkout form.

File

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

Class

AjaxTest
Tests Ajax updating of checkout and order pages.

Namespace

Drupal\Tests\uc_store\FunctionalJavascript

Code

public function testCheckoutAjax() {

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

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

  // Enable two payment methods and set a condition on one.
  $this
    ->createPaymentMethod('check');

  // Use randomMachineName() as randomString() has escaping problems when
  // sent over Ajax.
  // @see https://www.drupal.org/node/2664320
  $other = $this
    ->createPaymentMethod('other', [
    'label' => $this
      ->randomMachineName(),
  ]);

  // $this->addPaymentZoneCondition($other['id'], 'KS');
  // Specify that the billing zone should update the payment pane.
  \Drupal::configFactory()
    ->getEditable('uc_cart.settings')
    ->set('ajax.checkout.panes][billing][address][zone', [
    'payment-pane' => 'payment-pane',
  ])
    ->save();

  // Go to the checkout page, verify that the conditional payment method
  // is not available.
  $product = $this
    ->createProduct([
    'shippable' => 0,
  ]);
  $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();

  // @todo Re-enable when shipping quote conditions are available.
  // $this->assertNoEscaped($other['label']);
  // Set the billing zone. This should trigger Ajax to load the payment
  // pane with the applicable payment methods for this zone. We then verify
  // that payment pane contains the method we expect.
  $page
    ->findField('panes[billing][zone]')
    ->selectOption('KS');
  $assert
    ->assertWaitOnAjaxRequest();
  $field = $page
    ->findField('panes[billing][zone]');
  $this
    ->assertNotEmpty($field);
  $this
    ->assertEquals($field
    ->getValue(), 'KS');
  $assert
    ->assertEscaped($other['label']);

  // Change the billing zone. This should trigger Ajax to change the
  // available payment options. We then verify that payment pane contains
  // the new value we expect.
  $page
    ->findField('panes[billing][zone]')
    ->selectOption('AL');
  $assert
    ->assertWaitOnAjaxRequest();
  $field = $page
    ->findField('panes[billing][zone]');
  $this
    ->assertNotEmpty($field);
  $this
    ->assertEquals($field
    ->getValue(), 'AL');
  $assert
    ->assertEscaped($other['label']);

  // Not in Kansas any more...
  // @todo Re-enable when shipping quote conditions are available.
  // $this->assertNoEscaped($other['label']);
}