function FileFieldWidgetTest::testSingleValuedWidget

Same name in this branch
  1. 10 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
Same name in other branches
  1. 9 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
  2. 9 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testSingleValuedWidget()
  3. 8.9.x core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
  4. 8.9.x core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testSingleValuedWidget()
  5. 11.x core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
  6. 11.x 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 83

Class

FileFieldWidgetTest
Tests the file field widget with public and private files.

Namespace

Drupal\Tests\file\Functional

Code

public function testSingleValuedWidget() : void {
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $type_name = 'article';
    $field_name = $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.