public function SaveUploadTest::testHandleExtension in Drupal 8
Same name and namespace in other branches
- 9 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testHandleExtension()
- 10 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testHandleExtension()
Test extension handling.
File
- core/
modules/ file/ tests/ src/ Functional/ SaveUploadTest.php, line 174
Class
- SaveUploadTest
- Tests the file_save_upload() function.
Namespace
Drupal\Tests\file\FunctionalCode
public function testHandleExtension() {
// The file being tested is a .gif which is in the default safe list
// of extensions to allow when the extension validator isn't used. This is
// implicitly tested at the testNormal() test. Here we tell
// file_save_upload() to only allow ".foo".
$extensions = 'foo';
$edit = [
'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
'files[file_test_upload]' => \Drupal::service('file_system')
->realpath($this->image
->getFileUri()),
'extensions' => $extensions,
];
$this
->drupalPostForm('file-test/upload', $edit, t('Submit'));
$this
->assertSession()
->statusCodeEquals(200);
$message = t('Only files with the following extensions are allowed:') . ' <em class="placeholder">' . $extensions . '</em>';
$this
->assertRaw($message, 'Cannot upload a disallowed extension');
$this
->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
// Check that the correct hooks were called.
$this
->assertFileHooksCalled([
'validate',
]);
// Reset the hook counters.
file_test_reset();
$extensions = 'foo ' . $this->imageExtension;
// Now tell file_save_upload() to allow the extension of our test image.
$edit = [
'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
'files[file_test_upload]' => \Drupal::service('file_system')
->realpath($this->image
->getFileUri()),
'extensions' => $extensions,
];
$this
->drupalPostForm('file-test/upload', $edit, t('Submit'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoRaw(t('Only files with the following extensions are allowed:'), 'Can upload an allowed extension.');
$this
->assertRaw(t('You WIN!'), 'Found the success message.');
// Check that the correct hooks were called.
$this
->assertFileHooksCalled([
'validate',
'load',
'update',
]);
// Reset the hook counters.
file_test_reset();
// Now tell file_save_upload() to allow any extension.
$edit = [
'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
'files[file_test_upload]' => \Drupal::service('file_system')
->realpath($this->image
->getFileUri()),
'allow_all_extensions' => 'empty_array',
];
$this
->drupalPostForm('file-test/upload', $edit, t('Submit'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoRaw(t('Only files with the following extensions are allowed:'), 'Can upload any extension.');
$this
->assertRaw(t('You WIN!'), 'Found the success message.');
// Check that the correct hooks were called.
$this
->assertFileHooksCalled([
'validate',
'load',
'update',
]);
// Reset the hook counters.
file_test_reset();
// Now tell file_save_upload() to allow any extension and try and upload a
// malicious file.
$edit = [
'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
'files[file_test_upload]' => \Drupal::service('file_system')
->realpath($this->phpfile->uri),
'allow_all_extensions' => 'empty_array',
'is_image_file' => FALSE,
];
$this
->drupalPostForm('file-test/upload', $edit, t('Submit'));
$this
->assertSession()
->statusCodeEquals(200);
$message = t('For security reasons, your upload has been renamed to') . ' <em class="placeholder">' . $this->phpfile->filename . '_.txt' . '</em>';
$this
->assertRaw($message, 'Dangerous file was renamed.');
$this
->assertSession()
->pageTextContains('File name is php-2.php_.txt.');
$this
->assertRaw(t('File MIME type is text/plain.'), "Dangerous file's MIME type was changed.");
$this
->assertRaw(t('You WIN!'), 'Found the success message.');
// Check that the correct hooks were called.
$this
->assertFileHooksCalled([
'validate',
'insert',
]);
}