[FEATURE] EXT:form - integrate new form framework
[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\Core\Tests\UnitTestCase;
20 use TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException;
21 use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
22
23 /**
24 * Test case
25 */
26 class MimeTypeValidatorTest extends UnitTestCase
27 {
28
29 /**
30 * @test
31 */
32 public function MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsString()
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
46 /**
47 * @test
48 */
49 public function MimeTypeValidatorThrowsExceptionIfAllowedMimeTypesOptionIsEmptyArray()
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
63 /**
64 * @test
65 */
66 public function MimeTypeValidatorReturnsTrueIfFileResourceIsNotAllowedMimeType()
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(['identifier' => '/foo', 'mime_type' => 'image/png'], $mockedStorage);
79 $this->assertTrue($validator->validate($file)->hasErrors());
80 }
81
82 /**
83 * @test
84 */
85 public function MimeTypeValidatorReturnsFalseIfInputIsEmptyString()
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
96 /**
97 * @test
98 */
99 public function MimeTypeValidatorReturnsTrueIfInputIsNoFileResource()
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 }