View source
<?php
namespace Drupal\Tests\commerce_order\Kernel;
use Drupal\commerce_number_pattern\Entity\NumberPattern;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderType;
use Drupal\commerce_price\Price;
class OrderNumberTest extends OrderKernelTestBase {
public static $modules = [
'commerce_test',
];
public function testSetOrderNumber() {
$user = $this
->createUser([
'mail' => $this
->randomString() . '@example.com',
]);
$number_pattern = NumberPattern::load('order_default');
$number_pattern
->setPluginConfiguration([
'initial_number' => 102,
'padding' => 4,
] + $number_pattern
->getPluginConfiguration());
$number_pattern
->save();
$first_order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'unit_price' => new Price('12.00', 'USD'),
]);
$first_order_item
->save();
$first_order = Order::create([
'type' => 'default',
'store_id' => $this->store
->id(),
'state' => 'draft',
'mail' => 'text@example.com',
'uid' => $user
->id(),
'ip_address' => '127.0.0.1',
'order_items' => [
$first_order_item,
],
]);
$first_order
->save();
$first_order
->getState()
->applyTransitionById('place');
$first_order
->save();
$this
->assertEquals('0102', $first_order
->getOrderNumber());
$order_type = OrderType::load('default');
$order_type
->setNumberPatternId('INVALID');
$order_type
->save();
$second_order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'unit_price' => new Price('12.00', 'USD'),
]);
$second_order_item
->save();
$second_order = Order::create([
'type' => 'default',
'store_id' => $this->store
->id(),
'state' => 'draft',
'mail' => 'text@example.com',
'uid' => $user
->id(),
'ip_address' => '127.0.0.1',
'order_items' => [
$second_order_item,
],
]);
$second_order
->save();
$second_order
->getState()
->applyTransitionById('place');
$second_order
->save();
$this
->assertEquals($second_order
->id(), $second_order
->getOrderNumber());
$third_order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'unit_price' => new Price('14.00', 'USD'),
]);
$third_order_item
->save();
$third_order = Order::create([
'type' => 'default',
'store_id' => $this->store
->id(),
'state' => 'draft',
'mail' => 'text@example.com',
'uid' => $user
->id(),
'ip_address' => '127.0.0.1',
'order_number' => '9999',
'order_items' => [
$third_order_item,
],
]);
$third_order
->save();
$third_order
->getState()
->applyTransitionById('place');
$third_order
->save();
$this
->assertEquals('9999', $third_order
->getOrderNumber());
}
}