You are here

public function CommerceCartTestCaseAnonymousToAuthenticated::testCommerceCartAnonymousToAuthenticated in Commerce Core 7

Test anonymous cart conversion.

File

modules/cart/tests/commerce_cart.test, line 502
Functional tests for the commerce cart module.

Class

CommerceCartTestCaseAnonymousToAuthenticated
Test cart conversion from anonymous to authenticated.

Code

public function testCommerceCartAnonymousToAuthenticated() {

  // Logout to be anonymous and force user uid.
  $this
    ->drupalLogout();
  global $user;
  $user = user_load(0);

  // Submit the add to cart form.
  $this
    ->drupalPost('node/' . $this->product_node->nid, array(), t('Add to cart'));

  // Get the order just created.
  $orders = commerce_order_load_multiple(array(), array(
    'uid' => $user->uid,
    'status' => 'cart',
  ), TRUE);
  $order_anonymous = reset($orders);

  // Reset the cache as we don't want to keep the lock.
  entity_get_controller('commerce_order')
    ->resetCache();

  // Access to the cart and check if the product is in it.
  $this
    ->drupalGet($this
    ->getCommerceUrl('cart'));
  $this
    ->assertNoText(t('Your shopping cart is empty.'), t('Cart is not empty'));
  $this
    ->assertText($this->product->title, t('Product was added to the cart'));

  // Change the price to check if the amount gets updated when the user logs
  // in.
  $new_price = $this->product->commerce_price[LANGUAGE_NONE][0]['amount'] + rand(2, 500);
  $this->product->commerce_price[LANGUAGE_NONE][0]['amount'] = $new_price;
  $this->product->is_new = FALSE;
  commerce_product_save($this->product);

  // Log in with normal user.
  $this
    ->drupalPost('user', array(
    'name' => $this->store_customer->name,
    'pass' => $this->store_customer->pass_raw,
  ), t('Log in'));

  // Get the order for user just logged in.
  $orders = commerce_order_load_multiple(array(), array(
    'uid' => $this->store_customer->uid,
    'status' => 'cart',
  ), TRUE);
  $order_authenticated = reset($orders);

  // Reset the cache as we don't want to keep the lock.
  entity_get_controller('commerce_order')
    ->resetCache();

  // Access to the cart and check if the product is in it.
  $this
    ->drupalGet($this
    ->getCommerceUrl('cart'));
  $this
    ->assertNoText(t('Your shopping cart is empty.'), t('Cart is not empty'));
  $this
    ->assertText($this->product->title, t('Product is still in the cart'));
  $this
    ->assertText(trim(commerce_currency_amount_to_decimal($this->product->commerce_price[LANGUAGE_NONE][0]['amount'], $this->product->commerce_price[LANGUAGE_NONE][0]['currency_code'])), t('Product price has been updated'));

  // Check if the order is the same.
  $this
    ->assertTrue($order_anonymous->order_id == $order_authenticated->order_id, t('Cart has been converted successfully'));
}