public function LicenseCronExpiryTest::testLicenseExpireJob in Commerce License 8.2
Tests that the LicenseExpire job expires the license.
File
- tests/
src/ Kernel/ LicenseCronExpiryTest.php, line 272
Class
- LicenseCronExpiryTest
- Tests that cron expires a license.
Namespace
Drupal\Tests\commerce_license\Kernel\SystemCode
public function testLicenseExpireJob() {
$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();
$license = $this
->reloadEntity($license);
$this
->assertEquals('active', $license
->getState()
->getId(), "The license is currently active.");
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
$queue = Queue::load('commerce_license');
$job = Job::create('commerce_license_expire', [
'license_id' => $license
->id(),
]);
$queue
->enqueueJob($job);
$processor = $this->container
->get('advancedqueue.processor');
$num_processed = $processor
->processQueue($queue);
$this
->assertEquals(1, $num_processed);
$counts = array_filter($queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_SUCCESS => 1,
], $counts);
$license = $this
->reloadEntity($license);
$this
->assertEquals('expired', $license
->getState()
->getId(), "The license is now expired.");
// Note that we don't need to check that the expiry did something, as that
// is covered by LicenseStateChangeTest.
// Check the notification email is now queued.
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
$notification_queue = Queue::load('commerce_license_notify');
$counts = array_filter($notification_queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_QUEUED => 1,
], $counts);
// Run the notification queue.
$num_processed = $processor
->processQueue($notification_queue);
$this
->assertEquals(1, $num_processed);
$counts = array_filter($notification_queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_SUCCESS => 1,
], $counts);
$mails = $this
->getMails();
$this
->assertEquals(1, count($mails));
$expiry_email = reset($mails);
$this
->assertEquals('text/html; charset=UTF-8;', $expiry_email['headers']['Content-Type']);
$this
->assertEquals('8Bit', $expiry_email['headers']['Content-Transfer-Encoding']);
$this
->assertMailString('subject', 'Your purchase of test license has now expired', 1);
$this
->assertMailString('body', 'License Expiry', 1);
$this
->assertMailString('body', 'Your purchase of test license has now expired', 1);
// TODO: add a product to test the product text.
}