You are here

public function SchedulerCommerceProductSetupTrait::schedulerCommerceProductSetUp in Scheduler 2.x

Set common properties, define content types and create users.

File

tests/src/Traits/SchedulerCommerceProductSetupTrait.php, line 73

Class

SchedulerCommerceProductSetupTrait
Additional setup trait for Scheduler tests that use Commerce Product.

Namespace

Drupal\Tests\scheduler\Traits

Code

public function schedulerCommerceProductSetUp() {

  /** @var Store $store */
  $this->store = $this
    ->entityStorageObject('commerce_store')
    ->create([
    'type' => 'online',
    'name' => 'My Test Store',
  ]);
  $this->store
    ->save();
  $product_type_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_product_type');

  // Create a test product type that is enabled for scheduling.

  /** @var Drupal\commerce_product\Entity\ProductType $productType */
  $this->productType = $product_type_storage
    ->create([
    'id' => $this->productTypeName,
    'label' => $this->productTypeLabel,
    'variationType' => 'default',
  ]);

  // Add scheduler functionality to the product type, then save.
  $this->productType
    ->setThirdPartySetting('scheduler', 'publish_enable', TRUE)
    ->setThirdPartySetting('scheduler', 'unpublish_enable', TRUE)
    ->save();

  // Add the body field using the existing commerce_product function.
  commerce_product_add_body_field($this->productType);

  // Create a test product type which is not enabled for scheduling.

  /** @var Drupal\commerce_product\Entity\ProductType $nonSchedulerProductType */
  $this->nonSchedulerProductType = $product_type_storage
    ->create([
    'id' => $this->nonSchedulerProductTypeName,
    'label' => $this->nonSchedulerProductTypeLabel,
    'variationType' => 'default',
  ]);

  // Requires a separate save, not part of the create() above, if not doing
  // any other save() on the product type.
  $this->nonSchedulerProductType
    ->save();

  /** @var Drupal\commerce\CommerceContentEntityStorage $productStorage */
  $this->productStorage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_product');

  // Add extra permisssions to the role assigned to the adminUser.
  $this
    ->addPermissionsToUser($this->adminUser, [
    'create ' . $this->productTypeName . ' commerce_product',
    'update any ' . $this->productTypeName . ' commerce_product',
    'delete any ' . $this->productTypeName . ' commerce_product',
    'create ' . $this->nonSchedulerProductTypeName . ' commerce_product',
    'update any ' . $this->nonSchedulerProductTypeName . ' commerce_product',
    'delete any ' . $this->nonSchedulerProductTypeName . ' commerce_product',
    // 'administer commerce_store' is needed to see and use any store, i.e
    // cannot add a product without this. Is it a bug?
    'administer commerce_store',
    'access commerce_product overview',
    'view own unpublished commerce_product',
    'schedule publishing of commerce_product',
    'view scheduled commerce_product',
  ]);

  // Add extra permisssions to the role assigned to the schedulerUser.
  $this
    ->addPermissionsToUser($this->schedulerUser, [
    'create ' . $this->productTypeName . ' commerce_product',
    'update any ' . $this->productTypeName . ' commerce_product',
    'delete any ' . $this->productTypeName . ' commerce_product',
    // 'administer commerce_store' is needed to see and use any store, i.e
    // cannot add a product without this. Is it a bug?
    'administer commerce_store',
    'view own unpublished commerce_product',
    'schedule publishing of commerce_product',
  ]);
}