diff --git a/core/modules/file/file.module b/core/modules/file/file.module index d331147..1ce4298 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -701,29 +701,33 @@ function file_cron() { * there was an error. Function returns NULL if no file was uploaded. */ function file_save_upload_from_form($element, FormStateInterface $form_state, $delta = NULL, $replace = FILE_EXISTS_RENAME) { - $errors_before = drupal_get_messages('error', TRUE); + // Get all errors set before calling this method. This will also clear them + // from $_SESSION. + $errors_before = drupal_get_messages('error'); + $upload_location = isset($element['#upload_location']) ? $element['#upload_location'] : FALSE; $upload_name = implode('_', $element['#parents']); - $upload_validators = isset($element['#upload_validators']) ? $element['#upload_validators'] : array(); + $upload_validators = isset($element['#upload_validators']) ? $element['#upload_validators'] : []; $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $replace); - // Get new errors that are generated while trying to save the upload. - $errors_new = drupal_get_messages('error', TRUE); + // Get new errors that are generated while trying to save the upload. This + // will also clear them from $_SESSION. + $errors_new = drupal_get_messages('error'); if (!empty($errors_new['error'])) { $errors_new = $errors_new['error']; if (count($errors_new) > 1) { // Render multiple errors into a single message. - $render_array = array( - 'error' => array( + $render_array = [ + 'error' => [ '#markup' => t('One or more files could not be uploaded.'), - ), - 'item_list' => array( + ], + 'item_list' => [ '#theme' => 'item_list', '#items' => $errors_new, - ), - ); + ], + ]; $error_message = \Drupal::service('renderer')->renderPlain($render_array); } else { @@ -733,7 +737,8 @@ function file_save_upload_from_form($element, FormStateInterface $form_state, $d $form_state->setError($element, $error_message); } - // Put the errors back as before. + // Ensure that errors set prior to calling this method are still shown to the + // user. if (!empty($errors_before['error'])) { foreach ($errors_before['error'] as $error) { drupal_set_message($error, 'error'); diff --git a/core/modules/file/src/Tests/SaveUploadFormTest.php b/core/modules/file/src/Tests/SaveUploadFormTest.php index 238288d..c795120 100644 --- a/core/modules/file/src/Tests/SaveUploadFormTest.php +++ b/core/modules/file/src/Tests/SaveUploadFormTest.php @@ -44,9 +44,12 @@ class SaveUploadFormTest extends FileManagedTestBase { */ protected $imageExtension; + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); - $account = $this->drupalCreateUser(array('access site reports')); + $account = $this->drupalCreateUser(['access site reports']); $this->drupalLogin($account); $image_files = $this->drupalGetTestFiles('image'); @@ -61,22 +64,22 @@ protected function setUp() { $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); // Upload with replace to guarantee there's something there. - $edit = array( + $edit = [ 'file_test_replace' => FILE_EXISTS_REPLACE, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), - ); + ]; $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('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called then clean out the hook // counters. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); file_test_reset(); } /** - * Test the file_save_upload() function. + * Tests the file_save_upload_from_form() function. */ public function testNormal() { $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); @@ -91,14 +94,14 @@ public function testNormal() { // Upload a second file. $image2 = current($this->drupalGetTestFiles('image')); - $edit = array('files[file_test_upload][]' => drupal_realpath($image2->uri)); + $edit = ['files[file_test_upload][]' => drupal_realpath($image2->uri)]; $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('You WIN!')); $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); $file2 = File::load($max_fid_after); $this->assertTrue($file2, 'Loaded the file'); @@ -106,7 +109,7 @@ public function testNormal() { $this->assertEqual(substr($file2->getMimeType(), 0, 5), 'image', 'A MIME type was set.'); // Load both files using File::loadMultiple(). - $files = File::loadMultiple(array($file1->id(), $file2->id())); + $files = File::loadMultiple([$file1->id(), $file2->id()]); $this->assertTrue(isset($files[$file1->id()]), 'File was loaded successfully'); $this->assertTrue(isset($files[$file2->id()]), 'File was loaded successfully'); @@ -114,10 +117,10 @@ public function testNormal() { $image3 = current($this->drupalGetTestFiles('image')); $image3_realpath = drupal_realpath($image3->uri); $dir = $this->randomMachineName(); - $edit = array( + $edit = [ 'files[file_test_upload][]' => $image3_realpath, 'file_subdir' => $dir, - ); + ]; $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('You WIN!')); @@ -125,19 +128,19 @@ public function testNormal() { } /** - * Test extension handling. + * Tests extension handling. */ public function testHandleExtension() { // The file being tested is a .gif which is in the default safe list // of extensions to allow when the extension validator isn't used. This is // implicitly tested at the testNormal() test. Here we tell - // file_save_upload() to only allow ".foo". + // file_save_upload_from_form() to only allow ".foo". $extensions = 'foo'; - $edit = array( + $edit = [ 'file_test_replace' => FILE_EXISTS_REPLACE, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), 'extensions' => $extensions, - ); + ]; $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); @@ -146,18 +149,18 @@ public function testHandleExtension() { $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate')); + $this->assertFileHooksCalled(['validate']); // Reset the hook counters. file_test_reset(); $extensions = 'foo ' . $this->imageExtension; - // Now tell file_save_upload() to allow the extension of our test image. - $edit = array( + // Now tell file_save_upload_from_form() to allow the extension of our test image. + $edit = [ 'file_test_replace' => FILE_EXISTS_REPLACE, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), 'extensions' => $extensions, - ); + ]; $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); @@ -165,39 +168,39 @@ public function testHandleExtension() { $this->assertRaw(t('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'load', 'update')); + $this->assertFileHooksCalled(['validate', 'load', 'update']); // Reset the hook counters. file_test_reset(); - // Now tell file_save_upload() to allow any extension. - $edit = array( + // Now tell file_save_upload_from_form() to allow any extension. + $edit = [ 'file_test_replace' => FILE_EXISTS_REPLACE, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), 'allow_all_extensions' => 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->assertNoRaw(t('Only files with the following extensions are allowed:'), 'Can upload any extension.'); $this->assertRaw(t('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'load', 'update')); + $this->assertFileHooksCalled(['validate', 'load', 'update']); } /** - * Test dangerous file handling. + * Tests dangerous file handling. */ public function testHandleDangerousFile() { $config = $this->config('system.file'); // Allow the .php extension and make sure it gets renamed to .txt for // safety. Also check to make sure its MIME type was changed. - $edit = array( + $edit = [ 'file_test_replace' => FILE_EXISTS_REPLACE, 'files[file_test_upload][]' => drupal_realpath($this->phpfile->uri), 'is_image_file' => FALSE, 'extensions' => 'php', - ); + ]; $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); @@ -207,7 +210,7 @@ public function testHandleDangerousFile() { $this->assertRaw(t('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); // Ensure dangerous files are not renamed when insecure uploads is TRUE. // Turn on insecure uploads. @@ -218,18 +221,18 @@ public function testHandleDangerousFile() { $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.'); - $this->assertRaw(t('File name is @filename', array('@filename' => $this->phpfile->filename)), 'Dangerous file was not renamed when insecure uploads is TRUE.'); + $this->assertRaw(t('File name is @filename', ['@filename' => $this->phpfile->filename]), 'Dangerous file was not renamed when insecure uploads is TRUE.'); $this->assertRaw(t('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); // Turn off insecure uploads. $config->set('allow_insecure_uploads', 0)->save(); } /** - * Test file munge handling. + * Tests file munge handling. */ public function testHandleFileMunge() { // Ensure insecure uploads are disabled for this test. @@ -240,10 +243,10 @@ public function testHandleFileMunge() { file_test_reset(); $extensions = $this->imageExtension; - $edit = array( + $edit = [ 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), 'extensions' => $extensions, - ); + ]; $munged_filename = $this->image->getFilename(); $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.')); @@ -252,84 +255,84 @@ public function testHandleFileMunge() { $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('For security reasons, your upload has been renamed'), 'Found security message.'); - $this->assertRaw(t('File name is @filename', array('@filename' => $munged_filename)), 'File was successfully munged.'); + $this->assertRaw(t('File name is @filename', ['@filename' => $munged_filename]), 'File was successfully munged.'); $this->assertRaw(t('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); // Ensure we don't munge files if we're allowing any extension. // Reset the hook counters. file_test_reset(); - $edit = array( + $edit = [ 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), 'allow_all_extensions' => 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->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.'); - $this->assertRaw(t('File name is @filename', array('@filename' => $this->image->getFilename())), 'File was not munged when allowing any extension.'); + $this->assertRaw(t('File name is @filename', ['@filename' => $this->image->getFilename()]), 'File was not munged when allowing any extension.'); $this->assertRaw(t('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); } /** - * Test renaming when uploading over a file that already exists. + * Tests renaming when uploading over a file that already exists. */ public function testExistingRename() { - $edit = array( + $edit = [ 'file_test_replace' => FILE_EXISTS_RENAME, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()) - ); + ]; $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('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'insert')); + $this->assertFileHooksCalled(['validate', 'insert']); } /** - * Test replacement when uploading over a file that already exists. + * Tests replacement when uploading over a file that already exists. */ public function testExistingReplace() { - $edit = array( + $edit = [ 'file_test_replace' => FILE_EXISTS_REPLACE, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()) - ); + ]; $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('You WIN!'), 'Found the success message.'); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('validate', 'load', 'update')); + $this->assertFileHooksCalled(['validate', 'load', 'update']); } /** - * Test for failure when uploading over a file that already exists. + * Tests for failure when uploading over a file that already exists. */ public function testExistingError() { - $edit = array( + $edit = [ 'file_test_replace' => FILE_EXISTS_ERROR, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()) - ); + ]; $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.'); // Check that the no hooks were called while failing. - $this->assertFileHooksCalled(array()); + $this->assertFileHooksCalled([]); } /** - * Test for no failures when not uploading a file. + * Tests for no failures when not uploading a file. */ public function testNoUpload() { - $this->drupalPostForm('file-test/save_upload_from_form_test', array(), t('Submit')); + $this->drupalPostForm('file-test/save_upload_from_form_test', [], t('Submit')); $this->assertNoRaw(t('Epic upload FAIL!'), 'Failure message not found.'); } @@ -342,10 +345,10 @@ public function testDrupalMovingUploadedFileError() { drupal_mkdir('temporary://' . $test_directory, 0000); $this->assertTrue(is_dir('temporary://' . $test_directory)); - $edit = array( + $edit = [ 'file_subdir' => $test_directory, 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()) - ); + ]; \Drupal::state()->set('file_test.disable_error_collection', TRUE); $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit')); @@ -356,34 +359,65 @@ public function testDrupalMovingUploadedFileError() { // Uploading failed. Now check the log. $this->drupalGet('admin/reports/dblog'); $this->assertResponse(200); - $this->assertRaw(t('Upload error. Could not move uploaded file @file to destination @destination.', array( + $this->assertRaw(t('Upload error. Could not move uploaded file @file to destination @destination.', [ '@file' => $this->image->getFilename(), '@destination' => 'temporary://' . $test_directory . '/' . $this->image->getFilename() - )), 'Found upload error log entry.'); + ]), 'Found upload error log entry.'); } /** - * Test that form validation does not change error messages. + * Tests that form validation does not change error messages. */ public function testErrorMessagesAreNotChanged() { - $error = $this->randomMachineName(); + $error = 'An error message set before file_save_upload_from_form()'; + + $edit = [ + 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), + 'error_message' => $error, + ]; + $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('You WIN!'), 'Found the success message.'); + + // Ensure the expected error message is present and the counts before and + // after calling file_save_upload_from_form() are correct. + $this->assertText($error); + $this->assertRaw('Number of error messages before file_save_upload_from_form(): 1'); + $this->assertRaw('Number of error messages after file_save_upload_from_form(): 1'); + // Test that error messages are preserved when an error occurs. $edit = [ 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), 'error_message' => $error, + 'extensions' => 'foo' + ]; + $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.'); + + // Ensure the expected error message is present and the counts before and + // after calling file_save_upload_from_form() are correct. + $this->assertText($error); + $this->assertRaw('Number of error messages before file_save_upload_from_form(): 1'); + $this->assertRaw('Number of error messages after file_save_upload_from_form(): 1'); + + // Test a successful upload with no messages. + $edit = [ + 'files[file_test_upload][]' => drupal_realpath($this->image->getFileUri()), ]; $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('You WIN!'), 'Found the success message.'); - $this->assertRaw(t('Error message @key before validation: @message', array('@key' => 0, '@message' => $error)), 'Custom error message is set before validation.'); - $this->assertRaw(t('Number of error messages before validation: @count', array('@count' => 1)), 'No extra error messages are set before validation.'); - $this->assertRaw(t('Error message @key after validation: @message', array('@key' => 0, '@message' => $error)), 'Custom error message is set after validation.'); - $this->assertRaw(t('Number of error messages after validation: @count', array('@count' => 1)), 'No extra error messages are set after validation.'); + // Ensure the error message is not present and the counts before and after + // calling file_save_upload_from_form() are correct. + $this->assertNoText($error); + $this->assertRaw('Number of error messages before file_save_upload_from_form(): 0'); + $this->assertRaw('Number of error messages after file_save_upload_from_form(): 0'); } /** - * Test that multiple validation errors are combined in one message. + * Tests that multiple validation errors are combined in one message. */ public function testCombinedErrorMessages() { $textfile = current($this->drupalGetTestFiles('text')); @@ -408,7 +442,7 @@ public function testCombinedErrorMessages() { } /** - * Test highlighting of file upload field when it has an error. + * Tests highlighting of file upload field when it has an error. */ public function testUploadFieldIsHighlighted() { $this->assertEqual(0, count($this->cssSelect('input[name="files[file_test_upload][]"].error')), 'Successful file upload has no error.'); diff --git a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php index 8500e39..d06f53f 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php @@ -2,13 +2,41 @@ namespace Drupal\file_test\Form; -use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\State\StateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * File test form class. */ -class FileTestSaveUploadFromForm implements FormInterface { +class FileTestSaveUploadFromForm extends FormBase { + + /** + * Stores the state storage service. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** + * Constructs a FileTestSaveUploadFromForm object. + * + * @param \Drupal\Core\State\StateInterface $state + * The state key value store. + */ + public function __construct(StateInterface $state) { + $this->state = $state; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('state') + ); + } /** * {@inheritdoc} @@ -21,55 +49,55 @@ public function getFormId() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $form['file_test_upload'] = array( + $form['file_test_upload'] = [ '#type' => 'file', '#multiple' => TRUE, - '#title' => t('Upload a file'), - ); - $form['file_test_replace'] = array( + '#title' => $this->t('Upload a file'), + ]; + $form['file_test_replace'] = [ '#type' => 'select', - '#title' => t('Replace existing image'), - '#options' => array( - FILE_EXISTS_RENAME => t('Appends number until name is unique'), - FILE_EXISTS_REPLACE => t('Replace the existing file'), - FILE_EXISTS_ERROR => t('Fail with an error'), - ), + '#title' => $this->t('Replace existing image'), + '#options' => [ + FILE_EXISTS_RENAME => $this->t('Appends number until name is unique'), + FILE_EXISTS_REPLACE => $this->t('Replace the existing file'), + FILE_EXISTS_ERROR => $this->t('Fail with an error'), + ], '#default_value' => FILE_EXISTS_RENAME, - ); - $form['file_subdir'] = array( + ]; + $form['file_subdir'] = [ '#type' => 'textfield', - '#title' => t('Subdirectory for test file'), + '#title' => $this->t('Subdirectory for test file'), '#default_value' => '', - ); + ]; - $form['extensions'] = array( + $form['extensions'] = [ '#type' => 'textfield', - '#title' => t('Allowed extensions.'), + '#title' => $this->t('Allowed extensions.'), '#default_value' => '', - ); + ]; - $form['allow_all_extensions'] = array( + $form['allow_all_extensions'] = [ '#type' => 'checkbox', - '#title' => t('Allow all extensions?'), + '#title' => $this->t('Allow all extensions?'), '#default_value' => FALSE, - ); + ]; - $form['is_image_file'] = array( + $form['is_image_file'] = [ '#type' => 'checkbox', - '#title' => t('Is this an image file?'), + '#title' => $this->t('Is this an image file?'), '#default_value' => TRUE, - ); + ]; - $form['error_message'] = array( + $form['error_message'] = [ '#type' => 'textfield', - '#title' => t('Custom error message.'), + '#title' => $this->t('Custom error message.'), '#default_value' => '', - ); + ]; - $form['submit'] = array( + $form['submit'] = [ '#type' => 'submit', - '#value' => t('Submit'), - ); + '#value' => $this->t('Submit'), + ]; return $form; } @@ -93,51 +121,41 @@ public function validateForm(array &$form, FormStateInterface $form_state) { } // Setup validators. - $validators = array(); + $validators = []; if ($form_state->getValue('is_image_file')) { - $validators['file_validate_is_image'] = array(); + $validators['file_validate_is_image'] = []; } if ($form_state->getValue('allow_all_extensions')) { - $validators['file_validate_extensions'] = array(); + $validators['file_validate_extensions'] = []; } elseif (!$form_state->isValueEmpty('extensions')) { - $validators['file_validate_extensions'] = array($form_state->getValue('extensions')); + $validators['file_validate_extensions'] = [$form_state->getValue('extensions')]; } // The test for drupal_move_uploaded_file() triggering a warning is // unavoidable. We're interested in what happens afterwards in - // file_save_upload(). - if (\Drupal::state()->get('file_test.disable_error_collection')) { + // file_save_upload_from_form(). + if ($this->state->get('file_test.disable_error_collection')) { define('SIMPLETEST_COLLECT_ERRORS', FALSE); } $form['file_test_upload']['#upload_validators'] = $validators; $form['file_test_upload']['#upload_location'] = $destination; - $messages_before = drupal_get_messages('error', FALSE) + array('error' => array()); + drupal_set_message($this->t('Number of error messages before file_save_upload_from_form(): @count.', ['@count' => count(drupal_get_messages('error', FALSE))])); $file = file_save_upload_from_form($form['file_test_upload'], $form_state, 0, $form_state->getValue('file_test_replace')); - $messages_after = drupal_get_messages('error', FALSE) + array('error' => array()); + drupal_set_message($this->t('Number of error messages after file_save_upload_from_form(): @count.', ['@count' => count(drupal_get_messages('error', FALSE))])); if ($file) { $form_state->setValue('file_test_upload', $file); - drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->getFileUri()))); - drupal_set_message(t('File name is @filename.', array('@filename' => $file->getFilename()))); - drupal_set_message(t('File MIME type is @mimetype.', array('@mimetype' => $file->getMimeType()))); - drupal_set_message(t('You WIN!')); + drupal_set_message($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()])); + drupal_set_message($this->t('File name is @filename.', ['@filename' => $file->getFilename()])); + drupal_set_message($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()])); + drupal_set_message($this->t('You WIN!')); } elseif ($file === FALSE) { - drupal_set_message(t('Epic upload FAIL!'), 'error'); - } - - drupal_set_message(t('Number of error messages before validation: @count', array('@count' => count($messages_before['error'])))); - foreach ($messages_before['error'] as $key => $message) { - drupal_set_message(t('Error message @key before validation: @message', array('@key' => $key, '@message' => $message))); - } - - drupal_set_message(t('Number of error messages after validation: @count', array('@count' => count($messages_after['error'])))); - foreach ($messages_after['error'] as $key => $message) { - drupal_set_message(t('Error message @key after validation: @message', array('@key' => $key, '@message' => $message))); + drupal_set_message($this->t('Epic upload FAIL!'), 'error'); } }