function MaximumFileSizeExceededUploadTest::testUploadFileExceedingMaximumFileSize

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php \Drupal\Tests\file\FunctionalJavascript\MaximumFileSizeExceededUploadTest::testUploadFileExceedingMaximumFileSize()
  2. 8.9.x core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php \Drupal\Tests\file\FunctionalJavascript\MaximumFileSizeExceededUploadTest::testUploadFileExceedingMaximumFileSize()
  3. 11.x core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php \Drupal\Tests\file\FunctionalJavascript\MaximumFileSizeExceededUploadTest::testUploadFileExceedingMaximumFileSize()

Tests that uploading files exceeding maximum size are handled correctly.

File

core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php, line 100

Class

MaximumFileSizeExceededUploadTest
Tests uploading a file that exceeds the maximum file size.

Namespace

Drupal\Tests\file\FunctionalJavascript

Code

public function testUploadFileExceedingMaximumFileSize() : void {
  $session = $this->getSession();
  // Create a test file that exceeds the maximum POST size with 1 kilobyte.
  $post_max_size = (int) Bytes::toNumber(ini_get('post_max_size'));
  $invalid_file = 'public://exceeding_post_max_size.bin';
  $file = fopen($invalid_file, 'wb');
  fseek($file, $post_max_size + 1024);
  fwrite($file, '0');
  fclose($file);
  // Go to the node creation form and try to upload the test file.
  $this->drupalGet('node/add/article');
  $page = $session->getPage();
  $page->attachFileToField("files[field_file_0]", $this->fileSystem
    ->realpath($invalid_file));
  // An error message should appear informing the user that the file exceeded
  // the maximum file size. The error message includes the actual file size
  // limit which depends on the current environment, so we check for a part
  // of the message.
  $this->assertSession()
    ->statusMessageContainsAfterWait('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size', 'error');
  // Now upload a valid file and check that the error message disappears.
  $valid_file = $this->generateFile('not_exceeding_post_max_size', 8, 8);
  $page->attachFileToField("files[field_file_0]", $this->fileSystem
    ->realpath($valid_file));
  $this->assertSession()
    ->waitForElement('named', [
    'id_or_name',
    'field_file_0_remove_button',
  ]);
  $this->assertSession()
    ->statusMessageNotExistsAfterWait('error');
}

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