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