Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / media_entity_twitter / tests / src / Kernel / ThumbnailTest.php
1 <?php
2
3 namespace Drupal\Tests\media_entity_twitter\Kernel;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\media\Entity\Media;
9 use Drupal\media\Entity\MediaType;
10 use Drupal\media_entity_twitter\Plugin\media\Source\Twitter;
11 use Drupal\media_entity_twitter\TweetFetcherInterface;
12
13 /**
14  * Tests SVG thumbnail generation from Twitter API responses.
15  *
16  * @group media_entity_twitter
17  */
18 class ThumbnailTest extends KernelTestBase {
19
20   /**
21    * The mocked tweet fetcher.
22    *
23    * @var \Drupal\media_entity_twitter\TweetFetcherInterface
24    */
25   protected $tweetFetcher;
26
27   /**
28    * The plugin under test.
29    *
30    * @var \Drupal\media_entity_twitter\Plugin\media\Source\Twitter
31    */
32   protected $plugin;
33
34   /**
35    * A tweet media entity.
36    *
37    * @var \Drupal\media\MediaInterface
38    */
39   protected $entity;
40
41   /**
42    * {@inheritdoc}
43    */
44   public static $modules = [
45     'field',
46     'file',
47     'image',
48     'media',
49     'media_entity_twitter',
50     'system',
51     'text',
52     'user',
53   ];
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function setUp() {
59     parent::setUp();
60     $this->installEntitySchema('file');
61     $this->installEntitySchema('media');
62     $this->installConfig(['media_entity_twitter', 'system']);
63
64     $this->tweetFetcher = $this->getMock(TweetFetcherInterface::class);
65     $this->container->set('media_entity_twitter.tweet_fetcher', $this->tweetFetcher);
66
67     MediaType::create([
68       'id' => 'tweet',
69       'source' => 'twitter',
70       'source_configuration' => [
71         'source_field' => 'tweet',
72         'use_twitter_api' => TRUE,
73         'consumer_key' => $this->randomString(),
74         'consumer_secret' => $this->randomString(),
75         'oauth_access_token' => $this->randomString(),
76         'oauth_access_token_secret' => $this->randomString(),
77       ],
78     ])->save();
79
80     FieldStorageConfig::create([
81       'field_name' => 'tweet',
82       'entity_type' => 'media',
83       'type' => 'string_long',
84     ])->save();
85
86     FieldConfig::create([
87       'field_name' => 'tweet',
88       'entity_type' => 'media',
89       'bundle' => 'tweet',
90     ])->save();
91
92     $this->entity = Media::create([
93       'bundle' => 'tweet',
94       'tweet' => 'https://twitter.com/foobar/status/12345',
95     ]);
96
97     $this->plugin = Twitter::create(
98       $this->container,
99       MediaType::load('tweet')->get('source_configuration'),
100       'twitter',
101       MediaType::load('tweet')->getSource()->getPluginDefinition()
102     );
103
104     $dir = $this->container
105       ->get('config.factory')
106       ->get('media_entity_twitter.settings')
107       ->get('local_images');
108
109     file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
110   }
111
112   /**
113    * Tests that an existing local image is used as the thumbnail.
114    */
115   public function testLocalImagePresent() {
116     $this->tweetFetcher
117       ->method('fetchTweet')
118       ->willReturn([
119         'extended_entities' => [
120           'media' => [
121             [
122               'media_url' => 'https://drupal.org/favicon.ico',
123             ],
124           ],
125         ],
126       ]);
127
128     $uri = 'public://twitter-thumbnails/12345.ico';
129     touch($uri);
130     $this->assertEquals($uri, $this->plugin->getMetadata($this->entity, 'thumbnail_uri'));
131   }
132
133   /**
134    * Tests that a local image is downloaded if available but not present.
135    */
136   public function testLocalImageNotPresent() {
137     $this->tweetFetcher
138       ->method('fetchTweet')
139       ->willReturn([
140         'extended_entities' => [
141           'media' => [
142             [
143               'media_url' => 'https://drupal.org/favicon.ico',
144             ],
145           ],
146         ],
147       ]);
148
149     $this->plugin->getMetadata($this->entity, 'thumbnail_uri');
150     $this->assertFileExists('public://twitter-thumbnails/12345.ico');
151   }
152
153   /**
154    * Tests that the default thumbnail is used if no local image is available.
155    */
156   public function testNoLocalImage() {
157     $this->assertEquals(
158       '/twitter.png',
159       $this->plugin->getMetadata($this->entity, 'thumbnail_uri')
160     );
161   }
162
163   /**
164    * Tests that thumbnail is generated if enabled and local image not available.
165    */
166   public function testThumbnailGeneration() {
167     $configuration = $this->plugin->getConfiguration();
168     $configuration['generate_thumbnails'] = TRUE;
169     $this->plugin->setConfiguration($configuration);
170
171     $uri = $this->plugin->getMetadata($this->entity, 'thumbnail_uri');
172     $this->assertFileExists($uri);
173   }
174
175 }