You are here

public function ProductVariationAccessTest::testAccess in Commerce Core 8.2

@covers ::checkAccess

File

modules/product/tests/src/Kernel/ProductVariationAccessTest.php, line 54

Class

ProductVariationAccessTest
Tests the product variation access control.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testAccess() {

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => $this
      ->randomMachineName(),
    'title' => $this
      ->randomString(),
    'status' => 1,
  ]);
  $variation
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $product = Product::create([
    'type' => 'default',
    'title' => 'My Product Title',
    'variations' => [
      $variation,
    ],
  ]);
  $product
    ->save();
  $variation = $this
    ->reloadEntity($variation);
  $account = $this
    ->createUser([], [
    'access administration pages',
  ]);
  $this
    ->assertFalse($variation
    ->access('view', $account));
  $this
    ->assertFalse($variation
    ->access('update', $account));
  $this
    ->assertFalse($variation
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'view commerce_product',
  ]);
  $this
    ->assertTrue($variation
    ->access('view', $account));
  $this
    ->assertFalse($variation
    ->access('update', $account));
  $this
    ->assertFalse($variation
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'update any default commerce_product',
  ]);
  $this
    ->assertFalse($variation
    ->access('view', $account));
  $this
    ->assertFalse($variation
    ->access('update', $account));
  $this
    ->assertFalse($variation
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'manage default commerce_product_variation',
  ]);
  $this
    ->assertFalse($variation
    ->access('view', $account));
  $this
    ->assertTrue($variation
    ->access('update', $account));
  $this
    ->assertTrue($variation
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'administer commerce_product',
  ]);
  $this
    ->assertTrue($variation
    ->access('view', $account));
  $this
    ->assertTrue($variation
    ->access('update', $account));
  $this
    ->assertTrue($variation
    ->access('delete', $account));

  // Broken product reference.
  $variation
    ->set('product_id', '999');
  $account = $this
    ->createUser([], [
    'manage default commerce_product_variation',
  ]);
  $this
    ->assertFalse($variation
    ->access('view', $account));
  $this
    ->assertFalse($variation
    ->access('update', $account));
  $this
    ->assertFalse($variation
    ->access('delete', $account));
}