‪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 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪MimeTypeValidatorTest extends UnitTestCase
30 {
35  {
36  $this->expectException(InvalidValidationOptionsException::class);
37  $this->expectExceptionCode(1471713296);
38 
39  $options = ['allowedMimeTypes' => ''];
40  ‪$validator = $this->getMockBuilder(MimeTypeValidator::class)
41  ->setMethods(['translateErrorMessage'])
42  ->setConstructorArgs(['options' => $options])
43  ->getMock();
44 
45  ‪$validator->validate(true);
46  }
47 
52  {
53  $this->expectException(InvalidValidationOptionsException::class);
54  $this->expectExceptionCode(1471713296);
55 
56  $options = ['allowedMimeTypes' => []];
57  ‪$validator = $this->getMockBuilder(MimeTypeValidator::class)
58  ->setMethods(['translateErrorMessage'])
59  ->setConstructorArgs(['options' => $options])
60  ->getMock();
61 
62  ‪$validator->validate(true);
63  }
64 
69  {
70  $options = ['allowedMimeTypes' => ['image/jpeg']];
71  ‪$validator = $this->getMockBuilder(MimeTypeValidator::class)
72  ->setMethods(['translateErrorMessage'])
73  ->setConstructorArgs(['options' => $options])
74  ->getMock();
75 
76  $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79 
80  $file = new ‪File(['name' => 'foo', 'identifier' => '/foo', 'mime_type' => 'image/png'], $mockedStorage);
81  self::assertTrue(‪$validator->validate($file)->hasErrors());
82  }
83 
88  {
89  $options = ['allowedMimeTypes' => ['fake']];
90  ‪$validator = $this->getMockBuilder(MimeTypeValidator::class)
91  ->setMethods(['translateErrorMessage'])
92  ->setConstructorArgs(['options' => $options])
93  ->getMock();
94 
95  self::assertFalse(‪$validator->validate('')->hasErrors());
96  }
97 
102  {
103  $options = ['allowedMimeTypes' => ['fake']];
104  ‪$validator = $this->getMockBuilder(MimeTypeValidator::class)
105  ->setMethods(['translateErrorMessage'])
106  ->setConstructorArgs(['options' => $options])
107  ->getMock();
108 
109  self::assertTrue(‪$validator->validate('string')->hasErrors());
110  }
111 
113  {
114  $allowedMimeTypes = ['application/pdf', 'application/vnd.oasis.opendocument.text'];
115  return [
116  // filename, file mime-type, allowed types, is valid (is allowed)
117  ['something.pdf', 'application/pdf', $allowedMimeTypes, true],
118  ['something.txt', 'application/pdf', $allowedMimeTypes, false],
119  ['something.pdf', 'application/pdf', [false], false],
120  ['something.pdf', 'false', $allowedMimeTypes, false],
121  ];
122  }
123 
132  public function ‪fileExtensionMatchesMimeTypes(string $fileName, string $fileMimeType, array $allowedMimeTypes, bool $isValid): void
133  {
134  $options = ['allowedMimeTypes' => $allowedMimeTypes];
135  ‪$validator = $this->getMockBuilder(MimeTypeValidator::class)
136  ->setMethods(['translateErrorMessage'])
137  ->setConstructorArgs(['options' => $options])
138  ->getMock();
139  $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
140  ->disableOriginalConstructor()
141  ->getMock();
142  $file = new ‪File([
143  'name' => $fileName,
144  'identifier' => '/folder/' . $fileName,
145  'mime_type' => $fileMimeType
146  ], $mockedStorage);
147  $result = ‪$validator->validate($file);
148  self::assertSame($isValid, !$result->hasErrors());
149  }
150 }
‪TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:23
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation
Definition: CountValidatorTest.php:16
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\fileExtensionMatchesMimeTypes
‪fileExtensionMatchesMimeTypes(string $fileName, string $fileMimeType, array $allowedMimeTypes, bool $isValid)
Definition: MimeTypeValidatorTest.php:132
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest
Definition: MimeTypeValidatorTest.php:30
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\MimeTypeValidatorReturnsFalseIfInputIsEmptyString
‪MimeTypeValidatorReturnsFalseIfInputIsEmptyString()
Definition: MimeTypeValidatorTest.php:87
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsString
‪MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsString()
Definition: MimeTypeValidatorTest.php:34
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\MimeTypeValidatorReturnsTrueIfFileResourceIsNotAllowedMimeType
‪MimeTypeValidatorReturnsTrueIfFileResourceIsNotAllowedMimeType()
Definition: MimeTypeValidatorTest.php:68
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsEmptyArray
‪MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsEmptyArray()
Definition: MimeTypeValidatorTest.php:51
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\fileExtensionMatchesMimeTypesDataProvider
‪fileExtensionMatchesMimeTypesDataProvider()
Definition: MimeTypeValidatorTest.php:112
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\MimeTypeValidatorTest\MimeTypeValidatorReturnsTrueIfInputIsNoFileResource
‪MimeTypeValidatorReturnsTrueIfInputIsNoFileResource()
Definition: MimeTypeValidatorTest.php:101