View source
<?php
namespace Drupal\Tests\commerce_product\Kernel;
use Drupal\commerce_product\Entity\ProductAttribute;
use Drupal\commerce_product\Entity\ProductAttributeValue;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class ProductAttributeValueAccessTest extends CommerceKernelTestBase {
public static $modules = [
'path',
'commerce_product',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_product_attribute');
$this
->installEntitySchema('commerce_product_attribute_value');
$admin_user = $this
->createUser();
}
public function testAccess() {
$attribute = ProductAttribute::create([
'id' => 'color',
'label' => 'Color',
]);
$attribute
->save();
$attribute_value = ProductAttributeValue::create([
'attribute' => 'color',
'name' => 'Black',
'weight' => 3,
]);
$attribute_value
->save();
$account = $this
->createUser([], [
'access administration pages',
]);
$this
->assertFalse($attribute_value
->access('view', $account));
$this
->assertFalse($attribute_value
->access('update', $account));
$this
->assertFalse($attribute_value
->access('delete', $account));
$account = $this
->createUser([], [
'access content',
]);
$this
->assertTrue($attribute_value
->access('view', $account));
$this
->assertFalse($attribute_value
->access('update', $account));
$this
->assertFalse($attribute_value
->access('delete', $account));
$account = $this
->createUser([], [
'update commerce_product_attribute',
]);
$this
->assertFalse($attribute_value
->access('view', $account));
$this
->assertTrue($attribute_value
->access('update', $account));
$this
->assertTrue($attribute_value
->access('delete', $account));
$account = $this
->createUser([], [
'administer commerce_product_attribute',
]);
$this
->assertTrue($attribute_value
->access('view', $account));
$this
->assertTrue($attribute_value
->access('update', $account));
$this
->assertTrue($attribute_value
->access('delete', $account));
}
public function testCreateAccess() {
$access_control_handler = \Drupal::entityTypeManager()
->getAccessControlHandler('commerce_product_attribute_value');
$attribute = ProductAttribute::create([
'id' => 'color',
'label' => 'Color',
]);
$attribute
->save();
$account = $this
->createUser([], [
'access content',
]);
$this
->assertFalse($access_control_handler
->createAccess('color', $account));
$account = $this
->createUser([], [
'administer commerce_product_attribute',
]);
$this
->assertTrue($access_control_handler
->createAccess('color', $account));
$account = $this
->createUser([], [
'update commerce_product_attribute',
]);
$this
->assertTrue($access_control_handler
->createAccess('color', $account));
}
}