diff --git a/core/modules/comment/src/Tests/CommentPreviewTest.php b/core/modules/comment/src/Tests/CommentPreviewTest.php
index 75aca4d..3bb5ccf 100644
--- a/core/modules/comment/src/Tests/CommentPreviewTest.php
+++ b/core/modules/comment/src/Tests/CommentPreviewTest.php
@@ -41,7 +41,7 @@ function testCommentPreview() {
 
     // Login as web user and add a user picture.
     $this->drupalLogin($this->webUser);
-    $image = current($this->drupalGetTestFiles('image'));
+    $image = $this->drupalGetTestImageFile();
     $edit['files[user_picture_0]'] = drupal_realpath($image->uri);
     $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save'));
 
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index e351459..f6163d3 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -391,7 +391,7 @@ function file_validate_is_image(FileInterface $file) {
   $image = $image_factory->get($file->getFileUri());
   if (!$image->isValid()) {
     $supported_extensions = $image_factory->getSupportedExtensions();
-    $errors[] = t('Image type not supported. Allowed types: %types', array('%types' => implode(' ', $supported_extensions)));
+    $errors[] = t('The image is invalid, or not supported. Allowed types: %types', array('%types' => implode(' ', $supported_extensions)));
   }
 
   return $errors;
diff --git a/core/modules/file/src/Tests/SaveUploadTest.php b/core/modules/file/src/Tests/SaveUploadTest.php
index 5b0dbc1..d477c29 100644
--- a/core/modules/file/src/Tests/SaveUploadTest.php
+++ b/core/modules/file/src/Tests/SaveUploadTest.php
@@ -49,8 +49,7 @@ protected function setUp() {
     $account = $this->drupalCreateUser(array('access site reports'));
     $this->drupalLogin($account);
 
-    $image_files = $this->drupalGetTestFiles('image');
-    $this->image = entity_create('file', (array) current($image_files));
+    $this->image = entity_create('file', (array) $this->drupalGetTestImageFile());
 
     list(, $this->imageExtension) = explode('.', $this->image->getFilename());
     $this->assertTrue(is_file($this->image->getFileUri()), "The image file we're going to upload exists.");
@@ -90,7 +89,7 @@ function testNormal() {
     file_test_reset();
 
     // Upload a second file.
-    $image2 = current($this->drupalGetTestFiles('image'));
+    $image2 = $this->drupalGetTestImageFile();
     $edit = array('files[file_test_upload]' => drupal_realpath($image2->uri));
     $this->drupalPostForm('file-test/upload', $edit, t('Submit'));
     $this->assertResponse(200, 'Received a 200 response for posted test file.');
@@ -111,7 +110,7 @@ function testNormal() {
     $this->assertTrue(isset($files[$file2->id()]), 'File was loaded successfully');
 
     // Upload a third file to a subdirectory.
-    $image3 = current($this->drupalGetTestFiles('image'));
+    $image3 = $this->drupalGetTestImageFile();
     $image3_realpath = drupal_realpath($image3->uri);
     $dir = $this->randomMachineName();
     $edit = array(
diff --git a/core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php b/core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php
index df860f2..a30363b 100644
--- a/core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php
+++ b/core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php
@@ -94,14 +94,14 @@ function testImageSource() {
     $title_text = t('This image has been removed. For security reasons, only images from the local domain are allowed.');
 
     // Put a test image in the files directory.
-    $test_images = $this->drupalGetTestFiles('image');
-    $test_image = $test_images[0]->filename;
+    $image_file = $this->drupalGetTestImageFile();
+    $test_image = $image_file->filename;
 
     // Put a test image in the files directory with special filename.
     $special_filename = 'tést fïle nàme.png';
     $special_image = rawurlencode($special_filename);
-    $special_uri = str_replace($test_images[0]->filename, $special_filename, $test_images[0]->uri);
-    file_unmanaged_copy($test_images[0]->uri, $special_uri);
+    $special_uri = str_replace($image_file->filename, $special_filename, $image_file->uri);
+    file_unmanaged_copy($image_file->uri, $special_uri);
 
     // Create a list of test image sources.
     // The keys become the value of the IMG 'src' attribute, the values are the
diff --git a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
index e2b6816..0ed5b13 100644
--- a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
+++ b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
@@ -116,6 +116,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
 
     $field_settings = $this->getFieldSettings();
 
+    // Add image validation.
+    $element['#upload_validators']['file_validate_is_image'] = array();
+
     // Add upload resolution validation.
     if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
       $element['#upload_validators']['file_validate_image_resolution'] = array($field_settings['max_resolution'], $field_settings['min_resolution']);
diff --git a/core/modules/image/src/Tests/FileMoveTest.php b/core/modules/image/src/Tests/FileMoveTest.php
index 34c54a7..8d457e8 100644
--- a/core/modules/image/src/Tests/FileMoveTest.php
+++ b/core/modules/image/src/Tests/FileMoveTest.php
@@ -29,7 +29,7 @@ class FileMoveTest extends WebTestBase {
    */
   function testNormal() {
     // Pick a file for testing.
-    $file = entity_create('file', (array) current($this->drupalGetTestFiles('image')));
+    $file = entity_create('file', (array) $this->drupalGetTestImageFile());
 
     // Create derivative image.
     $styles = ImageStyle::loadMultiple();
diff --git a/core/modules/image/src/Tests/ImageAdminStylesTest.php b/core/modules/image/src/Tests/ImageAdminStylesTest.php
index dedb9b5..10f6f76 100644
--- a/core/modules/image/src/Tests/ImageAdminStylesTest.php
+++ b/core/modules/image/src/Tests/ImageAdminStylesTest.php
@@ -28,8 +28,7 @@ function createSampleImage(ImageStyleInterface $style) {
     // First, we need to make sure we have an image in our testing
     // file directory. Copy over an image on the first run.
     if (!isset($file_path)) {
-      $files = $this->drupalGetTestFiles('image');
-      $file = reset($files);
+      $file = $this->drupalGetTestImageFile();
       $file_path = file_unmanaged_copy($file->uri);
     }
 
@@ -296,7 +295,7 @@ function testStyleReplacement() {
       ->save();
 
     // Create a new node with an image attached.
-    $test_image = current($this->drupalGetTestFiles('image'));
+    $test_image = $this->drupalGetTestImageFile();
     $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
     $node = Node::load($nid);
 
@@ -390,8 +389,8 @@ public function testFlushUserInterface() {
     $style->save();
 
     // Create an image to make sure it gets flushed.
-    $files = $this->drupalGetTestFiles('image');
-    $image_uri = $files[0]->uri;
+    $image_file = $this->drupalGetTestImageFile();
+    $image_uri = $image_file->uri;
     $derivative_uri = $style->buildUri($image_uri);
     $this->assertTrue($style->createDerivative($image_uri, $derivative_uri));
     $this->assertEqual($this->getImageCount($style), 1);
@@ -430,7 +429,7 @@ function testConfigImport() {
       ->save();
 
     // Create a new node with an image attached.
-    $test_image = current($this->drupalGetTestFiles('image'));
+    $test_image = $this->drupalGetTestImageFile();
     $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
     $node = Node::load($nid);
 
diff --git a/core/modules/image/src/Tests/ImageDimensionsTest.php b/core/modules/image/src/Tests/ImageDimensionsTest.php
index 2db7e24..23d31cc 100644
--- a/core/modules/image/src/Tests/ImageDimensionsTest.php
+++ b/core/modules/image/src/Tests/ImageDimensionsTest.php
@@ -31,8 +31,7 @@ class ImageDimensionsTest extends WebTestBase {
   function testImageDimensions() {
     $image_factory = $this->container->get('image.factory');
     // Create a working copy of the file.
-    $files = $this->drupalGetTestFiles('image');
-    $file = reset($files);
+    $file = $this->drupalGetTestImageFile();
     $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
 
     // Create a style.
diff --git a/core/modules/image/src/Tests/ImageFieldDefaultImagesTest.php b/core/modules/image/src/Tests/ImageFieldDefaultImagesTest.php
index 39e0d50..ea13176 100644
--- a/core/modules/image/src/Tests/ImageFieldDefaultImagesTest.php
+++ b/core/modules/image/src/Tests/ImageFieldDefaultImagesTest.php
@@ -28,19 +28,22 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
    */
   public function testDefaultImages() {
     $node_storage = $this->container->get('entity.manager')->getStorage('node');
-    // Create files to use as the default images.
-    $files = $this->drupalGetTestFiles('image');
-    // Create 10 files so the default image fids are not a single value.
+
+    // Create 10 file entities so the default image fids are not a single
+    // value.
     for ($i = 1; $i <= 10; $i++) {
       $filename = $this->randomMachineName() . "$i";
       $desired_filepath = 'public://' . $filename;
-      file_unmanaged_copy($files[0]->uri, $desired_filepath, FILE_EXISTS_ERROR);
+      file_unmanaged_copy($this->drupalGetTestImageFile()->uri, $desired_filepath, FILE_EXISTS_ERROR);
       $file = entity_create('file', array('uri' => $desired_filepath, 'filename' => $filename, 'name' => $filename));
       $file->save();
     }
+
+    // Create file entities to use as the default images.
+    $files = ['image-1.png', 'image-test.jpg', 'image-2.jpg', 'image-test-no-transparency.gif', 'image-test-transparent-out-of-range.gif'];
     $default_images = array();
     foreach (array('field', 'field', 'field2', 'field_new', 'field_new') as $image_target) {
-      $file = entity_create('file', (array) array_pop($files));
+      $file = entity_create('file', (array) $this->drupalGetTestImageFile(array_shift($files)));
       $file->save();
       $default_images[$image_target] = $file;
     }
diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php
index 875a2a2..4acc4a4 100644
--- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php
+++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php
@@ -75,7 +75,7 @@ function _testImageFieldFormatters($scheme) {
     user_role_change_permissions(reset($admin_user_roles), array('administer image styles' => TRUE));
 
     // Create a new node with an image attached.
-    $test_image = current($this->drupalGetTestFiles('image'));
+    $test_image = $this->drupalGetTestImageFile();
 
     // Ensure that preview works.
     $this->previewNodeImage($test_image, $field_name, 'article');
@@ -207,7 +207,7 @@ function _testImageFieldFormatters($scheme) {
    */
   function testImageFieldSettings() {
     $node_storage = $this->container->get('entity.manager')->getStorage('node');
-    $test_image = current($this->drupalGetTestFiles('image'));
+    $test_image = $this->drupalGetTestImageFile();
     list(, $test_image_extension) = explode('.', $test_image->filename);
     $field_name = strtolower($this->randomMachineName());
     $field_settings = array(
@@ -341,11 +341,11 @@ function testImageFieldDefaultImage() {
     $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
 
     // Add a default image to the public image field.
-    $images = $this->drupalGetTestFiles('image');
+    $image = $this->drupalGetTestImageFile('image-test.png');
     $alt = $this->randomString(512);
     $title = $this->randomString(1024);
     $edit = array(
-      'files[settings_default_image_uuid]' => drupal_realpath($images[0]->uri),
+      'files[settings_default_image_uuid]' => drupal_realpath($image->uri),
       'settings[default_image][alt]' => $alt,
       'settings[default_image][title]' => $title,
     );
@@ -377,7 +377,8 @@ function testImageFieldDefaultImage() {
     // Create alt text for the image.
     $alt = $this->randomMachineName();
 
-    $nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt);
+    $image_file = $this->drupalGetTestImageFile('image-test.jpg');
+    $nid = $this->uploadNodeImage($image_file, $field_name, 'article', $alt);
     $node_storage->resetCache(array($nid));
     $node = $node_storage->load($nid);
     $file = $node->{$field_name}->entity;
@@ -412,7 +413,7 @@ function testImageFieldDefaultImage() {
     $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
     // Add a default image to the new field.
     $edit = array(
-      'files[settings_default_image_uuid]' => drupal_realpath($images[1]->uri),
+      'files[settings_default_image_uuid]' => drupal_realpath($image_file->uri),
       'settings[default_image][alt]' => $alt,
       'settings[default_image][title]' => $title,
     );
diff --git a/core/modules/image/src/Tests/ImageFieldValidateTest.php b/core/modules/image/src/Tests/ImageFieldValidateTest.php
index 938176a..080692c 100644
--- a/core/modules/image/src/Tests/ImageFieldValidateTest.php
+++ b/core/modules/image/src/Tests/ImageFieldValidateTest.php
@@ -14,6 +14,40 @@
  */
 class ImageFieldValidateTest extends ImageFieldTestBase {
   /**
+   * Test image validity.
+   */
+  function testValid() {
+    $field_name = strtolower($this->randomMachineName());
+    $this->createImageField($field_name, 'article', array(), array());
+
+    // Create alt text for the image.
+    $alt = $this->randomMachineName();
+
+    // Create a node with a valid image.
+    $node = $this->uploadNodeImage($this->drupalGetTestImageFile('image-1.png'), $field_name, 'article', $alt);
+
+    // Remove the image.
+    $this->drupalPostForm('node/' . $node . '/edit', array(), t('Remove'));
+    $this->drupalPostForm(NULL, array(), t('Save and keep published'));
+
+    // Try uploading a zero-byte image.
+    $zero_size_image = $this->drupalGetTestImageFile('image-zero-size.png');
+    $edit = array(
+      'files[' . $field_name . '_0]' => drupal_realpath($zero_size_image->uri),
+    );
+    $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
+    $this->assertFalse(file_exists('public://image-zero-size_0.png'), 'Zero-byte image file was not loaded.');
+
+    // Try uploading an invalid image.
+    $invalid_image = $this->drupalGetTestImageFile('image-invalid.png');
+    $edit = array(
+      'files[' . $field_name . '_0]' => drupal_realpath($invalid_image->uri),
+    );
+    $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
+    $this->assertFalse(file_exists('public://image-invalid_0.png'), 'Invalid image file was not loaded.');
+  }
+
+  /**
    * Test min/max resolution settings.
    */
   function testResolution() {
@@ -28,24 +62,11 @@ function testResolution() {
     $this->createImageField($field_name, 'article', array(), $field_settings);
 
     // We want a test image that is too small, and a test image that is too
-    // big, so cycle through test image files until we have what we need.
-    $image_that_is_too_big = FALSE;
-    $image_that_is_too_small = FALSE;
-    $image_factory = $this->container->get('image.factory');
-    foreach ($this->drupalGetTestFiles('image') as $image) {
-      $image_file = $image_factory->get($image->uri);
-      if ($image_file->getWidth() > $max_resolution) {
-        $image_that_is_too_big = $image;
-      }
-      if ($image_file->getWidth() < $min_resolution) {
-        $image_that_is_too_small = $image;
-      }
-      if ($image_that_is_too_small && $image_that_is_too_big) {
-        break;
-      }
-    }
+    // big.
+    $image_that_is_too_small = $this->drupalGetTestImageFile('image-test.jpg');
     $this->uploadNodeImage($image_that_is_too_small, $field_name, 'article');
     $this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $image_that_is_too_small->filename)) . ' ' . t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => '50x50')), 'Node save failed when minimum image resolution was not met.');
+    $image_that_is_too_big = $this->drupalGetTestImageFile('image-1.png');
     $this->uploadNodeImage($image_that_is_too_big, $field_name, 'article');
     $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'), 'Image exceeding max resolution was properly resized.');
   }
@@ -63,9 +84,8 @@ function testRequiredAttributes() {
       'required' => 1,
     );
     $instance = $this->createImageField($field_name, 'article', array(), $field_settings);
-    $images = $this->drupalGetTestFiles('image');
-    // Let's just use the first image.
-    $image = $images[0];
+    // Make sure we use a valid image.
+    $image = $this->drupalGetTestImageFile();
     $this->uploadNodeImage($image, $field_name, 'article');
 
     // Look for form-required for the alt text.
diff --git a/core/modules/image/src/Tests/ImageStyleFlushTest.php b/core/modules/image/src/Tests/ImageStyleFlushTest.php
index 0a6a5fa..e092f61 100644
--- a/core/modules/image/src/Tests/ImageStyleFlushTest.php
+++ b/core/modules/image/src/Tests/ImageStyleFlushTest.php
@@ -23,8 +23,7 @@ function createSampleImage($style, $wrapper) {
     static $file;
 
     if (!isset($file)) {
-      $files = $this->drupalGetTestFiles('image');
-      $file = reset($files);
+      $file = $this->drupalGetTestImageFile();
     }
 
     // Make sure we have an image in our wrapper testing file directory.
diff --git a/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php
index 16fc8e6..8307a7a 100644
--- a/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php
+++ b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php
@@ -111,8 +111,7 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash =
     $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.');
 
     // Create a working copy of the file.
-    $files = $this->drupalGetTestFiles('image');
-    $file = array_shift($files);
+    $file = $this->drupalGetTestImageFile('image-test.png');
     $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
     // Let the image_module_test module know about this file, so it can claim
     // ownership in hook_file_download().
@@ -178,7 +177,7 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash =
 
       // Repeat this with a different file that we do not have access to and
       // make sure that access is denied.
-      $file_noaccess = array_shift($files);
+      $file_noaccess = $this->drupalGetTestImageFile('image-test.jpg');
       $original_uri_noaccess = file_unmanaged_copy($file_noaccess->uri, $scheme . '://', FILE_EXISTS_RENAME);
       $generated_uri_noaccess = $scheme . '://styles/' . $this->style->id() . '/' . $scheme . '/'. drupal_basename($original_uri_noaccess);
       $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.');
@@ -207,8 +206,7 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash =
     $this->config('image.settings')->set('allow_insecure_derivatives', TRUE)->save();
 
     // Create another working copy of the file.
-    $files = $this->drupalGetTestFiles('image');
-    $file = array_shift($files);
+    $file = $this->drupalGetTestImageFile('image-test.png');
     $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
     // Let the image_module_test module know about this file, so it can claim
     // ownership in hook_file_download().
diff --git a/core/modules/image/src/Tests/ImageThemeFunctionTest.php b/core/modules/image/src/Tests/ImageThemeFunctionTest.php
index a67c2eb..b9d949a 100644
--- a/core/modules/image/src/Tests/ImageThemeFunctionTest.php
+++ b/core/modules/image/src/Tests/ImageThemeFunctionTest.php
@@ -64,8 +64,7 @@ protected function setUp() {
    */
   function testImageFormatterTheme() {
     // Create an image.
-    $files = $this->drupalGetTestFiles('image');
-    $file = reset($files);
+    $file = $this->drupalGetTestImageFile();
     $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
 
     // Create a style.
@@ -123,8 +122,7 @@ function testImageFormatterTheme() {
    */
   function testImageStyleTheme() {
     // Create an image.
-    $files = $this->drupalGetTestFiles('image');
-    $file = reset($files);
+    $file = $this->drupalGetTestImageFile();
     $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
 
     // Create a style.
diff --git a/core/modules/rdf/src/Tests/ImageFieldAttributesTest.php b/core/modules/rdf/src/Tests/ImageFieldAttributesTest.php
index 4ae8f0b..bdbaa06 100644
--- a/core/modules/rdf/src/Tests/ImageFieldAttributesTest.php
+++ b/core/modules/rdf/src/Tests/ImageFieldAttributesTest.php
@@ -64,7 +64,7 @@ protected function setUp() {
       ->save();
 
     // Get the test image that simpletest provides.
-    $image = current($this->drupalGetTestFiles('image'));
+    $image = $this->drupalGetTestImageFile();
 
     // Save a node with the image.
     $nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
diff --git a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php
index ccc09be..a85b54d 100644
--- a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php
+++ b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php
@@ -166,9 +166,9 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
     $node_storage = $this->container->get('entity.manager')->getStorage('node');
     $field_name = Unicode::strtolower($this->randomMachineName());
     $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
-    // Create a new node with an image attached. Make sure we use a large image
+    // Create a new node with an image attached. We use a large image
     // so the scale effects of the image styles always have an effect.
-    $test_image = current($this->drupalGetTestFiles('image', 39325));
+    $test_image = $this->drupalGetTestImageFile('image-1.png');
 
     // Create alt text for the image.
     $alt = $this->randomMachineName();
@@ -331,7 +331,7 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() {
     $field_name = Unicode::strtolower($this->randomMachineName());
     $this->createImageField($field_name, 'article', array('uri_scheme' => 'public'));
     // Create a new node with an image attached.
-    $test_image = current($this->drupalGetTestFiles('image'));
+    $test_image = $this->drupalGetTestImageFile();
     $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
     $node_storage->resetCache(array($nid));
 
@@ -371,7 +371,7 @@ private function assertResponsiveImageFieldFormattersLink($link_type) {
     $field_settings = array('alt_field_required' => 0);
     $this->createImageField($field_name, 'article', array('uri_scheme' => 'public'), $field_settings);
     // Create a new node with an image attached.
-    $test_image = current($this->drupalGetTestFiles('image'));
+    $test_image = $this->drupalGetTestImageFile();
 
     // Test the image linked to file formatter.
     $display_options = array(
diff --git a/core/modules/simpletest/files/image-invalid.png b/core/modules/simpletest/files/image-invalid.png
new file mode 100644
index 0000000..8175b44
--- /dev/null
+++ b/core/modules/simpletest/files/image-invalid.png
@@ -0,0 +1 @@
+invalid image file
diff --git a/core/modules/simpletest/files/image-zero-size.png b/core/modules/simpletest/files/image-zero-size.png
new file mode 100644
index 0000000..e69de29
diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index 6ace8a8..c2faf1e 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -503,6 +503,25 @@ protected function drupalGetTestFiles($type, $size = NULL) {
   }
 
   /**
+   * Gets an image file that can be used in tests.
+   *
+   * @param string|null $file_name
+   *   (Optional) The file name of the file to be returned. If not specified,
+   *   will return a valid image file from the image files.
+   *
+   * @return stdClass|null
+   *   A file object, or NULL if requested file is not existing.
+   */
+  protected function drupalGetTestImageFile($file_name = NULL) {
+    $image_files = array();
+    foreach ($this->drupalGetTestFiles('image') as $file) {
+      $image_files[$file->filename] = $file;
+    }
+    $file_name = $file_name ?: 'image-test.png';
+    return isset($image_files[$file_name]) ? $image_files[$file_name] : NULL;
+  }
+
+  /**
    * Compare two files based on size and file name.
    */
   protected function drupalCompareFiles($file1, $file2) {
diff --git a/core/modules/system/src/Tests/Form/StateValuesCleanAdvancedTest.php b/core/modules/system/src/Tests/Form/StateValuesCleanAdvancedTest.php
index 1c9ed4c..6806eb4 100644
--- a/core/modules/system/src/Tests/Form/StateValuesCleanAdvancedTest.php
+++ b/core/modules/system/src/Tests/Form/StateValuesCleanAdvancedTest.php
@@ -36,8 +36,7 @@ class StateValuesCleanAdvancedTest extends WebTestBase {
   function testFormStateValuesCleanAdvanced() {
 
     // Get an image for uploading.
-    $image_files = $this->drupalGetTestFiles('image');
-    $this->image = current($image_files);
+    $this->image = $this->drupalGetTestImageFile();
 
     // Check if the physical file is there.
     $this->assertTrue(is_file($this->image->uri), "The image file we're going to upload exists.");
diff --git a/core/modules/system/src/Tests/Image/ToolkitTestBase.php b/core/modules/system/src/Tests/Image/ToolkitTestBase.php
index 72703ad..ccb140a 100644
--- a/core/modules/system/src/Tests/Image/ToolkitTestBase.php
+++ b/core/modules/system/src/Tests/Image/ToolkitTestBase.php
@@ -50,7 +50,7 @@ protected function setUp() {
     $this->imageFactory = $this->container->get('image.factory');
 
     // Pick a file for testing.
-    $file = current($this->drupalGetTestFiles('image'));
+    $file = $this->drupalGetTestImageFile();
     $this->file = $file->uri;
 
     // Setup a dummy image to work with.
diff --git a/core/modules/system/src/Tests/System/ThemeTest.php b/core/modules/system/src/Tests/System/ThemeTest.php
index d5bb6f6..8a237dd 100644
--- a/core/modules/system/src/Tests/System/ThemeTest.php
+++ b/core/modules/system/src/Tests/System/ThemeTest.php
@@ -53,7 +53,7 @@ function testThemeSettings() {
     $this->assertResponse(404, 'The theme settings form URL for a non-existent theme could not be found.');
 
     // Specify a filesystem path to be used for the logo.
-    $file = current($this->drupalGetTestFiles('image'));
+    $file = $this->drupalGetTestImageFile();
     $file_relative = strtr($file->uri, array('public:/' => PublicStream::basePath()));
     $default_theme_path = 'core/themes/classy';
 
diff --git a/core/modules/taxonomy/src/Tests/TaxonomyImageTest.php b/core/modules/taxonomy/src/Tests/TaxonomyImageTest.php
index fba0f15..7ff4be6 100644
--- a/core/modules/taxonomy/src/Tests/TaxonomyImageTest.php
+++ b/core/modules/taxonomy/src/Tests/TaxonomyImageTest.php
@@ -73,8 +73,7 @@ public function testTaxonomyImageAccess() {
     $this->drupalLogin($user);
 
     // Create a term and upload the image.
-    $files = $this->drupalGetTestFiles('image');
-    $image = array_pop($files);
+    $image = $this->drupalGetTestImageFile();
     $edit['name[0][value]'] = $this->randomMachineName();
     $edit['files[field_test_0]'] = drupal_realpath($image->uri);
     $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id()  . '/add', $edit, t('Save'));
diff --git a/core/modules/update/src/Tests/UpdateUploadTest.php b/core/modules/update/src/Tests/UpdateUploadTest.php
index 8bb7535..6a29247 100644
--- a/core/modules/update/src/Tests/UpdateUploadTest.php
+++ b/core/modules/update/src/Tests/UpdateUploadTest.php
@@ -34,12 +34,8 @@ protected function setUp() {
    * Tests upload and extraction of a module.
    */
   public function testUploadModule() {
-    // Images are not valid archives, so get one and try to install it. We
-    // need an extra variable to store the result of drupalGetTestFiles()
-    // since reset() takes an argument by reference and passing in a constant
-    // emits a notice in strict mode.
-    $imageTestFiles = $this->drupalGetTestFiles('image');
-    $invalidArchiveFile = reset($imageTestFiles);
+    // Images are not valid archives, so get one and try to install it.
+    $invalidArchiveFile = $this->drupalGetTestImageFile();
     $edit = array(
       'files[project_upload]' => $invalidArchiveFile->uri,
     );
diff --git a/core/modules/user/src/Tests/UserPasswordResetTest.php b/core/modules/user/src/Tests/UserPasswordResetTest.php
index 3db2c00..08c63d5 100644
--- a/core/modules/user/src/Tests/UserPasswordResetTest.php
+++ b/core/modules/user/src/Tests/UserPasswordResetTest.php
@@ -111,7 +111,7 @@ function testUserPasswordReset() {
 
     // Make sure the ajax request from uploading a user picture does not
     // invalidate the reset token.
-    $image = current($this->drupalGetTestFiles('image'));
+    $image = $this->drupalGetTestImageFile();
     $edit = array(
       'files[user_picture_0]' => drupal_realpath($image->uri),
     );
diff --git a/core/modules/user/src/Tests/UserPictureTest.php b/core/modules/user/src/Tests/UserPictureTest.php
index 4f70d44..6603617 100644
--- a/core/modules/user/src/Tests/UserPictureTest.php
+++ b/core/modules/user/src/Tests/UserPictureTest.php
@@ -51,7 +51,7 @@ function testCreateDeletePicture() {
     $this->drupalLogin($this->webUser);
 
     // Save a new picture.
-    $image = current($this->drupalGetTestFiles('image'));
+    $image = $this->drupalGetTestImageFile();
     $file = $this->saveUserPicture($image);
 
     // Verify that the image is displayed on the user account page.
@@ -88,7 +88,7 @@ function testPictureOnNodeComment() {
     $this->drupalLogin($this->webUser);
 
     // Save a new picture.
-    $image = current($this->drupalGetTestFiles('image'));
+    $image = $this->drupalGetTestImageFile();
     $file = $this->saveUserPicture($image);
 
     $node = $this->drupalCreateNode(array('type' => 'article'));
