You are here

public function OrderEventsTransactionsTest::testWorkflowCancelEvent in Commerce Stock 8

Whether proper transactions are created on cancel transition with config set to act on order cancel.

File

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

Class

OrderEventsTransactionsTest
Ensure the stock transactions are performed on order events.

Namespace

Drupal\Tests\commerce_stock_local\Kernel

Code

public function testWorkflowCancelEvent() {
  $config = \Drupal::configFactory()
    ->getEditable('commerce_stock.core_stock_events');
  $config
    ->set('core_stock_events_order_cancel', TRUE);
  $config
    ->save();
  $this
    ->assertEquals(10, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $transition = $this->order
    ->getState()
    ->getTransitions();
  $this->order
    ->setOrderNumber('2017/01');
  $this->order
    ->getState()
    ->applyTransition($transition['place']);
  $this->order
    ->save();
  $this
    ->assertEquals(9, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
  $this->order
    ->getState()
    ->applyTransition($transition['cancel']);
  $this->order
    ->save();
  $this
    ->assertEquals($this->order
    ->getState()
    ->getLabel(), 'Canceled');
  $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($this->order
    ->id(), $result[0]->related_oid);
  $this
    ->assertEquals($this->order
    ->getCustomerId(), $result[0]->related_uid);
  $this
    ->assertEquals('1.00', $result[0]->qty);
  $this
    ->assertEquals('Order canceled.', unserialize($result[0]->data)['message']);
  $this
    ->assertEquals(10, $this->checker
    ->getTotalStockLevel($this->variation, $this->locations));
}