$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'
));
$this->
assertStringContainsString($expected_image_src,
$image_element->
getAttribute('src'
));
$assert_session->
elementNotExists('css', 'a',
$field);
// Ensure the image has the correct alt attribute.
$this->
assertSame('Image Alt Text 1',
$image_element->
getAttribute('alt'
));
// Load the media and check that all fields are properly populated.
$media = Media::
load(1
);
$this->
assertSame('example_1.jpeg',
$media->
getName());
$this->
assertSame('200',
$media->
get('field_string_width'
)->value
);
$this->
assertSame('89',
$media->
get('field_string_height'
)->value
);