helper.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. class Test_Helper extends PHPUnit_Framework_TestCase {
  9. /**
  10. * @dataProvider humanFileSizeProvider
  11. */
  12. public function testHumanFileSize($expected, $input)
  13. {
  14. $result = OC_Helper::humanFileSize($input);
  15. $this->assertEquals($expected, $result);
  16. }
  17. public function humanFileSizeProvider()
  18. {
  19. return array(
  20. array('0 B', 0),
  21. array('1 kB', 1024),
  22. array('9.5 MB', 10000000),
  23. array('465.7 GB', 500000000000),
  24. array('454.7 TB', 500000000000000),
  25. array('444.1 PB', 500000000000000000),
  26. );
  27. }
  28. /**
  29. * @dataProvider computerFileSizeProvider
  30. */
  31. function testComputerFileSize($expected, $input) {
  32. $result = OC_Helper::computerFileSize($input);
  33. $this->assertEquals($expected, $result);
  34. }
  35. function computerFileSizeProvider(){
  36. return array(
  37. array(0.0, "0 B"),
  38. array(1024.0, "1 kB"),
  39. array(9961472.0, "9.5 MB"),
  40. array(500041567436.8, "465.7 GB"),
  41. );
  42. }
  43. function testGetMimeType() {
  44. $dir=OC::$SERVERROOT.'/tests/data';
  45. $result = OC_Helper::getMimeType($dir."/");
  46. $expected = 'httpd/unix-directory';
  47. $this->assertEquals($result, $expected);
  48. $result = OC_Helper::getMimeType($dir."/data.tar.gz");
  49. $expected = 'application/x-gzip';
  50. $this->assertEquals($result, $expected);
  51. $result = OC_Helper::getMimeType($dir."/data.zip");
  52. $expected = 'application/zip';
  53. $this->assertEquals($result, $expected);
  54. $result = OC_Helper::getMimeType($dir."/logo-wide.svg");
  55. $expected = 'image/svg+xml';
  56. $this->assertEquals($result, $expected);
  57. $result = OC_Helper::getMimeType($dir."/logo-wide.png");
  58. $expected = 'image/png';
  59. $this->assertEquals($result, $expected);
  60. }
  61. function testGetFileNameMimeType() {
  62. $this->assertEquals('text/plain', OC_Helper::getFileNameMimeType('foo.txt'));
  63. $this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.png'));
  64. $this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.bar.png'));
  65. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('.png'));
  66. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('foo'));
  67. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType(''));
  68. }
  69. function testGetStringMimeType() {
  70. $result = OC_Helper::getStringMimeType("/data/data.tar.gz");
  71. $expected = 'text/plain; charset=us-ascii';
  72. $this->assertEquals($result, $expected);
  73. }
  74. function testIssubdirectory() {
  75. $result = OC_Helper::issubdirectory("./data/", "/anotherDirectory/");
  76. $this->assertFalse($result);
  77. $result = OC_Helper::issubdirectory("./data/", "./data/");
  78. $this->assertTrue($result);
  79. mkdir("data/TestSubdirectory", 0777);
  80. $result = OC_Helper::issubdirectory("data/TestSubdirectory/", "data");
  81. rmdir("data/TestSubdirectory");
  82. $this->assertTrue($result);
  83. }
  84. function testMb_array_change_key_case() {
  85. $arrayStart = array(
  86. "Foo" => "bar",
  87. "Bar" => "foo",
  88. );
  89. $arrayResult = array(
  90. "foo" => "bar",
  91. "bar" => "foo",
  92. );
  93. $result = OC_Helper::mb_array_change_key_case($arrayStart);
  94. $expected = $arrayResult;
  95. $this->assertEquals($result, $expected);
  96. $arrayStart = array(
  97. "foo" => "bar",
  98. "bar" => "foo",
  99. );
  100. $arrayResult = array(
  101. "FOO" => "bar",
  102. "BAR" => "foo",
  103. );
  104. $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER);
  105. $expected = $arrayResult;
  106. $this->assertEquals($result, $expected);
  107. }
  108. function testMb_substr_replace() {
  109. $result = OC_Helper::mb_substr_replace("This is a teststring", "string", 5);
  110. $expected = "This string is a teststring";
  111. $this->assertEquals($result, $expected);
  112. }
  113. function testMb_str_replace() {
  114. $result = OC_Helper::mb_str_replace("teststring", "string", "This is a teststring");
  115. $expected = "This is a string";
  116. $this->assertEquals($result, $expected);
  117. }
  118. function testRecursiveArraySearch() {
  119. $haystack = array(
  120. "Foo" => "own",
  121. "Bar" => "Cloud",
  122. );
  123. $result = OC_Helper::recursiveArraySearch($haystack, "own");
  124. $expected = "Foo";
  125. $this->assertEquals($result, $expected);
  126. $result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
  127. $this->assertFalse($result);
  128. }
  129. function testBuildNotExistingFileNameForView() {
  130. $viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false);
  131. $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock));
  132. $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  133. $viewMock->expects($this->at(0))
  134. ->method('file_exists')
  135. ->will($this->returnValue(true)); // filename.ext exists
  136. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  137. $viewMock->expects($this->at(0))
  138. ->method('file_exists')
  139. ->will($this->returnValue(true)); // filename.ext exists
  140. $viewMock->expects($this->at(1))
  141. ->method('file_exists')
  142. ->will($this->returnValue(true)); // filename (2).ext exists
  143. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  144. $viewMock->expects($this->at(0))
  145. ->method('file_exists')
  146. ->will($this->returnValue(true)); // filename (1).ext exists
  147. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
  148. $viewMock->expects($this->at(0))
  149. ->method('file_exists')
  150. ->will($this->returnValue(true)); // filename (2).ext exists
  151. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  152. $viewMock->expects($this->at(0))
  153. ->method('file_exists')
  154. ->will($this->returnValue(true)); // filename (2).ext exists
  155. $viewMock->expects($this->at(1))
  156. ->method('file_exists')
  157. ->will($this->returnValue(true)); // filename (3).ext exists
  158. $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  159. $viewMock->expects($this->at(0))
  160. ->method('file_exists')
  161. ->will($this->returnValue(true)); // filename(1).ext exists
  162. $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
  163. $viewMock->expects($this->at(0))
  164. ->method('file_exists')
  165. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  166. $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  167. $viewMock->expects($this->at(0))
  168. ->method('file_exists')
  169. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  170. $viewMock->expects($this->at(1))
  171. ->method('file_exists')
  172. ->will($this->returnValue(true)); // filename(1) (2).ext exists
  173. $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  174. $viewMock->expects($this->at(0))
  175. ->method('file_exists')
  176. ->will($this->returnValue(true)); // filename(1) (2) (3).ext exists
  177. $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
  178. }
  179. /**
  180. * @dataProvider streamCopyDataProvider
  181. */
  182. public function testStreamCopy($expectedCount, $expectedResult, $source, $target) {
  183. if (is_string($source)) {
  184. $source = fopen($source, 'r');
  185. }
  186. if (is_string($target)) {
  187. $target = fopen($target, 'w');
  188. }
  189. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  190. if (is_resource($source)) {
  191. fclose($source);
  192. }
  193. if (is_resource($target)) {
  194. fclose($target);
  195. }
  196. $this->assertSame($expectedCount, $count);
  197. $this->assertSame($expectedResult, $result);
  198. }
  199. function streamCopyDataProvider() {
  200. return array(
  201. array(0, false, false, false),
  202. array(0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false),
  203. array(filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'),
  204. array(3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'),
  205. );
  206. }
  207. }