-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathEscaperTest.php
147 lines (113 loc) · 4.44 KB
/
EscaperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace League\Plates\Tests\Extension;
use League\Plates\Engine;
use League\Plates\Extension\Escaper;
use League\Plates\Template\Template;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use const ENT_HTML401;
use const ENT_HTML5;
use const ENT_XHTML;
use const ENT_XML1;
class EscaperTest extends TestCase
{
protected function setUp(): void
{
}
public function testCanCreateInstance()
{
$this->assertInstanceOf(Escaper::class, new Escaper());
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_HTML5));
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_XHTML));
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_XML1));
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_HTML401, 'ISO-8859-15'));
}
public function testThatEscaperIsAutoRegisteredByDefault()
{
$engine = new Engine();
$this->assertTrue($engine->doesFunctionExist('escape'));
$this->assertTrue($engine->doesFunctionExist('e'));
}
public function testThatEscaperAutoRegistrationCanBeBypassed()
{
$engine = new Engine(null, 'php', false);
$this->assertFalse($engine->doesFunctionExist('escape'));
$this->assertFalse($engine->doesFunctionExist('e'));
}
public function testDefaultHtml401EscapeFunction()
{
$string = '<&>"\'';
$expected = '<&>"'';
$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string));
}
public function testHtml5EscapeFunction()
{
$string = '<&>"\'';
$expected = '<&>"'';
$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string, null, ENT_HTML5));
$escaper = new Escaper(ENT_HTML5);
$this->assertSame($expected, $escaper->escape($string));
}
public function testXhtmlEscapeFunction()
{
$string = '<&>"\'';
$expected = '<&>"'';
$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string, null, ENT_XHTML));
$escaper = new Escaper(ENT_XHTML);
$this->assertSame($expected, $escaper->escape($string));
}
public function testXml1EscapeFunction()
{
$string = '<&>"\'';
$expected = '<&>"'';
$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string, null, ENT_XML1));
$escaper = new Escaper(ENT_XML1);
$this->assertSame($expected, $escaper->escape($string));
}
public function testThatDoubleEncodeIsEnabledByDefault()
{
$string = '<&>"'';
$expected = '&lt;&amp;&gt;&quot;&apos;';
$escaper = new Escaper(ENT_HTML5);
$this->assertSame($expected, $escaper->escape($string));
}
public function testThatDoubleEncodeCanBeDisabled()
{
$escaper = new Escaper();
$string = '<&>"'';
$expected = '<&>"&apos;';
$this->assertSame($expected, $escaper->escape($string, null, null, null, false));
$expected = '<&>"'';
$this->assertSame($expected, $escaper->escape($string, null, ENT_HTML5, null, false));
$this->assertSame($expected, $escaper->escape($string, null, ENT_XHTML, null, false));
$this->assertSame($expected, $escaper->escape($string, null, ENT_XML1, null, false));
}
public function testThatBatchFunctionsAreCalledFromWithinTemplate()
{
vfsStream::setup('templates');
$engine = new Engine(vfsStream::url('templates'));
$engine->registerFunction('tr', 'trim');
$engine->registerFunction('uc', 'strtoupper');
$template = new Template($engine, 'template');
vfsStream::create(
array(
'template.php' => '<?php echo $this->batch(" abc ", "tr|uc") ?>',
)
);
$this->assertSame('ABC', $template->render());
}
public function testThatBatchFunctionsAreSkippedIfOutsideTemplate()
{
$escaper = new Escaper(ENT_HTML5);
$engine = new Engine(null, 'php', false);
$engine->loadExtension($escaper);
$engine->registerFunction('tr', 'trim');
$engine->registerFunction('uc', 'strtoupper');
$this->assertSame('abc', $escaper->escape('abc', 'tr|uc'));
}
}