Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / media / tests / src / Functional / UrlResolverTest.php
1 <?php
2
3 namespace Drupal\Tests\media\Functional;
4
5 use Drupal\Tests\media\Traits\OEmbedTestTrait;
6
7 /**
8  * Tests the oEmbed URL resolver service.
9  *
10  * @coversDefaultClass \Drupal\media\OEmbed\UrlResolver
11  *
12  * @group media
13  */
14 class UrlResolverTest extends MediaFunctionalTestBase {
15
16   use OEmbedTestTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function setUp() {
22     parent::setUp();
23     $this->lockHttpClientToFixtures();
24     $this->useFixtureProviders();
25   }
26
27   /**
28    * Data provider for testEndpointMatching().
29    *
30    * @see ::testEndpointMatching()
31    *
32    * @return array
33    */
34   public function providerEndpointMatching() {
35     return [
36       'match by endpoint: Twitter' => [
37         'https://twitter.com/Dries/status/999985431595880448',
38         'https://publish.twitter.com/oembed?url=https%3A//twitter.com/Dries/status/999985431595880448',
39       ],
40       'match by endpoint: Vimeo' => [
41         'https://vimeo.com/14782834',
42         'https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/14782834',
43       ],
44       'match by endpoint: CollegeHumor' => [
45         'http://www.collegehumor.com/video/40002870/lets-not-get-a-drink-sometime',
46         'http://www.collegehumor.com/oembed.json?url=http%3A//www.collegehumor.com/video/40002870/lets-not-get-a-drink-sometime',
47       ],
48     ];
49   }
50
51   /**
52    * Tests resource URL resolution when the asset URL can be matched to a
53    * provider endpoint.
54    *
55    * @covers ::getProviderByUrl
56    * @covers ::getResourceUrl
57    *
58    * @param string $url
59    *   The asset URL to resolve.
60    * @param string $resource_url
61    *   The expected oEmbed resource URL of the asset.
62    *
63    * @dataProvider providerEndpointMatching
64    */
65   public function testEndpointMatching($url, $resource_url) {
66     $this->assertSame(
67       $resource_url,
68       $this->container->get('media.oembed.url_resolver')->getResourceUrl($url)
69     );
70   }
71
72   /**
73    * Tests that hook_oembed_resource_url_alter() is invoked.
74    *
75    * @depends testEndpointMatching
76    */
77   public function testResourceUrlAlterHook() {
78     $this->container->get('module_installer')->install(['media_test_oembed']);
79
80     $resource_url = $this->container->get('media.oembed.url_resolver')
81       ->getResourceUrl('https://vimeo.com/14782834');
82
83     $this->assertContains('altered=1', parse_url($resource_url, PHP_URL_QUERY));
84   }
85
86   /**
87    * Data provider for testUrlDiscovery().
88    *
89    * @see ::testUrlDiscovery()
90    *
91    * @return array
92    */
93   public function providerUrlDiscovery() {
94     return [
95       'JSON resource' => [
96         'video_vimeo.html',
97         'https://vimeo.com/api/oembed.json?url=video_vimeo.html',
98       ],
99       'XML resource' => [
100         'video_collegehumor.html',
101         // The endpoint does not explicitly declare that it supports XML, so
102         // only JSON support is assumed, which is why the discovered URL
103         // contains '.json'. However, the fetched HTML file contains a
104         // relationship to an XML representation of the resource, with the
105         // application/xml+oembed MIME type.
106         'http://www.collegehumor.com/oembed.json?url=video_collegehumor.html',
107       ],
108     ];
109   }
110
111   /**
112    * Tests URL resolution when the resource URL must be actively discovered by
113    * scanning the asset.
114    *
115    * @param string $url
116    *   The asset URL to resolve.
117    * @param string $resource_url
118    *   The expected oEmbed resource URL of the asset.
119    *
120    * @covers ::discoverResourceUrl
121    * @covers ::getProviderByUrl
122    * @covers ::getResourceUrl
123    *
124    * @dataProvider providerUrlDiscovery
125    */
126   public function testUrlDiscovery($url, $resource_url) {
127     $this->assertSame(
128       $this->container->get('media.oembed.url_resolver')->getResourceUrl($url),
129       $resource_url
130     );
131   }
132
133 }