View source
<?php
namespace Drupal\Tests\commerce_log\Kernel;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Tests\commerce_cart\Kernel\CartKernelTestBase;
class CartIntegrationTest extends CartKernelTestBase {
protected $user;
protected $variation;
protected $logStorage;
protected $logViewBuilder;
public static $modules = [
'commerce_log',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_log');
$this->user = $this
->createUser();
$this->logStorage = $this->container
->get('entity_type.manager')
->getStorage('commerce_log');
$this->logViewBuilder = $this->container
->get('entity_type.manager')
->getViewBuilder('commerce_log');
$variation_type = ProductVariationType::load('default');
$variation_type
->setGenerateTitle(FALSE);
$variation_type
->save();
$this->variation = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this
->randomMachineName()),
'title' => 'Testing product',
'status' => 1,
'price' => new Price('12.00', 'USD'),
]);
}
public function testAddedToCart() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->user);
$this->cartManager
->addEntity($cart, $this->variation);
$logs = $this->logStorage
->loadMultipleByEntity($cart);
$this
->assertEquals(1, count($logs));
$log = reset($logs);
$build = $this->logViewBuilder
->view($log);
$this
->render($build);
$this
->assertText("{$this->variation->label()} added to the cart.");
}
public function testAddedToCartNoPurchasableEntity() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->user);
$order_item = OrderItem::create([
'title' => 'Membership subscription',
'type' => 'test',
'quantity' => 1,
'unit_price' => [
'number' => '10.00',
'currency_code' => 'USD',
],
]);
$order_item
->save();
$this->cartManager
->addOrderItem($cart, $order_item);
$logs = $this->logStorage
->loadMultipleByEntity($cart);
$this
->assertEquals(0, count($logs));
}
public function testRemovedFromCart() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->user);
$order_item = $this->cartManager
->addEntity($cart, $this->variation);
$this->cartManager
->removeOrderItem($cart, $order_item);
$logs = $this->logStorage
->loadMultipleByEntity($cart);
$this
->assertEquals(2, count($logs));
$log = end($logs);
$build = $this->logViewBuilder
->view($log);
$this
->render($build);
$this
->assertText("{$this->variation->label()} removed from the cart.");
}
public function testRemovedFromCartNoPurchasableEntity() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->user);
$order_item = OrderItem::create([
'title' => 'Membership subscription',
'type' => 'test',
'quantity' => 1,
'unit_price' => [
'number' => '10.00',
'currency_code' => 'USD',
],
]);
$order_item
->save();
$order_item = $this->cartManager
->addOrderItem($cart, $order_item);
$this->cartManager
->removeOrderItem($cart, $order_item);
$logs = $this->logStorage
->loadMultipleByEntity($cart);
$this
->assertEquals(1, count($logs));
$log = end($logs);
$build = $this->logViewBuilder
->view($log);
$this
->render($build);
$this
->assertText("{$order_item->label()} removed from the cart.");
}
}