diff --git a/core/modules/file/src/Tests/FileFieldTestBase.php b/core/modules/file/src/Tests/FileFieldTestBase.php
index b7b90d7d37..91bb58ca7d 100644
--- a/core/modules/file/src/Tests/FileFieldTestBase.php
+++ b/core/modules/file/src/Tests/FileFieldTestBase.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\file\Tests;
 
+@trigger_error('The ' . __NAMESPACE__ . '\FileFieldTestBase is scheduled for removal in Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileFieldTestBase', E_USER_DEPRECATED);
+
 use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\file\FileInterface;
diff --git a/core/modules/file/src/Tests/FileManagedTestBase.php b/core/modules/file/src/Tests/FileManagedTestBase.php
index 879e1c4e94..fde651dca6 100644
--- a/core/modules/file/src/Tests/FileManagedTestBase.php
+++ b/core/modules/file/src/Tests/FileManagedTestBase.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\file\Tests;
 
+@trigger_error('The ' . __NAMESPACE__ . '\FileManagedTestBase is scheduled for removal in Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileManagedTestBase', E_USER_DEPRECATED);
+
 use Drupal\file\Entity\File;
 use Drupal\file\FileInterface;
 use Drupal\simpletest\WebTestBase;
diff --git a/core/modules/file/src/Tests/DownloadTest.php b/core/modules/file/tests/src/Functional/DownloadTest.php
similarity index 83%
rename from core/modules/file/src/Tests/DownloadTest.php
rename to core/modules/file/tests/src/Functional/DownloadTest.php
index 239155cf40..f563daeb62 100644
--- a/core/modules/file/src/Tests/DownloadTest.php
+++ b/core/modules/file/tests/src/Functional/DownloadTest.php
@@ -1,6 +1,9 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
+
+use Drupal\Core\Site\Settings;
+use GuzzleHttp\Exception\RequestException;
 
 /**
  * Tests for download/file transfer functions.
@@ -25,16 +28,16 @@ public function testPublicFileTransfer() {
     // encoded.
     $filename = $GLOBALS['base_url'] . '/' . \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename());
     $this->assertEqual($filename, $url, 'Correctly generated a URL for a created file.');
-    $this->drupalHead($url);
-    $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the created file.');
-
+    $http_client = \Drupal::httpClient();
+    $response = $http_client->head($url);
+    $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the created file.');
     // Test generating a URL to a shipped file (i.e. a file that is part of
     // Drupal core, a module or a theme, for example a JavaScript file).
     $filepath = 'core/assets/vendor/jquery/jquery.min.js';
     $url = file_create_url($filepath);
     $this->assertEqual($GLOBALS['base_url'] . '/' . $filepath, $url, 'Correctly generated a URL for a shipped file.');
-    $this->drupalHead($url);
-    $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
+    $response = $http_client->head($url);
+    $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the shipped file.');
   }
 
   /**
@@ -70,17 +73,28 @@ protected function doPrivateFileTransferTest() {
     $this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.');
 
     // Test that the file transferred correctly.
-    $this->assertEqual($contents, $this->content, 'Contents of the file are correct.');
+    $this->assertSession()->responseContains($contents);
 
     // Deny access to all downloads via a -1 header.
     file_test_set_return('download', -1);
-    $this->drupalHead($url);
-    $this->assertResponse(403, 'Correctly denied access to a file when file_test sets the header to -1.');
+    $http_client = \Drupal::httpClient();
+    try {
+      $http_client->head($url);
+      $this->fail('Not correctly denied access to a file when file_test sets the header to -1.');
+    }
+    catch (RequestException $e) {
+      $this->assertSame(403, $e->getCode());
+    }
 
     // Try non-existent file.
     $url = file_create_url('private://' . $this->randomMachineName());
-    $this->drupalHead($url);
-    $this->assertResponse(404, 'Correctly returned 404 response for a non-existent file.');
+    try {
+      $http_client->head($url);
+      $this->fail('Not correctly returned 404 response for a non-existent file.');
+    }
+    catch (RequestException $e) {
+      $this->assertSame(404, $e->getCode());
+    }
   }
 
   /**
@@ -172,4 +186,20 @@ private function checkUrl($scheme, $directory, $filename, $expected_url) {
     $file->delete();
   }
 
+  /**
+   * Changes in memory settings.
+   *
+   * @param $name
+   *   The name of the setting to return.
+   * @param $value
+   *   The value of the setting.
+   *
+   * @see \Drupal\Core\Site\Settings::get()
+   */
+  protected function settingsSet($name, $value) {
+    $settings = Settings::getAll();
+    $settings[$name] = $value;
+    new Settings($settings);
+  }
+
 }
diff --git a/core/modules/file/src/Tests/FileFieldAnonymousSubmissionTest.php b/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php
similarity index 95%
rename from core/modules/file/src/Tests/FileFieldAnonymousSubmissionTest.php
rename to core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php
index 66d87cd7e8..0bed72fbdc 100644
--- a/core/modules/file/src/Tests/FileFieldAnonymousSubmissionTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\node\Entity\Node;
 use Drupal\user\RoleInterface;
@@ -88,7 +88,7 @@ public function testAnonymousNodeWithFile() {
       $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
       $node = Node::load($nid);
       $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
-      $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.');
+      $this->assertFileExistsOnDisk(File::load($node->field_image->target_id), 'The image was uploaded successfully.');
     }
   }
 
@@ -162,7 +162,7 @@ protected function doTestNodeWithFileWithoutTitle() {
       $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
       $node = Node::load($nid);
       $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
-      $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.');
+      $this->assertFileExistsOnDisk(File::load($node->field_image->target_id), 'The image was uploaded successfully.');
     }
   }
 
diff --git a/core/modules/file/src/Tests/FileFieldDisplayTest.php b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php
similarity index 97%
rename from core/modules/file/src/Tests/FileFieldDisplayTest.php
rename to core/modules/file/tests/src/Functional/FileFieldDisplayTest.php
index 9589db1785..1ae160426d 100644
--- a/core/modules/file/src/Tests/FileFieldDisplayTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php
@@ -1,10 +1,11 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
 use Drupal\file\Entity\File;
 use Drupal\node\Entity\Node;
+use Drupal\Tests\Traits\DrupalPostFormHack;
 
 /**
  * Tests the display of file fields in node and views.
@@ -13,6 +14,8 @@
  */
 class FileFieldDisplayTest extends FileFieldTestBase {
 
+  use DrupalPostFormHack;
+
   /**
    * Tests normal formatter display on node display.
    */
@@ -102,8 +105,8 @@ public function testNodeDisplay() {
     // Uncheck the display checkboxes and go to the preview.
     $edit[$field_name . '[0][display]'] = FALSE;
     $edit[$field_name . '[1][display]'] = FALSE;
-    $this->drupalPostForm("node/$nid/edit", $edit, t('Preview'));
-    $this->clickLink(t('Back to content editing'));
+    $this->drupalPostFormHack("node/$nid/edit", $edit, t('Preview'));
+    $this->drupalGet("node/$nid/edit");
     $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.');
     $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.');
   }
diff --git a/core/modules/file/src/Tests/FileFieldFormatterAccessTest.php b/core/modules/file/tests/src/Functional/FileFieldFormatterAccessTest.php
similarity index 95%
rename from core/modules/file/src/Tests/FileFieldFormatterAccessTest.php
rename to core/modules/file/tests/src/Functional/FileFieldFormatterAccessTest.php
index 68bb0473ef..5fbbdf14ff 100644
--- a/core/modules/file/src/Tests/FileFieldFormatterAccessTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldFormatterAccessTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 /**
  * Tests file formatter access.
diff --git a/core/modules/file/src/Tests/FileFieldPathTest.php b/core/modules/file/tests/src/Functional/FileFieldPathTest.php
similarity index 99%
rename from core/modules/file/src/Tests/FileFieldPathTest.php
rename to core/modules/file/tests/src/Functional/FileFieldPathTest.php
index 300b92c3ca..f4eabd457a 100644
--- a/core/modules/file/src/Tests/FileFieldPathTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldPathTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
 
diff --git a/core/modules/file/src/Tests/FileFieldRSSContentTest.php b/core/modules/file/tests/src/Functional/FileFieldRSSContentTest.php
similarity index 89%
rename from core/modules/file/src/Tests/FileFieldRSSContentTest.php
rename to core/modules/file/tests/src/Functional/FileFieldRSSContentTest.php
index af2b5f2b3e..26d14dbfb4 100644
--- a/core/modules/file/src/Tests/FileFieldRSSContentTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldRSSContentTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
 
@@ -60,12 +60,12 @@ public function testFileFieldRSSContent() {
     $this->drupalGet('rss.xml');
     $uploaded_filename = str_replace('public://', '', $node_file->getFileUri());
     $selector = sprintf(
-      'enclosure[url="%s"][length="%s"][type="%s"]',
+      'enclosure[@url="%s"][@length="%s"][@type="%s"]',
       file_create_url("public://$uploaded_filename", ['absolute' => TRUE]),
       $node_file->getSize(),
       $node_file->getMimeType()
     );
-    $this->assertTrue(!empty($this->cssSelect($selector)), 'File field RSS enclosure is displayed when viewing the RSS feed.');
+    $this->assertNotNull($this->getSession()->getDriver()->find('xpath', $selector), 'File field RSS enclosure is displayed when viewing the RSS feed.');
   }
 
 }
diff --git a/core/modules/file/src/Tests/FileFieldRevisionTest.php b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php
similarity index 88%
rename from core/modules/file/src/Tests/FileFieldRevisionTest.php
rename to core/modules/file/tests/src/Functional/FileFieldRevisionTest.php
index 04fbcb8767..7b4bd66965 100644
--- a/core/modules/file/src/Tests/FileFieldRevisionTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
 
@@ -44,7 +44,7 @@ public function testRevisions() {
     $node = $node_storage->load($nid);
     $node_file_r1 = File::load($node->{$field_name}->target_id);
     $node_vid_r1 = $node->getRevisionId();
-    $this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
+    $this->assertFileExistsOnDisk($node_file_r1, 'New file saved to disk on node creation.');
     $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
     $this->assertFileIsPermanent($node_file_r1, 'File is permanent.');
 
@@ -54,7 +54,7 @@ public function testRevisions() {
     $node = $node_storage->load($nid);
     $node_file_r2 = File::load($node->{$field_name}->target_id);
     $node_vid_r2 = $node->getRevisionId();
-    $this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
+    $this->assertFileExistsOnDisk($node_file_r2, 'Replacement file exists on disk after creating new revision.');
     $this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
     $this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
 
@@ -62,7 +62,7 @@ public function testRevisions() {
     $node = node_revision_load($node_vid_r1);
     $current_file = File::load($node->{$field_name}->target_id);
     $this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.');
-    $this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
+    $this->assertFileExistsOnDisk($node_file_r1, 'Original file still in place after replacing file in new revision.');
     $this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
     $this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
 
@@ -87,7 +87,7 @@ public function testRevisions() {
     // Delete the second revision and check that the file is kept (since it is
     // still being used by the third revision).
     $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', [], t('Delete'));
-    $this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.');
+    $this->assertFileExistsOnDisk($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.');
     $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
     $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
 
@@ -100,7 +100,7 @@ public function testRevisions() {
 
     // Delete the third revision and check that the file is not deleted yet.
     $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', [], t('Delete'));
-    $this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.');
+    $this->assertFileExistsOnDisk($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.');
     $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
     $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
 
@@ -125,7 +125,7 @@ public function testRevisions() {
       ->execute();
     \Drupal::service('cron')->run();
 
-    $this->assertFileNotExists($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
+    $this->assertFileNotExistsOnDisk($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
     $this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
 
     // Delete the entire node and check that the original file is deleted.
@@ -140,7 +140,7 @@ public function testRevisions() {
       ->condition('fid', $node_file_r1->id())
       ->execute();
     \Drupal::service('cron')->run();
-    $this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.');
+    $this->assertFileNotExistsOnDisk($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.');
     $this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
   }
 
diff --git a/core/modules/file/tests/src/Functional/FileFieldTestBase.php b/core/modules/file/tests/src/Functional/FileFieldTestBase.php
index d6096874d6..e8f6569f74 100644
--- a/core/modules/file/tests/src/Functional/FileFieldTestBase.php
+++ b/core/modules/file/tests/src/Functional/FileFieldTestBase.php
@@ -7,12 +7,17 @@
 use Drupal\file\FileInterface;
 use Drupal\Tests\BrowserTestBase;
 use Drupal\file\Entity\File;
+use Drupal\Tests\TestFileCreationTrait;
 
 /**
  * Provides methods specifically for testing File module's field handling.
  */
 abstract class FileFieldTestBase extends BrowserTestBase {
 
+  use TestFileCreationTrait {
+    getTestFiles as drupalGetTestFiles;
+  }
+
   /**
   * Modules to enable.
   *
@@ -224,7 +229,7 @@ public function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_re
         $edit[$name][] = $file_path;
       }
     }
-    $this->drupalPostForm("node/$nid/edit", $edit, t('Save and keep published'));
+    $this->drupalPostForm("node/$nid/edit", $edit, t('Save'));
 
     return $nid;
   }
@@ -240,7 +245,7 @@ public function removeNodeFile($nid, $new_revision = TRUE) {
     ];
 
     $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
-    $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
+    $this->drupalPostForm(NULL, $edit, t('Save'));
   }
 
   /**
@@ -253,24 +258,15 @@ public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE)
     ];
 
     $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
-    $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
+    $this->drupalPostForm(NULL, $edit, t('Save'));
   }
 
   /**
    * Asserts that a file exists physically on disk.
-   *
-   * Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with
-   * file entities.
-   *
-   * @param \Drupal\File\FileInterface|string $file
-   *   Either the file entity or the file URI.
-   * @param string $message
-   *   (optional) A message to display with the assertion.
    */
-  public static function assertFileExists($file, $message = NULL) {
+  public function assertFileExistsOnDisk($file, $message = NULL) {
     $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
-    $filename = $file instanceof FileInterface ? $file->getFileUri() : $file;
-    parent::assertFileExists($filename, $message);
+    $this->assertTrue(is_file($file->getFileUri()), $message);
   }
 
   /**
@@ -285,19 +281,10 @@ public function assertFileEntryExists($file, $message = NULL) {
 
   /**
    * Asserts that a file does not exist on disk.
-   *
-   * Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with
-   * file entities.
-   *
-   * @param \Drupal\File\FileInterface|string $file
-   *   Either the file entity or the file URI.
-   * @param string $message
-   *   (optional) A message to display with the assertion.
    */
-  public static function assertFileNotExists($file, $message = NULL) {
+  public function assertFileNotExistsOnDisk($file, $message = NULL) {
     $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
-    $filename = $file instanceof FileInterface ? $file->getFileUri() : $file;
-    parent::assertFileNotExists($filename, $message);
+    $this->assertFalse(is_file($file->getFileUri()), $message);
   }
 
   /**
diff --git a/core/modules/file/src/Tests/FileFieldValidateTest.php b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
similarity index 89%
rename from core/modules/file/src/Tests/FileFieldValidateTest.php
rename to core/modules/file/tests/src/Functional/FileFieldValidateTest.php
index 4698185c4f..8847e87dd3 100644
--- a/core/modules/file/src/Tests/FileFieldValidateTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
 use Drupal\field\Entity\FieldConfig;
@@ -40,7 +40,7 @@ public function testRequired() {
     $node = $node_storage->load($nid);
 
     $node_file = File::load($node->{$field_name}->target_id);
-    $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
+    $this->assertFileExistsOnDisk($node_file, 'File exists after uploading to the required field.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
 
     // Try again with a multiple value field.
@@ -58,7 +58,7 @@ public function testRequired() {
     $node_storage->resetCache([$nid]);
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
-    $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
+    $this->assertFileExistsOnDisk($node_file, 'File exists after uploading to the required multiple value field.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
   }
 
@@ -92,7 +92,7 @@ public function testFileMaxSize() {
       $node_storage->resetCache([$nid]);
       $node = $node_storage->load($nid);
       $node_file = File::load($node->{$field_name}->target_id);
-      $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
+      $this->assertFileExistsOnDisk($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
       $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
 
       // Check that uploading the large file fails (1M limit).
@@ -109,7 +109,7 @@ public function testFileMaxSize() {
     $node_storage->resetCache([$nid]);
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
-    $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
+    $this->assertFileExistsOnDisk($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
     $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
   }
 
@@ -133,7 +133,7 @@ public function testFileExtension() {
     $node_storage->resetCache([$nid]);
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
-    $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
+    $this->assertFileExistsOnDisk($node_file, 'File exists after uploading a file with no extension checking.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
 
     // Enable extension checking for text files.
@@ -152,7 +152,7 @@ public function testFileExtension() {
     $node_storage->resetCache([$nid]);
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
-    $this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
+    $this->assertFileExistsOnDisk($node_file, 'File exists after uploading a file with extension checking.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
   }
 
@@ -175,7 +175,7 @@ public function testFileRemoval() {
     $node_storage->resetCache([$nid]);
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
-    $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
+    $this->assertFileExistsOnDisk($node_file, 'File exists after uploading a file with no extension checking.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
 
     // Enable extension checking for text files.
diff --git a/core/modules/file/src/Tests/FileListingTest.php b/core/modules/file/tests/src/Functional/FileListingTest.php
similarity index 98%
rename from core/modules/file/src/Tests/FileListingTest.php
rename to core/modules/file/tests/src/Functional/FileListingTest.php
index cca21c5a4c..0468f96666 100644
--- a/core/modules/file/src/Tests/FileListingTest.php
+++ b/core/modules/file/tests/src/Functional/FileListingTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\node\Entity\Node;
 use Drupal\file\Entity\File;
@@ -127,7 +127,7 @@ public function testFileListingPages() {
     $usage = $this->sumUsages($file_usage->listUsage($file));
     $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
 
-    $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", [':value' => t('Temporary')]);
+    $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", [':value' => 'Temporary']);
     $this->assertEqual(1, count($result), 'Unused file marked as temporary.');
 
     // Test file usage page.
diff --git a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php b/core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php
similarity index 99%
rename from core/modules/file/src/Tests/FileOnTranslatedEntityTest.php
rename to core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php
index b8a22ae01b..508ead4742 100644
--- a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php
+++ b/core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
 
diff --git a/core/modules/file/src/Tests/FilePrivateTest.php b/core/modules/file/tests/src/Functional/FilePrivateTest.php
similarity index 92%
rename from core/modules/file/src/Tests/FilePrivateTest.php
rename to core/modules/file/tests/src/Functional/FilePrivateTest.php
index 0174a89dc8..82cae97540 100644
--- a/core/modules/file/src/Tests/FilePrivateTest.php
+++ b/core/modules/file/tests/src/Functional/FilePrivateTest.php
@@ -1,11 +1,12 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\Core\Entity\Plugin\Validation\Constraint\ReferenceAccessConstraint;
 use Drupal\Component\Utility\SafeMarkup;
 use Drupal\file\Entity\File;
 use Drupal\node\Entity\NodeType;
+use Drupal\Tests\Traits\DrupalPostFormHack;
 use Drupal\user\RoleInterface;
 
 /**
@@ -15,6 +16,8 @@
  */
 class FilePrivateTest extends FileFieldTestBase {
 
+  use DrupalPostFormHack;
+
   /**
    * Modules to enable.
    *
@@ -80,22 +83,19 @@ public function testPrivateFile() {
     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $edit[$field_name . '[0][fids]'] = $node_file->id();
-    $this->drupalPostForm('node/' . $new_node->id() . '/edit', $edit, t('Save'));
-    // Make sure the form submit failed - we stayed on the edit form.
-    $this->assertUrl('node/' . $new_node->id() . '/edit');
+    $this->drupalPostFormHack('node/' . $new_node->id() . '/edit', $edit, t('Save'));
     // Check that we got the expected constraint form error.
     $constraint = new ReferenceAccessConstraint();
-    $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()]));
+    $this->assertContains(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()])->jsonSerialize(), (string) $this->postResponse->getBody());
     // Attempt to reuse the existing file when creating a new node, and confirm
     // that access is still denied.
     $edit = [];
     $edit['title[0][value]'] = $this->randomMachineName();
     $edit[$field_name . '[0][fids]'] = $node_file->id();
-    $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
+    $this->drupalPostFormHack('node/add/' . $type_name, $edit, t('Save'));
     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $this->assertTrue(empty($new_node), 'Node was not created.');
-    $this->assertUrl('node/add/' . $type_name);
-    $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()]));
+    $this->assertContains(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()])->jsonSerialize(), (string) $this->postResponse->getBody());
 
     // Now make file_test_file_download() return everything.
     \Drupal::state()->set('file_test.allow_all', TRUE);
@@ -142,9 +142,7 @@ public function testPrivateFile() {
     $this->drupalGet($file_url);
     $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the temporary file.');
     // Close the prior connection and remove the session cookie.
-    $this->curlClose();
-    $this->curlCookies = [];
-    $this->cookies = [];
+    $this->getSession()->reset();
     $this->drupalGet($file_url);
     $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the temporary file.');
 
@@ -172,9 +170,7 @@ public function testPrivateFile() {
     $this->drupalGet($file_url);
     $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the file whose references were removed.');
     // Close the prior connection and remove the session cookie.
-    $this->curlClose();
-    $this->curlCookies = [];
-    $this->cookies = [];
+    $this->getSession()->reset();
     $this->drupalGet($file_url);
     $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the file whose references were removed.');
 
@@ -195,9 +191,7 @@ public function testPrivateFile() {
     $this->drupalGet($file_url);
     $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the permanent file that is referenced by a published node.');
     // Close the prior connection and remove the session cookie.
-    $this->curlClose();
-    $this->curlCookies = [];
-    $this->cookies = [];
+    $this->getSession()->reset();
     $this->drupalGet($file_url);
     $this->assertResponse(200, 'Confirmed that another anonymous user also has access to the permanent file that is referenced by a published node.');
 
@@ -222,9 +216,7 @@ public function testPrivateFile() {
     $this->drupalGet($file_url);
     $this->assertResponse(403, 'Confirmed that the anonymous uploader cannot access the permanent file when it is referenced by an unpublished node.');
     // Close the prior connection and remove the session cookie.
-    $this->curlClose();
-    $this->curlCookies = [];
-    $this->cookies = [];
+    $this->getSession()->reset();
     $this->drupalGet($file_url);
     $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the permanent file when it is referenced by an unpublished node.');
   }
diff --git a/core/modules/file/src/Tests/FileTokenReplaceTest.php b/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php
similarity index 99%
rename from core/modules/file/src/Tests/FileTokenReplaceTest.php
rename to core/modules/file/tests/src/Functional/FileTokenReplaceTest.php
index 5bfa07b9ab..fd955913b9 100644
--- a/core/modules/file/src/Tests/FileTokenReplaceTest.php
+++ b/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\Component\Utility\Html;
 use Drupal\Core\Render\BubbleableMetadata;
diff --git a/core/modules/file/src/Tests/PrivateFileOnTranslatedEntityTest.php b/core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php
similarity index 99%
rename from core/modules/file/src/Tests/PrivateFileOnTranslatedEntityTest.php
rename to core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php
index da5abb5bd7..bc97a15e7c 100644
--- a/core/modules/file/src/Tests/PrivateFileOnTranslatedEntityTest.php
+++ b/core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
 use Drupal\node\Entity\Node;
diff --git a/core/modules/file/src/Tests/RemoteFileSaveUploadTest.php b/core/modules/file/tests/src/Functional/RemoteFileSaveUploadTest.php
similarity index 90%
rename from core/modules/file/src/Tests/RemoteFileSaveUploadTest.php
rename to core/modules/file/tests/src/Functional/RemoteFileSaveUploadTest.php
index d468ed326f..e0e4be4b3c 100644
--- a/core/modules/file/src/Tests/RemoteFileSaveUploadTest.php
+++ b/core/modules/file/tests/src/Functional/RemoteFileSaveUploadTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 /**
  * Tests the file uploading functions.
diff --git a/core/modules/file/src/Tests/SaveUploadFormTest.php b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php
similarity index 94%
rename from core/modules/file/src/Tests/SaveUploadFormTest.php
rename to core/modules/file/tests/src/Functional/SaveUploadFormTest.php
index 6d51198a7f..ab178707f5 100644
--- a/core/modules/file/src/Tests/SaveUploadFormTest.php
+++ b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php
@@ -1,8 +1,10 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
+use Drupal\Tests\TestFileCreationTrait;
+use Drupal\Tests\Traits\DrupalPostFormHack;
 
 /**
  * Tests the _file_save_upload_from_form() function.
@@ -13,6 +15,12 @@
  */
 class SaveUploadFormTest extends FileManagedTestBase {
 
+  use TestFileCreationTrait {
+    getTestFiles as drupalGetTestFiles;
+  }
+
+  use DrupalPostFormHack;
+
   /**
    * Modules to enable.
    *
@@ -351,10 +359,9 @@ public function testDrupalMovingUploadedFileError() {
     ];
 
     \Drupal::state()->set('file_test.disable_error_collection', TRUE);
-    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
-    $this->assertResponse(200, 'Received a 200 response for posted test file.');
-    $this->assertRaw(t('File upload error. Could not move uploaded file.'), 'Found the failure message.');
-    $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
+    $this->drupalPostFormHack('file-test/save_upload_from_form_test', $edit, t('Submit'));
+    $this->assertContains(t('File upload error. Could not move uploaded file.')->render(), (string) $this->postResponse->getBody(), 'Found the failure message.');
+    $this->assertContains(t('Epic upload FAIL!')->render(), (string) $this->postResponse->getBody(), 'Found the failure message.');
 
     // Uploading failed. Now check the log.
     $this->drupalGet('admin/reports/dblog');
@@ -433,12 +440,11 @@ public function testCombinedErrorMessages() {
       'extensions' => 'jpeg',
     ];
 
-    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
-    $this->assertResponse(200, 'Received a 200 response for posted test file.');
-    $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
+    $this->drupalPostFormHack('file-test/save_upload_from_form_test', $edit, t('Submit'));
+    $this->assertContains(t('Epic upload FAIL!')->render(), (string) $this->postResponse->getBody(), 'Found the failure message.');
 
     // Search for combined error message followed by a formatted list of messages.
-    $this->assertRaw(t('One or more files could not be uploaded.') . '<div class="item-list">', 'Error message contains combined list of validation errors.');
+    $this->assertContains(t('One or more files could not be uploaded.') . '<div class="item-list">', (string) $this->postResponse->getBody());
   }
 
   /**
diff --git a/core/modules/file/src/Tests/SaveUploadTest.php b/core/modules/file/tests/src/Functional/SaveUploadTest.php
similarity index 98%
rename from core/modules/file/src/Tests/SaveUploadTest.php
rename to core/modules/file/tests/src/Functional/SaveUploadTest.php
index a9f69c2fb7..9382c8b4a6 100644
--- a/core/modules/file/src/Tests/SaveUploadTest.php
+++ b/core/modules/file/tests/src/Functional/SaveUploadTest.php
@@ -1,8 +1,9 @@
 <?php
 
-namespace Drupal\file\Tests;
+namespace Drupal\Tests\file\Functional;
 
 use Drupal\file\Entity\File;
+use Drupal\Tests\TestFileCreationTrait;
 
 /**
  * Tests the file_save_upload() function.
@@ -10,6 +11,11 @@
  * @group file
  */
 class SaveUploadTest extends FileManagedTestBase {
+
+  use TestFileCreationTrait {
+    getTestFiles as drupalGetTestFiles;
+  }
+
   /**
    * Modules to enable.
    *
diff --git a/core/modules/file/src/Tests/Views/RelationshipUserFileDataTest.php b/core/modules/file/tests/src/Functional/Views/RelationshipUserFileDataTest.php
similarity index 92%
rename from core/modules/file/src/Tests/Views/RelationshipUserFileDataTest.php
rename to core/modules/file/tests/src/Functional/Views/RelationshipUserFileDataTest.php
index 6a5f53b5d3..0169e0a6d8 100644
--- a/core/modules/file/src/Tests/Views/RelationshipUserFileDataTest.php
+++ b/core/modules/file/tests/src/Functional/Views/RelationshipUserFileDataTest.php
@@ -1,10 +1,10 @@
 <?php
 
-namespace Drupal\file\Tests\Views;
+namespace Drupal\Tests\file\Functional\Views;
 
 use Drupal\field\Entity\FieldConfig;
 use Drupal\file\Entity\File;
-use Drupal\views\Tests\ViewTestBase;
+use Drupal\Tests\views\Functional\ViewTestBase;
 use Drupal\views\Views;
 use Drupal\views\Tests\ViewTestData;
 use Drupal\field\Entity\FieldStorageConfig;
@@ -30,8 +30,8 @@ class RelationshipUserFileDataTest extends ViewTestBase {
    */
   public static $testViews = ['test_file_user_file_data'];
 
-  protected function setUp() {
-    parent::setUp();
+  protected function setUp($import_test_views = TRUE) {
+    parent::setUp($import_test_views);
 
     // Create the user profile field and instance.
     FieldStorageConfig::create([
diff --git a/core/tests/Drupal/Tests/Traits/DrupalPostFormHack.php b/core/tests/Drupal/Tests/Traits/DrupalPostFormHack.php
new file mode 100644
index 0000000000..01a08c52dd
--- /dev/null
+++ b/core/tests/Drupal/Tests/Traits/DrupalPostFormHack.php
@@ -0,0 +1,286 @@
+<?php
+
+namespace Drupal\Tests\Traits;
+
+use GuzzleHttp\Cookie\CookieJar;
+
+trait DrupalPostFormHack {
+
+  /**
+   * The response object after last drupalPostFormHack() call.
+   *
+   * @var \Psr\Http\Message\ResponseInterface
+   */
+  protected $postResponse;
+
+  /**
+   * Executes multipart POST request with Guzzle client.
+   *
+   * @param \Drupal\Core\Url|string $path
+   *   Location of the post form.
+   * @param array $edit
+   *   Field data in an associative array.
+   * @param string|object $submit
+   *   Value of the submit button whose click is to be emulated.
+   * @param array $options
+   *   Options to be forwarded to the url generator.
+   * @param array $headers
+   *   An array containing additional HTTP request headers.
+   * @param string $form_html_id
+   *   (optional) HTML ID of the form to be submitted.
+   * @param string $extra_post
+   *   (optional) A string of additional data to append to the POST submission.
+   */
+  protected function drupalPostFormHack($path, array $edit, $submit, array $options = [], array $headers = [], $form_html_id = NULL, $extra_post = NULL) {
+
+    if (is_object($submit)) {
+      // Cast MarkupInterface objects to string.
+      $submit = (string) $submit;
+    }
+    if ($edit === NULL) {
+      $edit = [];
+    }
+    if (is_array($edit)) {
+      $edit = $this->castSafeStrings($edit);
+    }
+
+    if (isset($path)) {
+      $this->drupalGet($path, $options);
+    }
+
+    $edit_save = $edit;
+    // Let's iterate over all the forms.
+    $xpath = "//form";
+    if (!empty($form_html_id)) {
+      $xpath .= "[@id='" . $form_html_id . "']";
+    }
+    $forms = $this->xpath($xpath);
+    foreach ($forms as $form) {
+      // We try to set the fields of this form as specified in $edit.
+      $edit = $edit_save;
+      $post = [];
+      $upload = [];
+      $submit_matches = $this->handleForm($post, $edit, $upload, $submit, $form);
+      $action = $form->hasAttribute('action') ? $this->getAbsoluteUrl($form->getAttribute('action')) : $this->getUrl();
+      // We post only if we managed to handle every field in edit and the
+      // submit button matches.
+      if (!$edit && ($submit_matches || !isset($submit))) {
+        $post_multipart = [];
+        foreach ($post as $key => $value) {
+          $post_multipart[] = [
+            'name' => $key,
+            'contents' => $value,
+          ];
+        }
+
+        if ($upload) {
+          foreach ($upload as $key => $file) {
+            if (is_array($file) && count($file)) {
+              // There seems to be no way via php's API to cURL to upload
+              // several files with the same post field name. However, Drupal
+              // still sees array-index syntax in a similar way.
+              for ($i = 0; $i < count($file); $i++) {
+                $postfield = str_replace('[]', '', $key) . '[' . $i . ']';
+                $file_path = $this->container->get('file_system')
+                  ->realpath($file[$i]);
+                $post_multipart[] = [
+                  'name' => $postfield,
+                  'contents' => fopen($file_path, 'r'),
+                ];
+              }
+            }
+            else {
+              $file = $this->container->get('file_system')->realpath($file);
+              if ($file && is_file($file)) {
+                $post_multipart[] = [
+                  'name' => $key,
+                  'contents' => fopen($file, 'r'),
+                ];
+              }
+            }
+          }
+        }
+
+        $domain = parse_url($this->getUrl(), PHP_URL_HOST);
+        $session_id = $this->getSession()->getCookie($this->getSessionName());
+        $cookies = CookieJar::fromArray([$this->getSessionName() => $session_id], $domain);
+
+        /** @var \GuzzleHttp\Client $client */
+        $client = $this->getSession()->getDriver()->getClient()->getClient();
+
+        $response = $client->post($action, [
+          'multipart' => $post_multipart,
+          'cookies' => $cookies,
+          'http_errors' => FALSE,
+        ]);
+        $this->postResponse = $response;
+      }
+    }
+  }
+
+  /**
+   * Handles form input related to drupalPostFormHack().
+   *
+   * Ensure that the specified fields exist and attempt to create POST data in
+   * the correct manner for the particular field type.
+   *
+   * @param $post
+   *   Reference to array of post values.
+   * @param $edit
+   *   Reference to array of edit values to be checked against the form.
+   * @param $submit
+   *   Form submit button value.
+   * @param \Behat\Mink\Element\NodeElement $form
+   *   Array of form elements.
+   *
+   * @return
+   *   Submit value matches a valid submit input in the form.
+   */
+  protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
+    // Retrieve the form elements.
+    $elements = $form->findAll('xpath', './/input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]');
+    $submit_matches = FALSE;
+    /** @var \Behat\Mink\Element\NodeElement $element */
+    foreach ($elements as $element) {
+      // SimpleXML objects need string casting all the time.
+      $name = $element->getAttribute('name');
+      // This can either be the type of <input> or the name of the tag itself
+      // for <select> or <textarea>.
+      $type = $element->getAttribute('type') ?: $element->getTagName();
+      $value = $element->getValue() ?: '';
+      $done = FALSE;
+      if (isset($edit[$name])) {
+        switch ($type) {
+          case 'text':
+          case 'tel':
+          case 'textarea':
+          case 'url':
+          case 'number':
+          case 'range':
+          case 'color':
+          case 'hidden':
+          case 'password':
+          case 'email':
+          case 'search':
+          case 'date':
+          case 'time':
+          case 'datetime':
+          case 'datetime-local';
+            $post[$name] = $edit[$name];
+            unset($edit[$name]);
+            break;
+          case 'radio':
+            if ($edit[$name] == $value) {
+              $post[$name] = $edit[$name];
+              unset($edit[$name]);
+            }
+            break;
+          case 'checkbox':
+            // To prevent checkbox from being checked.pass in a FALSE,
+            // otherwise the checkbox will be set to its value regardless
+            // of $edit.
+            if ($edit[$name] === FALSE) {
+              unset($edit[$name]);
+              continue 2;
+            }
+            else {
+              unset($edit[$name]);
+              $post[$name] = $value;
+            }
+            break;
+          case 'select':
+            $new_value = $edit[$name];
+            $options = $element->findAll('xpath', '//option');
+            if (is_array($new_value)) {
+              // Multiple select box.
+              if (!empty($new_value)) {
+                $index = 0;
+                $key = preg_replace('/\[\]$/', '', $name);
+                /** @var \Behat\Mink\Element\NodeElement $option */
+                foreach ($options as $option) {
+                  $option_value = $option->getValue();
+                  if (in_array($option_value, $new_value)) {
+                    $post[$key . '[' . $index++ . ']'] = $option_value;
+                    $done = TRUE;
+                    unset($edit[$name]);
+                  }
+                }
+              }
+              else {
+                // No options selected: do not include any POST data for the
+                // element.
+                $done = TRUE;
+                unset($edit[$name]);
+              }
+            }
+            else {
+              // Single select box.
+              /** @var \Behat\Mink\Element\NodeElement $option */
+              foreach ($options as $option) {
+                if ($new_value == $option->getValue()) {
+                  $post[$name] = $new_value;
+                  unset($edit[$name]);
+                  $done = TRUE;
+                  break;
+                }
+              }
+            }
+            break;
+          case 'file':
+            $upload[$name] = $edit[$name];
+            unset($edit[$name]);
+            break;
+        }
+      }
+      if (!isset($post[$name]) && !$done) {
+        switch ($type) {
+          case 'textarea':
+            $post[$name] = $element->getText();
+            break;
+          case 'select':
+            $single = empty($element->getAttribute('multiple'));
+            $first = TRUE;
+            $index = 0;
+            $key = preg_replace('/\[\]$/', '', $name);
+            $options = $element->findAll('xpath', '//option');
+            /** @var \Behat\Mink\Element\NodeElement $option */
+            foreach ($options as $option) {
+              // For single select, we load the first option, if there is a
+              // selected option that will overwrite it later.
+              if ($option->getAttribute('selected') || ($first && $single)) {
+                $first = FALSE;
+                if ($single) {
+                  $post[$name] = $option->getValue();
+                }
+                else {
+                  $post[$key . '[' . $index++ . ']'] = $option->getValue();
+                }
+              }
+            }
+            break;
+          case 'file':
+            break;
+          case 'submit':
+          case 'image':
+            if (isset($submit) && $submit == $value) {
+              $post[$name] = $value;
+              $submit_matches = TRUE;
+            }
+            break;
+          case 'radio':
+          case 'checkbox':
+            if (!$element->hasAttribute('checked')) {
+              break;
+            }
+          // Deliberate no break.
+          default:
+            $post[$name] = $value;
+        }
+      }
+    }
+    // An empty name means the value is not sent.
+    unset($post['']);
+    return $submit_matches;
+  }
+
+}
