util.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_Util extends PHPUnit_Framework_TestCase {
  9. public function testGetVersion() {
  10. $version = \OC_Util::getVersion();
  11. $this->assertTrue(is_array($version));
  12. foreach ($version as $num) {
  13. $this->assertTrue(is_int($num));
  14. }
  15. }
  16. public function testGetVersionString() {
  17. $version = \OC_Util::getVersionString();
  18. $this->assertTrue(is_string($version));
  19. }
  20. public function testGetEditionString() {
  21. $edition = \OC_Util::getEditionString();
  22. $this->assertTrue(is_string($edition));
  23. }
  24. function testFormatDate() {
  25. date_default_timezone_set("UTC");
  26. $result = OC_Util::formatDate(1350129205);
  27. $expected = 'October 13, 2012 11:53';
  28. $this->assertEquals($expected, $result);
  29. $result = OC_Util::formatDate(1102831200, true);
  30. $expected = 'December 12, 2004';
  31. $this->assertEquals($expected, $result);
  32. }
  33. function testCallRegister() {
  34. $result = strlen(OC_Util::callRegister());
  35. $this->assertEquals(20, $result);
  36. }
  37. function testSanitizeHTML() {
  38. $badString = "<script>alert('Hacked!');</script>";
  39. $result = OC_Util::sanitizeHTML($badString);
  40. $this->assertEquals("&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;", $result);
  41. $goodString = "This is an harmless string.";
  42. $result = OC_Util::sanitizeHTML($goodString);
  43. $this->assertEquals("This is an harmless string.", $result);
  44. }
  45. function testEncodePath(){
  46. $component = '/§#@test%&^ä/-child';
  47. $result = OC_Util::encodePath($component);
  48. $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
  49. }
  50. public function testFileInfoLoaded() {
  51. $expected = function_exists('finfo_open');
  52. $this->assertEquals($expected, \OC_Util::fileInfoLoaded());
  53. }
  54. public function testIsInternetConnectionEnabled() {
  55. \OC_Config::setValue("has_internet_connection", false);
  56. $this->assertFalse(\OC_Util::isInternetConnectionEnabled());
  57. \OC_Config::setValue("has_internet_connection", true);
  58. $this->assertTrue(\OC_Util::isInternetConnectionEnabled());
  59. }
  60. function testGenerateRandomBytes() {
  61. $result = strlen(OC_Util::generateRandomBytes(59));
  62. $this->assertEquals(59, $result);
  63. }
  64. function testGetDefaultEmailAddress() {
  65. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  66. $this->assertEquals('no-reply@localhost.localdomain', $email);
  67. }
  68. function testGetDefaultEmailAddressFromConfig() {
  69. OC_Config::setValue('mail_domain', 'example.com');
  70. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  71. $this->assertEquals('no-reply@example.com', $email);
  72. OC_Config::deleteKey('mail_domain');
  73. }
  74. function testGetInstanceIdGeneratesValidId() {
  75. OC_Config::deleteKey('instanceid');
  76. $this->assertStringStartsWith('oc', OC_Util::getInstanceId());
  77. }
  78. /**
  79. * @dataProvider baseNameProvider
  80. */
  81. public function testBaseName($expected, $file)
  82. {
  83. $base = \OC_Util::basename($file);
  84. $this->assertEquals($expected, $base);
  85. }
  86. public function baseNameProvider()
  87. {
  88. return array(
  89. array('public_html', '/home/user/public_html/'),
  90. array('public_html', '/home/user/public_html'),
  91. array('', '/'),
  92. array('public_html', 'public_html'),
  93. array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/')
  94. );
  95. }
  96. }