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);
80 public function dataLinks()
83 /** @see https://github.com/friendica/friendica/issues/2487 */
85 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
89 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
93 'data' => 'https://friendica.wäckerlin.ch/friendica',
97 'data' => 'https://mastodon.social/@morevnaproject',
100 /** @see https://github.com/friendica/friendica/issues/5795 */
102 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
103 'assertHTML' => true,
105 /** @see https://github.com/friendica/friendica/issues/6095 */
107 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
108 'assertHTML' => true,
111 'data' => 'example.com/path',
112 'assertHTML' => false
114 'wrong-protocol' => [
115 'data' => 'ftp://example.com',
116 'assertHTML' => false
118 'wrong-domain-without-path' => [
119 'data' => 'http://example',
120 'assertHTML' => false
122 'wrong-domain-with-path' => [
123 'data' => 'http://example/path',
124 'assertHTML' => false
126 'bug-6857-domain-start' => [
127 'data' => "http://\nexample.com",
128 'assertHTML' => false
130 'bug-6857-domain-end' => [
131 'data' => "http://example\n.com",
132 'assertHTML' => false
135 'data' => "http://example.\ncom",
136 'assertHTML' => false
139 'data' => "http://example.com\ntest",
140 'assertHTML' => false
143 'data' => "http://example.com<ul>",
144 'assertHTML' => false
147 'data' => html_entity_decode('http://example.com ', ENT_QUOTES, 'UTF-8'),
148 'assertHTML' => false
150 'bug-7271-query-string-brackets' => [
151 'data' => 'https://example.com/search?q=square+brackets+[url]',
154 'bug-7271-path-brackets' => [
155 'data' => 'http://example.com/path/to/file[3].html',
162 * Test convert different links inside a text
164 * @dataProvider dataLinks
166 * @param string $data The data to text
167 * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
169 * @throws InternalServerErrorException
171 public function testAutoLinking(string $data, bool $assertHTML)
173 $output = BBCode::convert($data);
174 $assert = '<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>';
176 self::assertEquals($assert, $output);
178 self::assertNotEquals($assert, $output);
182 public function dataBBCodes()
185 'bug-7271-condensed-space' => [
186 '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>',
187 'text' => '[ol][*] http://example.com/[/ol]',
189 'bug-7271-condensed-nospace' => [
190 '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>',
191 'text' => '[ol][*]http://example.com/[/ol]',
193 'bug-7271-indented-space' => [
194 '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>',
196 [*] http://example.com/
199 'bug-7271-indented-nospace' => [
200 '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>',
202 [*]http://example.com/
205 'bug-2199-named-size' => [
206 'expectedHtml' => '<span style="font-size: xx-large; line-height: initial;">Test text</span>',
207 'text' => '[size=xx-large]Test text[/size]',
209 'bug-2199-numeric-size' => [
210 'expectedHtml' => '<span style="font-size: 24px; line-height: initial;">Test text</span>',
211 'text' => '[size=24]Test text[/size]',
213 'bug-2199-diaspora-no-named-size' => [
214 'expectedHtml' => 'Test text',
215 'text' => '[size=xx-large]Test text[/size]',
216 'try_oembed' => false,
217 // Triggers the diaspora compatible output
220 'bug-2199-diaspora-no-numeric-size' => [
221 'expectedHtml' => 'Test text',
222 'text' => '[size=24]Test text[/size]',
223 'try_oembed' => false,
224 // Triggers the diaspora compatible output
227 'bug-7665-audio-tag' => [
228 'expectedHtml' => '<audio src="http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3" controls="controls"><a href="http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3">http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3</a></audio>',
229 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
230 'try_oembed' => true,
232 'bug-7808-code-lt' => [
233 'expectedHtml' => '<code><</code>',
234 'text' => '[code]<[/code]',
236 'bug-7808-code-gt' => [
237 'expectedHtml' => '<code>></code>',
238 'text' => '[code]>[/code]',
240 'bug-7808-code-amp' => [
241 'expectedHtml' => '<code>&</code>',
242 'text' => '[code]&[/code]',
244 'task-8800-pre-spaces-notag' => [
245 'expectedHtml' => '[test] Space',
246 'text' => '[test] Space',
248 'task-8800-pre-spaces' => [
249 'expectedHtml' => ' Spaces',
250 'text' => '[pre] Spaces[/pre]',
256 * Test convert bbcodes to HTML
258 * @dataProvider dataBBCodes
260 * @param string $expectedHtml Expected HTML output
261 * @param string $text BBCode text
262 * @param bool $try_oembed Whether to convert multimedia BBCode tag
263 * @param int $simpleHtml BBCode::convert method $simple_html parameter value, optional.
264 * @param bool $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
266 * @throws InternalServerErrorException
268 public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
270 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
272 self::assertEquals($expectedHtml, $actual);
275 public function dataBBCodesToMarkdown()
279 'expected' => '>`>`',
280 'text' => '>[code]>[/code]',
283 'expected' => '<`<`',
284 'text' => '<[code]<[/code]',
287 'expected' => '&`&`',
288 'text' => '&[code]&[/code]',
294 * Test convert bbcodes to Markdown
296 * @dataProvider dataBBCodesToMarkdown
298 * @param string $expected Expected Markdown output
299 * @param string $text BBCode text
300 * @param bool $for_diaspora
302 * @throws InternalServerErrorException
304 public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
306 $actual = BBCode::toMarkdown($text, $for_diaspora);
308 self::assertEquals($expected, $actual);