You are here

public function OrderEventsTransactionsTest::testOrderEditEvents in Commerce Stock 8

Test order edit events.

Tests that transactions are created for all other order and order item events.

File

modules/local_storage/tests/src/Kernel/OrderEventsTransactionsTest.php, line 451

Class

OrderEventsTransactionsTest
Ensure the stock transactions are performed on order events.

Namespace

Drupal\Tests\commerce_stock_local\Kernel

Code

public function testOrderEditEvents() {

  // Tests the order item creation event.
  $this
    ->assertEquals(11, $this->checker2
    ->getTotalStockLevel($this->variation2, $this->locations2));
  $config = \Drupal::configFactory()
    ->getEditable('commerce_stock.core_stock_events');
  $config
    ->set('core_stock_events_order_updates', TRUE);
  $config
    ->set('core_stock_events_order_cancel', TRUE);
  $config
    ->set('core_stock_events_order_complete', TRUE);
  $config
    ->save();
  $transition = $this->order
    ->getState()
    ->getTransitions();
  $this->order
    ->getState()
    ->applyTransition($transition['place']);
  $this->order
    ->save();

  // Ensure all setup is done and we have the stock level we expect here.
  $this
    ->assertEquals(9, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $this
    ->assertEquals(11, $this->checker2
    ->getTotalStockLevel($this->variation2, $this->locations2));

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

  // Add new order item to order.
  $order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variation2, [
    'quantity' => 3,
  ]);
  $order_item
    ->save();
  $order_item = $this
    ->reloadEntity($order_item);
  $this->order
    ->addItem($order_item);
  $this->order
    ->save();
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('entity_id', $this->variation2
    ->id())
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_SALE);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertEquals(8, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));
  $this
    ->assertCount(1, $result);
  $this
    ->assertEquals('4', $result[0]->id);
  $this
    ->assertEquals($this->variation2
    ->id(), $result[0]->entity_id);
  $this
    ->assertEquals($this->variation2
    ->getEntityTypeId(), $result[0]->entity_type);
  $this
    ->assertEquals($this->order
    ->id(), $result[0]->related_oid);
  $this
    ->assertEquals($this->order
    ->getCustomerId(), $result[0]->related_uid);
  $this
    ->assertEquals('-3.00', $result[0]->qty);
  $this
    ->assertEquals('Order updated: new order item added.', unserialize($result[0]->data)['message']);

  // Tests the order item update event.
  $items = $this->order
    ->getItems();
  $items[0]
    ->setQuantity('3')
    ->save();
  $this->order
    ->save();
  $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(2, $result);
  $this
    ->assertEquals('5', $result[1]->id);
  $this
    ->assertEquals($this->variation
    ->id(), $result[1]->entity_id);
  $this
    ->assertEquals($this->order
    ->id(), $result[1]->related_oid);
  $this
    ->assertEquals($this->order
    ->getCustomerId(), $result[1]->related_uid);
  $this
    ->assertEquals('-2.00', $result[1]->qty);
  $this
    ->assertEquals('Order item updated.', unserialize($result[1]->data)['message']);
  $this
    ->assertEquals(7, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));

  // Tests the order item delete event.
  $items = $this->order
    ->getItems();
  $this->order
    ->removeItem($items[0])
    ->save();
  $items[0]
    ->delete();
  $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('6', $result[0]->id);
  $this
    ->assertEquals($this->variation
    ->id(), $result[0]->entity_id);
  $this
    ->assertEquals($this->order
    ->id(), $result[0]->related_oid);
  $this
    ->assertEquals($this->order
    ->getCustomerId(), $result[0]->related_uid);
  $this
    ->assertEquals('3.00', $result[0]->qty);
  $this
    ->assertEquals('Order item deleted.', unserialize($result[0]->data)['message']);
  $this
    ->assertEquals(10, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));

  // Tests the order delete event.
  $this->order
    ->delete();
  $query = \Drupal::database()
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn')
    ->condition('entity_id', $this->variation2
    ->id())
    ->condition('transaction_type_id', StockTransactionsInterface::STOCK_RETURN);
  $result = $query
    ->execute()
    ->fetchAll();
  $this
    ->assertCount(1, $result);
  $this
    ->assertEquals('7', $result[0]->id);
  $this
    ->assertEquals($this->variation2
    ->id(), $result[0]->entity_id);
  $this
    ->assertEquals($this->order
    ->id(), $result[0]->related_oid);
  $this
    ->assertEquals($this->order
    ->getCustomerId(), $result[0]->related_uid);
  $this
    ->assertEquals('3.00', $result[0]->qty);
  $this
    ->assertEquals('Order deleted.', unserialize($result[0]->data)['message']);
  $this
    ->assertEquals(10, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $this
    ->assertEquals(11, $this->checker2
    ->getTotalStockLevel($this->variation2, $this->locations2));
}