function SaveUploadFormTest::testErrorMessagesAreNotChanged

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testErrorMessagesAreNotChanged()
  2. 8.9.x core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testErrorMessagesAreNotChanged()
  3. 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 493

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testErrorMessagesAreNotChanged() : void {
    $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->drupalGet('file-test/save_upload_from_form_test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("You WIN!");
    // Ensure the expected error message is present and the counts before and
    // after calling _file_save_upload_from_form() are correct.
    $this->assertSession()
        ->pageTextContains($error);
    $this->assertSession()
        ->pageTextContains('Number of error messages before _file_save_upload_from_form(): 1');
    $this->assertSession()
        ->pageTextContains('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->drupalGet('file-test/save_upload_from_form_test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("Epic upload FAIL!");
    // Ensure the expected error message is present and the counts before and
    // after calling _file_save_upload_from_form() are correct.
    $this->assertSession()
        ->pageTextContains($error);
    $this->assertSession()
        ->pageTextContains('Number of error messages before _file_save_upload_from_form(): 1');
    $this->assertSession()
        ->pageTextContains('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->drupalGet('file-test/save_upload_from_form_test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("You WIN!");
    // Ensure the error message is not present and the counts before and after
    // calling _file_save_upload_from_form() are correct.
    $this->assertSession()
        ->pageTextNotContains($error);
    $this->assertSession()
        ->pageTextContains('Number of error messages before _file_save_upload_from_form(): 0');
    $this->assertSession()
        ->pageTextContains('Number of error messages after _file_save_upload_from_form(): 0');
}

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