helper.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. function testHumanFileSize() {
  10. $result = OC_Helper::humanFileSize(0);
  11. $expected = '0 B';
  12. $this->assertEquals($result, $expected);
  13. $result = OC_Helper::humanFileSize(1024);
  14. $expected = '1 kB';
  15. $this->assertEquals($result, $expected);
  16. $result = OC_Helper::humanFileSize(10000000);
  17. $expected = '9.5 MB';
  18. $this->assertEquals($result, $expected);
  19. $result = OC_Helper::humanFileSize(500000000000);
  20. $expected = '465.7 GB';
  21. $this->assertEquals($result, $expected);
  22. }
  23. function testComputerFileSize() {
  24. $result = OC_Helper::computerFileSize("0 B");
  25. $expected = '0.0';
  26. $this->assertEquals($result, $expected);
  27. $result = OC_Helper::computerFileSize("1 kB");
  28. $expected = '1024.0';
  29. $this->assertEquals($result, $expected);
  30. $result = OC_Helper::computerFileSize("9.5 MB");
  31. $expected = '9961472.0';
  32. $this->assertEquals($result, $expected);
  33. $result = OC_Helper::computerFileSize("465.7 GB");
  34. $expected = '500041567436.8';
  35. $this->assertEquals($result, $expected);
  36. }
  37. function testGetMimeType() {
  38. $dir=OC::$SERVERROOT.'/tests/data';
  39. $result = OC_Helper::getMimeType($dir."/");
  40. $expected = 'httpd/unix-directory';
  41. $this->assertEquals($result, $expected);
  42. $result = OC_Helper::getMimeType($dir."/data.tar.gz");
  43. $expected = 'application/x-gzip';
  44. $this->assertEquals($result, $expected);
  45. $result = OC_Helper::getMimeType($dir."/data.zip");
  46. $expected = 'application/zip';
  47. $this->assertEquals($result, $expected);
  48. $result = OC_Helper::getMimeType($dir."/logo-wide.svg");
  49. $expected = 'image/svg+xml';
  50. $this->assertEquals($result, $expected);
  51. $result = OC_Helper::getMimeType($dir."/logo-wide.png");
  52. $expected = 'image/png';
  53. $this->assertEquals($result, $expected);
  54. }
  55. function testGetFileNameMimeType() {
  56. $this->assertEquals('text/plain', OC_Helper::getFileNameMimeType('foo.txt'));
  57. $this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.png'));
  58. $this->assertEquals('image/png', OC_Helper::getFileNameMimeType('foo.bar.png'));
  59. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('.png'));
  60. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType('foo'));
  61. $this->assertEquals('application/octet-stream', OC_Helper::getFileNameMimeType(''));
  62. }
  63. function testGetStringMimeType() {
  64. $result = OC_Helper::getStringMimeType("/data/data.tar.gz");
  65. $expected = 'text/plain; charset=us-ascii';
  66. $this->assertEquals($result, $expected);
  67. }
  68. function testIssubdirectory() {
  69. $result = OC_Helper::issubdirectory("./data/", "/anotherDirectory/");
  70. $this->assertFalse($result);
  71. $result = OC_Helper::issubdirectory("./data/", "./data/");
  72. $this->assertTrue($result);
  73. mkdir("data/TestSubdirectory", 0777);
  74. $result = OC_Helper::issubdirectory("data/TestSubdirectory/", "data");
  75. rmdir("data/TestSubdirectory");
  76. $this->assertTrue($result);
  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 testMb_substr_replace() {
  103. $result = OC_Helper::mb_substr_replace("This is a teststring", "string", 5);
  104. $expected = "This string is a teststring";
  105. $this->assertEquals($result, $expected);
  106. }
  107. function testMb_str_replace() {
  108. $result = OC_Helper::mb_str_replace("teststring", "string", "This is a teststring");
  109. $expected = "This is a string";
  110. $this->assertEquals($result, $expected);
  111. }
  112. function testRecursiveArraySearch() {
  113. $haystack = array(
  114. "Foo" => "own",
  115. "Bar" => "Cloud",
  116. );
  117. $result = OC_Helper::recursiveArraySearch($haystack, "own");
  118. $expected = "Foo";
  119. $this->assertEquals($result, $expected);
  120. $result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
  121. $this->assertFalse($result);
  122. }
  123. function testBuildNotExistingFileNameForView() {
  124. $viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false);
  125. $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock));
  126. $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  127. $viewMock->expects($this->at(0))
  128. ->method('file_exists')
  129. ->will($this->returnValue(true)); // filename.ext exists
  130. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  131. $viewMock->expects($this->at(0))
  132. ->method('file_exists')
  133. ->will($this->returnValue(true)); // filename.ext exists
  134. $viewMock->expects($this->at(1))
  135. ->method('file_exists')
  136. ->will($this->returnValue(true)); // filename (2).ext exists
  137. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
  138. $viewMock->expects($this->at(0))
  139. ->method('file_exists')
  140. ->will($this->returnValue(true)); // filename (1).ext exists
  141. $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
  142. $viewMock->expects($this->at(0))
  143. ->method('file_exists')
  144. ->will($this->returnValue(true)); // filename (2).ext exists
  145. $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  146. $viewMock->expects($this->at(0))
  147. ->method('file_exists')
  148. ->will($this->returnValue(true)); // filename (2).ext exists
  149. $viewMock->expects($this->at(1))
  150. ->method('file_exists')
  151. ->will($this->returnValue(true)); // filename (3).ext exists
  152. $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
  153. $viewMock->expects($this->at(0))
  154. ->method('file_exists')
  155. ->will($this->returnValue(true)); // filename(1).ext exists
  156. $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
  157. $viewMock->expects($this->at(0))
  158. ->method('file_exists')
  159. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  160. $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  161. $viewMock->expects($this->at(0))
  162. ->method('file_exists')
  163. ->will($this->returnValue(true)); // filename(1) (1).ext exists
  164. $viewMock->expects($this->at(1))
  165. ->method('file_exists')
  166. ->will($this->returnValue(true)); // filename(1) (2).ext exists
  167. $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
  168. $viewMock->expects($this->at(0))
  169. ->method('file_exists')
  170. ->will($this->returnValue(true)); // filename(1) (2) (3).ext exists
  171. $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
  172. }
  173. }