]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | use Shaarli\Config\ConfigManager; | |
3 | ||
4 | /** | |
5 | * PluginMarkdownTest.php | |
6 | */ | |
7 | ||
8 | require_once 'application/Utils.php'; | |
9 | require_once 'plugins/markdown/markdown.php'; | |
10 | ||
11 | /** | |
12 | * Class PluginMarkdownTest | |
13 | * Unit test for the Markdown plugin | |
14 | */ | |
15 | class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |
16 | { | |
17 | /** | |
18 | * @var ConfigManager instance. | |
19 | */ | |
20 | protected $conf; | |
21 | ||
22 | /** | |
23 | * Reset plugin path | |
24 | */ | |
25 | public function setUp() | |
26 | { | |
27 | PluginManager::$PLUGINS_PATH = 'plugins'; | |
28 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
29 | $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']); | |
30 | } | |
31 | ||
32 | /** | |
33 | * Test render_linklist hook. | |
34 | * Only check that there is basic markdown rendering. | |
35 | */ | |
36 | public function testMarkdownLinklist() | |
37 | { | |
38 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
39 | $data = array( | |
40 | 'links' => array( | |
41 | 0 => array( | |
42 | 'description' => $markdown, | |
43 | ), | |
44 | ), | |
45 | ); | |
46 | ||
47 | $data = hook_markdown_render_linklist($data, $this->conf); | |
48 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); | |
49 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Test render_daily hook. | |
54 | * Only check that there is basic markdown rendering. | |
55 | */ | |
56 | public function testMarkdownDaily() | |
57 | { | |
58 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | |
59 | $data = array( | |
60 | // Columns data | |
61 | 'linksToDisplay' => array( | |
62 | // nth link | |
63 | 0 => array( | |
64 | 'formatedDescription' => $markdown, | |
65 | ), | |
66 | ), | |
67 | ); | |
68 | ||
69 | $data = hook_markdown_render_daily($data, $this->conf); | |
70 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>')); | |
71 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>')); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Test reverse_text2clickable(). | |
76 | */ | |
77 | public function testReverseText2clickable() | |
78 | { | |
79 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
80 | $clickableText = text2clickable($text, ''); | |
81 | $reversedText = reverse_text2clickable($clickableText); | |
82 | $this->assertEquals($text, $reversedText); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Test reverse_nl2br(). | |
87 | */ | |
88 | public function testReverseNl2br() | |
89 | { | |
90 | $text = 'stuff' . PHP_EOL . 'otherstuff'; | |
91 | $processedText = nl2br($text); | |
92 | $reversedText = reverse_nl2br($processedText); | |
93 | $this->assertEquals($text, $reversedText); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Test reverse_space2nbsp(). | |
98 | */ | |
99 | public function testReverseSpace2nbsp() | |
100 | { | |
101 | $text = ' stuff' . PHP_EOL . ' otherstuff and another'; | |
102 | $processedText = space2nbsp($text); | |
103 | $reversedText = reverse_space2nbsp($processedText); | |
104 | $this->assertEquals($text, $reversedText); | |
105 | } | |
106 | ||
107 | /** | |
108 | * Test sanitize_html(). | |
109 | */ | |
110 | public function testSanitizeHtml() | |
111 | { | |
112 | $input = '< script src="js.js"/>'; | |
113 | $input .= '< script attr>alert(\'xss\');</script>'; | |
114 | $input .= '<style> * { display: none }</style>'; | |
115 | $output = escape($input); | |
116 | $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>'; | |
117 | $output .= '<a href="#" attr="tt">link</a>'; | |
118 | $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>'; | |
119 | $output .= '<a href="#" attr="tt">link</a>'; | |
120 | $this->assertEquals($output, sanitize_html($input)); | |
121 | // Do not touch escaped HTML. | |
122 | $input = escape($input); | |
123 | $this->assertEquals($input, sanitize_html($input)); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Test the no markdown tag. | |
128 | */ | |
129 | public function testNoMarkdownTag() | |
130 | { | |
131 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
132 | $data = array( | |
133 | 'links' => array(array( | |
134 | 'description' => $str, | |
135 | 'tags' => NO_MD_TAG, | |
136 | 'taglist' => array(NO_MD_TAG), | |
137 | )) | |
138 | ); | |
139 | ||
140 | $processed = hook_markdown_render_linklist($data, $this->conf); | |
141 | $this->assertEquals($str, $processed['links'][0]['description']); | |
142 | ||
143 | $processed = hook_markdown_render_feed($data, $this->conf); | |
144 | $this->assertEquals($str, $processed['links'][0]['description']); | |
145 | ||
146 | $data = array( | |
147 | // Columns data | |
148 | 'linksToDisplay' => array( | |
149 | // nth link | |
150 | 0 => array( | |
151 | 'formatedDescription' => $str, | |
152 | 'tags' => NO_MD_TAG, | |
153 | 'taglist' => array(), | |
154 | ), | |
155 | ), | |
156 | ); | |
157 | ||
158 | $data = hook_markdown_render_daily($data, $this->conf); | |
159 | $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | |
164 | */ | |
165 | public function testNoMarkdownNotExcactlyMatching() | |
166 | { | |
167 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | |
168 | $data = array( | |
169 | 'links' => array(array( | |
170 | 'description' => $str, | |
171 | 'tags' => '.' . NO_MD_TAG, | |
172 | 'taglist' => array('.'. NO_MD_TAG), | |
173 | )) | |
174 | ); | |
175 | ||
176 | $data = hook_markdown_render_feed($data, $this->conf); | |
177 | $this->assertContains('<em>', $data['links'][0]['description']); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Make sure that the generated HTML match the reference HTML file. | |
182 | */ | |
183 | public function testMarkdownGlobalProcessDescription() | |
184 | { | |
185 | $md = file_get_contents('tests/plugins/resources/markdown.md'); | |
186 | $md = format_description($md); | |
187 | $html = file_get_contents('tests/plugins/resources/markdown.html'); | |
188 | ||
189 | $data = process_markdown( | |
190 | $md, | |
191 | $this->conf->get('security.markdown_escape', true), | |
192 | $this->conf->get('security.allowed_protocols') | |
193 | ); | |
194 | $this->assertEquals($html, $data); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Make sure that the HTML tags are escaped. | |
199 | */ | |
200 | public function testMarkdownWithHtmlEscape() | |
201 | { | |
202 | $md = '**strong** <strong>strong</strong>'; | |
203 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | |
204 | $data = array( | |
205 | 'links' => array( | |
206 | 0 => array( | |
207 | 'description' => $md, | |
208 | ), | |
209 | ), | |
210 | ); | |
211 | $data = hook_markdown_render_linklist($data, $this->conf); | |
212 | $this->assertEquals($html, $data['links'][0]['description']); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Make sure that the HTML tags aren't escaped with the setting set to false. | |
217 | */ | |
218 | public function testMarkdownWithHtmlNoEscape() | |
219 | { | |
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>'; | |
223 | $data = array( | |
224 | 'links' => array( | |
225 | 0 => array( | |
226 | 'description' => $md, | |
227 | ), | |
228 | ), | |
229 | ); | |
230 | $data = hook_markdown_render_linklist($data, $this->conf); | |
231 | $this->assertEquals($html, $data['links'][0]['description']); | |
232 | } | |
233 | } |