LegacyHelperTest.php 8.0 KB

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