helper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 UnitTestCase {
  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 testGetStringMimeType() {
  56. $result = OC_Helper::getStringMimeType("/data/data.tar.gz");
  57. $expected = 'text/plain; charset=us-ascii';
  58. $this->assertEquals($result, $expected);
  59. }
  60. function testIssubdirectory() {
  61. $result = OC_Helper::issubdirectory("./data/", "/anotherDirectory/");
  62. $this->assertFalse($result);
  63. $result = OC_Helper::issubdirectory("./data/", "./data/");
  64. $this->assertTrue($result);
  65. mkdir("data/TestSubdirectory", 0777);
  66. $result = OC_Helper::issubdirectory("data/TestSubdirectory/", "data");
  67. rmdir("data/TestSubdirectory");
  68. $this->assertTrue($result);
  69. }
  70. function testMb_array_change_key_case() {
  71. $arrayStart = array(
  72. "Foo" => "bar",
  73. "Bar" => "foo",
  74. );
  75. $arrayResult = array(
  76. "foo" => "bar",
  77. "bar" => "foo",
  78. );
  79. $result = OC_Helper::mb_array_change_key_case($arrayStart);
  80. $expected = $arrayResult;
  81. $this->assertEquals($result, $expected);
  82. $arrayStart = array(
  83. "foo" => "bar",
  84. "bar" => "foo",
  85. );
  86. $arrayResult = array(
  87. "FOO" => "bar",
  88. "BAR" => "foo",
  89. );
  90. $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER);
  91. $expected = $arrayResult;
  92. $this->assertEquals($result, $expected);
  93. }
  94. function testMb_substr_replace() {
  95. $result = OC_Helper::mb_substr_replace("This is a teststring", "string", 5);
  96. $expected = "This string is a teststring";
  97. $this->assertEquals($result, $expected);
  98. }
  99. function testMb_str_replace() {
  100. $result = OC_Helper::mb_str_replace("teststring", "string", "This is a teststring");
  101. $expected = "This is a string";
  102. $this->assertEquals($result, $expected);
  103. }
  104. function testRecursiveArraySearch() {
  105. $haystack = array(
  106. "Foo" => "own",
  107. "Bar" => "Cloud",
  108. );
  109. $result = OC_Helper::recursiveArraySearch($haystack, "own");
  110. $expected = "Foo";
  111. $this->assertEquals($result, $expected);
  112. $result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
  113. $this->assertFalse($result);
  114. }
  115. }