You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DetectionTest.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Files\Type;
  22. use OC\Files\Type\Detection;
  23. use OCP\IURLGenerator;
  24. use Psr\Log\LoggerInterface;
  25. class DetectionTest extends \Test\TestCase {
  26. /** @var Detection */
  27. private $detection;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->detection = new Detection(
  31. \OC::$server->getURLGenerator(),
  32. \OC::$server->get(LoggerInterface::class),
  33. \OC::$SERVERROOT . '/config/',
  34. \OC::$SERVERROOT . '/resources/config/'
  35. );
  36. }
  37. public function dataDetectPath(): array {
  38. return [
  39. ['foo.txt', 'text/plain'],
  40. ['foo.png', 'image/png'],
  41. ['foo.bar.png', 'image/png'],
  42. ['.hidden.png', 'image/png'],
  43. ['.hidden.foo.png', 'image/png'],
  44. ['.hidden/foo.png', 'image/png'],
  45. ['.hidden/.hidden.png', 'image/png'],
  46. ['test.jpg/foo.png', 'image/png'],
  47. ['.png', 'application/octet-stream'],
  48. ['..hidden', 'application/octet-stream'],
  49. ['foo', 'application/octet-stream'],
  50. ['', 'application/octet-stream'],
  51. ['foo.png.ocTransferId123456789.part', 'image/png'],
  52. ['foo.png.v1234567890', 'image/png'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider dataDetectPath
  57. *
  58. * @param string $path
  59. * @param string $expected
  60. */
  61. public function testDetectPath(string $path, string $expected): void {
  62. $this->assertEquals($expected, $this->detection->detectPath($path));
  63. }
  64. public function dataDetectContent(): array {
  65. return [
  66. ['/', 'httpd/unix-directory'],
  67. ['/data.tar.gz', 'application/gzip'],
  68. ['/data.zip', 'application/zip'],
  69. ['/testimage.mp3', 'audio/mpeg'],
  70. ['/testimage.png', 'image/png'],
  71. ];
  72. }
  73. /**
  74. * @dataProvider dataDetectContent
  75. *
  76. * @param string $path
  77. * @param string $expected
  78. */
  79. public function testDetectContent(string $path, string $expected): void {
  80. $this->assertEquals($expected, $this->detection->detectContent(\OC::$SERVERROOT . '/tests/data' . $path));
  81. }
  82. public function dataDetect(): array {
  83. return [
  84. ['/', 'httpd/unix-directory'],
  85. ['/data.tar.gz', 'application/gzip'],
  86. ['/data.zip', 'application/zip'],
  87. ['/testimagelarge.svg', 'image/svg+xml'],
  88. ['/testimage.png', 'image/png'],
  89. ];
  90. }
  91. /**
  92. * @dataProvider dataDetect
  93. *
  94. * @param string $path
  95. * @param string $expected
  96. */
  97. public function testDetect(string $path, string $expected): void {
  98. $this->assertEquals($expected, $this->detection->detect(\OC::$SERVERROOT . '/tests/data' . $path));
  99. }
  100. public function testDetectString(): void {
  101. $result = $this->detection->detectString('/data/data.tar.gz');
  102. $expected = 'text/plain';
  103. $this->assertEquals($expected, $result);
  104. }
  105. public function dataGetSecureMimeType(): array {
  106. return [
  107. ['image/svg+xml', 'text/plain'],
  108. ['image/png', 'image/png'],
  109. ];
  110. }
  111. /**
  112. * @dataProvider dataGetSecureMimeType
  113. *
  114. * @param string $mimeType
  115. * @param string $expected
  116. */
  117. public function testGetSecureMimeType(string $mimeType, string $expected): void {
  118. $this->assertEquals($expected, $this->detection->getSecureMimeType($mimeType));
  119. }
  120. public function testMimeTypeIcon() {
  121. if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
  122. $this->markTestSkipped('Package vfsStream not installed');
  123. }
  124. $confDir = \org\bovigo\vfs\vfsStream::setup();
  125. $mimetypealiases_dist = \org\bovigo\vfs\vfsStream::newFile('mimetypealiases.dist.json')->at($confDir);
  126. //Empty alias file
  127. $mimetypealiases_dist->setContent(json_encode([], JSON_FORCE_OBJECT));
  128. /*
  129. * Test dir mimetype
  130. */
  131. //Mock UrlGenerator
  132. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. /** @var LoggerInterface $logger */
  136. $logger = $this->createMock(LoggerInterface::class);
  137. //Only call the url generator once
  138. $urlGenerator->expects($this->once())
  139. ->method('imagePath')
  140. ->with($this->equalTo('core'), $this->equalTo('filetypes/folder.png'))
  141. ->willReturn('folder.svg');
  142. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  143. $mimeType = $detection->mimeTypeIcon('dir');
  144. $this->assertEquals('folder.svg', $mimeType);
  145. /*
  146. * Test dir-shareed mimetype
  147. */
  148. //Mock UrlGenerator
  149. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. //Only call the url generator once
  153. $urlGenerator->expects($this->once())
  154. ->method('imagePath')
  155. ->with($this->equalTo('core'), $this->equalTo('filetypes/folder-shared.png'))
  156. ->willReturn('folder-shared.svg');
  157. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  158. $mimeType = $detection->mimeTypeIcon('dir-shared');
  159. $this->assertEquals('folder-shared.svg', $mimeType);
  160. /*
  161. * Test dir external
  162. */
  163. //Mock UrlGenerator
  164. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. //Only call the url generator once
  168. $urlGenerator->expects($this->once())
  169. ->method('imagePath')
  170. ->with($this->equalTo('core'), $this->equalTo('filetypes/folder-external.png'))
  171. ->willReturn('folder-external.svg');
  172. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  173. $mimeType = $detection->mimeTypeIcon('dir-external');
  174. $this->assertEquals('folder-external.svg', $mimeType);
  175. /*
  176. * Test complete mimetype
  177. */
  178. //Mock UrlGenerator
  179. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. //Only call the url generator once
  183. $urlGenerator->expects($this->once())
  184. ->method('imagePath')
  185. ->with($this->equalTo('core'), $this->equalTo('filetypes/my-type.png'))
  186. ->willReturn('my-type.svg');
  187. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  188. $mimeType = $detection->mimeTypeIcon('my-type');
  189. $this->assertEquals('my-type.svg', $mimeType);
  190. /*
  191. * Test subtype
  192. */
  193. //Mock UrlGenerator
  194. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  195. ->disableOriginalConstructor()
  196. ->getMock();
  197. //Only call the url generator once
  198. $urlGenerator->expects($this->exactly(2))
  199. ->method('imagePath')
  200. ->withConsecutive(
  201. [$this->equalTo('core'), $this->equalTo('filetypes/my-type.png')],
  202. [$this->equalTo('core'), $this->equalTo('filetypes/my.png')]
  203. )
  204. ->willReturnCallback(
  205. function ($appName, $file) {
  206. if ($file === 'filetypes/my.png') {
  207. return 'my.svg';
  208. }
  209. throw new \RuntimeException();
  210. }
  211. );
  212. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  213. $mimeType = $detection->mimeTypeIcon('my-type');
  214. $this->assertEquals('my.svg', $mimeType);
  215. /*
  216. * Test default mimetype
  217. */
  218. //Mock UrlGenerator
  219. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  220. ->disableOriginalConstructor()
  221. ->getMock();
  222. //Only call the url generator once
  223. $urlGenerator->expects($this->exactly(3))
  224. ->method('imagePath')
  225. ->withConsecutive(
  226. [$this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png')],
  227. [$this->equalTo('core'), $this->equalTo('filetypes/foo.png')],
  228. [$this->equalTo('core'), $this->equalTo('filetypes/file.png')]
  229. )
  230. ->willReturnCallback(
  231. function ($appName, $file) {
  232. if ($file === 'filetypes/file.png') {
  233. return 'file.svg';
  234. }
  235. throw new \RuntimeException();
  236. }
  237. );
  238. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  239. $mimeType = $detection->mimeTypeIcon('foo-bar');
  240. $this->assertEquals('file.svg', $mimeType);
  241. /*
  242. * Test chaching
  243. */
  244. //Mock UrlGenerator
  245. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. //Only call the url generator once
  249. $urlGenerator->expects($this->once())
  250. ->method('imagePath')
  251. ->with($this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png'))
  252. ->willReturn('foo-bar.svg');
  253. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  254. $mimeType = $detection->mimeTypeIcon('foo-bar');
  255. $this->assertEquals('foo-bar.svg', $mimeType);
  256. $mimeType = $detection->mimeTypeIcon('foo-bar');
  257. $this->assertEquals('foo-bar.svg', $mimeType);
  258. /*
  259. * Test aliases
  260. */
  261. //Put alias
  262. $mimetypealiases_dist->setContent(json_encode(['foo' => 'foobar/baz'], JSON_FORCE_OBJECT));
  263. //Mock UrlGenerator
  264. $urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. //Only call the url generator once
  268. $urlGenerator->expects($this->once())
  269. ->method('imagePath')
  270. ->with($this->equalTo('core'), $this->equalTo('filetypes/foobar-baz.png'))
  271. ->willReturn('foobar-baz.svg');
  272. $detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
  273. $mimeType = $detection->mimeTypeIcon('foo');
  274. $this->assertEquals('foobar-baz.svg', $mimeType);
  275. }
  276. }