Skip to content
Snippets Groups Projects
Unverified Commit 81b76e31 authored by Alex Pott's avatar Alex Pott
Browse files

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
No related branches found
No related tags found
No related merge requests found
......@@ -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];
......
......@@ -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) {
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment