Test Failed
Push — master ( dbca4e...c73643 )
by Jeroen De
04:56
created

SyntaxRendererTest::newRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace GitHub\Tests\Phpunit;
4
5
use GitHub\SyntaxRenderer;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @covers GitHub\SyntaxRenderer
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class SyntaxRendererTest extends TestCase {
15
16
	private $language = 'bash';
17
	private $enableLineNumbers = false;
18
	private $startingLineNumber = 1;
19
	private $highlightedLines = '';
20
	private $inlineSource = false;
21
22
	private function newRenderer(): SyntaxRenderer {
23
		 return new SyntaxRenderer(
24
			function( string $a ): string {
25
				return $a;
26
			},
27
			$this->language,
28
			$this->enableLineNumbers,
29
			$this->startingLineNumber,
30
			$this->highlightedLines,
31
			$this->inlineSource
32
		);
33
	}
34
35
	public function testBasicCase() {
36
		$this->assertSame(
37
			'<syntaxhighlight lang="bash" start="1">some nice bash script</syntaxhighlight>',
38
			$this->newRenderer()->getRenderedContent( 'some nice bash script' )
39
		);
40
	}
41
42
	public function testEnableLineNumbers() {
43
		$this->enableLineNumbers = true;
44
45
		$this->assertSame(
46
			'<syntaxhighlight lang="bash" start="1" line="1">some nice bash script</syntaxhighlight>',
47
			$this->newRenderer()->getRenderedContent( 'some nice bash script' )
48
		);
49
	}
50
51
	public function testInlineSource() {
52
		$this->inlineSource = true;
53
54
		$this->assertSame(
55
			'<syntaxhighlight lang="bash" start="1" inline="1">some nice bash script</syntaxhighlight>',
56
			$this->newRenderer()->getRenderedContent( 'some nice bash script' )
57
		);
58
	}
59
60
	public function testAlternativeStartingLineNumber() {
61
		$this->startingLineNumber = 42;
62
63
		$this->assertSame(
64
			'<syntaxhighlight lang="bash" start="42">some nice bash script</syntaxhighlight>',
65
			$this->newRenderer()->getRenderedContent( 'some nice bash script' )
66
		);
67
	}
68
69
	public function testHighlightedLines() {
70
		$this->highlightedLines = '1,2,1337';
71
72
		$this->assertSame(
73
			'<syntaxhighlight lang="bash" start="1" highlight="1,2,1337">some nice bash script</syntaxhighlight>',
74
			$this->newRenderer()->getRenderedContent( 'some nice bash script' )
75
		);
76
	}
77
78
	public function testHtmlContent() {
79
		$this->assertSame(
80
			'<syntaxhighlight lang="bash" start="1"><h1><a href="http://test">hi</a></h1></syntaxhighlight>',
81
			$this->newRenderer()->getRenderedContent( '<h1><a href="http://test">hi</a></h1>' )
82
		);
83
	}
84
85
	public function testEvilAttributes() {
86
		$this->language = '"><script>alert("hi");</script>';
87
88
		$this->assertSame(
89
			'<syntaxhighlight lang="&quot;&gt;&lt;script&gt;alert(&quot;hi&quot;);&lt;/script&gt;" start="1">some script</syntaxhighlight>',
90
			$this->newRenderer()->getRenderedContent( 'some script' )
91
		);
92
	}
93
94
	public function testEvilContent() {
95
		// It is fine that the content can break out of the tag. The effect will be the same as placing
96
		// it on the wiki directly as the syntaxhighlight tag still goes though the MediaWiki parser.
97
		$this->assertSame(
98
			'<syntaxhighlight lang="bash" start="1"></syntaxhighlight><script>alert("hi");</script><syntaxhighlight></syntaxhighlight>',
99
			$this->newRenderer()->getRenderedContent( '</syntaxhighlight><script>alert("hi");</script><syntaxhighlight>' )
100
		);
101
	}
102
103
}
104