Completed
Push — master ( 79e63b...a5181c )
by Daniel
05:26 queued 02:19
created

tests/MimeUploadValidatorTest.php (2 issues)

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
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
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
}
0 ignored issues
show
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
74