1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
<?php
/*
* Copyright (c) 2024 Hubzilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Zotlabs\Tests\Unit\includes;
use Zotlabs\Tests\Unit\UnitTestCase;
class BBCodeTest extends UnitTestCase {
/**
* Test converting BBCode to HTML
*
* @dataProvider bbcode_to_html_provider
*/
public function test_parsing_bbcode_to_html(string $src, string $expected): void {
$this->assertBBCode($expected, $src);
}
/**
* Test the `[observer]` BBCode tags.
*
* @dataProvider bbcode_observer_provider
*/
public function test_bbcode_observer(string $src, bool $logged_in, string $lang, string $expected): void {
if ($logged_in) {
\App::$observer = [
'xchan_addr' => '',
'xchan_name' => '',
'xchan_connurl' => '',
'xchan_photo_l' => '',
// port required in xchan url due to bug in get_rpost_path
'xchan_url' => 'https://example.com:666',
];
} else {
\App::$observer = null;
}
\App::$language = $lang;
$this->assertBBCode($expected, $src);
}
/**
* Test parsing the `[channel]` tag.
*/
public function test_bbcode_channel(): void {
$src = '[channel=1]This is only for channels[/channel][channel=0]This is for everyone else[/channel]';
// Verify that the right part is shown to users _not_ in a channel
\App::$channel = null;
$this->assertBBCode('This is for everyone else', $src);
// Now verify that the right part is shown to users _in_ a channel
\App::$channel = [42];
$this->assertBBCode('This is only for channels', $src);
}
/**
* Test converting html to BBCode.
*
* @dataProvider html2bbcode_provider
*/
public function test_html2bbcode(string $src, string $expected): void {
$this->assertEquals($expected, html2bbcode($src));
}
/**
* Helper method to validate BBCode conversions.
*
* @param string $expected The expected result of the conversion.
* @param string $src The BBCode to be converted.
*/
private function assertBBCode(string $expected, string $src): void {
// Note! We turn off trying to create oembeds, as that will trigger a
// network request when running the test.
$this->assertEquals($expected, bbcode($src, ['tryoembed' => false]));
}
/**
* Dataprovider for test_parsing_bbcode_to_html.
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function bbcode_to_html_provider(): array {
return [
'code block' => [
"[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]",
'<pre><code>testvar = "this is a test"<br />echo "the message is $testvar"</code></pre>',
],
'code block with surroundin linebreaks \n' => [
"some text\n[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]\nsome more text",
'some text<br /><pre><code>testvar = "this is a test"<br />echo "the message is $testvar"</code></pre>some more text',
],
'list with linebreaks \n' => [
"some text\n[list]\n[*] item1\n[*] item2\n[/list]\nsome more text",
'some text<br /><ul class="listbullet"><li> item1<li> item2</ul>some more text'
],
'list with linebreaks \n in text' => [
"some text\n[list]\n[*] item1\nsome text[*] item2\nsome text[/list]\nsome more text",
'some text<br /><ul class="listbullet"><li> item1<br />some text<li> item2<br />some text</ul>some more text'
],
'list with linebreaks \r' => [
"some text\r[list]\r[*] item1\r[*] item2\r[/list]\rsome more text",
'some text<br /><ul class="listbullet"><li> item1<li> item2</ul>some more text'
],
'list with linebreaks \r in text' => [
"some text\r[list]\r[*] item1\rsome text\r[*] item2\rsome text\r[/list]\rsome more text",
'some text<br /><ul class="listbullet"><li> item1<br />some text<li> item2<br />some text</ul>some more text'
],
'list with linebreaks \r\n' => [
"some text\r\n[list]\r\n[*] item1\r\n[*] item2\r\n[/list]\r\nsome more text",
'some text<br /><ul class="listbullet"><li> item1<li> item2</ul>some more text'
],
'list with linebreaks \r\n in text' => [
"some text\r\n[list]\r\n[*] item1\r\nsome text[*] item2\r\nsome text[/list]\r\nsome more text",
'some text<br /><ul class="listbullet"><li> item1<br />some text<li> item2<br />some text</ul>some more text'
]
];
}
/**
* Dataprovider for test_bbcode_observer
*
* @returns an array of arrays with the following fields:
* * `string $src` - The source string to convert
* * `bool $logged_in` - Whether we should test with a logged in user
* * `string $lang` - The language code of the simulated user
* * `string $expected` - The expecte result of the conversion.
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function bbcode_observer_provider(): array {
return [
'authenticated observer' => [
'[observer=1]This should be visible[/observer][observer=0]but not this[/observer]',
true,
'en',
'This should be visible',
],
'unauthenticated observer' => [
'[observer=1]This should not be visible[/observer][observer=0]but this should be![/observer]',
false,
'en',
'but this should be!',
],
'authenticated observer.language matching' => [
'[observer.language=nb]Kun på norsk[/observer][observer.language!=nb]To everybody else[/observer]',
true,
'nb',
'Kun på norsk',
],
'authenticated observer.language no match' => [
'[observer.language=nb]Kun på norsk[/observer][observer.language!=nb]To everybody else[/observer]',
true,
'en',
'To everybody else',
],
'multiple observer blocks' => [
'[observer=1]This should be visible,[/observer][observer=0] but not this,[/observer][observer=1] and this as well.[/observer]',
true,
'en',
'This should be visible, and this as well.',
],
'authenticated observer rpost' => [
'[rpost=a title]This is the body[/rpost]',
true,
'en',
'<a href="https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body" target="_blank" rel="nofollow noopener">https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body</a>',
],
'unauthenticated observer rpost' => [
'[rpost=a title]This is the body[/rpost]',
false,
'en',
'',
],
];
}
/**
* Dataprovider for test_html2bbcode.
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function html2bbcode_provider(): array {
return [
'paragraph over multiple lines' => [
"<p>A paragraph over\nmultiple lines\nshould be unwrapped</p>",
'A paragraph over multiple lines should be unwrapped'
],
'image with alt text' => [
'<img src="https://example.com/image.jpg" alt="Alt text">',
'[img=https://example.com/image.jpg]Alt text[/img]'
],
'code block' => [
"<pre><code>some\ncode</code></pre>",
"[code]some\ncode[/code]"
],
'code block with indentation' => [
"<pre><code>some\n indented\ncode</code></pre>",
"[code]some\n indented\ncode[/code]"
],
'paragraph with a mention and some text' => [
'<p><span class="h-card" translate="no"><a href="https://example.org/@profile" class="u-url mention">@<span>profile</span></a></span> some content</p>',
'[url=https://example.org/@profile]@profile[/url] some content'
],
'nested tags with ampersand and new line' => [
"<b>\n<i>foo & bar</i></b>",
'[b] [i]foo & bar[/i][/b]'
],
'html reshares from streams' => [
'<div><div><a href="https://example.com"><img src="https://example.com/image.jpg" alt="image/photo"></a> shared something</div>something</div>',
'[url=https://example.com][img=https://example.com/image.jpg]image/photo[/img][/url] shared something' . "\n" . 'something'
],
'list' => [
'<ul><li>list 1</li><li>list 2</li><li>list 3</li></ul>',
'[list][*]list 1[*]list 2[*]list 3[/list]'
],
'nested list' => [
'<ul><li>list 1</li><li>list 2</li><li>list 3</li><ul><li>list 1</li><li>list 2</li><li>list 3</li></ul></ul>',
'[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list]'
],
'double nested list' => [
'<ul><li>list 1</li><li>list 2</li><li>list 3</li><ul><li>list 1</li><li>list 2</li><li>list 3</li><ul><li>list 1</li><li>list 2</li><li>list 3</li></ul></ul></ul>',
'[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list][/list]'
],
'list without closing li' => [
'<ul><li>list 1<li>list 2<li>list 3</ul>',
'[list][*]list 1[*]list 2[*]list 3[/list]'
],
'nested list without closing li' => [
'<ul><li>list 1<li>list 2<li>list 3<ul><li>list 1<li>list 2<li>list 3</ul></ul>',
'[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list]'
],
'double nested list without closing li' => [
'<ul><li>list 1<li>list 2<li>list 3<ul><li>list 1<li>list 2<li>list 3<ul><li>list 1<li>list 2<li>list 3</ul></ul></ul>',
'[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list][/list]'
]
];
}
}
|