3 namespace Drupal\Tests\media_entity_twitter\Kernel;
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;
14 * Tests SVG thumbnail generation from Twitter API responses.
16 * @group media_entity_twitter
18 class ThumbnailTest extends KernelTestBase {
21 * The mocked tweet fetcher.
23 * @var \Drupal\media_entity_twitter\TweetFetcherInterface
25 protected $tweetFetcher;
28 * The plugin under test.
30 * @var \Drupal\media_entity_twitter\Plugin\media\Source\Twitter
35 * A tweet media entity.
37 * @var \Drupal\media\MediaInterface
44 public static $modules = [
49 'media_entity_twitter',
58 protected function setUp() {
60 $this->installEntitySchema('file');
61 $this->installEntitySchema('media');
62 $this->installConfig(['media_entity_twitter', 'system']);
64 $this->tweetFetcher = $this->getMock(TweetFetcherInterface::class);
65 $this->container->set('media_entity_twitter.tweet_fetcher', $this->tweetFetcher);
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(),
80 FieldStorageConfig::create([
81 'field_name' => 'tweet',
82 'entity_type' => 'media',
83 'type' => 'string_long',
87 'field_name' => 'tweet',
88 'entity_type' => 'media',
92 $this->entity = Media::create([
94 'tweet' => 'https://twitter.com/foobar/status/12345',
97 $this->plugin = Twitter::create(
99 MediaType::load('tweet')->get('source_configuration'),
101 MediaType::load('tweet')->getSource()->getPluginDefinition()
104 $dir = $this->container
105 ->get('config.factory')
106 ->get('media_entity_twitter.settings')
107 ->get('local_images');
109 file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
113 * Tests that an existing local image is used as the thumbnail.
115 public function testLocalImagePresent() {
117 ->method('fetchTweet')
119 'extended_entities' => [
122 'media_url' => 'https://drupal.org/favicon.ico',
128 $uri = 'public://twitter-thumbnails/12345.ico';
130 $this->assertEquals($uri, $this->plugin->getMetadata($this->entity, 'thumbnail_uri'));
134 * Tests that a local image is downloaded if available but not present.
136 public function testLocalImageNotPresent() {
138 ->method('fetchTweet')
140 'extended_entities' => [
143 'media_url' => 'https://drupal.org/favicon.ico',
149 $this->plugin->getMetadata($this->entity, 'thumbnail_uri');
150 $this->assertFileExists('public://twitter-thumbnails/12345.ico');
154 * Tests that the default thumbnail is used if no local image is available.
156 public function testNoLocalImage() {
159 $this->plugin->getMetadata($this->entity, 'thumbnail_uri')
164 * Tests that thumbnail is generated if enabled and local image not available.
166 public function testThumbnailGeneration() {
167 $configuration = $this->plugin->getConfiguration();
168 $configuration['generate_thumbnails'] = TRUE;
169 $this->plugin->setConfiguration($configuration);
171 $uri = $this->plugin->getMetadata($this->entity, 'thumbnail_uri');
172 $this->assertFileExists($uri);