You are here

public function CartQueryAccessTest::testAccess in Commerce Core 8.2

@covers ::onQueryAccess

File

modules/cart/tests/src/Kernel/CartQueryAccessTest.php, line 41

Class

CartQueryAccessTest
Tests query access filtering for carts.

Namespace

Drupal\Tests\commerce_cart\Kernel

Code

public function testAccess() {

  // User with full access.
  foreach ([
    'administer commerce_order',
    'view commerce_order',
  ] as $permission) {
    $user = $this
      ->createUser([], [
      $permission,
    ]);
    $conditions = $this->handler
      ->getConditions('view', $user);
    $this
      ->assertEquals(0, $conditions
      ->count());
    $this
      ->assertEquals([
      'user.permissions',
    ], $conditions
      ->getCacheContexts());
    $this
      ->assertFalse($conditions
      ->isAlwaysFalse());
  }

  // Anonymous user with no access other than to their own carts.
  $anon_user = new AnonymousUserSession();
  $cart = $this->cartProvider
    ->createCart('default', $this->store, $anon_user);
  $conditions = $this->handler
    ->getConditions('view');
  $expected_conditions = [
    new Condition('order_id', [
      $cart
        ->id(),
    ]),
  ];
  $this
    ->assertEquals(1, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());

  // Confirm that finalized carts are also allowed.
  $this->cartProvider
    ->finalizeCart($cart);
  $conditions = $this->handler
    ->getConditions('view', $anon_user);
  $this
    ->assertEquals(1, $conditions
    ->count());
  $this
    ->assertEquals([
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $expected_conditions = [
    new Condition('order_id', [
      $cart
        ->id(),
    ]),
  ];
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());

  // Create another cart.
  $another_cart = $this->cartProvider
    ->createCart('default', $this->store, $anon_user);
  $conditions = $this->handler
    ->getConditions('view', $anon_user);
  $expected_conditions = [
    new Condition('order_id', [
      $another_cart
        ->id(),
      $cart
        ->id(),
    ]),
  ];
  $this
    ->assertEquals(1, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());
}