View source
<?php
namespace Drupal\KernelTests\Core\TypedData;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
class AllowedValuesConstraintValidatorTest extends KernelTestBase {
protected $typedData;
protected function setUp() {
parent::setUp();
$this->typedData = $this->container
->get('typed_data_manager');
}
public function testValidation() {
$definition = DataDefinition::create('integer')
->addConstraint('AllowedValues', [
1,
2,
3,
]);
$typed_data = $this->typedData
->create($definition, 1);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 0, 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, 4);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed for incorrect value.');
$violation = $violations[0];
$this
->assertEqual($violation
->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.');
$this
->assertEqual($violation
->getRoot(), $typed_data, 'Violation root is correct.');
$this
->assertEqual($violation
->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
$typed_data = $this->typedData
->create($definition, '1');
$violations = $typed_data
->validate();
$this
->assertEquals(0, $violations
->count(), 'Value is coerced to the correct type and is valid.');
}
public function testValidationCallback() {
$definition = DataDefinition::create('string')
->addConstraint('AllowedValues', [
'choices' => [
1,
2,
3,
],
'callback' => [
static::class,
'allowedValueCallback',
],
]);
$typed_data = $this->typedData
->create($definition, 'a');
$violations = $typed_data
->validate();
$this
->assertEquals(0, $violations
->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, 1);
$violations = $typed_data
->validate();
$this
->assertEquals(0, $violations
->count(), 'Validation passed for value that will be cast to the correct type.');
$typed_data = $this->typedData
->create($definition, 2);
$violations = $typed_data
->validate();
$this
->assertEquals(1, $violations
->count(), 'Validation failed for incorrect value.');
$typed_data = $this->typedData
->create($definition, 'd');
$violations = $typed_data
->validate();
$this
->assertEquals(1, $violations
->count(), 'Validation failed for incorrect value.');
$typed_data = $this->typedData
->create($definition, 0);
$violations = $typed_data
->validate();
$this
->assertEquals(1, $violations
->count(), 'Validation failed for incorrect value.');
}
public static function allowedValueCallback() {
return [
'a',
'b',
'c',
'1',
];
}
public function testValidationCallbackException() {
$definition = DataDefinition::create('string')
->addConstraint('AllowedValues', [
'choices' => [
1,
2,
3,
],
'callback' => [
static::class,
'doesNotExist',
],
]);
$typed_data = $this->typedData
->create($definition, 1);
$this
->expectException(ConstraintDefinitionException::class);
$this
->expectExceptionMessage('The AllowedValuesConstraint constraint expects a valid callback');
$typed_data
->validate();
}
}