You are here

public function CreditCardTest::testCheckout in Ubercart 8.4

Tests that an order can be placed using the test gateway.

File

payment/uc_credit/tests/src/Functional/CreditCardTest.php, line 184

Class

CreditCardTest
Tests credit card payments with the test gateway.

Namespace

Drupal\Tests\uc_credit\Functional

Code

public function testCheckout() {

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

  // Submit the checkout page. Note that because of the Ajax on the country
  // fields, which is used to populate the zone select, the zone doesn't
  // actually get set by this post. That's OK because we're not checking that
  // yet. But we need to make sure that the next time we post this page
  // (which now has the country set from the first post) we include the zones.
  $edit = $this
    ->populateCheckoutForm([
    'panes[payment][details][cc_number]' => array_rand(array_flip(self::$cardTestNumbers)),
    'panes[payment][details][cc_cvv]' => mt_rand(100, 999),
    'panes[payment][details][cc_exp_month]' => mt_rand(1, 12),
    'panes[payment][details][cc_exp_year]' => mt_rand(date('Y') + 1, 2022),
  ]);
  $this
    ->drupalGet('cart/checkout');
  $this
    ->submitForm($edit, 'Review order');

  // Confirm that truncated credit card number was found.
  $assert
    ->pageTextContains('(Last 4) ' . substr($edit['panes[payment][details][cc_number]'], -4));

  // Confirm that expiry date was found.
  $assert
    ->pageTextContains($edit['panes[payment][details][cc_exp_year]']);

  // Go back. Form will still be populated, but verify that the credit
  // card number is truncated and CVV is masked for security.
  $this
    ->submitForm([], 'Back');

  // Check that truncated credit card number found.
  $assert
    ->fieldValueEquals('panes[payment][details][cc_number]', '(Last 4) ' . substr($edit['panes[payment][details][cc_number]'], -4));

  // Check that masked CVV found.
  $assert
    ->fieldValueEquals('panes[payment][details][cc_cvv]', '---');

  // Check that expiry month found.
  $assert
    ->fieldValueEquals('panes[payment][details][cc_exp_month]', $edit['panes[payment][details][cc_exp_month]']);

  // Check that expiry year found.
  $assert
    ->fieldValueEquals('panes[payment][details][cc_exp_year]', $edit['panes[payment][details][cc_exp_year]']);

  // Change the card number and fail with a known-bad CVV.
  $edit['panes[payment][details][cc_number]'] = array_rand(array_flip(self::$cardTestNumbers));
  $edit['panes[payment][details][cc_cvv]'] = '000';

  // If zones were set, we must re-submit them here to work around the Ajax
  // situation described above. So we just re-submit all the data to be safe.
  $this
    ->submitForm($edit, 'Review order');

  // Confirm that truncated updated credit card number was found.
  $assert
    ->pageTextContains('(Last 4) ' . substr($edit['panes[payment][details][cc_number]'], -4));

  // Try to submit the bad CVV.
  $this
    ->submitForm([], 'Submit order');
  $assert
    ->pageTextContains('We were unable to process your credit card payment. Please verify your details and try again.');

  // Go back. Again check for truncated card number and masked CVV.
  $this
    ->submitForm([], 'Back');

  // Verify truncated updated credit card number found.
  $assert
    ->fieldValueEquals('panes[payment][details][cc_number]', '(Last 4) ' . substr($edit['panes[payment][details][cc_number]'], -4));

  // Verify masked CVV found.
  $assert
    ->fieldValueEquals('panes[payment][details][cc_cvv]', '---');

  // Fix the CVV and repost.
  $edit['panes[payment][details][cc_cvv]'] = mt_rand(100, 999);
  $this
    ->submitForm($edit, 'Review order');

  // Check for success.
  $this
    ->submitForm([], 'Submit order');
  $assert
    ->pageTextContains('Your order is complete!');
}