You are here

public function OrderEventTransactionsKernelTest::testCancelTransitionDontFireInDraftState in Commerce Stock 8

Whether the order cancel transition dont return stock if the order is in 'draft' state. That would result in wrong stock levels.

@covers ::onOrderCancel

File

modules/local_storage/tests/src/Kernel/OrderEventTransactionsKernelTest.php, line 446

Class

OrderEventTransactionsKernelTest
Ensure the stock transactions are performed on order events.

Namespace

Drupal\Tests\commerce_stock_local\Kernel

Code

public function testCancelTransitionDontFireInDraftState() {

  /** @var \Drupal\commerce_order\Entity\Order $order */
  $order = Order::create([
    'type' => 'default',
    'state' => 'draft',
    'mail' => $this->user
      ->getEmail(),
    'uid' => $this->user
      ->id(),
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'billing_profile' => $this->profile,
    'store_id' => $this->store
      ->id(),
  ]);

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_order_item');

  // Add order item.
  $order_item1 = $order_item_storage
    ->createFromPurchasableEntity($this->variation);
  $order_item1
    ->setQuantity('44');
  $order_item1
    ->save();
  $order
    ->addItem($order_item1);
  $order
    ->save();
  $transition = $order
    ->getState()
    ->getTransitions();
  $order
    ->getState()
    ->applyTransition($transition['cancel']);
  $order
    ->save();
  $this
    ->assertEquals(100, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('entity_id', $this->variation
    ->id())
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_RETURN);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(0, $result);
}