]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
Merge pull request #10618 from MrPetovan/task/10603-ap-string-mentions
[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
79                 $config = \HTMLPurifier_HTML5Config::createDefault();
80                 $config->set('HTML.Doctype', 'HTML5');
81                 $config->set('Attr.AllowedRel', [
82                         'noreferrer' => true,
83                         'noopener' => true,
84                 ]);
85                 $config->set('Attr.AllowedFrameTargets', [
86                         '_blank' => true,
87                 ]);
88
89                 $this->HTMLPurifier = new \HTMLPurifier($config);
90         }
91
92         public function dataLinks()
93         {
94                 return [
95                         /** @see https://github.com/friendica/friendica/issues/2487 */
96                         'bug-2487-1' => [
97                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
98                                 'assertHTML' => true,
99                         ],
100                         'bug-2487-2' => [
101                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
102                                 'assertHTML' => true,
103                         ],
104                         'bug-2487-3' => [
105                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
106                                 'assertHTML' => true,
107                         ],
108                         'bug-2487-4' => [
109                                 'data' => 'https://mastodon.social/@morevnaproject',
110                                 'assertHTML' => true,
111                         ],
112                         /** @see https://github.com/friendica/friendica/issues/5795 */
113                         'bug-5795' => [
114                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
115                                 'assertHTML' => true,
116                         ],
117                         /** @see https://github.com/friendica/friendica/issues/6095 */
118                         'bug-6095' => [
119                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
120                                 'assertHTML' => true,
121                         ],
122                         'no-protocol' => [
123                                 'data' => 'example.com/path',
124                                 'assertHTML' => false
125                         ],
126                         'wrong-protocol' => [
127                                 'data' => 'ftp://example.com',
128                                 'assertHTML' => false
129                         ],
130                         'wrong-domain-without-path' => [
131                                 'data' => 'http://example',
132                                 'assertHTML' => false
133                         ],
134                         'wrong-domain-with-path' => [
135                                 'data' => 'http://example/path',
136                                 'assertHTML' => false
137                         ],
138                         'bug-6857-domain-start' => [
139                                 'data' => "http://\nexample.com",
140                                 'assertHTML' => false
141                         ],
142                         'bug-6857-domain-end' => [
143                                 'data' => "http://example\n.com",
144                                 'assertHTML' => false
145                         ],
146                         'bug-6857-tld' => [
147                                 'data' => "http://example.\ncom",
148                                 'assertHTML' => false
149                         ],
150                         'bug-6857-end' => [
151                                 'data' => "http://example.com\ntest",
152                                 'assertHTML' => false
153                         ],
154                         'bug-6901' => [
155                                 'data' => "http://example.com<ul>",
156                                 'assertHTML' => false
157                         ],
158                         'bug-7150' => [
159                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
160                                 'assertHTML' => false
161                         ],
162                         'bug-7271-query-string-brackets' => [
163                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
164                                 'assertHTML' => true
165                         ],
166                         'bug-7271-path-brackets' => [
167                                 'data' => 'http://example.com/path/to/file[3].html',
168                                 'assertHTML' => true
169                         ],
170                 ];
171         }
172
173         /**
174          * Test convert different links inside a text
175          *
176          * @dataProvider dataLinks
177          *
178          * @param string $data       The data to text
179          * @param bool   $assertHTML True, if the link is a HTML link (<a href...>...</a>)
180          *
181          * @throws InternalServerErrorException
182          */
183         public function testAutoLinking(string $data, bool $assertHTML)
184         {
185                 $output = BBCode::convert($data);
186                 $assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
187                 if ($assertHTML) {
188                         self::assertEquals($assert, $output);
189                 } else {
190                         self::assertNotEquals($assert, $output);
191                 }
192         }
193
194         public function dataBBCodes()
195         {
196                 return [
197                         'bug-7271-condensed-space' => [
198                                 '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>',
199                                 'text' => '[ol][*] http://example.com/[/ol]',
200                         ],
201                         'bug-7271-condensed-nospace' => [
202                                 '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>',
203                                 'text' => '[ol][*]http://example.com/[/ol]',
204                         ],
205                         'bug-7271-indented-space' => [
206                                 '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>',
207                                 'text' => '[ul]
208 [*] http://example.com/
209 [/ul]',
210                         ],
211                         'bug-7271-indented-nospace' => [
212                                 '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>',
213                                 'text' => '[ul]
214 [*]http://example.com/
215 [/ul]',
216                         ],
217                         'bug-2199-named-size' => [
218                                 'expectedHtml' => '<span style="font-size:xx-large;line-height:normal;">Test text</span>',
219                                 'text' => '[size=xx-large]Test text[/size]',
220                         ],
221                         'bug-2199-numeric-size' => [
222                                 'expectedHtml' => '<span style="font-size:24px;line-height:normal;">Test text</span>',
223                                 'text' => '[size=24]Test text[/size]',
224                         ],
225                         'bug-2199-diaspora-no-named-size' => [
226                                 'expectedHtml' => 'Test text',
227                                 'text' => '[size=xx-large]Test text[/size]',
228                                 'try_oembed' => false,
229                                 // Triggers the diaspora compatible output
230                                 'simpleHtml' => 3,
231                         ],
232                         'bug-2199-diaspora-no-numeric-size' => [
233                                 'expectedHtml' => 'Test text',
234                                 'text' => '[size=24]Test text[/size]',
235                                 'try_oembed' => false,
236                                 // Triggers the diaspora compatible output
237                                 'simpleHtml' => 3,
238                         ],
239                         'bug-7665-audio-tag' => [
240                                 '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>',
241                                 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
242                                 'try_oembed' => true,
243                         ],
244                         'bug-7808-code-lt' => [
245                                 'expectedHtml' => '<code>&lt;</code>',
246                                 'text' => '[code]<[/code]',
247                         ],
248                         'bug-7808-code-gt' => [
249                                 'expectedHtml' => '<code>&gt;</code>',
250                                 'text' => '[code]>[/code]',
251                         ],
252                         'bug-7808-code-amp' => [
253                                 'expectedHtml' => '<code>&amp;</code>',
254                                 'text' => '[code]&[/code]',
255                         ],
256                         'task-8800-pre-spaces-notag' => [
257                                 'expectedHtml' => '[test] Space',
258                                 'text' => '[test] Space',
259                         ],
260                         'task-8800-pre-spaces' => [
261                                 'expectedHtml' => '    Spaces',
262                                 'text' => '[pre]    Spaces[/pre]',
263                         ],
264                         'bug-9611-purify-xss-nobb' => [
265                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
266                                 'text' => '[nobb]<span onmouseover="alert(0)">dare to move your mouse here</span>[/nobb]'
267                         ],
268                         'bug-9611-purify-xss-noparse' => [
269                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
270                                 'text' => '[noparse]<span onmouseover="alert(0)">dare to move your mouse here</span>[/noparse]'
271                         ],
272                         'bug-9611-purify-xss-attributes' => [
273                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
274                                 'text' => '[color="onmouseover=alert(0) style="]dare to move your mouse here[/color]'
275                         ],
276                         'bug-9611-purify-attributes-correct' => [
277                                 'expectedHTML' => '<span style="color:#FFFFFF;">dare to move your mouse here</span>',
278                                 'text' => '[color=FFFFFF]dare to move your mouse here[/color]'
279                         ],
280                         'bug-9639-span-classes' => [
281                                 'expectedHTML' => '<span class="arbitrary classes">Test</span>',
282                                 'text' => '[class=arbitrary classes]Test[/class]',
283                         ],
284                 ];
285         }
286
287         /**
288          * Test convert bbcodes to HTML
289          *
290          * @dataProvider dataBBCodes
291          *
292          * @param string $expectedHtml Expected HTML output
293          * @param string $text         BBCode text
294          * @param bool   $try_oembed   Whether to convert multimedia BBCode tag
295          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
296          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
297          *
298          * @throws InternalServerErrorException
299          */
300         public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
301         {
302                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
303
304                 self::assertEquals($expectedHtml, $actual);
305         }
306
307         public function dataBBCodesToMarkdown()
308         {
309                 return [
310                         'bug-7808-gt' => [
311                                 'expected' => '&gt;`>`',
312                                 'text' => '>[code]>[/code]',
313                         ],
314                         'bug-7808-lt' => [
315                                 'expected' => '&lt;`<`',
316                                 'text' => '<[code]<[/code]',
317                         ],
318                         'bug-7808-amp' => [
319                                 'expected' => '&amp;`&`',
320                                 'text' => '&[code]&[/code]',
321                         ],
322                 ];
323         }
324
325         /**
326          * Test convert bbcodes to Markdown
327          *
328          * @dataProvider dataBBCodesToMarkdown
329          *
330          * @param string $expected Expected Markdown output
331          * @param string $text     BBCode text
332          * @param bool   $for_diaspora
333          *
334          * @throws InternalServerErrorException
335          */
336         public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
337         {
338                 $actual = BBCode::toMarkdown($text, $for_diaspora);
339
340                 self::assertEquals($expected, $actual);
341         }
342 }