You are here

public function ProductVariationAccessTest::testRouteAccess in Commerce Core 8.2

Tests route access for variations.

File

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

Class

ProductVariationAccessTest
Tests the product variation access control.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testRouteAccess() {

  /** @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([], [
    'administer commerce_product',
  ]);
  $this
    ->assertTrue($variation
    ->toUrl('collection')
    ->access($account));
  $this
    ->assertTrue($variation
    ->toUrl('add-form')
    ->access($account));
  $this
    ->assertTrue($variation
    ->toUrl('edit-form')
    ->access($account));
  $this
    ->assertTrue($variation
    ->toUrl('delete-form')
    ->access($account));
  $account = $this
    ->createUser([], [
    'manage default commerce_product_variation',
  ]);
  $this
    ->assertTrue($variation
    ->toUrl('collection')
    ->access($account));
  $this
    ->assertTrue($variation
    ->toUrl('add-form')
    ->access($account));
  $this
    ->assertTrue($variation
    ->toUrl('edit-form')
    ->access($account));
  $this
    ->assertTrue($variation
    ->toUrl('delete-form')
    ->access($account));
  $account = $this
    ->createUser([], [
    'access commerce_product overview',
  ]);
  $this
    ->assertTrue($variation
    ->toUrl('collection')
    ->access($account));
  $this
    ->assertFalse($variation
    ->toUrl('add-form')
    ->access($account));
  $this
    ->assertFalse($variation
    ->toUrl('edit-form')
    ->access($account));
  $this
    ->assertFalse($variation
    ->toUrl('delete-form')
    ->access($account));
}