]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
Merge pull request #8128 from nupplaphil/task/di_static_methods
[friendica.git] / tests / src / Content / Text / BBCodeTest.php
1 <?php
2
3 namespace Friendica\Test\src\Content\Text;
4
5 use Friendica\App\BaseURL;
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Core\L10n\L10n;
8 use Friendica\Test\MockedTest;
9 use Friendica\Test\Util\AppMockTrait;
10 use Friendica\Test\Util\VFSTrait;
11
12 class BBCodeTest extends MockedTest
13 {
14         use VFSTrait;
15         use AppMockTrait;
16
17         protected function setUp()
18         {
19                 parent::setUp();
20                 $this->setUpVfsDir();
21                 $this->mockApp($this->root);
22                 $this->app->videowidth = 425;
23                 $this->app->videoheight = 350;
24                 $this->configMock->shouldReceive('get')
25                         ->with('system', 'remove_multiplicated_lines')
26                         ->andReturn(false);
27                 $this->configMock->shouldReceive('get')
28                         ->with('system', 'no_oembed')
29                         ->andReturn(false);
30                 $this->configMock->shouldReceive('get')
31                         ->with('system', 'allowed_link_protocols')
32                         ->andReturn(null);
33                 $this->configMock->shouldReceive('get')
34                         ->with('system', 'itemcache_duration')
35                         ->andReturn(-1);
36                 $this->configMock->shouldReceive('get')
37                         ->with('system', 'url')
38                         ->andReturn('friendica.local');
39                 $this->configMock->shouldReceive('get')
40                         ->with('system', 'no_smilies')
41                         ->andReturn(false);
42
43                 $l10nMock = \Mockery::mock(L10n::class);
44                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
45                 $this->dice->shouldReceive('create')
46                            ->with(L10n::class)
47                            ->andReturn($l10nMock);
48
49                 $baseUrlMock = \Mockery::mock(BaseURL::class);
50                 $baseUrlMock->shouldReceive('get')->withAnyArgs()->andReturn('friendica.local');
51                 $this->dice->shouldReceive('create')
52                            ->with(BaseURL::class)
53                            ->andReturn($baseUrlMock);
54         }
55
56         public function dataLinks()
57         {
58                 return [
59                         /** @see https://github.com/friendica/friendica/issues/2487 */
60                         'bug-2487-1' => [
61                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
62                                 'assertHTML' => true,
63                         ],
64                         'bug-2487-2' => [
65                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
66                                 'assertHTML' => true,
67                         ],
68                         'bug-2487-3' => [
69                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
70                                 'assertHTML' => true,
71                         ],
72                         'bug-2487-4' => [
73                                 'data' => 'https://mastodon.social/@morevnaproject',
74                                 'assertHTML' => true,
75                         ],
76                         /** @see https://github.com/friendica/friendica/issues/5795 */
77                         'bug-5795' => [
78                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
79                                 'assertHTML' => true,
80                         ],
81                         /** @see https://github.com/friendica/friendica/issues/6095 */
82                         'bug-6095' => [
83                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
84                                 'assertHTML' => true,
85                         ],
86                         'no-protocol' => [
87                                 'data' => 'example.com/path',
88                                 'assertHTML' => false
89                         ],
90                         'wrong-protocol' => [
91                                 'data' => 'ftp://example.com',
92                                 'assertHTML' => false
93                         ],
94                         'wrong-domain-without-path' => [
95                                 'data' => 'http://example',
96                                 'assertHTML' => false
97                         ],
98                         'wrong-domain-with-path' => [
99                                 'data' => 'http://example/path',
100                                 'assertHTML' => false
101                         ],
102                         'bug-6857-domain-start' => [
103                                 'data' => "http://\nexample.com",
104                                 'assertHTML' => false
105                         ],
106                         'bug-6857-domain-end' => [
107                                 'data' => "http://example\n.com",
108                                 'assertHTML' => false
109                         ],
110                         'bug-6857-tld' => [
111                                 'data' => "http://example.\ncom",
112                                 'assertHTML' => false
113                         ],
114                         'bug-6857-end' => [
115                                 'data' => "http://example.com\ntest",
116                                 'assertHTML' => false
117                         ],
118                         'bug-6901' => [
119                                 'data' => "http://example.com<ul>",
120                                 'assertHTML' => false
121                         ],
122                         'bug-7150' => [
123                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
124                                 'assertHTML' => false
125                         ],
126                         'bug-7271-query-string-brackets' => [
127                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
128                                 'assertHTML' => true
129                         ],
130                         'bug-7271-path-brackets' => [
131                                 'data' => 'http://example.com/path/to/file[3].html',
132                                 'assertHTML' => true
133                         ],
134                 ];
135         }
136
137         /**
138          * Test convert different links inside a text
139          * @dataProvider dataLinks
140          *
141          * @param string $data The data to text
142          * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
143          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144          */
145         public function testAutoLinking($data, $assertHTML)
146         {
147                 $output = BBCode::convert($data);
148                 $assert = '<a href="' . $data . '" target="_blank">' . $data . '</a>';
149                 if ($assertHTML) {
150                         $this->assertEquals($assert, $output);
151                 } else {
152                         $this->assertNotEquals($assert, $output);
153                 }
154         }
155
156         public function dataBBCodes()
157         {
158                 return [
159                         'bug-7271-condensed-space' => [
160                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
161                                 'text' => '[ol][*] http://example.com/[/ol]',
162                         ],
163                         'bug-7271-condensed-nospace' => [
164                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
165                                 'text' => '[ol][*]http://example.com/[/ol]',
166                         ],
167                         'bug-7271-indented-space' => [
168                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
169                                 'text' => '[ul]
170 [*] http://example.com/
171 [/ul]',
172                         ],
173                         'bug-7271-indented-nospace' => [
174                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
175                                 'text' => '[ul]
176 [*]http://example.com/
177 [/ul]',
178                         ],
179                         'bug-2199-named-size' => [
180                                 'expectedHtml' => '<span style="font-size: xx-large; line-height: initial;">Test text</span>',
181                                 'text' => '[size=xx-large]Test text[/size]',
182                         ],
183                         'bug-2199-numeric-size' => [
184                                 'expectedHtml' => '<span style="font-size: 24px; line-height: initial;">Test text</span>',
185                                 'text' => '[size=24]Test text[/size]',
186                         ],
187                         'bug-2199-diaspora-no-named-size' => [
188                                 'expectedHtml' => 'Test text',
189                                 'text' => '[size=xx-large]Test text[/size]',
190                                 'try_oembed' => false,
191                                 // Triggers the diaspora compatible output
192                                 'simpleHtml' => 3,
193                         ],
194                         'bug-2199-diaspora-no-numeric-size' => [
195                                 'expectedHtml' => 'Test text',
196                                 'text' => '[size=24]Test text[/size]',
197                                 'try_oembed' => false,
198                                 // Triggers the diaspora compatible output
199                                 'simpleHtml' => 3,
200                         ],
201                         'bug-7665-audio-tag' => [
202                                 '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>',
203                                 'text' => '[audio]http://www.cendrones.fr/colloque2017/jonathanbocquet.mp3[/audio]',
204                                 'try_oembed' => true,
205                         ],
206                         'bug-7808-code-lt' => [
207                                 'expectedHtml' => '<code>&lt;</code>',
208                                 'text' => '[code]<[/code]',
209                         ],
210                         'bug-7808-code-gt' => [
211                                 'expectedHtml' => '<code>&gt;</code>',
212                                 'text' => '[code]>[/code]',
213                         ],
214                         'bug-7808-code-amp' => [
215                                 'expectedHtml' => '<code>&amp;</code>',
216                                 'text' => '[code]&[/code]',
217                         ]
218                 ];
219         }
220
221         /**
222          * Test convert bbcodes to HTML
223          *
224          * @dataProvider dataBBCodes
225          *
226          * @param string $expectedHtml Expected HTML output
227          * @param string $text         BBCode text
228          * @param bool   $try_oembed   Whether to convert multimedia BBCode tag
229          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
230          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
231          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
232          */
233         public function testConvert($expectedHtml, $text, $try_oembed = false, $simpleHtml = 0, $forPlaintext = false)
234         {
235                 $actual = BBCode::convert($text, $try_oembed, $simpleHtml, $forPlaintext);
236
237                 $this->assertEquals($expectedHtml, $actual);
238         }
239
240         public function dataBBCodesToMarkdown()
241         {
242                 return [
243                         'bug-7808-gt' => [
244                                 'expected' => '&gt;`>`',
245                                 'text' => '>[code]>[/code]',
246                         ],
247                         'bug-7808-lt' => [
248                                 'expected' => '&lt;`<`',
249                                 'text' => '<[code]<[/code]',
250                         ],
251                         'bug-7808-amp' => [
252                                 'expected' => '&amp;`&`',
253                                 'text' => '&[code]&[/code]',
254                         ],
255                 ];
256         }
257
258         /**
259          * Test convert bbcodes to Markdown
260          *
261          * @dataProvider dataBBCodesToMarkdown
262          *
263          * @param string $expected     Expected Markdown output
264          * @param string $text         BBCode text
265          * @param bool   $for_diaspora
266          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
267          */
268         public function testToMarkdown($expected, $text, $for_diaspora = false)
269         {
270                 $actual = BBCode::toMarkdown($text, $for_diaspora);
271
272                 $this->assertEquals($expected, $actual);
273         }
274 }