public function FileFieldWidgetTest::testFileExtensionsSetting in Drupal 9
Same name and namespace in other branches
- 10 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testFileExtensionsSetting()
Tests configuring file field's allowed file extensions setting.
File
- core/
modules/ file/ tests/ src/ Functional/ FileFieldWidgetTest.php, line 504
Class
- FileFieldWidgetTest
- Tests the file field widget with public and private files.
Namespace
Drupal\Tests\file\FunctionalCode
public function testFileExtensionsSetting() {
// Grant the admin user required permissions.
user_role_grant_permissions($this->adminUser->roles[0]->target_id, [
'administer node fields',
]);
$type_name = 'article';
$field_name = strtolower($this
->randomMachineName());
$this
->createFileField($field_name, 'node', $type_name);
$field = FieldConfig::loadByName('node', $type_name, $field_name);
$field_id = $field
->id();
// By default allowing .php files without .txt is not permitted.
$this
->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}");
$edit = [
'settings[file_extensions]' => 'jpg php',
];
$this
->submitForm($edit, 'Save settings');
$this
->assertSession()
->pageTextContains('Add txt to the list of allowed extensions to securely upload files with a php extension. The txt extension will then be added automatically.');
// Test allowing .php and .txt.
$edit = [
'settings[file_extensions]' => 'jpg php txt',
];
$this
->submitForm($edit, 'Save settings');
$this
->assertSession()
->pageTextContains('Saved ' . $field_name . ' configuration.');
// If the system is configured to allow insecure uploads, .txt is not
// required when allowing .php.
$this
->config('system.file')
->set('allow_insecure_uploads', TRUE)
->save();
$this
->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}");
$edit = [
'settings[file_extensions]' => 'jpg php',
];
$this
->submitForm($edit, 'Save settings');
$this
->assertSession()
->pageTextContains('Saved ' . $field_name . ' configuration.');
// Check that a file extension with an underscore can be configured.
$edit = [
'settings[file_extensions]' => 'x_t x.t xt x_y_t',
];
$this
->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}");
$this
->submitForm($edit, 'Save settings');
$field = FieldConfig::loadByName('node', $type_name, $field_name);
$this
->assertEquals('x_t x.t xt x_y_t', $field
->getSetting('file_extensions'));
// Check that a file field with an invalid value in allowed extensions
// property throws an error message.
$invalid_extensions = [
'x_.t',
'x._t',
'xt_',
'x__t',
'_xt',
];
foreach ($invalid_extensions as $value) {
$edit = [
'settings[file_extensions]' => $value,
];
$this
->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}");
$this
->submitForm($edit, 'Save settings');
$this
->assertSession()
->pageTextContains("The list of allowed extensions is not valid. Allowed characters are a-z, 0-9, '.', and '_'. The first and last characters cannot be '.' or '_', and these two characters cannot appear next to each other. Separate extensions with a comma or space.");
}
}