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\Test\MockedTest;
28 use Friendica\Test\Util\AppMockTrait;
29 use Friendica\Test\Util\VFSTrait;
31 class BBCodeTest extends MockedTest
36 protected function setUp()
40 $this->mockApp($this->root);
41 $this->app->videowidth = 425;
42 $this->app->videoheight = 350;
43 $this->configMock->shouldReceive('get')
44 ->with('system', 'remove_multiplicated_lines')
46 $this->configMock->shouldReceive('get')
47 ->with('system', 'no_oembed')
49 $this->configMock->shouldReceive('get')
50 ->with('system', 'allowed_link_protocols')
52 $this->configMock->shouldReceive('get')
53 ->with('system', 'itemcache_duration')
55 $this->configMock->shouldReceive('get')
56 ->with('system', 'url')
57 ->andReturn('friendica.local');
58 $this->configMock->shouldReceive('get')
59 ->with('system', 'no_smilies')
61 $this->configMock->shouldReceive('get')
62 ->with('system', 'big_emojis')
65 $l10nMock = \Mockery::mock(L10n::class);
66 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
67 $this->dice->shouldReceive('create')
69 ->andReturn($l10nMock);
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);
78 public function dataLinks()
81 /** @see https://github.com/friendica/friendica/issues/2487 */
83 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
87 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
91 'data' => 'https://friendica.wäckerlin.ch/friendica',
95 'data' => 'https://mastodon.social/@morevnaproject',
98 /** @see https://github.com/friendica/friendica/issues/5795 */
100 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
101 'assertHTML' => true,
103 /** @see https://github.com/friendica/friendica/issues/6095 */
105 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
106 'assertHTML' => true,
109 'data' => 'example.com/path',
110 'assertHTML' => false
112 'wrong-protocol' => [
113 'data' => 'ftp://example.com',
114 'assertHTML' => false
116 'wrong-domain-without-path' => [
117 'data' => 'http://example',
118 'assertHTML' => false
120 'wrong-domain-with-path' => [
121 'data' => 'http://example/path',
122 'assertHTML' => false
124 'bug-6857-domain-start' => [
125 'data' => "http://\nexample.com",
126 'assertHTML' => false
128 'bug-6857-domain-end' => [
129 'data' => "http://example\n.com",
130 'assertHTML' => false
133 'data' => "http://example.\ncom",
134 'assertHTML' => false
137 'data' => "http://example.com\ntest",
138 'assertHTML' => false
141 'data' => "http://example.com<ul>",
142 'assertHTML' => false
145 'data' => html_entity_decode('http://example.com ', ENT_QUOTES, 'UTF-8'),
146 'assertHTML' => false
148 'bug-7271-query-string-brackets' => [
149 'data' => 'https://example.com/search?q=square+brackets+[url]',
152 'bug-7271-path-brackets' => [
153 'data' => 'http://example.com/path/to/file[3].html',
160 * Test convert different links inside a text
161 * @dataProvider dataLinks
163 * @param string $data The data to text
164 * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
165 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
167 public function testAutoLinking($data, $assertHTML)
169 $output = BBCode::convert($data);
170 $assert = '<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>';
172 $this->assertEquals($assert, $output);
174 $this->assertNotEquals($assert, $output);
178 public function dataBBCodes()
181 'bug-7271-condensed-space' => [
182 '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>',
183 'text' => '[ol][*] http://example.com/[/ol]',
185 'bug-7271-condensed-nospace' => [
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-indented-space' => [
190 '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>',
192 [*] http://example.com/
195 'bug-7271-indented-nospace' => [
196 '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>',
198 [*]http://example.com/
201 'bug-2199-named-size' => [
202 'expectedHtml' => '<span style="font-size: xx-large; line-height: initial;">Test text</span>',
203 'text' => '[size=xx-large]Test text[/size]',
205 'bug-2199-numeric-size' => [
206 'expectedHtml' => '<span style="font-size: 24px; line-height: initial;">Test text</span>',
207 'text' => '[size=24]Test text[/size]',
209 'bug-2199-diaspora-no-named-size' => [
210 'expectedHtml' => 'Test text',
211 'text' => '[size=xx-large]Test text[/size]',
212 'try_oembed' => false,
213 // Triggers the diaspora compatible output
216 'bug-2199-diaspora-no-numeric-size' => [
217 'expectedHtml' => 'Test text',
218 'text' => '[size=24]Test text[/size]',
219 'try_oembed' => false,
220 // Triggers the diaspora compatible output
223 'bug-7665-audio-tag' => [
224 '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>',
225 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
226 'try_oembed' => true,
228 'bug-7808-code-lt' => [
229 'expectedHtml' => '<code><</code>',
230 'text' => '[code]<[/code]',
232 'bug-7808-code-gt' => [
233 'expectedHtml' => '<code>></code>',
234 'text' => '[code]>[/code]',
236 'bug-7808-code-amp' => [
237 'expectedHtml' => '<code>&</code>',
238 'text' => '[code]&[/code]',
244 * Test convert bbcodes to HTML
246 * @dataProvider dataBBCodes
248 * @param string $expectedHtml Expected HTML output
249 * @param string $text BBCode text
250 * @param bool $try_oembed Whether to convert multimedia BBCode tag
251 * @param int $simpleHtml BBCode::convert method $simple_html parameter value, optional.
252 * @param bool $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
253 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255 public function testConvert($expectedHtml, $text, $try_oembed = false, $simpleHtml = 0, $forPlaintext = false)
257 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
259 $this->assertEquals($expectedHtml, $actual);
262 public function dataBBCodesToMarkdown()
266 'expected' => '>`>`',
267 'text' => '>[code]>[/code]',
270 'expected' => '<`<`',
271 'text' => '<[code]<[/code]',
274 'expected' => '&`&`',
275 'text' => '&[code]&[/code]',
281 * Test convert bbcodes to Markdown
283 * @dataProvider dataBBCodesToMarkdown
285 * @param string $expected Expected Markdown output
286 * @param string $text BBCode text
287 * @param bool $for_diaspora
288 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
290 public function testToMarkdown($expected, $text, $for_diaspora = false)
292 $actual = BBCode::toMarkdown($text, $for_diaspora);
294 $this->assertEquals($expected, $actual);