function OEmbedSourceTest::testThumbnailRejected
Same name and namespace in other branches
- main core/modules/media/tests/src/Kernel/OEmbedSourceTest.php \Drupal\Tests\media\Kernel\OEmbedSourceTest::testThumbnailRejected()
Tests that unsafe or invalid remote thumbnails are rejected.
Attributes
#[TestWith([
'internal:/malicious.php',
'application/x-php',
'Could not determine a supported image extension for the remote thumbnail at @url.',
])]
#[TestWith([
'internal:/imposter.png',
'image/png',
'The remote thumbnail at @url is not a valid image.',
])]
Parameters
string $remote_thumbnail_url: The URL of the remote thumbnail.
string $content_type: The Content-Type header to return with the response.
string $expected_warning: The warning message that should be logged.
File
-
core/
modules/ media/ tests/ src/ Kernel/ OEmbedSourceTest.php, line 238
Class
Namespace
Drupal\Tests\media\KernelCode
public function testThumbnailRejected(string $remote_thumbnail_url, string $content_type, string $expected_warning) : void {
$resource = Resource::rich('<html></html>', 16, 16, NULL, 'Test resource', NULL, NULL, NULL, $remote_thumbnail_url, 16, 16);
$this->container
->set('media.oembed.url_resolver', $this->prophesize(UrlResolverInterface::class)
->reveal());
$resource_fetcher = $this->prophesize(ResourceFetcherInterface::class);
$resource_fetcher->fetchResource(Argument::any())
->willReturn($resource);
$this->container
->set('media.oembed.resource_fetcher', $resource_fetcher->reveal());
$response = new Response(200, [
'Content-Type' => [
$content_type,
],
], "<?php echo 'pwned'; ?>");
$client = new Client([
'handler' => new HandlerStack(new MockHandler([
$response,
])),
]);
$this->container
->set('http_client', $client);
// Capture log messages so the test can assert on them.
$this->container
->get('logger.factory')
->addLogger($this);
$media_type = $this->createMediaType('oembed:video');
/** @var \Drupal\media\Plugin\media\Source\OEmbed $source */
$source = $media_type->getSource();
$media_type->save();
$media = Media::create([
'bundle' => $media_type->id(),
$source->getSourceFieldDefinition($media_type)
->getName() => $this->randomString(),
]);
$media->save();
// The source should fall back to the default thumbnail URI.
$default_filename = $source->getPluginDefinition()['default_thumbnail_filename'];
$default_uri = $this->config('media.settings')
->get('icon_base_uri') . '/' . $default_filename;
$this->assertSame($default_uri, $media->thumbnail->entity
->getFileUri());
// No file should remain in the thumbnails directory: either it was never
// saved, or it was saved and then deleted after toolkit validation failed.
$date = date('Y-m', $this->container
->get('datetime.time')
->getRequestTime());
$this->assertEmpty($this->container
->get('file_system')
->scanDirectory("public://oembed_thumbnails/{$date}", '/.*/'));
$this->assertContains($expected_warning, $this->logMessages[RfcLogLevel::WARNING] ?? []);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.