helper.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 providesComputerFileSize
  51. */
  52. function testComputerFileSize($expected, $input) {
  53. $result = OC_Helper::computerFileSize($input);
  54. $this->assertEquals($expected, $result);
  55. }
  56. function providesComputerFileSize(){
  57. return [
  58. [0.0, "0 B"],
  59. [1024.0, "1 KB"],
  60. [1395864371.0, '1.3 GB'],
  61. [9961472.0, "9.5 MB"],
  62. [500041567437.0, "465.7 GB"],
  63. [false, "12 GB etfrhzui"]
  64. ];
  65. }
  66. function testIsSubDirectory() {
  67. $result = OC_Helper::isSubDirectory("./data/", "/anotherDirectory/");
  68. $this->assertFalse($result);
  69. $result = OC_Helper::isSubDirectory("./data/", "./data/");
  70. $this->assertTrue($result);
  71. mkdir("data/TestSubdirectory", 0777);
  72. $result = OC_Helper::isSubDirectory("data/TestSubdirectory/", "data");
  73. rmdir("data/TestSubdirectory");
  74. $this->assertTrue($result);
  75. }
  76. function testMb_array_change_key_case() {
  77. $arrayStart = array(
  78. "Foo" => "bar",
  79. "Bar" => "foo",
  80. );
  81. $arrayResult = array(
  82. "foo" => "bar",
  83. "bar" => "foo",
  84. );
  85. $result = OC_Helper::mb_array_change_key_case($arrayStart);
  86. $expected = $arrayResult;
  87. $this->assertEquals($result, $expected);
  88. $arrayStart = array(
  89. "foo" => "bar",
  90. "bar" => "foo",
  91. );
  92. $arrayResult = array(
  93. "FOO" => "bar",
  94. "BAR" => "foo",
  95. );
  96. $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER);
  97. $expected = $arrayResult;
  98. $this->assertEquals($result, $expected);
  99. }
  100. function testRecursiveArraySearch() {
  101. $haystack = array(
  102. "Foo" => "own",
  103. "Bar" => "Cloud",
  104. );
  105. $result = OC_Helper::recursiveArraySearch($haystack, "own");
  106. $expected = "Foo";
  107. $this->assertEquals($result, $expected);
  108. $result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
  109. $this->assertFalse($result);
  110. }
  111. function testBuildNotExistingFileNameForView() {
  112. $viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false);
  113. $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock));
  114. $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  115. $viewMock->expects($this->at(0))
  116. ->method('file_exists')
  117. ->will($this->returnValue(true)); // filename.ext exists
  118. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  119. $viewMock->expects($this->at(0))
  120. ->method('file_exists')
  121. ->will($this->returnValue(true)); // filename.ext exists
  122. $viewMock->expects($this->at(1))
  123. ->method('file_exists')
  124. ->will($this->returnValue(true)); // filename (2).ext exists
  125. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  126. $viewMock->expects($this->at(0))
  127. ->method('file_exists')
  128. ->will($this->returnValue(true)); // filename (1).ext exists
  129. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
  130. $viewMock->expects($this->at(0))
  131. ->method('file_exists')
  132. ->will($this->returnValue(true)); // filename (2).ext exists
  133. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  134. $viewMock->expects($this->at(0))
  135. ->method('file_exists')
  136. ->will($this->returnValue(true)); // filename (2).ext exists
  137. $viewMock->expects($this->at(1))
  138. ->method('file_exists')
  139. ->will($this->returnValue(true)); // filename (3).ext exists
  140. $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  141. $viewMock->expects($this->at(0))
  142. ->method('file_exists')
  143. ->will($this->returnValue(true)); // filename(1).ext exists
  144. $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
  145. $viewMock->expects($this->at(0))
  146. ->method('file_exists')
  147. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  148. $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  149. $viewMock->expects($this->at(0))
  150. ->method('file_exists')
  151. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  152. $viewMock->expects($this->at(1))
  153. ->method('file_exists')
  154. ->will($this->returnValue(true)); // filename(1) (2).ext exists
  155. $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  156. $viewMock->expects($this->at(0))
  157. ->method('file_exists')
  158. ->will($this->returnValue(true)); // filename(1) (2) (3).ext exists
  159. $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
  160. }
  161. /**
  162. * @dataProvider streamCopyDataProvider
  163. */
  164. public function testStreamCopy($expectedCount, $expectedResult, $source, $target) {
  165. if (is_string($source)) {
  166. $source = fopen($source, 'r');
  167. }
  168. if (is_string($target)) {
  169. $target = fopen($target, 'w');
  170. }
  171. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  172. if (is_resource($source)) {
  173. fclose($source);
  174. }
  175. if (is_resource($target)) {
  176. fclose($target);
  177. }
  178. $this->assertSame($expectedCount, $count);
  179. $this->assertSame($expectedResult, $result);
  180. }
  181. function streamCopyDataProvider() {
  182. return array(
  183. array(0, false, false, false),
  184. array(0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false),
  185. array(filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'),
  186. array(3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'),
  187. );
  188. }
  189. // Url generator methods
  190. /**
  191. * @small
  192. * test linkToPublic URL construction
  193. */
  194. public function testLinkToPublic() {
  195. \OC::$WEBROOT = '';
  196. $result = \OC_Helper::linkToPublic('files');
  197. $this->assertEquals('http://localhost/s', $result);
  198. $result = \OC_Helper::linkToPublic('files', false);
  199. $this->assertEquals('http://localhost/s', $result);
  200. $result = \OC_Helper::linkToPublic('files', true);
  201. $this->assertEquals('http://localhost/s/', $result);
  202. $result = \OC_Helper::linkToPublic('other');
  203. $this->assertEquals('http://localhost/public.php?service=other', $result);
  204. $result = \OC_Helper::linkToPublic('other', false);
  205. $this->assertEquals('http://localhost/public.php?service=other', $result);
  206. $result = \OC_Helper::linkToPublic('other', true);
  207. $this->assertEquals('http://localhost/public.php?service=other/', $result);
  208. \OC::$WEBROOT = '/owncloud';
  209. $result = \OC_Helper::linkToPublic('files');
  210. $this->assertEquals('http://localhost/owncloud/s', $result);
  211. $result = \OC_Helper::linkToPublic('files', false);
  212. $this->assertEquals('http://localhost/owncloud/s', $result);
  213. $result = \OC_Helper::linkToPublic('files', true);
  214. $this->assertEquals('http://localhost/owncloud/s/', $result);
  215. $result = \OC_Helper::linkToPublic('other');
  216. $this->assertEquals('http://localhost/owncloud/public.php?service=other', $result);
  217. $result = \OC_Helper::linkToPublic('other', false);
  218. $this->assertEquals('http://localhost/owncloud/public.php?service=other', $result);
  219. $result = \OC_Helper::linkToPublic('other', true);
  220. $this->assertEquals('http://localhost/owncloud/public.php?service=other/', $result);
  221. }
  222. /**
  223. * Tests recursive folder deletion with rmdirr()
  224. */
  225. public function testRecursiveFolderDeletion() {
  226. $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/';
  227. mkdir($baseDir . 'a/b/c/d/e', 0777, true);
  228. mkdir($baseDir . 'a/b/c1/d/e', 0777, true);
  229. mkdir($baseDir . 'a/b/c2/d/e', 0777, true);
  230. mkdir($baseDir . 'a/b1/c1/d/e', 0777, true);
  231. mkdir($baseDir . 'a/b2/c1/d/e', 0777, true);
  232. mkdir($baseDir . 'a/b3/c1/d/e', 0777, true);
  233. mkdir($baseDir . 'a1/b', 0777, true);
  234. mkdir($baseDir . 'a1/c', 0777, true);
  235. file_put_contents($baseDir . 'a/test.txt', 'Hello file!');
  236. file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!');
  237. file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!');
  238. \OC_Helper::rmdirr($baseDir . 'a');
  239. $this->assertFalse(file_exists($baseDir . 'a'));
  240. $this->assertTrue(file_exists($baseDir . 'a1'));
  241. \OC_Helper::rmdirr($baseDir);
  242. $this->assertFalse(file_exists($baseDir));
  243. }
  244. /**
  245. * Allows us to test private methods/properties
  246. *
  247. * @param $object
  248. * @param $methodName
  249. * @param array $parameters
  250. * @return mixed
  251. * @deprecated Please extend \Test\TestCase and use self::invokePrivate() then
  252. */
  253. public static function invokePrivate($object, $methodName, array $parameters = array()) {
  254. return parent::invokePrivate($object, $methodName, $parameters);
  255. }
  256. }