Same filename and directory in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php
Namespace
Drupal\KernelTests\Core\Entity
File
core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\TypedData\DataDefinition;
class EntityTypeConstraintValidatorTest extends EntityKernelTestBase {
protected $typedData;
protected static $modules = [
'node',
'field',
'user',
];
protected function setUp() : void {
parent::setUp();
$this->typedData = $this->container
->get('typed_data_manager');
}
public function testValidation() {
$entity_type = 'node';
$definition = DataDefinition::create('entity_reference')
->setConstraints([
'EntityType' => $entity_type,
]);
$node = $this->container
->get('entity_type.manager')
->getStorage('node')
->create([
'type' => 'page',
]);
$typed_data = $this->typedData
->create($definition, $node);
$violations = $typed_data
->validate();
$this
->assertEquals(0, $violations
->count(), 'Validation passed for correct value.');
$account = $this
->createUser();
$typed_data = $this->typedData
->create($definition, $account);
$violations = $typed_data
->validate();
$this
->assertEquals(1, $violations
->count(), 'Validation failed for incorrect value.');
$violation = $violations[0];
$this
->assertEquals(sprintf('The entity must be of type %s.', $entity_type), $violation
->getMessage(), 'The message for invalid value is correct.');
$this
->assertEquals($typed_data, $violation
->getRoot(), 'Violation root is correct.');
$this
->assertEquals($account, $violation
->getInvalidValue(), 'The invalid value is set correctly in the violation.');
}
}
Classes