function EntityBundleExistsConstraintValidatorTest::testDynamicEntityType

Tests getting the entity type ID.

@testWith ["%parent.entity_type_id", "entity_test_with_bundle"] ["%key", "bundle"]

Parameters

string $constraint_value: The entity type ID to supply to the validation constraint. Must be a dynamic token starting with %.

string $resolved_entity_type_id: The actual entity type ID which should be checked for the existence of a bundle.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityBundleExistsConstraintValidatorTest.php, line 81

Class

EntityBundleExistsConstraintValidatorTest
@group Entity @group Validation

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testDynamicEntityType(string $constraint_value, string $resolved_entity_type_id) : void {
    
    /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager */
    $typed_data_manager = $this->container
        ->get('typed_data_manager');
    $this->assertStringStartsWith('%', $constraint_value);
    $value_definition = DataDefinition::create('string')->addConstraint('EntityBundleExists', $constraint_value);
    $parent_definition = MapDataDefinition::create()->setPropertyDefinition('entity_type_id', DataDefinition::create('string'))
        ->setPropertyDefinition('bundle', $value_definition);
    $violations = $typed_data_manager->create($parent_definition, [
        'entity_type_id' => 'entity_test_with_bundle',
        'bundle' => 'bar',
    ])
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame("The 'bar' bundle does not exist on the '{$resolved_entity_type_id}' entity type.", (string) $violations->get(0)
        ->getMessage());
    $this->assertSame('bundle', $violations->get(0)
        ->getPropertyPath());
}

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