You are here

public function CartCheckoutTest::testCartOrderTimeout in Ubercart 8.4

Tests that cart orders are marked abandoned after a timeout.

File

uc_cart/tests/src/Functional/CartCheckoutTest.php, line 597

Class

CartCheckoutTest
Tests the cart and checkout functionality.

Namespace

Drupal\Tests\uc_cart\Functional

Code

public function testCartOrderTimeout() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $this
    ->addToCart($this->product);
  $this
    ->drupalGet('cart');
  $this
    ->submitForm([], 'Checkout');
  $assert
    ->pageTextContains('Enter your billing address and information here.', 'Viewed cart page: Billing pane has been displayed.');

  // Submit the checkout page.
  $edit = $this
    ->populateCheckoutForm();
  $oldname = $edit['panes[delivery][first_name]'];
  $this
    ->drupalGet('cart/checkout');
  $this
    ->submitForm($edit, 'Review order');
  $order_ids = \Drupal::entityQuery('uc_order')
    ->condition('delivery_first_name', $oldname)
    ->execute();
  $order_id = reset($order_ids);
  if ($order_id) {

    // Go to a different page, then back to order to make sure
    // order_id is the same.
    $this
      ->drupalGet('<front>');
    $this
      ->addToCart($this->product);
    $this
      ->drupalGet('cart');
    $this
      ->submitForm([], 'Checkout');

    // Check that customer name was unchanged.
    $assert
      ->responseContains($oldname);
    $this
      ->drupalGet('cart/checkout');
    $this
      ->submitForm($edit, 'Review order');
    $new_order_ids = \Drupal::entityQuery('uc_order')
      ->condition('delivery_first_name', $edit['panes[delivery][first_name]'])
      ->execute();
    $new_order_id = reset($new_order_ids);
    $this
      ->assertEquals($order_id, $new_order_id, 'Original order_id was reused.');

    // Jump 10 minutes into the future.
    // @todo Can we set changed through the Entity API rather than DBTNG?
    \Drupal::database()
      ->update('uc_orders')
      ->fields([
      'changed' => \Drupal::time()
        ->getCurrentTime() - CartInterface::ORDER_TIMEOUT - 1,
    ])
      ->condition('order_id', $order_id)
      ->execute();

    // Go to a different page, then back to order to verify that we are
    // using a new order.
    $this
      ->drupalGet('<front>');
    $this
      ->drupalGet('cart');
    $this
      ->submitForm([], 'Checkout');

    // Check that customer name was cleared after timeout.
    $assert
      ->responseNotContains($oldname);
    $newname = $this
      ->randomMachineName(10);
    $edit['panes[delivery][first_name]'] = $newname;
    $this
      ->drupalGet('cart/checkout');
    $this
      ->submitForm($edit, 'Review order');
    $new_order_ids = \Drupal::entityQuery('uc_order')
      ->condition('delivery_first_name', $newname)
      ->execute();
    $new_order_id = reset($new_order_ids);
    $this
      ->assertNotEquals($order_id, $new_order_id, 'New order was created after timeout.');

    // Force the order to load from the DB instead of the entity cache.
    $old_order = \Drupal::entityTypeManager()
      ->getStorage('uc_order')
      ->loadUnchanged($order_id);

    // Verify that the status of old order is abandoned.
    $this
      ->assertEquals('abandoned', $old_order
      ->getStatusId(), 'Original order was marked abandoned.');
  }
  else {
    $this
      ->fail('No order was created.');
  }
}