function MediaSourceTest::testConstraints
Same name in other branches
- 9 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testConstraints()
- 8.9.x core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testConstraints()
- 10 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testConstraints()
Tests the media item constraints functionality.
File
-
core/
modules/ media/ tests/ src/ Kernel/ MediaSourceTest.php, line 389
Class
- MediaSourceTest
- Tests media source plugins related logic.
Namespace
Drupal\Tests\media\KernelCode
public function testConstraints() : void {
// Test entity constraints.
\Drupal::state()->set('media_source_test_entity_constraints', [
'MediaTestConstraint' => [],
]);
// Create a media item media that uses a source plugin with constraints and
// make sure the constraints works as expected when validating.
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
'bundle' => $this->testConstraintsMediaType
->id(),
'name' => 'I do not like Drupal',
'field_media_test_constraints' => 'Not checked',
]);
// Validate the entity and make sure violation is reported.
/** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
$violations = $media->validate();
$this->assertCount(1, $violations, 'Expected number of validations not found.');
$this->assertEquals('Inappropriate text.', $violations->get(0)
->getMessage(), 'Incorrect constraint validation message found.');
// Fix the violation and make sure it is not reported anymore.
$media->setName('I love Drupal!');
$violations = $media->validate();
$this->assertCount(0, $violations, 'Expected number of validations not found.');
// Save and make sure it succeeded.
$this->assertEmpty($media->id(), 'Entity ID was found.');
$media->save();
$this->assertNotEmpty($media->id(), 'Entity ID was not found.');
$this->assertSame($media->getName(), 'I love Drupal!');
// Test source field constraints.
\Drupal::state()->set('media_source_test_field_constraints', [
'MediaTestConstraint' => [],
]);
\Drupal::state()->set('media_source_test_entity_constraints', []);
// Create media that uses source with constraints and make sure it can't
// be saved without validating them.
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
'bundle' => $this->testConstraintsMediaType
->id(),
'name' => 'Not checked',
'field_media_test_constraints' => 'I do not like Drupal',
]);
// Validate the entity and make sure violation is reported.
/** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
$violations = $media->validate();
$this->assertCount(1, $violations, 'Expected number of validations not found.');
$this->assertEquals('Inappropriate text.', $violations->get(0)
->getMessage(), 'Incorrect constraint validation message found.');
// Fix the violation and make sure it is not reported anymore.
$media->set('field_media_test_constraints', 'I love Drupal!');
$violations = $media->validate();
$this->assertCount(0, $violations, 'Expected number of validations not found.');
// Save and make sure it succeeded.
$this->assertEmpty($media->id(), 'Entity ID was found.');
$media->save();
$this->assertNotEmpty($media->id(), 'Entity ID was not found.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.