Skip to content
Snippets Groups Projects
Verified Commit dd92aad4 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3071760 by phenaproxima, vijaycs85, catch, nils.destoop, longwave,...

Issue #3071760 by phenaproxima, vijaycs85, catch, nils.destoop, longwave, alexpott: oEmbed system does not remove query strings from local thumbnail filenames

(cherry picked from commit 9800208d)
parent 934da7a3
No related branches found
No related tags found
No related merge requests found
......@@ -390,10 +390,14 @@ protected function getLocalThumbnailUri(Resource $resource) {
}
$remote_thumbnail_url = $remote_thumbnail_url->toString();
// Remove the query string, since we do not want to include it in the local
// thumbnail URI.
$local_thumbnail_url = parse_url($remote_thumbnail_url, PHP_URL_PATH);
// Compute the local thumbnail URI, regardless of whether or not it exists.
$configuration = $this->getConfiguration();
$directory = $configuration['thumbnails_directory'];
$local_thumbnail_uri = "$directory/" . Crypt::hashBase64($remote_thumbnail_url) . '.' . pathinfo($remote_thumbnail_url, PATHINFO_EXTENSION);
$local_thumbnail_uri = "$directory/" . Crypt::hashBase64($local_thumbnail_url) . '.' . pathinfo($local_thumbnail_url, PATHINFO_EXTENSION);
// If the local thumbnail already exists, return its URI.
if (file_exists($local_thumbnail_uri)) {
......
......@@ -2,15 +2,23 @@
namespace Drupal\Tests\media\Kernel;
use Drupal\KernelTests\KernelTestBase;
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;
/**
* @coversDefaultClass \Drupal\media\Plugin\media\Source\OEmbed
*
* @group media
*/
class OEmbedSourceTest extends KernelTestBase {
class OEmbedSourceTest extends MediaKernelTestBase {
/**
* {@inheritdoc}
......@@ -34,4 +42,49 @@ public function testGetMetadata() {
$this->assertNull($plugin->getMetadata($media->reveal(), 'type'));
}
/**
* @covers ::getLocalThumbnailUri
*/
public function testLocalThumbnailUriQueryStringIsIgnored() {
// There's no need to resolve the resource URL in this test; we just need
// to fetch the resource.
$this->container->set(
'media.oembed.url_resolver',
$this->prophesize(UrlResolverInterface::class)->reveal()
);
$thumbnail_url = Url::fromUri('internal:/core/misc/druplicon.png?foo=bar');
// Create a mocked resource whose thumbnail URL contains a query string.
$resource = $this->prophesize(Resource::class);
$resource->getTitle()->willReturn('Test resource');
$resource->getThumbnailUrl()->willReturn($thumbnail_url);
// The source plugin will try to fetch the remote thumbnail, so mock the
// HTTP client to ensure that request returns an empty "OK" response.
$http_client = $this->prophesize(Client::class);
$http_client->get(Argument::type('string'))->willReturn(new Response());
$this->container->set('http_client', $http_client->reveal());
// Mock the resource fetcher so that it will return our mocked resource.
$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();
// Get the local thumbnail URI and ensure that it does not contain any
// query string.
$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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment