function SaveUploadFormTest::testCombinedErrorMessages

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testCombinedErrorMessages()
  2. 10 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testCombinedErrorMessages()
  3. 11.x 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 500

Class

SaveUploadFormTest
Tests the <a href="/api/drupal/core%21modules%21file%21file.module/function/_file_save_upload_from_form/8.9.x" title="Saves form file uploads." class="local">_file_save_upload_from_form</a>() 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 drupalPostForm() 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->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
    // Search for combined error message followed by a formatted list of messages.
    $this->assertRaw(t('One or more files could not be uploaded.') . '<div class="item-list">', 'Error message contains combined list of validation errors.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.