You are here

public function AddToCartFormTest::testCartAssignment in Commerce Core 8.2

Test assigning an anonymous cart to a logged in user.

File

modules/cart/tests/src/Functional/AddToCartFormTest.php, line 66

Class

AddToCartFormTest
Tests the add to cart form.

Namespace

Drupal\Tests\commerce_cart\Functional

Code

public function testCartAssignment() {
  $this
    ->drupalLogout();
  $this
    ->postAddToCart($this->variation
    ->getProduct());

  // Find the newly created anonymous cart.
  $query = \Drupal::entityTypeManager()
    ->getStorage('commerce_order')
    ->getQuery()
    ->condition('cart', TRUE)
    ->condition('uid', 0)
    ->accessCheck(FALSE);
  $result = $query
    ->execute();
  $cart_id = reset($result);
  $cart = Order::load($cart_id);
  $this
    ->assertEquals(0, $cart
    ->getCustomerId());
  $this
    ->assertNotEmpty($cart
    ->hasItems());
  $this
    ->drupalLogin($this->adminUser);
  \Drupal::entityTypeManager()
    ->getStorage('commerce_order')
    ->resetCache();
  $cart = Order::load($cart
    ->id());
  $this
    ->assertEquals($this->adminUser
    ->id(), $cart
    ->getCustomerId());
}