]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
Merge pull request #11129 from urbalazs/copyright-2022
[friendica.git] / tests / src / Content / Text / BBCodeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Content\Text;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\DI;
26 use Friendica\Network\HTTPException\InternalServerErrorException;
27 use Friendica\Test\FixtureTest;
28
29 class BBCodeTest extends FixtureTest
30 {
31         protected function setUp(): void
32         {
33                 parent::setUp();
34                 DI::config()->set('system', 'remove_multiplicated_lines', false);
35                 DI::config()->set('system', 'no_oembed', false);
36                 DI::config()->set('system', 'allowed_link_protocols', []);
37                 DI::config()->set('system', 'url', 'friendica.local');
38                 DI::config()->set('system', 'no_smilies', false);
39                 DI::config()->set('system', 'big_emojis', false);
40                 DI::config()->set('system', 'allowed_oembed', '');
41
42                 DI::baseUrl()->save('friendica.local', DI::baseUrl()::SSL_POLICY_FULL, '');
43
44                 $config = \HTMLPurifier_HTML5Config::createDefault();
45                 $config->set('HTML.Doctype', 'HTML5');
46                 $config->set('Attr.AllowedRel', [
47                         'noreferrer' => true,
48                         'noopener' => true,
49                 ]);
50                 $config->set('Attr.AllowedFrameTargets', [
51                         '_blank' => true,
52                 ]);
53
54                 $this->HTMLPurifier = new \HTMLPurifier($config);
55         }
56
57         public function dataLinks()
58         {
59                 return [
60                         /** @see https://github.com/friendica/friendica/issues/2487 */
61                         'bug-2487-1' => [
62                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
63                                 'assertHTML' => true,
64                         ],
65                         'bug-2487-2' => [
66                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
67                                 'assertHTML' => true,
68                         ],
69                         'bug-2487-3' => [
70                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
71                                 'assertHTML' => true,
72                         ],
73                         'bug-2487-4' => [
74                                 'data' => 'https://mastodon.social/@morevnaproject',
75                                 'assertHTML' => true,
76                         ],
77                         /** @see https://github.com/friendica/friendica/issues/5795 */
78                         'bug-5795' => [
79                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
80                                 'assertHTML' => true,
81                         ],
82                         /** @see https://github.com/friendica/friendica/issues/6095 */
83                         'bug-6095' => [
84                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
85                                 'assertHTML' => true,
86                         ],
87                         'no-protocol' => [
88                                 'data' => 'example.com/path',
89                                 'assertHTML' => false
90                         ],
91                         'wrong-protocol' => [
92                                 'data' => 'ftp://example.com',
93                                 'assertHTML' => false
94                         ],
95                         'wrong-domain-without-path' => [
96                                 'data' => 'http://example',
97                                 'assertHTML' => false
98                         ],
99                         'wrong-domain-with-path' => [
100                                 'data' => 'http://example/path',
101                                 'assertHTML' => false
102                         ],
103                         'bug-6857-domain-start' => [
104                                 'data' => "http://\nexample.com",
105                                 'assertHTML' => false
106                         ],
107                         'bug-6857-domain-end' => [
108                                 'data' => "http://example\n.com",
109                                 'assertHTML' => false
110                         ],
111                         'bug-6857-tld' => [
112                                 'data' => "http://example.\ncom",
113                                 'assertHTML' => false
114                         ],
115                         'bug-6857-end' => [
116                                 'data' => "http://example.com\ntest",
117                                 'assertHTML' => false
118                         ],
119                         'bug-6901' => [
120                                 'data' => "http://example.com<ul>",
121                                 'assertHTML' => false
122                         ],
123                         'bug-7150' => [
124                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
125                                 'assertHTML' => false
126                         ],
127                         'bug-7271-query-string-brackets' => [
128                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
129                                 'assertHTML' => true
130                         ],
131                         'bug-7271-path-brackets' => [
132                                 'data' => 'http://example.com/path/to/file[3].html',
133                                 'assertHTML' => true
134                         ],
135                 ];
136         }
137
138         /**
139          * Test convert different links inside a text
140          *
141          * @dataProvider dataLinks
142          *
143          * @param string $data       The data to text
144          * @param bool   $assertHTML True, if the link is a HTML link (<a href...>...</a>)
145          *
146          * @throws InternalServerErrorException
147          */
148         public function testAutoLinking(string $data, bool $assertHTML)
149         {
150                 $output = BBCode::convert($data);
151                 $assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
152                 if ($assertHTML) {
153                         self::assertEquals($assert, $output);
154                 } else {
155                         self::assertNotEquals($assert, $output);
156                 }
157         }
158
159         public function dataBBCodes()
160         {
161                 return [
162                         'bug-7271-condensed-space' => [
163                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type:decimal;"><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
164                                 'text' => '[ol][*] http://example.com/[/ol]',
165                         ],
166                         'bug-7271-condensed-nospace' => [
167                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type:decimal;"><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
168                                 'text' => '[ol][*]http://example.com/[/ol]',
169                         ],
170                         'bug-7271-indented-space' => [
171                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type:circle;"><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
172                                 'text' => '[ul]
173 [*] http://example.com/
174 [/ul]',
175                         ],
176                         'bug-7271-indented-nospace' => [
177                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type:circle;"><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
178                                 'text' => '[ul]
179 [*]http://example.com/
180 [/ul]',
181                         ],
182                         'bug-2199-named-size' => [
183                                 'expectedHtml' => '<span style="font-size:xx-large;line-height:normal;">Test text</span>',
184                                 'text' => '[size=xx-large]Test text[/size]',
185                         ],
186                         'bug-2199-numeric-size' => [
187                                 'expectedHtml' => '<span style="font-size:24px;line-height:normal;">Test text</span>',
188                                 'text' => '[size=24]Test text[/size]',
189                         ],
190                         'bug-2199-diaspora-no-named-size' => [
191                                 'expectedHtml' => 'Test text',
192                                 'text' => '[size=xx-large]Test text[/size]',
193                                 'try_oembed' => false,
194                                 // Triggers the diaspora compatible output
195                                 'simpleHtml' => BBCode::DIASPORA,
196                         ],
197                         'bug-2199-diaspora-no-numeric-size' => [
198                                 'expectedHtml' => 'Test text',
199                                 'text' => '[size=24]Test text[/size]',
200                                 'try_oembed' => false,
201                                 // Triggers the diaspora compatible output
202                                 'simpleHtml' => BBCode::DIASPORA,
203                         ],
204                         'bug-7665-audio-tag' => [
205                                 'expectedHtml' => '<audio src="http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3" controls><a href="http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3">http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3</a></audio>',
206                                 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
207                                 'try_oembed' => true,
208                         ],
209                         'bug-7808-code-lt' => [
210                                 'expectedHtml' => '<code>&lt;</code>',
211                                 'text' => '[code]<[/code]',
212                         ],
213                         'bug-7808-code-gt' => [
214                                 'expectedHtml' => '<code>&gt;</code>',
215                                 'text' => '[code]>[/code]',
216                         ],
217                         'bug-7808-code-amp' => [
218                                 'expectedHtml' => '<code>&amp;</code>',
219                                 'text' => '[code]&[/code]',
220                         ],
221                         'task-8800-pre-spaces-notag' => [
222                                 'expectedHtml' => '[test] Space',
223                                 'text' => '[test] Space',
224                         ],
225                         'task-8800-pre-spaces' => [
226                                 'expectedHtml' => '    Spaces',
227                                 'text' => '[pre]    Spaces[/pre]',
228                         ],
229                         'bug-9611-purify-xss-nobb' => [
230                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
231                                 'text' => '[nobb]<span onmouseover="alert(0)">dare to move your mouse here</span>[/nobb]'
232                         ],
233                         'bug-9611-purify-xss-noparse' => [
234                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
235                                 'text' => '[noparse]<span onmouseover="alert(0)">dare to move your mouse here</span>[/noparse]'
236                         ],
237                         'bug-9611-purify-xss-attributes' => [
238                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
239                                 'text' => '[color="onmouseover=alert(0) style="]dare to move your mouse here[/color]'
240                         ],
241                         'bug-9611-purify-attributes-correct' => [
242                                 'expectedHTML' => '<span style="color:#FFFFFF;">dare to move your mouse here</span>',
243                                 'text' => '[color=FFFFFF]dare to move your mouse here[/color]'
244                         ],
245                         'bug-9639-span-classes' => [
246                                 'expectedHTML' => '<span class="arbitrary classes">Test</span>',
247                                 'text' => '[class=arbitrary classes]Test[/class]',
248                         ],
249                         'bug-10772-duplicated-links' => [
250                                 'expectedHTML' => 'Jetzt wird mir klar, warum Kapitalisten jedes Mal durchdrehen wenn Marx und das Kapital ins Gespräch kommt. Soziopathen.<br>Karl Marx - Die ursprüngliche Akkumulation<br><a href="https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation" target="_blank" rel="noopener noreferrer">https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation</a><br>#Podcast #Kapitalismus',
251                                 'text' => "Jetzt wird mir klar, warum Kapitalisten jedes Mal durchdrehen wenn Marx und das Kapital ins Gespräch kommt. Soziopathen.
252 Karl Marx - Die ursprüngliche Akkumulation
253 [url=https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation]https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation[/url]
254 #[url=https://horche.demkontinuum.de/search?tag=Podcast]Podcast[/url] #[url=https://horche.demkontinuum.de/search?tag=Kapitalismus]Kapitalismus[/url]
255 [attachment type='link' url='https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation' title='Ep. 107: Karl Marx #8 - Die urspr&uuml;ngliche Akkumulation' publisher_name='Wohlstand f&uuml;r Alle' preview='https://images.podigee-cdn.net/0x,s6LXshYO7uhG23H431B30t4hxj1bQuzlTsUlze0F_-H8=/https://cdn.podigee.com/uploads/u8126/bd5fe4f4-38b7-4f3f-b269-6a0080144635.jpg']Wie der Kapitalismus funktioniert und inwieweit Menschen darin ausgebeutet werden, haben wir bereits besprochen. Immer wieder verweisen wir auch darauf, dass der Kapitalismus nicht immer schon existierte, sondern historisiert werden muss.[/attachment]",
256                                 'try_oembed' => false,
257                                 'simpleHtml' => BBCode::TWITTER,
258                         ],
259                         'task-10886-deprecate-class' => [
260                                 'expectedHTML' => '<span class="mastodon emoji"><img src="https://fedi.underscore.world/emoji/custom/custom/heart_nb.png" alt=":heart_nb:" title=":heart_nb:"></span>',
261                                 'text' => '[emoji=https://fedi.underscore.world/emoji/custom/custom/heart_nb.png]:heart_nb:[/emoji]',
262                         ]
263                 ];
264         }
265
266         /**
267          * Test convert bbcodes to HTML
268          *
269          * @dataProvider dataBBCodes
270          *
271          * @param string $expectedHtml Expected HTML output
272          * @param string $text         BBCode text
273          * @param bool   $try_oembed   Whether to convert multimedia BBCode tag
274          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
275          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
276          *
277          * @throws InternalServerErrorException
278          */
279         public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
280         {
281                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
282
283                 self::assertEquals($expectedHtml, $actual);
284         }
285
286         public function dataBBCodesToMarkdown()
287         {
288                 return [
289                         'bug-7808-gt' => [
290                                 'expected' => '&gt;`>`',
291                                 'text' => '>[code]>[/code]',
292                         ],
293                         'bug-7808-lt' => [
294                                 'expected' => '&lt;`<`',
295                                 'text' => '<[code]<[/code]',
296                         ],
297                         'bug-7808-amp' => [
298                                 'expected' => '&amp;`&`',
299                                 'text' => '&[code]&[/code]',
300                         ],
301                 ];
302         }
303
304         /**
305          * Test convert bbcodes to Markdown
306          *
307          * @dataProvider dataBBCodesToMarkdown
308          *
309          * @param string $expected Expected Markdown output
310          * @param string $text     BBCode text
311          * @param bool   $for_diaspora
312          *
313          * @throws InternalServerErrorException
314          */
315         public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
316         {
317                 $actual = BBCode::toMarkdown($text, $for_diaspora);
318
319                 self::assertEquals($expected, $actual);
320         }
321
322         public function dataExpandTags()
323         {
324                 return [
325                         'bug-10692-non-word' => [
326                                 '[url=https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160]https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160[/url]',
327                                 '[url=https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160]https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160[/url]',
328                         ],
329                         'bug-10692-start-line' => [
330                                 '#[url=https://friendica.local/search?tag=L160]L160[/url]',
331                                 '#L160',
332                         ]
333                 ];
334         }
335
336         /**
337          * @dataProvider dataExpandTags
338          *
339          * @param string $expected Expected BBCode output
340          * @param string $text     Input text
341          */
342         public function testExpandTags(string $expected, string $text)
343         {
344                 $actual = BBCode::expandTags($text);
345
346                 self::assertEquals($expected, $actual);
347         }
348 }