function FileFieldValidateTest::testFileMaxSize

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileMaxSize()
  2. 10 core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileMaxSize()
  3. 11.x core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileMaxSize()

Tests the max file size validator.

File

core/modules/file/tests/src/Functional/FileFieldValidateTest.php, line 78

Class

FileFieldValidateTest
Tests file field validation functions.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileMaxSize() {
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name, [], [
        'required' => '1',
    ]);
    // 128KB.
    $small_file = $this->getTestFile('text', 131072);
    // 1.2MB
    $large_file = $this->getTestFile('text', 1310720);
    // Test uploading both a large and small file with different increments.
    $sizes = [
        '1M' => 1048576,
        '1024K' => 1048576,
        '1048576' => 1048576,
    ];
    foreach ($sizes as $max_filesize => $file_limit) {
        // Set the max file upload size.
        $this->updateFileField($field_name, $type_name, [
            'max_filesize' => $max_filesize,
        ]);
        // Create a new node with the small file, which should pass.
        $nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
        $node_storage->resetCache([
            $nid,
        ]);
        $node = $node_storage->load($nid);
        $node_file = File::load($node->{$field_name}->target_id);
        $this->assertFileExists($node_file->getFileUri());
        $this->assertFileEntryExists($node_file, new FormattableMarkup('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', [
            '%filesize' => format_size($small_file->getSize()),
            '%maxsize' => $max_filesize,
        ]));
        // Check that uploading the large file fails (1M limit).
        $this->uploadNodeFile($large_file, $field_name, $type_name);
        $filesize = format_size($large_file->getSize());
        $maxsize = format_size($file_limit);
        $this->assertSession()
            ->pageTextContains("The file is {$filesize} exceeding the maximum file size of {$maxsize}.");
    }
    // Turn off the max filesize.
    $this->updateFileField($field_name, $type_name, [
        'max_filesize' => '',
    ]);
    // Upload the big file successfully.
    $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
    $node_storage->resetCache([
        $nid,
    ]);
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    $this->assertFileExists($node_file->getFileUri());
    $this->assertFileEntryExists($node_file, new FormattableMarkup('File entry exists after uploading a file (%filesize) with no max limit.', [
        '%filesize' => format_size($large_file->getSize()),
    ]));
}

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