View source
<?php
namespace Drupal\Tests\commerce_license\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\commerce_cart\Kernel\CartKernelTestBase;
class CommerceAvailabilityExistingRightsTest extends CartKernelTestBase {
protected $variation;
protected $cartManager;
public static $modules = [
'recurring_period',
'commerce_license',
'commerce_license_test',
];
protected function setUp() : void {
parent::setUp();
$this
->createUser();
$order_type = $this
->createEntity('commerce_order_type', [
'id' => 'license_order_type',
'label' => $this
->randomMachineName(),
'workflow' => 'order_fulfillment',
]);
$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());
$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());
$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' => [],
],
'license_expiration' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
]);
$this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$this->variation,
],
]);
$this
->reloadEntity($this->variation);
$this->variation
->save();
$this->user = $this
->createUser();
}
public function testAddToCart() {
$this->store = $this
->createStore();
$customer = $this
->createUser();
$cart_order = $this->container
->get('commerce_cart.cart_provider')
->createCart('license_order_type', $this->store, $customer);
$this->cartManager = $this->container
->get('commerce_cart.cart_manager');
\Drupal::state()
->set('commerce_license_test.existing_rights_check_config', TRUE);
$this->cartManager
->addEntity($cart_order, $this->variation);
$cart_order = $this
->reloadEntity($cart_order);
$this
->assertTrue(\Drupal::state()
->get('commerce_license_test.called.checkUserHasExistingRights'), "The checkUserHasExistingRights() method was called on the license type plugin.");
$this
->assertCount(0, $cart_order
->getItems(), "The product was not added to the cart.");
\Drupal::state()
->set('commerce_license_test.existing_rights_check_config', FALSE);
$this->cartManager
->addEntity($cart_order, $this->variation);
$cart_order = $this
->reloadEntity($cart_order);
$this
->assertCount(1, $cart_order
->getItems(), "The product was added to the cart.");
}
protected function createEntity($entity_type, array $values) {
$storage = \Drupal::service('entity_type.manager')
->getStorage($entity_type);
$entity = $storage
->create($values);
$status = $entity
->save();
$this
->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created %label entity %type.', [
'%label' => $entity
->getEntityType()
->getLabel(),
'%type' => $entity
->id(),
]));
$entity = $storage
->load($entity
->id());
return $entity;
}
}