function MediaStandardProfileTest::imageTest
Same name in other branches
- 9 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::imageTest()
- 8.9.x core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::imageTest()
- 10 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::imageTest()
Tests the standard profile configuration for media type 'image'.
1 call to MediaStandardProfileTest::imageTest()
- MediaStandardProfileTest::testMediaSources in core/
modules/ media/ tests/ src/ FunctionalJavascript/ MediaStandardProfileTest.php - Tests all media sources in one method.
File
-
core/
modules/ media/ tests/ src/ FunctionalJavascript/ MediaStandardProfileTest.php, line 199
Class
- MediaStandardProfileTest
- Basic tests for Media configuration in the standard profile.
Namespace
Drupal\Tests\media\FunctionalJavascriptCode
protected function imageTest() : void {
$assert_session = $this->assertSession();
$page = $this->getSession()
->getPage();
$source_field_id = 'field_media_image';
// Check if the name field is properly hidden on the media form.
$this->drupalGet('media/add/image');
$assert_session->fieldNotExists('name');
// Check if the source field is available.
$assert_session->fieldExists("files[{$source_field_id}_0]");
// Create a media item.
$image_media_name = 'example_1.jpeg';
$page->attachFileToField("files[{$source_field_id}_0]", $this->root . '/core/modules/media/tests/fixtures/' . $image_media_name);
$result = $assert_session->waitForButton('Remove');
$this->assertNotEmpty($result);
$page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1');
$page->pressButton('Save');
$image_media_id = $this->container
->get('entity_type.manager')
->getStorage('media')
->getQuery()
->accessCheck(FALSE)
->sort('mid', 'DESC')
->execute();
$image_media_id = reset($image_media_id);
// Reference the created media using an entity_reference field and make sure
// the output is what we expect.
$node = Node::create([
'title' => 'Host node',
'type' => 'article',
'field_related_media' => [
'target_id' => $image_media_id,
],
]);
$node->save();
$this->drupalGet('/node/' . $node->id());
// Check if the default media name is generated as expected.
$media = \Drupal::entityTypeManager()->getStorage('media')
->loadUnchanged($image_media_id);
$this->assertSame($image_media_name, $media->label());
// Here we expect to see only the image, nothing else. Assert only one
// element in the content region.
$assert_session->elementsCount('css', 'div.media--type-image > *', 1);
// 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.
$image_element = $assert_session->elementExists('css', 'div.media--type-image img');
/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
$file_url_generator = \Drupal::service('file_url_generator');
$expected_image_src = $file_url_generator->generate(\Drupal::token()->replace('public://styles/large/public/[date:custom:Y]-[date:custom:m]/' . $image_media_name))
->toString();
$this->assertStringContainsString($expected_image_src, $image_element->getAttribute('src'));
$assert_session->elementExists('css', '.field--name-field-media-image .field__label.visually-hidden');
$assert_session->elementNotExists('css', '.field--name-field-media-image a');
// Assert the media name is updated through the field mapping when changing
// the source field.
$this->drupalGet('media/' . $image_media_id . '/edit');
$page->pressButton('Remove');
$result = $assert_session->waitForField("files[{$source_field_id}_0]");
$this->assertNotEmpty($result);
$image_media_name_updated = 'example_2.jpeg';
$page->attachFileToField("files[{$source_field_id}_0]", $this->root . '/core/modules/media/tests/fixtures/' . $image_media_name_updated);
$result = $assert_session->waitForButton('Remove');
$this->assertNotEmpty($result);
$page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 2');
$page->pressButton('Save');
$this->drupalGet('/node/' . $node->id());
// Check if the default media name is updated as expected.
$media = \Drupal::entityTypeManager()->getStorage('media')
->loadUnchanged($image_media_id);
$this->assertSame($image_media_name_updated, $media->label());
// Again we expect to see only the image, nothing else. Assert only one
// element in the content region.
$assert_session->elementsCount('css', 'div.media--type-image > *', 1);
// 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.
$image_element = $assert_session->elementExists('css', 'div.media--type-image img');
$expected_image_src = $file_url_generator->generate(\Drupal::token()->replace('public://styles/large/public/[date:custom:Y]-[date:custom:m]/' . $image_media_name_updated))
->toString();
$this->assertStringContainsString($expected_image_src, $image_element->getAttribute('src'));
$assert_session->elementExists('css', '.field--name-field-media-image .field__label.visually-hidden');
$assert_session->elementNotExists('css', '.field--name-field-media-image a');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.