3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Content\Text;
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;
33 class BBCodeTest extends MockedTest
38 protected function setUp(): void
42 $this->mockApp($this->root);
43 $this->app->videowidth = 425;
44 $this->app->videoheight = 350;
45 $this->configMock->shouldReceive('get')
46 ->with('system', 'remove_multiplicated_lines')
48 $this->configMock->shouldReceive('get')
49 ->with('system', 'no_oembed')
51 $this->configMock->shouldReceive('get')
52 ->with('system', 'allowed_link_protocols')
54 $this->configMock->shouldReceive('get')
55 ->with('system', 'url')
56 ->andReturn('friendica.local');
57 $this->configMock->shouldReceive('get')
58 ->with('system', 'no_smilies')
60 $this->configMock->shouldReceive('get')
61 ->with('system', 'big_emojis')
63 $this->configMock->shouldReceive('get')
64 ->with('system', 'allowed_oembed')
67 $l10nMock = Mockery::mock(L10n::class);
68 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
69 $this->dice->shouldReceive('create')
71 ->andReturn($l10nMock);
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('');
81 $config = \HTMLPurifier_HTML5Config::createDefault();
82 $config->set('HTML.Doctype', 'HTML5');
83 $config->set('Attr.AllowedRel', [
87 $config->set('Attr.AllowedFrameTargets', [
91 $this->HTMLPurifier = new \HTMLPurifier($config);
94 public function dataLinks()
97 /** @see https://github.com/friendica/friendica/issues/2487 */
99 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
100 'assertHTML' => true,
103 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
104 'assertHTML' => true,
107 'data' => 'https://friendica.wäckerlin.ch/friendica',
108 'assertHTML' => true,
111 'data' => 'https://mastodon.social/@morevnaproject',
112 'assertHTML' => true,
114 /** @see https://github.com/friendica/friendica/issues/5795 */
116 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
117 'assertHTML' => true,
119 /** @see https://github.com/friendica/friendica/issues/6095 */
121 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
122 'assertHTML' => true,
125 'data' => 'example.com/path',
126 'assertHTML' => false
128 'wrong-protocol' => [
129 'data' => 'ftp://example.com',
130 'assertHTML' => false
132 'wrong-domain-without-path' => [
133 'data' => 'http://example',
134 'assertHTML' => false
136 'wrong-domain-with-path' => [
137 'data' => 'http://example/path',
138 'assertHTML' => false
140 'bug-6857-domain-start' => [
141 'data' => "http://\nexample.com",
142 'assertHTML' => false
144 'bug-6857-domain-end' => [
145 'data' => "http://example\n.com",
146 'assertHTML' => false
149 'data' => "http://example.\ncom",
150 'assertHTML' => false
153 'data' => "http://example.com\ntest",
154 'assertHTML' => false
157 'data' => "http://example.com<ul>",
158 'assertHTML' => false
161 'data' => html_entity_decode('http://example.com ', ENT_QUOTES, 'UTF-8'),
162 'assertHTML' => false
164 'bug-7271-query-string-brackets' => [
165 'data' => 'https://example.com/search?q=square+brackets+[url]',
168 'bug-7271-path-brackets' => [
169 'data' => 'http://example.com/path/to/file[3].html',
176 * Test convert different links inside a text
178 * @dataProvider dataLinks
180 * @param string $data The data to text
181 * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
183 * @throws InternalServerErrorException
185 public function testAutoLinking(string $data, bool $assertHTML)
187 $output = BBCode::convert($data);
188 $assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
190 self::assertEquals($assert, $output);
192 self::assertNotEquals($assert, $output);
196 public function dataBBCodes()
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]',
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]',
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>',
210 [*] http://example.com/
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>',
216 [*]http://example.com/
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]',
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]',
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
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
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,
246 'bug-7808-code-lt' => [
247 'expectedHtml' => '<code><</code>',
248 'text' => '[code]<[/code]',
250 'bug-7808-code-gt' => [
251 'expectedHtml' => '<code>></code>',
252 'text' => '[code]>[/code]',
254 'bug-7808-code-amp' => [
255 'expectedHtml' => '<code>&</code>',
256 'text' => '[code]&[/code]',
258 'task-8800-pre-spaces-notag' => [
259 'expectedHtml' => '[test] Space',
260 'text' => '[test] Space',
262 'task-8800-pre-spaces' => [
263 'expectedHtml' => ' Spaces',
264 'text' => '[pre] Spaces[/pre]',
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]'
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]'
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]'
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]'
282 'bug-9639-span-classes' => [
283 'expectedHTML' => '<span class="arbitrary classes">Test</span>',
284 'text' => '[class=arbitrary classes]Test[/class]',
290 * Test convert bbcodes to HTML
292 * @dataProvider dataBBCodes
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.
300 * @throws InternalServerErrorException
302 public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
304 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
306 self::assertEquals($expectedHtml, $actual);
309 public function dataBBCodesToMarkdown()
313 'expected' => '>`>`',
314 'text' => '>[code]>[/code]',
317 'expected' => '<`<`',
318 'text' => '<[code]<[/code]',
321 'expected' => '&`&`',
322 'text' => '&[code]&[/code]',
328 * Test convert bbcodes to Markdown
330 * @dataProvider dataBBCodesToMarkdown
332 * @param string $expected Expected Markdown output
333 * @param string $text BBCode text
334 * @param bool $for_diaspora
336 * @throws InternalServerErrorException
338 public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
340 $actual = BBCode::toMarkdown($text, $for_diaspora);
342 self::assertEquals($expected, $actual);