LegacyHelperTest.php 9.8 KB

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