public function FileFieldWidgetTest::testSingleValuedWidget in Drupal 10
Same name in this branch
- 10 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testSingleValuedWidget()
- 10 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
Same name and namespace in other branches
- 8 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testSingleValuedWidget()
- 9 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testSingleValuedWidget()
Tests upload and remove buttons for a single-valued File field.
File
- core/
modules/ file/ tests/ src/ Functional/ FileFieldWidgetTest.php, line 81
Class
- FileFieldWidgetTest
- Tests the file field widget with public and private files.
Namespace
Drupal\Tests\file\FunctionalCode
public function testSingleValuedWidget() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type_name = 'article';
$field_name = strtolower($this
->randomMachineName());
$this
->createFileField($field_name, 'node', $type_name);
$test_file = $this
->getTestFile('text');
// Create a new node with the uploaded file and ensure it got uploaded
// successfully.
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node = $node_storage
->loadUnchanged($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
// Ensure the file can be downloaded.
$this
->drupalGet($node_file
->createFileUrl());
$this
->assertSession()
->statusCodeEquals(200);
// Ensure the edit page has a remove button instead of an upload button.
$this
->drupalGet("node/{$nid}/edit");
$this
->assertSession()
->buttonNotExists('Upload');
$this
->assertSession()
->buttonExists('Remove');
$this
->submitForm([], 'Remove');
// Ensure the page now has an upload button instead of a remove button.
$this
->assertSession()
->buttonNotExists('Remove');
$this
->assertSession()
->buttonExists('Upload');
// Test label has correct 'for' attribute.
$input = $this
->assertSession()
->fieldExists("files[{$field_name}_0]");
$this
->assertSession()
->elementExists('xpath', '//label[@for="' . $input
->getAttribute('id') . '"]');
// Save the node and ensure it does not have the file.
$this
->submitForm([], 'Save');
$node = $node_storage
->loadUnchanged($nid);
$this
->assertEmpty($node->{$field_name}->target_id, 'File was successfully removed from the node.');
}