LegacyHelperTest.php 8.5 KB

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