PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/unitTesting/test.php

https://github.com/mtwstudios/comtor
PHP | 165 lines | 120 code | 30 blank | 15 comment | 12 complexity | 63ca31bb84ce56760fb3fdfa7d5ba932 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once('simpletest/autorun.php');
  3. // Add comtor web folder to include path
  4. $path = "/home/sigwart4/comtor/web";
  5. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  6. session_start();
  7. require_once("smarty/Smarty.class.php");
  8. class TestOfDatabaseConnecting extends UnitTestCase
  9. {
  10. function testConnection()
  11. {
  12. require("connect.php");
  13. $this->assertNotEqual(mysql_stat(), null, "Failed to connect to database");
  14. }
  15. }
  16. class TestOfLinksClass extends UnitTestCase
  17. {
  18. function testLinkClass()
  19. {
  20. $tpl = new Smarty();
  21. require_once("header1.php");
  22. // Heading
  23. $header = new Link("COMTOR Account", null, true);
  24. $this->assertIdentical($header->name, "COMTOR Account", "Header link name incorrect");
  25. $this->assertIdentical($header->href, null, "Header link href should be null");
  26. $this->assertIdentical($header->heading, true, "Header link not classified as heading");
  27. $this->assertTrue(count($header->attrs) == 0, "Header link should not have attributes");
  28. // Normal link
  29. $link = new Link("Create Account", "test.php");
  30. $this->assertIdentical($link->name, "Create Account", "Link name incorrect");
  31. $this->assertEqual($link->href, "test.php", "Link href incorrect");
  32. $this->assertIdentical($link->heading, false, "Link incorrectly classified as heading");
  33. // Link with attributes
  34. $link->addAttr("class", "test");
  35. $link->addAttr("id", "testId");
  36. $this->assertEqual($link->attrs[0]->attr, "class", "Link attribute incorrect");
  37. $this->assertEqual($link->attrs[0]->value, "test", "Link attribute incorrect");
  38. $this->assertEqual($link->attrs[1]->attr, "id", "Link attribute incorrect");
  39. $this->assertEqual($link->attrs[1]->value, "testId", "Link attribute incorrect");
  40. }
  41. }
  42. class TestOfSecurityFunctions extends UnitTestCase
  43. {
  44. function testSecurity()
  45. {
  46. require_once("securityFunctions.php");
  47. // Check that security is not passed
  48. $this->assertTrue(checkSecurity("abcdefghijklmn", "abcdefghijklmn") != 1, "Security function failed");
  49. // Get page security info
  50. $arr = securityFormInputs();
  51. $this->assertTrue(checkSecurity($arr['page_sec'], "abcdefghijklmn") != 1, "Security function failed");
  52. $this->assertTrue(checkSecurity($arr['page_sec'], $arr['rand']) != 1, "Security function failed");
  53. $arr = securityFormInputs();
  54. $this->assertTrue(checkSecurity("abcdefghijklmn", $arr['rand']) != 1, "Security function failed");
  55. $this->assertTrue(checkSecurity($arr['page_sec'], $arr['rand']) == 1, "Security function failed");
  56. // Get page security info
  57. $arr = securityFormInputs();
  58. $this->assertTrue(checkSecurity($arr['page_sec'], $arr['rand']) == 1, "Security function failed");
  59. // Get page security info
  60. $arr1 = securityFormInputs();
  61. $arr2 = securityFormInputs();
  62. $arr3 = securityFormInputs();
  63. $arr4 = securityFormInputs();
  64. $arr5 = securityFormInputs();
  65. $this->assertTrue(checkSecurity($arr1['page_sec'], $arr1['rand']) == 1, "Security function failed");
  66. $this->assertTrue(checkSecurity($arr2['page_sec'], $arr2['rand']) == 1, "Security function failed");
  67. $this->assertTrue(checkSecurity($arr3['page_sec'], $arr3['rand']) == 1, "Security function failed");
  68. $this->assertTrue(checkSecurity($arr4['page_sec'], $arr4['rand']) == 1, "Security function failed");
  69. $this->assertTrue(checkSecurity($arr5['page_sec'], $arr5['rand']) == 1, "Security function failed");
  70. }
  71. }
  72. class TestOfRunPhp extends UnitTestCase
  73. {
  74. function testFileExtension()
  75. {
  76. $filename = "test.jar";
  77. $this->assertEqual(file_extension($filename), "jar", "File extension incorrect");
  78. $filename = "test.tar.gz";
  79. $this->assertEqual(file_extension($filename), "gz", "File extension incorrect");
  80. $filename = "README";
  81. $this->assertEqual(file_extension($filename), "README", "File extension incorrect");
  82. }
  83. }
  84. // From run.php
  85. function file_extension($filename)
  86. {
  87. return end(explode(".", $filename));
  88. }
  89. class TestOfReportsPhp extends UnitTestCase
  90. {
  91. function testFormatDateTime()
  92. {
  93. $timestamp = "2009/03/02 16:24:34";
  94. $str = formatDateTime($timestamp);
  95. $this->assertEqual($str, "Monday March 2, 2009 @ 4:24:34 PM", "Date formatted incorrectly");
  96. }
  97. function testColorize()
  98. {
  99. $str = "// Test\nTest/*ing*/\nHi";
  100. $str = htmlspecialchars($str);
  101. $str = str_replace(" ", "&nbsp;", $str);
  102. $str = nl2br($str);
  103. $str = colorize($str);
  104. // Expected result
  105. $testStr = "<span style=\"color: #008000;\">//&nbsp;Test<br /></span>\nTest<span style=\"color: #008000;\">/*ing*/</span><br />\nHi";
  106. $this->assertEqual($str, $testStr, "Code colorized incorrectly");
  107. }
  108. }
  109. // Format date from reports.php
  110. function formatDateTime($timestamp)
  111. {
  112. $year=substr($timestamp,0,4);
  113. $month=substr($timestamp,5,2);
  114. $day=substr($timestamp,8,2);
  115. $hour=substr($timestamp,11,2);
  116. $minute=substr($timestamp,14,2);
  117. $second=substr($timestamp,17,2);
  118. //$date = date("M d, Y @ g:i:s A", mktime($hour, $minute, $second, $month, $day, $year));
  119. $date = date("l F j, Y @ g:i:s A", mktime($hour, $minute, $second, $month, $day, $year));
  120. return $date;
  121. }
  122. // Colorize from reports.php
  123. function colorize($str)
  124. {
  125. // Make comments green
  126. $pattern = '/(\/\*.*\*\/)/msU';
  127. $replacement = '<span style="color: #008000;">$1</span>';
  128. $str = preg_replace($pattern, $replacement, $str);
  129. // Make comments green
  130. $pattern = '/(\/\/.*<br ?\/?>)/i';
  131. $replacement = '<span style="color: #008000;">$1</span>';
  132. $str = preg_replace($pattern, $replacement, $str);
  133. return $str;
  134. }
  135. ?>