View source
<?php
namespace Drupal\Tests\commerce_license\Kernel;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\commerce_cart\Kernel\CartKernelTestBase;
class CommerceOrderSyncTest extends CartKernelTestBase {
protected $orderType;
protected $variationType;
protected $variation;
protected $cartManager;
protected $licenseStorage;
protected $user;
public static $modules = [
'interval',
'recurring_period',
'commerce_license',
'commerce_license_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_license');
$this
->createUser();
$this->licenseStorage = $this->container
->get('entity_type.manager')
->getStorage('commerce_license');
$this->orderType = $this
->createEntity('commerce_order_type', [
'id' => 'license_order_type',
'label' => $this
->randomMachineName(),
'workflow' => 'order_default',
]);
$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',
],
]);
$trait_manager = \Drupal::service('plugin.manager.commerce_entity_trait');
$trait = $trait_manager
->createInstance('commerce_license_order_item_type');
$trait_manager
->installTrait($trait, 'commerce_order_item', $order_item_type
->id());
$this->variationType = $this
->createEntity('commerce_product_variation_type', [
'id' => 'license_pv_type',
'label' => $this
->randomMachineName(),
'orderItemType' => 'license_order_item_type',
'traits' => [
'commerce_license',
],
]);
$trait = $trait_manager
->createInstance('commerce_license');
$trait_manager
->installTrait($trait, 'commerce_product_variation', $this->variationType
->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' => 'simple',
'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 testOrderPaid() {
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(0, $licenses, "There are no licenses yet.");
$cart_order = $this->container
->get('commerce_cart.cart_provider')
->createCart('license_order_type', $this->store, $this->user);
$this->cartManager
->addEntity($cart_order, $this->variation);
$cart_order
->set('total_paid', $cart_order
->getTotalPrice());
$cart_order
->save();
$order = $this
->reloadEntity($cart_order);
$order_item = $order
->getItems()[0];
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(1, $licenses, "One license was saved.");
$license = reset($licenses);
$this
->assertEquals($license
->id(), $order_item->license->entity
->id(), "The order item has a reference to the saved license.");
$this
->assertEquals('commerce_license', $license
->getEntityTypeId(), 'The order item has a license entity set in its license field.');
$this
->assertEquals('simple', $license
->bundle(), 'The license entity is of the expected type.');
$this
->assertEquals($this->user
->id(), $license
->getOwnerId(), 'The license entity has the expected owner.');
$this
->assertEquals($this->variation
->id(), $license
->getPurchasedEntity()
->id(), 'The license entity references the product variation.');
$this
->assertEquals('active', $license
->getState()
->getId(), 'The license is active.');
$this
->assertEquals($order
->id(), $license
->getOriginatingOrderId());
$this
->assertNotEmpty($license
->getOriginatingOrder());
$this
->assertInstanceOf(OrderInterface::class, $license
->getOriginatingOrder());
}
public function testCreateOnOrderPlace() {
$order = $this->container
->get('commerce_cart.cart_provider')
->createCart('license_order_type', $this->store, $this->user);
$this->cartManager
->addEntity($order, $this->variation);
$order
->getState()
->applyTransitionById('place');
$order
->save();
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(1, $licenses, "One license was saved.");
$license = reset($licenses);
$order_item = $order
->getItems()[0];
$this
->assertEquals($license
->id(), $order_item->license->entity
->id(), "The order item has a reference to the saved license.");
$this
->assertEquals('commerce_license', $license
->getEntityTypeId(), 'The order item has a license entity set in its license field.');
$this
->assertEquals('simple', $license
->bundle(), 'The license entity is of the expected type.');
$this
->assertEquals($this->user
->id(), $license
->getOwnerId(), 'The license entity has the expected owner.');
$this
->assertEquals($this->variation
->id(), $license
->getPurchasedEntity()
->id(), 'The license entity references the product variation.');
$this
->assertEquals('pending', $license
->getState()
->getId(), 'The license is pending.');
$this
->assertEquals($order
->id(), $license
->getOriginatingOrderId());
$this
->assertNotEmpty($license
->getOriginatingOrder());
$this
->assertInstanceOf(OrderInterface::class, $license
->getOriginatingOrder());
}
public function testActivateOnOrderPlace() {
$this->orderType
->set('workflow', 'order_default_validation');
$this->orderType
->save();
$this->variationType
->setThirdPartySetting('commerce_license', 'activate_on_place', TRUE);
$this->variationType
->save();
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(0, $licenses, "There are no licenses yet.");
$cart_order = $this->container
->get('commerce_cart.cart_provider')
->createCart('license_order_type', $this->store, $this->user);
$this->cartManager
->addEntity($cart_order, $this->variation);
$cart_order
->getState()
->applyTransitionById('place');
$cart_order
->save();
$order = $this
->reloadEntity($cart_order);
$order_item = $order
->getItems()[0];
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(1, $licenses, "One license was saved.");
$license = reset($licenses);
$this
->assertEquals($license
->id(), $order_item->license->entity
->id(), "The order item has a reference to the saved license.");
$this
->assertEquals('commerce_license', $license
->getEntityTypeId(), 'The order item has a license entity set in its license field.');
$this
->assertEquals('simple', $license
->bundle(), 'The license entity is of the expected type.');
$this
->assertEquals($this->user
->id(), $license
->getOwnerId(), 'The license entity has the expected owner.');
$this
->assertEquals($this->variation
->id(), $license
->getPurchasedEntity()
->id(), 'The license entity references the product variation.');
$this
->assertEquals('active', $license
->getState()
->getId(), 'The license is active.');
$this
->assertEquals($order
->id(), $license
->getOriginatingOrderId());
$this
->assertNotEmpty($license
->getOriginatingOrder());
$this
->assertInstanceOf(OrderInterface::class, $license
->getOriginatingOrder());
}
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;
}
}