UtilProcessTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. /**
  4. * Input typing, output escaping, and default password hashing.
  5. */
  6. class UtilProcessTest extends \PHPUnit\Framework\TestCase
  7. {
  8. #[DataProvider('providerForProcessInput')]
  9. public function testProcessInput($mixedIn, $iType, $mixedOut)
  10. {
  11. $this->assertSame($mixedOut, bx_process_input($mixedIn, $iType));
  12. }
  13. static public function providerForProcessInput()
  14. {
  15. return [
  16. ['42', BX_DATA_INT, 42],
  17. [' 42 ', BX_DATA_INT, 42],
  18. ['x', BX_DATA_INT, false],
  19. ['', BX_DATA_INT, false],
  20. ['3.14', BX_DATA_FLOAT, 3.14],
  21. [' 2.5 ', BX_DATA_FLOAT, 2.5],
  22. ['nope', BX_DATA_FLOAT, false],
  23. ['on', BX_DATA_CHECKBOX, 'on'],
  24. [' on ', BX_DATA_CHECKBOX, 'on'],
  25. ['off', BX_DATA_CHECKBOX, ''],
  26. ['1', BX_DATA_CHECKBOX, ''],
  27. ['1985-10-28', BX_DATA_DATE, '1985-10-28'],
  28. ['1985-1-2', BX_DATA_DATE, '1985-01-02'],
  29. ['not-a-date', BX_DATA_DATE, false],
  30. ['1985/10/28', BX_DATA_DATE, false],
  31. ['1985-10-28T00:59:35', BX_DATA_DATETIME, '1985-10-28 00:59:35'],
  32. ['1985-10-28 00:59:35', BX_DATA_DATETIME, '1985-10-28 00:59:35'],
  33. ['1985-10-28 00:59', BX_DATA_DATETIME, '1985-10-28 00:59:00'],
  34. ['1985-10-28', BX_DATA_DATETIME, '1985-10-28'],
  35. [['1', '2'], BX_DATA_INT, [1, 2]],
  36. ['plain', BX_DATA_TEXT, 'plain'],
  37. ];
  38. }
  39. #[DataProvider('providerForJsString')]
  40. public function testJsString($sIn, $iQuoteType, $sOut)
  41. {
  42. $this->assertSame($sOut, bx_js_string($sIn, $iQuoteType));
  43. }
  44. static public function providerForJsString()
  45. {
  46. return [
  47. ['say "hi"', BX_ESCAPE_STR_AUTO, 'say &quot;hi&quot;'],
  48. ["it's", BX_ESCAPE_STR_AUTO, 'it&apos;s'],
  49. ['<script>x</script>', BX_ESCAPE_STR_AUTO, '&lt;script&gt;x&lt;/script&gt;'],
  50. ["it's", BX_ESCAPE_STR_APOS, "it\\'s"],
  51. ['<script>x</script>', BX_ESCAPE_STR_APOS, "<scr' + 'ipt>x</scr' + 'ipt>"],
  52. ['say "hi"', BX_ESCAPE_STR_QUOTE, 'say \\"hi\\"'],
  53. ['<script>x</script>', BX_ESCAPE_STR_QUOTE, '<scr" + "ipt>x</scr" + "ipt>'],
  54. ["line\nbreak", BX_ESCAPE_STR_AUTO, 'line\\nbreak'],
  55. ];
  56. }
  57. #[DataProvider('providerForHtmlAttribute')]
  58. public function testHtmlAttribute($sIn, $iQuoteType, $sOut)
  59. {
  60. $this->assertSame($sOut, bx_html_attribute($sIn, $iQuoteType));
  61. }
  62. static public function providerForHtmlAttribute()
  63. {
  64. return [
  65. ['say "hi"', BX_ESCAPE_STR_AUTO, 'say &quot;hi&quot;'],
  66. ["it's", BX_ESCAPE_STR_AUTO, 'it&apos;s'],
  67. ["it's", BX_ESCAPE_STR_APOS, "it\\'s"],
  68. ['say "hi"', BX_ESCAPE_STR_QUOTE, 'say &quot;hi&quot;'],
  69. ["it's", BX_ESCAPE_STR_QUOTE, "it's"],
  70. ];
  71. }
  72. #[DataProvider('providerForHtmlspecialcharsAdv')]
  73. public function testHtmlspecialcharsAdv($sIn, $sOut)
  74. {
  75. $this->assertSame($sOut, htmlspecialchars_adv($sIn));
  76. }
  77. static public function providerForHtmlspecialcharsAdv()
  78. {
  79. return [
  80. ['a & b', 'a &amp; b'],
  81. ['say "hi"', 'say &quot;hi&quot;'],
  82. ["it's", "it's"],
  83. ['&amp;', '&amp;'],
  84. ];
  85. }
  86. public function testEncryptUserPwdDefaultAlgo()
  87. {
  88. $sAlgo = defined('BX_PWD_ALGO') ? BX_PWD_ALGO : '';
  89. if ($sAlgo && $sAlgo !== 'sha1_md5_salt')
  90. $this->markTestSkipped('BX_PWD_ALGO is not sha1_md5_salt');
  91. $sPwd = 'secret';
  92. $sSalt = 'pepper';
  93. $this->assertSame(sha1(md5($sPwd) . $sSalt), encryptUserPwd($sPwd, $sSalt));
  94. }
  95. }