]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
Add expected mocked calls to BBCodeTest
[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                 $this->configMock->shouldReceive('get')
67                         ->with('system', 'allowed_oembed')
68                         ->andReturn('');
69
70                 $l10nMock = Mockery::mock(L10n::class);
71                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
72                 $this->dice->shouldReceive('create')
73                            ->with(L10n::class)
74                            ->andReturn($l10nMock);
75
76                 $baseUrlMock = Mockery::mock(BaseURL::class);
77                 $baseUrlMock->shouldReceive('get')->withAnyArgs()->andReturn('friendica.local');
78                 $this->dice->shouldReceive('create')
79                            ->with(BaseURL::class)
80                            ->andReturn($baseUrlMock);
81                 $baseUrlMock->shouldReceive('getHostname')->withNoArgs()->andReturn('friendica.local');
82                 $baseUrlMock->shouldReceive('getUrlPath')->withNoArgs()->andReturn('');
83
84                 $config = \HTMLPurifier_HTML5Config::createDefault();
85                 $config->set('HTML.Doctype', 'HTML5');
86                 $config->set('Attr.AllowedRel', [
87                         'noreferrer' => true,
88                         'noopener' => true,
89                 ]);
90                 $config->set('Attr.AllowedFrameTargets', [
91                         '_blank' => true,
92                 ]);
93
94                 $this->HTMLPurifier = new \HTMLPurifier($config);
95         }
96
97         public function dataLinks()
98         {
99                 return [
100                         /** @see https://github.com/friendica/friendica/issues/2487 */
101                         'bug-2487-1' => [
102                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
103                                 'assertHTML' => true,
104                         ],
105                         'bug-2487-2' => [
106                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
107                                 'assertHTML' => true,
108                         ],
109                         'bug-2487-3' => [
110                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
111                                 'assertHTML' => true,
112                         ],
113                         'bug-2487-4' => [
114                                 'data' => 'https://mastodon.social/@morevnaproject',
115                                 'assertHTML' => true,
116                         ],
117                         /** @see https://github.com/friendica/friendica/issues/5795 */
118                         'bug-5795' => [
119                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
120                                 'assertHTML' => true,
121                         ],
122                         /** @see https://github.com/friendica/friendica/issues/6095 */
123                         'bug-6095' => [
124                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
125                                 'assertHTML' => true,
126                         ],
127                         'no-protocol' => [
128                                 'data' => 'example.com/path',
129                                 'assertHTML' => false
130                         ],
131                         'wrong-protocol' => [
132                                 'data' => 'ftp://example.com',
133                                 'assertHTML' => false
134                         ],
135                         'wrong-domain-without-path' => [
136                                 'data' => 'http://example',
137                                 'assertHTML' => false
138                         ],
139                         'wrong-domain-with-path' => [
140                                 'data' => 'http://example/path',
141                                 'assertHTML' => false
142                         ],
143                         'bug-6857-domain-start' => [
144                                 'data' => "http://\nexample.com",
145                                 'assertHTML' => false
146                         ],
147                         'bug-6857-domain-end' => [
148                                 'data' => "http://example\n.com",
149                                 'assertHTML' => false
150                         ],
151                         'bug-6857-tld' => [
152                                 'data' => "http://example.\ncom",
153                                 'assertHTML' => false
154                         ],
155                         'bug-6857-end' => [
156                                 'data' => "http://example.com\ntest",
157                                 'assertHTML' => false
158                         ],
159                         'bug-6901' => [
160                                 'data' => "http://example.com<ul>",
161                                 'assertHTML' => false
162                         ],
163                         'bug-7150' => [
164                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
165                                 'assertHTML' => false
166                         ],
167                         'bug-7271-query-string-brackets' => [
168                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
169                                 'assertHTML' => true
170                         ],
171                         'bug-7271-path-brackets' => [
172                                 'data' => 'http://example.com/path/to/file[3].html',
173                                 'assertHTML' => true
174                         ],
175                 ];
176         }
177
178         /**
179          * Test convert different links inside a text
180          *
181          * @dataProvider dataLinks
182          *
183          * @param string $data       The data to text
184          * @param bool   $assertHTML True, if the link is a HTML link (<a href...>...</a>)
185          *
186          * @throws InternalServerErrorException
187          */
188         public function testAutoLinking(string $data, bool $assertHTML)
189         {
190                 $output = BBCode::convert($data);
191                 $assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
192                 if ($assertHTML) {
193                         self::assertEquals($assert, $output);
194                 } else {
195                         self::assertNotEquals($assert, $output);
196                 }
197         }
198
199         public function dataBBCodes()
200         {
201                 return [
202                         'bug-7271-condensed-space' => [
203                                 '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>',
204                                 'text' => '[ol][*] http://example.com/[/ol]',
205                         ],
206                         'bug-7271-condensed-nospace' => [
207                                 '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>',
208                                 'text' => '[ol][*]http://example.com/[/ol]',
209                         ],
210                         'bug-7271-indented-space' => [
211                                 '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>',
212                                 'text' => '[ul]
213 [*] http://example.com/
214 [/ul]',
215                         ],
216                         'bug-7271-indented-nospace' => [
217                                 '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>',
218                                 'text' => '[ul]
219 [*]http://example.com/
220 [/ul]',
221                         ],
222                         'bug-2199-named-size' => [
223                                 'expectedHtml' => '<span style="font-size:xx-large;line-height:normal;">Test text</span>',
224                                 'text' => '[size=xx-large]Test text[/size]',
225                         ],
226                         'bug-2199-numeric-size' => [
227                                 'expectedHtml' => '<span style="font-size:24px;line-height:normal;">Test text</span>',
228                                 'text' => '[size=24]Test text[/size]',
229                         ],
230                         'bug-2199-diaspora-no-named-size' => [
231                                 'expectedHtml' => 'Test text',
232                                 'text' => '[size=xx-large]Test text[/size]',
233                                 'try_oembed' => false,
234                                 // Triggers the diaspora compatible output
235                                 'simpleHtml' => 3,
236                         ],
237                         'bug-2199-diaspora-no-numeric-size' => [
238                                 'expectedHtml' => 'Test text',
239                                 'text' => '[size=24]Test text[/size]',
240                                 'try_oembed' => false,
241                                 // Triggers the diaspora compatible output
242                                 'simpleHtml' => 3,
243                         ],
244                         'bug-7665-audio-tag' => [
245                                 'expectedHtml' => '<audio src="http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3" controls><a href="http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3">http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3</a></audio>',
246                                 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
247                                 'try_oembed' => true,
248                         ],
249                         'bug-7808-code-lt' => [
250                                 'expectedHtml' => '<code>&lt;</code>',
251                                 'text' => '[code]<[/code]',
252                         ],
253                         'bug-7808-code-gt' => [
254                                 'expectedHtml' => '<code>&gt;</code>',
255                                 'text' => '[code]>[/code]',
256                         ],
257                         'bug-7808-code-amp' => [
258                                 'expectedHtml' => '<code>&amp;</code>',
259                                 'text' => '[code]&[/code]',
260                         ],
261                         'task-8800-pre-spaces-notag' => [
262                                 'expectedHtml' => '[test] Space',
263                                 'text' => '[test] Space',
264                         ],
265                         'task-8800-pre-spaces' => [
266                                 'expectedHtml' => '    Spaces',
267                                 'text' => '[pre]    Spaces[/pre]',
268                         ],
269                         'bug-9611-purify-xss-nobb' => [
270                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
271                                 'text' => '[nobb]<span onmouseover="alert(0)">dare to move your mouse here</span>[/nobb]'
272                         ],
273                         'bug-9611-purify-xss-noparse' => [
274                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
275                                 'text' => '[noparse]<span onmouseover="alert(0)">dare to move your mouse here</span>[/noparse]'
276                         ],
277                         'bug-9611-purify-xss-attributes' => [
278                                 'expectedHTML' => '<span>dare to move your mouse here</span>',
279                                 'text' => '[color="onmouseover=alert(0) style="]dare to move your mouse here[/color]'
280                         ],
281                         'bug-9611-purify-attributes-correct' => [
282                                 'expectedHTML' => '<span style="color:#FFFFFF;">dare to move your mouse here</span>',
283                                 'text' => '[color=FFFFFF]dare to move your mouse here[/color]'
284                         ],
285                         'bug-9639-span-classes' => [
286                                 'expectedHTML' => '<span class="arbitrary classes">Test</span>',
287                                 'text' => '[class=arbitrary classes]Test[/class]',
288                         ],
289                 ];
290         }
291
292         /**
293          * Test convert bbcodes to HTML
294          *
295          * @dataProvider dataBBCodes
296          *
297          * @param string $expectedHtml Expected HTML output
298          * @param string $text         BBCode text
299          * @param bool   $try_oembed   Whether to convert multimedia BBCode tag
300          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
301          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
302          *
303          * @throws InternalServerErrorException
304          */
305         public function testConvert(string $expectedHtml, string $text, $try_oembed = false, int $simpleHtml = 0, bool $forPlaintext = false)
306         {
307                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
308
309                 self::assertEquals($expectedHtml, $actual);
310         }
311
312         public function dataBBCodesToMarkdown()
313         {
314                 return [
315                         'bug-7808-gt' => [
316                                 'expected' => '&gt;`>`',
317                                 'text' => '>[code]>[/code]',
318                         ],
319                         'bug-7808-lt' => [
320                                 'expected' => '&lt;`<`',
321                                 'text' => '<[code]<[/code]',
322                         ],
323                         'bug-7808-amp' => [
324                                 'expected' => '&amp;`&`',
325                                 'text' => '&[code]&[/code]',
326                         ],
327                 ];
328         }
329
330         /**
331          * Test convert bbcodes to Markdown
332          *
333          * @dataProvider dataBBCodesToMarkdown
334          *
335          * @param string $expected Expected Markdown output
336          * @param string $text     BBCode text
337          * @param bool   $for_diaspora
338          *
339          * @throws InternalServerErrorException
340          */
341         public function testToMarkdown(string $expected, string $text, $for_diaspora = false)
342         {
343                 $actual = BBCode::toMarkdown($text, $for_diaspora);
344
345                 self::assertEquals($expected, $actual);
346         }
347 }