LegacyHelperTest.php 9.4 KB

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