Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.33% covered (danger)
8.33%
19 / 228
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_bbcode_parser_test
8.33% covered (danger)
8.33%
19 / 228
0.00% covered (danger)
0.00%
0 / 2
9.93
0.00% covered (danger)
0.00%
0 / 1
 bbcode_firstpass_data
0.00% covered (danger)
0.00%
0 / 208
0.00% covered (danger)
0.00%
0 / 1
2
 test_bbcode_firstpass
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14require_once __DIR__ . '/../../phpBB/includes/bbcode.php';
15require_once __DIR__ . '/../../phpBB/includes/message_parser.php';
16
17class phpbb_bbcode_parser_test extends \phpbb_test_case
18{
19    public static function bbcode_firstpass_data()
20    {
21        return array(
22            // Default bbcodes from in their simplest way
23            array(
24                'Test default bbcodes: simple bold',
25                '[b]bold[/b]',
26                '[b:]bold[/b:]',
27            ),
28            array(
29                'Test default bbcodes: simple underlined',
30                '[u]underlined[/u]',
31                '[u:]underlined[/u:]',
32            ),
33            array(
34                'Test default bbcodes: simple italic',
35                '[i]italic[/i]',
36                '[i:]italic[/i:]',
37            ),
38            array(
39                'Test default bbcodes: simple color rgb',
40                '[color=#FF0000]colored[/color]',
41                '[color=#FF0000:]colored[/color:]',
42            ),
43            array(
44                'Test default bbcodes: simple color name',
45                '[color=red]colored[/color]',
46                '[color=red:]colored[/color:]',
47            ),
48            array(
49                'Test default bbcodes: simple size',
50                '[size=75]smaller[/size]',
51                '[size=75:]smaller[/size:]',
52            ),
53            array(
54                'Test default bbcodes: simple quote',
55                '[quote]quoted[/quote]',
56                '[quote:]quoted[/quote:]',
57            ),
58            array(
59                'Test default bbcodes: simple quote with username',
60                '[quote=&quot;username&quot;]quoted[/quote]',
61                '[quote=&quot;username&quot;:]quoted[/quote:]',
62            ),
63            array(
64                'Test default bbcodes: simple code',
65                '[code]unparsed code[/code]',
66                '[code:]unparsed code[/code:]',
67            ),
68            array(
69                'Test default bbcodes: simple list',
70                '[list]no item[/list]',
71                '[list:]no item[/list:u:]',
72            ),
73            array(
74                'Test default bbcodes: simple list-item only',
75                '[*]unparsed',
76                '[*]unparsed',
77            ),
78            array(
79                'Test default bbcodes: simple list-item',
80                '[list][*]item[/list]',
81                '[list:][*:]item[/*:m:][/list:u:]',
82            ),
83            array(
84                'Test default bbcodes: simple list-item closed',
85                '[list][*]item[/*][/list]',
86                '[list:][*:]item[/*:][/list:u:]',
87            ),
88            array(
89                'Test default bbcodes: simple list-item numbered',
90                '[list=1][*]item[/list]',
91                '[list=1:][*:]item[/*:m:][/list:o:]',
92            ),
93            array(
94                'Test default bbcodes: simple list-item alpha',
95                '[list=a][*]item[/list]',
96                '[list=a:][*:]item[/*:m:][/list:o:]',
97            ),
98            array(
99                'Test default bbcodes: simple list-item roman',
100                '[list=i][*]item[/list]',
101                '[list=i:][*:]item[/*:m:][/list:o:]',
102            ),
103            array(
104                'Test default bbcodes: simple list-item disc',
105                '[list=disc][*]item[/list]',
106                '[list=disc:][*:]item[/*:m:][/list:u:]',
107            ),
108            array(
109                'Test default bbcodes: simple list-item circle',
110                '[list=circle][*]item[/list]',
111                '[list=circle:][*:]item[/*:m:][/list:u:]',
112            ),
113            array(
114                'Test default bbcodes: simple list-item square',
115                '[list=square][*]item[/list]',
116                '[list=square:][*:]item[/*:m:][/list:u:]',
117            ),
118            array(
119                'Test default bbcodes: simple img',
120                '[img]https://area51.phpbb.com/images/area51.png[/img]',
121                '[img:]https&#58;//area51&#46;phpbb&#46;com/images/area51&#46;png[/img:]',
122            ),
123            array(
124                'Test default bbcodes: img with unsupported protocol',
125                '[img]foo://foo/bar[/img]',
126                '[img]foo://foo/bar[/img]',
127            ),
128            array(
129                'Test default bbcodes: simple url',
130                '[url]https://area51.phpbb.com/[/url]',
131                '[url:]https&#58;//area51&#46;phpbb&#46;com/[/url:]',
132            ),
133            array(
134                'Test default bbcodes: simple url with description',
135                '[url=https://area51.phpbb.com/]Area51[/url]',
136                '[url=https&#58;//area51&#46;phpbb&#46;com/:]Area51[/url:]',
137            ),
138            array(
139                'Test default bbcodes: simple email',
140                '[email]bbcode-test@phpbb.com[/email]',
141                '[email:]bbcode-test@phpbb&#46;com[/email:]',
142            ),
143            array(
144                'Test default bbcodes: simple email with description',
145                '[email=bbcode-test@phpbb.com]Email[/email]',
146                '[email=bbcode-test@phpbb&#46;com:]Email[/email:]',
147            ),
148            array(
149                'Test default bbcodes: simple attachment',
150                '[attachment=0]filename[/attachment]',
151                '[attachment=0:]<!-- ia0 -->filename<!-- ia0 -->[/attachment:]',
152            ),
153
154            // Special cases for quote which were reported as bugs before
155            array(
156                'PHPBB3-1401 - correct: parsed',
157                '[quote=&quot;&#91;test]test&quot;]test [ test[/quote]',
158                '[quote=&quot;&#91;test]test&quot;:]test [ test[/quote:]',
159            ),
160            array(
161                'PHPBB3-6117 - correct: parsed',
162                '[quote]test[/quote] test ] and [ test [quote]test[/quote]',
163                '[quote:]test[/quote:] test ] and [ test [quote:]test[/quote:]',
164            ),
165            array(
166                'PHPBB3-6200 - correct: parsed',
167                '[quote=&quot;[&quot;]test[/quote]',
168                '[quote=&quot;&#91;&quot;:]test[/quote:]',
169            ),
170            array(
171                'PHPBB3-9364 - quoted: "test[/[/b]quote] test" / non-quoted: "[/quote] test" - also failed if layout distorted',
172                '[quote]test[/[/b]quote] test [/quote][/quote] test',
173                '[quote:]test[/[/b]quote] test [/quote:][/quote] test',
174            ),
175            array(
176                'PHPBB3-8096 - first quote tag parsed, second quote tag unparsed',
177                '[quote=&quot;a&quot;]a[/quote][quote=&quot;a]a[/quote]',
178                '[quote=&quot;a&quot;:]a[/quote:][quote=&quot;a]a[/quote]',
179            ),
180
181            // Simple bbcodes nesting
182            array(
183                'Allow textual bbcodes in textual bbcodes',
184                '[b]bold [i]bold + italic[/i][/b]',
185                '[b:]bold [i:]bold + italic[/i:][/b:]',
186            ),
187            array(
188                'Allow textual bbcodes in url with description',
189                '[url=https://area51.phpbb.com/]Area51 [i]italic[/i][/url]',
190                '[url=https&#58;//area51&#46;phpbb&#46;com/:]Area51 [i:]italic[/i:][/url:]',
191            ),
192            array(
193                'Allow url with description in textual bbcodes',
194                '[i]italic [url=https://area51.phpbb.com/]Area51[/url][/i]',
195                '[i:]italic [url=https&#58;//area51&#46;phpbb&#46;com/:]Area51[/url:][/i:]',
196            ),
197
198            // Nesting bbcodes into quote usernames
199            array(
200                'Allow textual bbcodes in usernames',
201                '[quote=&quot;[i]test[/i]&quot;]test[/quote]',
202                '[quote=&quot;[i:]test[/i:]&quot;:]test[/quote:]',
203            ),
204            array(
205                'Allow links bbcodes in usernames',
206                '[quote=&quot;[url=https://area51.phpbb.com/]test[/url]&quot;]test[/quote]',
207                '[quote=&quot;[url=https&#58;//area51&#46;phpbb&#46;com/:]test[/url:]&quot;:]test[/quote:]',
208            ),
209            array(
210                'Allow img bbcodes in usernames - Username displayed the image',
211                '[quote=&quot;[img]https://area51.phpbb.com/images/area51.png[/img]&quot;]test[/quote]',
212                '[quote=&quot;[img:]https&#58;//area51&#46;phpbb&#46;com/images/area51&#46;png[/img:]&quot;:]test[/quote:]',
213            ),
214            array(
215                'Disallow quote bbcodes in usernames - Username displayed as [quote]test[/quote]',
216                '[quote=&quot;[quote]test[/quote]&quot;]test[/quote]',
217                '[quote=&quot;&#91;quote]test&#91;/quote]&quot;:]test[/quote:]',
218            ),
219
220            // Do not parse bbcodes in code boxes
221            array(
222                'Do not parse textual bbcodes in code',
223                '[code]unparsed code [b]bold [i]bold + italic[/i][/b][/code]',
224                '[code:]unparsed code &#91;b&#93;bold &#91;i&#93;bold + italic&#91;/i&#93;&#91;/b&#93;[/code:]',
225            ),
226            array(
227                'Do not parse quote bbcodes in code',
228                '[code]unparsed code [quote=&quot;username&quot;]quoted[/quote][/code]',
229                '[code:]unparsed code &#91;quote=&quot;username&quot;&#93;quoted&#91;/quote&#93;[/code:]',
230            ),
231
232            // New user friendly mixed nesting
233            array(
234                'Textual bbcode nesting into textual bbcode',
235                '[b]bold [i]bold + italic[/b] italic[/i]',
236                '[b:]bold [i:]bold + italic[/b:] italic[/i:]',
237                'Incomplete test case: secondpass parses as [b:]bold [i:]bold + italic[/i:] italic[/b:]',
238            ),
239        );
240    }
241
242    /**
243    * @dataProvider bbcode_firstpass_data
244    */
245    public function test_bbcode_firstpass($description, $message, $expected, $incomplete = false)
246    {
247        if ($incomplete)
248        {
249            $this->markTestIncomplete($incomplete);
250        }
251
252        global $user, $request, $symfony_request, $phpbb_dispatcher, $config, $phpEx;
253        $phpEx = 'php';
254        $config = new \phpbb\config\config([
255            'max_post_font_size' => 0,
256            'force_server_vars' => 0,
257            'server_name' => 'testhost',
258        ]);
259        $user = new phpbb_mock_user;
260        $user->lang['UNAUTHORISED_BBCODE'] = 'UNAUTHORISED_BBCODE';
261        $request = new phpbb_mock_request;
262        $symfony_request = new \phpbb\symfony_request($request);
263        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
264
265        $bbcode = new bbcode_firstpass();
266        $bbcode->mode = 'post';
267        $bbcode->message = $message;
268        $bbcode->bbcode_init(false);
269        $bbcode->parse_bbcode();
270        $this->assertEquals($expected, $bbcode->message);
271    }
272}