PluginMarkdownTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. $this->assertEquals($markdown, $data['links'][0]['description_src']);
  45. }
  46. /**
  47. * Test render_feed hook.
  48. */
  49. public function testMarkdownFeed()
  50. {
  51. $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
  52. $markdown .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
  53. $data = array(
  54. 'links' => array(
  55. 0 => array(
  56. 'description' => $markdown,
  57. ),
  58. ),
  59. );
  60. $data = hook_markdown_render_feed($data, $this->conf);
  61. $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
  62. $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
  63. $this->assertStringEndsWith(
  64. '&#8212; <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
  65. $data['links'][0]['description']
  66. );
  67. }
  68. /**
  69. * Test render_daily hook.
  70. * Only check that there is basic markdown rendering.
  71. */
  72. public function testMarkdownDaily()
  73. {
  74. $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
  75. $data = array(
  76. // Columns data
  77. 'linksToDisplay' => array(
  78. // nth link
  79. 0 => array(
  80. 'formatedDescription' => $markdown,
  81. ),
  82. ),
  83. );
  84. $data = hook_markdown_render_daily($data, $this->conf);
  85. $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
  86. $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
  87. }
  88. /**
  89. * Test reverse_text2clickable().
  90. */
  91. public function testReverseText2clickable()
  92. {
  93. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  94. $clickableText = text2clickable($text, '');
  95. $reversedText = reverse_text2clickable($clickableText);
  96. $this->assertEquals($text, $reversedText);
  97. }
  98. /**
  99. * Test reverse_text2clickable().
  100. */
  101. public function testReverseText2clickableHashtags()
  102. {
  103. $text = file_get_contents('tests/plugins/resources/hashtags.raw');
  104. $md = file_get_contents('tests/plugins/resources/hashtags.md');
  105. $clickableText = hashtag_autolink($text);
  106. $reversedText = reverse_text2clickable($clickableText);
  107. $this->assertEquals($md, $reversedText);
  108. }
  109. /**
  110. * Test reverse_nl2br().
  111. */
  112. public function testReverseNl2br()
  113. {
  114. $text = 'stuff' . PHP_EOL . 'otherstuff';
  115. $processedText = nl2br($text);
  116. $reversedText = reverse_nl2br($processedText);
  117. $this->assertEquals($text, $reversedText);
  118. }
  119. /**
  120. * Test reverse_space2nbsp().
  121. */
  122. public function testReverseSpace2nbsp()
  123. {
  124. $text = ' stuff' . PHP_EOL . ' otherstuff and another';
  125. $processedText = space2nbsp($text);
  126. $reversedText = reverse_space2nbsp($processedText);
  127. $this->assertEquals($text, $reversedText);
  128. }
  129. public function testReverseFeedPermalink()
  130. {
  131. $text = 'Description... ';
  132. $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
  133. $expected = 'Description... &#8212; [Permalien](http://domain.tld/?0oc_VQ)';
  134. $processedText = reverse_feed_permalink($text);
  135. $this->assertEquals($expected, $processedText);
  136. }
  137. public function testReverseLastFeedPermalink()
  138. {
  139. $text = 'Description... ';
  140. $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
  141. $expected = $text;
  142. $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
  143. $expected .= '<br>&#8212; [Permalien](http://domain.tld/?0oc_VQ)';
  144. $processedText = reverse_feed_permalink($text);
  145. $this->assertEquals($expected, $processedText);
  146. }
  147. public function testReverseNoFeedPermalink()
  148. {
  149. $text = 'Hello! Where are you from?';
  150. $expected = $text;
  151. $processedText = reverse_feed_permalink($text);
  152. $this->assertEquals($expected, $processedText);
  153. }
  154. /**
  155. * Test sanitize_html().
  156. */
  157. public function testSanitizeHtml()
  158. {
  159. $input = '< script src="js.js"/>';
  160. $input .= '< script attr>alert(\'xss\');</script>';
  161. $input .= '<style> * { display: none }</style>';
  162. $output = escape($input);
  163. $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
  164. $output .= '<a href="#" attr="tt">link</a>';
  165. $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
  166. $output .= '<a href="#" attr="tt">link</a>';
  167. $this->assertEquals($output, sanitize_html($input));
  168. // Do not touch escaped HTML.
  169. $input = escape($input);
  170. $this->assertEquals($input, sanitize_html($input));
  171. }
  172. /**
  173. * Test the no markdown tag.
  174. */
  175. public function testNoMarkdownTag()
  176. {
  177. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  178. $data = array(
  179. 'links' => array(array(
  180. 'description' => $str,
  181. 'tags' => NO_MD_TAG,
  182. 'taglist' => array(NO_MD_TAG),
  183. ))
  184. );
  185. $processed = hook_markdown_render_linklist($data, $this->conf);
  186. $this->assertEquals($str, $processed['links'][0]['description']);
  187. $processed = hook_markdown_render_feed($data, $this->conf);
  188. $this->assertEquals($str, $processed['links'][0]['description']);
  189. $data = array(
  190. // Columns data
  191. 'linksToDisplay' => array(
  192. // nth link
  193. 0 => array(
  194. 'formatedDescription' => $str,
  195. 'tags' => NO_MD_TAG,
  196. 'taglist' => array(),
  197. ),
  198. ),
  199. );
  200. $data = hook_markdown_render_daily($data, $this->conf);
  201. $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
  202. }
  203. /**
  204. * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
  205. */
  206. public function testNoMarkdownNotExcactlyMatching()
  207. {
  208. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  209. $data = array(
  210. 'links' => array(array(
  211. 'description' => $str,
  212. 'tags' => '.' . NO_MD_TAG,
  213. 'taglist' => array('.'. NO_MD_TAG),
  214. ))
  215. );
  216. $data = hook_markdown_render_feed($data, $this->conf);
  217. $this->assertContains('<em>', $data['links'][0]['description']);
  218. }
  219. /**
  220. * Make sure that the generated HTML match the reference HTML file.
  221. */
  222. public function testMarkdownGlobalProcessDescription()
  223. {
  224. $md = file_get_contents('tests/plugins/resources/markdown.md');
  225. $md = format_description($md);
  226. $html = file_get_contents('tests/plugins/resources/markdown.html');
  227. $data = process_markdown(
  228. $md,
  229. $this->conf->get('security.markdown_escape', true),
  230. $this->conf->get('security.allowed_protocols')
  231. );
  232. $this->assertEquals($html, $data . PHP_EOL);
  233. }
  234. /**
  235. * Make sure that the HTML tags are escaped.
  236. */
  237. public function testMarkdownWithHtmlEscape()
  238. {
  239. $md = '**strong** <strong>strong</strong>';
  240. $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
  241. $data = array(
  242. 'links' => array(
  243. 0 => array(
  244. 'description' => $md,
  245. ),
  246. ),
  247. );
  248. $data = hook_markdown_render_linklist($data, $this->conf);
  249. $this->assertEquals($html, $data['links'][0]['description']);
  250. }
  251. /**
  252. * Make sure that the HTML tags aren't escaped with the setting set to false.
  253. */
  254. public function testMarkdownWithHtmlNoEscape()
  255. {
  256. $this->conf->set('security.markdown_escape', false);
  257. $md = '**strong** <strong>strong</strong>';
  258. $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
  259. $data = array(
  260. 'links' => array(
  261. 0 => array(
  262. 'description' => $md,
  263. ),
  264. ),
  265. );
  266. $data = hook_markdown_render_linklist($data, $this->conf);
  267. $this->assertEquals($html, $data['links'][0]['description']);
  268. }
  269. }