This repository was archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathEscapeJsTest.php
202 lines (184 loc) · 6.39 KB
/
EscapeJsTest.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\View\Helper;
use PHPUnit\Framework\TestCase;
use stdClass;
use Zend\Escaper\Escaper;
use Zend\View\Helper\EscapeJs as EscapeHelper;
class EscapeJsTest extends TestCase
{
protected $supportedEncodings = [
'iso-8859-1', 'iso8859-1', 'iso-8859-5', 'iso8859-5',
'iso-8859-15', 'iso8859-15', 'utf-8', 'cp866',
'ibm866', '866', 'cp1251', 'windows-1251',
'win-1251', '1251', 'cp1252', 'windows-1252',
'1252', 'koi8-r', 'koi8-ru', 'koi8r',
'big5', '950', 'gb2312', '936',
'big5-hkscs', 'shift_jis', 'sjis', 'sjis-win',
'cp932', '932', 'euc-jp', 'eucjp',
'eucjp-win', 'macroman'
];
protected function setUp()
{
$this->helper = new EscapeHelper;
}
public function testUsesUtf8EncodingByDefault()
{
$this->assertEquals('UTF-8', $this->helper->getEncoding());
}
/**
* @expectedException \Zend\View\Exception\InvalidArgumentException
*/
public function testEncodingIsImmutable()
{
$this->helper->setEncoding('BIG5-HKSCS');
$this->helper->getEscaper();
$this->helper->setEncoding('UTF-8');
}
public function testGetEscaperCreatesDefaultInstanceWithCorrectEncoding()
{
$this->helper->setEncoding('BIG5-HKSCS');
$escaper = $this->helper->getEscaper();
$this->assertInstanceOf(Escaper::class, $escaper);
$this->assertEquals('big5-hkscs', $escaper->getEncoding());
}
public function testSettingEscaperObjectAlsoSetsEncoding()
{
$escaper = new Escaper('big5-hkscs');
$this->helper->setEscaper($escaper);
$escaper = $this->helper->getEscaper();
$this->assertInstanceOf(Escaper::class, $escaper);
$this->assertEquals('big5-hkscs', $escaper->getEncoding());
}
public function testEscapehtmlCalledOnEscaperObject()
{
$escaper = $this->getMockBuilder(Escaper::class)->getMock();
$escaper->expects($this->any())->method('escapeJs');
$this->helper->setEscaper($escaper);
$this->helper->__invoke('foo');
}
public function testAllowsRecursiveEscapingOfArrays()
{
$original = [
'foo' => '<b>bar</b>',
'baz' => [
'<em>bat</em>',
'second' => [
'<i>third</i>',
],
],
];
$expected = [
'foo' => '\x3Cb\x3Ebar\x3C\x2Fb\x3E',
'baz' => [
'\x3Cem\x3Ebat\x3C\x2Fem\x3E',
'second' => [
'\x3Ci\x3Ethird\x3C\x2Fi\x3E',
],
],
];
$test = $this->helper->__invoke($original, EscapeHelper::RECURSE_ARRAY);
$this->assertEquals($expected, $test);
}
public function testWillCastObjectsToStringsBeforeEscaping()
{
$object = new TestAsset\Stringified;
$test = $this->helper->__invoke($object);
$this->assertEquals(
'ZendTest\x5CView\x5CHelper\x5CTestAsset\x5CStringified',
$test
);
}
public function testCanRecurseObjectImplementingToArray()
{
$original = [
'foo' => '<b>bar</b>',
'baz' => [
'<em>bat</em>',
'second' => [
'<i>third</i>',
],
],
];
$object = new TestAsset\ToArray();
$object->array = $original;
$expected = [
'foo' => '\x3Cb\x3Ebar\x3C\x2Fb\x3E',
'baz' => [
'\x3Cem\x3Ebat\x3C\x2Fem\x3E',
'second' => [
'\x3Ci\x3Ethird\x3C\x2Fi\x3E',
],
],
];
$test = $this->helper->__invoke($object, EscapeHelper::RECURSE_OBJECT);
$this->assertEquals($expected, $test);
}
public function testCanRecurseObjectProperties()
{
$original = [
'foo' => '<b>bar</b>',
'baz' => [
'<em>bat</em>',
'second' => [
'<i>third</i>',
],
],
];
$object = new stdClass();
foreach ($original as $key => $value) {
$object->$key = $value;
}
$expected = [
'foo' => '\x3Cb\x3Ebar\x3C\x2Fb\x3E',
'baz' => [
'\x3Cem\x3Ebat\x3C\x2Fem\x3E',
'second' => [
'\x3Ci\x3Ethird\x3C\x2Fi\x3E',
],
],
];
$test = $this->helper->__invoke($object, EscapeHelper::RECURSE_OBJECT);
$this->assertEquals($expected, $test);
}
/**
* @expectedException \Zend\Escaper\Exception\InvalidArgumentException
*
* PHP 5.3 instates default encoding on empty string instead of the expected
* warning level error for htmlspecialchars() encoding param. PHP 5.4 attempts
* to guess the encoding or take it from php.ini default_charset when an empty
* string is set. Both are insecure behaviours.
*/
public function testSettingEncodingToEmptyStringShouldThrowException()
{
$this->helper->setEncoding('');
$this->helper->getEscaper();
}
public function testSettingValidEncodingShouldNotThrowExceptions()
{
foreach ($this->supportedEncodings as $value) {
$helper = new EscapeHelper;
$helper->setEncoding($value);
$helper->getEscaper();
}
}
/**
* @expectedException \Zend\Escaper\Exception\InvalidArgumentException
*
* All versions of PHP - when an invalid encoding is set on htmlspecialchars()
* a warning level error is issued and escaping continues with the default encoding
* for that PHP version. Preventing the continuation behaviour offsets display_errors
* off in production env.
*/
public function testSettingEncodingToInvalidValueShouldThrowException()
{
$this->helper->setEncoding('completely-invalid');
$this->helper->getEscaper();
}
}