You are here

public function OrderEventTransactionsKernelTest::testOrderItemEvents in Commerce Stock 8

Whether order item modifications resulting in proper stock transactions.

@covers ::onOrderUpdate @covers ::onOrderItemUpdate @covers ::onOrderItemDelete @covers ::onOrderDelete

File

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

Class

OrderEventTransactionsKernelTest
Ensure the stock transactions are performed on order events.

Namespace

Drupal\Tests\commerce_stock_local\Kernel

Code

public function testOrderItemEvents() {

  // 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();
  $order_item1 = $this
    ->reloadEntity($order_item1);
  $transition = $order
    ->getState()
    ->getTransitions();
  $order
    ->getState()
    ->applyTransition($transition['place']);
  $order
    ->save();
  $order_item2 = $order_item_storage
    ->createFromPurchasableEntity($this->variation2);
  $order_item2
    ->setQuantity('22');
  $order_item2
    ->save();
  $order
    ->addItem($order_item2);
  $order
    ->save();
  $order_item2 = $this
    ->reloadEntity($order_item2);
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn');
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(4, $result);
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_SALE);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(2, $result);
  $this
    ->assertEquals('4', $result[1]->id);
  $this
    ->assertEquals($this->variation2
    ->id(), $result[1]->entity_id);
  $this
    ->assertEquals('-22.00', $result[1]->qty);
  $this
    ->assertNotEmpty(unserialize($result[1]->data)['message']);
  $this
    ->assertEquals(178, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));

  // Whether changing the qty triggers the stock transaction.
  $order_item2
    ->setQuantity('33');
  $order_item2
    ->save();
  $order
    ->save();
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_SALE);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(3, $result);
  $this
    ->assertEquals('5', $result[2]->id);
  $this
    ->assertEquals($this->variation2
    ->id(), $result[2]->entity_id);
  $this
    ->assertEquals('-11.00', $result[2]->qty);
  $this
    ->assertNotEmpty(unserialize($result[2]->data)['message']);
  $this
    ->assertEquals(167, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));

  // Whether changing the qty triggers the stock transaction.
  $order_item2
    ->setQuantity('22');
  $order_item2
    ->save();
  $order
    ->save();
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_RETURN);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(1, $result);
  $this
    ->assertEquals('6', $result[0]->id);
  $this
    ->assertEquals($this->variation2
    ->id(), $result[0]->entity_id);
  $this
    ->assertEquals('11.00', $result[0]->qty);
  $this
    ->assertNotEmpty(unserialize($result[0]->data)['message']);
  $this
    ->assertEquals(178, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));

  // Whether removing one item from order results in the proper transaction.
  $order
    ->removeItem($order_item1);
  $order_item1
    ->delete();
  $order
    ->save();
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_RETURN);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(2, $result);
  $this
    ->assertEquals('7', $result[1]->id);
  $this
    ->assertEquals($this->variation
    ->id(), $result[1]->entity_id);
  $this
    ->assertEquals('44.00', $result[1]->qty);
  $this
    ->assertNotEmpty(unserialize($result[1]->data)['message']);
  $this
    ->assertEquals(100, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $this
    ->assertEquals(178, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));

  // Whether deleting the order triggers stock transactions.
  $order
    ->delete();
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_RETURN);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(3, $result);
  $this
    ->assertEquals(200, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));
  $this
    ->assertNotEmpty(unserialize($result[2]->data)['message']);
}