You are here

public function CartExpirationTest::testNonEligibleOrder in Commerce Core 8.2

Tests expiration when a queued order is no longer eligible for deletion.

File

modules/cart/tests/src/Kernel/CartExpirationTest.php, line 142

Class

CartExpirationTest
Tests cart expiration.

Namespace

Drupal\Tests\commerce_cart\Kernel

Code

public function testNonEligibleOrder() {
  $order_type = OrderType::load('default');
  $order_type
    ->setThirdPartySetting('commerce_cart', 'cart_expiration', [
    'unit' => 'day',
    'number' => 3,
  ]);
  $order_type
    ->save();
  $time = $this->container
    ->get('datetime.time');
  $four_days_ago = $time
    ->getRequestTime() - 86400 * 4;
  $cart1 = Order::create([
    'type' => $order_type
      ->id(),
    'store_id' => $this->store
      ->id(),
    'uid' => $this
      ->createUser()
      ->id(),
    'cart' => TRUE,
    'created' => $four_days_ago,
    'changed' => $four_days_ago,
  ]);
  $cart1
    ->save();
  $cart2 = Order::create([
    'type' => $order_type
      ->id(),
    'store_id' => $this->store
      ->id(),
    'uid' => $this
      ->createUser()
      ->id(),
    'cart' => TRUE,
    'created' => $four_days_ago,
    'changed' => $four_days_ago,
  ]);
  $cart2
    ->save();

  // Setting the `changed` attribute doesn't work in save.
  $count = Database::getConnection()
    ->update('commerce_order')
    ->fields([
    'changed' => $four_days_ago,
  ])
    ->condition('order_id', [
    $cart1
      ->id(),
    $cart2
      ->id(),
  ], 'IN')
    ->execute();
  $this
    ->assertEquals(2, $count);
  $this->container
    ->get('commerce_cart.cron')
    ->run();
  $this
    ->assertEquals(1, \Drupal::queue('commerce_cart_expiration')
    ->numberOfItems());
  $cart1
    ->getState()
    ->applyTransitionById('place');
  $cart1
    ->save();

  // Confirm that $cart1 was skipped and $cart2 was deleted.
  $this->container
    ->get('cron')
    ->run();
  $this->orderStorage
    ->resetCache();
  $orders = $this->orderStorage
    ->getQuery()
    ->accessCheck(FALSE)
    ->execute();
  $this
    ->assertEquals(1, count($orders));
  $this
    ->assertNotNull($this
    ->reloadEntity($cart1));
  $this
    ->assertNull($this
    ->reloadEntity($cart2));
}