public function MediaSourceTest::testThumbnail in Drupal 9
Same name and namespace in other branches
- 8 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testThumbnail()
- 10 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testThumbnail()
Tests the thumbnail functionality.
File
- core/
modules/ media/ tests/ src/ Kernel/ MediaSourceTest.php, line 230
Class
- MediaSourceTest
- Tests media source plugins related logic.
Namespace
Drupal\Tests\media\KernelCode
public function testThumbnail() {
file_put_contents('public://thumbnail1.jpg', '');
file_put_contents('public://thumbnail2.jpg', '');
// Save a media item and make sure thumbnail was added.
\Drupal::state()
->set('media_source_test_attributes', [
'thumbnail_uri' => [
'value' => 'public://thumbnail1.jpg',
],
]);
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'name' => 'Mr. Jones',
'field_media_test' => 'some_value',
]);
$media_source = $media
->getSource();
$this
->assertSame('public://thumbnail1.jpg', $media_source
->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media
->save();
$this
->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity
->getFileUri(), 'Thumbnail was not added to the media item.');
// We expect the title not to be present on the Thumbnail.
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Now change the metadata attribute and make sure that the thumbnail stays
// the same.
\Drupal::state()
->set('media_source_test_attributes', [
'thumbnail_uri' => [
'value' => 'public://thumbnail2.jpg',
],
]);
$this
->assertSame('public://thumbnail2.jpg', $media_source
->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media
->save();
$this
->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity
->getFileUri(), 'Thumbnail was not preserved.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Remove the thumbnail and make sure that it is auto-updated on save.
$media->thumbnail->target_id = NULL;
$this
->assertSame('public://thumbnail2.jpg', $media_source
->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media
->save();
$this
->assertSame('public://thumbnail2.jpg', $media->thumbnail->entity
->getFileUri(), 'New thumbnail was not added to the media item.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Change the metadata attribute again, change the source field value too
// and make sure that the thumbnail updates.
\Drupal::state()
->set('media_source_test_attributes', [
'thumbnail_uri' => [
'value' => 'public://thumbnail1.jpg',
],
]);
$media->field_media_test->value = 'some_new_value';
$this
->assertSame('public://thumbnail1.jpg', $media_source
->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media
->save();
$this
->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity
->getFileUri(), 'New thumbnail was not added to the media item.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Change the thumbnail metadata attribute and make sure that the thumbnail
// is set correctly.
\Drupal::state()
->set('media_source_test_attributes', [
'thumbnail_uri' => [
'value' => 'public://thumbnail1.jpg',
],
'alternative_thumbnail_uri' => [
'value' => 'public://thumbnail2.jpg',
],
]);
\Drupal::state()
->set('media_source_test_definition', [
'thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri',
]);
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'name' => 'Mr. Jones',
'field_media_test' => 'some_value',
]);
$media_source = $media
->getSource();
$this
->assertSame('public://thumbnail1.jpg', $media_source
->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
$this
->assertSame('public://thumbnail2.jpg', $media_source
->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media
->save();
$this
->assertSame('public://thumbnail2.jpg', $media->thumbnail->entity
->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Enable queued thumbnails and make sure that the entity gets the default
// thumbnail initially.
\Drupal::state()
->set('media_source_test_definition', []);
\Drupal::state()
->set('media_source_test_attributes', [
'thumbnail_uri' => [
'value' => 'public://thumbnail1.jpg',
],
]);
$this->testMediaType
->setQueueThumbnailDownloadsStatus(TRUE)
->save();
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'name' => 'Mr. Jones',
'field_media_test' => 'some_value',
]);
$this
->assertSame('public://thumbnail1.jpg', $media
->getSource()
->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
$media
->save();
$this
->assertSame('public://media-icons/generic/generic.png', $media->thumbnail->entity
->getFileUri(), 'Default thumbnail was not set initially.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Process the queue item and make sure that the thumbnail was updated too.
$queue_name = 'media_entity_thumbnail';
/** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
$queue_worker = \Drupal::service('plugin.manager.queue_worker')
->createInstance($queue_name);
$queue = \Drupal::queue($queue_name);
$this
->assertSame(1, $queue
->numberOfItems(), 'Item was not added to the queue.');
$item = $queue
->claimItem();
$this
->assertSame($media
->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.');
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
$this
->assertSame(0, $queue
->numberOfItems(), 'Item was not removed from the queue.');
$media = Media::load($media
->id());
$this
->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity
->getFileUri(), 'Thumbnail was not updated by the queue.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('', $media->thumbnail->alt);
// Set the alt metadata attribute and make sure it's used for the thumbnail.
\Drupal::state()
->set('media_source_test_definition', [
'thumbnail_alt_metadata_attribute' => 'alt',
]);
\Drupal::state()
->set('media_source_test_attributes', [
'alt' => [
'value' => 'This will be alt.',
],
]);
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'name' => 'Boxer',
'field_media_test' => 'some_value',
]);
$media
->save();
$this
->assertSame('Boxer', $media
->getName(), 'Correct name was not set on the media item.');
$this
->assertEmpty($media->thumbnail->title);
$this
->assertSame('This will be alt.', $media->thumbnail->alt);
}