// Create a media item.
$this->
drupalGet("media/add/{
$media_type_id}"
);
$page->
attachFileToField("files[{
$source_field_id}_0]",
$this->root . '/core/modules/media/tests/fixtures/example_1.jpeg'
);
$result =
$assert_session->
waitForButton('Remove'
);
$this->
assertNotEmpty($result);
$page->
fillField("{
$source_field_id}[0][alt]", 'Image Alt Text 1'
);
$page->
pressButton('Save'
);
$assert_session->
addressEquals('admin/content/media'
);
// Get the media entity view URL from the creation message.
$this->
drupalGet($this->
assertLinkToCreatedMedia());
// Assert the image element is present inside the media element and that its
// src attribute uses the large image style, the label is visually hidden,
// and there is no link to the image file.
$label =
$assert_session->
elementExists('xpath', '//div[contains(@class, "visually-hidden") and text()="Image"]'
);
// The field is the parent div of the label.
$field =
$label->
getParent();
$image_element =
$field->
find('css', 'img'
);
/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
$file_url_generator = \Drupal::
service('file_url_generator'
);
$expected_image_src =
$file_url_generator->
generateString(\Drupal::
token()->
replace('public://styles/large/public/[date:custom:Y]-[date:custom:m]/example_1.jpeg'
));