EvaluateTest.php 24 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/15/16
 * Time: 8:24 AM
 */

9
namespace qfq;
Carsten  Rose's avatar
Carsten Rose committed
10

11
require_once(__DIR__ . '/database/AbstractDatabaseTest.php');
Carsten  Rose's avatar
Carsten Rose committed
12

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

Carsten  Rose's avatar
Carsten Rose committed
19
20
21
    /**
     *
     */
Carsten  Rose's avatar
Carsten Rose committed
22
    public function testVars() {
Carsten  Rose's avatar
Carsten Rose committed
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
        try {
            $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);

            // no variable
            $this->assertEquals('nothing', $eval->parse('nothing'));
            $this->assertEquals('TestFormName', $eval->parse('{{form:T}}'));

            //  pure digit variable
            $this->store->setVar('a', '1', 'C');
            $this->assertEquals('1', $eval->parse('{{a:C}}'));

            // variable contains variable
            $this->store->setVar('a', '{{b:C}}', 'C');
            $this->store->setVar('b', '1234', 'C');
            $this->assertEquals('1234', $eval->parse('{{a:C:all}}'));

            // multiple vaules, Recursive
            $this->store->setVar('m1', 'M1=[{{m2:C:all}}]', 'C');
            $this->store->setVar('m2', 'M2=[{{m3:C:all}}]', 'C');
            $this->store->setVar('m3', 'Some really nice text', 'C');
            $this->assertEquals('These are the variables > m1:M1=[M2=[Some really nice text]], m2:M2=[Some really nice text], m3:Some really nice text - end outer line',
                $eval->parse('These are the variables > m1:{{m1:C:all}}, m2:{{m2:C:all}}, m3:{{m3:C:all}} - end outer line'));
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
Carsten  Rose's avatar
Carsten Rose committed
48
49
    }

Carsten  Rose's avatar
Carsten Rose committed
50
51
52
53
54
55
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
56
    public function testParse() {
Carsten  Rose's avatar
Carsten Rose committed
57
58

        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
Carsten  Rose's avatar
Carsten Rose committed
59
60

        // database: lower case
61
        $this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{select id,name,firstName,adrId,gender,groups from Person where id < 3 order by id}}'));
Carsten  Rose's avatar
Carsten Rose committed
62
63

        // database: upper case
64
        $this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{SELECT id,name,firstName,adrId,gender,groups FROM Person WHERE id < 3 ORDER BY id}}'));
Carsten  Rose's avatar
Carsten Rose committed
65
66


67
68
        $this->store->setVar('sql', 'SELECT id,name,firstName,adrId,gender,groups FROM Person WHERE id < 3 ORDER BY id', 'C');
        $this->assertEquals('SELECT id,name,firstName,adrId,gender,groups FROM Person WHERE id < 3 ORDER BY id', $eval->parse('{{sql:C:all}}'));
69
        $this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{{{sql:C:all}}}}'));
Carsten  Rose's avatar
Carsten Rose committed
70

71
        // Get 2 row Array
Carsten  Rose's avatar
Carsten Rose committed
72
73
        $expected = [
            [
Carsten  Rose's avatar
Carsten Rose committed
74
                'id' => '1',
Carsten  Rose's avatar
Carsten Rose committed
75
                'name' => 'Doe',
Carsten  Rose's avatar
Carsten Rose committed
76
77
            ],
            [
Carsten  Rose's avatar
Carsten Rose committed
78
                'id' => '2',
Carsten  Rose's avatar
Carsten Rose committed
79
                'name' => 'Smith',
Carsten  Rose's avatar
Carsten Rose committed
80
81
            ],
        ];
82
        $this->assertEquals($expected, $eval->parse('{{!SELECT id, name FROM Person WHERE id < 3 ORDER BY id}}'));
Carsten  Rose's avatar
Carsten Rose committed
83

84
85
86
87
        // Get empty array
        $this->assertEquals(array(), $eval->parse('{{!SELECT id, name FROM Person WHERE id=0}}'));

        // INSERT: Use 'Eval' to write a record
Carsten  Rose's avatar
Carsten Rose committed
88
89
90
91
92
93
94
95
96
97
98
99
100
        $eval->parse('{{INSERT INTO Person (name, firstname) VALUES (\'Sinatra\', \'Frank\')}}');
        $this->assertEquals('Frank Sinatra', $eval->parse('{{SELECT firstname, " ", name FROM Person WHERE name="Sinatra" AND firstname="Frank"}}'));

        // INSERT & DELETE: Use eval() to write a record
        $eval->parse('{{INSERT INTO Person (name, firstname) VALUES (\'Sinatra\', \'Frank\')}}');
        $eval->parse('{{DELETE FROM Person WHERE name="Sinatra" AND firstname="Frank" LIMIT 1}}');
        $this->assertEquals('1', $eval->parse('{{SELECT count(id) FROM Person WHERE name="Sinatra" AND firstname="Frank"}}'));

        // UPDATE
        $eval->parse('{{UPDATE Person set name=\'Zappa\' WHERE name="Sinatra" AND firstname="Frank" LIMIT 1}}');
        $this->assertEquals('1', $eval->parse('{{SELECT count(id) FROM Person WHERE name="Zappa" AND firstname="Frank"}}'));

        // SHOW tables
101
        $expected = "idbigint(20)NOPRIauto_incrementnamevarchar(128)YESfirstNamevarchar(128)YESadrIdint(11)NO0genderenum('','male','female')NOmalegroupsset('','a','b','c')NOmodifiedtimestampNOCURRENT_TIMESTAMPon update CURRENT_TIMESTAMPcreateddatetimeNO";
102

Carsten  Rose's avatar
Carsten Rose committed
103
104
105
        $this->assertEquals($expected, $eval->parse('{{SHOW COLUMNS FROM Person}}'));
    }

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
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
    public function testParseStore() {

        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);

        $allStores = [STORE_BEFORE, STORE_CLIENT, STORE_TABLE_DEFAULT, STORE_FORM, STORE_LDAP,
            STORE_TABLE_COLUMN_TYPES, STORE_PARENT_RECORD, STORE_SIP, STORE_TYPO3, STORE_USER, STORE_VAR, STORE_SYSTEM];

        // Not found anywhere
        foreach ($allStores as $store) {
            $this->assertEquals("$store - start {{not-set-in-any-store:$store}} end", $eval->parse("$store - start {{not-set-in-any-store:$store}} end"));
        }
        $this->assertEquals("E - start  end", $eval->parse('E - start {{not-set-in-any-store:E}} end'));
        $this->assertEquals("0 - start 0 end", $eval->parse('0 - start {{not-set-in-any-store:0}} end'));

        // All stores (but empty,zero): Check default
        foreach ($allStores as $store) {

            if ($store == STORE_EMPTY || $store == STORE_ZERO) {
                continue;
            }
            $this->assertEquals("$store - start Jane end", $eval->parse("$store - start {{not-set-in-any-store:$store:::Jane}} end"));
        }

        // Set and retrieve
        foreach ($allStores as $store) {
            $this->store::setVar('value', '0123', $store);
            $this->assertEquals("$store - start 0123 end", $eval->parse("$store - start {{value:$store}} end"));
        }

        // Check default sanitize class
        // Set and retrieve
        foreach ($allStores as $store) {
            if ($store == STORE_CLIENT || $store == STORE_FORM) {
                continue;
            }
            $this->store::setVar('value', 'John', $store);
            $this->assertEquals("$store - start John end", $eval->parse("$store - start {{value:$store}} end"));
        }
        $this->store::setVar('value', 'John', STORE_CLIENT);
        $this->assertEquals("start !!digit!! end", $eval->parse('start {{value:C}} end'));
        $this->store::setVar('value', 'John', STORE_FORM);
        $this->assertEquals("start !!digit!! end", $eval->parse('start {{value:F}} end'));

        // All Stores: Sanitize Class explizit set: alnumx
        foreach ($allStores as $store) {
            $this->assertEquals("start John end", $eval->parse('start {{value:' . $store . ':alnumx}} end'));
        }

    }

Carsten  Rose's avatar
Carsten Rose committed
162
163
164
165
166
167
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
Carsten  Rose's avatar
Carsten Rose committed
168
    public function testDbVars() {
Carsten  Rose's avatar
Carsten Rose committed
169
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
Carsten  Rose's avatar
Carsten Rose committed
170
171
172
173
174
175
176

        $eval->parse('{{INSERT INTO Person (name, firstname) VALUES (\'Holiday\', \'Billie\')}}');
        $this->store->setVar('a', '{{b:C:all}}', 'C');
        $this->store->setVar('b', 'Holiday', 'C');
        $this->assertEquals('Holiday', $eval->parse('{{SELECT name FROM Person WHERE name=\'{{a:C:all}}\'}}'));
    }

Carsten  Rose's avatar
Carsten Rose committed
177
178
179
180
181
182
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
183
    public function testParseArray() {
Carsten  Rose's avatar
Carsten Rose committed
184
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
185
186
187

        $data = [
            'formName' => 'MyTestForm',
188
189
            'title' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee',
            'key1' => 'Hello',
Carsten  Rose's avatar
Carsten Rose committed
190
            'key2' => 'World',
191
192
193
        ];
        $expected = [
            'formName' => 'MyTestForm',
194
195
            'title' => 'Person: John Doe / Employee',
            'key1' => 'Hello',
Carsten  Rose's avatar
Carsten Rose committed
196
            'key2' => 'World',
197
198
199
        ];

        $this->assertEquals($expected, $eval->parseArray($data));
200
201
202

        $expected2 = $expected;
        $expected2['title'] = $data['title'];
Carsten  Rose's avatar
Carsten Rose committed
203
        $this->assertEquals($expected2, $eval->parseArray($data, ['formName', 'title']));
204
205

        $expected2 = $expected;
206
        $data['formName'] = 'SELECT * FROM unknown, garbage, WITH missing, parameter';
207
208
209
        $expected2['formName'] = $data['formName'];

        $expected2['title'] = $data['title'];
Carsten  Rose's avatar
Carsten Rose committed
210
        $this->assertEquals($expected2, $eval->parseArray($data, ['formName', 'title']));
211
212
    }

213

Carsten  Rose's avatar
Carsten Rose committed
214
215
216
217
218
219
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
220
    public function testParseArrayOfArray() {
Carsten  Rose's avatar
Carsten Rose committed
221
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
222
223
224
        $data = [
            [
                'formName' => 'MyTestForm',
Carsten  Rose's avatar
Carsten Rose committed
225
                'title' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee',
226
227
228
            ],
            [
                'order' => '100',
Carsten  Rose's avatar
Carsten Rose committed
229
230
                'class' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee',
            ],
231
232
233
234
        ];
        $expected = [
            [
                'formName' => 'MyTestForm',
Carsten  Rose's avatar
Carsten Rose committed
235
                'title' => 'Person: John Doe / Employee',
236
237
238
            ],
            [
                'order' => '100',
Carsten  Rose's avatar
Carsten Rose committed
239
240
                'class' => 'Person: John Doe / Employee',
            ],
241
242
243
        ];

        $this->assertEquals($expected, $eval->parseArray($data));
244
245
    }

Carsten  Rose's avatar
Carsten Rose committed
246
247
248
249
250
251
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
252
    public function testSubstituteSql() {
Carsten  Rose's avatar
Carsten Rose committed
253
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

        $expectArr = [0 => ['name' => 'Holiday', 'firstname' => 'Billie'], 1 => ['name' => 'Holiday', 'firstname' => 'Billie']];

        $eval->parse('{{INSERT INTO Person (name, firstname) VALUES (\'Holiday\', \'Billie\')}}');
        $eval->parse('{{INSERT INTO Person (name, firstname) VALUES (\'Holiday\', \'Billie\')}}');
        $this->store->setVar('a', '{{b:C:all}}', 'C');
        $this->store->setVar('b', 'Holiday', 'C');

        // Fire query: empty
        $this->assertEquals('', $eval->substitute('SELECT name FROM Person WHERE id=0', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query
        $this->assertEquals('Holiday', $eval->substitute('SELECT name FROM Person WHERE name LIKE "Holiday" LIMIT 1', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query, surrounding whitespace
        $this->assertEquals('Holiday', $eval->substitute('    SELECT name FROM Person WHERE name LIKE "Holiday" LIMIT 1   ', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query, lowercase
        $this->assertEquals('Holiday', $eval->substitute('   SELECT    name FROM Person WHERE name LIKE "Holiday" LIMIT 1', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query, join two records
        $this->assertEquals('HolidayBillieHolidayBillie', $eval->substitute('SELECT name, firstname FROM Person WHERE name LIKE "Holiday" LIMIT 2', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query, two records as assoc
        $this->assertEquals($expectArr, $eval->substitute('!SELECT name, firstname FROM Person WHERE name LIKE "Holiday" LIMIT 2', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query, no result
        $this->assertEquals(array(), $eval->substitute('!SELECT name, firstname FROM Person WHERE id=0', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);

        // Fire query, two records as assoc, surrounding whitespace
        $this->assertEquals($expectArr, $eval->substitute('  !select  name, firstname FROM Person WHERE name LIKE "Holiday" LIMIT 2  ', $foundInStore));
        $this->assertEquals(TOKEN_FOUND_IN_STORE_QUERY, $foundInStore);
    }

Carsten  Rose's avatar
Carsten Rose committed
295
296
297
298
299
300
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
301
    public function testSubstituteVar() {
Carsten  Rose's avatar
Carsten Rose committed
302
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

        // Retrieve in PRIO `FSRD`
        $this->store->setVar('a', '1234', STORE_TABLE_DEFAULT, true);
        $this->assertEquals('1234', $eval->substitute('a', $foundInStore));
        $this->assertEquals(STORE_TABLE_DEFAULT, $foundInStore);

        $this->store->setVar('a', '12345', STORE_RECORD, true);
        $this->assertEquals('12345', $eval->substitute('a', $foundInStore));
        $this->assertEquals(STORE_RECORD, $foundInStore);

        $this->store->setVar('a', '123456', STORE_SIP, true);
        $this->assertEquals('123456', $eval->substitute('a', $foundInStore));
        $this->assertEquals(STORE_SIP, $foundInStore);

        $this->store->setVar('a', '1234567', STORE_FORM, true);
        $this->assertEquals('1234567', $eval->substitute('a', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals(false, $eval->substitute('notFound', $foundInStore));
        $this->assertEquals('', $foundInStore);

        // Specific Store
        $this->assertEquals('1234', $eval->substitute('a:D', $foundInStore));
        $this->assertEquals(STORE_TABLE_DEFAULT, $foundInStore);

        $this->assertEquals('12345', $eval->substitute('a:R', $foundInStore));
        $this->assertEquals(STORE_RECORD, $foundInStore);

        $this->assertEquals('123456', $eval->substitute('a:S', $foundInStore));
        $this->assertEquals(STORE_SIP, $foundInStore);

        $this->assertEquals('1234567', $eval->substitute('a:F', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals(false, $eval->substitute('a:V', $foundInStore));
        $this->assertEquals('', $foundInStore);

        // Sanatize Class: digits
        $this->assertEquals('1234567', $eval->substitute('a:F:digit', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals('1234567', $eval->substitute('a:F:alnumx', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals('1234567', $eval->substitute('a:F:allbut', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals('1234567', $eval->substitute('a:F:all', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        // Sanatize Class: text
        $this->store->setVar('a', 'Hello world @-_.,;: /()', STORE_FORM, true);

356
        $this->assertEquals('!!digit!!', $eval->substitute('a:F:digit', $foundInStore));
357
358
359
360
361
362
363
364
365
366
367
368
369
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals('Hello world @-_.,;: /()', $eval->substitute('a:F:alnumx', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals('Hello world @-_.,;: /()', $eval->substitute('a:F:allbut', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        $this->assertEquals('Hello world @-_.,;: /()', $eval->substitute('a:F:all', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

    }

Carsten  Rose's avatar
Carsten Rose committed
370
371
372
373
374
375
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
376
    public function testSubstituteVarEscape() {
Carsten  Rose's avatar
Carsten Rose committed
377
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

        // No escape
        $this->store->setVar('a', 'hello', STORE_FORM, true);
        $this->assertEquals('hello', $eval->substitute('a:F:all', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        // None, Single Tick
        $this->store->setVar('a', 'hello', STORE_FORM, true);
        $this->assertEquals('hello', $eval->substitute('a:F:all:s', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        // None, Double Tick
        $this->store->setVar('a', 'hello', STORE_FORM, true);
        $this->assertEquals('hello', $eval->substitute('a:F:all:d', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

394
395
396
397
398
        // None, Colon
        $this->store->setVar('a', 'hello', STORE_FORM, true);
        $this->assertEquals('hello', $eval->substitute('a:F:all:C', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

399
400
401
402
403
404
405
406
407
408
409

        // ', Single Tick
        $this->store->setVar('a', 'hel\'lo', STORE_FORM, true);
        $this->assertEquals('hel\\\'lo', $eval->substitute('a:F:all:s', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        // ' Double Tick
        $this->store->setVar('a', 'hel\'lo', STORE_FORM, true);
        $this->assertEquals('hel\'lo', $eval->substitute('a:F:all:d', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

410
411
412
413
414
        // Colon
        $this->store->setVar('a', 'hel:lo', STORE_FORM, true);
        $this->assertEquals('hel\:lo', $eval->substitute('a:F:all:C', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436

        // ", Single Tick
        $this->store->setVar('a', 'hel"lo', STORE_FORM, true);
        $this->assertEquals('hel"lo', $eval->substitute('a:F:all:s', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        // ", Double Tick
        $this->store->setVar('a', 'hel"lo', STORE_FORM, true);
        $this->assertEquals('hel\"lo', $eval->substitute('a:F:all:d', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);


        // Multi ', Single Tick
        $this->store->setVar('a', "h\"e' 'l\"lo ' ", STORE_FORM, true);
        $this->assertEquals("h\"e\' \'l\"lo \' ", $eval->substitute('a:F:all:s', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

        // Multi , Double Tick
        $this->store->setVar('a', 'h"e\' \'l"lo \' ', STORE_FORM, true);
        $this->assertEquals('h\"e\' \'l\"lo \' ', $eval->substitute('a:F:all:d', $foundInStore));
        $this->assertEquals(STORE_FORM, $foundInStore);

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//---
        $this->store->setVar('a', ' hello world ', STORE_FORM, true);
        $this->assertEquals(' hello world ', $eval->substitute('a:F:all:l', $foundInStore));

        $this->store->setVar('a', ' hel\lo world ', STORE_FORM, true);
        $this->assertEquals(' hel\5clo world ', $eval->substitute('a:F:all:l', $foundInStore));

        $this->store->setVar('a', ' hel*lo world ', STORE_FORM, true);
        $this->assertEquals(' hel\2alo world ', $eval->substitute('a:F:all:l', $foundInStore));

        $this->store->setVar('a', ' hel(lo world ', STORE_FORM, true);
        $this->assertEquals(' hel\28lo world ', $eval->substitute('a:F:all:l', $foundInStore));

        $this->store->setVar('a', ' hel)lo world ', STORE_FORM, true);
        $this->assertEquals(' hel\29lo world ', $eval->substitute('a:F:all:l', $foundInStore));

        $this->store->setVar('a', " hel\x00lo world ", STORE_FORM, true);
        $this->assertEquals(' hel\00lo world ', $eval->substitute('a:F:all:l', $foundInStore));

        $this->store->setVar('a', ' h\e*l(l)o world ', STORE_FORM, true);
        $this->assertEquals(' h\5ce\2al\28l\29o world ', $eval->substitute('a:F:all:l', $foundInStore));


//        LDAP_ESCAPE_FILTER => array('\\', '*', '(', ')', "\x00"),
//                LDAP_ESCAPE_DN => array('\\', ',', '=', '+', '<', '>', ';', '"', '#'),
        $this->store->setVar('a', ' hello world ', STORE_FORM, true);
463
//        $this->assertEquals('\\20hello world\\20', $eval->substitute('a:F:all:L', $foundInStore));
464
465
466
467
468
469

        $this->store->setVar('a', 'h\e,l=l+o< >w;o"r#ld', STORE_FORM, true);
        $this->assertEquals('h\5ce\2cl\3dl\2bo\3c \3ew\3bo\22r\23ld', $eval->substitute('a:F:all:L', $foundInStore));


        $this->store->setVar('a', ' hel;lo world ', STORE_FORM, true);
470
//        $this->assertEquals('\20hel\3blo world\20', $eval->substitute('a:F:all:sL', $foundInStore));
471
472
473

    }

Carsten  Rose's avatar
Carsten Rose committed
474
475
476
477
478
479
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
480
    public function testSubstituteDefault() {
Carsten  Rose's avatar
Carsten Rose committed
481
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
482
483
484
485

        // No escape
        $this->store->setVar('a', 'hello', STORE_FORM, true);
        $this->assertEquals('hello', $eval->substitute('a:F:all::world', $foundInStore));
486
487
        $this->assertEquals(STORE_FORM, $foundInStore);

488
        $this->assertEquals('world', $eval->substitute('a-notused:F:all::world', $foundInStore));
489
490
        $this->assertEquals(TOKEN_FOUND_AS_DEFAULT, $foundInStore);

491
        $this->assertEquals('hello:world', $eval->substitute('a-notused:F:all::hello\\:world', $foundInStore));
492
493
494
495
        $this->assertEquals(TOKEN_FOUND_AS_DEFAULT, $foundInStore);

        $this->assertEquals(false, $eval->substitute('a-notused:F:all::', $foundInStore));
        $this->assertEquals('', $foundInStore);
496
497
498

    }

499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
    public function testTypeViolateMessage() {

        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);

        $store = STORE_CLIENT;

        $this->store::setVar('value', 'Joh%n', $store);
        $this->assertEquals("$store - start 0 end", $eval->parse("$store - start {{value:$store:digit:::0}} end"));

        $this->store::setVar('value', 'Joh%n', $store);
        $this->assertEquals("$store - start  end", $eval->parse("$store - start {{value:$store:digit:::e}} end"));

        $this->store::setVar('value', 'Joh%n', $store);
        $this->assertEquals("$store - start !!digit!! end", $eval->parse("$store - start {{value:$store:digit:::c}} end"));

        $this->store::setVar('value', 'Joh%n', $store);
        $this->assertEquals("$store - start custom message end", $eval->parse("$store - start {{value:$store:digit:::custom message}} end"));
    }


    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
    public function testParseSpecialCharInParameter() {

        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);

        $store = STORE_CLIENT;

        // default
        $this->assertEquals("$store - start default mit : in string end", $eval->parse("$store - start {{not in store:$store:alnumx::default mit \: in string}} end"));
        $this->assertEquals("$store - start default mit ' in string end", $eval->parse("$store - start {{not in store:$store:alnumx::default mit ' in string}} end"));
        $this->assertEquals("$store - start default mit \" in string end", $eval->parse("$store - start {{not in store:$store:alnumx::default mit \" in string}} end"));

        // typeMessageViolate
        $this->store::setVar('value', 'John', $store);
        $this->assertEquals("$store - start violate standard end", $eval->parse("$store - start {{value:$store::::violate standard}} end"));
        $this->assertEquals("$store - start violate with : in string end", $eval->parse("$store - start {{value:$store::::violate with \: in string}} end"));
        $this->assertEquals("$store - start violate with \' in string end", $eval->parse("$store - start {{value:$store::::violate with ' in string}} end"));
        $this->assertEquals("$store - start violate with \\\" in string end", $eval->parse("$store - start {{value:$store::::violate with \" in string}} end"));
    }

Carsten  Rose's avatar
Carsten Rose committed
550
551
552
553
554
555
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
Carsten  Rose's avatar
Carsten Rose committed
556
557
    protected function setUp() {

Carsten  Rose's avatar
Carsten Rose committed
558
559
560
561
562
563
564
565
566
567
        try {
            $this->store = Store::getInstance('form=TestFormName', true);
            parent::setUp();

            $this->store->setVar('form', 'TestFormName', STORE_TYPO3);
            $this->executeSQLFile(__DIR__ . '/database/fixtures/Generic.sql', true);

        } catch (\Exception $e) {
            echo $e->getMessage();
        }
Carsten  Rose's avatar
Carsten Rose committed
568
569
    }
}