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