You are here

public function UbercartCartCheckoutTestCase::testCartOrderTimeout in Ubercart 7.3

Tests that cart orders are marked abandoned after a timeout.

File

uc_cart/tests/uc_cart.test, line 505
Shopping cart and checkout tests.

Class

UbercartCartCheckoutTestCase
Tests the cart and checkout functionality.

Code

public 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][delivery_first_name]' => $oldname,
    'panes[delivery][delivery_last_name]' => $this
      ->randomName(10),
    'panes[delivery][delivery_street1]' => $this
      ->randomName(10),
    'panes[delivery][delivery_city]' => $this
      ->randomName(10),
    'panes[delivery][delivery_zone]' => $zone_id,
    'panes[delivery][delivery_postal_code]' => mt_rand(10000, 99999),
    'panes[billing][billing_first_name]' => $this
      ->randomName(10),
    'panes[billing][billing_last_name]' => $this
      ->randomName(10),
    'panes[billing][billing_street1]' => $this
      ->randomName(10),
    'panes[billing][billing_city]' => $this
      ->randomName(10),
    'panes[billing][billing_zone]' => $zone_id,
    'panes[billing][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 to 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][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 to 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][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.');
  }
}