]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
1a1d06dc7b1c5678ce27f9de4b30045e71d777e8
[friendica.git] / tests / src / Content / Text / BBCodeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Content\Text;
23
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;
30
31 class BBCodeTest extends MockedTest
32 {
33         use VFSTrait;
34         use AppMockTrait;
35
36         protected function setUp()
37         {
38                 parent::setUp();
39                 $this->setUpVfsDir();
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')
45                         ->andReturn(false);
46                 $this->configMock->shouldReceive('get')
47                         ->with('system', 'no_oembed')
48                         ->andReturn(false);
49                 $this->configMock->shouldReceive('get')
50                         ->with('system', 'allowed_link_protocols')
51                         ->andReturn(null);
52                 $this->configMock->shouldReceive('get')
53                         ->with('system', 'itemcache_duration')
54                         ->andReturn(-1);
55                 $this->configMock->shouldReceive('get')
56                         ->with('system', 'url')
57                         ->andReturn('friendica.local');
58                 $this->configMock->shouldReceive('get')
59                         ->with('system', 'no_smilies')
60                         ->andReturn(false);
61
62                 $l10nMock = \Mockery::mock(L10n::class);
63                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
64                 $this->dice->shouldReceive('create')
65                            ->with(L10n::class)
66                            ->andReturn($l10nMock);
67
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);
73         }
74
75         public function dataLinks()
76         {
77                 return [
78                         /** @see https://github.com/friendica/friendica/issues/2487 */
79                         'bug-2487-1' => [
80                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
81                                 'assertHTML' => true,
82                         ],
83                         'bug-2487-2' => [
84                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
85                                 'assertHTML' => true,
86                         ],
87                         'bug-2487-3' => [
88                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
89                                 'assertHTML' => true,
90                         ],
91                         'bug-2487-4' => [
92                                 'data' => 'https://mastodon.social/@morevnaproject',
93                                 'assertHTML' => true,
94                         ],
95                         /** @see https://github.com/friendica/friendica/issues/5795 */
96                         'bug-5795' => [
97                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
98                                 'assertHTML' => true,
99                         ],
100                         /** @see https://github.com/friendica/friendica/issues/6095 */
101                         'bug-6095' => [
102                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
103                                 'assertHTML' => true,
104                         ],
105                         'no-protocol' => [
106                                 'data' => 'example.com/path',
107                                 'assertHTML' => false
108                         ],
109                         'wrong-protocol' => [
110                                 'data' => 'ftp://example.com',
111                                 'assertHTML' => false
112                         ],
113                         'wrong-domain-without-path' => [
114                                 'data' => 'http://example',
115                                 'assertHTML' => false
116                         ],
117                         'wrong-domain-with-path' => [
118                                 'data' => 'http://example/path',
119                                 'assertHTML' => false
120                         ],
121                         'bug-6857-domain-start' => [
122                                 'data' => "http://\nexample.com",
123                                 'assertHTML' => false
124                         ],
125                         'bug-6857-domain-end' => [
126                                 'data' => "http://example\n.com",
127                                 'assertHTML' => false
128                         ],
129                         'bug-6857-tld' => [
130                                 'data' => "http://example.\ncom",
131                                 'assertHTML' => false
132                         ],
133                         'bug-6857-end' => [
134                                 'data' => "http://example.com\ntest",
135                                 'assertHTML' => false
136                         ],
137                         'bug-6901' => [
138                                 'data' => "http://example.com<ul>",
139                                 'assertHTML' => false
140                         ],
141                         'bug-7150' => [
142                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
143                                 'assertHTML' => false
144                         ],
145                         'bug-7271-query-string-brackets' => [
146                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
147                                 'assertHTML' => true
148                         ],
149                         'bug-7271-path-brackets' => [
150                                 'data' => 'http://example.com/path/to/file[3].html',
151                                 'assertHTML' => true
152                         ],
153                 ];
154         }
155
156         /**
157          * Test convert different links inside a text
158          * @dataProvider dataLinks
159          *
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
163          */
164         public function testAutoLinking($data, $assertHTML)
165         {
166                 $output = BBCode::convert($data);
167                 $assert = '<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>';
168                 if ($assertHTML) {
169                         $this->assertEquals($assert, $output);
170                 } else {
171                         $this->assertNotEquals($assert, $output);
172                 }
173         }
174
175         public function dataBBCodes()
176         {
177                 return [
178                         'bug-7271-condensed-space' => [
179                                 '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>',
180                                 'text' => '[ol][*] http://example.com/[/ol]',
181                         ],
182                         'bug-7271-condensed-nospace' => [
183                                 '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>',
184                                 'text' => '[ol][*]http://example.com/[/ol]',
185                         ],
186                         'bug-7271-indented-space' => [
187                                 '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>',
188                                 'text' => '[ul]
189 [*] http://example.com/
190 [/ul]',
191                         ],
192                         'bug-7271-indented-nospace' => [
193                                 '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>',
194                                 'text' => '[ul]
195 [*]http://example.com/
196 [/ul]',
197                         ],
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]',
201                         ],
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]',
205                         ],
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
211                                 'simpleHtml' => 3,
212                         ],
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
218                                 'simpleHtml' => 3,
219                         ],
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,
224                         ],
225                         'bug-7808-code-lt' => [
226                                 'expectedHtml' => '<code>&lt;</code>',
227                                 'text' => '[code]<[/code]',
228                         ],
229                         'bug-7808-code-gt' => [
230                                 'expectedHtml' => '<code>&gt;</code>',
231                                 'text' => '[code]>[/code]',
232                         ],
233                         'bug-7808-code-amp' => [
234                                 'expectedHtml' => '<code>&amp;</code>',
235                                 'text' => '[code]&[/code]',
236                         ]
237                 ];
238         }
239
240         /**
241          * Test convert bbcodes to HTML
242          *
243          * @dataProvider dataBBCodes
244          *
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
251          */
252         public function testConvert($expectedHtml, $text, $try_oembed = false, $simpleHtml = 0, $forPlaintext = false)
253         {
254                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
255
256                 $this->assertEquals($expectedHtml, $actual);
257         }
258
259         public function dataBBCodesToMarkdown()
260         {
261                 return [
262                         'bug-7808-gt' => [
263                                 'expected' => '&gt;`>`',
264                                 'text' => '>[code]>[/code]',
265                         ],
266                         'bug-7808-lt' => [
267                                 'expected' => '&lt;`<`',
268                                 'text' => '<[code]<[/code]',
269                         ],
270                         'bug-7808-amp' => [
271                                 'expected' => '&amp;`&`',
272                                 'text' => '&[code]&[/code]',
273                         ],
274                 ];
275         }
276
277         /**
278          * Test convert bbcodes to Markdown
279          *
280          * @dataProvider dataBBCodesToMarkdown
281          *
282          * @param string $expected     Expected Markdown output
283          * @param string $text         BBCode text
284          * @param bool   $for_diaspora
285          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
286          */
287         public function testToMarkdown($expected, $text, $for_diaspora = false)
288         {
289                 $actual = BBCode::toMarkdown($text, $for_diaspora);
290
291                 $this->assertEquals($expected, $actual);
292         }
293 }