function FileFieldDisplayTest::testDescriptionDefaultFileFieldDisplay
Same name in other branches
- 9 core/modules/file/tests/src/Functional/FileFieldDisplayTest.php \Drupal\Tests\file\Functional\FileFieldDisplayTest::testDescriptionDefaultFileFieldDisplay()
- 8.9.x core/modules/file/tests/src/Functional/FileFieldDisplayTest.php \Drupal\Tests\file\Functional\FileFieldDisplayTest::testDescriptionDefaultFileFieldDisplay()
- 10 core/modules/file/tests/src/Functional/FileFieldDisplayTest.php \Drupal\Tests\file\Functional\FileFieldDisplayTest::testDescriptionDefaultFileFieldDisplay()
Tests description display of File Field.
File
-
core/
modules/ file/ tests/ src/ Functional/ FileFieldDisplayTest.php, line 194
Class
- FileFieldDisplayTest
- Tests the display of file fields in node and views.
Namespace
Drupal\Tests\file\FunctionalCode
public function testDescriptionDefaultFileFieldDisplay() : void {
$field_name = $this->randomMachineName();
$type_name = 'article';
$field_storage_settings = [
'display_field' => '1',
'display_default' => '1',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
];
$field_settings = [
'description_field' => '1',
];
$widget_settings = [];
$this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
$test_file = $this->getTestFile('text');
// Create a new node with the uploaded file.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
// Add file description.
$description = 'This is the test file description';
$this->drupalGet("node/{$nid}/edit");
$this->submitForm([
$field_name . '[0][description]' => $description,
], 'Save');
// Load uncached node.
\Drupal::entityTypeManager()->getStorage('node')
->resetCache([
$nid,
]);
$node = Node::load($nid);
// Test default formatter.
$this->drupalGet('node/' . $nid);
$this->assertSession()
->elementTextContains('xpath', '//a[@href="' . $node->{$field_name}->entity
->createFileUrl() . '"]', $description);
// Change formatter to "Table of files".
$display = \Drupal::entityTypeManager()->getStorage('entity_view_display')
->load('node.' . $type_name . '.default');
$display->setComponent($field_name, [
'label' => 'hidden',
'type' => 'file_table',
])
->save();
$this->drupalGet('node/' . $nid);
$this->assertSession()
->elementTextContains('xpath', '//a[@href="' . $node->{$field_name}->entity
->createFileUrl() . '"]', $description);
// Test that null file size is rendered as "Unknown".
$nonexistent_file = File::create([
'uri' => 'temporary://' . $this->randomMachineName() . '.txt',
]);
$nonexistent_file->save();
$node->set($field_name, $nonexistent_file->id());
$node->save();
$this->drupalGet('node/' . $nid);
$this->assertSession()
->elementTextEquals('xpath', '//a[@href="' . $node->{$field_name}->entity
->createFileUrl() . '"]/../../../td[2]', 'Unknown');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.