View source
<?php
namespace Drupal\Tests\commerce_license\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_payment\Entity\PaymentGateway;
use Drupal\commerce_payment\Entity\PaymentMethod;
use Drupal\commerce_recurring\Entity\BillingSchedule;
use Drupal\commerce_recurring\Entity\Subscription;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
class CommerceRecurringSubscriptionLifecycleTest extends OrderKernelTestBase {
protected $licenseStorage;
public static $modules = [
'advancedqueue',
'commerce_payment',
'commerce_payment_example',
'interval',
'recurring_period',
'commerce_license',
'commerce_license_test',
'commerce_recurring',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_payment');
$this
->installEntitySchema('commerce_payment_method');
$this
->installEntitySchema('commerce_subscription');
$this
->installEntitySchema('commerce_license');
$this
->installEntitySchema('user');
$this
->installSchema('advancedqueue', 'advancedqueue');
$this
->installConfig('entity');
$this
->installConfig('commerce_recurring');
$this->licenseStorage = $this->container
->get('entity_type.manager')
->getStorage('commerce_license');
$trait_manager = \Drupal::service('plugin.manager.commerce_entity_trait');
$this->user = $this
->createUser();
$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 = $trait_manager
->createInstance('commerce_license_order_item_type');
$trait_manager
->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 = $trait_manager
->createInstance('commerce_license');
$trait_manager
->installTrait($trait, 'commerce_product_variation', $product_variation_type
->id());
$trait = $trait_manager
->createInstance('purchasable_entity_subscription');
$trait_manager
->installTrait($trait, 'commerce_product_variation', $product_variation_type
->id());
$billing_schedule = BillingSchedule::create([
'id' => 'test_id',
'label' => 'Hourly schedule',
'displayLabel' => 'Hourly schedule',
'billingType' => BillingSchedule::BILLING_TYPE_PREPAID,
'plugin' => 'fixed',
'configuration' => [
'interval' => [
'number' => '1',
'unit' => 'hour',
],
],
'prorater' => 'proportional',
'proraterConfiguration' => [],
]);
$billing_schedule
->save();
$this->billingSchedule = $this
->reloadEntity($billing_schedule);
$payment_gateway = PaymentGateway::create([
'id' => 'example',
'label' => 'Example',
'plugin' => 'example_onsite',
]);
$payment_gateway
->save();
$this->paymentGateway = $this
->reloadEntity($payment_gateway);
$payment_method = PaymentMethod::create([
'type' => 'credit_card',
'payment_gateway' => $this->paymentGateway,
'uid' => $this->user
->id(),
]);
$payment_method
->save();
$this->paymentMethod = $this
->reloadEntity($payment_method);
$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' => 'state_change_with_rights',
'target_plugin_configuration' => [],
],
'license_expiration' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
'billing_schedule' => $this->billingSchedule,
'subscription_type' => [
'target_plugin_id' => 'license',
],
]);
$this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$this->variation,
],
]);
$this
->reloadEntity($this->variation);
$this->variation
->save();
}
public function testCreation() {
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(0, $licenses, "There are no licenses yet.");
$order_item = OrderItem::create([
'type' => 'license_order_item_type',
'purchased_entity' => $this->variation,
'quantity' => '1',
]);
$order_item
->save();
$order = Order::create([
'type' => 'license_order_type',
'store_id' => $this->store,
'uid' => $this->user,
'order_items' => [
$order_item,
],
'state' => 'draft',
'payment_method' => $this->paymentMethod,
]);
$order
->save();
$subscriptions = Subscription::loadMultiple();
$this
->assertCount(0, $subscriptions);
$order
->set('total_paid', $order
->getTotalPrice());
$order
->getState()
->applyTransitionByid('place');
$order
->save();
$subscriptions = Subscription::loadMultiple();
$this
->assertCount(1, $subscriptions);
$subscription = reset($subscriptions);
$this
->assertEquals($this->store
->id(), $subscription
->getStoreId());
$this
->assertEquals($this->billingSchedule
->id(), $subscription
->getBillingSchedule()
->id());
$this
->assertEquals($this->user
->id(), $subscription
->getCustomerId());
$this
->assertEquals($this->paymentMethod
->id(), $subscription
->getPaymentMethod()
->id());
$this
->assertEquals($this->variation
->id(), $subscription
->getPurchasedEntityId());
$this
->assertEquals($this->variation
->getOrderItemTitle(), $subscription
->getTitle());
$this
->assertEquals('1', $subscription
->getQuantity());
$this
->assertEquals($this->variation
->getPrice(), $subscription
->getUnitPrice());
$this
->assertEquals('active', $subscription
->getState()->value);
$order_storage = \Drupal::entityTypeManager()
->getStorage('commerce_order');
$result = $order_storage
->getQuery()
->condition('type', 'recurring')
->pager(1)
->accessCheck(FALSE)
->execute();
$this
->assertNotEmpty($result);
$recurring_order = $order_storage
->load(reset($result));
$this
->assertNotEmpty($recurring_order);
$recurring_order_items = $recurring_order
->getItems();
$this
->assertCount(1, $recurring_order_items, "The recurring order has an order item.");
$recurring_order_item = reset($recurring_order_items);
$this
->assertEquals($subscription
->id(), $recurring_order_item
->get('subscription')->target_id, "The recurring order's order item has a reference to the subscription.");
$licenses = $this->licenseStorage
->loadMultiple();
$this
->assertCount(1, $licenses, "One license was saved.");
$license = reset($licenses);
$order_item = $this
->reloadEntity($order_item);
$this
->assertEquals($license
->id(), $order_item->license->entity
->id(), "The order item has a reference to the saved license.");
$this
->assertEquals($license
->id(), $subscription->license->entity
->id(), "The subscription has a reference to the saved license.");
$this
->assertEquals('active', $license
->getState()
->getId(), "The license is active.");
$this
->assertEquals('grantLicense', \Drupal::state()
->get('commerce_license_state_change_test'), "The license plugin's grantLicense() method was called.");
$subscription->state = 'canceled';
$subscription
->save();
$license = $this
->reloadEntity($license);
$this
->assertEquals('canceled', $license
->getState()
->getId(), "The license is canceled.");
$this
->assertEquals('revokeLicense', \Drupal::state()
->get('commerce_license_state_change_test'), "The license plugin's revokeLicense() method was called.");
}
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;
}
}