function FileFieldWidgetTest::testMaximumUploadFileSizeValidation

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testMaximumUploadFileSizeValidation()
  2. 11.x core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testMaximumUploadFileSizeValidation()

Tests maximum upload file size validation.

File

core/modules/file/tests/src/Functional/FileFieldWidgetTest.php, line 477

Class

FileFieldWidgetTest
Tests the file field widget with public and private files.

Namespace

Drupal\Tests\file\Functional

Code

public function testMaximumUploadFileSizeValidation() : void {
    // Grant the admin user required permissions.
    user_role_grant_permissions($this->adminUser->roles[0]->target_id, [
        'administer node fields',
    ]);
    $type_name = 'article';
    $field_name = $this->randomMachineName();
    $this->createFileField($field_name, 'node', $type_name);
    
    /** @var \Drupal\Field\FieldConfigInterface $field */
    $field = FieldConfig::loadByName('node', $type_name, $field_name);
    $field_id = $field->id();
    $this->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}");
    // Tests that form validation trims the user input.
    $edit = [
        'settings[max_filesize]' => ' 5.1 megabytes ',
    ];
    $this->submitForm($edit, 'Save settings');
    $this->assertSession()
        ->pageTextContains('Saved ' . $field_name . ' configuration.');
    // Reload the field config to check for the saved value.
    
    /** @var \Drupal\Field\FieldConfigInterface $field */
    $field = FieldConfig::loadByName('node', $type_name, $field_name);
    $settings = $field->getSettings();
    $this->assertEquals('5.1 megabytes', $settings['max_filesize'], 'The max filesize value had been trimmed on save.');
}

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