1
|
|
|
<?php |
2
|
|
|
class MimeUploadValidatorTest extends SapphireTest { |
|
|
|
|
3
|
|
|
|
4
|
|
|
public function testInvalidFileExtensionValidatingMimeType() { |
5
|
|
|
// setup plaintext file with invalid extension |
6
|
|
|
$tmpFileName = 'UploadTest-testUpload.jpg'; |
7
|
|
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
8
|
|
|
$tmpFileContent = ''; |
9
|
|
|
for($i=0; $i<10000; $i++) $tmpFileContent .= '0'; |
10
|
|
|
file_put_contents($tmpFilePath, $tmpFileContent); |
11
|
|
|
|
12
|
|
|
// emulates the $_FILES array |
13
|
|
|
$tmpFile = array( |
14
|
|
|
'name' => $tmpFileName, |
15
|
|
|
'size' => filesize($tmpFilePath), |
16
|
|
|
'tmp_name' => $tmpFilePath, |
17
|
|
|
'extension' => 'jpg', |
18
|
|
|
'error' => UPLOAD_ERR_OK, |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
$u = new Upload(); |
22
|
|
|
$u->setValidator(new MimeUploadValidator()); |
23
|
|
|
$result = $u->load($tmpFile); |
24
|
|
|
$errors = $u->getErrors(); |
25
|
|
|
$this->assertFalse($result, 'Load failed because file extension does not match excepted MIME type'); |
|
|
|
|
26
|
|
|
$this->assertEquals('File extension does not match known MIME type', $errors[0]); |
|
|
|
|
27
|
|
|
|
28
|
|
|
unlink($tmpFilePath); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function testGetExpectedMimeTypes() { |
33
|
|
|
// Setup a file with a capitalised extension and try to match it against a lowercase file. |
34
|
|
|
$tmpFileName = 'text.TXT'; |
35
|
|
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; |
36
|
|
|
$tmpFileContent = ''; |
37
|
|
|
for($i=0; $i<10000; $i++) $tmpFileContent .= '0'; |
38
|
|
|
file_put_contents($tmpFilePath, $tmpFileContent); |
39
|
|
|
|
40
|
|
|
$validator = new MimeUploadValidator(); |
41
|
|
|
$tmpFile = array( |
42
|
|
|
'name' => $tmpFileName, |
43
|
|
|
'tmp_name' => $tmpFilePath, |
44
|
|
|
); |
45
|
|
|
$expected = $validator->getExpectedMimeTypes($tmpFile); |
46
|
|
|
$this->assertCount(1, $expected); |
|
|
|
|
47
|
|
|
$this->assertContains('text/plain', $expected); |
48
|
|
|
|
49
|
|
|
unlink($tmpFilePath); |
50
|
|
|
|
51
|
|
|
// Test a physical ico file with capitalised extension |
52
|
|
|
$tmpFile = array( |
53
|
|
|
'name' => 'favicon.ICO', |
54
|
|
|
'tmp_name' => 'assets/favicon.ICO', |
55
|
|
|
); |
56
|
|
|
$expected = $validator->getExpectedMimeTypes($tmpFile); |
57
|
|
|
$this->assertCount(3, $expected); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testMimeComparison() { |
61
|
|
|
$validator = new MimeUploadValidator(); |
62
|
|
|
|
63
|
|
|
$this->assertTrue($validator->compareMime('application/xhtml+xml', 'application/xml')); |
|
|
|
|
64
|
|
|
$this->assertTrue($validator->compareMime('application/vnd.text', 'application/text')); |
|
|
|
|
65
|
|
|
$this->assertTrue($validator->compareMime('application/vnd.vnd.text', 'application/text')); |
|
|
|
|
66
|
|
|
$this->assertTrue($validator->compareMime('application/x-text', 'application/text')); |
|
|
|
|
67
|
|
|
$this->assertTrue($validator->compareMime('application/gzip', 'application/gzip')); |
|
|
|
|
68
|
|
|
$this->assertTrue($validator->compareMime('application/x-gzip', 'application/gzip')); |
|
|
|
|
69
|
|
|
$this->assertFalse($validator->compareMime('application/png', 'application/json')); |
|
|
|
|
70
|
|
|
$this->assertFalse($validator->compareMime('text/plain', 'text/json')); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
|
|
|
|
74
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.