Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.65% covered (warning)
80.65%
275 / 341
36.36% covered (danger)
36.36%
4 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 2
phpbb_text_processing_message_parser_test
82.83% covered (warning)
82.83%
275 / 332
57.14% covered (warning)
57.14%
4 / 7
8.32
0.00% covered (danger)
0.00%
0 / 1
 setUpBeforeClass
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 tearDownAfterClass
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 prepare_s9e_services
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
2
 test_parse_poll
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 get_test_polls
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
2
 test_options
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 get_test_cases
100.00% covered (success)
100.00%
214 / 214
100.00% covered (success)
100.00%
1 / 1
1
phpbb_text_processing_message_parser_test_proxy
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 stream_open
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 stream_stat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 stream_read
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 stream_eof
0.00% covered (danger)
0.00%
0 / 1
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_text_processing_message_parser_test extends phpbb_test_case
18{
19    static public function setUpBeforeClass(): void
20    {
21        parent::setUpBeforeClass();
22
23        // Set up an intercepting proxy for getimagesize() calls
24        stream_wrapper_unregister('http');
25        stream_wrapper_register('http', __CLASS__ . '_proxy');
26    }
27
28    static public function tearDownAfterClass(): void
29    {
30        parent::tearDownAfterClass();
31        stream_wrapper_restore('http');
32    }
33
34    protected function prepare_s9e_services($setup = null)
35    {
36        global $config, $phpbb_container, $user;
37
38        $config = new \phpbb\config\config(array('max_poll_options' => 999));
39
40        $map = array(
41            array('MAX_FONT_SIZE_EXCEEDED', 120, 'You may only use fonts up to size 120.'),
42            array('MAX_FONT_SIZE_EXCEEDED', 200, 'You may only use fonts up to size 200.'),
43            array('TOO_MANY_SMILIES', 3, 'Your message contains too many smilies. The maximum number of smilies allowed is 3.'),
44            array('TOO_MANY_URLS', 2, 'Your message contains too many URLs. The maximum number of URLs allowed is 2.'),
45            array('UNAUTHORISED_BBCODE', '[img]', 'You cannot use certain BBCodes: [img].'),
46            array('UNAUTHORISED_BBCODE', '[quote]', 'You cannot use certain BBCodes: [quote].'),
47            array('UNAUTHORISED_BBCODE', '[url]', 'You cannot use certain BBCodes: [url].'),
48            array('UNABLE_GET_IMAGE_SIZE', 'It was not possible to determine the dimensions of the image.'),
49        );
50
51        $user = $this->getMockBuilder('phpbb\\user')->disableOriginalConstructor()->getMock();
52        $user->expects($this->any())
53             ->method('lang')
54             ->will($this->returnValueMap($map));
55
56        $user->data = array(
57            'is_bot' => false,
58            'is_registered' => true,
59            'user_id' => 2,
60        );
61        $user->style = array('style_id' => 1);
62
63        $user->expects($this->any())
64            ->method('__get')->with('lang')->willReturn([
65                'NO_POLL_TITLE' => 'You have to enter a poll title.',
66                'POLL_TITLE_TOO_LONG' => 'The poll title must contain fewer than 100 characters.',
67                'POLL_TITLE_COMP_TOO_LONG' => 'The parsed size of your poll title is too large, consider removing BBCodes or smilies.',
68                'TOO_FEW_POLL_OPTIONS' => 'You must enter at least two poll options.',
69                'TOO_MANY_POLL_OPTIONS' => 'You have tried to enter too many poll options.',
70                'TOO_MANY_USER_OPTIONS' => 'You cannot specify more options per user than existing poll options.',
71            ]);
72
73        $phpbb_container = new phpbb_mock_container_builder;
74        $phpbb_container->set('user', $user);
75        $phpbb_container->set('config', $config);
76
77        if (isset($setup))
78        {
79            $setup($phpbb_container, $this);
80        }
81
82        $this->get_test_case_helpers()->set_s9e_services($phpbb_container);
83    }
84
85    /**
86    * @dataProvider get_test_polls
87    */
88    public function test_parse_poll($poll, $expected, $warn_msg = array())
89    {
90        $this->prepare_s9e_services();
91
92        $message_parser = new parse_message('Me[i]s[/i]sage');
93
94        // Add some default values
95        $poll += array(
96            'poll_length'        => 123,
97            'poll_start'        => 123,
98            'poll_last_vote'    => 123,
99            'poll_vote_change'    => true,
100            'enable_bbcode'        => true,
101            'enable_urls'        => true,
102            'enable_smilies'    => true,
103            'img_status'        => true
104        );
105
106        $message_parser->parse_poll($poll);
107        $this->assertSame($expected, array_intersect_key($poll, $expected));
108
109        $this->assertSame(
110            '<r>Me<I><s>[i]</s>s<e>[/i]</e></I>sage</r>',
111            $message_parser->parse(true, true, true, true, true, true, false)
112        );
113
114        $this->assertSame($warn_msg, $message_parser->warn_msg);
115    }
116
117    public static function get_test_polls()
118    {
119        return array(
120            array(
121                array(
122                    'poll_title' => 'foo [b]bar[/b] baz',
123                    'poll_option_text' => "[i]foo[/i]\nbar\n[i]baz[/i]",
124                    'poll_max_options'    => 3,
125                    'poll_options_size' => 3
126                ),
127                array(
128                    'poll_title' => '<r>foo <B><s>[b]</s>bar<e>[/b]</e></B> baz</r>',
129                    'poll_option_text' => "<r><I><s>[i]</s>foo<e>[/i]</e></I></r>\n<t>bar</t>\n<r><I><s>[i]</s>baz<e>[/i]</e></I></r>",
130                    'poll_options' => array(
131                        '<r><I><s>[i]</s>foo<e>[/i]</e></I></r>',
132                        '<t>bar</t>',
133                        '<r><I><s>[i]</s>baz<e>[/i]</e></I></r>'
134                    )
135                )
136            ),
137            array(
138                array(
139                    'poll_title' => 'xxx',
140                    'poll_option_text' => "[quote]quote[/quote]\n:)",
141                    'poll_max_options'    => 2,
142                    'poll_options_size' => 2
143                ),
144                array(
145                    'poll_title' => '<t>xxx</t>',
146                    'poll_option_text' => "<t>[quote]quote[/quote]</t>\n<r><E>:)</E></r>",
147                    'poll_options' => array(
148                        '<t>[quote]quote[/quote]</t>',
149                        '<r><E>:)</E></r>'
150                    )
151                ),
152                array('You cannot use certain BBCodes: [quote].')
153            ),
154            array(
155                array(
156                    'poll_title' => 'xxx',
157                    'poll_option_text' => "[b]x\ny[/b]",
158                    'poll_max_options'    => 2,
159                    'poll_options_size' => 2
160                ),
161                array(
162                    'poll_title' => '<t>xxx</t>',
163                    'poll_option_text' => "<r><B><s>[b]</s>x</B></r>\n<t>y[/b]</t>",
164                    'poll_options' => array(
165                        '<r><B><s>[b]</s>x</B></r>',
166                        '<t>y[/b]</t>',
167                    )
168                )
169            ),
170        );
171    }
172
173    /**
174    * @dataProvider get_test_cases
175    */
176    public function test_options($original, $expected, array $args, $setup = null, $warn_msg = array())
177    {
178        $this->prepare_s9e_services($setup);
179
180        $message_parser = new parse_message($original);
181        call_user_func_array(array($message_parser, 'parse'), $args);
182
183        $this->assertSame($expected, $message_parser->message);
184        $this->assertSame($warn_msg, $message_parser->warn_msg);
185    }
186
187    public static function get_test_cases()
188    {
189        return array(
190            array(
191                '[b]bold[/b]',
192                '<r><B><s>[b]</s>bold<e>[/b]</e></B></r>',
193                array(true, true, true, true, true, true)
194            ),
195            array(
196                '[b]bold[/b]',
197                '<t>[b]bold[/b]</t>',
198                array(false, true, true, true, true, true)
199            ),
200            array(
201                'http://example.org',
202                '<r><URL url="http://example.org">http://example.org</URL></r>',
203                array(true, true, true, true, true, true)
204            ),
205            array(
206                'http://example.org',
207                '<t>http://example.org</t>',
208                array(true, false, true, true, true, true)
209            ),
210            array(
211                ':)',
212                '<r><E>:)</E></r>',
213                array(true, true, true, true, true, true)
214            ),
215            array(
216                ':)',
217                '<t>:)</t>',
218                array(true, true, false, true, true, true)
219            ),
220            array(
221                '[url=http://example.org][img]http://example.org/img.png[/img][/url]',
222                '<r><URL url="http://example.org"><s>[url=http://example.org]</s><IMG src="http://example.org/img.png"><s>[img]</s>http://example.org/img.png<e>[/img]</e></IMG><e>[/url]</e></URL></r>',
223                array(true, true, true, true, true, true)
224            ),
225            array(
226                '[url=http://example.org][img]http://example.org/img.png[/img][/url]',
227                '<r><URL url="http://example.org"><s>[url=http://example.org]</s>[img]http://example.org/img.png[/img]<e>[/url]</e></URL></r>',
228                array(true, true, true, false, true, true),
229                null,
230                array('You cannot use certain BBCodes: [img].')
231            ),
232            array(
233                '[flash=12,34]http://example.org/foo.swf[/flash]',
234                '<r>[flash=12,34]<URL url="http://example.org/foo.swf">http://example.org/foo.swf</URL>[/flash]</r>',
235                array(true, true, true, true, true, true)
236            ),
237            array(
238                '[quote="foo"]bar :)[/quote]',
239                '<r><QUOTE author="foo"><s>[quote="foo"]</s>bar <E>:)</E><e>[/quote]</e></QUOTE></r>',
240                array(true, true, true, true, true, true)
241            ),
242            array(
243                '[quote="foo"]bar :)[/quote]',
244                '<r>[quote="foo"]bar <E>:)</E>[/quote]</r>',
245                array(true, true, true, true, false, true),
246                null,
247                array('You cannot use certain BBCodes: [quote].')
248            ),
249            array(
250                '[url=http://example.org][img]http://example.org/img.png[/img][/url]',
251                '<r><URL url="http://example.org"><s>[url=http://example.org]</s><IMG src="http://example.org/img.png"><s>[img]</s>http://example.org/img.png<e>[/img]</e></IMG><e>[/url]</e></URL></r>',
252                array(true, true, true, true, true, true)
253            ),
254            array(
255                '[url=http://example.org][img]http://example.org/img.png[/img][/url]',
256                '<r>[url=http://example.org]<IMG src="http://example.org/img.png"><s>[img]</s>http://example.org/img.png<e>[/img]</e></IMG>[/url]</r>',
257                array(true, true, true, true, true, false),
258                null,
259                array('You cannot use certain BBCodes: [url].')
260            ),
261            array(
262                '[size=200]200[/size]',
263                '<r><SIZE size="200"><s>[size=200]</s>200<e>[/size]</e></SIZE></r>',
264                array(true, true, true, true, true, true),
265                function ($phpbb_container)
266                {
267                    $phpbb_container->get('config')->set('max_post_font_size', 200);
268                }
269            ),
270            array(
271                '[size=200]200[/size]',
272                '<r><SIZE size="200"><s>[size=200]</s>200<e>[/size]</e></SIZE></r>',
273                array(true, true, true, true, true, true),
274                function ($phpbb_container)
275                {
276                    $phpbb_container->get('config')->set('max_post_font_size', 0);
277                }
278            ),
279            array(
280                '[size=2000]2000[/size]',
281                '<t>[size=2000]2000[/size]</t>',
282                array(true, true, true, true, true, true),
283                function ($phpbb_container)
284                {
285                    $phpbb_container->get('config')->set('max_post_font_size', 200);
286                },
287                array('You may only use fonts up to size 200.')
288            ),
289            array(
290                '[size=0]0[/size]',
291                '<t>[size=0]0[/size]</t>',
292                array(true, true, true, true, true, true),
293                function ($phpbb_container)
294                {
295                    $phpbb_container->get('config')->set('max_post_font_size', 200);
296                }
297            ),
298            array(
299                '[size=200]200[/size]',
300                '<r><SIZE size="200"><s>[size=200]</s>200<e>[/size]</e></SIZE></r>',
301                array(true, true, true, true, true, true),
302                function ($phpbb_container)
303                {
304                    $phpbb_container->get('config')->set('max_sig_font_size', 200);
305                }
306            ),
307            array(
308                '[size=200]200[/size]',
309                '<t>[size=200]200[/size]</t>',
310                array(true, true, true, true, true, true, true, 'sig'),
311                function ($phpbb_container)
312                {
313                    $phpbb_container->get('config')->set('max_sig_font_size', 120);
314                },
315                array('You may only use fonts up to size 120.')
316            ),
317            array(
318                '[img]http://example.org/100x100.png[/img]',
319                '<r><IMG src="http://example.org/100x100.png"><s>[img]</s><URL url="http://example.org/100x100.png">http://example.org/100x100.png</URL><e>[/img]</e></IMG></r>',
320                array(true, true, true, true, true, true),
321                function ($phpbb_container)
322                {
323                    $phpbb_container->get('config')->set('max_sig_img_height', 12);
324                    $phpbb_container->get('config')->set('max_sig_img_width', 34);
325                }
326            ),
327            array(
328                ':) :) :)',
329                '<r><E>:)</E> <E>:)</E> <E>:)</E></r>',
330                array(true, true, true, true, true, true, true),
331                function ($phpbb_container)
332                {
333                    $phpbb_container->get('config')->set('max_post_smilies', 3);
334                }
335            ),
336            array(
337                ':) :) :) :)',
338                '<r><E>:)</E> <E>:)</E> <E>:)</E> :)</r>',
339                array(true, true, true, true, true, true, true),
340                function ($phpbb_container)
341                {
342                    $phpbb_container->get('config')->set('max_post_smilies', 3);
343                },
344                array('Your message contains too many smilies. The maximum number of smilies allowed is 3.')
345            ),
346            array(
347                ':) :) :) :)',
348                '<r><E>:)</E> <E>:)</E> <E>:)</E> <E>:)</E></r>',
349                array(true, true, true, true, true, true, true),
350                function ($phpbb_container)
351                {
352                    $phpbb_container->get('config')->set('max_post_smilies', 0);
353                }
354            ),
355            array(
356                ':) :) :) :)',
357                '<r><E>:)</E> <E>:)</E> <E>:)</E> <E>:)</E></r>',
358                array(true, true, true, true, true, true, true),
359                function ($phpbb_container)
360                {
361                    $phpbb_container->get('config')->set('max_sig_smilies', 3);
362                }
363            ),
364            array(
365                ':) :) :) :)',
366                '<r><E>:)</E> <E>:)</E> <E>:)</E> :)</r>',
367                array(true, true, true, true, true, true, true, 'sig'),
368                function ($phpbb_container)
369                {
370                    $phpbb_container->get('config')->set('max_sig_smilies', 3);
371                },
372                array('Your message contains too many smilies. The maximum number of smilies allowed is 3.')
373            ),
374            array(
375                'http://example.org http://example.org http://example.org',
376                '<r><URL url="http://example.org">http://example.org</URL> <URL url="http://example.org">http://example.org</URL> http://example.org</r>',
377                array(true, true, true, true, true, true, true),
378                function ($phpbb_container)
379                {
380                    $phpbb_container->get('config')->set('max_post_urls', 2);
381                },
382                array('Your message contains too many URLs. The maximum number of URLs allowed is 2.')
383            ),
384            array(
385                'http://example.org http://example.org http://example.org',
386                '<r><URL url="http://example.org">http://example.org</URL> <URL url="http://example.org">http://example.org</URL> <URL url="http://example.org">http://example.org</URL></r>',
387                array(true, true, true, true, true, true, true),
388                function ($phpbb_container)
389                {
390                    $phpbb_container->get('config')->set('max_post_urls', 0);
391                }
392            ),
393            array(
394                'http://example.org http://example.org http://example.org',
395                '<r><URL url="http://example.org">http://example.org</URL> <URL url="http://example.org">http://example.org</URL> <URL url="http://example.org">http://example.org</URL></r>',
396                array(true, true, true, true, true, true, true),
397                function ($phpbb_container)
398                {
399                    $phpbb_container->get('config')->set('max_sig_urls', 2);
400                }
401            ),
402        );
403    }
404}
405
406class phpbb_text_processing_message_parser_test_proxy
407{
408    protected $response;
409
410    public function stream_open($url)
411    {
412        if (strpos($url, '100x100'))
413        {
414            // Return a 100 x 100 PNG image
415            $this->response = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQAAAABYmaj5AAAAE0lEQVR4AWOgKxgFo2AUjIJRAAAFeAABHs0ozQAAAABJRU5ErkJggg==');
416        }
417        else
418        {
419            $this->response = '404 not found';
420        }
421
422        return true;
423    }
424
425    public function stream_stat()
426    {
427        return false;
428    }
429
430    public function stream_read($len)
431    {
432        $chunk = substr($this->response, 0, $len);
433        $this->response = substr($this->response, $len);
434
435        return $chunk;
436    }
437
438    public function stream_eof()
439    {
440        return ($this->response === false);
441    }
442}