public function testCartAPI() { // Test the empty cart. $items = $this->cart->getContents(); $this->assertEqual($items, [], 'Cart is an empty array.'); // Add an item to the cart. $this->cart->addItem($this->product->id()); $items = $this->cart->getContents(); $this->assertEqual(count($items), 1, 'Cart contains one item.'); $item = reset($items); $this->assertEqual($item->nid->target_id, $this->product->id(), 'Cart item nid is correct.'); $this->assertEqual($item->qty->value, 1, 'Cart item quantity is correct.'); // Add more of the same item. $qty = mt_rand(1, 100); $this->cart->addItem($this->product->id(), $qty); $items = $this->cart->getContents(); $this->assertEqual(count($items), 1, 'Updated cart contains one item.'); $item = reset($items); $this->assertEqual($item->qty->value, $qty + 1, 'Updated cart item quantity is correct.'); // Set the quantity and data. $qty = mt_rand(1, 100); $item->qty->value = $qty; $item->data->updated = TRUE; $item->save(); $items = $this->cart->getContents(); $item = reset($items); $this->assertEqual($item->qty->value, $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. $this->cart->addItem($this->product->id(), 1, array('test' => TRUE)); $items = $this->cart->getContents(); $this->assertEqual(count($items), 2, 'Updated cart contains two items.'); // Remove the items. foreach ($items as $item) { $item->delete(); } $items = $this->cart->getContents(); $this->assertEqual(count($items), 0, 'Cart is empty after removal.'); // Empty the cart. $this->cart->addItem($this->product->id()); $this->cart->emptyCart(); $items = $this->cart->getContents(); $this->assertEqual($items, [], 'Cart is emptied correctly.'); }