trait OEmbedTestTrait

Same name in other branches
  1. 8.9.x core/modules/media/tests/src/Traits/OEmbedTestTrait.php \Drupal\Tests\media\Traits\OEmbedTestTrait
  2. 10 core/modules/media/tests/src/Traits/OEmbedTestTrait.php \Drupal\Tests\media\Traits\OEmbedTestTrait
  3. 11.x core/modules/media/tests/src/Traits/OEmbedTestTrait.php \Drupal\Tests\media\Traits\OEmbedTestTrait

Contains helper functions for testing oEmbed functionality in isolation.

Hierarchy

6 files declare their use of OEmbedTestTrait
MediaSourceOEmbedVideoTest.php in core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
MediaStandardProfileTest.php in core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
OEmbedFormatterTest.php in core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
ResourceFetcherTest.php in core/modules/media/tests/src/Functional/ResourceFetcherTest.php
UrlResolverTest.php in core/modules/media/tests/src/Functional/UrlResolverTest.php

... See full list

File

core/modules/media/tests/src/Traits/OEmbedTestTrait.php, line 12

Namespace

Drupal\Tests\media\Traits
View source
trait OEmbedTestTrait {
    
    /**
     * Returns the relative path to the oEmbed fixtures directory.
     *
     * @return string
     */
    protected function getFixturesDirectory() {
        return \Drupal::service('extension.list.module')->getPath('media') . '/tests/fixtures/oembed';
    }
    
    /**
     * Returns the absolute URL of the oEmbed fixtures directory.
     *
     * @return string
     */
    protected function getFixturesUrl() {
        return $this->baseUrl . '/' . $this->getFixturesDirectory();
    }
    
    /**
     * Forces Media to use the provider database in the fixtures directory.
     */
    protected function useFixtureProviders() {
        $this->config('media.settings')
            ->set('oembed_providers_url', $this->getFixturesUrl() . '/providers.json')
            ->save();
    }
    
    /**
     * Configures the HTTP client to always use the fixtures directory.
     *
     * All requests are carried out relative to the URL of the fixtures directory.
     * For example, after calling this method, a request for foobar.html will
     * actually request http://test-site/path/to/fixtures/foobar.html.
     */
    protected function lockHttpClientToFixtures() {
        $this->writeSettings([
            'settings' => [
                'http_client_config' => [
                    'base_uri' => (object) [
                        'value' => $this->getFixturesUrl() . '/',
                        'required' => TRUE,
                    ],
                ],
            ],
        ]);
        // Rebuild the container in case there is already an instantiated service
        // that has a dependency on the http_client service.
        $this->container
            ->get('kernel')
            ->rebuildContainer();
        $this->container = $this->container
            ->get('kernel')
            ->getContainer();
    }
    
    /**
     * Ensures that oEmbed provider endpoints use the test resource route.
     *
     * All oEmbed provider endpoints defined in the fixture providers.json will
     * use the media_test_oembed.resource.get route as their URL.
     *
     * This requires the media_test_oembed module in order to work.
     */
    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();
        
        /** @var \Drupal\media_test_oembed\ProviderRepository $provider_repository */
        $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']));
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
OEmbedTestTrait::getFixturesDirectory protected function Returns the relative path to the oEmbed fixtures directory.
OEmbedTestTrait::getFixturesUrl protected function Returns the absolute URL of the oEmbed fixtures directory.
OEmbedTestTrait::hijackProviderEndpoints protected function Ensures that oEmbed provider endpoints use the test resource route.
OEmbedTestTrait::lockHttpClientToFixtures protected function Configures the HTTP client to always use the fixtures directory.
OEmbedTestTrait::useFixtureProviders protected function Forces Media to use the provider database in the fixtures directory.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.