3 namespace Drupal\Tests\file\FunctionalJavascript;
5 use Drupal\Component\Utility\Bytes;
6 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
8 use Drupal\Tests\file\Functional\FileFieldCreationTrait;
11 * Tests uploading a file that exceeds the maximum file size.
15 class MaximumFileSizeExceededUploadTest extends JavascriptTestBase {
17 use FileFieldCreationTrait;
18 use TestFileCreationTrait;
23 public static $modules = ['node', 'file'];
26 * The file system service.
28 * @var \Drupal\Core\File\FileSystemInterface
30 protected $fileSystem;
35 * @var \Drupal\user\UserInterface
40 * The original value of the 'display_errors' PHP configuration option.
42 * @todo Remove this when issue #2905597 is fixed.
43 * @see https://www.drupal.org/node/2905597
47 protected $originalDisplayErrorsValue;
52 protected function setUp() {
55 $this->fileSystem = $this->container->get('file_system');
57 // Create the Article node type.
58 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
60 // Attach a file field to the node type.
61 $field_settings = ['file_extensions' => 'txt'];
62 $this->createFileField('field_file', 'node', 'article', [], $field_settings);
64 // Log in as a content author who can create Articles.
65 $this->user = $this->drupalCreateUser([
67 'create article content',
69 $this->drupalLogin($this->user);
71 // Disable the displaying of errors, so that the AJAX responses are not
72 // contaminated with error messages about exceeding the maximum POST size.
73 // @todo Remove this when issue #2905597 is fixed.
74 // @see https://www.drupal.org/node/2905597
75 $this->originalDisplayErrorsValue = ini_set('display_errors', '0');
81 protected function tearDown() {
82 // Restore the displaying of errors to the original value.
83 // @todo Remove this when issue #2905597 is fixed.
84 // @see https://www.drupal.org/node/2905597
85 ini_set('display_errors', $this->originalDisplayErrorsValue);
91 * Tests that uploading files exceeding maximum size are handled correctly.
93 public function testUploadFileExceedingMaximumFileSize() {
94 $session = $this->getSession();
96 // Create a test file that exceeds the maximum POST size with 1 kilobyte.
97 $post_max_size = Bytes::toInt(ini_get('post_max_size'));
98 $invalid_file = $this->generateFile('exceeding_post_max_size', ceil(($post_max_size + 1024) / 1024), 1024);
100 // Go to the node creation form and try to upload the test file.
101 $this->drupalGet('node/add/article');
102 $page = $session->getPage();
103 $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($invalid_file));
105 // An error message should appear informing the user that the file exceeded
106 // the maximum file size.
107 $this->assertSession()->waitForElement('css', '.messages--error');
108 // The error message includes the actual file size limit which depends on
109 // the current environment, so we check for a part of the message.
110 $this->assertSession()->pageTextContains('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size');
112 // Now upload a valid file and check that the error message disappears.
113 $valid_file = $this->generateFile('not_exceeding_post_max_size', 8, 8);
114 $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($valid_file));
115 $this->assertSession()->waitForElement('named', ['id_or_name', 'field_file_0_remove_button']);
116 $this->assertSession()->elementNotExists('css', '.messages--error');