protected function MediaStandardProfileTest::documentTest in Drupal 10
Same name and namespace in other branches
- 8 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::documentTest()
- 9 core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaStandardProfileTest::documentTest()
Tests the standard profile configuration for media type 'document'.
File
- core/
modules/ media/ tests/ src/ FunctionalJavascript/ MediaStandardProfileTest.php, line 292
Class
- MediaStandardProfileTest
- Basic tests for Media configuration in the standard profile.
Namespace
Drupal\Tests\media\FunctionalJavascriptCode
protected function documentTest() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$source_field_id = 'field_media_document';
// Create 2 test files.
$test_filename = $this
->randomMachineName() . '.txt';
$test_filepath = 'public://' . $test_filename;
$test_filename_updated = $this
->randomMachineName() . '.txt';
$test_filepath_updated = 'public://' . $test_filename_updated;
file_put_contents($test_filepath, $this
->randomMachineName());
file_put_contents($test_filepath_updated, $this
->randomMachineName());
// Check if the name field is properly hidden on the media form.
$this
->drupalGet('media/add/document');
$assert_session
->fieldNotExists('name');
// Check if the source field is available.
$assert_session
->fieldExists("files[{$source_field_id}_0]");
// Create a media item.
$page
->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')
->realpath($test_filepath));
$result = $assert_session
->waitForButton('Remove');
$this
->assertNotEmpty($result);
$page
->pressButton('Save');
$file_media_id = $this->container
->get('entity_type.manager')
->getStorage('media')
->getQuery()
->accessCheck(FALSE)
->sort('mid', 'DESC')
->execute();
$file_media_id = reset($file_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' => $file_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($file_media_id);
$this
->assertSame($test_filename, $media
->label());
// Here we expect to see only the linked filename. Assert only one element
// in the content region.
$assert_session
->elementsCount('css', 'div.media--type-document > *', 1);
// Assert the file link is present in the media element and its text matches
// the filename.
$link_element = $assert_session
->elementExists('css', 'div.media--type-document .field--name-field-media-document a');
$this
->assertSame($test_filename, $link_element
->getText());
// Assert the media name is updated through the field mapping when changing
// the source field.
$this
->drupalGet('media/' . $file_media_id . '/edit');
$page
->pressButton('Remove');
$result = $assert_session
->waitForField("files[{$source_field_id}_0]");
$this
->assertNotEmpty($result);
$page
->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')
->realpath($test_filepath_updated));
$result = $assert_session
->waitForButton('Remove');
$this
->assertNotEmpty($result);
$page
->pressButton('Save');
$this
->drupalGet('/node/' . $node
->id());
// Check if the default media name is updated as expected.
$media = \Drupal::entityTypeManager()
->getStorage('media')
->loadUnchanged($file_media_id);
$this
->assertSame($test_filename_updated, $media
->label());
// Again we expect to see only the linked filename. Assert only one element
// in the content region.
$assert_session
->elementsCount('css', 'div.media--type-document > *', 1);
// Assert the file link is present in the media element and its text matches
// the updated filename.
$link_element = $assert_session
->elementExists('css', 'div.media--type-document .field--name-field-media-document a');
$this
->assertSame($test_filename_updated, $link_element
->getText());
}