You are here

public function OrderUserTest::testViewOrder in Commerce Core 8.2

Tests viewing a created order.

File

modules/order/tests/src/Functional/OrderUserTest.php, line 40

Class

OrderUserTest
Tests normal user operations with orders.

Namespace

Drupal\Tests\commerce_order\Functional

Code

public function testViewOrder() {
  $uid = $this->loggedInUser
    ->id();
  $order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'unit_price' => new Price('12.00', 'USD'),
  ]);
  $order_item
    ->save();

  /** @var \Drupal\commerce_order\Entity\Order $order */
  $order = Order::create([
    'type' => 'default',
    'state' => 'draft',
    'uid' => $uid,
    'order_items' => [
      $order_item,
    ],
    'mail' => 'testViewOrder@example.com',
    'order_number' => '1',
    'store_id' => $this->store
      ->id(),
  ]);
  $order
    ->save();

  // Check that we can view the orders page.
  $this
    ->drupalGet('/user/' . $uid . '/orders/');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Check that the draft order is not available.
  $this
    ->assertSession()
    ->linkByHrefNotExists('/user/' . $uid . '/orders/' . $order
    ->id());

  // Verify the order cannot be viewed, either.
  $this
    ->drupalGet('/user/' . $uid . '/orders/' . $order
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $order
    ->getState()
    ->applyTransitionById('place');
  $order
    ->save();

  // Check that the order is available.
  $this
    ->drupalGet('/user/' . $uid . '/orders/');
  $this
    ->assertSession()
    ->linkByHrefExists('/user/' . $uid . '/orders/' . $order
    ->id());

  // Click order and make sure it works.
  $this
    ->getSession()
    ->getPage()
    ->clickLink($order
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains($order
    ->getEmail());
}