* @license GNU General Public License, version 2 (GPL-2.0) * * For full copyright and license information, please see * the docs/CREDITS.txt file. * */ require_once __DIR__ . '/../../phpBB/includes/bbcode.php'; require_once __DIR__ . '/../../phpBB/includes/message_parser.php'; class phpbb_text_processing_message_parser_test extends phpbb_test_case { public static function setUpBeforeClass() { parent::setUpBeforeClass(); // Set up an intercepting proxy for getimagesize() calls stream_wrapper_unregister('http'); stream_wrapper_register('http', __CLASS__ . '_proxy'); } public static function tearDownAfterClass() { parent::tearDownAfterClass(); stream_wrapper_restore('http'); } protected function prepare_s9e_services($setup = null) { global $config, $phpbb_container, $user; $config = new \phpbb\config\config(array('max_poll_options' => 999)); $map = array( array('MAX_FLASH_HEIGHT_EXCEEDED', 123, 'Your flash files may only be up to 123 pixels high.'), array('MAX_FLASH_WIDTH_EXCEEDED', 456, 'Your flash files may only be up to 456 pixels wide.'), array('MAX_FONT_SIZE_EXCEEDED', 120, 'You may only use fonts up to size 120.'), array('MAX_FONT_SIZE_EXCEEDED', 200, 'You may only use fonts up to size 200.'), array('MAX_IMG_HEIGHT_EXCEEDED', 12, 'Your images may only be up to 12 pixels high.'), array('MAX_IMG_WIDTH_EXCEEDED', 34, 'Your images may only be up to 34 pixels wide.'), array('TOO_MANY_SMILIES', 3, 'Your message contains too many smilies. The maximum number of smilies allowed is 3.'), array('TOO_MANY_URLS', 2, 'Your message contains too many URLs. The maximum number of URLs allowed is 2.'), array('UNAUTHORISED_BBCODE', '[flash]', 'You cannot use certain BBCodes: [flash].'), array('UNAUTHORISED_BBCODE', '[img]', 'You cannot use certain BBCodes: [img].'), array('UNAUTHORISED_BBCODE', '[quote]', 'You cannot use certain BBCodes: [quote].'), array('UNAUTHORISED_BBCODE', '[url]', 'You cannot use certain BBCodes: [url].'), array('UNABLE_GET_IMAGE_SIZE', 'It was not possible to determine the dimensions of the image.'), ); $user = $this->getMockBuilder('phpbb\\user')->disableOriginalConstructor()->getMock(); $user->expects($this->any()) ->method('lang') ->will($this->returnValueMap($map)); $user->data = array( 'is_bot' => false, 'is_registered' => true, 'user_id' => 2, ); $user->style = array('style_id' => 1); $user->lang = array( 'NO_POLL_TITLE' => 'You have to enter a poll title.', 'POLL_TITLE_TOO_LONG' => 'The poll title must contain fewer than 100 characters.', 'POLL_TITLE_COMP_TOO_LONG' => 'The parsed size of your poll title is too large, consider removing BBCodes or smilies.', 'TOO_FEW_POLL_OPTIONS' => 'You must enter at least two poll options.', 'TOO_MANY_POLL_OPTIONS' => 'You have tried to enter too many poll options.', 'TOO_MANY_USER_OPTIONS' => 'You cannot specify more options per user than existing poll options.', ); $phpbb_container = new phpbb_mock_container_builder; $phpbb_container->set('user', $user); $phpbb_container->set('config', $config); if (isset($setup)) { $setup($phpbb_container, $this); } $this->get_test_case_helpers()->set_s9e_services($phpbb_container); } /** * @dataProvider get_test_polls */ public function test_parse_poll($poll, $expected, $warn_msg = array()) { $this->prepare_s9e_services(); $message_parser = new parse_message('Me[i]s[/i]sage'); // Add some default values $poll += array( 'poll_length' => 123, 'poll_start' => 123, 'poll_last_vote' => 123, 'poll_vote_change' => true, 'enable_bbcode' => true, 'enable_urls' => true, 'enable_smilies' => true, 'img_status' => true ); $message_parser->parse_poll($poll); $this->assertSame($expected, array_intersect_key($poll, $expected)); $this->assertSame( 'Me[i]s[/i]sage', $message_parser->parse(true, true, true, true, true, true, true, false) ); $this->assertSame($warn_msg, $message_parser->warn_msg); } public function get_test_polls() { return array( array( array( 'poll_title' => 'foo [b]bar[/b] baz', 'poll_option_text' => "[i]foo[/i]\nbar\n[i]baz[/i]", 'poll_max_options' => 3, 'poll_options_size' => 3 ), array( 'poll_title' => 'foo [b]bar[/b] baz', 'poll_option_text' => "[i]foo[/i]\nbar\n[i]baz[/i]", 'poll_options' => array( '[i]foo[/i]', 'bar', '[i]baz[/i]' ) ) ), array( array( 'poll_title' => 'xxx', 'poll_option_text' => "[quote]quote[/quote]\n:)", 'poll_max_options' => 2, 'poll_options_size' => 2 ), array( 'poll_title' => 'xxx', 'poll_option_text' => "[quote]quote[/quote]\n:)", 'poll_options' => array( '[quote]quote[/quote]', ':)' ) ), array('You cannot use certain BBCodes: [quote].') ), array( array( 'poll_title' => 'xxx', 'poll_option_text' => "[flash=12,34]http://example.org/x.swf[/flash]\n:)", 'poll_max_options' => 2, 'poll_options_size' => 2 ), array( 'poll_title' => 'xxx', 'poll_option_text' => "[flash=12,34]http://example.org/x.swf[/flash]\n:)", 'poll_options' => array( '[flash=12,34]http://example.org/x.swf[/flash]', ':)' ) ), array('You cannot use certain BBCodes: [flash].') ), array( array( 'poll_title' => 'xxx', 'poll_option_text' => "[b]x\ny[/b]", 'poll_max_options' => 2, 'poll_options_size' => 2 ), array( 'poll_title' => 'xxx', 'poll_option_text' => "[b]x\ny[/b]", 'poll_options' => array( '[b]x', 'y[/b]', ) ) ), ); } /** * @dataProvider get_test_cases */ public function test_options($original, $expected, array $args, $setup = null, $warn_msg = array()) { $this->prepare_s9e_services($setup); $message_parser = new parse_message($original); call_user_func_array(array($message_parser, 'parse'), $args); $this->assertSame($expected, $message_parser->message); $this->assertSame($warn_msg, $message_parser->warn_msg); } public function get_test_cases() { return array( array( '[b]bold[/b]', '[b]bold[/b]', array(true, true, true, true, true, true, true) ), array( '[b]bold[/b]', '[b]bold[/b]', array(false, true, true, true, true, true, true) ), array( 'http://example.org', 'http://example.org', array(true, true, true, true, true, true, true) ), array( 'http://example.org', 'http://example.org', array(true, false, true, true, true, true, true) ), array( ':)', ':)', array(true, true, true, true, true, true, true) ), array( ':)', ':)', array(true, true, false, true, true, true, true) ), array( '[url=http://example.org][img]http://example.org/img.png[/img][/url]', '[url=http://example.org][img]http://example.org/img.png[/img][/url]', array(true, true, true, true, true, true, true) ), array( '[url=http://example.org][img]http://example.org/img.png[/img][/url]', '[url=http://example.org][img]http://example.org/img.png[/img][/url]', array(true, true, true, false, true, true, true), null, array('You cannot use certain BBCodes: [img].') ), array( '[flash=12,34]http://example.org/foo.swf[/flash]', '[flash=12,34]http://example.org/foo.swf[/flash]', array(true, true, true, true, true, true, true) ), array( '[flash=12,34]http://example.org/foo.swf[/flash]', '[flash=12,34]http://example.org/foo.swf[/flash]', array(true, true, true, true, false, true, true), null, array('You cannot use certain BBCodes: [flash].') ), array( '[quote="foo"]bar :)[/quote]', '[quote="foo"]bar :)[/quote]', array(true, true, true, true, true, true, true) ), array( '[quote="foo"]bar :)[/quote]', '[quote="foo"]bar :)[/quote]', array(true, true, true, true, true, false, true), null, array('You cannot use certain BBCodes: [quote].') ), array( '[url=http://example.org][img]http://example.org/img.png[/img][/url]', '[url=http://example.org][img]http://example.org/img.png[/img][/url]', array(true, true, true, true, true, true, true) ), array( '[url=http://example.org][img]http://example.org/img.png[/img][/url]', '[url=http://example.org][img]http://example.org/img.png[/img][/url]', array(true, true, true, true, true, true, false), null, array('You cannot use certain BBCodes: [url].') ), array( '[size=200]200[/size]', '[size=200]200[/size]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_font_size', 200); } ), array( '[size=200]200[/size]', '[size=200]200[/size]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_font_size', 0); } ), array( '[size=2000]2000[/size]', '[size=2000]2000[/size]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_font_size', 200); }, array('You may only use fonts up to size 200.') ), array( '[size=0]0[/size]', '[size=0]0[/size]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_font_size', 200); } ), array( '[size=200]200[/size]', '[size=200]200[/size]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_sig_font_size', 200); } ), array( '[size=200]200[/size]', '[size=200]200[/size]', array(true, true, true, true, true, true, true, true, 'sig'), function ($phpbb_container) { $phpbb_container->get('config')->set('max_sig_font_size', 120); }, array('You may only use fonts up to size 120.') ), array( '[img]http://example.org/100x100.png[/img]', '[img]http://example.org/100x100.png[/img]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_height', 12); }, array('Your images may only be up to 12 pixels high.') ), array( '[img]http://example.org/100x100.png[/img]', '[img]http://example.org/100x100.png[/img]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_width', 34); }, array('Your images may only be up to 34 pixels wide.') ), array( '[img]http://example.org/100x100.png[/img]', '[img]http://example.org/100x100.png[/img]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_height', 0); $phpbb_container->get('config')->set('max_post_img_width', 0); } ), array( '[img]http://example.org/100x100.png[/img]', '[img]http://example.org/100x100.png[/img]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_height', 100); $phpbb_container->get('config')->set('max_post_img_width', 100); } ), array( '[img]http://example.org/100x100.png[/img]', '[img]http://example.org/100x100.png[/img]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_sig_img_height', 12); $phpbb_container->get('config')->set('max_sig_img_width', 34); } ), array( '[img]http://example.org/404.png[/img]', '[img]http://example.org/404.png[/img]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_height', 12); }, array('It was not possible to determine the dimensions of the image.') ), array( '[flash=999,999]http://example.org/foo.swf[/flash]', '[flash=999,999]http://example.org/foo.swf[/flash]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_height', 123); }, array('Your flash files may only be up to 123 pixels high.') ), array( '[flash=999,999]http://example.org/foo.swf[/flash]', '[flash=999,999]http://example.org/foo.swf[/flash]', array(true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_img_width', 456); }, array('Your flash files may only be up to 456 pixels wide.') ), array( ':) :) :)', ':) :) :)', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_smilies', 3); } ), array( ':) :) :) :)', ':) :) :) :)', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_smilies', 3); }, array('Your message contains too many smilies. The maximum number of smilies allowed is 3.') ), array( ':) :) :) :)', ':) :) :) :)', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_smilies', 0); } ), array( ':) :) :) :)', ':) :) :) :)', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_sig_smilies', 3); } ), array( ':) :) :) :)', ':) :) :) :)', array(true, true, true, true, true, true, true, true, 'sig'), function ($phpbb_container) { $phpbb_container->get('config')->set('max_sig_smilies', 3); }, array('Your message contains too many smilies. The maximum number of smilies allowed is 3.') ), array( 'http://example.org http://example.org http://example.org', 'http://example.org http://example.org http://example.org', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_urls', 2); }, array('Your message contains too many URLs. The maximum number of URLs allowed is 2.') ), array( 'http://example.org http://example.org http://example.org', 'http://example.org http://example.org http://example.org', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_post_urls', 0); } ), array( 'http://example.org http://example.org http://example.org', 'http://example.org http://example.org http://example.org', array(true, true, true, true, true, true, true, true), function ($phpbb_container) { $phpbb_container->get('config')->set('max_sig_urls', 2); } ), ); } } class phpbb_text_processing_message_parser_test_proxy { protected $response; public function stream_open($url) { if (strpos($url, '100x100')) { // Return a 100 x 100 PNG image $this->response = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQAAAABYmaj5AAAAE0lEQVR4AWOgKxgFo2AUjIJRAAAFeAABHs0ozQAAAABJRU5ErkJggg=='); } else { $this->response = '404 not found'; } return true; } public function stream_stat() { return false; } public function stream_read($len) { $chunk = substr($this->response, 0, $len); $this->response = substr($this->response, $len); return $chunk; } public function stream_eof() { return ($this->response === false); } }