You are here

public function PriceListRepositoryTest::testCustomerRoles in Commerce Pricelist 8.2

Tests roles-based loading.

@covers ::loadItem @covers ::loadItems

File

tests/src/Kernel/PriceListRepositoryTest.php, line 152

Class

PriceListRepositoryTest
Tests the price list repository.

Namespace

Drupal\Tests\commerce_pricelist\Kernel

Code

public function testCustomerRoles() {
  $repository = $this->container
    ->get('commerce_pricelist.repository');
  $first_role = Role::create([
    'id' => strtolower($this
      ->randomMachineName(8)),
    'label' => $this
      ->randomMachineName(8),
  ]);
  $first_role
    ->save();
  $second_role = Role::create([
    'id' => strtolower($this
      ->randomMachineName(8)),
    'label' => $this
      ->randomMachineName(8),
  ]);
  $second_role
    ->save();
  $this->priceList
    ->setCustomerRoles([
    $first_role
      ->id(),
    $second_role
      ->id(),
  ]);
  $this->priceList
    ->save();
  $context = new Context($this->user, $this->store);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEmpty($price_list_item);
  $second_user = $this
    ->createUser();
  $second_user
    ->addRole($first_role
    ->id());
  $second_user
    ->save();
  $context = new Context($second_user, $this->store);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $price_list_item
    ->getPrice());
  $third_user = $this
    ->createUser();
  $third_user
    ->addRole($second_role
    ->id());
  $third_user
    ->save();
  $context = new Context($third_user, $this->store);
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $price_list_item
    ->getPrice());
}