PluginMarkdownTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. 'cols' => array(
  55. // First, second, third.
  56. 0 => array(
  57. // nth link
  58. 0 => array(
  59. 'formatedDescription' => $markdown,
  60. ),
  61. ),
  62. ),
  63. );
  64. $data = hook_markdown_render_daily($data, $this->conf);
  65. $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
  66. $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
  67. }
  68. /**
  69. * Test reverse_text2clickable().
  70. */
  71. public function testReverseText2clickable()
  72. {
  73. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  74. $clickableText = text2clickable($text, '');
  75. $reversedText = reverse_text2clickable($clickableText);
  76. $this->assertEquals($text, $reversedText);
  77. }
  78. /**
  79. * Test reverse_nl2br().
  80. */
  81. public function testReverseNl2br()
  82. {
  83. $text = 'stuff' . PHP_EOL . 'otherstuff';
  84. $processedText = nl2br($text);
  85. $reversedText = reverse_nl2br($processedText);
  86. $this->assertEquals($text, $reversedText);
  87. }
  88. /**
  89. * Test reverse_space2nbsp().
  90. */
  91. public function testReverseSpace2nbsp()
  92. {
  93. $text = ' stuff' . PHP_EOL . ' otherstuff and another';
  94. $processedText = space2nbsp($text);
  95. $reversedText = reverse_space2nbsp($processedText);
  96. $this->assertEquals($text, $reversedText);
  97. }
  98. /**
  99. * Test sanitize_html().
  100. */
  101. public function testSanitizeHtml()
  102. {
  103. $input = '< script src="js.js"/>';
  104. $input .= '< script attr>alert(\'xss\');</script>';
  105. $input .= '<style> * { display: none }</style>';
  106. $output = escape($input);
  107. $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
  108. $output .= '<a href="#" attr="tt">link</a>';
  109. $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
  110. $output .= '<a href="#" attr="tt">link</a>';
  111. $this->assertEquals($output, sanitize_html($input));
  112. // Do not touch escaped HTML.
  113. $input = escape($input);
  114. $this->assertEquals($input, sanitize_html($input));
  115. }
  116. /**
  117. * Test the no markdown tag.
  118. */
  119. public function testNoMarkdownTag()
  120. {
  121. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  122. $data = array(
  123. 'links' => array(array(
  124. 'description' => $str,
  125. 'tags' => NO_MD_TAG,
  126. 'taglist' => array(NO_MD_TAG),
  127. ))
  128. );
  129. $processed = hook_markdown_render_linklist($data, $this->conf);
  130. $this->assertEquals($str, $processed['links'][0]['description']);
  131. $processed = hook_markdown_render_feed($data, $this->conf);
  132. $this->assertEquals($str, $processed['links'][0]['description']);
  133. $data = array(
  134. // Columns data
  135. 'cols' => array(
  136. // First, second, third.
  137. 0 => array(
  138. // nth link
  139. 0 => array(
  140. 'formatedDescription' => $str,
  141. 'tags' => NO_MD_TAG,
  142. 'taglist' => array(),
  143. ),
  144. ),
  145. ),
  146. );
  147. $data = hook_markdown_render_daily($data, $this->conf);
  148. $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
  149. }
  150. /**
  151. * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
  152. */
  153. public function testNoMarkdownNotExcactlyMatching()
  154. {
  155. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  156. $data = array(
  157. 'links' => array(array(
  158. 'description' => $str,
  159. 'tags' => '.' . NO_MD_TAG,
  160. 'taglist' => array('.'. NO_MD_TAG),
  161. ))
  162. );
  163. $data = hook_markdown_render_feed($data, $this->conf);
  164. $this->assertContains('<em>', $data['links'][0]['description']);
  165. }
  166. /**
  167. * Make sure that the generated HTML match the reference HTML file.
  168. */
  169. public function testMarkdownGlobalProcessDescription()
  170. {
  171. $md = file_get_contents('tests/plugins/resources/markdown.md');
  172. $md = format_description($md);
  173. $html = file_get_contents('tests/plugins/resources/markdown.html');
  174. $data = process_markdown(
  175. $md,
  176. $this->conf->get('security.markdown_escape', true),
  177. $this->conf->get('security.allowed_protocols')
  178. );
  179. $this->assertEquals($html, $data);
  180. }
  181. /**
  182. * Make sure that the HTML tags are escaped.
  183. */
  184. public function testMarkdownWithHtmlEscape()
  185. {
  186. $md = '**strong** <strong>strong</strong>';
  187. $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
  188. $data = array(
  189. 'links' => array(
  190. 0 => array(
  191. 'description' => $md,
  192. ),
  193. ),
  194. );
  195. $data = hook_markdown_render_linklist($data, $this->conf);
  196. $this->assertEquals($html, $data['links'][0]['description']);
  197. }
  198. /**
  199. * Make sure that the HTML tags aren't escaped with the setting set to false.
  200. */
  201. public function testMarkdownWithHtmlNoEscape()
  202. {
  203. $this->conf->set('security.markdown_escape', false);
  204. $md = '**strong** <strong>strong</strong>';
  205. $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
  206. $data = array(
  207. 'links' => array(
  208. 0 => array(
  209. 'description' => $md,
  210. ),
  211. ),
  212. );
  213. $data = hook_markdown_render_linklist($data, $this->conf);
  214. $this->assertEquals($html, $data['links'][0]['description']);
  215. }
  216. }