Completed
Push — master ( 8f4937...fedf49 )
by smiley
03:03
created

HTML5ModuleTestCritical::nestingDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * @filesource   HTML5ModuleTestCritical.php
5
 * @created      10.02.2016
6
 * @package      chillerlan\BBCodeTest
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2015 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\BBCodeTest\critical\Modules;
13
14
use chillerlan\bbcode\Modules\Html5\Code;
15
use chillerlan\bbcode\Modules\Html5\Containers;
16
use chillerlan\bbcode\Modules\Html5\Html5BaseModule;
17
use chillerlan\bbcode\Modules\Html5\Singletags;
18
use chillerlan\bbcode\Parser;
19
use chillerlan\bbcode\ParserOptions;
20
21
/**
22
 * Class HTML5ModuleTestCritical
23
 *
24
 * may cause the one or other PREG_ERROR or run into a php bug depending on OS and PHP version...
25
 *
26
 * @link https://github.com/travis-ci/travis-ci/issues/4593
27
 * @link https://github.com/travis-ci/travis-ci/issues/5039
28
 * @link https://github.com/travis-ci/travis-ci/issues/5323
29
 * @link https://github.com/travis-ci/travis-ci/issues/5332
30
 */
31
class HTML5ModuleTestCritical extends \PHPUnit_Framework_TestCase{
32
33
	/**
34
	 * @var \chillerlan\bbcode\Parser
35
	 */
36
	protected $parser;
37
38
	protected function setUp(){
39
		$options = new ParserOptions;
40
		$options->allow_all = true;
41
		$this->parser = new Parser($options);
42
	}
43
44
	public function bbcodeDataProvider(){
45
		return [
46
			// basics
47
			['', ''], // empty string test (coverage)
48
			['no bbcode at all', 'no bbcode at all'],
49
			['[somebbcode]invalid bbcodes will be eaten :P[/somebbcode]', 'invalid bbcodes will be eaten :P'],
50
			// XSS
51
			['<script>alert(\'Hello, i am an XSS attempt!\')</script>', '&lt;script&gt;alert(\'Hello, i am an XSS attempt!\')&lt;/script&gt;'],
52
			['<img src="javascript:alert(\'XSS\');" />', '&lt;img src="javascript:alert(\'XSS\');" /&gt;'],
53
			['[img]JaVaScRiPt:alert(\'XSS\');[/img]', ''],
54
			['[img]""><SCRIPT>alert("XSS")</SCRIPT>"[/img]', ''],
55
			['[img]&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;[/img]', ''],
56
			['[img]¼script¾alert(¢XSS¢)¼/script¾[/img]', ''],
57
			['[img]vbscript:msgbox("XSS")[/img]', ''],
58
			['[img]&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041[/img]', ''],
59
			['[img alt=Privateinvestocat]https://octodex.github.com/images/privateinvestocat.jpg[/img]', '<img src="https://octodex.github.com/images/privateinvestocat.jpg" alt="Privateinvestocat" class="bb-image" />'],
60
			// noparse
61
			['[noparse][u][b]some unparsed bbcode[/b][/u][/noparse]', '<pre class="bbcode noparse">[u][b]some unparsed bbcode[/b][/u]</pre>'],
62
		];
63
	}
64
65
	/**
66
	 * @dataProvider bbcodeDataProvider
67
	 */
68
	public function testParser($bbcode, $expected){
69
		$this->assertEquals($expected, $this->parser->parse($bbcode));
70
	}
71
72
	public function nestingDataProvider(){
73
		return [
74
			[0,   'bbcode_nesting.txt'],
75
			[1,   'results/html5_nesting_1.txt'],
76
			[10,  'results/html5_nesting_10.txt'],
77
			[100, 'results/html5_nesting_100.txt'],
78
		];
79
	}
80
81
	/**
82
	 * @dataProvider nestingDataProvider
83
	 */
84
	public function testNesting($limit, $resultfile){
85
		$options = new ParserOptions;
86
		$options->allow_all = true;
87
		$options->nesting_limit = $limit;
88
		$this->parser->setOptions($options);
89
90
		$bbcode = file_get_contents(dirname(__FILE__).'/../../bbcode_samples/bbcode_nesting.txt');
91
		$expected = file_get_contents(dirname(__FILE__).'/../../bbcode_samples/'.$resultfile);
92
93
		$parsed = $this->parser->parse($bbcode);
94
		// replace the random IDs with something more testable
95
		$parsed = preg_replace('/\"([a-f\d]{8})\"/i', '"abcdef12"', $parsed);
96
97
		$this->assertEquals($expected, $parsed);
98
	}
99
100
	public function testCodeModule(){
101
		foreach(array_keys($this->parser->getTagmap(), Code::class) as $lang){
102
			$bbcode = file_get_contents(dirname(__FILE__).'/../../bbcode_samples/bbcode_code_'.$lang.'.txt');
103
			$expected = file_get_contents(dirname(__FILE__).'/../../bbcode_samples/results/html5_code_'.$lang.'.txt');
104
			$parsed = $this->parser->parse($bbcode);
105
			$parsed = preg_replace('/\"([a-f\d]{8})\"/i', '"abcdef12"', $parsed);
106
107
			$this->assertEquals($expected, $parsed);
108
		}
109
	}
110
111
}
112