View source
<?php
namespace Drupal\Tests\commerce_stock_field\Kernel;
use Drupal\commerce\Context;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_stock\StockTransactionsInterface;
use Drupal\commerce_stock_field\Plugin\Field\FieldType\StockLevel;
use Drupal\commerce_stock_local\Entity\StockLocation;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Tests\commerce_stock\Kernel\CommerceStockKernelTestBase;
use Drupal\Tests\commerce_stock_local\Kernel\StockTransactionQueryTrait;
class StockLevelTest extends CommerceStockKernelTestBase {
use StockLevelFieldCreationTrait;
use StockTransactionQueryTrait;
public static $modules = [
'path',
'commerce_product',
'commerce_stock_field',
'commerce_stock_local',
];
protected $variation;
protected $stockServiceManager;
protected $checker;
protected $stockServiceConfiguration;
protected $locations;
protected function setUp() {
$this->fieldName = 'test_stock_level';
parent::setUp();
$this
->installEntitySchema('commerce_stock_location');
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->installConfig([
'commerce_product',
'commerce_stock',
'commerce_stock_local',
]);
$this
->installSchema('commerce_stock_local', [
'commerce_stock_transaction',
'commerce_stock_transaction_type',
'commerce_stock_location_level',
]);
$location = StockLocation::create([
'type' => 'default',
'name' => $this
->randomString(),
'status' => 1,
]);
$location
->save();
$configFactory = $this->container
->get('config.factory');
$config = $configFactory
->getEditable('commerce_stock.service_manager');
$config
->set('default_service_id', 'local_stock');
$config
->save();
$this->stockServiceManager = \Drupal::service('commerce_stock.service_manager');
$entity_type = 'commerce_product_variation';
$bundle = 'default';
$widget_id = 'commerce_stock_level_simple';
$this
->createStockLevelField($entity_type, $bundle, $widget_id);
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this
->randomMachineName()),
'status' => 1,
'price' => [
'number' => '12.00',
'currency_code' => 'USD',
],
]);
$variation
->save();
Product::create([
'type' => 'default',
'variations' => [
$variation,
],
'stores' => [
$this->store,
],
])
->save();
$this->variation = $this
->reloadEntity($variation);
$user = $this
->createUser([
'mail' => $this
->randomString() . '@example.com',
]);
$this->stockServiceManager
->createTransaction($this->variation, 1, '', 55, 0, 'EUR', StockTransactionsInterface::STOCK_IN);
$this->checker = $this->stockServiceManager
->getService($this->variation)
->getStockChecker();
$this->stockServiceConfiguration = $this->stockServiceManager
->getService($this->variation)
->getConfiguration();
$context = new Context($user, $this->store);
$this->locations = $this->stockServiceConfiguration
->getAvailabilityLocations($context, $this->variation);
}
public function testBaseFieldisAddedtoPurchasableEntity() {
$variation = ProductVariation::create([
'type' => 'default',
]);
$variation
->save();
$field = $variation
->get('commerce_stock_always_in_stock');
self::assertFalse($field
->getValue()[0]['value']);
}
public function testStockLevelStockIn() {
$this->variation
->set('test_stock_level', 10);
$this->variation
->save();
$this
->assertEquals(65, $this->checker
->getTotalStockLevel($this->variation, $this->locations));
}
public function testStockLevelStockOut() {
$this->variation
->set('test_stock_level', -10);
$this->variation
->save();
$this
->assertEquals(45, $this->checker
->getTotalStockLevel($this->variation, $this->locations));
}
public function testInvalidArgumentThrows() {
$this
->expectException(\InvalidArgumentException::class);
$this->variation
->set('test_stock_level', 'FAIL');
}
public function testTransactionData() {
$test_note = $this
->randomString();
$zone = 'TestZone';
$mock_widget_values = [
'adjustment' => '3.33',
'stock_transaction_note' => $test_note,
'user_id' => 7,
'unit_cost' => [
'amount' => 33,
'currency_code' => 'USD',
],
'zone' => $zone,
];
$this->variation
->set('test_stock_level', $mock_widget_values);
$this->variation
->save();
$transaction = $this
->getLastEntityTransaction($this->variation
->id());
$data = unserialize($transaction->data);
$this
->assertEquals($mock_widget_values['zone'], $transaction->location_zone);
$this
->assertEquals($mock_widget_values['adjustment'], $transaction->qty);
$this
->assertEquals($mock_widget_values['user_id'], $transaction->related_uid);
$this
->assertEquals($mock_widget_values['unit_cost']['amount'], $transaction->unit_cost);
$this
->assertEquals($mock_widget_values['unit_cost']['currency_code'], $transaction->currency_code);
$this
->assertEquals($mock_widget_values['stock_transaction_note'], $data['message']);
}
public function testSampeValueGenerator() {
$i = 0;
$fieldDefinition = $this
->createMock(FieldDefinitionInterface::class);
while ($i < 100) {
$sampleValue = StockLevel::generateSampleValue($fieldDefinition);
$value = $sampleValue['value'];
$this
->assertTrue(is_float($value));
$this
->assertTrue(is_float($value));
$this
->assertTrue(999 >= $value && -999 <= $value);
$i++;
}
}
}