You are here

public function CommerceCheckoutTestProcess::testCommerceCheckoutProcessAuthenticatedUser in Commerce Core 7

Test the checkout process using an authenticated user.

File

modules/checkout/tests/commerce_checkout.test, line 113
Functional tests for the commerce checkout module.

Class

CommerceCheckoutTestProcess
Test checkout process.

Code

public function testCommerceCheckoutProcessAuthenticatedUser() {

  // Log in as normal user.
  $this
    ->drupalLogin($this->store_customer);

  // Order creation, in cart status.
  $this->order = $this
    ->createDummyOrder($this->store_customer->uid);

  // Access to checkout page.
  $this
    ->drupalGet($this
    ->getCommerceUrl('checkout'));

  // Check if the page resolves and if the default panes are present.
  $this
    ->assertResponse(200, t('The owner of the order can access to the checkout page'));
  $this
    ->assertTitle(t('Checkout') . ' | Drupal', t('Title of the checkout phase is correct'));
  $this
    ->assertText(t('Shopping cart contents'), t('Shopping cart contents pane is present'));
  $this
    ->assertText(t('Billing information'), t('Billing information pane is present'));

  // We are testing with authenticated user, so no account information
  // should appear.
  $this
    ->assertNoText(t('Account information'), t('Account information pane is not present'));

  // Generate random information, as city, postal code, etc.
  $address_info = $this
    ->generateAddressInformation();

  // Fill in the billing address information.
  $billing_pane = $this
    ->xpath("//select[starts-with(@name, 'customer_profile_billing[commerce_customer_address]')]");
  $this
    ->drupalPostAJAX(NULL, array(
    (string) $billing_pane[0]['name'] => 'US',
  ), (string) $billing_pane[0]['name']);

  // Check if the country has been selected correctly, this uses XPath as the
  //  ajax call replaces the element and the id may change.
  $this
    ->assertFieldByXPath("//select[starts-with(@id, 'edit-customer-profile-billing-commerce-customer-address')]//option[@selected='selected']", 'US', t('Country selected'));

  // Fill in the required information for billing pane, with a random State.
  $info = array(
    'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $address_info['name_line'],
    'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $address_info['thoroughfare'],
    'customer_profile_billing[commerce_customer_address][und][0][locality]' => $address_info['locality'],
    'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => 'KY',
    'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => $address_info['postal_code'],
  );
  $this
    ->drupalPost(NULL, $info, t('Continue to next step'));

  // Check for default panes and information in this checkout phase.
  $this
    ->pass(t('Checking the default panes and the page information:'));
  $this
    ->assertTitle(t('Review order') . ' | Drupal', t('Title of the checkout phase \'Review order\' is correct'));
  $this
    ->assertText($address_info['name_line'], t('Billing information for \'name_line\' is correct'));
  $this
    ->assertText($address_info['thoroughfare'], t('Billing information for \'thoroughfare\' is correct'));
  $this
    ->assertText($address_info['locality'], t('Billing information for \'locality\' is correct'));
  $this
    ->assertText(trim($address_info['postal_code']), t('Billing information for \'postal_code\' is correct'));
  $this
    ->assertText('United States', t('Billing information country is correct'));
  $this
    ->assertText('Example payment', t('Example payment method pane is present'));

  // Load the order to check the status.
  $order = commerce_order_load_multiple(array(
    $this->order->order_id,
  ), array(), TRUE);

  // Reset the cache as we don't want to keep the lock.
  entity_get_controller('commerce_order')
    ->resetCache();

  // At this point we should be in Checkout Review.
  $this
    ->assertEqual(reset($order)->status, 'checkout_review', t('Order status is \'Checkout Review\' in the review phase.'));

  // Test the back & continue buttons.
  $this
    ->drupalPost(NULL, array(), t('Go back'));
  $this
    ->assertTitle(t('Checkout') . ' | Drupal', t('When clicking in the \'Back\' button, the title displayed corresponds with the current checkout phase: \'Checkout\''));
  $this
    ->drupalPost(NULL, array(), t('Continue to next step'));
  $this
    ->assertTitle(t('Review order') . ' | Drupal', t('When clicking in the \'Continue\' button, the title displayed corresponds with the current checkout phase: \'Review order\''));

  // Finish checkout process
  $this
    ->drupalPost(NULL, array(), t('Continue to next step'));

  // Reload the order directly from db to update status.
  $order = commerce_order_load_multiple(array(
    $this->order->order_id,
  ), array(), TRUE);

  // Order status should be pending when completing checkout process.
  $this
    ->assertEqual(reset($order)->status, 'pending', t('Order status is \'Pending\' after completing checkout'));

  // Check if the completion message has been displayed.
  $this
    ->assertTitle(t('Checkout complete') . ' | Drupal', t('Title of the page is \'Checkout complete\' when finishing the checkout process'));
  $this
    ->assertText(t('Your order number is @order-number.', array(
    '@order-number' => $this->order->order_number,
  )), t('Completion message for the checkout is correctly displayed'));
}