‪TYPO3CMS  10.4
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 {
34  protected ‪$coreExtensionsToLoad = [
35  'form',
36  ];
37 
41  private $files = [
42  'file.exe' => "MZ\x90\x00\x03\x00",
43  'file.zip' => "PK\x03\x04",
44  'file.jpg' => "\xFF\xD8\xFF\xDB",
45  'file.gif' => 'GIF87a',
46  'file.pdf' => '%PDF-',
47  ];
48 
52  private $tmp;
53 
54  protected function setUp(): void
55  {
56  parent::setUp();
57  ‪$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
58  $this->‪tmp = vfsStream::setup('tmp', null, $this->files);
59  }
60 
61  protected function ‪tearDown(): void
62  {
63  unset(‪$GLOBALS['LANG'], $this->‪tmp);
64  parent::tearDown();
65  }
66 
67  public function ‪dataProvider(): array
68  {
69  // error-codes
70  // + 1471708998: mime-type not allowed
71  // + 1613126216: mime-type to file-extension mismatch
72  return [
73  'submitted gif as upload.gif' => [
74  [
75  'tmp_name' => 'vfs://tmp/file.gif',
76  'name' => 'upload.gif',
77  'type' => 'does/not-matter',
78  ],
79  ['image/gif'],
80  ],
81  'submitted jpg as upload.jpg' => [
82  [
83  'tmp_name' => 'vfs://tmp/file.jpg',
84  'name' => 'upload.jpg',
85  'type' => 'does/not-matter',
86  ],
87  ['image/jpeg'],
88  ],
89  'submitted pdf as upload.pdf' => [
90  [
91  'tmp_name' => 'vfs://tmp/file.pdf',
92  'name' => 'upload.pdf',
93  'type' => 'does/not-matter',
94  ],
95  ['application/pdf'],
96  ],
97  'submitted exe as upload.exe' => [
98  [
99  'tmp_name' => 'vfs://tmp/file.exe',
100  'name' => 'upload.exe',
101  'type' => 'does/not-matter',
102  ], // upload data (as in $_FILES)
103  ['image/gif'], // allowed mime-types
104  [1471708998], // expected error-codes
105  ],
106  'submitted gif as upload.exe' => [
107  [
108  'tmp_name' => 'vfs://tmp/file.gif',
109  'name' => 'upload.exe',
110  'type' => 'does/not-matter',
111  ], // upload data (as in $_FILES)
112  ['image/gif'], // allowed mime-types
113  [1613126216], // expected error-codes
114  ],
115  ];
116  }
117 
126  public function ‪someTest(array $uploadData, array $allowedMimeTypes, array $expectedErrorCodes = []): void
127  {
128  $uploadData['error'] = \UPLOAD_ERR_OK;
129  $uploadData['size'] = filesize($uploadData['tmp_name']);
130 
131  ‪$validator = new MimeTypeValidator(['allowedMimeTypes' => $allowedMimeTypes]);
132 
133  $resource = new PseudoFile($uploadData);
134  $result = ‪$validator->validate($resource);
135  $errorCodes = array_map([$this, 'resolveErrorCode'], $result->getErrors());
136  self::assertSame($expectedErrorCodes, $errorCodes);
137  }
138 
139  private function ‪resolveErrorCode(‪Error $error)
140  {
141  return $error->‪getCode();
142  }
143 }
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation
Definition: MimeTypeValidatorTest.php:18
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\tmp
‪array< string, $files=array('file.exe'=> "MZ\x90\x00\x03\x00", 'file.zip'=> "PK\x03\x04", 'file.jpg'=> "\xFF\xD8\xFF\xDB", 'file.gif'=> 'GIF87a', 'file.pdf'=> '%PDF-',);private vfsStreamDirectory $tmp;protected function setUp():void { parent::setUp();$GLOBALS[ 'LANG']=GeneralUtility::makeInstance(LanguageService::class);$this-> tmp
Definition: MimeTypeValidatorTest.php:55
‪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:136
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest
Definition: MimeTypeValidatorTest.php:30
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\dataProvider
‪dataProvider()
Definition: MimeTypeValidatorTest.php:64
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\someTest
‪someTest(array $uploadData, array $allowedMimeTypes, array $expectedErrorCodes=[])
Definition: MimeTypeValidatorTest.php:123
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\tearDown
‪tearDown()
Definition: MimeTypeValidatorTest.php:58
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\PseudoFile
Definition: PseudoFile.php:30
‪TYPO3\CMS\Form\Tests\Functional\Mvc\Validation\MimeTypeValidatorTest\$coreExtensionsToLoad
‪string[] $coreExtensionsToLoad
Definition: MimeTypeValidatorTest.php:33