function ImmutablePropertiesConstraintValidatorTest::testValidatedEntityMustHaveAnId

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Entity/ImmutablePropertiesConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\ImmutablePropertiesConstraintValidatorTest::testValidatedEntityMustHaveAnId()

Tests that entities without an ID will raise an exception.

File

core/tests/Drupal/KernelTests/Core/Entity/ImmutablePropertiesConstraintValidatorTest.php, line 67

Class

ImmutablePropertiesConstraintValidatorTest
@group Entity @group Validation

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testValidatedEntityMustHaveAnId() : void {
  $entity = $this->prophesize(ConfigEntityInterface::class);
  $entity->isNew()
    ->willReturn(FALSE)
    ->shouldBeCalled();
  $entity->getOriginalId()
    ->shouldBeCalled();
  $entity->id()
    ->shouldBeCalled();
  $definition = DataDefinition::createFromDataType('any')->addConstraint('ImmutableProperties', [
    'read_only',
  ]);
  $data = $this->container
    ->get(TypedDataManagerInterface::class)
    ->create($definition, $entity->reveal());
  $this->expectException(LogicException::class);
  $this->expectExceptionMessage('The entity does not have an ID.');
  $data->validate();
}

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