function MediaThumbnailFormatterTest::testRender
Same name in other branches
- 10 core/modules/media/tests/src/Functional/FieldFormatter/MediaThumbnailFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\MediaThumbnailFormatterTest::testRender()
- 11.x core/modules/media/tests/src/Functional/FieldFormatter/MediaThumbnailFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\MediaThumbnailFormatterTest::testRender()
Tests the media thumbnail field formatter.
File
-
core/
modules/ media/ tests/ src/ Functional/ FieldFormatter/ MediaThumbnailFormatterTest.php, line 29
Class
- MediaThumbnailFormatterTest
- @covers \Drupal\media\Plugin\Field\FieldFormatter\MediaThumbnailFormatter
Namespace
Drupal\Tests\media\Functional\FieldFormatterCode
public function testRender() {
$this->drupalLogin($this->adminUser);
/** @var \Drupal\node\NodeStorage $node_storage */
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
// Create an image media type for testing the formatter.
$this->createMediaType('image', [
'id' => 'image',
]);
// Create an article content type.
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
// Creates an entity reference field for media.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_media_reference',
'type' => 'entity_reference',
'entity_type' => 'node',
'cardinality' => 1,
'settings' => [
'target_type' => 'media',
],
]);
$field_storage->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Reference media',
'translatable' => FALSE,
])->save();
// Alter the form display.
$this->container
->get('entity_display.repository')
->getFormDisplay('node', 'article')
->setComponent('field_media_reference', [
'type' => 'entity_reference_autocomplete',
])
->save();
// Change the image thumbnail to point into the media.
$this->changeMediaReferenceFieldLinkType('media');
// Create and upload a file to the media.
$file = File::create([
'uri' => current($this->getTestFiles('image'))->uri,
]);
$file->save();
$mediaImage = Media::create([
'bundle' => 'image',
'name' => 'Test image',
'field_media_image' => $file->id(),
]);
$mediaImage->save();
// Save the article node.
$title = $this->randomMachineName();
$edit = [
'title[0][value]' => $title,
];
$edit['field_media_reference[0][target_id]'] = $mediaImage->getName();
$this->drupalGet('node/add/article');
$this->submitForm($edit, 'Save');
// Validate the image being loaded with the media reference.
$this->assertSession()
->responseContains('<a href="' . $mediaImage->toUrl('edit-form')
->toString());
// Retrieve the created node.
$node = $this->drupalGetNodeByTitle($title);
$nid = $node->id();
// Change the image thumbnail to point into the content node.
$this->changeMediaReferenceFieldLinkType('content');
$node_storage->resetCache([
$nid,
]);
$this->drupalGet('node/' . $nid);
// Validate image being loaded with the content on the link.
$this->assertSession()
->responseContains('<a href="' . $node->toUrl()
->toString());
$this->assertSession()
->responseContains('loading="eager"');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.