function SaveUploadTest::testSanitization

Same name in other branches
  1. 10 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testSanitization()

Tests filename sanitization.

File

core/modules/file/tests/src/Functional/SaveUploadTest.php, line 771

Class

SaveUploadTest
Tests the file_save_upload() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testSanitization() : void {
    $file = $this->generateFile('TÉXT-œ', 64, 5, 'text');
    $this->drupalGet('file-test/upload');
    // Upload a file with a name with uppercase and unicode characters.
    $edit = [
        'files[file_test_upload]' => \Drupal::service('file_system')->realpath($file),
        'extensions' => 'txt',
        'is_image_file' => FALSE,
    ];
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that the file name has not been sanitized.
    $this->assertSession()
        ->responseContains('File name is TÉXT-œ.txt.');
    // Enable sanitization via the UI.
    $admin = $this->createUser([
        'administer site configuration',
    ]);
    $this->drupalLogin($admin);
    // For now, just transliterate, with no other transformations.
    $options = [
        'filename_sanitization[transliterate]' => TRUE,
        'filename_sanitization[replace_whitespace]' => FALSE,
        'filename_sanitization[replace_non_alphanumeric]' => FALSE,
        'filename_sanitization[deduplicate_separators]' => FALSE,
        'filename_sanitization[lowercase]' => FALSE,
        'filename_sanitization[replacement_character]' => '-',
    ];
    $this->drupalGet('admin/config/media/file-system');
    $this->submitForm($options, 'Save configuration');
    $this->drupalLogin($this->account);
    // Upload a file with a name with uppercase and unicode characters.
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that the file name has been transliterated.
    $this->assertSession()
        ->responseContains('File name is TEXT-oe.txt.');
    // Make sure we got a message about the rename.
    $message = 'Your upload has been renamed to <em class="placeholder">TEXT-oe.txt</em>';
    $this->assertSession()
        ->responseContains($message);
    // Generate another file with a name with All The Things(tm) we care about.
    $file = $this->generateFile('S  Pácê--táb#	#--🙈', 64, 5, 'text');
    $edit = [
        'files[file_test_upload]' => \Drupal::service('file_system')->realpath($file),
        'extensions' => 'txt',
        'is_image_file' => FALSE,
    ];
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that the file name has only been transliterated.
    $this->assertSession()
        ->responseContains('File name is S  Pace--tab#	#---.txt.');
    // Leave transliteration on and enable whitespace replacement.
    $this->drupalLogin($admin);
    $options['filename_sanitization[replace_whitespace]'] = TRUE;
    $this->drupalGet('admin/config/media/file-system');
    $this->submitForm($options, 'Save configuration');
    $this->drupalLogin($this->account);
    // Try again with the monster filename.
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that the file name has been transliterated and whitespace replaced.
    $this->assertSession()
        ->responseContains('File name is S--Pace--tab#-#---.txt.');
    // Leave transliteration and whitespace replacement on, replace non-alpha.
    $this->drupalLogin($admin);
    $options['filename_sanitization[replace_non_alphanumeric]'] = TRUE;
    $options['filename_sanitization[replacement_character]'] = '_';
    $this->drupalGet('admin/config/media/file-system');
    $this->submitForm($options, 'Save configuration');
    $this->drupalLogin($this->account);
    // Try again with the monster filename.
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that the file name has been transliterated, whitespace replaced with
    // '_', and non-alphanumeric characters replaced with '_'.
    $this->assertSession()
        ->responseContains('File name is S__Pace--tab___--_.txt.');
    // Now turn on the setting to remove duplicate separators.
    $this->drupalLogin($admin);
    $options['filename_sanitization[deduplicate_separators]'] = TRUE;
    $options['filename_sanitization[replacement_character]'] = '-';
    $this->drupalGet('admin/config/media/file-system');
    $this->submitForm($options, 'Save configuration');
    $this->drupalLogin($this->account);
    // Try again with the monster filename.
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that the file name has been transliterated, whitespace replaced,
    // non-alphanumeric characters replaced, and duplicate separators removed.
    $this->assertSession()
        ->responseContains('File name is S-Pace-tab.txt.');
    // Finally, check the lowercase setting.
    $this->drupalLogin($admin);
    $options['filename_sanitization[lowercase]'] = TRUE;
    $this->drupalGet('admin/config/media/file-system');
    $this->submitForm($options, 'Save configuration');
    $this->drupalLogin($this->account);
    // Generate another file since we're going to start getting collisions with
    // previously uploaded and renamed copies.
    $file = $this->generateFile('S  Pácê--táb#	#--🙈-2', 64, 5, 'text');
    $edit = [
        'files[file_test_upload]' => \Drupal::service('file_system')->realpath($file),
        'extensions' => 'txt',
        'is_image_file' => FALSE,
    ];
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Make sure all the sanitization options work as intended.
    $this->assertSession()
        ->responseContains('File name is s-pace-tab-2.txt.');
    // Make sure we got a message about the rename.
    $message = 'Your upload has been renamed to <em class="placeholder">s-pace-tab-2.txt</em>';
    $this->assertSession()
        ->responseContains($message);
}

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