‪TYPO3CMS  11.5
MimeTypeValidatorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use org\bovigo\vfs\vfsStream;
21 use org\bovigo\vfs\vfsStreamDirectory;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 class ‪MimeTypeValidatorTest extends FunctionalTestCase
30 {
31  protected ‪$coreExtensionsToLoad = ['form'];
32 
36  private $files = [
37  'file.exe' => "MZ\x90\x00\x03\x00",
38  'file.zip' => "PK\x03\x04",
39  'file.jpg' => "\xFF\xD8\xFF\xDB",
40  'file.gif' => 'GIF87a',
41  'file.pdf' => '%PDF-',
42  ];
43 
47  private $tmp;
48 
49  protected function setUp(): void
50  {
51  parent::setUp();
52  ‪$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
53  $this->‪tmp = vfsStream::setup('tmp', null, $this->files);
54  }
55 
56  protected function ‪tearDown(): void
57  {
58  unset(‪$GLOBALS['LANG'], $this->‪tmp);
59  parent::tearDown();
60  }
61 
62  public function ‪dataProvider(): array
63  {
64  // error-codes
65  // + 1471708998: mime-type not allowed
66  // + 1613126216: mime-type to file-extension mismatch
67  return [
68  'submitted gif as upload.gif' => [
69  [
70  'tmp_name' => 'vfs://tmp/file.gif',
71  'name' => 'upload.gif',
72  'type' => 'does/not-matter',
73  ],
74  ['image/gif'],
75  ],
76  'submitted jpg as upload.jpg' => [
77  [
78  'tmp_name' => 'vfs://tmp/file.jpg',
79  'name' => 'upload.jpg',
80  'type' => 'does/not-matter',
81  ],
82  ['image/jpeg'],
83  ],
84  'submitted pdf as upload.pdf' => [
85  [
86  'tmp_name' => 'vfs://tmp/file.pdf',
87  'name' => 'upload.pdf',
88  'type' => 'does/not-matter',
89  ],
90  ['application/pdf'],
91  ],
92  'submitted exe as upload.exe' => [
93  [
94  'tmp_name' => 'vfs://tmp/file.exe',
95  'name' => 'upload.exe',
96  'type' => 'does/not-matter',
97  ], // upload data (as in $_FILES)
98  ['image/gif'], // allowed mime-types
99  [1471708998], // expected error-codes
100  ],
101  'submitted gif as upload.exe' => [
102  [
103  'tmp_name' => 'vfs://tmp/file.gif',
104  'name' => 'upload.exe',
105  'type' => 'does/not-matter',
106  ], // upload data (as in $_FILES)
107  ['image/gif'], // allowed mime-types
108  [1613126216], // expected error-codes
109  ],
110  ];
111  }
112 
121  public function ‪someTest(array $uploadData, array $allowedMimeTypes, array $expectedErrorCodes = []): void
122  {
123  $uploadData['error'] = \UPLOAD_ERR_OK;
124  $uploadData['size'] = filesize($uploadData['tmp_name']);
125 
126  ‪$validator = new MimeTypeValidator(['allowedMimeTypes' => $allowedMimeTypes]);
127 
128  $resource = new PseudoFile($uploadData);
129  $result = ‪$validator->validate($resource);
130  $errorCodes = array_map([$this, 'resolveErrorCode'], $result->getErrors());
131  self::assertSame($expectedErrorCodes, $errorCodes);
132  }
133 
134  private function ‪resolveErrorCode(‪Error $error): int
135  {
136  return $error->‪getCode();
137  }
138 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation
Definition: MimeTypeValidatorTest.php:18
‪TYPO3\CMS\Extbase\Error\Message\getCode
‪int getCode()
Definition: Message.php:80
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\resolveErrorCode
‪resolveErrorCode(Error $error)
Definition: MimeTypeValidatorTest.php:132
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest
Definition: MimeTypeValidatorTest.php:30
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\$coreExtensionsToLoad
‪$coreExtensionsToLoad
Definition: MimeTypeValidatorTest.php:31
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\tmp
‪$this tmp
Definition: MimeTypeValidatorTest.php:51
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\dataProvider
‪dataProvider()
Definition: MimeTypeValidatorTest.php:60
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\someTest
‪someTest(array $uploadData, array $allowedMimeTypes, array $expectedErrorCodes=[])
Definition: MimeTypeValidatorTest.php:119
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\tearDown
‪tearDown()
Definition: MimeTypeValidatorTest.php:54
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\PseudoFile
Definition: PseudoFile.php:30