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 8bf72431a0..2dce2faa77 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php @@ -4,6 +4,7 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\State\StateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -19,14 +20,24 @@ class FileTestSaveUploadFromForm extends FormBase { */ protected $state; + /** + * The messenger. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + /** * Constructs a FileTestSaveUploadFromForm object. * * @param \Drupal\Core\State\StateInterface $state * The state key value store. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger. */ - public function __construct(StateInterface $state) { + public function __construct(StateInterface $state, MessengerInterface $messenger) { $this->state = $state; + $this->messenger = $messenger; } /** @@ -34,7 +45,8 @@ public function __construct(StateInterface $state) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('state') + $container->get('state'), + $container->get('messenger') ); } @@ -117,7 +129,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { // Preset custom error message if requested. if ($form_state->getValue('error_message')) { - drupal_set_message($form_state->getValue('error_message'), 'error'); + $this->messenger->addError($form_state->getValue('error_message')); } // Setup validators. @@ -143,21 +155,19 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $form['file_test_upload']['#upload_validators'] = $validators; $form['file_test_upload']['#upload_location'] = $destination; - $beforeMessages = drupal_get_messages('error', FALSE); - drupal_set_message($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => isset($beforeMessages['error']) ? count($beforeMessages['error']) : 0])); + $this->messenger->addStatus($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))])); $file = _file_save_upload_from_form($form['file_test_upload'], $form_state, 0, $form_state->getValue('file_test_replace')); - $afterMessages = drupal_get_messages('error', FALSE); - drupal_set_message($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => isset($afterMessages['error']) ? count($afterMessages['error']) : 0])); + $this->messenger->addStatus($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))])); if ($file) { $form_state->setValue('file_test_upload', $file); - 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!')); + $this->messenger->addStatus($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()])); + $this->messenger->addStatus($this->t('File name is @filename.', ['@filename' => $file->getFilename()])); + $this->messenger->addStatus($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()])); + $this->messenger->addStatus($this->t('You WIN!')); } elseif ($file === FALSE) { - drupal_set_message($this->t('Epic upload FAIL!'), 'error'); + $this->messenger->addError($this->t('Epic upload FAIL!')); } }