You are here

public function CommerceCheckoutTestProcess::testCommerceCheckoutProcessAnonymousUser in Commerce Core 7

Test the checkout process with anonymous user.

File

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

Class

CommerceCheckoutTestProcess
Test checkout process.

Code

public function testCommerceCheckoutProcessAnonymousUser() {

  // Prepare the cart for Anonymous.
  $this
    ->prepareAnonymousEnviroment();

  // 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('Anonymous user can access to the checkout page for the order.'));
  $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'));
  $this
    ->assertText(t('Account information'), t('Account information pane is present'));

  // Generate random information, as user mail, city, etc.
  $user_mail = $this
    ->randomName() . '@example.com';
  $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'],
  );

  // Also add the mail for the account pane.
  $info += array(
    'account[login][mail]' => $user_mail,
  );

  // Go to the next checkout step with the required information.
  $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'));
  $this
    ->assertText($user_mail, t('Account information is correct'));

  // 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.'));

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

  // Reload the order directly from db to check 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'));

  // Check completion message.
  $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'));
}