EntityTypeConstraintValidatorTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/Entity/EntityTypeConstraintValidatorTest.php
View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\system\Tests\TypedData;
use Drupal\Core\TypedData\DataDefinition;
class EntityTypeConstraintValidatorTest extends EntityUnitTestBase {
protected $typedData;
public static $modules = array(
'node',
'field',
'user',
);
protected function setUp() {
parent::setUp();
$this->typedData = $this->container
->get('typed_data_manager');
}
public function testValidation() {
$entity_type = 'node';
$definition = DataDefinition::create('entity_reference')
->setConstraints(array(
'EntityType' => $entity_type,
));
$node = $this->container
->get('entity.manager')
->getStorage('node')
->create(array(
'type' => 'page',
));
$typed_data = $this->typedData
->create($definition, $node);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 0, 'Validation passed for correct value.');
$account = $this
->createUser();
$typed_data = $this->typedData
->create($definition, $account);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed for incorrect value.');
$violation = $violations[0];
$this
->assertEqual($violation
->getMessage(), t('The entity must be of type %type.', array(
'%type' => $entity_type,
)), 'The message for invalid value is correct.');
$this
->assertEqual($violation
->getRoot(), $typed_data, 'Violation root is correct.');
$this
->assertEqual($violation
->getInvalidValue(), $account, 'The invalid value is set correctly in the violation.');
}
}