2015-04-09 16:19:57 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @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 <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-09-08 13:17:57 +03:00
|
|
|
namespace Test\Files\Type;
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
use OC\Files\Type\Detection;
|
2019-12-08 19:20:50 +03:00
|
|
|
use OCP\ILogger;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IURLGenerator;
|
2015-04-09 16:19:57 +03:00
|
|
|
|
|
|
|
class DetectionTest extends \Test\TestCase {
|
2015-09-08 13:17:57 +03:00
|
|
|
/** @var Detection */
|
|
|
|
private $detection;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-09-08 13:17:57 +03:00
|
|
|
parent::setUp();
|
|
|
|
$this->detection = new Detection(
|
|
|
|
\OC::$server->getURLGenerator(),
|
2019-12-08 19:20:50 +03:00
|
|
|
\OC::$server->getLogger(),
|
2015-09-08 13:17:57 +03:00
|
|
|
\OC::$SERVERROOT . '/config/',
|
|
|
|
\OC::$SERVERROOT . '/resources/config/'
|
|
|
|
);
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
public function dataDetectPath(): array {
|
|
|
|
return [
|
|
|
|
['foo.txt', 'text/plain'],
|
|
|
|
['foo.png', 'image/png'],
|
|
|
|
['foo.bar.png', 'image/png'],
|
|
|
|
['.hidden.png', 'image/png'],
|
|
|
|
['.hidden.foo.png', 'image/png'],
|
|
|
|
['.hidden/foo.png', 'image/png'],
|
|
|
|
['.hidden/.hidden.png', 'image/png'],
|
|
|
|
['test.jpg/foo.png', 'image/png'],
|
|
|
|
['.png', 'application/octet-stream'],
|
|
|
|
['..hidden', 'application/octet-stream'],
|
|
|
|
['foo', 'application/octet-stream'],
|
|
|
|
['', 'application/octet-stream'],
|
|
|
|
['foo.png.ocTransferId123456789.part', 'image/png'],
|
|
|
|
['foo.png.v1234567890', 'image/png'],
|
|
|
|
];
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataDetectPath
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testDetectPath(string $path, string $expected): void {
|
|
|
|
$this->assertEquals($expected, $this->detection->detectPath($path));
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
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'],
|
|
|
|
];
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
/**
|
|
|
|
* @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));
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
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'],
|
|
|
|
];
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
/**
|
|
|
|
* @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));
|
2015-04-09 16:19:57 +03:00
|
|
|
}
|
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
public function testDetectString(): void {
|
|
|
|
$result = $this->detection->detectString('/data/data.tar.gz');
|
2015-04-09 16:19:57 +03:00
|
|
|
$expected = 'text/plain';
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
public function dataGetSecureMimeType(): array {
|
|
|
|
return [
|
|
|
|
['image/svg+xml', 'text/plain'],
|
|
|
|
['image/png', 'image/png'],
|
|
|
|
];
|
2015-04-09 16:19:57 +03:00
|
|
|
}
|
|
|
|
|
2019-12-05 00:32:54 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataGetSecureMimeType
|
|
|
|
*
|
|
|
|
* @param string $mimeType
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testGetSecureMimeType(string $mimeType, string $expected): void {
|
|
|
|
$this->assertEquals($expected, $this->detection->getSecureMimeType($mimeType));
|
2015-04-09 16:19:57 +03:00
|
|
|
}
|
|
|
|
|
2015-07-20 13:28:36 +03:00
|
|
|
public function testMimeTypeIcon() {
|
|
|
|
if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
|
2015-09-08 13:17:57 +03:00
|
|
|
$this->markTestSkipped('Package vfsStream not installed');
|
2015-07-20 13:28:36 +03:00
|
|
|
}
|
|
|
|
$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
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2019-12-09 00:16:25 +03:00
|
|
|
/** @var ILogger $logger */
|
|
|
|
$logger = $this->createMock(ILogger::class);
|
2019-12-08 19:20:50 +03:00
|
|
|
|
2015-07-20 13:28:36 +03:00
|
|
|
//Only call the url generator once
|
|
|
|
$urlGenerator->expects($this->once())
|
|
|
|
->method('imagePath')
|
|
|
|
->with($this->equalTo('core'), $this->equalTo('filetypes/folder.png'))
|
|
|
|
->willReturn('folder.svg');
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('dir');
|
|
|
|
$this->assertEquals('folder.svg', $mimeType);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test dir-shareed mimetype
|
|
|
|
*/
|
|
|
|
//Mock UrlGenerator
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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');
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('dir-shared');
|
|
|
|
$this->assertEquals('folder-shared.svg', $mimeType);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test dir external
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Mock UrlGenerator
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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');
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('dir-external');
|
|
|
|
$this->assertEquals('folder-external.svg', $mimeType);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test complete mimetype
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Mock UrlGenerator
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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');
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('my-type');
|
|
|
|
$this->assertEquals('my-type.svg', $mimeType);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test subtype
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Mock UrlGenerator
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('my-type');
|
|
|
|
$this->assertEquals('my.svg', $mimeType);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test default mimetype
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Mock UrlGenerator
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
|
|
|
$this->assertEquals('file.svg', $mimeType);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test chaching
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Mock UrlGenerator
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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');
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$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
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2015-07-20 13:28:36 +03:00
|
|
|
->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');
|
|
|
|
|
2019-12-08 19:20:50 +03:00
|
|
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
2015-07-20 13:28:36 +03:00
|
|
|
$mimeType = $detection->mimeTypeIcon('foo');
|
|
|
|
$this->assertEquals('foobar-baz.svg', $mimeType);
|
|
|
|
}
|
2015-04-09 16:19:57 +03:00
|
|
|
}
|