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')
62 $l10nMock = \Mockery::mock(L10n::class);
63 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
64 $this->dice->shouldReceive('create')
66 ->andReturn($l10nMock);
68 $baseUrlMock = \Mockery::mock(BaseURL::class);
69 $baseUrlMock->shouldReceive('get')->withAnyArgs()->andReturn('friendica.local');
70 $this->dice->shouldReceive('create')
71 ->with(BaseURL::class)
72 ->andReturn($baseUrlMock);
75 public function dataLinks()
78 /** @see https://github.com/friendica/friendica/issues/2487 */
80 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
84 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
88 'data' => 'https://friendica.wäckerlin.ch/friendica',
92 'data' => 'https://mastodon.social/@morevnaproject',
95 /** @see https://github.com/friendica/friendica/issues/5795 */
97 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
100 /** @see https://github.com/friendica/friendica/issues/6095 */
102 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
103 'assertHTML' => true,
106 'data' => 'example.com/path',
107 'assertHTML' => false
109 'wrong-protocol' => [
110 'data' => 'ftp://example.com',
111 'assertHTML' => false
113 'wrong-domain-without-path' => [
114 'data' => 'http://example',
115 'assertHTML' => false
117 'wrong-domain-with-path' => [
118 'data' => 'http://example/path',
119 'assertHTML' => false
121 'bug-6857-domain-start' => [
122 'data' => "http://\nexample.com",
123 'assertHTML' => false
125 'bug-6857-domain-end' => [
126 'data' => "http://example\n.com",
127 'assertHTML' => false
130 'data' => "http://example.\ncom",
131 'assertHTML' => false
134 'data' => "http://example.com\ntest",
135 'assertHTML' => false
138 'data' => "http://example.com<ul>",
139 'assertHTML' => false
142 'data' => html_entity_decode('http://example.com ', ENT_QUOTES, 'UTF-8'),
143 'assertHTML' => false
145 'bug-7271-query-string-brackets' => [
146 'data' => 'https://example.com/search?q=square+brackets+[url]',
149 'bug-7271-path-brackets' => [
150 'data' => 'http://example.com/path/to/file[3].html',
157 * Test convert different links inside a text
158 * @dataProvider dataLinks
160 * @param string $data The data to text
161 * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
162 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
164 public function testAutoLinking($data, $assertHTML)
166 $output = BBCode::convert($data);
167 $assert = '<a href="' . $data . '" target="_blank">' . $data . '</a>';
169 $this->assertEquals($assert, $output);
171 $this->assertNotEquals($assert, $output);
175 public function dataBBCodes()
178 'bug-7271-condensed-space' => [
179 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
180 'text' => '[ol][*] http://example.com/[/ol]',
182 'bug-7271-condensed-nospace' => [
183 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
184 'text' => '[ol][*]http://example.com/[/ol]',
186 'bug-7271-indented-space' => [
187 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
189 [*] http://example.com/
192 'bug-7271-indented-nospace' => [
193 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
195 [*]http://example.com/
198 'bug-2199-named-size' => [
199 'expectedHtml' => '<span style="font-size: xx-large; line-height: initial;">Test text</span>',
200 'text' => '[size=xx-large]Test text[/size]',
202 'bug-2199-numeric-size' => [
203 'expectedHtml' => '<span style="font-size: 24px; line-height: initial;">Test text</span>',
204 'text' => '[size=24]Test text[/size]',
206 'bug-2199-diaspora-no-named-size' => [
207 'expectedHtml' => 'Test text',
208 'text' => '[size=xx-large]Test text[/size]',
209 'try_oembed' => false,
210 // Triggers the diaspora compatible output
213 'bug-2199-diaspora-no-numeric-size' => [
214 'expectedHtml' => 'Test text',
215 'text' => '[size=24]Test text[/size]',
216 'try_oembed' => false,
217 // Triggers the diaspora compatible output
220 'bug-7665-audio-tag' => [
221 '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>',
222 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
223 'try_oembed' => true,
225 'bug-7808-code-lt' => [
226 'expectedHtml' => '<code><</code>',
227 'text' => '[code]<[/code]',
229 'bug-7808-code-gt' => [
230 'expectedHtml' => '<code>></code>',
231 'text' => '[code]>[/code]',
233 'bug-7808-code-amp' => [
234 'expectedHtml' => '<code>&</code>',
235 'text' => '[code]&[/code]',
241 * Test convert bbcodes to HTML
243 * @dataProvider dataBBCodes
245 * @param string $expectedHtml Expected HTML output
246 * @param string $text BBCode text
247 * @param bool $try_oembed Whether to convert multimedia BBCode tag
248 * @param int $simpleHtml BBCode::convert method $simple_html parameter value, optional.
249 * @param bool $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
250 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
252 public function testConvert($expectedHtml, $text, $try_oembed = false, $simpleHtml = 0, $forPlaintext = false)
254 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
256 $this->assertEquals($expectedHtml, $actual);
259 public function dataBBCodesToMarkdown()
263 'expected' => '>`>`',
264 'text' => '>[code]>[/code]',
267 'expected' => '<`<`',
268 'text' => '<[code]<[/code]',
271 'expected' => '&`&`',
272 'text' => '&[code]&[/code]',
278 * Test convert bbcodes to Markdown
280 * @dataProvider dataBBCodesToMarkdown
282 * @param string $expected Expected Markdown output
283 * @param string $text BBCode text
284 * @param bool $for_diaspora
285 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
287 public function testToMarkdown($expected, $text, $for_diaspora = false)
289 $actual = BBCode::toMarkdown($text, $for_diaspora);
291 $this->assertEquals($expected, $actual);