public function MediaSourceTest::testConstraints in Drupal 8
Same name and namespace in other branches
- 9 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 361
Class
- MediaSourceTest
- Tests media source plugins related logic.
Namespace
Drupal\Tests\media\KernelCode
public function testConstraints() {
// 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.');
}