Update the unit test to dataProviders

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2019-12-04 22:32:54 +01:00 committed by Backportbot
parent 25d7d6965d
commit b7b7380d6b
1 changed files with 82 additions and 47 deletions

View File

@ -37,61 +37,96 @@ class DetectionTest extends \Test\TestCase {
); );
} }
public function testDetect() { public function dataDetectPath(): array {
$dir = \OC::$SERVERROOT.'/tests/data'; return [
['foo.txt', 'text/plain'],
$result = $this->detection->detect($dir."/"); ['foo.png', 'image/png'],
$expected = 'httpd/unix-directory'; ['foo.bar.png', 'image/png'],
$this->assertEquals($expected, $result); ['.hidden.png', 'image/png'],
['.hidden.foo.png', 'image/png'],
$result = $this->detection->detect($dir."/data.tar.gz"); ['.hidden/foo.png', 'image/png'],
$expected = 'application/x-gzip'; ['.hidden/.hidden.png', 'image/png'],
$this->assertEquals($expected, $result); ['test.jpg/foo.png', 'image/png'],
['.png', 'application/octet-stream'],
$result = $this->detection->detect($dir."/data.zip"); ['..hidden', 'application/octet-stream'],
$expected = 'application/zip'; ['foo', 'application/octet-stream'],
$this->assertEquals($expected, $result); ['', 'application/octet-stream'],
['foo.png.ocTransferId123456789.part', 'image/png'],
$result = $this->detection->detect($dir."/testimagelarge.svg"); ['foo.png.v1234567890', 'image/png'],
$expected = 'image/svg+xml'; ];
$this->assertEquals($expected, $result);
$result = $this->detection->detect($dir."/testimage.png");
$expected = 'image/png';
$this->assertEquals($expected, $result);
} }
public function testGetSecureMimeType() { /**
$result = $this->detection->getSecureMimeType('image/svg+xml'); * @dataProvider dataDetectPath
*
* @param string $path
* @param string $expected
*/
public function testDetectPath(string $path, string $expected): void {
$this->assertEquals($expected, $this->detection->detectPath($path));
}
public function dataDetectContent(): array {
return [
['/', 'httpd/unix-directory'],
['/data.tar.gz', 'application/x-gzip'],
['/data.zip', 'application/zip'],
['/testimage.mp3', 'audio/mpeg'],
['/testimage.png', 'image/png'],
];
}
/**
* @dataProvider dataDetectContent
*
* @param string $path
* @param string $expected
*/
public function testDetectContent(string $path, string $expected): void {
$this->assertEquals($expected, $this->detection->detectContent(\OC::$SERVERROOT . '/tests/data' . $path));
}
public function dataDetect(): array {
return [
['/', 'httpd/unix-directory'],
['/data.tar.gz', 'application/x-gzip'],
['/data.zip', 'application/zip'],
['/testimagelarge.svg', 'image/svg+xml'],
['/testimage.png', 'image/png'],
];
}
/**
* @dataProvider dataDetect
*
* @param string $path
* @param string $expected
*/
public function testDetect(string $path, string $expected): void {
$this->assertEquals($expected, $this->detection->detect(\OC::$SERVERROOT . '/tests/data' . $path));
}
public function testDetectString(): void {
$result = $this->detection->detectString('/data/data.tar.gz');
$expected = 'text/plain'; $expected = 'text/plain';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->detection->getSecureMimeType('image/png');
$expected = 'image/png';
$this->assertEquals($expected, $result);
} }
public function testDetectPath() { public function dataGetSecureMimeType(): array {
$this->assertEquals('text/plain', $this->detection->detectPath('foo.txt')); return [
$this->assertEquals('image/png', $this->detection->detectPath('foo.png')); ['image/svg+xml', 'text/plain'],
$this->assertEquals('image/png', $this->detection->detectPath('foo.bar.png')); ['image/png', 'image/png'],
$this->assertEquals('image/png', $this->detection->detectPath('.hidden.png')); ];
$this->assertEquals('image/png', $this->detection->detectPath('.hidden.foo.png'));
$this->assertEquals('image/png', $this->detection->detectPath('.hidden/foo.png'));
$this->assertEquals('image/png', $this->detection->detectPath('.hidden/.hidden.png'));
$this->assertEquals('image/png', $this->detection->detectPath('test.jpg/foo.png'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('.png'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('..hidden'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('foo'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath(''));
$this->assertEquals('image/png', $this->detection->detectPath('foo.png.ocTransferId123456789.part'));
$this->assertEquals('image/png', $this->detection->detectPath('foo.png.v1234567890'));
} }
public function testDetectString() { /**
$result = $this->detection->detectString("/data/data.tar.gz"); * @dataProvider dataGetSecureMimeType
$expected = 'text/plain'; *
$this->assertEquals($expected, $result); * @param string $mimeType
* @param string $expected
*/
public function testGetSecureMimeType(string $mimeType, string $expected): void {
$this->assertEquals($expected, $this->detection->getSecureMimeType($mimeType));
} }
public function testMimeTypeIcon() { public function testMimeTypeIcon() {