function ImmutablePropertiesConstraintValidatorTest::testValidatorRejectsANonExistentProperty

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

Tests that the validator throws an exception for non-existent properties.

File

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

Class

ImmutablePropertiesConstraintValidatorTest
@group Entity @group Validation

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testValidatorRejectsANonExistentProperty() : void {
  /** @var \Drupal\block_content\BlockContentTypeInterface $entity */
  $entity = BlockContentType::create([
    'id' => 'test',
    'label' => 'Test',
  ]);
  $entity->save();
  $this->assertFalse(property_exists($entity, 'non_existent'));
  $definition = DataDefinition::createFromDataType('entity:block_content_type')->addConstraint('ImmutableProperties', [
    'non_existent',
  ]);
  $this->expectException(LogicException::class);
  $this->expectExceptionMessage("The entity does not have a 'non_existent' property.");
  $violations = $this->container
    ->get(TypedDataManagerInterface::class)
    ->create($definition, $entity)
    ->validate();
}

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