You are here

public function CheckoutOrderTest::testCheckout in Commerce Core 8.2

Tests anonymous and authenticated checkout.

File

modules/checkout/tests/src/Functional/CheckoutOrderTest.php, line 148

Class

CheckoutOrderTest
Tests the checkout of an order.

Namespace

Drupal\Tests\commerce_checkout\Functional

Code

public function testCheckout() {
  $config = \Drupal::configFactory()
    ->getEditable('commerce_checkout.commerce_checkout_flow.default');
  $config
    ->set('configuration.display_checkout_progress_breadcrumb_links', TRUE);
  $config
    ->save();
  $this
    ->drupalLogout();
  $this
    ->drupalGet($this->product
    ->toUrl());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->assertSession()
    ->pageTextContains('1 item');
  $cart_link = $this
    ->getSession()
    ->getPage()
    ->findLink('your cart');
  $cart_link
    ->click();
  $this
    ->submitForm([], 'Checkout');
  $this
    ->assertSession()
    ->pageTextNotContains('Order Summary');

  // Check breadcrumbs are links.
  $this
    ->assertSession()
    ->elementsCount('css', '.block-commerce-checkout-progress li.checkout-progress--step > a', 0);
  $this
    ->submitForm([], 'Continue as Guest');

  // Check breadcrumb link functionality.
  $this
    ->assertSession()
    ->elementsCount('css', '.block-commerce-checkout-progress li.checkout-progress--step > a', 1);
  $this
    ->getSession()
    ->getPage()
    ->findLink('Login')
    ->click();
  $this
    ->assertSession()
    ->pageTextNotContains('Order Summary');
  $this
    ->assertCheckoutProgressStep('Login');
  $this
    ->submitForm([], 'Continue as Guest');
  $this
    ->assertCheckoutProgressStep('Order information');
  $this
    ->submitForm([
    'contact_information[email]' => 'guest@example.com',
    'contact_information[email_confirm]' => 'guest@example.com',
    'billing_information[profile][address][0][address][given_name]' => 'John',
    'billing_information[profile][address][0][address][family_name]' => 'Smith',
    'billing_information[profile][address][0][address][organization]' => 'Centarro',
    'billing_information[profile][address][0][address][address_line1]' => '9 Drupal Ave',
    'billing_information[profile][address][0][address][postal_code]' => '94043',
    'billing_information[profile][address][0][address][locality]' => 'Mountain View',
    'billing_information[profile][address][0][address][administrative_area]' => 'CA',
  ], 'Continue to review');
  $this
    ->assertCheckoutProgressStep('Review');
  $this
    ->assertSession()
    ->elementsCount('css', '.block-commerce-checkout-progress li.checkout-progress--step > a', 2);
  $this
    ->assertSession()
    ->pageTextContains('Contact information');
  $this
    ->assertSession()
    ->pageTextContains('Billing information');
  $this
    ->assertSession()
    ->pageTextContains('Order Summary');
  $this
    ->submitForm([], 'Complete checkout');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
  $this
    ->assertSession()
    ->pageTextContains('0 items');
  $order = Order::load(1);

  // Confirm that the checkout completion event was fired.
  $this
    ->assertTrue(TRUE, $order
    ->getData('checkout_completed'));

  // Confirm that the profile hasn't been copied to the address book yet.
  $billing_profile = $order
    ->getBillingProfile();
  $this
    ->assertTrue($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertEmpty($billing_profile
    ->getData('address_book_profile_id'));

  // Confirm that the profile has been copied after the order was assigned.
  $order_assignment = $this->container
    ->get('commerce_order.order_assignment');
  $order_assignment
    ->assign($order, $this->adminUser);
  $billing_profile = $this
    ->reloadEntity($billing_profile);
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertNotEmpty($billing_profile
    ->getData('address_book_profile_id'));

  // Test second order.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet($this->product
    ->toUrl());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->assertSession()
    ->pageTextContains('1 item');
  $cart_link = $this
    ->getSession()
    ->getPage()
    ->findLink('your cart');
  $cart_link
    ->click();
  $this
    ->submitForm([], 'Checkout');
  $this
    ->assertCheckoutProgressStep('Order information');

  // Confirm that the information from the address book profile is rendered.
  $expected_address = [
    'given_name' => 'John',
    'family_name' => 'Smith',
    'organization' => 'Centarro',
    'address_line1' => '9 Drupal Ave',
    'postal_code' => '94043',
    'locality' => 'Mountain View',
    'administrative_area' => 'CA',
  ];
  $page = $this
    ->getSession()
    ->getPage();
  foreach ($expected_address as $property => $value) {
    $this
      ->assertStringContainsString($value, $page
      ->find('css', 'p.address')
      ->getText());
    $this
      ->assertSession()
      ->fieldNotExists("billing_information[profile][address][0][address][{$property}]");
  }
  $this
    ->assertSession()
    ->fieldNotExists('billing_information[profile][copy_to_address_book]');
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Billing information');
  $this
    ->assertSession()
    ->pageTextContains('Order Summary');
  $this
    ->assertSession()
    ->elementsCount('css', '.block-commerce-checkout-progress li.checkout-progress--step > a', 1);
  $this
    ->assertCheckoutProgressStep('Review');

  // Go back with the breadcrumb.
  $this
    ->getSession()
    ->getPage()
    ->findLink('Order information')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('Order Summary');
  $this
    ->assertCheckoutProgressStep('Order information');
  $this
    ->assertSession()
    ->elementsCount('css', '.block-commerce-checkout-progress li.checkout-progress--step > a', 0);
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertCheckoutProgressStep('Review');

  // Go back and forth.
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Go back');
  $this
    ->assertCheckoutProgressStep('Order information');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Continue to review');
  $this
    ->assertCheckoutProgressStep('Review');
  $this
    ->submitForm([], 'Complete checkout');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 2. You can view your order on your account page when logged in.');
  $this
    ->assertSession()
    ->pageTextContains('0 items');
  $order = Order::load(2);

  // Confirm that the checkout completion event was fired.
  $this
    ->assertTrue(TRUE, $order
    ->getData('checkout_completed'));

  // Confirm that the billing profile has the expected address.
  $expected_address += [
    'country_code' => 'US',
  ];
  $billing_profile = $order
    ->getBillingProfile();
  $this
    ->assertEquals($expected_address, array_filter($billing_profile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertNotEmpty($billing_profile
    ->getData('address_book_profile_id'));
}