PluginMarkdownTest.php 9.2 KB

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