function SaveUploadFormTest::testHandleFileMunge

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

Tests file munge handling.

File

core/modules/file/tests/src/Functional/SaveUploadFormTest.php, line 260

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testHandleFileMunge() {
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    // Ensure insecure uploads are disabled for this test.
    $this->config('system.file')
        ->set('allow_insecure_uploads', 0)
        ->save();
    $original_uri = $this->image
        ->getFileUri();
    $this->image = file_move($this->image, $original_uri . '.foo.' . $this->imageExtension);
    // Reset the hook counters to get rid of the 'move' we just called.
    file_test_reset();
    $extensions = $this->imageExtension;
    $edit = [
        'files[file_test_upload][]' => $file_system->realpath($this->image
            ->getFileUri()),
        'extensions' => $extensions,
    ];
    $munged_filename = $this->image
        ->getFilename();
    $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
    $munged_filename .= '_.' . $this->imageExtension;
    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
    $this->assertRaw(t('File name is @filename', [
        '@filename' => $munged_filename,
    ]), 'File was successfully munged.');
    $this->assertRaw(t('You WIN!'), 'Found the success message.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        'validate',
        'insert',
    ]);
    // Test with uppercase extensions.
    $this->image = file_move($this->image, $original_uri . '.foo2.' . $this->imageExtension);
    // Reset the hook counters.
    file_test_reset();
    $extensions = $this->imageExtension;
    $edit = [
        'files[file_test_upload][]' => $file_system->realpath($this->image
            ->getFileUri()),
        'extensions' => mb_strtoupper($extensions),
    ];
    $munged_filename = $this->image
        ->getFilename();
    $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
    $munged_filename .= '_.' . $this->imageExtension;
    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
    $this->assertRaw(t('File name is @filename', [
        '@filename' => $munged_filename,
    ]), 'File was successfully munged.');
    $this->assertRaw(t('You WIN!'), 'Found the success message.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        'validate',
        'insert',
    ]);
    // Ensure we don't munge files if we're allowing any extension.
    // Reset the hook counters.
    file_test_reset();
    // Ensure we don't munge files if we're allowing any extension.
    $edit = [
        'files[file_test_upload][]' => $file_system->realpath($this->image
            ->getFileUri()),
        'allow_all_extensions' => 'empty_array',
    ];
    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
    $this->assertRaw(t('File name is @filename', [
        '@filename' => $this->image
            ->getFilename(),
    ]), 'File was not munged when allowing any extension.');
    $this->assertRaw(t('You WIN!'), 'Found the success message.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        'validate',
        'insert',
    ]);
    // Ensure that setting $validators['file_validate_extensions'] = ['']
    // rejects all files.
    // Reset the hook counters.
    file_test_reset();
    $edit = [
        'files[file_test_upload][]' => $file_system->realpath($this->image
            ->getFileUri()),
        'allow_all_extensions' => 'empty_string',
    ];
    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
    $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        'validate',
    ]);
}

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