class BundleConstraintValidatorTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\BundleConstraintValidatorTest
- 10 core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\BundleConstraintValidatorTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\BundleConstraintValidatorTest
Tests validation constraints for BundleConstraintValidator.
@group Entity
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Entity\BundleConstraintValidatorTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of BundleConstraintValidatorTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ BundleConstraintValidatorTest.php, line 13
Namespace
Drupal\KernelTests\Core\EntityView source
class BundleConstraintValidatorTest extends KernelTestBase {
/**
* The typed data manager to use.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected $typedData;
protected static $modules = [
'node',
'field',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installEntitySchema('user');
$this->typedData = $this->container
->get('typed_data_manager');
}
/**
* Tests bundle constraint validation.
*/
public function testValidation() {
// Test with multiple values.
$this->assertValidation([
'foo',
'bar',
]);
// Test with a single string value as well.
$this->assertValidation('foo');
}
/**
* Executes the BundleConstraintValidator test for a given bundle.
*
* @param string|array $bundle
* Bundle/bundles to use as constraint option.
*
* @internal
*/
protected function assertValidation($bundle) : void {
// Create a typed data definition with a Bundle constraint.
$definition = DataDefinition::create('entity_reference')->addConstraint('Bundle', $bundle);
// Test the validation.
$node = $this->container
->get('entity_type.manager')
->getStorage('node')
->create([
'type' => 'foo',
]);
$typed_data = $this->typedData
->create($definition, $node);
$violations = $typed_data->validate();
$this->assertEquals(0, $violations->count(), 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$page_node = $this->container
->get('entity_type.manager')
->getStorage('node')
->create([
'type' => 'baz',
]);
$typed_data = $this->typedData
->create($definition, $page_node);
$violations = $typed_data->validate();
$this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEquals(t('The entity must be of bundle %bundle.', [
'%bundle' => implode(', ', (array) $bundle),
]), $violation->getMessage(), 'The message for invalid value is correct.');
$this->assertEquals($typed_data, $violation->getRoot(), 'Violation root is correct.');
$this->assertEquals($page_node, $violation->getInvalidValue(), 'The invalid value is set correctly in the violation.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.