function MediaSourceValidationTest::testValidation
Tests existing validation constraints are respected by Media::validate.
@legacy-covers \Drupal\media\Entity\Media::validate
File
-
core/
modules/ media/ tests/ src/ Kernel/ MediaSourceValidationTest.php, line 100
Class
- MediaSourceValidationTest
- Tests media validation.
Namespace
Drupal\Tests\media\KernelCode
public function testValidation() : void {
$history = [];
$this->container
->set('http_client', $this->mockClient($history, new Response(body: \file_get_contents(dirname(__DIR__, 2) . '/fixtures/oembed/providers.json')), new Response(body: \file_get_contents(dirname(__DIR__, 2) . '/fixtures/oembed/video_youtube.json')), new Response(body: \file_get_contents(dirname(__DIR__, 2) . '/fixtures/oembed/video_youtube.json'))));
// Add an allowed video.
$media = Media::create([
'bundle' => $this->mediaType
->id(),
$this->fieldName => 'https://www.youtube.com/watch?v=15Nqbic6HZs',
'name' => $this->randomMachineName(),
]);
self::assertCount(0, $media->validate());
// Save this item so we can test the UniqueField constraint later.
$media->save();
// Add a disallowed video.
$media = Media::create([
'bundle' => $this->mediaType
->id(),
$this->fieldName => 'https://www.youtube.com/watch?v=9qbRHY1l0vc',
'name' => $this->randomMachineName(),
]);
$violations = $media->validate();
self::assertCount(1, $violations);
self::assertEquals($this->fieldName . '.0.value', $violations->get(0)
->getPropertyPath());
self::assertEquals('This site only allows Jazz videos, try again cat 🎷', (string) $violations->get(0)
->getMessage());
// Add an allowed video with an existing URL.
// This should trigger the UniqueField constraint.
$media = Media::create([
'bundle' => $this->mediaType
->id(),
$this->fieldName => 'https://www.youtube.com/watch?v=15Nqbic6HZs',
'name' => $this->randomMachineName(),
]);
$violations = $media->validate();
self::assertCount(1, $violations);
self::assertEquals($this->fieldName, $violations->get(0)
->getPropertyPath());
self::assertEquals('A media item with Remote video URL <em class="placeholder">https://www.youtube.com/watch?v=15Nqbic6HZs</em> already exists.', (string) $violations->get(0)
->getMessage());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.