]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
Merge pull request #7474 from MrPetovan/bug/7457-html-bbcode-plaintext
[friendica.git] / tests / src / Content / Text / BBCodeTest.php
1 <?php
2
3 namespace Friendica\Test\src\Content\Text;
4
5 use Friendica\Content\Text\BBCode;
6 use Friendica\Core\L10n\L10n;
7 use Friendica\Test\MockedTest;
8 use Friendica\Test\Util\AppMockTrait;
9 use Friendica\Test\Util\VFSTrait;
10
11 class BBCodeTest extends MockedTest
12 {
13         use VFSTrait;
14         use AppMockTrait;
15
16         protected function setUp()
17         {
18                 parent::setUp();
19                 $this->setUpVfsDir();
20                 $this->mockApp($this->root);
21                 $this->app->videowidth = 425;
22                 $this->app->videoheight = 350;
23                 $this->configMock->shouldReceive('get')
24                         ->with('system', 'remove_multiplicated_lines')
25                         ->andReturn(false);
26                 $this->configMock->shouldReceive('get')
27                         ->with('system', 'no_oembed')
28                         ->andReturn(false);
29                 $this->configMock->shouldReceive('get')
30                         ->with('system', 'allowed_link_protocols')
31                         ->andReturn(null);
32                 $this->configMock->shouldReceive('get')
33                         ->with('system', 'itemcache_duration')
34                         ->andReturn(-1);
35                 $this->configMock->shouldReceive('get')
36                         ->with('system', 'url')
37                         ->andReturn('friendica.local');
38                 $this->configMock->shouldReceive('get')
39                         ->with('system', 'no_smilies')
40                         ->andReturn(false);
41
42                 $l10nMock = \Mockery::mock(L10n::class);
43                 $l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
44                 $this->dice->shouldReceive('create')
45                            ->with(L10n::class)
46                            ->andReturn($l10nMock);
47         }
48
49         public function dataLinks()
50         {
51                 return [
52                         /** @see https://github.com/friendica/friendica/issues/2487 */
53                         'bug-2487-1' => [
54                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
55                                 'assertHTML' => true,
56                         ],
57                         'bug-2487-2' => [
58                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
59                                 'assertHTML' => true,
60                         ],
61                         'bug-2487-3' => [
62                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
63                                 'assertHTML' => true,
64                         ],
65                         'bug-2487-4' => [
66                                 'data' => 'https://mastodon.social/@morevnaproject',
67                                 'assertHTML' => true,
68                         ],
69                         /** @see https://github.com/friendica/friendica/issues/5795 */
70                         'bug-5795' => [
71                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
72                                 'assertHTML' => true,
73                         ],
74                         /** @see https://github.com/friendica/friendica/issues/6095 */
75                         'bug-6095' => [
76                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
77                                 'assertHTML' => true,
78                         ],
79                         'no-protocol' => [
80                                 'data' => 'example.com/path',
81                                 'assertHTML' => false
82                         ],
83                         'wrong-protocol' => [
84                                 'data' => 'ftp://example.com',
85                                 'assertHTML' => false
86                         ],
87                         'wrong-domain-without-path' => [
88                                 'data' => 'http://example',
89                                 'assertHTML' => false
90                         ],
91                         'wrong-domain-with-path' => [
92                                 'data' => 'http://example/path',
93                                 'assertHTML' => false
94                         ],
95                         'bug-6857-domain-start' => [
96                                 'data' => "http://\nexample.com",
97                                 'assertHTML' => false
98                         ],
99                         'bug-6857-domain-end' => [
100                                 'data' => "http://example\n.com",
101                                 'assertHTML' => false
102                         ],
103                         'bug-6857-tld' => [
104                                 'data' => "http://example.\ncom",
105                                 'assertHTML' => false
106                         ],
107                         'bug-6857-end' => [
108                                 'data' => "http://example.com\ntest",
109                                 'assertHTML' => false
110                         ],
111                         'bug-6901' => [
112                                 'data' => "http://example.com<ul>",
113                                 'assertHTML' => false
114                         ],
115                         'bug-7150' => [
116                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
117                                 'assertHTML' => false
118                         ],
119                         'bug-7271-query-string-brackets' => [
120                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
121                                 'assertHTML' => true
122                         ],
123                         'bug-7271-path-brackets' => [
124                                 'data' => 'http://example.com/path/to/file[3].html',
125                                 'assertHTML' => true
126                         ],
127                 ];
128         }
129
130         /**
131          * Test convert different links inside a text
132          * @dataProvider dataLinks
133          *
134          * @param string $data The data to text
135          * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
136          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
137          */
138         public function testAutoLinking($data, $assertHTML)
139         {
140                 $output = BBCode::convert($data);
141                 $assert = '<a href="' . $data . '" target="_blank">' . $data . '</a>';
142                 if ($assertHTML) {
143                         $this->assertEquals($assert, $output);
144                 } else {
145                         $this->assertNotEquals($assert, $output);
146                 }
147         }
148
149         public function dataBBCodes()
150         {
151                 return [
152                         'bug-7271-condensed-space' => [
153                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
154                                 'text' => '[ol][*] http://example.com/[/ol]',
155                         ],
156                         'bug-7271-condensed-nospace' => [
157                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
158                                 'text' => '[ol][*]http://example.com/[/ol]',
159                         ],
160                         'bug-7271-indented-space' => [
161                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
162                                 'text' => '[ul]
163 [*] http://example.com/
164 [/ul]',
165                         ],
166                         'bug-7271-indented-nospace' => [
167                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
168                                 'text' => '[ul]
169 [*]http://example.com/
170 [/ul]',
171                         ],
172                 ];
173         }
174
175         /**
176          * Test convert bbcodes to HTML
177          * @dataProvider dataBBCodes
178          *
179          * @param string $expectedHtml Expected HTML output
180          * @param string $text         BBCode text
181          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
182          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
183          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
184          */
185         public function testConvert($expectedHtml, $text, $simpleHtml = 0, $forPlaintext = false)
186         {
187                 $actual = BBCode::convert($text, false, $simpleHtml, $forPlaintext);
188
189                 $this->assertEquals($expectedHtml, $actual);
190         }
191 }