You are here

public function OrderEventsTransactionsTest::testDisableConfigurationPreventsTransaktions in Commerce Stock 8

Test configuration.

Tests that no transactions are triggered for all other order and order item events in case we disabled all configuration options.

File

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

Class

OrderEventsTransactionsTest
Ensure the stock transactions are performed on order events.

Namespace

Drupal\Tests\commerce_stock_local\Kernel

Code

public function testDisableConfigurationPreventsTransaktions() {

  // Tests the order item creation event.
  $this
    ->assertEquals(11, $this->checker2
    ->getTotalStockLevel($this->variation2, $this->locations2));
  $this
    ->assertEquals(10, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $config = \Drupal::configFactory()
    ->getEditable('commerce_stock.core_stock_events');
  $config
    ->set('core_stock_events_order_updates', FALSE);
  $config
    ->set('core_stock_events_order_cancel', FALSE);
  $config
    ->set('core_stock_events_order_complete', FALSE);
  $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(10, $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');

  // Adding new order item to order do not trigger a transaction.
  $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
    ->assertCount(0, $result);
  $this
    ->assertEquals(11, $this->checker
    ->getTotalStockLevel($this->variation2, $this->locations2));

  // 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(0, $result);
  $this
    ->assertEquals(10, $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(0, $result);

  // 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(0, $result);
}