Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / media / tests / src / Functional / ProviderRepositoryTest.php
1 <?php
2
3 namespace Drupal\Tests\media\Functional;
4
5 use Drupal\media\OEmbed\ProviderException;
6
7 /**
8  * Tests the oEmbed provider repository.
9  *
10  * @covers \Drupal\media\OEmbed\ProviderRepository
11  *
12  * @group media
13  */
14 class ProviderRepositoryTest extends MediaFunctionalTestBase {
15
16   /**
17    * Tests that provider discovery fails if the provider database is empty.
18    *
19    * @param string $content
20    *   The expected JSON content of the provider database.
21    *
22    * @dataProvider providerEmptyProviderList
23    */
24   public function testEmptyProviderList($content) {
25     $response = $this->prophesize('\GuzzleHttp\Psr7\Response');
26     $response->getBody()->willReturn($content);
27
28     $client = $this->createMock('\GuzzleHttp\Client');
29     $client->method('request')->withAnyParameters()->willReturn($response->reveal());
30     $this->container->set('http_client', $client);
31
32     $this->setExpectedException(ProviderException::class, 'Remote oEmbed providers database returned invalid or empty list.');
33     $this->container->get('media.oembed.provider_repository')->getAll();
34   }
35
36   /**
37    * Data provider for testEmptyProviderList().
38    *
39    * @see ::testEmptyProviderList()
40    *
41    * @return array
42    */
43   public function providerEmptyProviderList() {
44     return [
45       'empty array' => ['[]'],
46       'empty string' => [''],
47     ];
48   }
49
50   /**
51    * Tests that provider discovery fails with a non-existent provider database.
52    *
53    * @param string $providers_url
54    *   The URL of the provider database.
55    * @param string $exception_message
56    *   The expected exception message.
57    *
58    * @dataProvider providerNonExistingProviderDatabase
59    */
60   public function testNonExistingProviderDatabase($providers_url, $exception_message) {
61     $this->config('media.settings')
62       ->set('oembed_providers_url', $providers_url)
63       ->save();
64
65     $this->setExpectedException(ProviderException::class, $exception_message);
66     $this->container->get('media.oembed.provider_repository')->getAll();
67   }
68
69   /**
70    * Data provider for testEmptyProviderList().
71    *
72    * @see ::testEmptyProviderList()
73    *
74    * @return array
75    */
76   public function providerNonExistingProviderDatabase() {
77     return [
78       [
79         'http://oembed1.com/providers.json',
80         'Could not retrieve the oEmbed provider database from http://oembed1.com/providers.json',
81       ],
82       [
83         'http://oembed.com/providers1.json',
84         'Could not retrieve the oEmbed provider database from http://oembed.com/providers1.json',
85       ],
86     ];
87   }
88
89 }