3 * @copyright Copyright (C) 2020, Friendica
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()
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', 'itemcache_duration')
57 $this->configMock->shouldReceive('get')
58 ->with('system', 'url')
59 ->andReturn('friendica.local');
60 $this->configMock->shouldReceive('get')
61 ->with('system', 'no_smilies')
63 $this->configMock->shouldReceive('get')
64 ->with('system', 'big_emojis')
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);
79 $config = \HTMLPurifier_HTML5Config::createDefault();
80 $config->set('HTML.Doctype', 'HTML5');
81 $config->set('Attr.AllowedRel', [
85 $config->set('Attr.AllowedFrameTargets', [
89 $this->HTMLPurifier = new \HTMLPurifier($config);
92 public function dataLinks()
95 /** @see https://github.com/friendica/friendica/issues/2487 */
97 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
101 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
102 'assertHTML' => true,
105 'data' => 'https://friendica.wäckerlin.ch/friendica',
106 'assertHTML' => true,
109 'data' => 'https://mastodon.social/@morevnaproject',
110 'assertHTML' => true,
112 /** @see https://github.com/friendica/friendica/issues/5795 */
114 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
115 'assertHTML' => true,
117 /** @see https://github.com/friendica/friendica/issues/6095 */
119 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
120 'assertHTML' => true,
123 'data' => 'example.com/path',
124 'assertHTML' => false
126 'wrong-protocol' => [
127 'data' => 'ftp://example.com',
128 'assertHTML' => false
130 'wrong-domain-without-path' => [
131 'data' => 'http://example',
132 'assertHTML' => false
134 'wrong-domain-with-path' => [
135 'data' => 'http://example/path',
136 'assertHTML' => false
138 'bug-6857-domain-start' => [
139 'data' => "http://\nexample.com",
140 'assertHTML' => false
142 'bug-6857-domain-end' => [
143 'data' => "http://example\n.com",
144 'assertHTML' => false
147 'data' => "http://example.\ncom",
148 'assertHTML' => false
151 'data' => "http://example.com\ntest",
152 'assertHTML' => false
155 'data' => "http://example.com<ul>",
156 'assertHTML' => false
159 'data' => html_entity_decode('http://example.com ', ENT_QUOTES, 'UTF-8'),
160 'assertHTML' => false
162 'bug-7271-query-string-brackets' => [
163 'data' => 'https://example.com/search?q=square+brackets+[url]',
166 'bug-7271-path-brackets' => [
167 'data' => 'http://example.com/path/to/file[3].html',
174 * Test convert different links inside a text
176 * @dataProvider dataLinks
178 * @param string $data The data to text
179 * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
181 * @throws InternalServerErrorException
183 public function testAutoLinking(string $data, bool $assertHTML)
185 $output = BBCode::convert($data);
186 $assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
188 self::assertEquals($assert, $output);
190 self::assertNotEquals($assert, $output);
194 public function dataBBCodes()
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]',
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]',
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>',
208 [*] http://example.com/
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>',
214 [*]http://example.com/
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]',
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]',
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
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
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,
244 'bug-7808-code-lt' => [
245 'expectedHtml' => '<code><</code>',
246 'text' => '[code]<[/code]',
248 'bug-7808-code-gt' => [
249 'expectedHtml' => '<code>></code>',
250 'text' => '[code]>[/code]',
252 'bug-7808-code-amp' => [
253 'expectedHtml' => '<code>&</code>',
254 'text' => '[code]&[/code]',
256 'task-8800-pre-spaces-notag' => [
257 'expectedHtml' => '[test] Space',
258 'text' => '[test] Space',
260 'task-8800-pre-spaces' => [
261 'expectedHtml' => ' Spaces',
262 'text' => '[pre] Spaces[/pre]',
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]'
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]'
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]'
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]'
284 * Test convert bbcodes to HTML
286 * @dataProvider dataBBCodes
288 * @param string $expectedHtml Expected HTML output
289 * @param string $text BBCode text
290 * @param bool $try_oembed Whether to convert multimedia BBCode tag
291 * @param int $simpleHtml BBCode::convert method $simple_html parameter value, optional.
292 * @param bool $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
294 * @throws InternalServerErrorException
296 public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
298 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
300 self::assertEquals($expectedHtml, $actual);
303 public function dataBBCodesToMarkdown()
307 'expected' => '>`>`',
308 'text' => '>[code]>[/code]',
311 'expected' => '<`<`',
312 'text' => '<[code]<[/code]',
315 'expected' => '&`&`',
316 'text' => '&[code]&[/code]',
322 * Test convert bbcodes to Markdown
324 * @dataProvider dataBBCodesToMarkdown
326 * @param string $expected Expected Markdown output
327 * @param string $text BBCode text
328 * @param bool $for_diaspora
330 * @throws InternalServerErrorException
332 public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
334 $actual = BBCode::toMarkdown($text, $for_diaspora);
336 self::assertEquals($expected, $actual);