You are here

public function SaveUploadFormTest::testErrorMessagesAreNotChanged in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testErrorMessagesAreNotChanged()
  2. 10 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testErrorMessagesAreNotChanged()

Tests that form validation does not change error messages.

File

core/modules/file/tests/src/Functional/SaveUploadFormTest.php, line 447

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testErrorMessagesAreNotChanged() {
  $error = 'An error message set before _file_save_upload_from_form()';

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $edit = [
    'files[file_test_upload][]' => $file_system
      ->realpath($this->image
      ->getFileUri()),
    'error_message' => $error,
  ];
  $this
    ->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $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][]' => $file_system
      ->realpath($this->image
      ->getFileUri()),
    'error_message' => $error,
    'extensions' => 'foo',
  ];
  $this
    ->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $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][]' => $file_system
      ->realpath($this->image
      ->getFileUri()),
  ];
  $this
    ->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // 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');
}