You are here

protected function EntityKernelTestBase::generateRandomEntityId in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php \Drupal\KernelTests\Core\Entity\EntityKernelTestBase::generateRandomEntityId()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php \Drupal\KernelTests\Core\Entity\EntityKernelTestBase::generateRandomEntityId()

Generates a random ID avoiding collisions.

Parameters

bool $string: (optional) Whether the id should have string type. Defaults to FALSE.

Return value

int|string The entity identifier.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php, line 184

Class

EntityKernelTestBase
Defines an abstract test base for entity kernel tests.

Namespace

Drupal\KernelTests\Core\Entity

Code

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;
}