You are here

public function PaymentCheckoutTest::testFreeOrder in Commerce Core 8.2

Tests a free order, where only the billing information is collected.

File

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

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testFreeOrder() {

  // Prepare a different address book profile to test switching.
  $new_address = [
    'given_name' => 'Johnny',
    'family_name' => 'Appleseed',
    'address_line1' => '123 New York Drive',
    'locality' => 'New York City',
    'administrative_area' => 'NY',
    'postal_code' => '10001',
    'country_code' => 'US',
  ];
  $new_address_book_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $new_address,
  ]);
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');

  // Add an adjustment to zero out the order total.
  $order = Order::load(1);
  $order
    ->addAdjustment(new Adjustment([
    'type' => 'custom',
    'label' => 'Surprise, it is free!',
    'amount' => $order
      ->getTotalPrice()
      ->multiply('-1'),
    'locked' => TRUE,
  ]));
  $order
    ->save();
  $this
    ->drupalGet('checkout/1');
  $this
    ->assertSession()
    ->pageTextContains('Billing information');
  $this
    ->assertSession()
    ->pageTextNotContains('Payment information');
  $this
    ->assertRenderedAddress($this->defaultAddress, 'payment_information[billing_information]');
  $this
    ->getSession()
    ->getPage()
    ->fillField('payment_information[billing_information][select_address]', $new_address_book_profile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($new_address, 'payment_information[billing_information]');
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Billing information');
  $this
    ->assertSession()
    ->pageTextNotContains('Payment information');
  $this
    ->assertSession()
    ->pageTextContains('Example');
  $this
    ->assertSession()
    ->pageTextContains('Johnny Appleseed');
  $this
    ->assertSession()
    ->pageTextContains('123 New York Drive');
  $this
    ->submitForm([], 'Complete checkout');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
}