View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\simpletest\KernelTestBase;
use Drupal\system\Tests\TypedData;
class BundleConstraintValidatorTest extends KernelTestBase {
protected $typedData;
public static $modules = array(
'node',
'field',
'text',
'user',
);
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this->typedData = $this->container
->get('typed_data_manager');
}
public function testValidation() {
$this
->assertValidation(array(
'foo',
'bar',
));
$this
->assertValidation('foo');
}
protected function assertValidation($bundle) {
$definition = DataDefinition::create('entity_reference')
->addConstraint('Bundle', $bundle);
$node = $this->container
->get('entity.manager')
->getStorage('node')
->create(array(
'type' => 'foo',
));
$typed_data = $this->typedData
->create($definition, $node);
$violations = $typed_data
->validate();
$this
->assertEqual($violations
->count(), 0, 'Validation passed for correct value.');
$page_node = $this->container
->get('entity.manager')
->getStorage('node')
->create(array(
'type' => 'baz',
));
$typed_data = $this->typedData
->create($definition, $page_node);
$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 bundle %bundle.', array(
'%bundle' => implode(', ', (array) $bundle),
)), 'The message for invalid value is correct.');
$this
->assertEqual($violation
->getRoot(), $typed_data, 'Violation root is correct.');
$this
->assertEqual($violation
->getInvalidValue(), $page_node, 'The invalid value is set correctly in the violation.');
}
}