diff --git a/src/Cron.php b/src/Cron.php index 6f0767f..a1c33e5 100644 --- a/src/Cron.php +++ b/src/Cron.php @@ -4,7 +4,6 @@ namespace Drupal\commerce_license; use Drupal\advancedqueue\Job; use Drupal\Component\Datetime\TimeInterface; -use Drupal\Core\CronInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -20,13 +19,6 @@ class Cron implements CronInterface { */ protected $entityTypeManager; - /** - * The license storage. - * - * @var \Drupal\commerce_license\LicenseStorageInterface - */ - protected $licenseStorage; - /** * The time. * @@ -44,7 +36,6 @@ class Cron implements CronInterface { */ public function __construct(EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) { $this->entityTypeManager = $entity_type_manager; - $this->licenseStorage = $entity_type_manager->getStorage('commerce_license'); $this->time = $time; } @@ -63,14 +54,15 @@ class Cron implements CronInterface { */ public function run() { $time = $this->time->getRequestTime(); - $license_ids = $this->licenseStorage->getLicenseIdsToExpire($time); + $license_ids = $this->getLicensesToExpire($time); + if ($license_ids) { $queue_storage = $this->entityTypeManager->getStorage('advancedqueue_queue'); /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */ $queue = $queue_storage->load('commerce_license'); foreach ($license_ids as $license_id) { // Create a job and queue each one up. - $expire_remove_roles_job = Job::create('commerce_license_expire_expired_license', [ + $expire_remove_roles_job = Job::create('commerce_license_expire', [ 'license_id' => $license_id, ]); $queue->enqueueJob($expire_remove_roles_job); @@ -78,4 +70,25 @@ class Cron implements CronInterface { } } + /** + * Gets IDs of licenses that are set to expire. + * + * @param int $time + * Time to check against license expiration. + * + * @return array|int + * IDs of matching commerce_license entities. + */ + protected function getLicensesToExpire($time) { + // Get all of the active expired licenses. + // Ones we've already revoked will be marked expired. + $query = \Drupal::entityQuery('commerce_license') + ->condition('state', 'active') + ->condition('expires', $time, '<=') + ->condition('expires', 0, '<>'); + + $license_ids = $query->execute(); + return $license_ids; + } + } diff --git a/src/CronInterface.php b/src/CronInterface.php new file mode 100644 index 0000000..7da224d --- /dev/null +++ b/src/CronInterface.php @@ -0,0 +1,17 @@ +condition('state', 'active') - ->condition('expires', $time, '<=') - ->condition('expires', 0, '<>'); - - $license_ids = $query->execute(); - return $license_ids; - } - } diff --git a/src/LicenseStorageInterface.php b/src/LicenseStorageInterface.php index ae74c2b..88def38 100644 --- a/src/LicenseStorageInterface.php +++ b/src/LicenseStorageInterface.php @@ -46,15 +46,4 @@ interface LicenseStorageInterface extends ContentEntityStorageInterface { */ public function createFromProductVariation(ProductVariationInterface $variation, $uid); - /** - * Returns the ids of licenses that need to be expired. - * - * @param int $time - * The timestamp to be used for expiry check. - * - * @return array - * A list of license ids to be expired. - */ - public function getLicenseIdsToExpire($time); - } diff --git a/src/Plugin/AdvancedQueue/JobType/ExpireExpiredLicenses.php b/src/Plugin/AdvancedQueue/JobType/LicenseExpire.php similarity index 69% rename from src/Plugin/AdvancedQueue/JobType/ExpireExpiredLicenses.php rename to src/Plugin/AdvancedQueue/JobType/LicenseExpire.php index 8509a67..3576368 100644 --- a/src/Plugin/AdvancedQueue/JobType/ExpireExpiredLicenses.php +++ b/src/Plugin/AdvancedQueue/JobType/LicenseExpire.php @@ -5,21 +5,20 @@ namespace Drupal\commerce_license\Plugin\AdvancedQueue\JobType; use Drupal\advancedqueue\Job; use Drupal\advancedqueue\JobResult; use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase; -use Drupal\commerce_payment\Exception\DeclineException; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Exception; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** - * Provides the job type for expiring expired licenses. + * Provides the job type for expiring licenses. * * @AdvancedQueueJobType( - * id = "commerce_license_expire_expired_license", - * label = @Translation("Expire expired licenses."), + * id = "commerce_license_expire", + * label = @Translation("Expire licenses."), * ) */ -class ExpireExpiredLicenses extends JobTypeBase implements ContainerFactoryPluginInterface { +class LicenseExpire extends JobTypeBase implements ContainerFactoryPluginInterface { /** * The entity type manager. @@ -29,14 +28,7 @@ class ExpireExpiredLicenses extends JobTypeBase implements ContainerFactoryPlugi protected $entityTypeManager; /** - * The event dispatcher. - * - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface - */ - protected $eventDispatcher; - - /** - * Constructs a new ExpireExpiredLicenses object. + * Constructs a new LicenseExpire object. * * @param array $configuration * A configuration array containing information about the plugin instance. @@ -46,14 +38,11 @@ class ExpireExpiredLicenses extends JobTypeBase implements ContainerFactoryPlugi * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. - * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher - * The event dispatcher. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; - $this->eventDispatcher = $event_dispatcher; } /** @@ -64,8 +53,7 @@ class ExpireExpiredLicenses extends JobTypeBase implements ContainerFactoryPlugi $configuration, $plugin_id, $plugin_definition, - $container->get('entity_type.manager'), - $container->get('event_dispatcher') + $container->get('entity_type.manager') ); } @@ -86,7 +74,7 @@ class ExpireExpiredLicenses extends JobTypeBase implements ContainerFactoryPlugi $license->state = 'expired'; $license->save(); } - catch (DeclineException $exception) { + catch (Exception $exception) { return $result = JobResult::failure($exception->getMessage()); } return JobResult::success(); diff --git a/tests/src/Kernel/LicenseCronExpiryTest.php b/tests/src/Kernel/LicenseCronExpiryTest.php index 08b1bc4..5e03ce1 100644 --- a/tests/src/Kernel/LicenseCronExpiryTest.php +++ b/tests/src/Kernel/LicenseCronExpiryTest.php @@ -5,7 +5,7 @@ namespace Drupal\Tests\commerce_license\Kernel\System; use Drupal\advancedqueue\Entity\Queue; use Drupal\advancedqueue\Job; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; -use Drupal\Component\Datetime\TimeInterface; +use ReflectionClass; /** * Tests that cron expires a license. @@ -26,24 +26,6 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { */ const TIME_EXPIRES_TODAY = 1234000500 + 100; - /** - * A mocked timestamp for the first cron run. - */ - const TIME_CRON_ONE = 1234000500; - - /** - * A timestamp for the license's expiration. - * - * This is later than the first cron time, but earlier than the second cron - * time. - */ - const TIME_EXPIRY = 1234000500 + 100; - - /** - * A mocked timestamp for the second cron run. - */ - const TIME_CRON_TWO = 1234000500 + 200; - /** * The modules to enable. * @@ -128,6 +110,24 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { return self::today() + self::TIME_ONE_DAY; } + /** + * Gets a protected method for testing. + * + * @param string $class + * Name of the class. + * @param string $name + * Name of the method. + * + * @return mixed + * The method. + */ + protected static function getMethod($class, $name) { + $class = new ReflectionClass($class); + $method = $class->getMethod($name); + $method->setAccessible(TRUE); + return $method; + } + /** * License hasn't expired. * @@ -158,8 +158,10 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { $license->expires = self::tomorrow(); $license->save(); - $license_storage = $this->freshLicenseStorage(); - $expire_ids = $license_storage->getLicenseIdsToExpire(self::today()); + // Test getLicensesToExpire() method. + $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."); } @@ -193,8 +195,10 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { $license->expires = self::yesterday(); $license->save(); - $license_storage = $this->freshLicenseStorage(); - $expire_ids = $license_storage->getLicenseIdsToExpire(self::today()); + // Test getLicensesToExpire() method. + $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."); } @@ -275,13 +279,13 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { $job1 = $queue->getBackend()->claimJob(); $this->assertArraySubset(['license_id' => $license->id()], $job1->getPayload()); - $this->assertEquals('commerce_license_expire_expired_license', $job1->getType()); + $this->assertEquals('commerce_license_expire', $job1->getType()); } /** - * Tests that the ExpireExpiredLicenses job expires the license. + * Tests that the LicenseExpire job expires the license. */ - public function testExpireExpiredLicensesJob() { + public function testLicenseExpireJob() { $license_storage = $this->freshLicenseStorage(); $license_owner = $this->createUser(); @@ -305,7 +309,6 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { $license->expires = self::yesterday(); $license->save(); - // Reload the license. $license_storage = $this->freshLicenseStorage(); $license = $license_storage->load($license->id()); @@ -313,7 +316,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */ $queue = Queue::load('commerce_license'); - $job = Job::create('commerce_license_expire_expired_license', ['license_id' => $license->id()]); + $job = Job::create('commerce_license_expire', ['license_id' => $license->id()]); $queue->enqueueJob($job); $processor = $this->container->get('advancedqueue.processor'); $num_processed = $processor->processQueue($queue);