1
0

LegacyHelperTest.php 8.4 KB

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