You are here

public function ProductVariationAccessTest::testFrontendFiltering in Commerce Core 8.2

Tests that variations without access are not available on the frontend.

File

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

Class

ProductVariationAccessTest
Tests the product variation access control.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testFrontendFiltering() {

  /** @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\ProductVariationInterface $variation_denied */
  $variation_denied = ProductVariation::create([
    'type' => 'default',
    'sku' => 'DENY_' . $this
      ->randomMachineName(),
    'title' => $this
      ->randomString(),
    'status' => 1,
  ]);
  $variation_denied
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $product = Product::create([
    'type' => 'default',
    'title' => 'My Product Title',
    'variations' => [
      $variation,
      $variation_denied,
    ],
  ]);
  $product
    ->save();
  $product = $this
    ->reloadEntity($product);
  $user = $this
    ->createUser([], [
    'view commerce_product',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);

  /** @var \Drupal\commerce_product\ProductVariationStorageInterface $variation_storage */
  $variation_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_product_variation');
  $this->container
    ->get('request_stack')
    ->getCurrentRequest()->query
    ->set('v', $variation_denied
    ->id());
  $context = $variation_storage
    ->loadFromContext($product);
  $this
    ->assertNotEquals($variation_denied
    ->id(), $context
    ->id());
  $this
    ->assertEquals($variation
    ->id(), $context
    ->id());
  $enabled = $variation_storage
    ->loadEnabled($product);
  $this
    ->assertEquals(1, count($enabled));
}