You are here

public function PaymentCheckoutTest::testManual in Commerce Core 8.2

Tests checkout with a manual gateway.

File

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

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testManual() {
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');

  // Make the order partially paid, to confirm that checkout only charges
  // for the remaining amount.
  $payment = Payment::create([
    'type' => 'payment_manual',
    'payment_gateway' => 'manual',
    'order_id' => '1',
    'amount' => new Price('20', 'USD'),
    'state' => 'completed',
  ]);
  $payment
    ->save();
  $order = Order::load(1);

  // Save the order to recalculate the balance.
  $order
    ->save();
  $this
    ->assertEquals(new Price('20', 'USD'), $order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('19.99', 'USD'), $order
    ->getBalance());
  $radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('Cash on delivery');
  $radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->defaultAddress, 'payment_information[billing_information]');
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->assertSession()
    ->pageTextContains('Cash on delivery');
  $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.');
  $this
    ->assertSession()
    ->pageTextContains('Sample payment instructions.');
  \Drupal::entityTypeManager()
    ->getStorage('commerce_order')
    ->resetCache([
    1,
  ]);
  $order = Order::load(1);
  $this
    ->assertEquals('manual', $order
    ->get('payment_gateway')->target_id);
  $this
    ->assertFalse($order
    ->isLocked());

  // Verify that a pending payment was created, and that the totals are
  // still unchanged.
  $payment = Payment::load(2);
  $this
    ->assertNotNull($payment);
  $this
    ->assertEquals('pending', $payment
    ->getState()
    ->getId());
  $this
    ->assertEquals(new Price('19.99', 'USD'), $payment
    ->getAmount());
  $this
    ->assertEquals(new Price('20', 'USD'), $order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('19.99', 'USD'), $order
    ->getBalance());
}