diff --git a/src/Cron.php b/src/Cron.php index 94710ec..49bfb59 100644 --- a/src/Cron.php +++ b/src/Cron.php @@ -81,7 +81,8 @@ class Cron implements CronInterface { */ protected function getLicensesToExpire($time) { // Get all of the active expired licenses. - $query = \Drupal::entityQuery('commerce_license') + $query = $this->entityTypeManager->getStorage('commerce_license') + ->getQuery() ->condition('state', 'active') ->condition('expires', $time, '<=') ->condition('expires', 0, '<>'); diff --git a/tests/src/Kernel/LicenseCronExpiryTest.php b/tests/src/Kernel/LicenseCronExpiryTest.php index 9b90926..0fa44dc 100644 --- a/tests/src/Kernel/LicenseCronExpiryTest.php +++ b/tests/src/Kernel/LicenseCronExpiryTest.php @@ -135,7 +135,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { * expired yet. */ public function testGetLicenseIdsToExpireTomorrow() { - $license_storage = $this->freshLicenseStorage(); + $license_storage = $this->entityTypeManager->getStorage('commerce_license'); $license_owner = $this->createUser(); @@ -172,7 +172,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { * expired yet. */ public function testGetLicenseIdsToExpireYesterday() { - $license_storage = $this->freshLicenseStorage(); + $license_storage = $this->entityTypeManager->getStorage('commerce_license'); $license_owner = $this->createUser(); @@ -206,7 +206,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { * Tests that a cron run won't expire a current license. */ public function testLicenseCronExpiryCurrent() { - $license_storage = $this->freshLicenseStorage(); + $license_storage = $this->entityTypeManager->getStorage('commerce_license'); $license_owner = $this->createUser(); @@ -242,7 +242,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { * Tests that a cron run expires an expired license. */ public function testLicenseCronExpiryExpired() { - $license_storage = $this->freshLicenseStorage(); + $license_storage = $this->entityTypeManager->getStorage('commerce_license'); $license_owner = $this->createUser(); @@ -268,9 +268,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { // This cron run sets up the queued jobs. $this->cron->run(); - // Reload the license. - $license_storage = $this->freshLicenseStorage(); - $license = $license_storage->load($license->id()); + $license = $this->reloadEntity($license); /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */ $queue = Queue::load('commerce_license'); @@ -286,7 +284,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { * Tests that the LicenseExpire job expires the license. */ public function testLicenseExpireJob() { - $license_storage = $this->freshLicenseStorage(); + $license_storage = $this->entityTypeManager->getStorage('commerce_license'); $license_owner = $this->createUser(); @@ -309,9 +307,7 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { $license->expires = self::yesterday(); $license->save(); - // Reload the license. - $license_storage = $this->freshLicenseStorage(); - $license = $license_storage->load($license->id()); + $license = $this->reloadEntity($license); $this->assertEquals('active', $license->state->value, "The license is currently active."); /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */ @@ -322,30 +318,11 @@ class LicenseCronExpiryTest extends EntityKernelTestBase { $num_processed = $processor->processQueue($queue); $this->assertEquals(1, $num_processed); - $license_storage = $this->freshLicenseStorage(); - $license = $license_storage->load($license->id()); + $license = $this->reloadEntity($license); $this->assertEquals('expired', $license->state->value, "The license is now expired."); // Note that we don't need to check that the expiry did something, as that // is covered by LicenseStateChangeTest. } - /** - * Returns a fresh copy of commerce_license storage. - * - * Clear the entity handlers cached in the entity type manager, so that the - * license storage handler gets current data. - * - * (This clears all the entity type definitions too, which we don't need; - * see https://www.drupal.org/node/2902487.) - * - * @return \Drupal\Core\Entity\EntityStorageInterface - * A fresh copy of commerce_license storage. - */ - protected function freshLicenseStorage() { - $this->entityTypeManager->clearCachedDefinitions(); - $license_storage = $this->entityTypeManager->getStorage('commerce_license'); - return $license_storage; - } - }