You are here

protected function CommerceAvailabilityExistingRightsTest::setUp in Commerce License 8.2

Overrides CartKernelTestBase::setUp

File

tests/src/Kernel/CommerceAvailabilityExistingRightsTest.php, line 50

Class

CommerceAvailabilityExistingRightsTest
Tests a product is not added to the cart when the user has existing rights.

Namespace

Drupal\Tests\commerce_license\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->createUser();

  // Create an order type for licenses which uses the fulfillment workflow.
  $order_type = $this
    ->createEntity('commerce_order_type', [
    'id' => 'license_order_type',
    'label' => $this
      ->randomMachineName(),
    'workflow' => 'order_fulfillment',
  ]);

  // Create an order item type that uses that order type.
  $order_item_type = $this
    ->createEntity('commerce_order_item_type', [
    'id' => 'license_order_item_type',
    'label' => $this
      ->randomMachineName(),
    'purchasableEntityType' => 'commerce_product_variation',
    'orderType' => 'license_order_type',
    'traits' => [
      'commerce_license_order_item_type',
    ],
  ]);
  $this->traitManager = \Drupal::service('plugin.manager.commerce_entity_trait');
  $trait = $this->traitManager
    ->createInstance('commerce_license_order_item_type');
  $this->traitManager
    ->installTrait($trait, 'commerce_order_item', $order_item_type
    ->id());

  // Create a product variation type with the license trait, using our order
  // item type.
  $product_variation_type = $this
    ->createEntity('commerce_product_variation_type', [
    'id' => 'license_pv_type',
    'label' => $this
      ->randomMachineName(),
    'orderItemType' => 'license_order_item_type',
    'traits' => [
      'commerce_license',
    ],
  ]);
  $trait = $this->traitManager
    ->createInstance('commerce_license');
  $this->traitManager
    ->installTrait($trait, 'commerce_product_variation', $product_variation_type
    ->id());

  // Create a product variation which grants a license.
  $this->variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'license_pv_type',
    'sku' => $this
      ->randomMachineName(),
    'price' => [
      'number' => 999,
      'currency_code' => 'USD',
    ],
    'license_type' => [
      'target_plugin_id' => 'existing_rights_check_config',
      'target_plugin_configuration' => [],
    ],
    // Use the unlimited expiry plugin as it's simple.
    'license_expiration' => [
      'target_plugin_id' => 'unlimited',
      'target_plugin_configuration' => [],
    ],
  ]);

  // We need a product too otherwise tests complain about the missing
  // backreference.
  $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this->variation,
    ],
  ]);
  $this
    ->reloadEntity($this->variation);
  $this->variation
    ->save();

  // Create a user to use for orders.
  $this->user = $this
    ->createUser();
}