Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / media / tests / src / Traits / OEmbedTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\media\Traits;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Url;
7 use Drupal\media\OEmbed\Provider;
8
9 /**
10  * Contains helper functions for testing oEmbed functionality in isolation.
11  */
12 trait OEmbedTestTrait {
13
14   /**
15    * Returns the relative path to the oEmbed fixtures directory.
16    *
17    * @return string
18    */
19   protected function getFixturesDirectory() {
20     return drupal_get_path('module', 'media') . '/tests/fixtures/oembed';
21   }
22
23   /**
24    * Returns the absolute URL of the oEmbed fixtures directory.
25    *
26    * @return string
27    */
28   protected function getFixturesUrl() {
29     return $this->baseUrl . '/' . $this->getFixturesDirectory();
30   }
31
32   /**
33    * Forces Media to use the provider database in the fixtures directory.
34    */
35   protected function useFixtureProviders() {
36     $this->config('media.settings')
37       ->set('oembed_providers_url', $this->getFixturesUrl() . '/providers.json')
38       ->save();
39   }
40
41   /**
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.
46    */
47   protected function lockHttpClientToFixtures() {
48     $this->writeSettings([
49       'settings' => [
50         'http_client_config' => [
51           'base_uri' => (object) [
52             'value' => $this->getFixturesUrl() . '/',
53             'required' => TRUE,
54           ],
55         ],
56       ],
57     ]);
58   }
59
60   /**
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
63    * URL.
64    *
65    * This requires the media_test_oembed module in order to work.
66    */
67   protected function hijackProviderEndpoints() {
68     $providers = $this->getFixturesDirectory() . '/providers.json';
69     $providers = file_get_contents($providers);
70     $providers = Json::decode($providers);
71
72     $endpoint_url = Url::fromRoute('media_test_oembed.resource.get')
73       ->setAbsolute()
74       ->toString();
75
76     /** @var \Drupal\media_test_oembed\ProviderRepository $provider_repository */
77     $provider_repository = $this->container->get('media.oembed.provider_repository');
78
79     foreach ($providers as &$provider) {
80       foreach ($provider['endpoints'] as &$endpoint) {
81         $endpoint['url'] = $endpoint_url;
82       }
83       $provider_repository->setProvider(
84         new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints'])
85       );
86     }
87   }
88
89 }