OEmbedTestTrait.php in Drupal 8
File
core/modules/media/tests/src/Traits/OEmbedTestTrait.php
View source
<?php
namespace Drupal\Tests\media\Traits;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\media\OEmbed\Provider;
trait OEmbedTestTrait {
protected function getFixturesDirectory() {
return drupal_get_path('module', 'media') . '/tests/fixtures/oembed';
}
protected function getFixturesUrl() {
return $this->baseUrl . '/' . $this
->getFixturesDirectory();
}
protected function useFixtureProviders() {
$this
->config('media.settings')
->set('oembed_providers_url', $this
->getFixturesUrl() . '/providers.json')
->save();
}
protected function lockHttpClientToFixtures() {
$this
->writeSettings([
'settings' => [
'http_client_config' => [
'base_uri' => (object) [
'value' => $this
->getFixturesUrl() . '/',
'required' => TRUE,
],
],
],
]);
}
protected function hijackProviderEndpoints() {
$providers = $this
->getFixturesDirectory() . '/providers.json';
$providers = file_get_contents($providers);
$providers = Json::decode($providers);
$endpoint_url = Url::fromRoute('media_test_oembed.resource.get')
->setAbsolute()
->toString();
$provider_repository = $this->container
->get('media.oembed.provider_repository');
foreach ($providers as &$provider) {
foreach ($provider['endpoints'] as &$endpoint) {
$endpoint['url'] = $endpoint_url;
}
$provider_repository
->setProvider(new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']));
}
}
}
Traits
Name |
Description |
OEmbedTestTrait |
Contains helper functions for testing oEmbed functionality in isolation. |