You are here

public function OrderEventTransactionsKernelTest::testOrderEvents in Commerce Stock 8

Whether transitions and order deletion resulting in proper stock transactions.

@covers ::onOrderPlace @covers ::onOrderCancel

File

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

Class

OrderEventTransactionsKernelTest
Ensure the stock transactions are performed on order events.

Namespace

Drupal\Tests\commerce_stock_local\Kernel

Code

public function testOrderEvents() {

  // Change the workflow of the default order type.
  $order_type = OrderType::load('default');
  $order_type
    ->setWorkflowId('order_fulfillment_validation');
  $order_type
    ->save();

  /** @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();

  // We react on workflow transition event. Just adding something to an order
  // in 'draft' state shouldn't result in stock transactions.
  $this
    ->assertEquals(100, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));

  // Whether setting the order state to 'place' triggers stock transaction.
  $transition = $order
    ->getState()
    ->getTransitions();
  $order
    ->getState()
    ->applyTransition($transition['place']);
  $order
    ->save();
  $this
    ->assertEquals(56, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn');
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(3, $result);
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('entity_id', $this->variation
    ->id())
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_SALE);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(1, $result);
  $this
    ->assertEquals('3', $result[0]->id);
  $this
    ->assertEquals($this->variation
    ->id(), $result[0]->entity_id);
  $this
    ->assertEquals($order
    ->id(), $result[0]->related_oid);
  $this
    ->assertEquals($order
    ->getCustomerId(), $result[0]->related_uid);
  $this
    ->assertEquals('-44.00', $result[0]->qty);
  $this
    ->assertNotEmpty(unserialize($result[0]->data)['message']);

  // Whether setting the order state to 'cancel' returns the stock.
  $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(1, $result);
  $this
    ->assertEquals('4', $result[0]->id);
  $this
    ->assertEquals($this->variation
    ->id(), $result[0]->entity_id);
  $this
    ->assertEquals($order
    ->id(), $result[0]->related_oid);
  $this
    ->assertEquals($order
    ->getCustomerId(), $result[0]->related_uid);
  $this
    ->assertEquals('44.00', $result[0]->qty);
  $this
    ->assertNotEmpty(unserialize($result[0]->data)['message']);
}