ValidReferenceConstraintValidatorTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/Entity/ValidReferenceConstraintValidatorTest.php
View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\system\Tests\TypedData;
class ValidReferenceConstraintValidatorTest extends EntityUnitTestBase {
protected $typedData;
public static $modules = array(
'field',
'user',
);
public function setUp() {
parent::setUp();
$this
->installSchema('user', array(
'users_data',
));
$this->typedData = $this->container
->get('typed_data_manager');
}
public function testValidation() {
$entity = $this
->createUser();
$definition = BaseFieldDefinition::create('entity_reference')
->setSettings(array(
'target_type' => 'user',
));
$typed_data = $this->typedData
->create($definition, array(
'target_id' => $entity
->id(),
));
$violations = $typed_data
->validate();
$this
->assertFalse($violations
->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, array(
'target_id' => NULL,
));
$violations = $typed_data
->validate();
$this
->assertFalse($violations
->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, array(
'target_id' => $entity
->id(),
));
$entity
->delete();
$violations = $typed_data
->validate();
$this
->assertTrue($violations
->count(), 'Validation failed for incorrect value.');
$violation = $violations[0];
$this
->assertEqual($violation
->getMessage(), t('The referenced entity (%type: %id) does not exist.', array(
'%type' => 'user',
'%id' => $entity
->id(),
)), 'The message for invalid value is correct.');
$this
->assertEqual($violation
->getRoot(), $typed_data, 'Violation root is correct.');
}
}