]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
"escapeTags" is finally removed
[friendica.git] / tests / src / Content / Text / BBCodeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\App\BaseURL;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\L10n;
27 use Friendica\Network\HTTPException\InternalServerErrorException;
28 use Friendica\Test\MockedTest;
29 use Friendica\Test\Util\AppMockTrait;
30 use Friendica\Test\Util\VFSTrait;
31 use Mockery;
32
33 class BBCodeTest extends MockedTest
34 {
35         use VFSTrait;
36         use AppMockTrait;
37
38         protected function setUp(): void
39         {
40                 parent::setUp();
41                 $this->setUpVfsDir();
42                 $this->mockApp($this->root);
43                 $this->configMock->shouldReceive('get')
44                         ->with('system', 'remove_multiplicated_lines')
45                         ->andReturn(false);
46                 $this->configMock->shouldReceive('get')
47                         ->with('system', 'no_oembed')
48                         ->andReturn(false);
49                 $this->configMock->shouldReceive('get')
50                         ->with('system', 'allowed_link_protocols')
51                         ->andReturn(null);
52                 $this->configMock->shouldReceive('get')
53                         ->with('system', 'url')
54                         ->andReturn('friendica.local');
55                 $this->configMock->shouldReceive('get')
56                         ->with('system', 'no_smilies')
57                         ->andReturn(false);
58                 $this->configMock->shouldReceive('get')
59                         ->with('system', 'big_emojis')
60                         ->andReturn(false);
61                 $this->configMock->shouldReceive('get')
62                         ->with('system', 'allowed_oembed')
63                         ->andReturn('');
64
65                 $l10nMock = Mockery::mock(L10n::class);
66                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
67                 $this->dice->shouldReceive('create')
68                            ->with(L10n::class)
69                            ->andReturn($l10nMock);
70
71                 $baseUrlMock = Mockery::mock(BaseURL::class);
72                 $baseUrlMock->shouldReceive('get')->withAnyArgs()->andReturn('friendica.local');
73                 $this->dice->shouldReceive('create')
74                            ->with(BaseURL::class)
75                            ->andReturn($baseUrlMock);
76                 $baseUrlMock->shouldReceive('getHostname')->withNoArgs()->andReturn('friendica.local');
77                 $baseUrlMock->shouldReceive('getUrlPath')->withNoArgs()->andReturn('');
78                 $baseUrlMock->shouldReceive('__toString')->withNoArgs()->andReturn('friendica.local');
79
80                 $config = \HTMLPurifier_HTML5Config::createDefault();
81                 $config->set('HTML.Doctype', 'HTML5');
82                 $config->set('Attr.AllowedRel', [
83                         'noreferrer' => true,
84                         'noopener' => true,
85                 ]);
86                 $config->set('Attr.AllowedFrameTargets', [
87                         '_blank' => true,
88                 ]);
89
90                 $this->HTMLPurifier = new \HTMLPurifier($config);
91         }
92
93         public function dataLinks()
94         {
95                 return [
96                         /** @see https://github.com/friendica/friendica/issues/2487 */
97                         'bug-2487-1' => [
98                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
99                                 'assertHTML' => true,
100                         ],
101                         'bug-2487-2' => [
102                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
103                                 'assertHTML' => true,
104                         ],
105                         'bug-2487-3' => [
106                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
107                                 'assertHTML' => true,
108                         ],
109                         'bug-2487-4' => [
110                                 'data' => 'https://mastodon.social/@morevnaproject',
111                                 'assertHTML' => true,
112                         ],
113                         /** @see https://github.com/friendica/friendica/issues/5795 */
114                         'bug-5795' => [
115                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
116                                 'assertHTML' => true,
117                         ],
118                         /** @see https://github.com/friendica/friendica/issues/6095 */
119                         'bug-6095' => [
120                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
121                                 'assertHTML' => true,
122                         ],
123                         'no-protocol' => [
124                                 'data' => 'example.com/path',
125                                 'assertHTML' => false
126                         ],
127                         'wrong-protocol' => [
128                                 'data' => 'ftp://example.com',
129                                 'assertHTML' => false
130                         ],
131                         'wrong-domain-without-path' => [
132                                 'data' => 'http://example',
133                                 'assertHTML' => false
134                         ],
135                         'wrong-domain-with-path' => [
136                                 'data' => 'http://example/path',
137                                 'assertHTML' => false
138                         ],
139                         'bug-6857-domain-start' => [
140                                 'data' => "http://\nexample.com",
141                                 'assertHTML' => false
142                         ],
143                         'bug-6857-domain-end' => [
144                                 'data' => "http://example\n.com",
145                                 'assertHTML' => false
146                         ],
147                         'bug-6857-tld' => [
148                                 'data' => "http://example.\ncom",
149                                 'assertHTML' => false
150                         ],
151                         'bug-6857-end' => [
152                                 'data' => "http://example.com\ntest",
153                                 'assertHTML' => false
154                         ],
155                         'bug-6901' => [
156                                 'data' => "http://example.com<ul>",
157                                 'assertHTML' => false
158                         ],
159                         'bug-7150' => [
160                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
161                                 'assertHTML' => false
162                         ],
163                         'bug-7271-query-string-brackets' => [
164                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
165                                 'assertHTML' => true
166                         ],
167                         'bug-7271-path-brackets' => [
168                                 'data' => 'http://example.com/path/to/file[3].html',
169                                 'assertHTML' => true
170                         ],
171                 ];
172         }
173
174         /**
175          * Test convert different links inside a text
176          *
177          * @dataProvider dataLinks
178          *
179          * @param string $data       The data to text
180          * @param bool   $assertHTML True, if the link is a HTML link (<a href...>...</a>)
181          *
182          * @throws InternalServerErrorException
183          */
184         public function testAutoLinking(string $data, bool $assertHTML)
185         {
186                 $output = BBCode::convert($data);
187                 $assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
188                 if ($assertHTML) {
189                         self::assertEquals($assert, $output);
190                 } else {
191                         self::assertNotEquals($assert, $output);
192                 }
193         }
194
195         public function dataBBCodes()
196         {
197                 return [
198                         'bug-7271-condensed-space' => [
199                                 '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>',
200                                 'text' => '[ol][*] http://example.com/[/ol]',
201                         ],
202                         'bug-7271-condensed-nospace' => [
203                                 '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>',
204                                 'text' => '[ol][*]http://example.com/[/ol]',
205                         ],
206                         'bug-7271-indented-space' => [
207                                 '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>',
208                                 'text' => '[ul]
209 [*] http://example.com/
210 [/ul]',
211                         ],
212                         'bug-7271-indented-nospace' => [
213                                 '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>',
214                                 'text' => '[ul]
215 [*]http://example.com/
216 [/ul]',
217                         ],
218                         'bug-2199-named-size' => [
219                                 'expectedHtml' => '<span style="font-size:xx-large;line-height:normal;">Test text</span>',
220                                 'text' => '[size=xx-large]Test text[/size]',
221                         ],
222                         'bug-2199-numeric-size' => [
223                                 'expectedHtml' => '<span style="font-size:24px;line-height:normal;">Test text</span>',
224                                 'text' => '[size=24]Test text[/size]',
225                         ],
226                         'bug-2199-diaspora-no-named-size' => [
227                                 'expectedHtml' => 'Test text',
228                                 'text' => '[size=xx-large]Test text[/size]',
229                                 'try_oembed' => false,
230                                 // Triggers the diaspora compatible output
231                                 'simpleHtml' => BBCode::DIASPORA,
232                         ],
233                         'bug-2199-diaspora-no-numeric-size' => [
234                                 'expectedHtml' => 'Test text',
235                                 'text' => '[size=24]Test text[/size]',
236                                 'try_oembed' => false,
237                                 // Triggers the diaspora compatible output
238                                 'simpleHtml' => BBCode::DIASPORA,
239                         ],
240                         'bug-7665-audio-tag' => [
241                                 '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>',
242                                 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
243                                 'try_oembed' => true,
244                         ],
245                         'bug-7808-code-lt' => [
246                                 'expectedHtml' => '<code>&lt;</code>',
247                                 'text' => '[code]<[/code]',
248                         ],
249                         'bug-7808-code-gt' => [
250                                 'expectedHtml' => '<code>&gt;</code>',
251                                 'text' => '[code]>[/code]',
252                         ],
253                         'bug-7808-code-amp' => [
254                                 'expectedHtml' => '<code>&amp;</code>',
255                                 'text' => '[code]&[/code]',
256                         ],
257                         'task-8800-pre-spaces-notag' => [
258                                 'expectedHtml' => '[test] Space',
259                                 'text' => '[test] Space',
260                         ],
261                         'task-8800-pre-spaces' => [
262                                 'expectedHtml' => '    Spaces',
263                                 'text' => '[pre]    Spaces[/pre]',
264                         ],
265                         'bug-9611-purify-xss-nobb' => [
266                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
267                                 'text' => '[nobb]<span onmouseover="alert(0)">dare to move your mouse here</span>[/nobb]'
268                         ],
269                         'bug-9611-purify-xss-noparse' => [
270                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
271                                 'text' => '[noparse]<span onmouseover="alert(0)">dare to move your mouse here</span>[/noparse]'
272                         ],
273                         'bug-9611-purify-xss-attributes' => [
274                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
275                                 'text' => '[color="onmouseover=alert(0) style="]dare to move your mouse here[/color]'
276                         ],
277                         'bug-9611-purify-attributes-correct' => [
278                                 'expectedHTML' => '<span style="color:#FFFFFF;">dare to move your mouse here</span>',
279                                 'text' => '[color=FFFFFF]dare to move your mouse here[/color]'
280                         ],
281                         'bug-9639-span-classes' => [
282                                 'expectedHTML' => '<span class="arbitrary classes">Test</span>',
283                                 'text' => '[class=arbitrary classes]Test[/class]',
284                         ],
285                         'bug-10772-duplicated-links' => [
286                                 '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',
287                                 'text' => "Jetzt wird mir klar, warum Kapitalisten jedes Mal durchdrehen wenn Marx und das Kapital ins Gespräch kommt. Soziopathen.
288 Karl Marx - Die ursprüngliche Akkumulation
289 [url=https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation]https://wohlstandfueralle.podigee.io/107-urspruengliche-akkumulation[/url]
290 #[url=https://horche.demkontinuum.de/search?tag=Podcast]Podcast[/url] #[url=https://horche.demkontinuum.de/search?tag=Kapitalismus]Kapitalismus[/url]
291 [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]",
292                                 'try_oembed' => false,
293                                 'simpleHtml' => BBCode::TWITTER,
294                         ],
295                         'task-10886-deprecate-class' => [
296                                 'expectedHTML' => '<span class="mastodon emoji"><img src="https://fedi.underscore.world/emoji/custom/custom/heart_nb.png" alt=":heart_nb:" title=":heart_nb:"></span>',
297                                 'text' => '[emoji=https://fedi.underscore.world/emoji/custom/custom/heart_nb.png]:heart_nb:[/emoji]',
298                         ]
299                 ];
300         }
301
302         /**
303          * Test convert bbcodes to HTML
304          *
305          * @dataProvider dataBBCodes
306          *
307          * @param string $expectedHtml Expected HTML output
308          * @param string $text         BBCode text
309          * @param bool   $try_oembed   Whether to convert multimedia BBCode tag
310          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
311          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
312          *
313          * @throws InternalServerErrorException
314          */
315         public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
316         {
317                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
318
319                 self::assertEquals($expectedHtml, $actual);
320         }
321
322         public function dataBBCodesToMarkdown()
323         {
324                 return [
325                         'bug-7808-gt' => [
326                                 'expected' => '&gt;`>`',
327                                 'text' => '>[code]>[/code]',
328                         ],
329                         'bug-7808-lt' => [
330                                 'expected' => '&lt;`<`',
331                                 'text' => '<[code]<[/code]',
332                         ],
333                         'bug-7808-amp' => [
334                                 'expected' => '&amp;`&`',
335                                 'text' => '&[code]&[/code]',
336                         ],
337                 ];
338         }
339
340         /**
341          * Test convert bbcodes to Markdown
342          *
343          * @dataProvider dataBBCodesToMarkdown
344          *
345          * @param string $expected Expected Markdown output
346          * @param string $text     BBCode text
347          * @param bool   $for_diaspora
348          *
349          * @throws InternalServerErrorException
350          */
351         public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
352         {
353                 $actual = BBCode::toMarkdown($text, $for_diaspora);
354
355                 self::assertEquals($expected, $actual);
356         }
357
358         public function dataExpandTags()
359         {
360                 return [
361                         'bug-10692-non-word' => [
362                                 '[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]',
363                                 '[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]',
364                         ],
365                         'bug-10692-start-line' => [
366                                 '#[url=https://friendica.local/search?tag=L160]L160[/url]',
367                                 '#L160',
368                         ]
369                 ];
370         }
371
372         /**
373          * @dataProvider dataExpandTags
374          *
375          * @param string $expected Expected BBCode output
376          * @param string $text     Input text
377          */
378         public function testExpandTags(string $expected, string $text)
379         {
380                 $actual = BBCode::expandTags($text);
381
382                 self::assertEquals($expected, $actual);
383         }
384 }