You are here

function UbercartCartCheckoutTestCase::testCartAPI in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_cart/tests/uc_cart.test \UbercartCartCheckoutTestCase::testCartApi()

File

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

Class

UbercartCartCheckoutTestCase
Tests the cart and checkout functionality.

Code

function testCartAPI() {

  // Test the empty cart.
  $items = uc_cart_get_contents();
  $this
    ->assertEqual($items, array(), 'Cart is an empty array.');

  // Add an item to the cart.
  uc_cart_add_item($this->product->nid);
  $items = uc_cart_get_contents();
  $this
    ->assertEqual(count($items), 1, 'Cart contains one item.');
  $item = reset($items);
  $this
    ->assertEqual($item->nid, $this->product->nid, 'Cart item nid is correct.');
  $this
    ->assertEqual($item->qty, 1, 'Cart item quantity is correct.');

  // Add more of the same item.
  $qty = mt_rand(1, 100);
  uc_cart_add_item($this->product->nid, $qty);
  $items = uc_cart_get_contents();
  $this
    ->assertEqual(count($items), 1, 'Updated cart contains one item.');
  $item = reset($items);
  $this
    ->assertEqual($item->qty, $qty + 1, 'Updated cart item quantity is correct.');

  // Set the quantity and data.
  $qty = mt_rand(1, 100);
  $item->qty = $qty;
  $item->data['updated'] = TRUE;
  uc_cart_update_item($item);
  $items = uc_cart_get_contents();
  $item = reset($items);
  $this
    ->assertEqual($item->qty, $qty, 'Set cart item quantity is correct.');
  $this
    ->assertTrue($item->data['updated'], 'Set cart item data is correct.');

  // Add an item with different data to the cart.
  uc_cart_add_item($this->product->nid, 1, array(
    'test' => TRUE,
  ));
  $items = uc_cart_get_contents();
  $this
    ->assertEqual(count($items), 2, 'Updated cart contains two items.');

  // Remove the items.
  foreach ($items as $item) {
    uc_cart_remove_item($item->nid, NULL, $item->data);
  }

  // @TODO: remove the need for this
  uc_cart_get_contents(NULL, 'rebuild');
  $items = uc_cart_get_contents();
  $this
    ->assertEqual(count($items), 0, 'Cart is empty after removal.');

  // Empty the cart.
  uc_cart_add_item($this->product->nid);

  // @TODO: default to the current cart
  uc_cart_empty(uc_cart_get_id());
  $items = uc_cart_get_contents();
  $this
    ->assertEqual($items, array(), 'Cart is emptied correctly.');
}