You are here

protected function EntityUnitTestBase::generateRandomEntityId in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityUnitTestBase.php \Drupal\system\Tests\Entity\EntityUnitTestBase::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.

4 calls to EntityUnitTestBase::generateRandomEntityId()
EntityReferenceFieldTest::assertUserAutocreate in core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
Asserts that the setter callback performs autocreation for users.
EntityReferenceFieldTest::assertUserRoleAutocreate in core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
Asserts that the setter callback performs autocreation for user roles.
EntityReferenceFieldTest::testAutocreateApi in core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
Tests all the possible ways to autocreate an entity via the API.
EntityReferenceFieldTest::testTargetEntityNoLoad in core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
Tests that the target entity is not unnecessarily loaded.

File

core/modules/system/src/Tests/Entity/EntityUnitTestBase.php, line 188
Contains \Drupal\system\Tests\Entity\EntityUnitTestBase.

Class

EntityUnitTestBase
Defines an abstract test base for entity unit tests.

Namespace

Drupal\system\Tests\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;
}