View source
<?php
namespace Drupal\Tests\commerce_cart\Kernel;
use Drupal\commerce_cart\Exception\DuplicateCartException;
use Drupal\commerce_order\Entity\OrderInterface;
class CartProviderTest extends CartKernelTestBase {
protected $entityTypeManager;
protected $anonymousUser;
protected $authenticatedUser;
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->anonymousUser = $this
->createUser([
'uid' => 0,
'name' => '',
'status' => 0,
]);
$this->authenticatedUser = $this
->createUser();
}
public function testCreateAnonymousCart() {
$order_type = 'default';
$cart = $this->cartProvider
->createCart($order_type, $this->store, $this->anonymousUser);
$this
->assertInstanceOf(OrderInterface::class, $cart);
$this
->expectException(DuplicateCartException::class);
$this->cartProvider
->createCart($order_type, $this->store, $this->anonymousUser);
}
public function testGetAnonymousCart() {
$this->cartProvider
->createCart('default', $this->store, $this->anonymousUser);
$cart = $this->cartProvider
->getCart('default', $this->store, $this->anonymousUser);
$this
->assertInstanceOf(OrderInterface::class, $cart);
$cart_id = $this->cartProvider
->getCartId('default', $this->store, $this->anonymousUser);
$this
->assertEquals(1, $cart_id);
$carts = $this->cartProvider
->getCarts($this->anonymousUser);
$this
->assertContainsOnlyInstancesOf(OrderInterface::class, $carts);
$this
->assertContains(1, $this->cartProvider
->getCartIds($this->anonymousUser, $this->store));
$this
->assertContains(1, $this->cartProvider
->getCartIds($this->anonymousUser));
$another_store = $this
->createStore();
$another_cart = $this->cartProvider
->createCart('default', $another_store, $this->anonymousUser);
$another_cart = $this
->reloadEntity($another_cart);
$this
->assertInstanceOf(OrderInterface::class, $another_cart);
$this
->assertEquals($another_cart
->id(), $this->cartProvider
->getCartId('default', $another_store, $this->anonymousUser));
$carts = $this->cartProvider
->getCarts($this->anonymousUser, $another_store);
$this
->assertEquals($another_cart, reset($carts));
$this
->assertContains($another_cart
->id(), $this->cartProvider
->getCartIds($this->anonymousUser, $another_store));
$this
->assertCount(2, $this->cartProvider
->getCartIds($this->anonymousUser));
}
public function testCreateAuthenticatedCart() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$this
->assertInstanceOf(OrderInterface::class, $cart);
$this
->expectException(DuplicateCartException::class);
$this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
}
public function testGetAuthenticatedCart() {
$this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$cart = $this->cartProvider
->getCart('default', $this->store, $this->authenticatedUser);
$this
->assertInstanceOf(OrderInterface::class, $cart);
$cart_id = $this->cartProvider
->getCartId('default', $this->store, $this->authenticatedUser);
$this
->assertEquals(1, $cart_id);
$carts = $this->cartProvider
->getCarts($this->authenticatedUser);
$this
->assertContainsOnlyInstancesOf(OrderInterface::class, $carts);
$this
->assertContains(1, $this->cartProvider
->getCartIds($this->authenticatedUser, $this->store));
$this
->assertContains(1, $this->cartProvider
->getCartIds($this->authenticatedUser));
$another_store = $this
->createStore();
$another_cart = $this->cartProvider
->createCart('default', $another_store, $this->authenticatedUser);
$another_cart = $this
->reloadEntity($another_cart);
$this
->assertInstanceOf(OrderInterface::class, $another_cart);
$this
->assertEquals($another_cart
->id(), $this->cartProvider
->getCartId('default', $another_store, $this->authenticatedUser));
$carts = $this->cartProvider
->getCarts($this->authenticatedUser, $another_store);
$this
->assertEquals($another_cart, reset($carts));
$this
->assertContains($another_cart
->id(), $this->cartProvider
->getCartIds($this->authenticatedUser, $another_store));
$this
->assertCount(2, $this->cartProvider
->getCartIds($this->authenticatedUser));
}
public function testFinalizeCart() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$this->cartProvider
->finalizeCart($cart);
$cart = $this
->reloadEntity($cart);
$this
->assertEmpty($cart->cart->value);
$cart = $this->cartProvider
->getCart('default', $this->store, $this->authenticatedUser);
$this
->assertNull($cart);
}
public function testCartValidation() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$cart
->lock();
$cart
->save();
$this->cartProvider
->clearCaches();
$cart = $this->cartProvider
->getCart('default', $this->store, $this->authenticatedUser);
$this
->assertNull($cart);
$cart = $this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$cart->cart = FALSE;
$cart
->save();
$this->cartProvider
->clearCaches();
$cart = $this->cartProvider
->getCart('default', $this->store, $this->authenticatedUser);
$this
->assertNull($cart);
$cart = $this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$cart->uid = $this->anonymousUser
->id();
$cart
->save();
$this->cartProvider
->clearCaches();
$cart = $this->cartProvider
->getCart('default', $this->store, $this->authenticatedUser);
$this
->assertNull($cart);
$cart = $this->cartProvider
->createCart('default', $this->store, $this->authenticatedUser);
$cart->state = 'canceled';
$cart
->save();
$this->cartProvider
->clearCaches();
$cart = $this->cartProvider
->getCart('default', $this->store, $this->authenticatedUser);
$this
->assertNull($cart);
}
}