class ContentDispositionFilenameParserTest

Same name and namespace in other branches
  1. 10 core/modules/file/tests/src/Unit/Upload/ContentDispositionFilenameParserTest.php \Drupal\Tests\file\Unit\Upload\ContentDispositionFilenameParserTest

Tests the ContentDispositionFilenameParser class.

@group file @coversDefaultClass \Drupal\file\Upload\ContentDispositionFilenameParser

Hierarchy

Expanded class hierarchy of ContentDispositionFilenameParserTest

File

core/modules/file/tests/src/Unit/Upload/ContentDispositionFilenameParserTest.php, line 18

Namespace

Drupal\Tests\file\Unit\Upload
View source
class ContentDispositionFilenameParserTest extends UnitTestCase {
    
    /**
     * Tests the parseFilename() method.
     *
     * @covers ::parseFilename
     */
    public function testParseFilenameSuccess() : void {
        $request = $this->createRequest('filename="test.txt"');
        $filename = ContentDispositionFilenameParser::parseFilename($request);
        $this->assertEquals('test.txt', $filename);
    }
    
    /**
     * @covers ::parseFilename
     * @dataProvider invalidHeaderProvider
     */
    public function testParseFilenameInvalid(string|bool $contentDisposition) : void {
        $this->expectException(BadRequestHttpException::class);
        $this->expectExceptionMessage('No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.');
        $request = $this->createRequest($contentDisposition);
        ContentDispositionFilenameParser::parseFilename($request);
    }
    
    /**
     * @covers ::parseFilename
     */
    public function testParseFilenameMissing() : void {
        $this->expectException(BadRequestHttpException::class);
        $this->expectExceptionMessage('"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided.');
        $request = new Request();
        ContentDispositionFilenameParser::parseFilename($request);
    }
    
    /**
     * @covers ::parseFilename
     */
    public function testParseFilenameExtended() : void {
        $this->expectException(BadRequestHttpException::class);
        $this->expectExceptionMessage('The extended "filename*" format is currently not supported in the "Content-Disposition" header.');
        $request = $this->createRequest('filename*="UTF-8 \' \' example.txt"');
        $filename = ContentDispositionFilenameParser::parseFilename($request);
    }
    
    /**
     * A data provider for invalid headers.
     */
    public static function invalidHeaderProvider() : array {
        return [
            'multiple' => [
                'file; filename=""',
            ],
            'empty' => [
                'filename=""',
            ],
            'bad key' => [
                'not_a_filename="example.txt"',
            ],
        ];
    }
    
    /**
     * Creates a request with the given content-disposition header.
     */
    protected function createRequest(string $contentDisposition) : Request {
        $request = new Request();
        $request->headers
            ->set('Content-Disposition', $contentDisposition);
        return $request;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ContentDispositionFilenameParserTest::createRequest protected function Creates a request with the given content-disposition header.
ContentDispositionFilenameParserTest::invalidHeaderProvider public static function A data provider for invalid headers.
ContentDispositionFilenameParserTest::testParseFilenameExtended public function @covers ::parseFilename
ContentDispositionFilenameParserTest::testParseFilenameInvalid public function @covers ::parseFilename
@dataProvider invalidHeaderProvider
ContentDispositionFilenameParserTest::testParseFilenameMissing public function @covers ::parseFilename
ContentDispositionFilenameParserTest::testParseFilenameSuccess public function Tests the parseFilename() method.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 354
UnitTestCase::setUpBeforeClass public static function

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