You are here

function UcAddressesCartCheckoutTestCase::testCartOrderTimeout in Ubercart Addresses 7

Override of UbercartCartCheckoutTestCase::testCartOrderTimeout().

With Ubercart Addresses, address fields on checkout have a bit different name. Example: Instead of "panes[delivery][delivery_first_name]", Ubercart Addresses uses "panes[delivery][address][delivery_first_name]". This is done to fix issues with the zone field.

Overrides UbercartCartCheckoutTestCase::testCartOrderTimeout

File

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

Class

UcAddressesCartCheckoutTestCase
Test cases for checkout.

Code

function testCartOrderTimeout() {
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  $this
    ->drupalPost('cart', array(), 'Checkout');
  $this
    ->assertText(t('Enter your billing address and information here.'), t('Viewed cart page: Billing pane has been displayed.'));

  // Build the panes.
  $zone_id = db_query_range('SELECT zone_id FROM {uc_zones} WHERE zone_country_id = :country ORDER BY rand()', 0, 1, array(
    'country' => variable_get('uc_store_country', 840),
  ))
    ->fetchField();
  $oldname = $this
    ->randomName(10);
  $edit = array(
    'panes[delivery][address][delivery_first_name]' => $oldname,
    'panes[delivery][address][delivery_last_name]' => $this
      ->randomName(10),
    'panes[delivery][address][delivery_street1]' => $this
      ->randomName(10),
    'panes[delivery][address][delivery_city]' => $this
      ->randomName(10),
    'panes[delivery][address][delivery_zone]' => $zone_id,
    'panes[delivery][address][delivery_postal_code]' => mt_rand(10000, 99999),
    'panes[billing][address][billing_first_name]' => $this
      ->randomName(10),
    'panes[billing][address][billing_last_name]' => $this
      ->randomName(10),
    'panes[billing][address][billing_street1]' => $this
      ->randomName(10),
    'panes[billing][address][billing_city]' => $this
      ->randomName(10),
    'panes[billing][address][billing_zone]' => $zone_id,
    'panes[billing][address][billing_postal_code]' => mt_rand(10000, 99999),
  );

  // If the email address has not been set, and the user has not logged in,
  // add a primary email address.
  if (!isset($edit['panes[customer][primary_email]']) && !$this->loggedInUser) {
    $edit['panes[customer][primary_email]'] = $this
      ->randomName(8) . '@example.com';
  }

  // Submit the checkout page.
  $this
    ->drupalPost('cart/checkout', $edit, t('Review order'));
  $order_id = db_query("SELECT order_id FROM {uc_orders} WHERE delivery_first_name = :name", array(
    ':name' => $oldname,
  ))
    ->fetchField();
  if ($order_id) {

    // Go to a different page, then back to order - make sure order_id is the same.
    $this
      ->drupalGet('<front>');
    $this
      ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
    $this
      ->drupalPost('cart', array(), 'Checkout');
    $this
      ->assertRaw($oldname, 'Customer name was unchanged.');
    $this
      ->drupalPost('cart/checkout', $edit, t('Review order'));
    $new_order_id = db_query("SELECT order_id FROM {uc_orders} WHERE delivery_first_name = :name", array(
      ':name' => $edit['panes[delivery][address][delivery_first_name]'],
    ))
      ->fetchField();
    $this
      ->assertEqual($order_id, $new_order_id, 'Original order_id was reused.');

    // Jump 10 minutes into the future.
    db_update('uc_orders')
      ->fields(array(
      'modified' => time() - UC_CART_ORDER_TIMEOUT - 1,
    ))
      ->condition('order_id', $order_id)
      ->execute();
    $old_order = uc_order_load($order_id);

    // Go to a different page, then back to order - verify that we are using a new order.
    $this
      ->drupalGet('<front>');
    $this
      ->drupalPost('cart', array(), 'Checkout');
    $this
      ->assertNoRaw($oldname, 'Customer name was cleared after timeout.');
    $newname = $this
      ->randomName(10);
    $edit['panes[delivery][address][delivery_first_name]'] = $newname;
    $this
      ->drupalPost('cart/checkout', $edit, t('Review order'));
    $new_order_id = db_query("SELECT order_id FROM {uc_orders} WHERE delivery_first_name = :name", array(
      ':name' => $newname,
    ))
      ->fetchField();
    $this
      ->assertNotEqual($order_id, $new_order_id, 'New order was created after timeout.');

    // Verify that the status of old order is abandoned.
    $old_order = uc_order_load($order_id, TRUE);
    $this
      ->assertEqual($old_order->order_status, 'abandoned', 'Original order was marked abandoned.');
  }
  else {
    $this
      ->fail('No order was created.');
  }
}