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\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;
15 * Tests SVG thumbnail generation from Twitter API responses.
17 * @group media_entity_twitter
19 class ThumbnailTest extends KernelTestBase {
22 * The mocked tweet fetcher.
24 * @var TweetFetcherInterface
26 protected $tweetFetcher;
29 * The plugin under test.
36 * A tweet media entity.
45 public static $modules = [
50 'media_entity_twitter',
59 protected function setUp() {
61 $this->installEntitySchema('file');
62 $this->installEntitySchema('media');
63 $this->installConfig(['media_entity_twitter', 'system']);
65 $this->tweetFetcher = $this->getMock(TweetFetcherInterface::class);
66 $this->container->set('media_entity_twitter.tweet_fetcher', $this->tweetFetcher);
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(),
81 FieldStorageConfig::create([
82 'field_name' => 'tweet',
83 'entity_type' => 'media',
84 'type' => 'string_long',
88 'field_name' => 'tweet',
89 'entity_type' => 'media',
93 $this->entity = Media::create([
95 'tweet' => 'https://twitter.com/foobar/status/12345',
98 $this->plugin = Twitter::create(
100 MediaBundle::load('tweet')->getTypeConfiguration(),
105 $dir = $this->container
106 ->get('config.factory')
107 ->get('media_entity_twitter.settings')
108 ->get('local_images');
110 file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
114 * Tests that an existing local image is used as the thumbnail.
116 public function testLocalImagePresent() {
118 ->method('fetchTweet')
120 'extended_entities' => [
123 'media_url' => 'https://drupal.org/favicon.ico',
129 $uri = 'public://twitter-thumbnails/12345.ico';
131 $this->assertEquals($uri, $this->plugin->thumbnail($this->entity));
135 * Tests that a local image is downloaded if available but not present.
137 public function testLocalImageNotPresent() {
139 ->method('fetchTweet')
141 'extended_entities' => [
144 'media_url' => 'https://drupal.org/favicon.ico',
150 $this->plugin->thumbnail($this->entity);
151 $this->assertFileExists('public://twitter-thumbnails/12345.ico');
155 * Tests that the default thumbnail is used if no local image is available.
157 public function testNoLocalImage() {
159 $this->plugin->getDefaultThumbnail(),
160 $this->plugin->thumbnail($this->entity)
165 * Tests that thumbnail is generated if enabled and local image not available.
167 public function testThumbnailGeneration() {
168 $configuration = $this->plugin->getConfiguration();
169 $configuration['generate_thumbnails'] = TRUE;
170 $this->plugin->setConfiguration($configuration);
172 $uri = $this->plugin->thumbnail($this->entity);
173 $this->assertFileExists($uri);