UtilTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. /**
  4. * Test util functions
  5. */
  6. class UtilTest extends BxDolTestCase
  7. {
  8. /**
  9. * @see clear_xss
  10. */
  11. #[DataProvider('providerForClearXssAdmin')]
  12. public function testClearXssAdmin($sInput, $sOutput)
  13. {
  14. $this->assertEquals(clear_xss($sInput), $sOutput);
  15. // if user is admin then "purify" is never called
  16. // $this->_testClearXss(1, 'never');
  17. }
  18. /**
  19. * @see clear_xss
  20. */
  21. #[DataProvider('providerForClearXssNotAdmin')]
  22. public function testClearXssNotAdmin($sInput, $sOutput)
  23. {
  24. $this->assertEquals(clear_xss($sInput), $sOutput);
  25. // if user is not admin then "purify" is called once
  26. // $this->_testClearXss(0, 'once');
  27. }
  28. protected function _testClearXss($isAdmin, $sCalled)
  29. {
  30. // create mock object instance of HTMLPurifier class
  31. $GLOBALS['oHtmlPurifier'] = $this->createMock('HTMLPurifier');
  32. // set admin or not admin user
  33. $GLOBALS['logged']['admin'] = $isAdmin;
  34. // check if we have instance of correct class
  35. $this->assertInstanceOf('HTMLPurifier', $GLOBALS['oHtmlPurifier']);
  36. // we expect that 'purify' method should be called once(or never) when we call clear_xss function
  37. $GLOBALS['oHtmlPurifier']->expects($this->$sCalled())->method('purify');
  38. // call tested function
  39. clear_xss('test');
  40. }
  41. static public function providerForClearXssNotAdmin()
  42. {
  43. return array(
  44. array('test<script>alert(1);</script>', 'test'),
  45. array('test<style>div {border:3px solid red;}</style>', 'test'),
  46. );
  47. }
  48. static public function providerForClearXssAdmin()
  49. {
  50. // TODO: for admin all tags should be allowed including script and style
  51. return array(
  52. array('test<script>alert(1);</script>', 'test'),
  53. array('test<style>div {border:3px solid red;}</style>', 'test'),
  54. );
  55. }
  56. /**
  57. * @see return_bytes
  58. */
  59. #[DataProvider('providerForReturnBytes')]
  60. public function testReturnBytes($sInput, $sOutput)
  61. {
  62. $this->assertEquals(return_bytes($sInput), $sOutput);
  63. }
  64. static public function providerForReturnBytes()
  65. {
  66. return array(
  67. array('2k', 2048),
  68. array('2K', 2048),
  69. array('2 k', 2048),
  70. array('2 K', 2048),
  71. array('1m', 1048576),
  72. array('1g', 1073741824),
  73. );
  74. }
  75. /**
  76. * @see title2uri
  77. */
  78. #[DataProvider('providerForTitleToUri')]
  79. function testTitleToUri($sIn, $sOut)
  80. {
  81. $this->assertEquals(title2uri($sIn), $sOut);
  82. }
  83. static public function providerForTitleToUri()
  84. {
  85. return array(
  86. array('test', 'test'),
  87. array('test & test', 'test [and] test'),
  88. array('test + test', 'test [plus] test'),
  89. array('"test"', '[quote]test[quote]'),
  90. array('/test/', '[slash]test[slash]'),
  91. array('\\test\\', '[backslash]test[backslash]'),
  92. );
  93. }
  94. /**
  95. * @see uri2title
  96. */
  97. #[DataProvider('providerForUriToTitle')]
  98. function testUriToTitle($sIn, $sOut)
  99. {
  100. $this->assertEquals(uri2title($sIn), $sOut);
  101. }
  102. static public function providerForUriToTitle()
  103. {
  104. return array(
  105. array('test', 'test'),
  106. array('test [and] test', 'test & test'),
  107. array('test [plus] test', 'test + test'),
  108. array('[quote]test[quote]', '"test"'),
  109. array('[slash]test[slash]', '/test/'),
  110. array('[backslash]test[backslash]', '\\test\\'),
  111. );
  112. }
  113. /**
  114. * @see bx_is_serialized
  115. */
  116. #[DataProvider('providerForBxIsSerialized')]
  117. function testBxIsSerialized($sIn, $sOut)
  118. {
  119. $this->assertEquals(bx_is_serialized($sIn), $sOut);
  120. }
  121. static public function providerForBxIsSerialized()
  122. {
  123. return array(
  124. array([], false),
  125. array('serialized string', false),
  126. array('N;', true),
  127. array(serialize([]), true),
  128. array(serialize([new stdClass()]), true),
  129. );
  130. }
  131. /**
  132. * @see bx_process_output
  133. */
  134. #[DataProvider('providerForBxProcessOutputHtml')]
  135. function testBxProcessOutputHtml($sIn, $sOut)
  136. {
  137. $this->assertEquals(bx_process_output($sIn, BX_DATA_HTML), $sOut);
  138. }
  139. static public function providerForBxProcessOutputHtml()
  140. {
  141. return [
  142. ['<p>text</p><iframe src="https://example.com" /><p>text</p>', '<p>text</p><iframe src="https://example.com"></iframe><p>text</p>'],
  143. ['<p>text</p><img src="https://example.com" /><p>text</p>', '<p>text</p><img src="https://example.com"><p>text</p>'],
  144. ['<p>text</p><br/><br><br /><p>text</p>', '<p>text</p><br><p>text</p>'],
  145. ['<p>text</p><p></p><p> </p><p>text</p>', '<p>text</p><p>text</p>'],
  146. ['<p>www.example.com</p>', '<p><a class="bx-link" target="_blank" rel="nofollow" href="http://www.example.com">www.example.com</a></p>'],
  147. ];
  148. }
  149. /**
  150. * @see bx_process_output
  151. */
  152. #[DataProvider('providerForBxProcessOutputText')]
  153. function testBxProcessOutputText($sIn, $sOut)
  154. {
  155. $this->assertEquals(bx_process_output($sIn, BX_DATA_TEXT), $sOut);
  156. }
  157. static public function providerForBxProcessOutputText()
  158. {
  159. return [
  160. ['text <script>alert(1);</script>', 'text &lt;script&gt;alert(1);&lt;/script&gt;'],
  161. ];
  162. }
  163. }