diff --git a/core/modules/media/src/OEmbed/Endpoint.php b/core/modules/media/src/OEmbed/Endpoint.php index 38d265d5bd782cb353b8c27fa3bf58edb567834d..97f97e2815fc1ad4a421dde3c7c7da2147bf4240 100644 --- a/core/modules/media/src/OEmbed/Endpoint.php +++ b/core/modules/media/src/OEmbed/Endpoint.php @@ -162,11 +162,17 @@ public function matchUrl($url) { /** * Builds and returns the endpoint URL. * + * In most situations this function should not be used. Your are probably + * looking for \Drupal\media\OEmbed\UrlResolver::getResourceUrl(), because it + * is alterable and also cached. + * * @param string $url * The canonical media URL. * * @return string * URL of the oEmbed endpoint. + * + * @see \Drupal\media\OEmbed\UrlResolver::getResourceUrl() */ public function buildResourceUrl($url) { $query = ['url' => $url]; diff --git a/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php b/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php index 3b885cce8ffa0b4c76d3be2541551c769ee2535f..e39b2055cc7da808211ce91ec4ff26c7a0db0cac 100644 --- a/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php +++ b/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php @@ -113,8 +113,7 @@ public function validate($value, Constraint $constraint) { // Verify that resource fetching works, because some URLs might match // the schemes but don't support oEmbed. try { - $endpoints = $provider->getEndpoints(); - $resource_url = reset($endpoints)->buildResourceUrl($url); + $resource_url = $this->urlResolver->getResourceUrl($url); $this->resourceFetcher->fetchResource($resource_url); } catch (ResourceException $e) { diff --git a/core/modules/media/tests/src/Kernel/OEmbedResourceConstraintValidatorTest.php b/core/modules/media/tests/src/Kernel/OEmbedResourceConstraintValidatorTest.php index c18651efba896b27dac77e924c1babfdf284aecf..1ec413ca79263d7fedb5d56e137e0eb657624218 100644 --- a/core/modules/media/tests/src/Kernel/OEmbedResourceConstraintValidatorTest.php +++ b/core/modules/media/tests/src/Kernel/OEmbedResourceConstraintValidatorTest.php @@ -4,6 +4,8 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\media\Entity\Media; +use Drupal\media\OEmbed\Provider; +use Drupal\media\OEmbed\ResourceFetcher; use Drupal\media\OEmbed\UrlResolverInterface; use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraint; use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator; @@ -37,7 +39,7 @@ protected function setUp(): void { /** * @covers ::validate */ - public function testValidate() { + public function testValidateEmptySource() { $media = Media::create([ 'bundle' => $this->createMediaType('oembed:video')->id(), ]); @@ -52,7 +54,55 @@ public function testValidate() { $url_resolver = $this->prophesize(UrlResolverInterface::class); $url_resolver->getProviderByUrl(Argument::any())->shouldNotBeCalled(); - $value = new class ($media) { + $validator = new OEmbedResourceConstraintValidator( + $url_resolver->reveal(), + $this->container->get('media.oembed.resource_fetcher'), + $this->container->get('logger.factory') + ); + $validator->initialize($context->reveal()); + $validator->validate($this->getValue($media), $constraint); + } + + /** + * @covers ::validate + */ + public function testValidateUrlResolverInvoked() { + $media = Media::create([ + 'bundle' => $this->createMediaType('oembed:video')->id(), + 'field_media_oembed_video' => 'source value', + ]); + + $constraint = new OEmbedResourceConstraint(); + + $context = $this->prophesize(ExecutionContextInterface::class); + + $provider = $this->prophesize(Provider::class); + $provider->getName()->willReturn('YouTube'); + + $url_resolver = $this->prophesize(UrlResolverInterface::class); + $url_resolver->getProviderByUrl(Argument::any())->willReturn($provider->reveal()); + $url_resolver->getResourceUrl(Argument::any())->shouldBeCalledOnce(); + + $validator = new OEmbedResourceConstraintValidator( + $url_resolver->reveal(), + $this->prophesize(ResourceFetcher::class)->reveal(), + $this->container->get('logger.factory') + ); + $validator->initialize($context->reveal()); + $validator->validate($this->getValue($media), $constraint); + } + + /** + * Wraps a media entity in an anonymous class to mock a field value. + * + * @param \Drupal\media\Entity\Media $media + * The media object. + * + * @return object + * The mock field value to validate. + */ + protected function getValue(Media $media) { + return new class ($media) { public function __construct($entity) { $this->entity = $entity; @@ -63,14 +113,6 @@ public function getEntity() { } }; - - $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); } }