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