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