Commit 0594932b authored by Jonathan Sacksick's avatar Jonathan Sacksick
Browse files

Issue #3201529 by jsacksick, NigelCunningham, ViNCE, Steejo:...

Issue #3201529 by jsacksick, NigelCunningham, ViNCE, Steejo: LicenseSubscription handler fails if license not activated when order is placed.
parent fd31ad1c
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -74,7 +74,8 @@ class OrderSubscriber implements EventSubscriberInterface {
  /**
   * Reacts to an order being placed.
   *
   * Activate the licenses if the product variation type is configured to do so.
   * Creates the licenses for licensable order items, and optionally activate
   * them if configured to do so at the product variation type level.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The event we subscribed to.
@@ -92,6 +93,10 @@ class OrderSubscriber implements EventSubscriberInterface {
      if ($license && $license->getState()->getId() === 'active') {
        continue;
      }

      if (!$license) {
        $license = $this->createLicenseFromOrderItem($order_item);
      }
      $purchased_entity = $order_item->getPurchasedEntity();
      $product_variation_type = $product_variation_type_storage->load($purchased_entity->bundle());
      $activate_on_place = $product_variation_type->getThirdPartySetting('commerce_license', 'activate_on_place');
@@ -101,9 +106,6 @@ class OrderSubscriber implements EventSubscriberInterface {
      if (!$activate_on_place) {
        continue;
      }
      if (!$license) {
        $license = $this->createLicenseFromOrderItem($order_item);
      }
      $license->set('state', 'active');
      $license->save();
    }
@@ -185,6 +187,9 @@ class OrderSubscriber implements EventSubscriberInterface {
    /** @var \Drupal\commerce_license\LicenseStorageInterface $license_storage */
    $license_storage = $this->entityTypeManager->getStorage('commerce_license');
    $license = $license_storage->createFromOrderItem($order_item);
    // The license is "pending" until it gets activated, either when the order
    // gets paid, or if the license should be activated on order place.
    $license->set('state', 'pending');
    $license->save();
    // Set the license field on the order item so we have a reference
    // and can get hold of it in later events.
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ class LicenseSubscription extends SubscriptionTypeBase {
    }

    // The order item should already have a license set, as our
    // \Drupal\commerce_license\EventSubscriber\LicenseOrderSyncSubscriber's
    // \Drupal\commerce_license\EventSubscriber\OrderSubscriber's
    // commerce_order.place.pre_transition listener
    // should run before Commerce Recurring's
    // \Drupal\commerce_recurring\EventSubscriber\EventSubscriber listener,
+45 −23
Original line number Diff line number Diff line
@@ -50,6 +50,13 @@ class CommerceOrderSyncTest extends CartKernelTestBase {
   */
  protected $licenseStorage;

  /**
   * A sample user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;

  /**
   * Modules to enable.
   *
@@ -88,9 +95,9 @@ class CommerceOrderSyncTest extends CartKernelTestBase {
      '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());
    $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());

    // Create a product variation type with the license trait, using our order
    // item type.
@@ -100,8 +107,8 @@ class CommerceOrderSyncTest extends CartKernelTestBase {
      'orderItemType' => 'license_order_item_type',
      'traits' => ['commerce_license'],
    ]);
    $trait = $this->traitManager->createInstance('commerce_license');
    $this->traitManager->installTrait($trait, 'commerce_product_variation', $this->variationType->id());
    $trait = $trait_manager->createInstance('commerce_license');
    $trait_manager->installTrait($trait, 'commerce_product_variation', $this->variationType->id());

    // Create a product variation which grants a license.
    $this->variation = $this->createEntity('commerce_product_variation', [
@@ -144,10 +151,7 @@ class CommerceOrderSyncTest extends CartKernelTestBase {
    $licenses = $this->licenseStorage->loadMultiple();
    $this->assertCount(0, $licenses, "There are no licenses yet.");

    $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');
    $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());
@@ -167,14 +171,38 @@ class CommerceOrderSyncTest extends CartKernelTestBase {

    $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($customer->id(), $license->getOwnerId(), 'The license entity has the expected owner.');
    $this->assertEquals($this->variation->id(), $license->product_variation->target_id, 'The license entity references the product variation.');
    $this->assertEquals('active', $license->state->value, 'The license is active.');
    $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.');

    // Note that we don't need to check that the license has activated its
    // license type plugin, as that is covered by LicenseStateChangeTest.
  }

  /**
   * Tests a license is created on order place.
   */
  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.");
    /** @var \Drupal\commerce_license\Entity\LicenseInterface $license */
    $license = reset($licenses);
    // Get the order item. There should be only one in the order.
    $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.');
  }

  /**
   * Tests a license is created and activate with the activate_on_place setting.
   */
@@ -191,15 +219,11 @@ class CommerceOrderSyncTest extends CartKernelTestBase {
    $licenses = $this->licenseStorage->loadMultiple();
    $this->assertCount(0, $licenses, "There are no licenses yet.");

    $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');
    $cart_order = $this->container->get('commerce_cart.cart_provider')->createCart('license_order_type', $this->store, $this->user);
    $this->cartManager->addEntity($cart_order, $this->variation);

    // Place the order. This takes it only as far as the 'validation' state.
    $workflow = $cart_order->getState()->getWorkflow();
    $cart_order->getState()->applyTransition($workflow->getTransition('place'));
    $cart_order->getState()->applyTransitionById('place');
    $cart_order->save();

    $order = $this->reloadEntity($cart_order);
@@ -214,13 +238,11 @@ class CommerceOrderSyncTest extends CartKernelTestBase {
    $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($customer->id(), $license->getOwnerId(), 'The license entity has the expected owner.');
    $this->assertEquals($this->variation->id(), $license->product_variation->target_id, 'The license entity references the product variation.');
    $this->assertEquals('active', $license->state->value, 'The license is active.');

    $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.');
    // Note that we don't need to check that the license has activated its
    // license type plugin, as that is covered by LicenseStateChangeTest.
  }