1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MimeValidator\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Upload; |
6
|
|
|
use SilverStripe\MimeValidator\MimeUploadValidator; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\Core\Config\Config; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MimeUploadValidatorTest |
12
|
|
|
*/ |
13
|
|
|
class MimeUploadValidatorTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
public function testInvalidFileExtensionValidatingMimeType() |
16
|
|
|
{ |
17
|
|
|
// setup plaintext file with invalid extension |
18
|
|
|
$tmpFileName = 'UploadTest-testUpload.jpg'; |
19
|
|
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
20
|
|
|
$tmpFileContent = ''; |
21
|
|
|
|
22
|
|
|
for ($i = 0; $i < 10000; $i++) { |
23
|
|
|
$tmpFileContent .= '0'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
file_put_contents($tmpFilePath, $tmpFileContent); |
27
|
|
|
|
28
|
|
|
// emulates the $_FILES array |
29
|
|
|
$tmpFile = [ |
30
|
|
|
'name' => $tmpFileName, |
31
|
|
|
'size' => filesize($tmpFilePath), |
32
|
|
|
'tmp_name' => $tmpFilePath, |
33
|
|
|
'extension' => 'jpg', |
34
|
|
|
'error' => UPLOAD_ERR_OK, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$upload = Upload::create(); |
38
|
|
|
$upload->setValidator(MimeUploadValidator::create()); |
39
|
|
|
$result = $upload->load($tmpFile); |
40
|
|
|
$errors = $upload->getErrors(); |
41
|
|
|
|
42
|
|
|
$this->assertFalse($result, 'Load failed because file extension does not match excepted MIME type'); |
43
|
|
|
$this->assertEquals('File is not a valid upload', $errors[0]); |
44
|
|
|
|
45
|
|
|
unlink($tmpFilePath); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGetExpectedMimeTypes() |
49
|
|
|
{ |
50
|
|
|
// Setup a file with a capitalised extension and try to match it against a lowercase file. |
51
|
|
|
$tmpFileName = 'text.TXT'; |
52
|
|
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
53
|
|
|
$tmpFileContent = ''; |
54
|
|
|
|
55
|
|
|
for ($i = 0; $i < 10000; $i++) { |
56
|
|
|
$tmpFileContent .= '0'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
file_put_contents($tmpFilePath, $tmpFileContent); |
60
|
|
|
|
61
|
|
|
$validator = MimeUploadValidator::create(); |
62
|
|
|
$tmpFile = [ |
63
|
|
|
'name' => $tmpFileName, |
64
|
|
|
'tmp_name' => $tmpFilePath, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$expected = $validator->getExpectedMimeTypes($tmpFile); |
68
|
|
|
$this->assertCount(1, $expected); |
69
|
|
|
$this->assertContains('text/plain', $expected); |
70
|
|
|
|
71
|
|
|
unlink($tmpFilePath); |
72
|
|
|
|
73
|
|
|
// Test a physical ico file with capitalised extension |
74
|
|
|
$tmpFile = [ |
75
|
|
|
'name' => 'favicon.ICO', |
76
|
|
|
'tmp_name' => 'assets/favicon.ICO', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
// Ensure that site configuration doesn't interfere with the test |
80
|
|
|
$icoMimes = [ |
81
|
|
|
'ico' => [ |
82
|
|
|
'image/vnd.microsoft.icon', |
83
|
|
|
'image/x-icon', |
84
|
|
|
'image/x-ico' |
85
|
|
|
] |
86
|
|
|
]; |
87
|
|
|
Config::modify()->set(MimeUploadValidator::class, 'MimeTypes', $icoMimes); |
88
|
|
|
|
89
|
|
|
$expected = $validator->getExpectedMimeTypes($tmpFile); |
90
|
|
|
$this->assertCount(3, $expected); |
91
|
|
|
$this->assertContains('image/x-icon', $expected); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testMimeComparison() |
95
|
|
|
{ |
96
|
|
|
$validator = MimeUploadValidator::create(); |
97
|
|
|
|
98
|
|
|
$this->assertTrue($validator->compareMime('application/xhtml+xml', 'application/xml')); |
99
|
|
|
$this->assertTrue($validator->compareMime('application/vnd.text', 'application/text')); |
100
|
|
|
$this->assertTrue($validator->compareMime('application/vnd.vnd.text', 'application/text')); |
101
|
|
|
$this->assertTrue($validator->compareMime('application/x-text', 'application/text')); |
102
|
|
|
$this->assertTrue($validator->compareMime('application/gzip', 'application/gzip')); |
103
|
|
|
$this->assertTrue($validator->compareMime('application/x-gzip', 'application/gzip')); |
104
|
|
|
$this->assertFalse($validator->compareMime('application/png', 'application/json')); |
105
|
|
|
$this->assertFalse($validator->compareMime('text/plain', 'text/json')); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|