You are here

public function CommerceOrderSyncTest::testOrderPaid in Commerce License 8.2

Tests a license is created when an order gets paid.

File

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

Class

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

Namespace

Drupal\Tests\commerce_license\Kernel

Code

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);

  // 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.
}