You are here

public function CommerceOrderSyncTest::testActivateOnOrderPlace in Commerce License 8.2

Tests a license is created and activate with the activate_on_place setting.

File

tests/src/Kernel/CommerceOrderSyncTest.php, line 215

Class

CommerceOrderSyncTest
Tests changes to an order are synchronized to the license.

Namespace

Drupal\Tests\commerce_license\Kernel

Code

public function testActivateOnOrderPlace() {

  // Change the configuration of the order type to use validation.
  $this->orderType
    ->set('workflow', 'order_default_validation');
  $this->orderType
    ->save();

  // Change the configuration of the product variation type to activate as
  // soon as the customer places the order.
  $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);

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

  // Get the order item. There should be only one in the order.
  $order_item = $order
    ->getItems()[0];

  // Check that the order item now refers to a new license which has been
  // created for the user.
  $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());

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