View source
<?php
namespace Drupal\Tests\media\Unit;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\NullBackend;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcher;
use Drupal\Tests\UnitTestCase;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RequestOptions;
class ResourceFetcherTest extends UnitTestCase {
public function testFetchTimeout() : void {
$url = 'https://example.com/oembed?url=resource';
$headers = [
'Content-Type' => [
'text/javascript',
],
];
$body = Json::encode([
'version' => '1.0',
'type' => 'video',
'html' => 'test',
]);
$response = new Response(200, $headers, $body);
$client = $this
->prophesize(Client::class);
$client
->request('GET', $url, [
RequestOptions::TIMEOUT => 5,
])
->shouldBeCalled()
->willReturn($response);
$fetcher = new ResourceFetcher($client
->reveal(), $this
->createMock('\\Drupal\\media\\OEmbed\\ProviderRepositoryInterface'), new NullBackend('default'));
$fetcher
->fetchResource($url);
}
public function testUnknownContentTypeHeader() : void {
$headers = [
'Content-Type' => [
'text/html',
],
];
$body = Json::encode([
'version' => '1.0',
'type' => 'video',
'html' => 'test',
]);
$valid_response = new Response(200, $headers, $body);
$invalid_response = new Response(200, $headers, rtrim($body, '}'));
$non_array_response = new Response(200, $headers, '"Valid JSON, but not an array..."');
$mock_handler = new MockHandler([
$valid_response,
$invalid_response,
$non_array_response,
]);
$client = new Client([
'handler' => HandlerStack::create($mock_handler),
]);
$providers = $this
->createMock('\\Drupal\\media\\OEmbed\\ProviderRepositoryInterface');
$fetcher = new ResourceFetcher($client, $providers, new NullBackend('default'));
$resource = $fetcher
->fetchResource('valid');
$this
->assertSame('video', $resource
->getType());
$this
->assertSame('test', $resource
->getHtml());
try {
$fetcher
->fetchResource('invalid');
$this
->fail('Expected a ResourceException to be thrown for invalid JSON.');
} catch (ResourceException $e) {
$this
->assertSame('Error decoding oEmbed resource: Syntax error', $e
->getMessage());
}
$this
->expectException(ResourceException::class);
$this
->expectExceptionMessage('The oEmbed resource could not be decoded.');
$fetcher
->fetchResource('non_array');
}
}