function EntityTypeTest::testDerivativeIdMaxlength

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php \Drupal\Tests\Core\Entity\EntityTypeTest::testDerivativeIdMaxlength()

Tests maxlength validation of ID when it is a derivative.

Bundle class plugin IDs have derivative form of {entity type ID}:{bundle}, and both the entity type ID and the bundle can be up to 32 characters. As long as each is 32 or less, the combined ID is valid.

Attributes

#[TestWith([ TRUE, TRUE, ])] #[TestWith([ TRUE, FALSE, ])] #[TestWith([ FALSE, TRUE, ])] #[TestWith([ FALSE, FALSE, ])]

Parameters

bool $invalid_entity_type: Whether the entity type ID is too long.

bool $invalid_bundle: Whether the bundle is too long.

File

core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php, line 295

Class

EntityTypeTest
Tests Drupal\Core\Entity\EntityType.

Namespace

Drupal\Tests\Core\Entity

Code

public function testDerivativeIdMaxlength(bool $invalid_entity_type, bool $invalid_bundle) : void {
  $entity_type_id = $invalid_entity_type ? $this->randomMachineName(EntityTypeInterface::ID_MAX_LENGTH + 1) : $this->randomMachineName(EntityTypeInterface::ID_MAX_LENGTH);
  $bundle = $invalid_bundle ? $this->randomMachineName(EntityTypeInterface::BUNDLE_MAX_LENGTH + 1) : $this->randomMachineName(EntityTypeInterface::BUNDLE_MAX_LENGTH);
  try {
    $t = NULL;
    $entity_type = $this->setUpEntityType([
      'id' => "{$entity_type_id}:{$bundle}",
    ]);
  } catch (\Throwable $t) {
    $entity_type = NULL;
  }
  if ($invalid_bundle) {
    $this->assertInstanceOf(EntityTypeIdLengthException::class, $t);
    $this->assertSame('Attempt to create an entity type bundle class with an ID longer than ' . EntityTypeInterface::ID_MAX_LENGTH . " characters: {$bundle}.", $t->getMessage());
  }
  elseif ($invalid_entity_type) {
    $this->assertInstanceOf(EntityTypeIdLengthException::class, $t);
    $this->assertSame('Attempt to create an entity type with an ID longer than ' . EntityTypeInterface::ID_MAX_LENGTH . " characters: {$entity_type_id}.", $t->getMessage());
  }
  else {
    $this->assertInstanceOf(EntityTypeInterface::class, $entity_type);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.