[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / Validation / MimeTypeValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Validation;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\ResourceStorage;
19 use TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException;
20 use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
21
22 /**
23 * Test case
24 */
25 class MimeTypeValidatorTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
26 {
27
28 /**
29 * @test
30 */
31 public function MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsString()
32 {
33 $this->expectException(InvalidValidationOptionsException::class);
34 $this->expectExceptionCode(1471713296);
35
36 $options = ['allowedMimeTypes' => ''];
37 $validator = $this->getMockBuilder(MimeTypeValidator::class)
38 ->setMethods(['translateErrorMessage'])
39 ->setConstructorArgs(['options' => $options])
40 ->getMock();
41
42 $validator->validate(true);
43 }
44
45 /**
46 * @test
47 */
48 public function MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsEmptyArray()
49 {
50 $this->expectException(InvalidValidationOptionsException::class);
51 $this->expectExceptionCode(1471713296);
52
53 $options = ['allowedMimeTypes' => []];
54 $validator = $this->getMockBuilder(MimeTypeValidator::class)
55 ->setMethods(['translateErrorMessage'])
56 ->setConstructorArgs(['options' => $options])
57 ->getMock();
58
59 $validator->validate(true);
60 }
61
62 /**
63 * @test
64 */
65 public function MimeTypeValidatorReturnsTrueIfFileResourceIsNotAllowedMimeType()
66 {
67 $options = ['allowedMimeTypes' => ['image/jpeg']];
68 $validator = $this->getMockBuilder(MimeTypeValidator::class)
69 ->setMethods(['translateErrorMessage'])
70 ->setConstructorArgs(['options' => $options])
71 ->getMock();
72
73 $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
74 ->disableOriginalConstructor()
75 ->getMock();
76
77 $file = new File(['identifier' => '/foo', 'mime_type' => 'image/png'], $mockedStorage);
78 $this->assertTrue($validator->validate($file)->hasErrors());
79 }
80
81 /**
82 * @test
83 */
84 public function MimeTypeValidatorReturnsFalseIfInputIsEmptyString()
85 {
86 $options = ['allowedMimeTypes' => ['fake']];
87 $validator = $this->getMockBuilder(MimeTypeValidator::class)
88 ->setMethods(['translateErrorMessage'])
89 ->setConstructorArgs(['options' => $options])
90 ->getMock();
91
92 $this->assertFalse($validator->validate('')->hasErrors());
93 }
94
95 /**
96 * @test
97 */
98 public function MimeTypeValidatorReturnsTrueIfInputIsNoFileResource()
99 {
100 $options = ['allowedMimeTypes' => ['fake']];
101 $validator = $this->getMockBuilder(MimeTypeValidator::class)
102 ->setMethods(['translateErrorMessage'])
103 ->setConstructorArgs(['options' => $options])
104 ->getMock();
105
106 $this->assertTrue($validator->validate('string')->hasErrors());
107 }
108 }