diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-17 21:34:12 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-01-18 09:56:32 +0100 |
commit | e26e2060f5470ce8bf4c5973284bae07b8af170a (patch) | |
tree | adf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/plugins | |
parent | cf92b4dd1521241eefc58eaf6dcd202cd83969d8 (diff) | |
download | Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.gz Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.zst Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.zip |
Add and update unit test for the new system (Bookmark + Service)
See #1307
Diffstat (limited to 'tests/plugins')
-rw-r--r-- | tests/plugins/PluginArchiveorgTest.php | 4 | ||||
-rw-r--r-- | tests/plugins/PluginIssoTest.php | 12 | ||||
-rw-r--r-- | tests/plugins/PluginMarkdownTest.php | 316 |
3 files changed, 8 insertions, 324 deletions
diff --git a/tests/plugins/PluginArchiveorgTest.php b/tests/plugins/PluginArchiveorgTest.php index 510288bb..b9a67adb 100644 --- a/tests/plugins/PluginArchiveorgTest.php +++ b/tests/plugins/PluginArchiveorgTest.php | |||
@@ -24,7 +24,7 @@ class PluginArchiveorgTest extends \PHPUnit\Framework\TestCase | |||
24 | } | 24 | } |
25 | 25 | ||
26 | /** | 26 | /** |
27 | * Test render_linklist hook on external links. | 27 | * Test render_linklist hook on external bookmarks. |
28 | */ | 28 | */ |
29 | public function testArchiveorgLinklistOnExternalLinks() | 29 | public function testArchiveorgLinklistOnExternalLinks() |
30 | { | 30 | { |
@@ -54,7 +54,7 @@ class PluginArchiveorgTest extends \PHPUnit\Framework\TestCase | |||
54 | } | 54 | } |
55 | 55 | ||
56 | /** | 56 | /** |
57 | * Test render_linklist hook on internal links. | 57 | * Test render_linklist hook on internal bookmarks. |
58 | */ | 58 | */ |
59 | public function testArchiveorgLinklistOnInternalLinks() | 59 | public function testArchiveorgLinklistOnInternalLinks() |
60 | { | 60 | { |
diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php index bdfab439..99477205 100644 --- a/tests/plugins/PluginIssoTest.php +++ b/tests/plugins/PluginIssoTest.php | |||
@@ -2,7 +2,7 @@ | |||
2 | namespace Shaarli\Plugin\Isso; | 2 | namespace Shaarli\Plugin\Isso; |
3 | 3 | ||
4 | use DateTime; | 4 | use DateTime; |
5 | use Shaarli\Bookmark\LinkDB; | 5 | use Shaarli\Bookmark\Bookmark; |
6 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
7 | use Shaarli\Plugin\PluginManager; | 7 | use Shaarli\Plugin\PluginManager; |
8 | 8 | ||
@@ -60,7 +60,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase | |||
60 | array( | 60 | array( |
61 | 'id' => 12, | 61 | 'id' => 12, |
62 | 'url' => $str, | 62 | 'url' => $str, |
63 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), | 63 | 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date), |
64 | ) | 64 | ) |
65 | ) | 65 | ) |
66 | ); | 66 | ); |
@@ -85,7 +85,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase | |||
85 | } | 85 | } |
86 | 86 | ||
87 | /** | 87 | /** |
88 | * Test isso plugin when multiple links are displayed (shouldn't be displayed). | 88 | * Test isso plugin when multiple bookmarks are displayed (shouldn't be displayed). |
89 | */ | 89 | */ |
90 | public function testIssoMultipleLinks() | 90 | public function testIssoMultipleLinks() |
91 | { | 91 | { |
@@ -102,13 +102,13 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase | |||
102 | 'id' => 12, | 102 | 'id' => 12, |
103 | 'url' => $str, | 103 | 'url' => $str, |
104 | 'shorturl' => $short1 = 'abcd', | 104 | 'shorturl' => $short1 = 'abcd', |
105 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date1), | 105 | 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date1), |
106 | ), | 106 | ), |
107 | array( | 107 | array( |
108 | 'id' => 13, | 108 | 'id' => 13, |
109 | 'url' => $str . '2', | 109 | 'url' => $str . '2', |
110 | 'shorturl' => $short2 = 'efgh', | 110 | 'shorturl' => $short2 = 'efgh', |
111 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date2), | 111 | 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date2), |
112 | ), | 112 | ), |
113 | ) | 113 | ) |
114 | ); | 114 | ); |
@@ -136,7 +136,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase | |||
136 | 'id' => 12, | 136 | 'id' => 12, |
137 | 'url' => $str, | 137 | 'url' => $str, |
138 | 'shorturl' => $short1 = 'abcd', | 138 | 'shorturl' => $short1 = 'abcd', |
139 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), | 139 | 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date), |
140 | ) | 140 | ) |
141 | ), | 141 | ), |
142 | 'search_term' => $str | 142 | 'search_term' => $str |
diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php deleted file mode 100644 index 15fa9ba5..00000000 --- a/tests/plugins/PluginMarkdownTest.php +++ /dev/null | |||
@@ -1,316 +0,0 @@ | |||
1 | <?php | ||
2 | namespace Shaarli\Plugin\Markdown; | ||
3 | |||
4 | use Shaarli\Config\ConfigManager; | ||
5 | use Shaarli\Plugin\PluginManager; | ||
6 | |||
7 | /** | ||
8 | * PluginMarkdownTest.php | ||
9 | */ | ||
10 | |||
11 | require_once 'application/bookmark/LinkUtils.php'; | ||
12 | require_once 'application/Utils.php'; | ||
13 | require_once 'plugins/markdown/markdown.php'; | ||
14 | |||
15 | /** | ||
16 | * Class PluginMarkdownTest | ||
17 | * Unit test for the Markdown plugin | ||
18 | */ | ||
19 | class PluginMarkdownTest extends \PHPUnit\Framework\TestCase | ||
20 | { | ||
21 | /** | ||
22 | * @var ConfigManager instance. | ||
23 | */ | ||
24 | protected $conf; | ||
25 | |||
26 | /** | ||
27 | * Reset plugin path | ||
28 | */ | ||
29 | public function setUp() | ||
30 | { | ||
31 | PluginManager::$PLUGINS_PATH = 'plugins'; | ||
32 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | ||
33 | $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']); | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Test render_linklist hook. | ||
38 | * Only check that there is basic markdown rendering. | ||
39 | */ | ||
40 | public function testMarkdownLinklist() | ||
41 | { | ||
42 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | ||
43 | $data = array( | ||
44 | 'links' => array( | ||
45 | 0 => array( | ||
46 | 'description' => $markdown, | ||
47 | ), | ||
48 | ), | ||
49 | ); | ||
50 | |||
51 | $data = hook_markdown_render_linklist($data, $this->conf); | ||
52 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); | ||
53 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | ||
54 | |||
55 | $this->assertEquals($markdown, $data['links'][0]['description_src']); | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Test render_feed hook. | ||
60 | */ | ||
61 | public function testMarkdownFeed() | ||
62 | { | ||
63 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | ||
64 | $markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | ||
65 | $data = array( | ||
66 | 'links' => array( | ||
67 | 0 => array( | ||
68 | 'description' => $markdown, | ||
69 | ), | ||
70 | ), | ||
71 | ); | ||
72 | |||
73 | $data = hook_markdown_render_feed($data, $this->conf); | ||
74 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>')); | ||
75 | $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>')); | ||
76 | $this->assertStringEndsWith( | ||
77 | '— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>', | ||
78 | $data['links'][0]['description'] | ||
79 | ); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Test render_daily hook. | ||
84 | * Only check that there is basic markdown rendering. | ||
85 | */ | ||
86 | public function testMarkdownDaily() | ||
87 | { | ||
88 | $markdown = '# My title' . PHP_EOL . 'Very interesting content.'; | ||
89 | $data = array( | ||
90 | // Columns data | ||
91 | 'linksToDisplay' => array( | ||
92 | // nth link | ||
93 | 0 => array( | ||
94 | 'formatedDescription' => $markdown, | ||
95 | ), | ||
96 | ), | ||
97 | ); | ||
98 | |||
99 | $data = hook_markdown_render_daily($data, $this->conf); | ||
100 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>')); | ||
101 | $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>')); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Test reverse_text2clickable(). | ||
106 | */ | ||
107 | public function testReverseText2clickable() | ||
108 | { | ||
109 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | ||
110 | $clickableText = text2clickable($text); | ||
111 | $reversedText = reverse_text2clickable($clickableText); | ||
112 | $this->assertEquals($text, $reversedText); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Test reverse_text2clickable(). | ||
117 | */ | ||
118 | public function testReverseText2clickableHashtags() | ||
119 | { | ||
120 | $text = file_get_contents('tests/plugins/resources/hashtags.raw'); | ||
121 | $md = file_get_contents('tests/plugins/resources/hashtags.md'); | ||
122 | $clickableText = hashtag_autolink($text); | ||
123 | $reversedText = reverse_text2clickable($clickableText); | ||
124 | $this->assertEquals($md, $reversedText); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * Test reverse_nl2br(). | ||
129 | */ | ||
130 | public function testReverseNl2br() | ||
131 | { | ||
132 | $text = 'stuff' . PHP_EOL . 'otherstuff'; | ||
133 | $processedText = nl2br($text); | ||
134 | $reversedText = reverse_nl2br($processedText); | ||
135 | $this->assertEquals($text, $reversedText); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Test reverse_space2nbsp(). | ||
140 | */ | ||
141 | public function testReverseSpace2nbsp() | ||
142 | { | ||
143 | $text = ' stuff' . PHP_EOL . ' otherstuff and another'; | ||
144 | $processedText = space2nbsp($text); | ||
145 | $reversedText = reverse_space2nbsp($processedText); | ||
146 | $this->assertEquals($text, $reversedText); | ||
147 | } | ||
148 | |||
149 | public function testReverseFeedPermalink() | ||
150 | { | ||
151 | $text = 'Description... '; | ||
152 | $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | ||
153 | $expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)'; | ||
154 | $processedText = reverse_feed_permalink($text); | ||
155 | |||
156 | $this->assertEquals($expected, $processedText); | ||
157 | } | ||
158 | |||
159 | public function testReverseFeedDirectLink() | ||
160 | { | ||
161 | $text = 'Description... '; | ||
162 | $text .= '— <a href="http://domain.tld/?0oc_VQ" title="Direct link">Direct link</a>'; | ||
163 | $expected = 'Description... — [Direct link](http://domain.tld/?0oc_VQ)'; | ||
164 | $processedText = reverse_feed_permalink($text); | ||
165 | |||
166 | $this->assertEquals($expected, $processedText); | ||
167 | } | ||
168 | |||
169 | public function testReverseLastFeedPermalink() | ||
170 | { | ||
171 | $text = 'Description... '; | ||
172 | $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | ||
173 | $expected = $text; | ||
174 | $text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>'; | ||
175 | $expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)'; | ||
176 | $processedText = reverse_feed_permalink($text); | ||
177 | |||
178 | $this->assertEquals($expected, $processedText); | ||
179 | } | ||
180 | |||
181 | public function testReverseNoFeedPermalink() | ||
182 | { | ||
183 | $text = 'Hello! Where are you from?'; | ||
184 | $expected = $text; | ||
185 | $processedText = reverse_feed_permalink($text); | ||
186 | |||
187 | $this->assertEquals($expected, $processedText); | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * Test sanitize_html(). | ||
192 | */ | ||
193 | public function testSanitizeHtml() | ||
194 | { | ||
195 | $input = '< script src="js.js"/>'; | ||
196 | $input .= '< script attr>alert(\'xss\');</script>'; | ||
197 | $input .= '<style> * { display: none }</style>'; | ||
198 | $output = escape($input); | ||
199 | $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>'; | ||
200 | $output .= '<a href="#" attr="tt">link</a>'; | ||
201 | $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>'; | ||
202 | $output .= '<a href="#" attr="tt">link</a>'; | ||
203 | $this->assertEquals($output, sanitize_html($input)); | ||
204 | // Do not touch escaped HTML. | ||
205 | $input = escape($input); | ||
206 | $this->assertEquals($input, sanitize_html($input)); | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Test the no markdown tag. | ||
211 | */ | ||
212 | public function testNoMarkdownTag() | ||
213 | { | ||
214 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | ||
215 | $data = array( | ||
216 | 'links' => array(array( | ||
217 | 'description' => $str, | ||
218 | 'tags' => NO_MD_TAG, | ||
219 | 'taglist' => array(NO_MD_TAG), | ||
220 | )) | ||
221 | ); | ||
222 | |||
223 | $processed = hook_markdown_render_linklist($data, $this->conf); | ||
224 | $this->assertEquals($str, $processed['links'][0]['description']); | ||
225 | |||
226 | $processed = hook_markdown_render_feed($data, $this->conf); | ||
227 | $this->assertEquals($str, $processed['links'][0]['description']); | ||
228 | |||
229 | $data = array( | ||
230 | // Columns data | ||
231 | 'linksToDisplay' => array( | ||
232 | // nth link | ||
233 | 0 => array( | ||
234 | 'formatedDescription' => $str, | ||
235 | 'tags' => NO_MD_TAG, | ||
236 | 'taglist' => array(), | ||
237 | ), | ||
238 | ), | ||
239 | ); | ||
240 | |||
241 | $data = hook_markdown_render_daily($data, $this->conf); | ||
242 | $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']); | ||
243 | } | ||
244 | |||
245 | /** | ||
246 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | ||
247 | */ | ||
248 | public function testNoMarkdownNotExcactlyMatching() | ||
249 | { | ||
250 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | ||
251 | $data = array( | ||
252 | 'links' => array(array( | ||
253 | 'description' => $str, | ||
254 | 'tags' => '.' . NO_MD_TAG, | ||
255 | 'taglist' => array('.'. NO_MD_TAG), | ||
256 | )) | ||
257 | ); | ||
258 | |||
259 | $data = hook_markdown_render_feed($data, $this->conf); | ||
260 | $this->assertContains('<em>', $data['links'][0]['description']); | ||
261 | } | ||
262 | |||
263 | /** | ||
264 | * Make sure that the generated HTML match the reference HTML file. | ||
265 | */ | ||
266 | public function testMarkdownGlobalProcessDescription() | ||
267 | { | ||
268 | $md = file_get_contents('tests/plugins/resources/markdown.md'); | ||
269 | $md = format_description($md); | ||
270 | $html = file_get_contents('tests/plugins/resources/markdown.html'); | ||
271 | |||
272 | $data = process_markdown( | ||
273 | $md, | ||
274 | $this->conf->get('security.markdown_escape', true), | ||
275 | $this->conf->get('security.allowed_protocols') | ||
276 | ); | ||
277 | $this->assertEquals($html, $data . PHP_EOL); | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * Make sure that the HTML tags are escaped. | ||
282 | */ | ||
283 | public function testMarkdownWithHtmlEscape() | ||
284 | { | ||
285 | $md = '**strong** <strong>strong</strong>'; | ||
286 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | ||
287 | $data = array( | ||
288 | 'links' => array( | ||
289 | 0 => array( | ||
290 | 'description' => $md, | ||
291 | ), | ||
292 | ), | ||
293 | ); | ||
294 | $data = hook_markdown_render_linklist($data, $this->conf); | ||
295 | $this->assertEquals($html, $data['links'][0]['description']); | ||
296 | } | ||
297 | |||
298 | /** | ||
299 | * Make sure that the HTML tags aren't escaped with the setting set to false. | ||
300 | */ | ||
301 | public function testMarkdownWithHtmlNoEscape() | ||
302 | { | ||
303 | $this->conf->set('security.markdown_escape', false); | ||
304 | $md = '**strong** <strong>strong</strong>'; | ||
305 | $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>'; | ||
306 | $data = array( | ||
307 | 'links' => array( | ||
308 | 0 => array( | ||
309 | 'description' => $md, | ||
310 | ), | ||
311 | ), | ||
312 | ); | ||
313 | $data = hook_markdown_render_linklist($data, $this->conf); | ||
314 | $this->assertEquals($html, $data['links'][0]['description']); | ||
315 | } | ||
316 | } | ||