You are here

public function UbercartCartCheckoutTestCase::testCartApi in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 uc_cart/uc_cart.test \UbercartCartCheckoutTestCase::testCartAPI()

Tests cart API.

File

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

Class

UbercartCartCheckoutTestCase
Tests the cart and checkout functionality.

Code

public 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);
  uc_cart_empty();
  $items = uc_cart_get_contents();
  $this
    ->assertEqual($items, array(), 'Cart is emptied correctly.');
}