summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/private/files/type/detection.php19
-rw-r--r--lib/private/server.php4
-rw-r--r--tests/lib/files/type/detection.php203
3 files changed, 214 insertions, 12 deletions
diff --git a/lib/private/files/type/detection.php b/lib/private/files/type/detection.php
index 7c6c87250e2..ba286637df3 100644
--- a/lib/private/files/type/detection.php
+++ b/lib/private/files/type/detection.php
@@ -47,11 +47,16 @@ class Detection implements IMimeTypeDetector {
/** @var IURLGenerator */
private $urlGenerator;
+ /** @var string */
+ private $configDir;
+
/**
* @param IURLGenerator $urlGenerator
+ * @param string $configDir
*/
- public function __construct(IURLGenerator $urlGenerator) {
+ public function __construct(IURLGenerator $urlGenerator, $configDir) {
$this->urlGenerator = $urlGenerator;
+ $this->configDir = $configDir;
}
/**
@@ -96,11 +101,11 @@ class Detection implements IMimeTypeDetector {
return;
}
- $file = file_get_contents(\OC::$configDir . '/mimetypealiases.dist.json');
+ $file = file_get_contents($this->configDir . '/mimetypealiases.dist.json');
$this->mimeTypeAlias = get_object_vars(json_decode($file));
- if (file_exists(\OC::$configDir . '/mimetypealiases.json')) {
- $custom = get_object_vars(json_decode(file_get_contents(\OC::$configDir . '/mimetypealiases.json')));
+ if (file_exists($this->configDir . '/mimetypealiases.json')) {
+ $custom = get_object_vars(json_decode(file_get_contents($this->configDir . '/mimetypealiases.json')));
$this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom);
}
}
@@ -113,12 +118,12 @@ class Detection implements IMimeTypeDetector {
return;
}
- $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json');
+ $dist = file_get_contents($this->configDir . '/mimetypemapping.dist.json');
$mimetypemapping = get_object_vars(json_decode($dist));
//Check if need to load custom mappings
- if (file_exists(\OC::$configDir . '/mimetypemapping.json')) {
- $custom = file_get_contents(\OC::$configDir . '/mimetypemapping.json');
+ if (file_exists($this->configDir . '/mimetypemapping.json')) {
+ $custom = file_get_contents($this->configDir . '/mimetypemapping.json');
$custom_mapping = get_object_vars(json_decode($custom));
$mimetypemapping = array_merge($mimetypemapping, $custom_mapping);
}
diff --git a/lib/private/server.php b/lib/private/server.php
index 82b2d6765dc..12981fe7f19 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -445,7 +445,9 @@ class Server extends SimpleContainer implements IServerContainer {
return new \OC\Files\Mount\Manager();
});
$this->registerService('MimeTypeDetector', function(Server $c) {
- return new \OC\Files\Type\Detection($c->getURLGenerator());
+ return new \OC\Files\Type\Detection(
+ $c->getURLGenerator(),
+ \OC::$configDir);
});
}
diff --git a/tests/lib/files/type/detection.php b/tests/lib/files/type/detection.php
index 7bd4c430544..1997fa4dfd6 100644
--- a/tests/lib/files/type/detection.php
+++ b/tests/lib/files/type/detection.php
@@ -26,7 +26,7 @@ use \OC\Files\Type\Detection;
class DetectionTest extends \Test\TestCase {
public function testDetect() {
- $detection = new Detection(\OC::$server->getURLGenerator());
+ $detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
$dir = \OC::$SERVERROOT.'/tests/data';
$result = $detection->detect($dir."/");
@@ -51,7 +51,7 @@ class DetectionTest extends \Test\TestCase {
}
public function testGetSecureMimeType() {
- $detection = new Detection(\OC::$server->getURLGenerator());
+ $detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
$result = $detection->getSecureMimeType('image/svg+xml');
$expected = 'text/plain';
@@ -63,7 +63,7 @@ class DetectionTest extends \Test\TestCase {
}
public function testDetectPath() {
- $detection = new Detection(\OC::$server->getURLGenerator());
+ $detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
$this->assertEquals('text/plain', $detection->detectPath('foo.txt'));
$this->assertEquals('image/png', $detection->detectPath('foo.png'));
@@ -78,11 +78,206 @@ class DetectionTest extends \Test\TestCase {
$this->markTestSkipped('[Windows] Strings have mimetype application/octet-stream on Windows');
}
- $detection = new Detection(\OC::$server->getURLGenerator());
+ $detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
$result = $detection->detectString("/data/data.tar.gz");
$expected = 'text/plain; charset=us-ascii';
$this->assertEquals($expected, $result);
}
+ public function testMimeTypeIcon() {
+ if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
+ $this->markTestSkipped('Pacakge vfsStream not installed');
+ }
+ $confDir = \org\bovigo\vfs\vfsStream::setup();
+ $mimetypealiases_dist = \org\bovigo\vfs\vfsStream::newFile('mimetypealiases.dist.json')->at($confDir);
+
+ //Empty alias file
+ $mimetypealiases_dist->setContent(json_encode([], JSON_FORCE_OBJECT));
+
+
+ /*
+ * Test dir mimetype
+ */
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->once())
+ ->method('imagePath')
+ ->with($this->equalTo('core'), $this->equalTo('filetypes/folder.png'))
+ ->willReturn('folder.svg');
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('dir');
+ $this->assertEquals('folder.svg', $mimeType);
+
+
+ /*
+ * Test dir-shareed mimetype
+ */
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->once())
+ ->method('imagePath')
+ ->with($this->equalTo('core'), $this->equalTo('filetypes/folder-shared.png'))
+ ->willReturn('folder-shared.svg');
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('dir-shared');
+ $this->assertEquals('folder-shared.svg', $mimeType);
+
+
+ /*
+ * Test dir external
+ */
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->once())
+ ->method('imagePath')
+ ->with($this->equalTo('core'), $this->equalTo('filetypes/folder-external.png'))
+ ->willReturn('folder-external.svg');
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('dir-external');
+ $this->assertEquals('folder-external.svg', $mimeType);
+
+
+ /*
+ * Test complete mimetype
+ */
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->once())
+ ->method('imagePath')
+ ->with($this->equalTo('core'), $this->equalTo('filetypes/my-type.png'))
+ ->willReturn('my-type.svg');
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('my-type');
+ $this->assertEquals('my-type.svg', $mimeType);
+
+
+ /*
+ * Test subtype
+ */
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->exactly(2))
+ ->method('imagePath')
+ ->withConsecutive(
+ [$this->equalTo('core'), $this->equalTo('filetypes/my-type.png')],
+ [$this->equalTo('core'), $this->equalTo('filetypes/my.png')]
+ )
+ ->will($this->returnCallback(
+ function($appName, $file) {
+ if ($file === 'filetypes/my.png') {
+ return 'my.svg';
+ }
+ throw new \RuntimeException();
+ }
+ ));
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('my-type');
+ $this->assertEquals('my.svg', $mimeType);
+
+
+ /*
+ * Test default mimetype
+ */
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->exactly(3))
+ ->method('imagePath')
+ ->withConsecutive(
+ [$this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png')],
+ [$this->equalTo('core'), $this->equalTo('filetypes/foo.png')],
+ [$this->equalTo('core'), $this->equalTo('filetypes/file.png')]
+ )
+ ->will($this->returnCallback(
+ function($appName, $file) {
+ if ($file === 'filetypes/file.png') {
+ return 'file.svg';
+ }
+ throw new \RuntimeException();
+ }
+ ));
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('foo-bar');
+ $this->assertEquals('file.svg', $mimeType);
+
+ /*
+ * Test chaching
+ */
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->once())
+ ->method('imagePath')
+ ->with($this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png'))
+ ->willReturn('foo-bar.svg');
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('foo-bar');
+ $this->assertEquals('foo-bar.svg', $mimeType);
+ $mimeType = $detection->mimeTypeIcon('foo-bar');
+ $this->assertEquals('foo-bar.svg', $mimeType);
+
+
+
+ /*
+ * Test aliases
+ */
+
+ //Put alias
+ $mimetypealiases_dist->setContent(json_encode(['foo' => 'foobar/baz'], JSON_FORCE_OBJECT));
+
+ //Mock UrlGenerator
+ $urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ //Only call the url generator once
+ $urlGenerator->expects($this->once())
+ ->method('imagePath')
+ ->with($this->equalTo('core'), $this->equalTo('filetypes/foobar-baz.png'))
+ ->willReturn('foobar-baz.svg');
+
+ $detection = new Detection($urlGenerator, $confDir->url());
+ $mimeType = $detection->mimeTypeIcon('foo');
+ $this->assertEquals('foobar-baz.svg', $mimeType);
+ }
}