OrderUserTest.php in Commerce Core 8.2
File
modules/order/tests/src/Functional/OrderUserTest.php
View source
<?php
namespace Drupal\Tests\commerce_order\Functional;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
class OrderUserTest extends OrderBrowserTestBase {
protected $user;
protected function setUp() : void {
parent::setUp();
$permissions = [
'view own commerce_order',
];
$this->user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->user);
}
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();
$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();
$this
->drupalGet('/user/' . $uid . '/orders/');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkByHrefNotExists('/user/' . $uid . '/orders/' . $order
->id());
$this
->drupalGet('/user/' . $uid . '/orders/' . $order
->id());
$this
->assertSession()
->statusCodeEquals(403);
$order
->getState()
->applyTransitionById('place');
$order
->save();
$this
->drupalGet('/user/' . $uid . '/orders/');
$this
->assertSession()
->linkByHrefExists('/user/' . $uid . '/orders/' . $order
->id());
$this
->getSession()
->getPage()
->clickLink($order
->id());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($order
->getEmail());
}
public function testAnonymousViewOrder() {
$order_item = OrderItem::create([
'type' => 'default',
'quantity' => 2,
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item
->save();
$order = Order::create([
'type' => 'default',
'state' => 'completed',
'uid' => 0,
'order_items' => [
$order_item,
],
'mail' => 'testViewOrder@example.com',
'order_number' => '1',
]);
$order
->save();
$this
->drupalGet('/user/0/orders/');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('/user/0/orders/' . $order
->id());
$this
->assertSession()
->statusCodeEquals(403);
}
}