View source
<?php
namespace Drupal\Tests\commerce_license\Kernel\System;
use Drupal\advancedqueue\Entity\Queue;
use Drupal\advancedqueue\Job;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
use ReflectionClass;
class LicenseCronExpiryTest extends OrderKernelTestBase {
use AssertMailTrait;
const TIME_ONE_DAY = 60 * 60 * 24;
public static $modules = [
'advancedqueue',
'commerce_license',
'commerce_license_test',
'interval',
'recurring_period',
'commerce_license_set_expiry_test',
];
protected $entityTypeManager;
protected $cron;
protected function setUp() : void {
parent::setUp();
$this
->installSchema('advancedqueue', 'advancedqueue');
$this
->installEntitySchema('commerce_license');
$this
->installConfig('commerce_license_set_expiry_test');
$this->cron = \Drupal::service('cron');
$this->entityTypeManager = \Drupal::service('entity_type.manager');
}
protected static function today() {
return time();
}
protected static function yesterday() {
return self::today() - self::TIME_ONE_DAY;
}
protected static function tomorrow() {
return self::today() + self::TIME_ONE_DAY;
}
protected static function getMethod($class, $name) {
$class = new ReflectionClass($class);
$method = $class
->getMethod($name);
$method
->setAccessible(TRUE);
return $method;
}
public function testGetLicenseIdsToExpireTomorrow() {
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$license_owner = $this
->createUser();
$license = $license_storage
->create([
'type' => 'simple',
'state' => 'active',
'product' => 1,
'uid' => $license_owner
->id(),
'expiration_type' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
]);
$license
->save();
$license->expires = self::tomorrow();
$license
->save();
$cron = \Drupal::service('commerce_license.cron');
$getLicenseIdsToExpire = self::getMethod('\\Drupal\\commerce_license\\Cron', 'getLicensesToExpire');
$expire_ids = $getLicenseIdsToExpire
->invokeArgs($cron, [
self::today(),
]);
$this
->assertEquals([], $expire_ids, "The license ID is not returned by the expiration query.");
}
public function testGetLicenseIdsToExpireYesterday() {
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$license_owner = $this
->createUser();
$license = $license_storage
->create([
'type' => 'simple',
'state' => 'active',
'product' => 1,
'uid' => $license_owner
->id(),
'expiration_type' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
]);
$license
->save();
$license->expires = self::yesterday();
$license
->save();
$cron = \Drupal::service('commerce_license.cron');
$getLicenseIdsToExpire = self::getMethod('\\Drupal\\commerce_license\\Cron', 'getLicensesToExpire');
$expire_ids = $getLicenseIdsToExpire
->invokeArgs($cron, [
self::today(),
]);
$this
->assertEquals([
$license
->id() => $license
->id(),
], $expire_ids, "The license ID is returned by the expiration query.");
}
public function testLicenseCronExpiryCurrent() {
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$license_owner = $this
->createUser();
$license = $license_storage
->create([
'type' => 'simple',
'state' => 'active',
'product' => 1,
'uid' => $license_owner
->id(),
'expiration_type' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
]);
$license
->save();
$license->expires = self::tomorrow();
$license
->save();
$this->cron
->run();
$this
->assertEquals('active', $license
->getState()
->getId(), "The license has not been changed and is still active.");
$queue = $this->container
->get('queue')
->get('commerce_license_expire');
$this
->assertEquals(0, $queue
->numberOfItems(), 'The license item was not added to the queue.');
}
public function testLicenseCronExpiryExpired() {
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$license_owner = $this
->createUser();
$license = $license_storage
->create([
'type' => 'simple',
'state' => 'active',
'product' => 1,
'uid' => $license_owner
->id(),
'expiration_type' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
]);
$license
->save();
$license->expires = self::yesterday();
$license
->save();
$this->cron
->run();
$license = $this
->reloadEntity($license);
$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());
}
public function testLicenseExpireJob() {
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$license_owner = $this
->createUser();
$license = $license_storage
->create([
'type' => 'simple',
'state' => 'active',
'product' => 1,
'uid' => $license_owner
->id(),
'expiration_type' => [
'target_plugin_id' => 'unlimited',
'target_plugin_configuration' => [],
],
]);
$license
->save();
$license->expires = self::yesterday();
$license
->save();
$license = $this
->reloadEntity($license);
$this
->assertEquals('active', $license
->getState()
->getId(), "The license is currently active.");
$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.");
$notification_queue = Queue::load('commerce_license_notify');
$counts = array_filter($notification_queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_QUEUED => 1,
], $counts);
$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);
}
}