From 6db6689740a5d11dd53b2502d1eea6e9157479df Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 9 Apr 2015 15:19:57 +0200 Subject: Added mimetype detector * Copied unit tests from old functions --- tests/lib/files/type/detection.php | 97 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/lib/files/type/detection.php (limited to 'tests') diff --git a/tests/lib/files/type/detection.php b/tests/lib/files/type/detection.php new file mode 100644 index 00000000000..1483839bf7f --- /dev/null +++ b/tests/lib/files/type/detection.php @@ -0,0 +1,97 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Files\Type; + +use \OC\Files\Type\Detection; + +class DetectionTest extends \Test\TestCase { + + public function testDetect() { + $detection = new Detection(); + $dir = \OC::$SERVERROOT.'/tests/data'; + + $result = $detection->detect($dir."/"); + $expected = 'httpd/unix-directory'; + $this->assertEquals($expected, $result); + + $result = $detection->detect($dir."/data.tar.gz"); + $expected = 'application/x-gzip'; + $this->assertEquals($expected, $result); + + $result = $detection->detect($dir."/data.zip"); + $expected = 'application/zip'; + $this->assertEquals($expected, $result); + + $result = $detection->detect($dir."/testimagelarge.svg"); + $expected = 'image/svg+xml'; + $this->assertEquals($expected, $result); + + $result = $detection->detect($dir."/testimage.png"); + $expected = 'image/png'; + $this->assertEquals($expected, $result); + } + + public function testGetSecureMimeType() { + $detection = new Detection(); + $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); + $mimetypemapping = get_object_vars(json_decode($dist)); + $detection->registerTypeArray($mimetypemapping); + + $result = $detection->getSecureMimeType('image/svg+xml'); + $expected = 'text/plain'; + $this->assertEquals($expected, $result); + + $result = $detection->getSecureMimeType('image/png'); + $expected = 'image/png'; + $this->assertEquals($expected, $result); + } + + public function testDetectPath() { + $detection = new Detection(); + $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); + $mimetypemapping = get_object_vars(json_decode($dist)); + $detection->registerTypeArray($mimetypemapping); + + $this->assertEquals('text/plain', $detection->detectPath('foo.txt')); + $this->assertEquals('image/png', $detection->detectPath('foo.png')); + $this->assertEquals('image/png', $detection->detectPath('foo.bar.png')); + $this->assertEquals('application/octet-stream', $detection->detectPath('.png')); + $this->assertEquals('application/octet-stream', $detection->detectPath('foo')); + $this->assertEquals('application/octet-stream', $detection->detectPath('')); + } + + public function testDetectString() { + if (\OC_Util::runningOnWindows()) { + $this->markTestSkipped('[Windows] Strings have mimetype application/octet-stream on Windows'); + } + + $detection = new Detection(); + $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); + $mimetypemapping = get_object_vars(json_decode($dist)); + $detection->registerTypeArray($mimetypemapping); + + $result = $detection->detectString("/data/data.tar.gz"); + $expected = 'text/plain; charset=us-ascii'; + $this->assertEquals($expected, $result); + } + +} -- cgit v1.2.3