]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
77613e891ac24aed4ab6fe29569c13e6cb5ff4c5
[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                 $this->configMock->shouldReceive('get')
62                         ->with('system', 'big_emojis')
63                         ->andReturn(false);
64
65                 $l10nMock = \Mockery::mock(L10n::class);
66                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
67                 $this->dice->shouldReceive('create')
68                            ->with(L10n::class)
69                            ->andReturn($l10nMock);
70
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);
76         }
77
78         public function dataLinks()
79         {
80                 return [
81                         /** @see https://github.com/friendica/friendica/issues/2487 */
82                         'bug-2487-1' => [
83                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
84                                 'assertHTML' => true,
85                         ],
86                         'bug-2487-2' => [
87                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
88                                 'assertHTML' => true,
89                         ],
90                         'bug-2487-3' => [
91                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
92                                 'assertHTML' => true,
93                         ],
94                         'bug-2487-4' => [
95                                 'data' => 'https://mastodon.social/@morevnaproject',
96                                 'assertHTML' => true,
97                         ],
98                         /** @see https://github.com/friendica/friendica/issues/5795 */
99                         'bug-5795' => [
100                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
101                                 'assertHTML' => true,
102                         ],
103                         /** @see https://github.com/friendica/friendica/issues/6095 */
104                         'bug-6095' => [
105                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
106                                 'assertHTML' => true,
107                         ],
108                         'no-protocol' => [
109                                 'data' => 'example.com/path',
110                                 'assertHTML' => false
111                         ],
112                         'wrong-protocol' => [
113                                 'data' => 'ftp://example.com',
114                                 'assertHTML' => false
115                         ],
116                         'wrong-domain-without-path' => [
117                                 'data' => 'http://example',
118                                 'assertHTML' => false
119                         ],
120                         'wrong-domain-with-path' => [
121                                 'data' => 'http://example/path',
122                                 'assertHTML' => false
123                         ],
124                         'bug-6857-domain-start' => [
125                                 'data' => "http://\nexample.com",
126                                 'assertHTML' => false
127                         ],
128                         'bug-6857-domain-end' => [
129                                 'data' => "http://example\n.com",
130                                 'assertHTML' => false
131                         ],
132                         'bug-6857-tld' => [
133                                 'data' => "http://example.\ncom",
134                                 'assertHTML' => false
135                         ],
136                         'bug-6857-end' => [
137                                 'data' => "http://example.com\ntest",
138                                 'assertHTML' => false
139                         ],
140                         'bug-6901' => [
141                                 'data' => "http://example.com<ul>",
142                                 'assertHTML' => false
143                         ],
144                         'bug-7150' => [
145                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
146                                 'assertHTML' => false
147                         ],
148                         'bug-7271-query-string-brackets' => [
149                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
150                                 'assertHTML' => true
151                         ],
152                         'bug-7271-path-brackets' => [
153                                 'data' => 'http://example.com/path/to/file[3].html',
154                                 'assertHTML' => true
155                         ],
156                 ];
157         }
158
159         /**
160          * Test convert different links inside a text
161          * @dataProvider dataLinks
162          *
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
166          */
167         public function testAutoLinking($data, $assertHTML)
168         {
169                 $output = BBCode::convert($data);
170                 $assert = '<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>';
171                 if ($assertHTML) {
172                         $this->assertEquals($assert, $output);
173                 } else {
174                         $this->assertNotEquals($assert, $output);
175                 }
176         }
177
178         public function dataBBCodes()
179         {
180                 return [
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]',
184                         ],
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]',
188                         ],
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>',
191                                 'text' => '[ul]
192 [*] http://example.com/
193 [/ul]',
194                         ],
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>',
197                                 'text' => '[ul]
198 [*]http://example.com/
199 [/ul]',
200                         ],
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]',
204                         ],
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]',
208                         ],
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
214                                 'simpleHtml' => 3,
215                         ],
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
221                                 'simpleHtml' => 3,
222                         ],
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,
227                         ],
228                         'bug-7808-code-lt' => [
229                                 'expectedHtml' => '<code>&lt;</code>',
230                                 'text' => '[code]<[/code]',
231                         ],
232                         'bug-7808-code-gt' => [
233                                 'expectedHtml' => '<code>&gt;</code>',
234                                 'text' => '[code]>[/code]',
235                         ],
236                         'bug-7808-code-amp' => [
237                                 'expectedHtml' => '<code>&amp;</code>',
238                                 'text' => '[code]&[/code]',
239                         ],
240                         'task-8800-pre-spaces-notag' => [
241                                 'expectedHtml' => '[test] Space',
242                                 'text' => '[test] Space',
243                         ],
244                         'task-8800-pre-spaces' => [
245                                 'expectedHtml' => '&nbsp;&nbsp;&nbsp;&nbsp;Spaces',
246                                 'text' => '[pre]    Spaces[/pre]',
247                         ],
248                 ];
249         }
250
251         /**
252          * Test convert bbcodes to HTML
253          *
254          * @dataProvider dataBBCodes
255          *
256          * @param string $expectedHtml Expected HTML output
257          * @param string $text         BBCode text
258          * @param bool   $try_oembed   Whether to convert multimedia BBCode tag
259          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
260          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
261          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
262          */
263         public function testConvert($expectedHtml, $text, $try_oembed = false, $simpleHtml = 0, $forPlaintext = false)
264         {
265                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
266
267                 $this->assertEquals($expectedHtml, $actual);
268         }
269
270         public function dataBBCodesToMarkdown()
271         {
272                 return [
273                         'bug-7808-gt' => [
274                                 'expected' => '&gt;`>`',
275                                 'text' => '>[code]>[/code]',
276                         ],
277                         'bug-7808-lt' => [
278                                 'expected' => '&lt;`<`',
279                                 'text' => '<[code]<[/code]',
280                         ],
281                         'bug-7808-amp' => [
282                                 'expected' => '&amp;`&`',
283                                 'text' => '&[code]&[/code]',
284                         ],
285                 ];
286         }
287
288         /**
289          * Test convert bbcodes to Markdown
290          *
291          * @dataProvider dataBBCodesToMarkdown
292          *
293          * @param string $expected     Expected Markdown output
294          * @param string $text         BBCode text
295          * @param bool   $for_diaspora
296          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
297          */
298         public function testToMarkdown($expected, $text, $for_diaspora = false)
299         {
300                 $actual = BBCode::toMarkdown($text, $for_diaspora);
301
302                 $this->assertEquals($expected, $actual);
303         }
304 }