SanitizeTest.php 18.8 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/2/16
 * Time: 11:10 PM
 */

namespace qfq;

11
use PHPUnit\Framework\TestCase;
Carsten  Rose's avatar
Carsten Rose committed
12

13
14
15
16
17
/**
 * Class SanitizeTest
 * @package qfq
 */
class SanitizeTest extends TestCase {
Carsten  Rose's avatar
Carsten Rose committed
18

19
20
21
22
    /**
     * @throws CodeException
     * @throws UserFormException
     */
Carsten  Rose's avatar
Carsten Rose committed
23
24
25
26
27
    public function testSanitize() {

        # Violates SANITIZE class: SANITIZE string is always an empty string.
        # Access are cached: use new variables for every test.

28
29
30
        # Check ''
        $this->assertEquals('', Sanitize::sanitize('', SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
        $this->assertEquals('', Sanitize::sanitize('', SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
31
        $this->assertEquals('!!email!!', Sanitize::sanitize('', SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
32
33
        $this->assertEquals('', Sanitize::sanitize('', SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
        $this->assertEquals('', Sanitize::sanitize('', SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
34
        $this->assertEquals('', Sanitize::sanitize('', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
35

Carsten  Rose's avatar
Carsten Rose committed
36
37
        # Check '1'
        $this->assertEquals('1', Sanitize::sanitize('1', SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
38
        $this->assertEquals('1', Sanitize::sanitize('1', SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
39
        $this->assertEquals('!!email!!', Sanitize::sanitize('1', SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
40
        $this->assertEquals('1', Sanitize::sanitize('1', SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
Carsten  Rose's avatar
Carsten Rose committed
41
        $this->assertEquals('1', Sanitize::sanitize('1', SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
42
        $this->assertEquals('1', Sanitize::sanitize('1', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
Carsten  Rose's avatar
Carsten Rose committed
43
44
45

        # Check '-3'
        $this->assertEquals('-3', Sanitize::sanitize('-3', SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
46
47
        $this->assertEquals('!!digit!!', Sanitize::sanitize('-3', SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
        $this->assertEquals('!!email!!', Sanitize::sanitize('-3', SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
48
        $this->assertEquals('-3', Sanitize::sanitize('-3', SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
Carsten  Rose's avatar
Carsten Rose committed
49
        $this->assertEquals('-3', Sanitize::sanitize('-3', SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
50
        $this->assertEquals('-3', Sanitize::sanitize('-3', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
Carsten  Rose's avatar
Carsten Rose committed
51
52
53

        # Check 'a'
        $this->assertEquals('a', Sanitize::sanitize('a', SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
54
55
        $this->assertEquals('!!digit!!', Sanitize::sanitize('a', SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
        $this->assertEquals('!!email!!', Sanitize::sanitize('a', SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
56
        $this->assertEquals('a', Sanitize::sanitize('a', SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
Carsten  Rose's avatar
Carsten Rose committed
57
        $this->assertEquals('a', Sanitize::sanitize('a', SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
58
        $this->assertEquals('a', Sanitize::sanitize('a', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
Carsten  Rose's avatar
Carsten Rose committed
59
60
61


        # Check 'a@-_.,;Z09'
62
63
        $val = 'a@-_.,;Z09';
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
64
65
        $this->assertEquals('!!digit!!', Sanitize::sanitize($val, SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
66
67
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
68
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
Carsten  Rose's avatar
Carsten Rose committed
69
70

        # Check 'a+Z09'
71
        $val = 'a+Z09';
72
73
74
        $this->assertEquals('!!alnumx!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
        $this->assertEquals('!!digit!!', Sanitize::sanitize($val, SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
75
76
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
77
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
78
79
80
81

        # Check 'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüÿ'
        $val = 'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüÿ';
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
82
83
        $this->assertEquals('!!digit!!', Sanitize::sanitize($val, SANITIZE_ALLOW_DIGIT), "SANITIZE_DIGIT fails");
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_EMAIL fails");
84
85
86
87
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");

88
        # Check Decimal Format
89
        $msg = "Sanitize: Decimal Format check fails";
90
        $val = '123.45';
91
92
93
94
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'), $msg);
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '10,3'), $msg);
        $this->assertEquals('!!all!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '4,2'), $msg);
        $this->assertEquals('!!all!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,1'), $msg);
95
        $val = '-123.45';
96
97
98
99
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'), $msg);
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '10,3'), $msg);
        $this->assertEquals('!!all!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '4,2'), $msg);
        $this->assertEquals('!!all!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,1'), $msg);
100
        $val = 'a.00';
101
        $this->assertEquals('!!all!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'), $msg);
102
        $val = '-0.1e9';
103
        $this->assertEquals('!!all!!', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'), $msg);
104
        $val = '-4';
105
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'), $msg);
106
        $val = '.42';
107
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'), $msg);
108
109
    }

110
111
112
    /**
     * @throws UserFormException
     */
113
    public function testSanitizeMinMax() {
114
115
116
        $msg = "SANITIZE_MIN_MAX fails";

        # Check numerical min/max
117
        $val = 56;
118
119
120
        $this->assertEquals('', Sanitize::checkMinMax($val, "0", "2"), $msg);
        $this->assertEquals($val, Sanitize::checkMinMax($val, "0", ""), $msg);
        $this->assertEquals($val, Sanitize::checkMinMax($val, "", "56"), $msg);
121

122
        $this->assertEquals('', Sanitize::checkMinMax($val, "57", ""), $msg);
123
        $this->assertEquals('', Sanitize::checkMinMax($val, "", "2"), $msg);
124
125
        $this->assertEquals($val, Sanitize::checkMinMax($val, "0", "200"), $msg);
        $this->assertEquals($val, Sanitize::checkMinMax($val, "-100", "200"), $msg);
126

127
        $val = -56;
128
129
130
        $this->assertEquals('', Sanitize::checkMinMax($val, "0", "2"), $msg);
        $this->assertEquals('', Sanitize::checkMinMax($val, "0", "200"), $msg);
        $this->assertEquals($val, Sanitize::checkMinMax($val, "-100", "200"), $msg);
131
132

        # Check min/max dates
133
        $msg = "SANITIZE_MIN_MAX Date fails";
134
        $val = "2010-05-01";
135
136
137
138
139
        $this->assertEquals($val, Sanitize::checkMinMax($val, "2010-01-01", "2010-12-31"), $msg);
        $this->assertEquals('', Sanitize::checkMinMax($val, "2010-01-01", "2010-04-30"), $msg);
        $this->assertEquals('', Sanitize::checkMinMax($val, "2010-01-01", "2009-12-31"), $msg);
        $this->assertEquals('', Sanitize::checkMinMax($val, "2011-01-01", "2009-12-31"), $msg);
        $this->assertEquals($val, Sanitize::checkMinMax($val, "2010-05-01", "2010-05-01"), $msg);
140
141
    }

142
143
144
145
    /**
     * @throws CodeException
     * @throws UserFormException
     */
146
147
148
149
    public function testSanitizeEmail() {

        # Check
        $val = 'john';
150
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
151
152

        $val = 'john@';
153
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
154
155

        $val = 'john@doe';
156
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
157
158
159
160
161

        $val = 'john@doe.com';
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");

        $val = 'john@ doe.com';
162
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
163
164

        $val = '<john@doe.com>';
165
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
166
167

        $val = 'John Doe <john@doe.com>';
168
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
169
170
171
172
173

        $val = '_john@doe.com';
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");

        $val = 'jo*hn@doe.com';
174
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
175
176

        $val = 'jo[hn@doe.com';
177
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
178
179

        $val = 'jo\hn@doe.com';
180
        $this->assertEquals('!!email!!', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
181
182

        $val = 'jo%hn@doe.com';
Carsten  Rose's avatar
Carsten Rose committed
183
        $this->assertEquals('jo%hn@doe.com', Sanitize::sanitize($val, SANITIZE_ALLOW_EMAIL), "SANITIZE_ALLOW_EMAIL fails");
184
185
    }

186
187
188
189
    /**
     * @throws CodeException
     * @throws UserFormException
     */
190
191
192
193
    public function testSanitizePattern() {

        # Check
        $val = 'john';
194
195
        $this->assertEquals('!!pattern!!', Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '\d'), "SANITIZE_ALLOW_PATTERN fails");
        $this->assertEquals('!!pattern!!', Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '\s'), "SANITIZE_ALLOW_PATTERN fails");
196
197
198
199
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, 'john'), "SANITIZE_ALLOW_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '(john)?'), "SANITIZE_ALLOW_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '(john)*'), "SANITIZE_ALLOW_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '(John)*'), "SANITIZE_ALLOW_PATTERN fails");
Carsten  Rose's avatar
Carsten Rose committed
200
201
    }

202
    //[ ]  { } % & \ #
203

204
205
206
    /**
     */
    public function testSanitizeExceptionAllBut() {
207
208
        $bad = "[]{}%\\#";
        $good = 'abCD01`~&!@$^*()_+=-|":;.,<>/?\'';
209
210

        // Single
211
        $this->assertEquals('!!allbut!!', Sanitize::sanitize('[', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLOW_ALLBUT fails");
212
213
214
215
216
        $this->assertEquals('a', Sanitize::sanitize('a', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLOW_ALLBUT fails");


        for ($i = 0; $i < strlen($bad); $i++) {
            $str = '-' . substr($bad, $i, 1) . '-';
217
            $this->assertEquals('!!allbut!!', Sanitize::sanitize($str, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLOW_ALLBUT fails");
218
219
220
221
222
223
224
225
        }

        for ($i = 0; $i < strlen($good); $i++) {
            $str = '-' . substr($good, $i, 1) . '-';
            $this->assertEquals($str, Sanitize::sanitize($str, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLOW_ALLBUT fails");
        }
    }

Carsten  Rose's avatar
Carsten Rose committed
226
227
228
229
    /**
     * @expectedException \qfq\CodeException
     */
    public function testSanitizeException() {
230
        Sanitize::sanitize('Hello World', 'invalid sanitize class');
Carsten  Rose's avatar
Carsten Rose committed
231
232
    }

233
    /**
234
     * @expectedException \qfq\UserFormException
235
236
     */
    public function testSanitizeExceptionCheckFailed() {
237
        Sanitize::sanitize('string', SANITIZE_ALLOW_DIGIT, '', '', SANITIZE_EXCEPTION);
238
    }
239
240
241
242
243
244

    /**
     * Test string, numeric, array, subarray
     *
     * @throws CodeException
     */
245
    public function testNormalize() {
246

247
248
249
        // Nothing changed
        $char_A_ring = "\xC3\x85"; // 'LATIN CAPITAL LETTER 'A' WITH RING ABOVE' (U+00C5)
        $this->assertEquals($char_A_ring, Sanitize::normalize($char_A_ring), "'A' with ring above");
250

251
252
253
        // Convert "\xCC\x8A" to  "\xC3\x85"
        $char_combining_ring_above = 'A' . "\xCC\x8A";  // 'COMBINING RING ABOVE' (U+030A)
        $this->assertEquals($char_A_ring, Sanitize::normalize($char_combining_ring_above), "Combined 'A' with ring above");
254

255
256
257
        $in = [$char_A_ring, $char_combining_ring_above, $char_A_ring];
        $out = [$char_A_ring, $char_A_ring, $char_A_ring];
        $this->assertEquals($out, Sanitize::normalize($in), "Combined 'A' with ring above");
258

259
260
        $value = 0;
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');
261

262
263
        $value = '';
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');
264

265
266
        $value = 'string';
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');
267

268
269
        $value = 123.45;
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');
270

271
272
273
274
275
        $value = ['string', 'second'];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');

        $value = ['string', 'second', 123.45];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');
276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
        $value = ['string', 0, ''];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');

        $value = ['a' => 'string', 'b' => 'second'];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');

        $value = ['a' => 'string', 'b' => 'second', 'c' => 123.45];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');

        $value = ['a' => 'string', 'b' => 0, 'c' => ''];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');

        // subarray
        $value = [$value, 'a' => 'string', 'b' => 0, 'c' => ''];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');

        // sub sub array
        $value = [$value, 'a' => 'string', 'b' => 0, 'c' => ''];
        $this->assertEquals($value, Sanitize::normalize($value), 'Check simple string');
    }
297

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

    /**
     * Test string, numeric, array, subarray
     *
     */
    public function testSafeFilename() {

        $value = '';
        $this->assertEquals($value, Sanitize::safeFilename($value), 'Empty string');

        $value = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        $this->assertEquals($value, Sanitize::safeFilename($value), 'Alnum string');

        $value = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.pdf';
        $this->assertEquals($value, Sanitize::safeFilename($value), 'Alnum string with .');

        $value = '1ü2ö3ä4Ü5Ö6Ä7';
        $this->assertEquals('1ue2oe3ae4Ue5Oe6Ae7', Sanitize::safeFilename($value), 'Alnum string with umlaut');

        $value = '`~!@#$%^&*()_+=-[]{}\|;:\'"/?.> ,<`';
318
        $this->assertEquals('_______________-____________._____', Sanitize::safeFilename($value), 'Alnum string with umlaut');
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339


        $value = '';
        $this->assertEquals($value, Sanitize::safeFilename($value, true), 'Empty string');

        $value = 'test';
        $this->assertEquals('test', Sanitize::safeFilename($value, true));

        $value = 'test,./hello?ö';
        $this->assertEquals('hello_oe', Sanitize::safeFilename($value, true));


        $value = '';
        $this->assertEquals($value, Sanitize::safeFilename($value, false, true), 'Empty string');

        $value = 'test';
        $this->assertEquals('test', Sanitize::safeFilename($value, false, true));

        $value = 'test,./?ö';
        $this->assertEquals('test_./_oe', Sanitize::safeFilename($value, false, true));

340
    }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

    /**
     * Test string, numeric
     *
     */
    public function testDigitCheckAndCleanGet() {

        unset ($_GET[CLIENT_PAGE_LANGUAGE]);
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
        $this->assertEquals(false, isset($_GET[CLIENT_PAGE_LANGUAGE]));

        $_GET[CLIENT_PAGE_LANGUAGE] = '';
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
        $this->assertEquals($_GET[CLIENT_PAGE_LANGUAGE], '');

        $_GET[CLIENT_PAGE_LANGUAGE] = '0';
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
        $this->assertEquals($_GET[CLIENT_PAGE_LANGUAGE], '0');

        $_GET[CLIENT_PAGE_LANGUAGE] = '1234';
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
        $this->assertEquals($_GET[CLIENT_PAGE_LANGUAGE], '1234');

        $_GET[CLIENT_PAGE_LANGUAGE] = 'abc';
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
        $this->assertEquals($_GET[CLIENT_PAGE_LANGUAGE], '');

        $_GET[CLIENT_PAGE_LANGUAGE] = '54abc';
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
        $this->assertEquals($_GET[CLIENT_PAGE_LANGUAGE], '5');
    }

373
374
375
376
377
378
379
380
381
    /**
     * @throws CodeException
     * @throws UserFormException
     */
    public function testTypeMessageViolate() {

        // Default
        $result = Sanitize::sanitize('badstring', 'digit');
        $this->assertEquals('!!digit!!', $result);
382

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        // SANITIZE_TYPE_MESSAGE_VIOLATE_CLASS
        $result = Sanitize::sanitize('badstring', 'digit', '', '', SANITIZE_EMPTY_STRING,
            '', SANITIZE_TYPE_MESSAGE_VIOLATE_CLASS);
        $this->assertEquals('!!digit!!', $result);

        // SANITIZE_TYPE_MESSAGE_VIOLATE_EMPTY
        $result = Sanitize::sanitize('badstring', 'digit', '', '', SANITIZE_EMPTY_STRING,
            '', SANITIZE_TYPE_MESSAGE_VIOLATE_EMPTY);
        $this->assertEquals('', $result);

        // SANITIZE_TYPE_MESSAGE_VIOLATE_ZERO
        $result = Sanitize::sanitize('badstring', 'digit', '', '', SANITIZE_EMPTY_STRING,
            '', SANITIZE_TYPE_MESSAGE_VIOLATE_ZERO);
        $this->assertEquals('0', $result);

        // SANITIZE_TYPE_MESSAGE_VIOLATE_ ... custom
        $result = Sanitize::sanitize('badstring', 'digit', '', '', SANITIZE_EMPTY_STRING,
            '', 'custom message');
        $this->assertEquals('custom message', $result);

    }
Carsten  Rose's avatar
Carsten Rose committed
404
}