PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/text_processing/message_parser_test.php

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