Unverified Commit 81b76e31 authored by alexpott's avatar alexpott

Issue #3168301 by chr.fritsch, phenaproxima: oEmbed validator should use the...

Issue #3168301 by chr.fritsch, phenaproxima: oEmbed validator should use the urlResolver to get the resource URL

(cherry picked from commit e5bef54a)
parent 07716460
...@@ -162,11 +162,17 @@ public function matchUrl($url) { ...@@ -162,11 +162,17 @@ public function matchUrl($url) {
/** /**
* Builds and returns the endpoint 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 * @param string $url
* The canonical media URL. * The canonical media URL.
* *
* @return string * @return string
* URL of the oEmbed endpoint. * URL of the oEmbed endpoint.
*
* @see \Drupal\media\OEmbed\UrlResolver::getResourceUrl()
*/ */
public function buildResourceUrl($url) { public function buildResourceUrl($url) {
$query = ['url' => $url]; $query = ['url' => $url];
......
...@@ -113,8 +113,7 @@ public function validate($value, Constraint $constraint) { ...@@ -113,8 +113,7 @@ public function validate($value, Constraint $constraint) {
// Verify that resource fetching works, because some URLs might match // Verify that resource fetching works, because some URLs might match
// the schemes but don't support oEmbed. // the schemes but don't support oEmbed.
try { try {
$endpoints = $provider->getEndpoints(); $resource_url = $this->urlResolver->getResourceUrl($url);
$resource_url = reset($endpoints)->buildResourceUrl($url);
$this->resourceFetcher->fetchResource($resource_url); $this->resourceFetcher->fetchResource($resource_url);
} }
catch (ResourceException $e) { catch (ResourceException $e) {
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
use Drupal\KernelTests\KernelTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media; use Drupal\media\Entity\Media;
use Drupal\media\OEmbed\Provider;
use Drupal\media\OEmbed\ResourceFetcher;
use Drupal\media\OEmbed\UrlResolverInterface; use Drupal\media\OEmbed\UrlResolverInterface;
use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraint; use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraint;
use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator; use Drupal\media\Plugin\Validation\Constraint\OEmbedResourceConstraintValidator;
...@@ -37,7 +39,7 @@ protected function setUp(): void { ...@@ -37,7 +39,7 @@ protected function setUp(): void {
/** /**
* @covers ::validate * @covers ::validate
*/ */
public function testValidate() { public function testValidateEmptySource() {
$media = Media::create([ $media = Media::create([
'bundle' => $this->createMediaType('oembed:video')->id(), 'bundle' => $this->createMediaType('oembed:video')->id(),
]); ]);
...@@ -52,7 +54,55 @@ public function testValidate() { ...@@ -52,7 +54,55 @@ public function testValidate() {
$url_resolver = $this->prophesize(UrlResolverInterface::class); $url_resolver = $this->prophesize(UrlResolverInterface::class);
$url_resolver->getProviderByUrl(Argument::any())->shouldNotBeCalled(); $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) { public function __construct($entity) {
$this->entity = $entity; $this->entity = $entity;
...@@ -63,14 +113,6 @@ public function getEntity() { ...@@ -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);
} }
} }
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