PluginMarkdownTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. use Shaarli\Config\ConfigManager;
  3. /**
  4. * PluginMarkdownTest.php
  5. */
  6. require_once 'application/Utils.php';
  7. require_once 'plugins/markdown/markdown.php';
  8. /**
  9. * Class PluginMarkdownTest
  10. * Unit test for the Markdown plugin
  11. */
  12. class PluginMarkdownTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var ConfigManager instance.
  16. */
  17. protected $conf;
  18. /**
  19. * Reset plugin path
  20. */
  21. public function setUp()
  22. {
  23. PluginManager::$PLUGINS_PATH = 'plugins';
  24. $this->conf = new ConfigManager('tests/utils/config/configJson');
  25. $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']);
  26. }
  27. /**
  28. * Test render_linklist hook.
  29. * Only check that there is basic markdown rendering.
  30. */
  31. public function testMarkdownLinklist()
  32. {
  33. $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
  34. $data = array(
  35. 'links' => array(
  36. 0 => array(
  37. 'description' => $markdown,
  38. ),
  39. ),
  40. );
  41. $data = hook_markdown_render_linklist($data, $this->conf);
  42. $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
  43. $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
  44. }
  45. /**
  46. * Test render_daily hook.
  47. * Only check that there is basic markdown rendering.
  48. */
  49. public function testMarkdownDaily()
  50. {
  51. $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
  52. $data = array(
  53. // Columns data
  54. 'linksToDisplay' => array(
  55. // nth link
  56. 0 => array(
  57. 'formatedDescription' => $markdown,
  58. ),
  59. ),
  60. );
  61. $data = hook_markdown_render_daily($data, $this->conf);
  62. $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
  63. $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
  64. }
  65. /**
  66. * Test reverse_text2clickable().
  67. */
  68. public function testReverseText2clickable()
  69. {
  70. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  71. $clickableText = text2clickable($text, '');
  72. $reversedText = reverse_text2clickable($clickableText);
  73. $this->assertEquals($text, $reversedText);
  74. }
  75. /**
  76. * Test reverse_nl2br().
  77. */
  78. public function testReverseNl2br()
  79. {
  80. $text = 'stuff' . PHP_EOL . 'otherstuff';
  81. $processedText = nl2br($text);
  82. $reversedText = reverse_nl2br($processedText);
  83. $this->assertEquals($text, $reversedText);
  84. }
  85. /**
  86. * Test reverse_space2nbsp().
  87. */
  88. public function testReverseSpace2nbsp()
  89. {
  90. $text = ' stuff' . PHP_EOL . ' otherstuff and another';
  91. $processedText = space2nbsp($text);
  92. $reversedText = reverse_space2nbsp($processedText);
  93. $this->assertEquals($text, $reversedText);
  94. }
  95. /**
  96. * Test sanitize_html().
  97. */
  98. public function testSanitizeHtml()
  99. {
  100. $input = '< script src="js.js"/>';
  101. $input .= '< script attr>alert(\'xss\');</script>';
  102. $input .= '<style> * { display: none }</style>';
  103. $output = escape($input);
  104. $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
  105. $output .= '<a href="#" attr="tt">link</a>';
  106. $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
  107. $output .= '<a href="#" attr="tt">link</a>';
  108. $this->assertEquals($output, sanitize_html($input));
  109. // Do not touch escaped HTML.
  110. $input = escape($input);
  111. $this->assertEquals($input, sanitize_html($input));
  112. }
  113. /**
  114. * Test the no markdown tag.
  115. */
  116. public function testNoMarkdownTag()
  117. {
  118. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  119. $data = array(
  120. 'links' => array(array(
  121. 'description' => $str,
  122. 'tags' => NO_MD_TAG,
  123. 'taglist' => array(NO_MD_TAG),
  124. ))
  125. );
  126. $processed = hook_markdown_render_linklist($data, $this->conf);
  127. $this->assertEquals($str, $processed['links'][0]['description']);
  128. $processed = hook_markdown_render_feed($data, $this->conf);
  129. $this->assertEquals($str, $processed['links'][0]['description']);
  130. $data = array(
  131. // Columns data
  132. 'linksToDisplay' => array(
  133. // nth link
  134. 0 => array(
  135. 'formatedDescription' => $str,
  136. 'tags' => NO_MD_TAG,
  137. 'taglist' => array(),
  138. ),
  139. ),
  140. );
  141. $data = hook_markdown_render_daily($data, $this->conf);
  142. $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
  143. }
  144. /**
  145. * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
  146. */
  147. public function testNoMarkdownNotExcactlyMatching()
  148. {
  149. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  150. $data = array(
  151. 'links' => array(array(
  152. 'description' => $str,
  153. 'tags' => '.' . NO_MD_TAG,
  154. 'taglist' => array('.'. NO_MD_TAG),
  155. ))
  156. );
  157. $data = hook_markdown_render_feed($data, $this->conf);
  158. $this->assertContains('<em>', $data['links'][0]['description']);
  159. }
  160. /**
  161. * Make sure that the generated HTML match the reference HTML file.
  162. */
  163. public function testMarkdownGlobalProcessDescription()
  164. {
  165. $md = file_get_contents('tests/plugins/resources/markdown.md');
  166. $md = format_description($md);
  167. $html = file_get_contents('tests/plugins/resources/markdown.html');
  168. $data = process_markdown(
  169. $md,
  170. $this->conf->get('security.markdown_escape', true),
  171. $this->conf->get('security.allowed_protocols')
  172. );
  173. $this->assertEquals($html, $data);
  174. }
  175. /**
  176. * Make sure that the HTML tags are escaped.
  177. */
  178. public function testMarkdownWithHtmlEscape()
  179. {
  180. $md = '**strong** <strong>strong</strong>';
  181. $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
  182. $data = array(
  183. 'links' => array(
  184. 0 => array(
  185. 'description' => $md,
  186. ),
  187. ),
  188. );
  189. $data = hook_markdown_render_linklist($data, $this->conf);
  190. $this->assertEquals($html, $data['links'][0]['description']);
  191. }
  192. /**
  193. * Make sure that the HTML tags aren't escaped with the setting set to false.
  194. */
  195. public function testMarkdownWithHtmlNoEscape()
  196. {
  197. $this->conf->set('security.markdown_escape', false);
  198. $md = '**strong** <strong>strong</strong>';
  199. $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
  200. $data = array(
  201. 'links' => array(
  202. 0 => array(
  203. 'description' => $md,
  204. ),
  205. ),
  206. );
  207. $data = hook_markdown_render_linklist($data, $this->conf);
  208. $this->assertEquals($html, $data['links'][0]['description']);
  209. }
  210. }