OEmbedSourceTest.php in Drupal 8        
                          
                  
                        
  
  
  
  
File
  core/modules/media/tests/src/Kernel/OEmbedSourceTest.php
  
    View source  
  <?php
namespace Drupal\Tests\media\Kernel;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\media\OEmbed\Resource;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Drupal\media\Plugin\media\Source\OEmbed;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
class OEmbedSourceTest extends MediaKernelTestBase {
  
  public function testGetMetadata() {
    $configuration = [
      'source_field' => 'field_test_oembed',
    ];
    $plugin = OEmbed::create($this->container, $configuration, 'oembed', []);
    
    $media = $this
      ->prophesize('\\Drupal\\media\\MediaInterface');
    $field_items = $this
      ->prophesize('\\Drupal\\Core\\Field\\FieldItemListInterface');
    $field_items
      ->isEmpty()
      ->willReturn(TRUE);
    $media
      ->get($configuration['source_field'])
      ->willReturn($field_items
      ->reveal());
    $this
      ->assertNull($plugin
      ->getMetadata($media
      ->reveal(), 'type'));
  }
  
  public function testLocalThumbnailUriQueryStringIsIgnored() {
    
    $this->container
      ->set('media.oembed.url_resolver', $this
      ->prophesize(UrlResolverInterface::class)
      ->reveal());
    $thumbnail_url = Url::fromUri('internal:/core/misc/druplicon.png?foo=bar');
    
    $resource = $this
      ->prophesize(Resource::class);
    $resource
      ->getTitle()
      ->willReturn('Test resource');
    $resource
      ->getThumbnailUrl()
      ->willReturn($thumbnail_url);
    
    $http_client = $this
      ->prophesize(Client::class);
    $http_client
      ->get(Argument::type('string'))
      ->willReturn(new Response());
    $this->container
      ->set('http_client', $http_client
      ->reveal());
    
    $resource_fetcher = $this
      ->prophesize(ResourceFetcherInterface::class);
    $resource_fetcher
      ->fetchResource(NULL)
      ->willReturn($resource
      ->reveal());
    $this->container
      ->set('media.oembed.resource_fetcher', $resource_fetcher
      ->reveal());
    $media_type = $this
      ->createMediaType('oembed:video');
    $source = $media_type
      ->getSource();
    $media = Media::create([
      'bundle' => $media_type
        ->id(),
      $source
        ->getSourceFieldDefinition($media_type)
        ->getName() => $this
        ->randomString(),
    ]);
    $media
      ->save();
    
    $local_thumbnail_uri = $media_type
      ->getSource()
      ->getMetadata($media, 'thumbnail_uri');
    $expected_uri = 'public://oembed_thumbnails/' . Crypt::hashBase64('/core/misc/druplicon.png') . '.png';
    $this
      ->assertSame($expected_uri, $local_thumbnail_uri);
  }
}