function NameMungingTest::testMunging

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/File/NameMungingTest.php \Drupal\KernelTests\Core\File\NameMungingTest::testMunging()

Create a file and munge/unmunge the name.

File

core/tests/Drupal/KernelTests/Core/File/NameMungingTest.php, line 66

Class

NameMungingTest
Tests filename munging and unmunging.

Namespace

Drupal\KernelTests\Core\File

Code

public function testMunging() {
    $this->expectDeprecation('file_munge_filename() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Dispatch a \\Drupal\\Core\\File\\Event\\FileUploadSanitizeNameEvent event instead. See https://www.drupal.org/node/3032541');
    // Disable insecure uploads.
    $this->config('system.file')
        ->set('allow_insecure_uploads', 0)
        ->save();
    $munged_name = file_munge_filename($this->name, '', TRUE);
    $messages = \Drupal::messenger()->all();
    \Drupal::messenger()->deleteAll();
    $this->assertContainsEquals(strtr('For security reasons, your upload has been renamed to <em class="placeholder">%filename</em>.', [
        '%filename' => $munged_name,
    ]), $messages['status'], 'Alert properly set when a file is renamed.');
    $this->assertNotEquals($this->name, $munged_name, new FormattableMarkup('The new filename (%munged) has been modified from the original (%original)', [
        '%munged' => $munged_name,
        '%original' => $this->name,
    ]));
}

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