Tests the file_validate() function.
@group file @group legacy
Hierarchy
- class \Drupal\Tests\file\Kernel\FileManagedUnitTestBase extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\file\Kernel\LegacyValidateTest
Expanded class hierarchy of LegacyValidateTest
File
- core/
modules/ file/ tests/ src/ Kernel/ LegacyValidateTest.php, line 13
Namespace
Drupal\Tests\file\KernelView source
class LegacyValidateTest extends FileManagedUnitTestBase {
/**
* Tests that the validators passed into are checked.
*/
public function testCallerValidation() {
$file = $this
->createFile();
// Empty validators.
$this
->expectDeprecation('file_validate() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700');
$this
->assertEquals([], file_validate($file, []), 'Validating an empty array works successfully.');
$this
->assertFileHooksCalled([]);
// Use the file_test.module's test validator to ensure that passing tests
// return correctly.
file_test_reset();
file_test_set_return('validate', []);
$passing = [
'file_test_validator' => [
[],
],
];
$this
->assertEquals([], file_validate($file, $passing), 'Validating passes.');
$this
->assertFileHooksCalled([]);
// Now test for failures in validators passed in and by hook_validate.
file_test_reset();
$failing = [
'file_test_validator' => [
[
'Failed',
'Badly',
],
],
];
$this
->assertEquals([
'Failed',
'Badly',
], file_validate($file, $failing), 'Validating returns errors.');
$this
->assertFileHooksCalled([]);
}
/**
* Tests hard-coded security check in file_validate().
*/
public function testInsecureExtensions() {
$file = $this
->createFile('test.php', 'Invalid PHP');
// Test that file_validate() will check for insecure extensions by default.
$this
->expectDeprecation('file_validate() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700');
$errors = file_validate($file, []);
$this
->assertEquals('For security reasons, your upload has been rejected.', $errors[0]);
$this
->assertFileHooksCalled([]);
file_test_reset();
// Test that the 'allow_insecure_uploads' is respected.
$this
->config('system.file')
->set('allow_insecure_uploads', TRUE)
->save();
$errors = file_validate($file, []);
$this
->assertEmpty($errors);
$this
->assertFileHooksCalled([]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileManagedUnitTestBase:: |
protected static | property | Modules to enable. | |
FileManagedUnitTestBase:: |
public | function | Asserts that two files are not the same by comparing the fid and filepath. | |
FileManagedUnitTestBase:: |
public | function | Assert that a hook_file_* hook was called a certain number of times. | |
FileManagedUnitTestBase:: |
public | function | Asserts that the specified file hooks were called only once. | |
FileManagedUnitTestBase:: |
public | function | Asserts that two files have the same values (except timestamp). | |
FileManagedUnitTestBase:: |
public | function | Asserts that two files are the same by comparing the fid and filepath. | |
FileManagedUnitTestBase:: |
public | function | Creates and saves a file, asserting that it was saved. | |
FileManagedUnitTestBase:: |
public | function | Creates a file and returns its URI. | |
FileManagedUnitTestBase:: |
protected | function | 5 | |
LegacyValidateTest:: |
public | function | Tests that the validators passed into are checked. | |
LegacyValidateTest:: |
public | function | Tests hard-coded security check in file_validate(). |