You are here

public function PaymentCheckoutTest::testCheckoutWithStoredOffsiteRedirectPost in Commerce Core 8.2

Tests checkout with a stored off-site gateway (POST redirect method).

File

modules/payment/tests/src/FunctionalJavascript/PaymentCheckoutTest.php, line 716

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testCheckoutWithStoredOffsiteRedirectPost() {

  // Remove the initial test payment methods.
  PaymentMethod::load(1)
    ->delete();
  PaymentMethod::load(2)
    ->delete();
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $this
    ->assertSession()
    ->fieldNotExists('Visa ending in 1111');
  $radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('Credit card (Example Stored Offsite)');
  $radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->defaultAddress, 'payment_information[billing_information]');
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Example');
  $this
    ->assertSession()
    ->pageTextContains('Bryan Centarro');
  $this
    ->assertSession()
    ->pageTextContains('9 Drupal Ave');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
  $order = Order::load(1);
  $this
    ->assertFalse($order
    ->isLocked());
  $this
    ->assertEquals('stored_offsite', $order
    ->get('payment_gateway')->target_id);
  $this
    ->assertEquals(3, $order
    ->get('payment_method')->target_id);
  $payment = Payment::load(1);
  $this
    ->assertNotNull($payment);
  $this
    ->assertEquals($payment
    ->getAmount(), $order
    ->getTotalPrice());

  // Verify that a reusable payment method was created.
  $payment_method = $payment
    ->getPaymentMethod();
  $this
    ->assertEquals(TRUE, $payment_method
    ->isReusable());
  $this
    ->assertEquals('stored_offsite', $payment_method
    ->getPaymentGatewayId());
  $this
    ->assertEquals(3, $payment_method
    ->id());

  // Assert that the created payment method can be loaded.
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->createScreenshot('../checkout_1.png');
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->createScreenshot('../checkout_2.png');
  $this
    ->drupalGet('checkout/2');
  $this
    ->createScreenshot('../checkout_3.png');
  $radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('Visa ending in 1111');
  $radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 2. You can view your order on your account page when logged in.');
  $order = Order::load(2);
  $this
    ->assertFalse($order
    ->isLocked());
  $this
    ->assertEquals('stored_offsite', $order
    ->get('payment_gateway')->target_id);
  $this
    ->assertEquals(3, $order
    ->get('payment_method')->target_id);
  $payment = Payment::load(1);
  $this
    ->assertNotNull($payment);
  $this
    ->assertEquals($payment
    ->getAmount(), $order
    ->getTotalPrice());
  $this
    ->assertEquals('Z', $payment
    ->getAvsResponseCode());
  $this
    ->assertEquals('ZIP', $payment
    ->getAvsResponseCodeLabel());
  $this
    ->assertEquals(3, $payment
    ->get('payment_method')->target_id);
}