diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php index a283140ef64eead6b56d426d343166c009d784ca..c50aa3653a92ec2b54f2ebfd9cfef24f4c5782e4 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php @@ -4,14 +4,16 @@ namespace Drupal\KernelTests\Core\Entity; -use Drupal\Core\Entity\EntityInterface; use Drupal\KernelTests\KernelTestBase; +use Drupal\Tests\EntityTrait; use Drupal\Tests\user\Traits\UserCreationTrait; /** * Defines an abstract test base for entity kernel tests. */ abstract class EntityKernelTestBase extends KernelTestBase { + + use EntityTrait; use UserCreationTrait { checkPermissions as drupalCheckPermissions; createAdminRole as drupalCreateAdminRole; @@ -43,13 +45,6 @@ abstract class EntityKernelTestBase extends KernelTestBase { */ protected $entityTypeManager; - /** - * A list of generated identifiers. - * - * @var array - */ - protected $generatedIds = []; - /** * The state service. * @@ -112,21 +107,6 @@ protected function createUser(array $permissions = [], $name = NULL, bool $admin return $this->drupalCreateUser($permissions, $name, $admin, $values); } - /** - * Reloads the given entity from the storage and returns it. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to be reloaded. - * - * @return \Drupal\Core\Entity\EntityInterface - * The reloaded entity. - */ - protected function reloadEntity(EntityInterface $entity) { - $controller = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); - $controller->resetCache([$entity->id()]); - return $controller->load($entity->id()); - } - /** * Returns the entity_test hook invocation info. * @@ -172,25 +152,4 @@ protected function refreshServices() { $this->state = $this->container->get('state'); } - /** - * Generates a random ID avoiding collisions. - * - * @param bool $string - * (optional) Whether the id should have string type. Defaults to FALSE. - * - * @return int|string - * The entity identifier. - */ - protected function generateRandomEntityId($string = FALSE) { - srand(time()); - do { - // 0x7FFFFFFF is the maximum allowed value for integers that works for all - // Drupal supported databases and is known to work for other databases - // like SQL Server 2014 and Oracle 10 too. - $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF); - } while (isset($this->generatedIds[$id])); - $this->generatedIds[$id] = $id; - return $id; - } - } diff --git a/core/tests/Drupal/Tests/EntityTrait.php b/core/tests/Drupal/Tests/EntityTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..503e601e119e63a5ff23eb71fe9f21d6405beee2 --- /dev/null +++ b/core/tests/Drupal/Tests/EntityTrait.php @@ -0,0 +1,59 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests; + +use Drupal\Core\Entity\EntityInterface; + +/** + * Provides helper methods for working with entities in tests. + * + * Expects the $entityTypeManager class property. + */ +trait EntityTrait { + + /** + * A list of entity IDs generated by self::generateRandomEntityId(). + * + * @var array + */ + protected array $generatedIds = []; + + /** + * Reloads the given entity from the storage and returns it. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to be reloaded. + * + * @return \Drupal\Core\Entity\EntityInterface + * The reloaded entity. + */ + protected function reloadEntity(EntityInterface $entity): EntityInterface { + $controller = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); + $controller->resetCache([$entity->id()]); + return $controller->load($entity->id()); + } + + /** + * Generates a random ID avoiding collisions. + * + * @param bool $string + * (optional) Whether the id should have string type. Defaults to FALSE. + * + * @return int|string + * The entity identifier. + */ + protected function generateRandomEntityId(bool $string = FALSE): int|string { + srand(time()); + do { + // 0x7FFFFFFF is the maximum allowed value for integers that works for all + // Drupal supported databases and is known to work for other databases + // like SQL Server 2014 and Oracle 10 too. + $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF); + } while (isset($this->generatedIds[$id])); + $this->generatedIds[$id] = $id; + return $id; + } + +}