3 namespace Drupal\Tests\media\Traits;
5 use Drupal\Component\Serialization\Json;
7 use Drupal\media\OEmbed\Provider;
10 * Contains helper functions for testing oEmbed functionality in isolation.
12 trait OEmbedTestTrait {
15 * Returns the relative path to the oEmbed fixtures directory.
19 protected function getFixturesDirectory() {
20 return drupal_get_path('module', 'media') . '/tests/fixtures/oembed';
24 * Returns the absolute URL of the oEmbed fixtures directory.
28 protected function getFixturesUrl() {
29 return $this->baseUrl . '/' . $this->getFixturesDirectory();
33 * Forces Media to use the provider database in the fixtures directory.
35 protected function useFixtureProviders() {
36 $this->config('media.settings')
37 ->set('oembed_providers_url', $this->getFixturesUrl() . '/providers.json')
42 * Configures the http_client service so that all requests are carried out
43 * relative to the URL of the fixtures directory. For example, after calling
44 * this method, a request for foobar.html will actually request
45 * http://test-site/path/to/fuxtures/foobar.html.
47 protected function lockHttpClientToFixtures() {
48 $this->writeSettings([
50 'http_client_config' => [
51 'base_uri' => (object) [
52 'value' => $this->getFixturesUrl() . '/',
61 * Ensures that all oEmbed provider endpoints defined in the fixture
62 * providers.json will use the media_test_oembed.resource.get route as their
65 * This requires the media_test_oembed module in order to work.
67 protected function hijackProviderEndpoints() {
68 $providers = $this->getFixturesDirectory() . '/providers.json';
69 $providers = file_get_contents($providers);
70 $providers = Json::decode($providers);
72 $endpoint_url = Url::fromRoute('media_test_oembed.resource.get')
76 /** @var \Drupal\media_test_oembed\ProviderRepository $provider_repository */
77 $provider_repository = $this->container->get('media.oembed.provider_repository');
79 foreach ($providers as &$provider) {
80 foreach ($provider['endpoints'] as &$endpoint) {
81 $endpoint['url'] = $endpoint_url;
83 $provider_repository->setProvider(
84 new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints'])