protected function OrderEventsTransactionsTest::setUp in Commerce Stock 8
Overrides CommerceKernelTestBase::setUp
File
- modules/
local_storage/ tests/ src/ Kernel/ OrderEventsTransactionsTest.php, line 113
Class
- OrderEventsTransactionsTest
- Ensure the stock transactions are performed on order events.
Namespace
Drupal\Tests\commerce_stock_local\KernelCode
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('commerce_product_variation');
$this
->installEntitySchema('commerce_product_variation_type');
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_type');
$this
->installConfig([
'commerce_product',
]);
$this
->installEntitySchema('profile');
$this
->installEntitySchema('commerce_order');
$this
->installEntitySchema('commerce_order_item');
$this
->installConfig('commerce_order');
$this
->installEntitySchema('commerce_stock_location_type');
$this
->installEntitySchema('commerce_stock_location');
$this
->installConfig([
'commerce_stock',
]);
$this
->installConfig([
'commerce_stock_local',
]);
$this
->installConfig([
'commerce_number_pattern',
]);
$this
->installSchema('commerce_stock_local', [
'commerce_stock_transaction_type',
'commerce_stock_transaction',
'commerce_stock_location_level',
]);
$this
->installSchema('commerce_number_pattern', [
'commerce_number_pattern_sequence',
]);
// Change the workflow of the default order type.
$order_type = OrderType::load('default');
$order_type
->setWorkflowId('order_fulfillment_validation');
$order_type
->save();
$defaultStockLocation = StockLocation::create([
'name' => 'Test',
'status' => 1,
'type' => "default",
]);
$defaultStockLocation
->save();
$user = $this
->createUser();
$user = $this
->reloadEntity($user);
$this->user = $user;
$config = \Drupal::configFactory()
->getEditable('commerce_stock.service_manager');
$config
->set('default_service_id', 'local_stock');
$config
->save();
$stockServiceManager = \Drupal::service('commerce_stock.service_manager');
// Turn off title generation to allow explicit values to be used.
$variation_type = ProductVariationType::load('default');
$variation_type
->setGenerateTitle(FALSE);
$variation_type
->save();
$this->variation = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => 1,
'price' => [
'number' => '11.11',
'currency_code' => 'USD',
],
]);
$this->variation
->save();
$this->variation = $this
->reloadEntity($this->variation);
$this->variation2 = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => 1,
'price' => [
'number' => '12.12',
'currency_code' => 'USD',
],
]);
$this->variation2
->save();
$this->variation2 = $this
->reloadEntity($this->variation2);
$this->product = Product::create([
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$this->variation,
$this->variation2,
],
]);
$this->product
->save();
$this->checker = $stockServiceManager
->getService($this->variation)
->getStockChecker();
$this->checker2 = $stockServiceManager
->getService($this->variation2)
->getStockChecker();
$stockServiceConfiguration = $stockServiceManager
->getService($this->variation)
->getConfiguration();
$stockServiceConfiguration2 = $stockServiceManager
->getService($this->variation2)
->getConfiguration();
$context = new Context($user, $this->store);
$this->locations = $stockServiceConfiguration
->getAvailabilityLocations($context, $this->variation);
$this->locations2 = $stockServiceConfiguration2
->getAvailabilityLocations($context, $this->variation2);
// Set initial Stock level.
$stockServiceManager
->createTransaction($this->variation, $this->locations[1]
->getId(), '', 10, 10.1, 'USD', StockTransactionsInterface::STOCK_IN, []);
$stockServiceManager
->createTransaction($this->variation2, $this->locations2[1]
->getId(), '', 11, 11.11, 'USD', StockTransactionsInterface::STOCK_IN, []);
$profile = Profile::create([
'type' => 'customer',
'uid' => $user
->id(),
]);
$profile
->save();
/** @var \Drupal\commerce_order\Entity\Order $order */
$order = Order::create([
'type' => 'default',
'state' => 'draft',
'mail' => $user
->getEmail(),
'uid' => $user
->id(),
'ip_address' => '127.0.0.1',
'order_number' => '6',
'billing_profile' => $profile,
'store_id' => $this->store
->id(),
]);
$order
->save();
$this->order = $this
->reloadEntity($order);
/** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
$order_item_storage = $this->container
->get('entity_type.manager')
->getStorage('commerce_order_item');
$order_item2 = OrderItem::create([
'type' => 'default',
'quantity' => 2,
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item2
->save();
// Add order item.
$order_item1 = $order_item_storage
->createFromPurchasableEntity($this->variation);
$order_item1
->save();
$order_item1 = $this
->reloadEntity($order_item1);
$order
->addItem($order_item1);
$order
->addItem($order_item2);
$order
->save();
$this->order = $this
->reloadEntity($order);
}