PluginMarkdownTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * PluginMarkdownTest.php
  4. */
  5. require_once 'application/Utils.php';
  6. require_once 'plugins/markdown/markdown.php';
  7. /**
  8. * Class PluginMarkdownTest
  9. * Unit test for the Markdown plugin
  10. */
  11. class PluginMarkdownTest extends PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * Reset plugin path
  15. */
  16. function setUp()
  17. {
  18. PluginManager::$PLUGINS_PATH = 'plugins';
  19. }
  20. /**
  21. * Test render_linklist hook.
  22. * Only check that there is basic markdown rendering.
  23. */
  24. function testMarkdownLinklist()
  25. {
  26. $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
  27. $data = array(
  28. 'links' => array(
  29. 0 => array(
  30. 'description' => $markdown,
  31. ),
  32. ),
  33. );
  34. $data = hook_markdown_render_linklist($data);
  35. $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
  36. $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
  37. }
  38. /**
  39. * Test render_daily hook.
  40. * Only check that there is basic markdown rendering.
  41. */
  42. function testMarkdownDaily()
  43. {
  44. $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
  45. $data = array(
  46. // Columns data
  47. 'cols' => array(
  48. // First, second, third.
  49. 0 => array(
  50. // nth link
  51. 0 => array(
  52. 'formatedDescription' => $markdown,
  53. ),
  54. ),
  55. ),
  56. );
  57. $data = hook_markdown_render_daily($data);
  58. $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
  59. $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
  60. }
  61. /**
  62. * Test reverse_text2clickable().
  63. */
  64. function testReverseText2clickable()
  65. {
  66. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  67. $clickableText = text2clickable($text, '');
  68. $reversedText = reverse_text2clickable($clickableText);
  69. $this->assertEquals($text, $reversedText);
  70. }
  71. /**
  72. * Test reverse_nl2br().
  73. */
  74. function testReverseNl2br()
  75. {
  76. $text = 'stuff' . PHP_EOL . 'otherstuff';
  77. $processedText = nl2br($text);
  78. $reversedText = reverse_nl2br($processedText);
  79. $this->assertEquals($text, $reversedText);
  80. }
  81. /**
  82. * Test reverse_space2nbsp().
  83. */
  84. function testReverseSpace2nbsp()
  85. {
  86. $text = ' stuff' . PHP_EOL . ' otherstuff and another';
  87. $processedText = space2nbsp($text);
  88. $reversedText = reverse_space2nbsp($processedText);
  89. $this->assertEquals($text, $reversedText);
  90. }
  91. /**
  92. * Test sanitize_html().
  93. */
  94. function testSanitizeHtml()
  95. {
  96. $input = '< script src="js.js"/>';
  97. $input .= '< script attr>alert(\'xss\');</script>';
  98. $input .= '<style> * { display: none }</style>';
  99. $output = escape($input);
  100. $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
  101. $output .= '<a href="#" attr="tt">link</a>';
  102. $this->assertEquals($output, sanitize_html($input));
  103. // Do not touch escaped HTML.
  104. $input = escape($input);
  105. $this->assertEquals($input, sanitize_html($input));
  106. }
  107. /**
  108. * Test the no markdown tag.
  109. */
  110. function testNoMarkdownTag()
  111. {
  112. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  113. $data = array(
  114. 'links' => array(array(
  115. 'description' => $str,
  116. 'tags' => NO_MD_TAG,
  117. 'taglist' => array(NO_MD_TAG),
  118. ))
  119. );
  120. $processed = hook_markdown_render_linklist($data);
  121. $this->assertEquals($str, $processed['links'][0]['description']);
  122. $processed = hook_markdown_render_feed($data);
  123. $this->assertEquals($str, $processed['links'][0]['description']);
  124. $data = array(
  125. // Columns data
  126. 'cols' => array(
  127. // First, second, third.
  128. 0 => array(
  129. // nth link
  130. 0 => array(
  131. 'formatedDescription' => $str,
  132. 'tags' => NO_MD_TAG,
  133. 'taglist' => array(),
  134. ),
  135. ),
  136. ),
  137. );
  138. $data = hook_markdown_render_daily($data);
  139. $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
  140. }
  141. /**
  142. * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
  143. */
  144. function testNoMarkdownNotExcactlyMatching()
  145. {
  146. $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
  147. $data = array(
  148. 'links' => array(array(
  149. 'description' => $str,
  150. 'tags' => '.' . NO_MD_TAG,
  151. 'taglist' => array('.'. NO_MD_TAG),
  152. ))
  153. );
  154. $data = hook_markdown_render_feed($data);
  155. $this->assertContains('<em>', $data['links'][0]['description']);
  156. }
  157. /**
  158. * Test hashtag links processed with markdown.
  159. */
  160. function testMarkdownHashtagLinks()
  161. {
  162. $md = file_get_contents('tests/plugins/resources/markdown.md');
  163. $md = format_description($md);
  164. $html = file_get_contents('tests/plugins/resources/markdown.html');
  165. $data = process_markdown($md);
  166. $this->assertEquals($html, $data);
  167. }
  168. }