Loading core/modules/media/src/OEmbed/Endpoint.php +6 −0 Original line number Diff line number Diff line Loading @@ -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]; Loading core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php +1 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading core/modules/media/tests/src/Kernel/OEmbedResourceConstraintValidatorTest.php +52 −10 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -37,7 +39,7 @@ protected function setUp() { /** * @covers ::validate */ public function testValidate() { public function testValidateEmptySource() { $media = Media::create([ 'bundle' => $this->createMediaType('oembed:video')->id(), ]); Loading @@ -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; Loading @@ -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); } } Loading
core/modules/media/src/OEmbed/Endpoint.php +6 −0 Original line number Diff line number Diff line Loading @@ -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]; Loading
core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraintValidator.php +1 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading
core/modules/media/tests/src/Kernel/OEmbedResourceConstraintValidatorTest.php +52 −10 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -37,7 +39,7 @@ protected function setUp() { /** * @covers ::validate */ public function testValidate() { public function testValidateEmptySource() { $media = Media::create([ 'bundle' => $this->createMediaType('oembed:video')->id(), ]); Loading @@ -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; Loading @@ -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); } }