function FileExtensionSecureConstraintValidatorTest::testValidate

Same name in other branches
  1. 11.x core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileExtensionSecureConstraintValidatorTest.php \Drupal\Tests\file\Kernel\Plugin\Validation\Constraint\FileExtensionSecureConstraintValidatorTest::testValidate()

@covers ::validate

File

core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileExtensionSecureConstraintValidatorTest.php, line 20

Class

FileExtensionSecureConstraintValidatorTest
Tests the FileExtensionSecureConstraintValidator.

Namespace

Drupal\Tests\file\Kernel\Plugin\Validation\Constraint

Code

public function testValidate() : void {
    // Test success with .txt extension.
    $validators = [
        'FileExtensionSecure' => [],
    ];
    $violations = $this->validator
        ->validate($this->file, $validators);
    $this->assertCount(0, $violations);
    // Test failure with .php extension.
    $this->file
        ->setFilename('foo.php');
    $violations = $this->validator
        ->validate($this->file, $validators);
    $this->assertCount(1, $violations);
    $this->assertEquals('For security reasons, your upload has been rejected.', $violations->get(0)
        ->getMessage());
    // Test success with .php extension and allow_insecure_uploads.
    $this->config('system.file')
        ->set('allow_insecure_uploads', TRUE)
        ->save();
    $violations = $this->validator
        ->validate($this->file, $validators);
    $this->assertCount(0, $violations);
}

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