You are here

public function CommerceBaseTestCase::assertProductAddedToCart in Commerce Core 7

Asserts that a product has been added to the cart.

Parameters

$order: A full loaded commerce_order object.

$product: A full loaded commerce_product object.

$user: User that owns the cart.

Return value

TRUE if the product is in the cart, FALSE otherwise.

File

tests/commerce_base.test, line 677
Defines abstract base test class for the Commerce module tests.

Class

CommerceBaseTestCase
Abstract class for Commerce testing. All Commerce tests should extend this class.

Code

public function assertProductAddedToCart($order, $product, $user = NULL) {

  // The order should be in cart status.
  $this
    ->assertTrue(commerce_cart_order_is_cart($order), t('The order checked is in cart status'));
  $product_is_in_cart = FALSE;

  // Loop through the line items looking for products.
  foreach (entity_metadata_wrapper('commerce_order', $order)->commerce_line_items as $delta => $line_item_wrapper) {

    // If this line item matches the product checked...
    if ($line_item_wrapper
      ->getBundle() == 'product' && $line_item_wrapper->commerce_product->product_id
      ->value() == $product->product_id) {
      $product_is_in_cart = TRUE;
    }
  }
  $this
    ->assertTrue($product_is_in_cart, t('Product !product_title is present in the cart', array(
    '!product_title' => $product->title,
  )));

  // Access to the cart page to check if the product is there.
  if (empty($user)) {
    $user = $this
      ->createStoreCustomer();
  }
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet($this
    ->getCommerceUrl('cart'));
  $this
    ->assertText($product->title, t('Product !product_title is present in the cart view', array(
    '!product_title' => $product->title,
  )));
}