You are here

protected function UcAddressesCartCheckoutTestCase::doDefaultAddressesTests in Ubercart Addresses 7

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

Checkout with the default addresses.

Helper method for testDefaultAddresses().

See also

testDefaultAddresses()

checkCheckoutAddressFields()

1 call to UcAddressesCartCheckoutTestCase::doDefaultAddressesTests()
UcAddressesCartCheckoutTestCase::testDefaultAddresses in tests/uc_addresses.checkout.test
Test if the address fields are prefilled with the customer's default addresses.

File

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

Class

UcAddressesCartCheckoutTestCase
Test cases for checkout.

Code

protected function doDefaultAddressesTests() {
  global $user;

  // Load default addresses for this user.
  $address_types = uc_addresses_address_types();
  $addresses = array();
  foreach ($address_types as $address_type) {
    $address = UcAddressesAddressBook::get($user->uid)
      ->getDefaultAddress($address_type);
    if ($address instanceof UcAddressesAddress) {
      $addresses[$address_type] = $address;
    }
    else {
      $addresses[$address_type] = UcAddressesAddressBook::newAddress();
    }
  }

  // Create an empty address too.
  $empty_address = UcAddressesAddressBook::newAddress();

  // Initialize values for checkout form.
  $edit = array();

  // Initialize array for newly added addresses.
  $new_addresses = array();

  // Clear out any static variables in drupal_html_id().
  drupal_static_reset('drupal_html_id');

  // Place item in cart.
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));

  // Continue to checkout.
  $this
    ->drupalPost('cart', array(), 'Checkout');

  // Check if the address form is prefilled with the default addresses
  // according to the settings.
  foreach ($address_types as $address_type) {
    $order_address_type = $address_type;
    if ($address_type == 'shipping') {
      $order_address_type = 'delivery';
    }
    if (variable_get('uc_addresses_default_' . $address_type . '_address', TRUE)) {
      $this
        ->checkCheckoutAddressFields($order_address_type, $addresses[$address_type]);
    }
    else {
      $this
        ->checkCheckoutAddressFields($order_address_type, $empty_address);
      $values = UcAddressesTestCase::getEditAddressValues(array(
        'panes',
        $order_address_type,
        'address',
      ), array(), 'checkout_form', $order_address_type . '_');
      $edit += $values['form_values'];
      $new_addresses[$order_address_type] = $values['values'];
    }
  }

  // And checkout.
  $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.
  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);
  }
}