You are here

public function UcAddressesCartCheckoutTestCase::testCheckoutWithPreviousOrders in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 tests/uc_addresses.checkout.test \UcAddressesCartCheckoutTestCase::testCheckoutWithPreviousOrders()

Test if checkout works when customer has previous placed orders, but no addresses in the address book.

Test also if extra address fields get populated when selecting addresses from previous orders.

File

tests/uc_addresses.checkout.test, line 463
Test cases for checkout.

Class

UcAddressesCartCheckoutTestCase
Test cases for checkout.

Code

public function testCheckoutWithPreviousOrders() {

  // Enable the uc_addresses_test module.
  module_enable(array(
    'uc_addresses_test',
  ));
  drupal_get_schema(NULL, TRUE);

  // Create an order for the customer first.
  $order_data = array(
    'uid' => $this->customer->uid,
  );
  $delivery_values = UcAddressesTestCase::getEditAddressValues(array(), array(), 'order_form', 'delivery_');
  unset($delivery_values['form_values']['delivery_billing_extra1']);
  unset($delivery_values['values']['billing_extra1']);
  $billing_values = UcAddressesTestCase::getEditAddressValues(array(), array(), 'order_form', 'billing_');
  unset($billing_values['form_values']['billing_shipping_extra2']);
  unset($billing_values['values']['shipping_extra2']);
  $order_data += $delivery_values['form_values'];
  $order_data += $billing_values['form_values'];
  $order = $this
    ->createOrder($order_data);
  uc_cart_complete_sale($order, TRUE);
  uc_payment_enter($order->order_id, 'SimpleTest', $order->order_total);

  // Ensure an order was created for the customer.
  $query = db_select('uc_orders');
  $query = db_select('uc_orders');
  $query
    ->addExpression('COUNT(order_id)');
  $query
    ->condition('uid', $this->customer->uid);
  $query
    ->condition('order_status', uc_order_status_list('general', TRUE), 'IN');
  $result = $query
    ->execute();
  $count = (int) $result
    ->fetchField();
  $this
    ->assertTrue($count === 1, 'An order was created.');

  // Now go to checkout as customer.
  $this
    ->drupalLogin($this->customer);
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  $this
    ->drupalPost('cart', array(), 'Checkout');

  // Ensure that fields 'billing_extra1' and 'shipping_extra2' are present
  // and in the panes they should appear.
  $this
    ->assertFieldByName('panes[billing][address][billing_billing_extra1]');
  $this
    ->assertFieldByName('panes[delivery][address][delivery_shipping_extra2]');
  $this
    ->assertNoFieldByName('panes[billing][address][billing_shipping_extra2]');
  $this
    ->assertNoFieldByName('panes[delivery][address][delivery_billing_extra1]');

  // Fill in the checkout form, choose the first available address from the
  // address book.
  $edit = array();
  foreach (uc_addresses_order_address_types() as $address_type) {

    // Ensure the customer can choose an address for this address type.
    $field_id = 'edit-panes-' . $address_type . '-select-address';
    $result = $this
      ->assertFieldById($field_id);

    // Only continue if the address book field was found to avoid a fatal error.
    if ($result) {

      // Get address book option to choose.
      $fields = $this
        ->xpath($this
        ->constructFieldXpath('id', $field_id));
      $field = reset($fields);
      $options = $this
        ->getAllOptions($field);
      $edit['panes[' . $address_type . '][select_address]'] = (string) $options[1]
        ->attributes()->value;

      // Select address.
      $triggering_element = 'panes[' . $address_type . '][select_address]';
      $commands = $this
        ->drupalPostAJAX('cart/checkout', $edit, $triggering_element);
    }
  }

  // Omit filling in "last name" for the billing address to ensure checkout
  // will succeed if there were form errors the first time.
  $edit = array(
    'panes[billing][address][billing_last_name]' => '',
  );
  $this
    ->drupalPost(NULL, $edit, t('Review order'));

  // Ensure there was at least one form error.
  $this
    ->assertText(t('!name field is required.', array(
    '!name' => t('Last name'),
  )));

  // Now fix the form errors and try to checkout again.
  $edit = array();
  $edit['panes[billing][address][billing_last_name]'] = 'custom last name';
  $this
    ->drupalPost(NULL, $edit, t('Review order'));
  $messages = uc_cart_uc_message();
  $this
    ->assertRaw($messages['review_instructions']);
  $this
    ->drupalPost(NULL, array(), t('Submit order'));

  // Check if any new addresses got saved.
  $new_addresses = array(
    'delivery' => $delivery_values['values'],
    'billing' => $billing_values['values'],
  );
  $new_addresses['billing']['last_name'] = 'custom last name';
  foreach ($new_addresses as $address_type => $address_values) {
    $message = '';
    switch ($address_type) {
      case 'delivery':
        $message = t('The delivery address is correctly saved to the database.');
        break;
      case 'billing':
        $message = t('The billing address is correctly saved to the database.');
        break;
    }
    $this
      ->assertTrue(UcAddressesTestCase::checkAddressValuesInDatabase($address_values), $message);
  }
}