You are here

public function MediaThumbnailFormatterTest::testRender in Drupal 10

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\FieldFormatter

Code

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());
}