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

Marc Egger's avatar
Marc Egger committed
9
namespace IMATHUZH\Qfq\Tests\Unit\Core\Helper;
Carsten  Rose's avatar
Carsten Rose committed
10

Marc Egger's avatar
Marc Egger committed
11
12
 
use IMATHUZH\Qfq\Core\Helper\Sanitize;
13
use PHPUnit\Framework\TestCase;
Carsten  Rose's avatar
Carsten Rose committed
14

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

21
    /**
Marc Egger's avatar
Marc Egger committed
22
23
     * @throws \CodeException
     * @throws \UserFormException
24
     */
Carsten  Rose's avatar
Carsten Rose committed
25
26
27
28
29
    public function testSanitize() {

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

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

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

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

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


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

        # Check 'a+Z09'
73
        $val = 'a+Z09';
74
75
76
        $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");
77
78
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_PATTERN, '.*'), "SANITIZE_PATTERN fails");
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
79
        $this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
80
81
82
83

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

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

112
    /**
Marc Egger's avatar
Marc Egger committed
113
     * @throws \UserFormException
114
     */
115
    public function testSanitizeMinMax() {
116
117
118
        $msg = "SANITIZE_MIN_MAX fails";

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

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

129
        $val = -56;
130
131
132
        $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);
133
134

        # Check min/max dates
135
        $msg = "SANITIZE_MIN_MAX Date fails";
136
        $val = "2010-05-01";
137
138
139
140
141
        $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);
142
143
    }

144
    /**
Marc Egger's avatar
Marc Egger committed
145
146
     * @throws \CodeException
     * @throws \UserFormException
147
     */
148
149
150
151
    public function testSanitizeEmail() {

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

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

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

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

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

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

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

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

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

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

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

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

188
    /**
Marc Egger's avatar
Marc Egger committed
189
190
     * @throws \CodeException
     * @throws \UserFormException
191
     */
192
193
194
195
    public function testSanitizePattern() {

        # Check
        $val = 'john';
196
197
        $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");
198
199
200
201
        $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
202
203
    }

204
    //[ ]  { } % & \ #
205

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

        // Single
213
        $this->assertEquals('!!allbut!!', Sanitize::sanitize('[', SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLOW_ALLBUT fails");
214
215
216
217
218
        $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) . '-';
219
            $this->assertEquals('!!allbut!!', Sanitize::sanitize($str, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLOW_ALLBUT fails");
220
221
222
223
224
225
226
227
        }

        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
228
    /**
Marc Egger's avatar
Marc Egger committed
229
     * @expectedException CodeException
Carsten  Rose's avatar
Carsten Rose committed
230
231
     */
    public function testSanitizeException() {
232
        Sanitize::sanitize('Hello World', 'invalid sanitize class');
Carsten  Rose's avatar
Carsten Rose committed
233
234
    }

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

    /**
     * Test string, numeric, array, subarray
     *
Marc Egger's avatar
Marc Egger committed
245
     * @throws \CodeException
246
     */
247
    public function testNormalize() {
248

249
250
251
        // 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");
252

253
254
255
        // 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");
256

257
258
259
        $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");
260

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

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

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

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

273
274
275
276
277
        $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');
278

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        $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');
    }
299

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

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


        $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));

342
    }
343
344
345
346
347
348
349
350
351

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

        unset ($_GET[CLIENT_PAGE_LANGUAGE]);
        Sanitize::digitCheckAndCleanGet(CLIENT_PAGE_LANGUAGE);
Carsten  Rose's avatar
Carsten Rose committed
352
        $this->assertEquals('', $_GET[CLIENT_PAGE_LANGUAGE]);
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

        $_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');
    }

375
    /**
Marc Egger's avatar
Marc Egger committed
376
377
     * @throws \CodeException
     * @throws \UserFormException
378
379
380
381
382
383
     */
    public function testTypeMessageViolate() {

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

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
        // 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
406
}