LegacyHelperTest.php 7.6 KB

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