View source
<?php
namespace Drupal\Tests\commerce_promotion\Kernel;
use Drupal\commerce_promotion\Entity\Coupon;
use Drupal\commerce_promotion\Entity\Promotion;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
class CouponAccessTest extends OrderKernelTestBase {
public static $modules = [
'commerce_promotion',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_promotion');
$this
->installEntitySchema('commerce_promotion_coupon');
$this
->installSchema('commerce_promotion', [
'commerce_promotion_usage',
]);
$admin_user = $this
->createUser();
}
public function testAccess() {
$promotion = Promotion::create([
'order_types' => [
'default',
],
'stores' => [
$this->store
->id(),
],
'status' => TRUE,
]);
$promotion
->save();
$coupon = Coupon::create([
'promotion_id' => $promotion
->id(),
'code' => $this
->randomMachineName(),
'status' => TRUE,
]);
$coupon
->save();
$account = $this
->createUser([], [
'access administration pages',
]);
$this
->assertFalse($coupon
->access('view', $account));
$this
->assertFalse($coupon
->access('update', $account));
$this
->assertFalse($coupon
->access('delete', $account));
$account = $this
->createUser([], [
'view commerce_promotion',
]);
$this
->assertTrue($coupon
->access('view', $account));
$this
->assertFalse($coupon
->access('update', $account));
$this
->assertFalse($coupon
->access('delete', $account));
$account = $this
->createUser([], [
'update any commerce_promotion',
]);
$this
->assertFalse($coupon
->access('view', $account));
$this
->assertTrue($coupon
->access('update', $account));
$this
->assertTrue($coupon
->access('delete', $account));
$account = $this
->createUser([], [
'administer commerce_promotion',
]);
$this
->assertTrue($coupon
->access('view', $account));
$this
->assertTrue($coupon
->access('update', $account));
$this
->assertTrue($coupon
->access('delete', $account));
}
public function testCreateAccess() {
$access_control_handler = \Drupal::entityTypeManager()
->getAccessControlHandler('commerce_promotion_coupon');
$account = $this
->createUser([], [
'access content',
]);
$this
->assertFalse($access_control_handler
->createAccess('test', $account));
$account = $this
->createUser([], [
'update any commerce_promotion',
]);
$this
->assertTrue($access_control_handler
->createAccess('test', $account));
$account = $this
->createUser([], [
'administer commerce_promotion',
]);
$this
->assertTrue($access_control_handler
->createAccess('test', $account));
}
}