]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
Merge pull request #9436 from nupplaphil/task/test_notices
[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\Network\HTTPException\InternalServerErrorException;
28 use Friendica\Test\MockedTest;
29 use Friendica\Test\Util\AppMockTrait;
30 use Friendica\Test\Util\VFSTrait;
31 use Mockery;
32
33 class BBCodeTest extends MockedTest
34 {
35         use VFSTrait;
36         use AppMockTrait;
37
38         protected function setUp()
39         {
40                 parent::setUp();
41                 $this->setUpVfsDir();
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')
47                         ->andReturn(false);
48                 $this->configMock->shouldReceive('get')
49                         ->with('system', 'no_oembed')
50                         ->andReturn(false);
51                 $this->configMock->shouldReceive('get')
52                         ->with('system', 'allowed_link_protocols')
53                         ->andReturn(null);
54                 $this->configMock->shouldReceive('get')
55                         ->with('system', 'itemcache_duration')
56                         ->andReturn(-1);
57                 $this->configMock->shouldReceive('get')
58                         ->with('system', 'url')
59                         ->andReturn('friendica.local');
60                 $this->configMock->shouldReceive('get')
61                         ->with('system', 'no_smilies')
62                         ->andReturn(false);
63                 $this->configMock->shouldReceive('get')
64                         ->with('system', 'big_emojis')
65                         ->andReturn(false);
66
67                 $l10nMock = Mockery::mock(L10n::class);
68                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
69                 $this->dice->shouldReceive('create')
70                            ->with(L10n::class)
71                            ->andReturn($l10nMock);
72
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);
78         }
79
80         public function dataLinks()
81         {
82                 return [
83                         /** @see https://github.com/friendica/friendica/issues/2487 */
84                         'bug-2487-1' => [
85                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
86                                 'assertHTML' => true,
87                         ],
88                         'bug-2487-2' => [
89                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
90                                 'assertHTML' => true,
91                         ],
92                         'bug-2487-3' => [
93                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
94                                 'assertHTML' => true,
95                         ],
96                         'bug-2487-4' => [
97                                 'data' => 'https://mastodon.social/@morevnaproject',
98                                 'assertHTML' => true,
99                         ],
100                         /** @see https://github.com/friendica/friendica/issues/5795 */
101                         'bug-5795' => [
102                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
103                                 'assertHTML' => true,
104                         ],
105                         /** @see https://github.com/friendica/friendica/issues/6095 */
106                         'bug-6095' => [
107                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
108                                 'assertHTML' => true,
109                         ],
110                         'no-protocol' => [
111                                 'data' => 'example.com/path',
112                                 'assertHTML' => false
113                         ],
114                         'wrong-protocol' => [
115                                 'data' => 'ftp://example.com',
116                                 'assertHTML' => false
117                         ],
118                         'wrong-domain-without-path' => [
119                                 'data' => 'http://example',
120                                 'assertHTML' => false
121                         ],
122                         'wrong-domain-with-path' => [
123                                 'data' => 'http://example/path',
124                                 'assertHTML' => false
125                         ],
126                         'bug-6857-domain-start' => [
127                                 'data' => "http://\nexample.com",
128                                 'assertHTML' => false
129                         ],
130                         'bug-6857-domain-end' => [
131                                 'data' => "http://example\n.com",
132                                 'assertHTML' => false
133                         ],
134                         'bug-6857-tld' => [
135                                 'data' => "http://example.\ncom",
136                                 'assertHTML' => false
137                         ],
138                         'bug-6857-end' => [
139                                 'data' => "http://example.com\ntest",
140                                 'assertHTML' => false
141                         ],
142                         'bug-6901' => [
143                                 'data' => "http://example.com<ul>",
144                                 'assertHTML' => false
145                         ],
146                         'bug-7150' => [
147                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
148                                 'assertHTML' => false
149                         ],
150                         'bug-7271-query-string-brackets' => [
151                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
152                                 'assertHTML' => true
153                         ],
154                         'bug-7271-path-brackets' => [
155                                 'data' => 'http://example.com/path/to/file[3].html',
156                                 'assertHTML' => true
157                         ],
158                 ];
159         }
160
161         /**
162          * Test convert different links inside a text
163          *
164          * @dataProvider dataLinks
165          *
166          * @param string $data       The data to text
167          * @param bool   $assertHTML True, if the link is a HTML link (<a href...>...</a>)
168          *
169          * @throws InternalServerErrorException
170          */
171         public function testAutoLinking(string $data, bool $assertHTML)
172         {
173                 $output = BBCode::convert($data);
174                 $assert = '<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>';
175                 if ($assertHTML) {
176                         self::assertEquals($assert, $output);
177                 } else {
178                         self::assertNotEquals($assert, $output);
179                 }
180         }
181
182         public function dataBBCodes()
183         {
184                 return [
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]',
188                         ],
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]',
192                         ],
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>',
195                                 'text' => '[ul]
196 [*] http://example.com/
197 [/ul]',
198                         ],
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>',
201                                 'text' => '[ul]
202 [*]http://example.com/
203 [/ul]',
204                         ],
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]',
208                         ],
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]',
212                         ],
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
218                                 'simpleHtml' => 3,
219                         ],
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
225                                 'simpleHtml' => 3,
226                         ],
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,
231                         ],
232                         'bug-7808-code-lt' => [
233                                 'expectedHtml' => '<code>&lt;</code>',
234                                 'text' => '[code]<[/code]',
235                         ],
236                         'bug-7808-code-gt' => [
237                                 'expectedHtml' => '<code>&gt;</code>',
238                                 'text' => '[code]>[/code]',
239                         ],
240                         'bug-7808-code-amp' => [
241                                 'expectedHtml' => '<code>&amp;</code>',
242                                 'text' => '[code]&[/code]',
243                         ],
244                         'task-8800-pre-spaces-notag' => [
245                                 'expectedHtml' => '[test] Space',
246                                 'text' => '[test] Space',
247                         ],
248                         'task-8800-pre-spaces' => [
249                                 'expectedHtml' => '&nbsp;&nbsp;&nbsp;&nbsp;Spaces',
250                                 'text' => '[pre]    Spaces[/pre]',
251                         ],
252                 ];
253         }
254
255         /**
256          * Test convert bbcodes to HTML
257          *
258          * @dataProvider dataBBCodes
259          *
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.
265          *
266          * @throws InternalServerErrorException
267          */
268         public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
269         {
270                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
271
272                 self::assertEquals($expectedHtml, $actual);
273         }
274
275         public function dataBBCodesToMarkdown()
276         {
277                 return [
278                         'bug-7808-gt' => [
279                                 'expected' => '&gt;`>`',
280                                 'text' => '>[code]>[/code]',
281                         ],
282                         'bug-7808-lt' => [
283                                 'expected' => '&lt;`<`',
284                                 'text' => '<[code]<[/code]',
285                         ],
286                         'bug-7808-amp' => [
287                                 'expected' => '&amp;`&`',
288                                 'text' => '&[code]&[/code]',
289                         ],
290                 ];
291         }
292
293         /**
294          * Test convert bbcodes to Markdown
295          *
296          * @dataProvider dataBBCodesToMarkdown
297          *
298          * @param string $expected Expected Markdown output
299          * @param string $text     BBCode text
300          * @param bool   $for_diaspora
301          *
302          * @throws InternalServerErrorException
303          */
304         public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
305         {
306                 $actual = BBCode::toMarkdown($text, $for_diaspora);
307
308                 self::assertEquals($expected, $actual);
309         }
310 }