You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyHelperTest.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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->expects($this->at(0))
  96. ->method('file_exists')
  97. ->willReturn(true); // filename.ext exists
  98. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  99. $viewMock->expects($this->at(0))
  100. ->method('file_exists')
  101. ->willReturn(true); // filename.ext exists
  102. $viewMock->expects($this->at(1))
  103. ->method('file_exists')
  104. ->willReturn(true); // filename (2).ext exists
  105. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  106. $viewMock->expects($this->at(0))
  107. ->method('file_exists')
  108. ->willReturn(true); // filename (1).ext exists
  109. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
  110. $viewMock->expects($this->at(0))
  111. ->method('file_exists')
  112. ->willReturn(true); // filename (2).ext exists
  113. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  114. $viewMock->expects($this->at(0))
  115. ->method('file_exists')
  116. ->willReturn(true); // filename (2).ext exists
  117. $viewMock->expects($this->at(1))
  118. ->method('file_exists')
  119. ->willReturn(true); // filename (3).ext exists
  120. $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  121. $viewMock->expects($this->at(0))
  122. ->method('file_exists')
  123. ->willReturn(true); // filename(1).ext exists
  124. $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
  125. $viewMock->expects($this->at(0))
  126. ->method('file_exists')
  127. ->willReturn(true); // filename(1) (1).ext exists
  128. $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  129. $viewMock->expects($this->at(0))
  130. ->method('file_exists')
  131. ->willReturn(true); // filename(1) (1).ext exists
  132. $viewMock->expects($this->at(1))
  133. ->method('file_exists')
  134. ->willReturn(true); // filename(1) (2).ext exists
  135. $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  136. $viewMock->expects($this->at(0))
  137. ->method('file_exists')
  138. ->willReturn(true); // filename(1) (2) (3).ext exists
  139. $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
  140. }
  141. /**
  142. * @dataProvider streamCopyDataProvider
  143. */
  144. public function testStreamCopy($expectedCount, $expectedResult, $source, $target) {
  145. if (is_string($source)) {
  146. $source = fopen($source, 'r');
  147. }
  148. if (is_string($target)) {
  149. $target = fopen($target, 'w');
  150. }
  151. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  152. if (is_resource($source)) {
  153. fclose($source);
  154. }
  155. if (is_resource($target)) {
  156. fclose($target);
  157. }
  158. $this->assertSame($expectedCount, $count);
  159. $this->assertSame($expectedResult, $result);
  160. }
  161. public function streamCopyDataProvider() {
  162. return [
  163. [0, false, false, false],
  164. [0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false],
  165. [filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'],
  166. [3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'],
  167. ];
  168. }
  169. /**
  170. * Tests recursive folder deletion with rmdirr()
  171. */
  172. public function testRecursiveFolderDeletion() {
  173. $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/';
  174. mkdir($baseDir . 'a/b/c/d/e', 0777, true);
  175. mkdir($baseDir . 'a/b/c1/d/e', 0777, true);
  176. mkdir($baseDir . 'a/b/c2/d/e', 0777, true);
  177. mkdir($baseDir . 'a/b1/c1/d/e', 0777, true);
  178. mkdir($baseDir . 'a/b2/c1/d/e', 0777, true);
  179. mkdir($baseDir . 'a/b3/c1/d/e', 0777, true);
  180. mkdir($baseDir . 'a1/b', 0777, true);
  181. mkdir($baseDir . 'a1/c', 0777, true);
  182. file_put_contents($baseDir . 'a/test.txt', 'Hello file!');
  183. file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!');
  184. file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!');
  185. \OC_Helper::rmdirr($baseDir . 'a');
  186. $this->assertFalse(file_exists($baseDir . 'a'));
  187. $this->assertTrue(file_exists($baseDir . 'a1'));
  188. \OC_Helper::rmdirr($baseDir);
  189. $this->assertFalse(file_exists($baseDir));
  190. }
  191. /**
  192. * Allows us to test private methods/properties
  193. *
  194. * @param $object
  195. * @param $methodName
  196. * @param array $parameters
  197. * @return mixed
  198. * @deprecated Please extend \Test\TestCase and use self::invokePrivate() then
  199. */
  200. public static function invokePrivate($object, $methodName, array $parameters = []) {
  201. return parent::invokePrivate($object, $methodName, $parameters);
  202. }
  203. }