]> git.mxchange.org Git - friendica.git/blob - tests/src/Content/Text/BBCodeTest.php
df89bda31242b1bf99221211e2b29b893e2bb484
[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                 \Friendica\Core\L10n::init($l10nMock);
45
46         }
47
48         public function dataLinks()
49         {
50                 return [
51                         /** @see https://github.com/friendica/friendica/issues/2487 */
52                         'bug-2487-1' => [
53                                 'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
54                                 'assertHTML' => true,
55                         ],
56                         'bug-2487-2' => [
57                                 'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
58                                 'assertHTML' => true,
59                         ],
60                         'bug-2487-3' => [
61                                 'data' => 'https://friendica.wäckerlin.ch/friendica',
62                                 'assertHTML' => true,
63                         ],
64                         'bug-2487-4' => [
65                                 'data' => 'https://mastodon.social/@morevnaproject',
66                                 'assertHTML' => true,
67                         ],
68                         /** @see https://github.com/friendica/friendica/issues/5795 */
69                         'bug-5795' => [
70                                 'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
71                                 'assertHTML' => true,
72                         ],
73                         /** @see https://github.com/friendica/friendica/issues/6095 */
74                         'bug-6095' => [
75                                 'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
76                                 'assertHTML' => true,
77                         ],
78                         'no-protocol' => [
79                                 'data' => 'example.com/path',
80                                 'assertHTML' => false
81                         ],
82                         'wrong-protocol' => [
83                                 'data' => 'ftp://example.com',
84                                 'assertHTML' => false
85                         ],
86                         'wrong-domain-without-path' => [
87                                 'data' => 'http://example',
88                                 'assertHTML' => false
89                         ],
90                         'wrong-domain-with-path' => [
91                                 'data' => 'http://example/path',
92                                 'assertHTML' => false
93                         ],
94                         'bug-6857-domain-start' => [
95                                 'data' => "http://\nexample.com",
96                                 'assertHTML' => false
97                         ],
98                         'bug-6857-domain-end' => [
99                                 'data' => "http://example\n.com",
100                                 'assertHTML' => false
101                         ],
102                         'bug-6857-tld' => [
103                                 'data' => "http://example.\ncom",
104                                 'assertHTML' => false
105                         ],
106                         'bug-6857-end' => [
107                                 'data' => "http://example.com\ntest",
108                                 'assertHTML' => false
109                         ],
110                         'bug-6901' => [
111                                 'data' => "http://example.com<ul>",
112                                 'assertHTML' => false
113                         ],
114                         'bug-7150' => [
115                                 'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
116                                 'assertHTML' => false
117                         ],
118                         'bug-7271-query-string-brackets' => [
119                                 'data' => 'https://example.com/search?q=square+brackets+[url]',
120                                 'assertHTML' => true
121                         ],
122                         'bug-7271-path-brackets' => [
123                                 'data' => 'http://example.com/path/to/file[3].html',
124                                 'assertHTML' => true
125                         ],
126                 ];
127         }
128
129         /**
130          * Test convert different links inside a text
131          * @dataProvider dataLinks
132          *
133          * @param string $data The data to text
134          * @param bool $assertHTML True, if the link is a HTML link (<a href...>...</a>)
135          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136          */
137         public function testAutoLinking($data, $assertHTML)
138         {
139                 $output = BBCode::convert($data);
140                 $assert = '<a href="' . $data . '" target="_blank">' . $data . '</a>';
141                 if ($assertHTML) {
142                         $this->assertEquals($assert, $output);
143                 } else {
144                         $this->assertNotEquals($assert, $output);
145                 }
146         }
147
148         public function dataBBCodes()
149         {
150                 return [
151                         'bug-7271-condensed-space' => [
152                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
153                                 'text' => '[ol][*] http://example.com/[/ol]',
154                         ],
155                         'bug-7271-condensed-nospace' => [
156                                 'expectedHtml' => '<ul class="listdecimal" style="list-style-type: decimal;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
157                                 'text' => '[ol][*]http://example.com/[/ol]',
158                         ],
159                         'bug-7271-indented-space' => [
160                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li> <a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
161                                 'text' => '[ul]
162 [*] http://example.com/
163 [/ul]',
164                         ],
165                         'bug-7271-indented-nospace' => [
166                                 'expectedHtml' => '<ul class="listbullet" style="list-style-type: circle;"><li><a href="http://example.com/" target="_blank">http://example.com/</a></li></ul>',
167                                 'text' => '[ul]
168 [*]http://example.com/
169 [/ul]',
170                         ],
171                 ];
172         }
173
174         /**
175          * Test convert bbcodes to HTML
176          * @dataProvider dataBBCodes
177          *
178          * @param string $expectedHtml Expected HTML output
179          * @param string $text         BBCode text
180          * @param int    $simpleHtml   BBCode::convert method $simple_html parameter value, optional.
181          * @param bool   $forPlaintext BBCode::convert method $for_plaintext parameter value, optional.
182          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
183          */
184         public function testConvert($expectedHtml, $text, $simpleHtml = 0, $forPlaintext = false)
185         {
186                 $actual = BBCode::convert($text, false, $simpleHtml, $forPlaintext);
187
188                 $this->assertEquals($expectedHtml, $actual);
189         }
190 }