You are here

public function SaveUploadFormTest::testCombinedErrorMessages in Drupal 9

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

Tests that multiple validation errors are combined in one message.

File

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

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testCombinedErrorMessages() {
  $textfile = current($this
    ->drupalGetTestFiles('text'));
  $this
    ->assertFileExists($textfile->uri);

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');

  // Can't use submitForm() for set nonexistent fields.
  $this
    ->drupalGet('file-test/save_upload_from_form_test');
  $client = $this
    ->getSession()
    ->getDriver()
    ->getClient();
  $submit_xpath = $this
    ->assertSession()
    ->buttonExists('Submit')
    ->getXpath();
  $form = $client
    ->getCrawler()
    ->filterXPath($submit_xpath)
    ->form();
  $edit = [
    'is_image_file' => TRUE,
    'extensions' => 'jpeg',
  ];
  $edit += $form
    ->getPhpValues();
  $files['files']['file_test_upload'][0] = $file_system
    ->realpath($this->phpfile->uri);
  $files['files']['file_test_upload'][1] = $file_system
    ->realpath($textfile->uri);
  $client
    ->request($form
    ->getMethod(), $form
    ->getUri(), $edit, $files);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains("Epic upload FAIL!");

  // Search for combined error message followed by a formatted list of messages.
  $this
    ->assertSession()
    ->responseContains('One or more files could not be uploaded.<div class="item-list">');
}