View source
<?php
namespace Drupal\Tests\commerce_order\Kernel;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderType;
use Drupal\commerce_order\OrderRefresh;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\profile\Entity\Profile;
class OrderRefreshTest extends OrderKernelTestBase {
protected $user;
protected $order;
protected $variation1;
protected $variation2;
protected $orderItemStorage;
public static $modules = [
'commerce_order_test',
];
protected function setUp() : void {
parent::setUp();
$user = $this
->createUser();
$this->user = $this
->reloadEntity($user);
$this->orderItemStorage = $this->container
->get('entity_type.manager')
->getStorage('commerce_order_item');
$variation_type = ProductVariationType::load('default');
$variation_type
->setGenerateTitle(FALSE);
$variation_type
->save();
$product = Product::create([
'type' => 'default',
'title' => 'Default testing product',
]);
$product
->save();
$variation1 = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => 0,
'price' => new Price('2.00', 'USD'),
]);
$variation1
->save();
$product
->addVariation($variation1)
->save();
$this->variation1 = $this
->reloadEntity($variation1);
$variation2 = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => 1,
'price' => new Price('3.00', 'USD'),
]);
$variation2
->save();
$product
->addVariation($variation2)
->save();
$this->variation2 = $this
->reloadEntity($variation2);
$profile = Profile::create([
'type' => 'customer',
]);
$profile
->save();
$profile = $this
->reloadEntity($profile);
$order = Order::create([
'type' => 'default',
'state' => 'draft',
'mail' => $this->user
->getEmail(),
'uid' => $this->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);
}
public function testShouldRefresh() {
$order_refresh = $this
->createOrderRefresh(time() + 3600);
$order_type = OrderType::load($this->order
->bundle());
$order_type
->setRefreshMode(OrderType::REFRESH_CUSTOMER)
->save();
$this->container
->get('current_user')
->setAccount(new AnonymousUserSession());
$this
->assertEmpty($order_refresh
->shouldRefresh($this->order));
$this->container
->get('current_user')
->setAccount($this->user);
$this
->assertNotEmpty($order_refresh
->shouldRefresh($this->order));
$this->container
->get('current_user')
->setAccount(new AnonymousUserSession());
$order_type = OrderType::load($this->order
->bundle());
$order_type
->setRefreshMode(OrderType::REFRESH_ALWAYS)
->save();
$this
->assertNotEmpty($order_refresh
->shouldRefresh($this->order));
}
public function testNeedsRefresh() {
$order_refresh = $this
->createOrderRefresh();
$this->order->state = 'completed';
$this
->assertEmpty($order_refresh
->needsRefresh($this->order));
$this->order->state = 'draft';
$order_refresh = $this
->createOrderRefresh(mktime(0, 1, 0, 2, 24, 2016));
$this->order
->setChangedTime(mktime(23, 59, 59, 2, 23, 2016));
$this
->assertNotEmpty($order_refresh
->needsRefresh($this->order));
$order_refresh = $this
->createOrderRefresh(mktime(23, 12, 0, 2, 24, 2016));
$this->order
->setChangedTime(mktime(23, 11, 0, 2, 24, 2016));
$this
->assertEmpty($order_refresh
->needsRefresh($this->order));
$order_refresh = $this
->createOrderRefresh(mktime(23, 10, 0, 2, 24, 2016));
$this->order
->setChangedTime(mktime(23, 0, 0, 2, 24, 2016));
$this
->assertNotEmpty($order_refresh
->needsRefresh($this->order));
$this->order
->lock();
$this
->assertFalse($order_refresh
->needsRefresh($this->order));
$this->order
->unlock();
$this
->assertTrue($order_refresh
->needsRefresh($this->order));
}
public function testOrderItemRefresh() {
$order_refresh = $this
->createOrderRefresh();
$order_item = $this->orderItemStorage
->createFromPurchasableEntity($this->variation2);
$order_item
->save();
$this->order
->addItem($order_item);
$this->order
->setRefreshState(Order::REFRESH_SKIP);
$this->order
->save();
$this
->assertEquals($order_item
->label(), $this->variation2
->getTitle());
$this
->assertEquals($order_item
->getUnitPrice(), $this->variation2
->getPrice());
$this->variation2
->setTitle('Changed title');
$this->variation2
->setPrice(new Price('12.00', 'USD'));
$this->variation2
->save();
$order_refresh
->refresh($this->order);
$order_item = $this
->reloadEntity($order_item);
$this
->assertEquals($order_item
->label(), $this->variation2
->getTitle());
$this
->assertEquals($order_item
->getUnitPrice(), $this->variation2
->getPrice());
$unit_price = new Price('15.00', 'USD');
$order_item
->setUnitPrice($unit_price, TRUE);
$this->variation2
->setTitle('Changed title2');
$this->variation2
->setPrice(new Price('16.00', 'USD'));
$this->variation2
->save();
$order_refresh
->refresh($this->order);
$order_item = $this
->reloadEntity($order_item);
$this
->assertEquals($this->variation2
->getTitle(), $order_item
->label());
$this
->assertEquals($unit_price, $order_item
->getUnitPrice());
}
public function testAvailabilityOrderRefresh() {
$order_refresh = $this
->createOrderRefresh();
$order_item = $this->orderItemStorage
->createFromPurchasableEntity($this->variation1);
$order_item
->save();
$another_order_item = $this->orderItemStorage
->createFromPurchasableEntity($this->variation2);
$another_order_item
->save();
$this->order
->setItems([
$order_item,
$another_order_item,
]);
$this->order
->setRefreshState(Order::REFRESH_SKIP);
$this->order
->save();
$this
->assertEquals(2, count($this->order
->getItems()));
$order_refresh
->refresh($this->order);
$this
->assertEquals(1, count($this->order
->getItems()));
}
public function testStorage() {
$order_item = $this->orderItemStorage
->createFromPurchasableEntity($this->variation1);
$order_item
->save();
$another_order_item = $this->orderItemStorage
->createFromPurchasableEntity($this->variation2);
$another_order_item
->save();
$this->order
->setItems([
$order_item,
$another_order_item,
]);
$this->order
->save();
$this
->assertEquals(1, count($this->order
->getItems()));
$this
->assertNull($this->order
->getRefreshState());
$old_title = $this->variation2
->getTitle();
$this->variation2
->setTitle('Changed title');
$this->variation2
->save();
$this->order
->setRefreshState(Order::REFRESH_ON_LOAD);
$this->order
->save();
$another_order_item = $this
->reloadEntity($another_order_item);
$this
->assertEquals(Order::REFRESH_ON_LOAD, $this->order
->getRefreshState());
$this
->assertEquals($old_title, $another_order_item
->getTitle());
sleep(1);
$old_changed_time = $this->order
->getChangedTime();
$this->order = $this
->reloadEntity($this->order);
$another_order_item = $this
->reloadEntity($another_order_item);
$this
->assertNotEquals($old_changed_time, $this->order
->getChangedTime());
$this
->assertEquals('Changed title', $another_order_item
->getTitle());
$this
->assertNull($this->order
->getRefreshState());
}
protected function createOrderRefresh($current_time = NULL) {
$current_time = $current_time ?: time();
$entity_type_manager = $this->container
->get('entity_type.manager');
$chain_price_resolver = $this->container
->get('commerce_price.chain_price_resolver');
$user = $this->container
->get('current_user');
$time = $this
->prophesize(TimeInterface::class);
$time
->getCurrentTime()
->willReturn($current_time);
$time = $time
->reveal();
$order_refresh = new OrderRefresh($entity_type_manager, $chain_price_resolver, $user, $time);
$order_refresh
->addProcessor($this->container
->get('commerce_order.availability_order_processor'));
return $order_refresh;
}
}