View source
<?php
namespace Drupal\Tests\commerce_cart\Kernel;
use Drupal\commerce_order\OrderQueryAccessHandler;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\entity\QueryAccess\Condition;
class CartQueryAccessTest extends CartKernelTestBase {
protected $handler;
protected function setUp() : void {
parent::setUp();
$admin_user = $this
->createUser();
$entity_type_manager = $this->container
->get('entity_type.manager');
$entity_type = $entity_type_manager
->getDefinition('commerce_order');
$this->handler = OrderQueryAccessHandler::createInstance($this->container, $entity_type);
}
public function testAccess() {
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());
}
$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());
$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());
$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());
}
}