Unverified Commit 92608738 authored by alexpott's avatar alexpott
Browse files

Issue #3059950 by phenaproxima, Nebel54, seanB, alexpott: Media sources should...

Issue #3059950 by phenaproxima, Nebel54, seanB, alexpott: Media sources should handle empty source field values more gracefully
parent 479b652a
......@@ -330,8 +330,12 @@ public function getSourceFieldValue(MediaInterface $media) {
throw new \RuntimeException('Source field for media source is not defined.');
}
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
$field_item = $media->get($source_field)->first();
$items = $media->get($source_field);
if ($items->isEmpty()) {
return NULL;
}
$field_item = $items->first();
return $field_item->{$field_item->mainPropertyName()};
}
......
......@@ -185,7 +185,7 @@ public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayIn
* A media item.
*
* @return mixed
* The source value.
* The source value, or NULL if the media item's source field is empty.
*
* @throws \RuntimeException
* If the source field for the media source is not defined.
......
......@@ -83,6 +83,11 @@ public function validate($value, Constraint $constraint) {
throw new \LogicException('Media source must implement ' . OEmbedInterface::class);
}
$url = $source->getSourceFieldValue($media);
// The URL may be NULL if the source field is empty, which is invalid input.
if (empty($url)) {
$this->context->addViolation($constraint->invalidResourceMessage);
return;
}
// Ensure that the URL matches a provider.
try {
......
......@@ -216,6 +216,11 @@ public function getMetadataAttributes() {
*/
public function getMetadata(MediaInterface $media, $name) {
$media_url = $this->getSourceFieldValue($media);
// The URL may be NULL if the source field is empty, in which case just
// return NULL.
if (empty($media_url)) {
return NULL;
}
try {
$resource_url = $this->urlResolver->getResourceUrl($media_url);
......
......@@ -218,6 +218,10 @@ public function testGetSourceFieldValue() {
$media->save();
$media_source = $media->getSource();
$this->assertSame('some_value', $media_source->getSourceFieldValue($media));
// Test that NULL is returned if there is no value in the source field.
$media->set('field_media_test', NULL)->save();
$this->assertNull($media_source->getSourceFieldValue($media));
}
/**
......
<?php
namespace Drupal\Tests\media\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\media\OEmbed\UrlResolverInterface;
use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraint;
use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Prophecy\Argument;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @coversDefaultClass \Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator
*
* @group media
*/
class OEmbedResourceConstraintValidatorTest extends KernelTestBase {
use MediaTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['field', 'file', 'image', 'media', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('file');
$this->installEntitySchema('user');
}
/**
* @covers ::validate
*/
public function testValidate() {
$media = Media::create([
'bundle' => $this->createMediaType('oembed:video')->id(),
]);
$constraint = new OEmbedResourceConstraint();
// The media item has an empty source value, so the constraint validator
// should add a violation and return early before invoking the URL resolver.
$context = $this->prophesize(ExecutionContextInterface::class);
$context->addViolation($constraint->invalidResourceMessage)->shouldBeCalled();
$url_resolver = $this->prophesize(UrlResolverInterface::class);
$url_resolver->getProviderByUrl(Argument::any())->shouldNotBeCalled();
$value = new class ($media) {
public function __construct($entity) {
$this->entity = $entity;
}
public function getEntity() {
return $this->entity;
}
};
$validator = new OEmbedResourceConstraintValidator(
$url_resolver->reveal(),
$this->container->get('media.oembed.resource_fetcher'),
$this->container->get('logger.factory')
);
$validator->initialize($context->reveal());
$validator->validate($value, $constraint);
}
}
<?php
namespace Drupal\Tests\media\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Plugin\media\Source\OEmbed;
/**
* @coversDefaultClass \Drupal\media\Plugin\media\Source\OEmbed
*
* @group media
*/
class OEmbedSourceTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['media'];
/**
* @covers ::getMetadata
*/
public function testGetMetadata() {
$configuration = [
'source_field' => 'field_test_oembed',
];
$plugin = OEmbed::create($this->container, $configuration, 'oembed', []);
// Test that NULL is returned for a media item with no source value.
$media = $this->prophesize('\Drupal\media\MediaInterface');
$field_items = $this->prophesize('\Drupal\Core\Field\FieldItemListInterface');
$field_items->isEmpty()->willReturn(TRUE);
$media->get($configuration['source_field'])->willReturn($field_items->reveal());
$this->assertNull($plugin->getMetadata($media->reveal(), 'type'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment