|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace devtoolboxuk\soteria; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
class SanitiseTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
protected $testString = 'Test String'; |
|
10
|
|
|
protected $testEmail = '[email protected]'; |
|
11
|
|
|
protected $testUrl = 'https://www.google.com'; |
|
12
|
|
|
protected $latinAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
|
13
|
|
|
protected $numbers = '0123456789'; |
|
14
|
|
|
protected $specialCharactersA = '!"£$%^&*()_+{}:@~<>?¬|'; |
|
15
|
|
|
protected $specialCharactersB = "\`-=[];'#,./`"; |
|
16
|
|
|
protected $germanSpecialCharacters = "ÄÖÜẞäöüß"; |
|
17
|
|
|
protected $frenchSpecialCharacters = "âàäçéèêëîïôùûüœ"; |
|
18
|
|
|
protected $dutchSpecialCharacters = "áéíóúàèëïöüijÁÉÍÓÚÀÈËÏÖÜIJ"; |
|
19
|
|
|
protected $spanishSpecialCharacters = "áéíóúñü¿¡"; |
|
20
|
|
|
protected $scandinavianSpecialCharactersA = "æÆäÄøØöÖåÅ"; |
|
21
|
|
|
protected $scandinavianSpecialCharactersB = "ÅåÄäÖöŠšŽž"; |
|
22
|
|
|
protected $irishSpecialCharacters = "áíéóú"; |
|
23
|
|
|
protected $polishSpecialCharactersA = "AĄBCĆDEĘFGHIJKLŁMNŃOÓPRSŚTUWYZŹŻ"; |
|
24
|
|
|
protected $polishSpecialCharactersB = "aąbcćdeęfghijklłmnńoóprsśtuwyzźż"; |
|
25
|
|
|
protected $cyrillicCharactersA = "Аа Бб Вв Гг Дд Ее Жж Зз Ии Йй Кк Лл Мм Нн"; |
|
26
|
|
|
protected $cyrillicCharactersB = "Оо Пп Рр Сс Тт Уу Фф Хх Цц Чч Шш Щщ Ьь Юю Яя"; |
|
27
|
|
|
protected $arabic = "غ ظ ض ذ خ ث ت ش ر ق ص ف ع س ن م ل ك ي ط ح ز و ه د ج ب ا"; |
|
28
|
|
|
protected $chineseTraditionalA = "電 買 車 紅 無 東 馬 風 時 鳥 語 頭 魚 園 長 島 愛 紙 書 見 假 佛 德 拜 黑 冰 兔 妒 每 壤 步"; |
|
29
|
|
|
protected $chineseTraditionalB = "巢 惠 鞋 莓 圓 聽 實 證 龍 賣 龜 藝 戰 繩 關 鐵 圖 團 轉 廣 惡 豐 腦 雜 壓 雞 價 樂 氣 廳 發"; |
|
30
|
|
|
protected $chineseTraditionalC = "勞 劍 歲 權 燒 贊 兩 譯 觀 營 處 齒 驛 櫻 產 藥 讀 顏 聲 學 體 點 麥 蟲 舊 會 萬 盜 寶 國 醫"; |
|
31
|
|
|
protected $chineseTraditionalD = "雙 晝 觸 來 畫 黃 區"; |
|
32
|
|
|
protected $chineseSimplifiedA = "电 买 车 红 无 东 马 风 时 鸟 语 头 鱼 园 长 岛 爱 纸 书 见 假 佛 德 拜 黑 冰 兔 妒 每 壤"; |
|
33
|
|
|
protected $chineseSimplifiedB = "步 巢 惠 鞋 莓 圆 听 实 证 龙 卖 龟 艺 战 绳 关 铁 图 团 转 广 恶 丰 脑 杂 压 鸡 价 乐 气"; |
|
34
|
|
|
protected $chineseSimplifiedC = "厅 发 劳 剑 岁 权 烧 赞 两 译 观 营 处 齿 驿 樱 产 药 读 颜 声 学 体 点 麦 虫 旧 会 万 盗"; |
|
35
|
|
|
protected $chineseSimplifiedD = "宝 国 医 双 昼 触 来 画 黄 区"; |
|
36
|
|
|
protected $testArray = []; |
|
37
|
|
|
private $security; |
|
38
|
|
|
private $sanitise; |
|
39
|
|
|
|
|
40
|
|
|
function __construct($name = null, array $data = [], $dataName = '') |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct($name, $data, $dataName); |
|
43
|
|
|
$this->security = new SoteriaService(); |
|
44
|
|
|
|
|
45
|
|
|
$this->sanitise = $this->security->sanitise(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
function testEmail() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$this->sanitise->disinfect('[email protected]', 'email'); |
|
51
|
|
|
|
|
52
|
|
|
$result = $this->sanitise->result(); |
|
53
|
|
|
if ($result->isValid()) { |
|
54
|
|
|
echo "\nValid"; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// |
|
59
|
|
|
// |
|
60
|
|
|
// function testArrayEmailFilter() |
|
61
|
|
|
// { |
|
62
|
|
|
// $equalsArray = [ |
|
63
|
|
|
// ['!$%^&*_+{}@~?|', $this->specialCharactersA], |
|
64
|
|
|
// ["`-=[]'#.`", $this->specialCharactersB], |
|
65
|
|
|
// ["O'Neil", "O\'Neil"], |
|
66
|
|
|
// ["O'Neil", "O\\\'Neil"], |
|
67
|
|
|
// ["coDepartment", "c/o Department"] |
|
68
|
|
|
// ]; |
|
69
|
|
|
// |
|
70
|
|
|
// foreach ($equalsArray as $arr) { |
|
71
|
|
|
// $disinfect = s::filter()->string($arr[1]) |
|
72
|
|
|
// ->filterEmail(); |
|
73
|
|
|
// $this->assertEquals($arr[0], $disinfect->cleanse()); |
|
74
|
|
|
// } |
|
75
|
|
|
// } |
|
76
|
|
|
// |
|
77
|
|
|
// function testArrayStringFilter() |
|
78
|
|
|
// { |
|
79
|
|
|
// $equalsArray = [ |
|
80
|
|
|
// //Character Sets |
|
81
|
|
|
// [$this->numbers, $this->numbers], |
|
82
|
|
|
// [$this->latinAlphabet, $this->latinAlphabet], |
|
83
|
|
|
// [$this->latinAlphabet . $this->numbers, $this->latinAlphabet . $this->numbers], |
|
84
|
|
|
// [$this->germanSpecialCharacters, $this->germanSpecialCharacters], |
|
85
|
|
|
// [$this->frenchSpecialCharacters, $this->frenchSpecialCharacters], |
|
86
|
|
|
// [$this->dutchSpecialCharacters, $this->dutchSpecialCharacters], |
|
87
|
|
|
// [$this->spanishSpecialCharacters, $this->spanishSpecialCharacters], |
|
88
|
|
|
// [$this->scandinavianSpecialCharactersA, $this->scandinavianSpecialCharactersA], |
|
89
|
|
|
// [$this->scandinavianSpecialCharactersB, $this->scandinavianSpecialCharactersB], |
|
90
|
|
|
// [$this->irishSpecialCharacters, $this->irishSpecialCharacters], |
|
91
|
|
|
// [$this->cyrillicCharactersA, $this->cyrillicCharactersA], |
|
92
|
|
|
// [$this->cyrillicCharactersB, $this->cyrillicCharactersB], |
|
93
|
|
|
// [$this->arabic, $this->arabic], |
|
94
|
|
|
// [$this->chineseTraditionalA, $this->chineseTraditionalA], |
|
95
|
|
|
// [$this->chineseTraditionalB, $this->chineseTraditionalB], |
|
96
|
|
|
// [$this->chineseTraditionalC, $this->chineseTraditionalC], |
|
97
|
|
|
// [$this->chineseTraditionalD, $this->chineseTraditionalD], |
|
98
|
|
|
// [$this->chineseSimplifiedA, $this->chineseSimplifiedA], |
|
99
|
|
|
// [$this->chineseSimplifiedB, $this->chineseSimplifiedB], |
|
100
|
|
|
// [$this->chineseSimplifiedC, $this->chineseSimplifiedC], |
|
101
|
|
|
// [$this->chineseSimplifiedD, $this->chineseSimplifiedD], |
|
102
|
|
|
// //Known Cases |
|
103
|
|
|
// ['!"£$%^&*()_+{}:@~?¬|', $this->specialCharactersA], |
|
104
|
|
|
// ["`-=[];'#,./`", $this->specialCharactersB], |
|
105
|
|
|
// ["O'Neil", "O\'Neil"], |
|
106
|
|
|
// ["O'Neil", "O\\\'Neil"], |
|
107
|
|
|
// ["c/o Department", "c/o Department"], |
|
108
|
|
|
// //HTML |
|
109
|
|
|
// ["testing", '<a href="http://www.google.co.uk">testing</a>'] |
|
110
|
|
|
// ]; |
|
111
|
|
|
// |
|
112
|
|
|
// foreach ($equalsArray as $arr) { |
|
113
|
|
|
// $disinfect = s::filter()->string($arr[1]) |
|
114
|
|
|
// ->filterString(); |
|
115
|
|
|
// $this->assertEquals($arr[0], $disinfect->cleanse()); |
|
116
|
|
|
// } |
|
117
|
|
|
// } |
|
118
|
|
|
// |
|
119
|
|
|
// function testArraySpecialFilter() |
|
120
|
|
|
// { |
|
121
|
|
|
// $equalsArray = [ |
|
122
|
|
|
// //Character Sets |
|
123
|
|
|
// [$this->numbers, $this->numbers], |
|
124
|
|
|
// [$this->latinAlphabet, $this->latinAlphabet], |
|
125
|
|
|
// [$this->latinAlphabet . $this->numbers, $this->latinAlphabet . $this->numbers], |
|
126
|
|
|
// |
|
127
|
|
|
// //Known Cases |
|
128
|
|
|
// ['!"£$%^&*()_+{}:@~?¬|', $this->specialCharactersA], |
|
129
|
|
|
// ["`-=[];'#,./`", $this->specialCharactersB], |
|
130
|
|
|
// ["O'Neil", "O\'Neil"], |
|
131
|
|
|
// ["O'Neil", "O\\\'Neil"], |
|
132
|
|
|
// ["c/o Department", "c/o Department"], |
|
133
|
|
|
// //HTML |
|
134
|
|
|
// ["testing", '<a href="http://www.google.co.uk">testing</a>'] |
|
135
|
|
|
// ]; |
|
136
|
|
|
// |
|
137
|
|
|
// foreach ($equalsArray as $arr) { |
|
138
|
|
|
// $disinfect = s::filter()->string($arr[1]) |
|
139
|
|
|
// ->filterSpecial(); |
|
140
|
|
|
// $this->assertEquals($arr[0], $disinfect->cleanse()); |
|
141
|
|
|
// } |
|
142
|
|
|
// } |
|
143
|
|
|
// |
|
144
|
|
|
// function testArrayUrlFilter() |
|
145
|
|
|
// { |
|
146
|
|
|
// $equalsArray = [ |
|
147
|
|
|
// //Character Sets |
|
148
|
|
|
// ["c/oDepartment", "c/o Department"], |
|
149
|
|
|
// //HTML |
|
150
|
|
|
// ["testing", '<a href="http://www.google.co.uk">testing</a>'] |
|
151
|
|
|
// ]; |
|
152
|
|
|
// |
|
153
|
|
|
// foreach ($equalsArray as $arr) { |
|
154
|
|
|
// $disinfect = s::filter()->string($arr[1]) |
|
155
|
|
|
// ->filterUrl(); |
|
156
|
|
|
// $this->assertEquals($arr[0], $disinfect->cleanse()); |
|
157
|
|
|
// } |
|
158
|
|
|
// } |
|
159
|
|
|
// |
|
160
|
|
|
// |
|
161
|
|
|
// function testFilterInt() |
|
162
|
|
|
// { |
|
163
|
|
|
// $disinfect = s::filter() |
|
164
|
|
|
// ->string("[email protected]") |
|
165
|
|
|
// ->filterInt(); |
|
166
|
|
|
// |
|
167
|
|
|
// $this->assertEquals("", $disinfect->cleanse()); |
|
168
|
|
|
// |
|
169
|
|
|
// $disinfect = s::filter() |
|
170
|
|
|
// ->string("2.2") |
|
171
|
|
|
// ->filterInt(); |
|
172
|
|
|
// |
|
173
|
|
|
// $this->assertEquals("22", $disinfect->cleanse()); |
|
174
|
|
|
// } |
|
175
|
|
|
// |
|
176
|
|
|
// function testFilterUrl() |
|
177
|
|
|
// { |
|
178
|
|
|
// $disinfect = s::filter() |
|
179
|
|
|
// ->string($this->testUrl) |
|
180
|
|
|
// ->filterUrl(); |
|
181
|
|
|
// |
|
182
|
|
|
// $this->assertEquals($this->testUrl, $disinfect->cleanse()); |
|
183
|
|
|
// |
|
184
|
|
|
// } |
|
185
|
|
|
// |
|
186
|
|
|
// function testFilterSpecial() |
|
187
|
|
|
// { |
|
188
|
|
|
// $disinfect = s::filter() |
|
189
|
|
|
// ->string($this->testUrl."?alert('Data')") |
|
190
|
|
|
// ->filterSpecial(); |
|
191
|
|
|
// |
|
192
|
|
|
// $this->assertEquals($this->testUrl."?alert('Data')", $disinfect->cleanse()); |
|
193
|
|
|
// } |
|
194
|
|
|
// |
|
195
|
|
|
// function testFilterFloat() |
|
196
|
|
|
// { |
|
197
|
|
|
// $disinfect = s::filter() |
|
198
|
|
|
// ->string("[email protected]") |
|
199
|
|
|
// ->filterFloat(); |
|
200
|
|
|
// |
|
201
|
|
|
// $this->assertEquals("", $disinfect->cleanse()); |
|
202
|
|
|
// |
|
203
|
|
|
// $disinfect = s::filter() |
|
204
|
|
|
// ->string("2.2") |
|
205
|
|
|
// ->filterFloat(); |
|
206
|
|
|
// |
|
207
|
|
|
// $this->assertEquals("22", $disinfect->cleanse()); |
|
208
|
|
|
// |
|
209
|
|
|
// $disinfect = s::filter() |
|
210
|
|
|
// ->string("2.2") |
|
211
|
|
|
// ->filterFloatFraction(); |
|
212
|
|
|
// |
|
213
|
|
|
// $this->assertEquals("2.2", $disinfect->cleanse()); |
|
214
|
|
|
// } |
|
215
|
|
|
// |
|
216
|
|
|
// |
|
217
|
|
|
// function testEmailFilter() |
|
218
|
|
|
// { |
|
219
|
|
|
// $disinfect = s::filter()->string("[email protected]") |
|
220
|
|
|
// ->filterEmail(); |
|
221
|
|
|
// |
|
222
|
|
|
// $this->assertEquals($this->testEmail, $disinfect->cleanse()); |
|
223
|
|
|
// |
|
224
|
|
|
// |
|
225
|
|
|
// $disinfect = s::filter()->string($this->testEmail) |
|
226
|
|
|
// ->filterEmail(); |
|
227
|
|
|
// |
|
228
|
|
|
// $this->assertNotEquals($this->testString, $disinfect->cleanse()); |
|
229
|
|
|
// |
|
230
|
|
|
// $disinfect = s::filter()->string($this->testString) |
|
231
|
|
|
// ->filterEmail(); |
|
232
|
|
|
// |
|
233
|
|
|
// $this->assertNotEquals($this->testString, $disinfect->cleanse()); |
|
234
|
|
|
// } |
|
235
|
|
|
// |
|
236
|
|
|
// public function testBasicCleanse() |
|
237
|
|
|
// { |
|
238
|
|
|
// $equalsArray = [ |
|
239
|
|
|
// //Character Sets |
|
240
|
|
|
// [$this->numbers, $this->numbers], |
|
241
|
|
|
// [$this->latinAlphabet, $this->latinAlphabet], |
|
242
|
|
|
// [$this->latinAlphabet . $this->numbers, $this->latinAlphabet . $this->numbers], |
|
243
|
|
|
// [$this->germanSpecialCharacters, $this->germanSpecialCharacters], |
|
244
|
|
|
// [$this->frenchSpecialCharacters, $this->frenchSpecialCharacters], |
|
245
|
|
|
// [$this->dutchSpecialCharacters, $this->dutchSpecialCharacters], |
|
246
|
|
|
// [$this->spanishSpecialCharacters, $this->spanishSpecialCharacters], |
|
247
|
|
|
// [$this->scandinavianSpecialCharactersA, $this->scandinavianSpecialCharactersA], |
|
248
|
|
|
// [$this->scandinavianSpecialCharactersB, $this->scandinavianSpecialCharactersB], |
|
249
|
|
|
// [$this->irishSpecialCharacters, $this->irishSpecialCharacters], |
|
250
|
|
|
// [$this->cyrillicCharactersA, $this->cyrillicCharactersA], |
|
251
|
|
|
// [$this->cyrillicCharactersB, $this->cyrillicCharactersB], |
|
252
|
|
|
// [$this->arabic, $this->arabic], |
|
253
|
|
|
// [$this->chineseTraditionalA, $this->chineseTraditionalA], |
|
254
|
|
|
// [$this->chineseTraditionalB, $this->chineseTraditionalB], |
|
255
|
|
|
// [$this->chineseTraditionalC, $this->chineseTraditionalC], |
|
256
|
|
|
// [$this->chineseTraditionalD, $this->chineseTraditionalD], |
|
257
|
|
|
// [$this->chineseSimplifiedA, $this->chineseSimplifiedA], |
|
258
|
|
|
// [$this->chineseSimplifiedB, $this->chineseSimplifiedB], |
|
259
|
|
|
// [$this->chineseSimplifiedC, $this->chineseSimplifiedC], |
|
260
|
|
|
// [$this->chineseSimplifiedD, $this->chineseSimplifiedD], |
|
261
|
|
|
// //Known Cases |
|
262
|
|
|
// ['!"£$%^&*()_+{}:@~?¬|', $this->specialCharactersA], |
|
263
|
|
|
// ["`-=[];'#,./`", $this->specialCharactersB], |
|
264
|
|
|
// ["O'Neil", "O\'Neil"], |
|
265
|
|
|
// ["O'Neil", "O\\\'Neil"], |
|
266
|
|
|
// ["c/o Department", "c/o Department"], |
|
267
|
|
|
// //HTML |
|
268
|
|
|
// ["testing", '<a href="http://www.google.co.uk">testing</a>'] |
|
269
|
|
|
// ]; |
|
270
|
|
|
// |
|
271
|
|
|
// foreach ($equalsArray as $arr) { |
|
272
|
|
|
// $disinfect = s::filter()->string($arr[1]); |
|
273
|
|
|
// $this->assertEquals($arr[0], $disinfect->cleanse()); |
|
274
|
|
|
// } |
|
275
|
|
|
// } |
|
276
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.