function ImmutablePropertiesConstraintValidatorTest::testImmutablePropertyCannotBeChanged
Same name in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Entity/ImmutablePropertiesConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\ImmutablePropertiesConstraintValidatorTest::testImmutablePropertyCannotBeChanged()
Tests that changing a config entity's immutable property raises an error.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ ImmutablePropertiesConstraintValidatorTest.php, line 85
Class
- ImmutablePropertiesConstraintValidatorTest
- @group Entity @group Validation
Namespace
Drupal\KernelTests\Core\EntityCode
public function testImmutablePropertyCannotBeChanged() : void {
/** @var \Drupal\block_content\BlockContentTypeInterface $entity */
$entity = BlockContentType::create([
'id' => 'test',
'label' => 'Test',
]);
$entity->save();
$definition = DataDefinition::createFromDataType('entity:block_content_type')->addConstraint('ImmutableProperties', [
'id',
'description',
]);
/** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager */
$typed_data_manager = $this->container
->get(TypedDataManagerInterface::class);
// Try changing one immutable property, and one mutable property.
$entity->set('id', 'foo')
->set('label', 'Testing!');
$violations = $typed_data_manager->create($definition, $entity)
->validate();
$this->assertCount(1, $violations);
$this->assertSame("The 'id' property cannot be changed.", (string) $violations[0]->getMessage());
// Ensure we get multiple violations if more than one immutable property is
// changed.
$entity->set('description', "From hell's heart, I describe thee!");
$violations = $typed_data_manager->create($definition, $entity)
->validate();
$this->assertCount(2, $violations);
$this->assertSame("The 'id' property cannot be changed.", (string) $violations[0]->getMessage());
$this->assertSame("The 'description' property cannot be changed.", (string) $violations[1]->getMessage());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.