Completed
Push — master ( a5181c...e2d7cf )
by Damian
09:50
created

tests/MimeUploadValidatorTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
class MimeUploadValidatorTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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