You are here

public function LicenseCronExpiryTest::testLicenseCronExpiryExpired in Commerce License 8.2

Tests that a cron run expires an expired license.

File

tests/src/Kernel/LicenseCronExpiryTest.php, line 230

Class

LicenseCronExpiryTest
Tests that cron expires a license.

Namespace

Drupal\Tests\commerce_license\Kernel\System

Code

public function testLicenseCronExpiryExpired() {
  $license_storage = $this->entityTypeManager
    ->getStorage('commerce_license');
  $license_owner = $this
    ->createUser();

  // Create a license in the 'active' state.
  $license = $license_storage
    ->create([
    'type' => 'simple',
    'state' => 'active',
    'product' => 1,
    'uid' => $license_owner
      ->id(),
    // Use the unlimited expiry plugin as it's simple.
    'expiration_type' => [
      'target_plugin_id' => 'unlimited',
      'target_plugin_configuration' => [],
    ],
  ]);
  $license
    ->save();

  // Force the expiration timestamp.
  // As the state is not being changed, the expiration plugin won't be called.
  $license->expires = self::yesterday();
  $license
    ->save();

  // This cron run sets up the queued jobs.
  $this->cron
    ->run();
  $license = $this
    ->reloadEntity($license);

  /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
  $queue = Queue::load('commerce_license');
  $counts = array_filter($queue
    ->getBackend()
    ->countJobs());
  $this
    ->assertEquals([
    Job::STATE_QUEUED => 1,
  ], $counts);
  $job1 = $queue
    ->getBackend()
    ->claimJob();
  $this
    ->assertArraySubset([
    'license_id' => $license
      ->id(),
  ], $job1
    ->getPayload());
  $this
    ->assertEquals('commerce_license_expire', $job1
    ->getType());
}