helper.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_Helper extends \Test\TestCase {
  9. /**
  10. * @dataProvider humanFileSizeProvider
  11. */
  12. public function testHumanFileSize($expected, $input)
  13. {
  14. $result = OC_Helper::humanFileSize($input);
  15. $this->assertEquals($expected, $result);
  16. }
  17. public function humanFileSizeProvider()
  18. {
  19. return array(
  20. array('0 B', 0),
  21. array('1 kB', 1024),
  22. array('9.5 MB', 10000000),
  23. array('1.3 GB', 1395864371),
  24. array('465.7 GB', 500000000000),
  25. array('454.7 TB', 500000000000000),
  26. array('444.1 PB', 500000000000000000),
  27. );
  28. }
  29. /**
  30. * @dataProvider phpFileSizeProvider
  31. */
  32. public function testPhpFileSize($expected, $input)
  33. {
  34. $result = OC_Helper::phpFileSize($input);
  35. $this->assertEquals($expected, $result);
  36. }
  37. public function phpFileSizeProvider()
  38. {
  39. return array(
  40. array('0B', 0),
  41. array('1K', 1024),
  42. array('9.5M', 10000000),
  43. array('1.3G', 1395864371),
  44. array('465.7G', 500000000000),
  45. array('465661.3G', 500000000000000),
  46. array('465661287.3G', 500000000000000000),
  47. );
  48. }
  49. /**
  50. * @dataProvider computerFileSizeProvider
  51. */
  52. function testComputerFileSize($expected, $input) {
  53. $result = OC_Helper::computerFileSize($input);
  54. $this->assertEquals($expected, $result);
  55. }
  56. function computerFileSizeProvider(){
  57. return array(
  58. array(0.0, "0 B"),
  59. array(1024.0, "1 kB"),
  60. array(1395864371.0, '1.3 GB'),
  61. array(9961472.0, "9.5 MB"),
  62. array(500041567437.0, "465.7 GB"),
  63. );
  64. }
  65. function testGetMimeType() {
  66. $dir=OC::$SERVERROOT.'/tests/data';
  67. $result = OC_Helper::getMimeType($dir."/");
  68. $expected = 'httpd/unix-directory';
  69. $this->assertEquals($result, $expected);
  70. $result = OC_Helper::getMimeType($dir."/data.tar.gz");
  71. $expected = 'application/x-gzip';
  72. $this->assertEquals($result, $expected);
  73. $result = OC_Helper::getMimeType($dir."/data.zip");
  74. $expected = 'application/zip';
  75. $this->assertEquals($result, $expected);
  76. $result = OC_Helper::getMimeType($dir."/logo-wide.svg");
  77. $expected = 'image/svg+xml';
  78. $this->assertEquals($result, $expected);
  79. $result = OC_Helper::getMimeType($dir."/logo-wide.png");
  80. $expected = 'image/png';
  81. $this->assertEquals($result, $expected);
  82. }
  83. function testGetSecureMimeType() {
  84. $dir=OC::$SERVERROOT.'/tests/data';
  85. $result = OC_Helper::getSecureMimeType('image/svg+xml');
  86. $expected = 'text/plain';
  87. $this->assertEquals($result, $expected);
  88. $result = OC_Helper::getSecureMimeType('image/png');
  89. $expected = 'image/png';
  90. $this->assertEquals($result, $expected);
  91. }
  92. function testGetFileNameMimeType() {
  93. $this->assertEquals('text/plain', OC_Helper::getFileNameMimeType('foo.txt'));
  94. $this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.png'));
  95. $this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.bar.png'));
  96. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('.png'));
  97. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('foo'));
  98. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType(''));
  99. }
  100. function testGetStringMimeType() {
  101. if (\OC_Util::runningOnWindows()) {
  102. $this->markTestSkipped('[Windows] Strings have mimetype application/octet-stream on Windows');
  103. }
  104. $result = OC_Helper::getStringMimeType("/data/data.tar.gz");
  105. $expected = 'text/plain; charset=us-ascii';
  106. $this->assertEquals($result, $expected);
  107. }
  108. function testIsSubDirectory() {
  109. $result = OC_Helper::isSubDirectory("./data/", "/anotherDirectory/");
  110. $this->assertFalse($result);
  111. $result = OC_Helper::isSubDirectory("./data/", "./data/");
  112. $this->assertTrue($result);
  113. mkdir("data/TestSubdirectory", 0777);
  114. $result = OC_Helper::isSubDirectory("data/TestSubdirectory/", "data");
  115. rmdir("data/TestSubdirectory");
  116. $this->assertTrue($result);
  117. }
  118. function testMb_array_change_key_case() {
  119. $arrayStart = array(
  120. "Foo" => "bar",
  121. "Bar" => "foo",
  122. );
  123. $arrayResult = array(
  124. "foo" => "bar",
  125. "bar" => "foo",
  126. );
  127. $result = OC_Helper::mb_array_change_key_case($arrayStart);
  128. $expected = $arrayResult;
  129. $this->assertEquals($result, $expected);
  130. $arrayStart = array(
  131. "foo" => "bar",
  132. "bar" => "foo",
  133. );
  134. $arrayResult = array(
  135. "FOO" => "bar",
  136. "BAR" => "foo",
  137. );
  138. $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER);
  139. $expected = $arrayResult;
  140. $this->assertEquals($result, $expected);
  141. }
  142. function testMb_substr_replace() {
  143. $result = OC_Helper::mb_substr_replace("This is a teststring", "string", 5);
  144. $expected = "This string is a teststring";
  145. $this->assertEquals($result, $expected);
  146. }
  147. function testMb_str_replace() {
  148. $result = OC_Helper::mb_str_replace("teststring", "string", "This is a teststring");
  149. $expected = "This is a string";
  150. $this->assertEquals($result, $expected);
  151. }
  152. function testRecursiveArraySearch() {
  153. $haystack = array(
  154. "Foo" => "own",
  155. "Bar" => "Cloud",
  156. );
  157. $result = OC_Helper::recursiveArraySearch($haystack, "own");
  158. $expected = "Foo";
  159. $this->assertEquals($result, $expected);
  160. $result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
  161. $this->assertFalse($result);
  162. }
  163. function testBuildNotExistingFileNameForView() {
  164. $viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false);
  165. $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock));
  166. $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  167. $viewMock->expects($this->at(0))
  168. ->method('file_exists')
  169. ->will($this->returnValue(true)); // filename.ext exists
  170. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  171. $viewMock->expects($this->at(0))
  172. ->method('file_exists')
  173. ->will($this->returnValue(true)); // filename.ext exists
  174. $viewMock->expects($this->at(1))
  175. ->method('file_exists')
  176. ->will($this->returnValue(true)); // filename (2).ext exists
  177. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  178. $viewMock->expects($this->at(0))
  179. ->method('file_exists')
  180. ->will($this->returnValue(true)); // filename (1).ext exists
  181. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
  182. $viewMock->expects($this->at(0))
  183. ->method('file_exists')
  184. ->will($this->returnValue(true)); // filename (2).ext exists
  185. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  186. $viewMock->expects($this->at(0))
  187. ->method('file_exists')
  188. ->will($this->returnValue(true)); // filename (2).ext exists
  189. $viewMock->expects($this->at(1))
  190. ->method('file_exists')
  191. ->will($this->returnValue(true)); // filename (3).ext exists
  192. $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  193. $viewMock->expects($this->at(0))
  194. ->method('file_exists')
  195. ->will($this->returnValue(true)); // filename(1).ext exists
  196. $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
  197. $viewMock->expects($this->at(0))
  198. ->method('file_exists')
  199. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  200. $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  201. $viewMock->expects($this->at(0))
  202. ->method('file_exists')
  203. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  204. $viewMock->expects($this->at(1))
  205. ->method('file_exists')
  206. ->will($this->returnValue(true)); // filename(1) (2).ext exists
  207. $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  208. $viewMock->expects($this->at(0))
  209. ->method('file_exists')
  210. ->will($this->returnValue(true)); // filename(1) (2) (3).ext exists
  211. $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
  212. }
  213. /**
  214. * @dataProvider streamCopyDataProvider
  215. */
  216. public function testStreamCopy($expectedCount, $expectedResult, $source, $target) {
  217. if (is_string($source)) {
  218. $source = fopen($source, 'r');
  219. }
  220. if (is_string($target)) {
  221. $target = fopen($target, 'w');
  222. }
  223. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  224. if (is_resource($source)) {
  225. fclose($source);
  226. }
  227. if (is_resource($target)) {
  228. fclose($target);
  229. }
  230. $this->assertSame($expectedCount, $count);
  231. $this->assertSame($expectedResult, $result);
  232. }
  233. function streamCopyDataProvider() {
  234. return array(
  235. array(0, false, false, false),
  236. array(0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false),
  237. array(filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'),
  238. array(3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'),
  239. );
  240. }
  241. // Url generator methods
  242. /**
  243. * @small
  244. * test absolute URL construction
  245. * @dataProvider provideDocRootURLs
  246. */
  247. function testMakeAbsoluteURLDocRoot($url, $expectedResult) {
  248. \OC::$WEBROOT = '';
  249. $result = \OC_Helper::makeURLAbsolute($url);
  250. $this->assertEquals($expectedResult, $result);
  251. }
  252. /**
  253. * @small
  254. * test absolute URL construction
  255. * @dataProvider provideSubDirURLs
  256. */
  257. function testMakeAbsoluteURLSubDir($url, $expectedResult) {
  258. \OC::$WEBROOT = '/owncloud';
  259. $result = \OC_Helper::makeURLAbsolute($url);
  260. $this->assertEquals($expectedResult, $result);
  261. }
  262. public function provideDocRootURLs() {
  263. return array(
  264. array('index.php', 'http://localhost/index.php'),
  265. array('/index.php', 'http://localhost/index.php'),
  266. array('/apps/index.php', 'http://localhost/apps/index.php'),
  267. array('apps/index.php', 'http://localhost/apps/index.php'),
  268. );
  269. }
  270. public function provideSubDirURLs() {
  271. return array(
  272. array('index.php', 'http://localhost/owncloud/index.php'),
  273. array('/index.php', 'http://localhost/owncloud/index.php'),
  274. array('/apps/index.php', 'http://localhost/owncloud/apps/index.php'),
  275. array('apps/index.php', 'http://localhost/owncloud/apps/index.php'),
  276. );
  277. }
  278. /**
  279. * @small
  280. * test linkTo URL construction
  281. * @dataProvider provideDocRootAppUrlParts
  282. */
  283. public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
  284. \OC::$WEBROOT = '';
  285. $result = \OC_Helper::linkTo($app, $file, $args);
  286. $this->assertEquals($expectedResult, $result);
  287. }
  288. /**
  289. * @small
  290. * test linkTo URL construction in sub directory
  291. * @dataProvider provideSubDirAppUrlParts
  292. */
  293. public function testLinkToSubDir($app, $file, $args, $expectedResult) {
  294. \OC::$WEBROOT = '/owncloud';
  295. $result = \OC_Helper::linkTo($app, $file, $args);
  296. $this->assertEquals($expectedResult, $result);
  297. }
  298. public function provideDocRootAppUrlParts() {
  299. return array(
  300. array('files', 'index.php', array(), '/index.php/apps/files'),
  301. array('files', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php/apps/files?trut=trat&dut=dat'),
  302. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php?trut=trat&dut=dat'),
  303. );
  304. }
  305. public function provideSubDirAppUrlParts() {
  306. return array(
  307. array('files', 'index.php', array(), '/owncloud/index.php/apps/files'),
  308. array('files', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php/apps/files?trut=trat&dut=dat'),
  309. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php?trut=trat&dut=dat'),
  310. );
  311. }
  312. /**
  313. * @small
  314. * test linkToAbsolute URL construction
  315. * @dataProvider provideDocRootAppAbsoluteUrlParts
  316. */
  317. public function testLinkToAbsoluteDocRoot($app, $file, $args, $expectedResult) {
  318. \OC::$WEBROOT = '';
  319. $result = \OC_Helper::linkToAbsolute($app, $file, $args);
  320. $this->assertEquals($expectedResult, $result);
  321. }
  322. /**
  323. * @small
  324. * test linkToAbsolute URL construction in sub directory
  325. * @dataProvider provideSubDirAppAbsoluteUrlParts
  326. */
  327. public function testLinkToAbsoluteSubDir($app, $file, $args, $expectedResult) {
  328. \OC::$WEBROOT = '/owncloud';
  329. $result = \OC_Helper::linkToAbsolute($app, $file, $args);
  330. $this->assertEquals($expectedResult, $result);
  331. }
  332. public function provideDocRootAppAbsoluteUrlParts() {
  333. return array(
  334. array('files', 'index.php', array(), 'http://localhost/index.php/apps/files'),
  335. array('files', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), 'http://localhost/index.php/apps/files?trut=trat&dut=dat'),
  336. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), 'http://localhost/index.php?trut=trat&dut=dat'),
  337. );
  338. }
  339. public function provideSubDirAppAbsoluteUrlParts() {
  340. return array(
  341. array('files', 'index.php', array(), 'http://localhost/owncloud/index.php/apps/files'),
  342. array('files', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), 'http://localhost/owncloud/index.php/apps/files?trut=trat&dut=dat'),
  343. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), 'http://localhost/owncloud/index.php?trut=trat&dut=dat'),
  344. );
  345. }
  346. /**
  347. * @small
  348. * test linkToRemoteBase URL construction
  349. */
  350. public function testLinkToRemoteBase() {
  351. \OC::$WEBROOT = '';
  352. $result = \OC_Helper::linkToRemoteBase('webdav');
  353. $this->assertEquals('/remote.php/webdav', $result);
  354. \OC::$WEBROOT = '/owncloud';
  355. $result = \OC_Helper::linkToRemoteBase('webdav');
  356. $this->assertEquals('/owncloud/remote.php/webdav', $result);
  357. }
  358. /**
  359. * @small
  360. * test linkToRemote URL construction
  361. */
  362. public function testLinkToRemote() {
  363. \OC::$WEBROOT = '';
  364. $result = \OC_Helper::linkToRemote('webdav');
  365. $this->assertEquals('http://localhost/remote.php/webdav/', $result);
  366. $result = \OC_Helper::linkToRemote('webdav', false);
  367. $this->assertEquals('http://localhost/remote.php/webdav', $result);
  368. \OC::$WEBROOT = '/owncloud';
  369. $result = \OC_Helper::linkToRemote('webdav');
  370. $this->assertEquals('http://localhost/owncloud/remote.php/webdav/', $result);
  371. $result = \OC_Helper::linkToRemote('webdav', false);
  372. $this->assertEquals('http://localhost/owncloud/remote.php/webdav', $result);
  373. }
  374. /**
  375. * @small
  376. * test linkToPublic URL construction
  377. */
  378. public function testLinkToPublic() {
  379. \OC::$WEBROOT = '';
  380. $result = \OC_Helper::linkToPublic('files');
  381. $this->assertEquals('http://localhost/s', $result);
  382. $result = \OC_Helper::linkToPublic('files', false);
  383. $this->assertEquals('http://localhost/s', $result);
  384. $result = \OC_Helper::linkToPublic('files', true);
  385. $this->assertEquals('http://localhost/s/', $result);
  386. $result = \OC_Helper::linkToPublic('other');
  387. $this->assertEquals('http://localhost/public.php?service=other', $result);
  388. $result = \OC_Helper::linkToPublic('other', false);
  389. $this->assertEquals('http://localhost/public.php?service=other', $result);
  390. $result = \OC_Helper::linkToPublic('other', true);
  391. $this->assertEquals('http://localhost/public.php?service=other/', $result);
  392. \OC::$WEBROOT = '/owncloud';
  393. $result = \OC_Helper::linkToPublic('files');
  394. $this->assertEquals('http://localhost/owncloud/s', $result);
  395. $result = \OC_Helper::linkToPublic('files', false);
  396. $this->assertEquals('http://localhost/owncloud/s', $result);
  397. $result = \OC_Helper::linkToPublic('files', true);
  398. $this->assertEquals('http://localhost/owncloud/s/', $result);
  399. $result = \OC_Helper::linkToPublic('other');
  400. $this->assertEquals('http://localhost/owncloud/public.php?service=other', $result);
  401. $result = \OC_Helper::linkToPublic('other', false);
  402. $this->assertEquals('http://localhost/owncloud/public.php?service=other', $result);
  403. $result = \OC_Helper::linkToPublic('other', true);
  404. $this->assertEquals('http://localhost/owncloud/public.php?service=other/', $result);
  405. }
  406. /**
  407. * Tests recursive folder deletion with rmdirr()
  408. */
  409. public function testRecursiveFolderDeletion() {
  410. $baseDir = \OC_Helper::tmpFolder() . '/';
  411. mkdir($baseDir . 'a/b/c/d/e', 0777, true);
  412. mkdir($baseDir . 'a/b/c1/d/e', 0777, true);
  413. mkdir($baseDir . 'a/b/c2/d/e', 0777, true);
  414. mkdir($baseDir . 'a/b1/c1/d/e', 0777, true);
  415. mkdir($baseDir . 'a/b2/c1/d/e', 0777, true);
  416. mkdir($baseDir . 'a/b3/c1/d/e', 0777, true);
  417. mkdir($baseDir . 'a1/b', 0777, true);
  418. mkdir($baseDir . 'a1/c', 0777, true);
  419. file_put_contents($baseDir . 'a/test.txt', 'Hello file!');
  420. file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!');
  421. file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!');
  422. \OC_Helper::rmdirr($baseDir . 'a');
  423. $this->assertFalse(file_exists($baseDir . 'a'));
  424. $this->assertTrue(file_exists($baseDir . 'a1'));
  425. \OC_Helper::rmdirr($baseDir);
  426. $this->assertFalse(file_exists($baseDir));
  427. }
  428. /**
  429. * Allows us to test private methods/properties
  430. *
  431. * @param $object
  432. * @param $methodName
  433. * @param array $parameters
  434. * @return mixed
  435. */
  436. public static function invokePrivate($object, $methodName, array $parameters = array()) {
  437. $reflection = new ReflectionClass(get_class($object));
  438. if ($reflection->hasMethod($methodName)) {
  439. $method = $reflection->getMethod($methodName);
  440. $method->setAccessible(true);
  441. return $method->invokeArgs($object, $parameters);
  442. } elseif ($reflection->hasProperty($methodName)) {
  443. $property = $reflection->getProperty($methodName);
  444. $property->setAccessible(true);
  445. return $property->getValue($object);
  446. }
  447. return false;
  448. }
  449. }