Unverified Commit d27318df 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 b49283d6
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -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];
+1 −2
Original line number Diff line number Diff line
@@ -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) {
+52 −10
Original line number Diff line number Diff line
@@ -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() {
  /**
   * @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);
  }

}