You are here

public function CommerceOrderSyncTest::testCreateOnOrderPlace in Commerce License 8.2

Tests a license is created on order place.

File

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

Class

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

Namespace

Drupal\Tests\commerce_license\Kernel

Code

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.');
  $this
    ->assertEquals($order
    ->id(), $license
    ->getOriginatingOrderId());
  $this
    ->assertNotEmpty($license
    ->getOriginatingOrder());
  $this
    ->assertInstanceOf(OrderInterface::class, $license
    ->getOriginatingOrder());
}