4 * PluginMarkdownTest.php
7 require_once 'application/Utils.php';
8 require_once 'plugins/markdown/markdown.php';
11 * Class PluginMarkdownTest
12 * Unit test for the Markdown plugin
14 class PluginMarkdownTest
extends PHPUnit_Framework_TestCase
17 * @var ConfigManager instance.
26 PluginManager
::$PLUGINS_PATH = 'plugins';
27 $this->conf
= new ConfigManager('tests/utils/config/configJson');
31 * Test render_linklist hook.
32 * Only check that there is basic markdown rendering.
34 function testMarkdownLinklist()
36 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
40 'description' => $markdown,
45 $data = hook_markdown_render_linklist($data, $this->conf
);
46 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
47 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
51 * Test render_daily hook.
52 * Only check that there is basic markdown rendering.
54 function testMarkdownDaily()
56 $markdown = '# My title' . PHP_EOL
. 'Very interesting content.';
60 // First, second, third.
64 'formatedDescription' => $markdown,
70 $data = hook_markdown_render_daily($data, $this->conf
);
71 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
72 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
76 * Test reverse_text2clickable().
78 function testReverseText2clickable()
80 $text = 'stuff http://hello.there/is=someone#here otherstuff';
81 $clickableText = text2clickable($text, '');
82 $reversedText = reverse_text2clickable($clickableText);
83 $this->assertEquals($text, $reversedText);
87 * Test reverse_nl2br().
89 function testReverseNl2br()
91 $text = 'stuff' . PHP_EOL
. 'otherstuff';
92 $processedText = nl2br($text);
93 $reversedText = reverse_nl2br($processedText);
94 $this->assertEquals($text, $reversedText);
98 * Test reverse_space2nbsp().
100 function testReverseSpace2nbsp()
102 $text = ' stuff' . PHP_EOL
. ' otherstuff and another';
103 $processedText = space2nbsp($text);
104 $reversedText = reverse_space2nbsp($processedText);
105 $this->assertEquals($text, $reversedText);
109 * Test sanitize_html().
111 function testSanitizeHtml()
113 $input = '< script src="js.js"/>';
114 $input .= '< script attr>alert(\'xss\');</script>';
115 $input .= '<style> * { display: none }</style>';
116 $output = escape($input);
117 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
118 $output .= '<a href="#" attr="tt">link</a>';
119 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
120 $output .= '<a href="#" attr="tt">link</a>';
121 $this->assertEquals($output, sanitize_html($input));
122 // Do not touch escaped HTML.
123 $input = escape($input);
124 $this->assertEquals($input, sanitize_html($input));
128 * Test the no markdown tag.
130 function testNoMarkdownTag()
132 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
134 'links' => array(array(
135 'description' => $str,
137 'taglist' => array(NO_MD_TAG
),
141 $processed = hook_markdown_render_linklist($data, $this->conf
);
142 $this->assertEquals($str, $processed['links'][0]['description']);
144 $processed = hook_markdown_render_feed($data, $this->conf
);
145 $this->assertEquals($str, $processed['links'][0]['description']);
150 // First, second, third.
154 'formatedDescription' => $str,
156 'taglist' => array(),
162 $data = hook_markdown_render_daily($data, $this->conf
);
163 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
167 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
169 function testNoMarkdownNotExcactlyMatching()
171 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
173 'links' => array(array(
174 'description' => $str,
175 'tags' => '.' . NO_MD_TAG
,
176 'taglist' => array('.'. NO_MD_TAG
),
180 $data = hook_markdown_render_feed($data, $this->conf
);
181 $this->assertContains('<em>', $data['links'][0]['description']);
185 * Test hashtag links processed with markdown.
187 function testMarkdownHashtagLinks()
189 $md = file_get_contents('tests/plugins/resources/markdown.md');
190 $md = format_description($md);
191 $html = file_get_contents('tests/plugins/resources/markdown.html');
193 $data = process_markdown($md);
194 $this->assertEquals($html, $data);
198 * Make sure that the HTML tags are escaped.
200 public function testMarkdownWithHtmlEscape()
202 $md = '**strong** <strong>strong</strong>';
203 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
207 'description' => $md,
211 $data = hook_markdown_render_linklist($data, $this->conf
);
212 $this->assertEquals($html, $data['links'][0]['description']);
216 * Make sure that the HTML tags aren't escaped with the setting set to false.
218 public function testMarkdownWithHtmlNoEscape()
220 $this->conf
->set('security.markdown_escape', false);
221 $md = '**strong** <strong>strong</strong>';
222 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
226 'description' => $md,
230 $data = hook_markdown_render_linklist($data, $this->conf
);
231 $this->assertEquals($html, $data['links'][0]['description']);