View source
<?php
namespace Drupal\Tests\file\Kernel;
class ValidateTest extends FileManagedUnitTestBase {
public function testCallerValidation() {
$file = $this
->createFile();
$this
->assertEquals([], file_validate($file, []), 'Validating an empty array works successfully.');
$this
->assertFileHooksCalled([
'validate',
]);
file_test_reset();
file_test_set_return('validate', []);
$passing = [
'file_test_validator' => [
[],
],
];
$this
->assertEquals([], file_validate($file, $passing), 'Validating passes.');
$this
->assertFileHooksCalled([
'validate',
]);
file_test_reset();
file_test_set_return('validate', [
'Epic fail',
]);
$failing = [
'file_test_validator' => [
[
'Failed',
'Badly',
],
],
];
$this
->assertEquals([
'Failed',
'Badly',
'Epic fail',
], file_validate($file, $failing), 'Validating returns errors.');
$this
->assertFileHooksCalled([
'validate',
]);
}
public function testInsecureExtensions() {
$file = $this
->createFile('test.php', 'Invalid PHP');
$errors = file_validate($file, []);
$this
->assertEquals('For security reasons, your upload has been rejected.', $errors[0]);
$this
->assertFileHooksCalled([
'validate',
]);
file_test_reset();
$this
->config('system.file')
->set('allow_insecure_uploads', TRUE)
->save();
$errors = file_validate($file, []);
$this
->assertEmpty($errors);
$this
->assertFileHooksCalled([
'validate',
]);
}
}