EvaluateTest.php 26.1 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
 */

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

Marc Egger's avatar
Marc Egger committed
11
12
13
14
15
use IMATHUZH\Qfq\Core\Evaluate;
use IMATHUZH\Qfq\Core\Store\Store;
use IMATHUZH\Qfq\Tests\Unit\Core\Database\AbstractDatabaseTest;

require_once(__DIR__ . '/Database/AbstractDatabaseTest.php');
Carsten  Rose's avatar
Carsten Rose committed
16

17
18
19
20
/**
 * Class EvaluateTest
 * @package qfq
 */
21
class EvaluateTest extends AbstractDatabaseTest {
Carsten  Rose's avatar
Carsten Rose committed
22

Carsten  Rose's avatar
Carsten Rose committed
23
24
25
    /**
     *
     */
Carsten  Rose's avatar
Carsten Rose committed
26
    public function testVars() {
Carsten  Rose's avatar
Carsten Rose committed
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
        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
52
53
    }

Carsten  Rose's avatar
Carsten Rose committed
54
    /**
Marc Egger's avatar
Marc Egger committed
55
56
57
58
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
59
     */
60
    public function testParse() {
Carsten  Rose's avatar
Carsten Rose committed
61
62

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

        // database: lower case
65
        $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
66
67

        // database: upper case
68
        $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
69
70


71
72
        $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}}'));
73
        $this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{{{sql:C:all}}}}'));
Carsten  Rose's avatar
Carsten Rose committed
74

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

82
83
84
85
86
87
88
        // Get 2 row Array with nested query
        $expected = [
            ['id' => 1, 'name' => 'Doe', 'cnt' => 1],
            ['id' => 2, 'name' => 'Smith', 'cnt' => 1],
        ];
        $this->assertEquals($expected, $eval->parse('{{!SELECT id, name, {{SELECT 1 }} AS cnt FROM Person WHERE id < 3 ORDER BY id}}'));

89
90
91
        // Get empty array
        $this->assertEquals(array(), $eval->parse('{{!SELECT id, name FROM Person WHERE id=0}}'));

92

93
        // INSERT: Use 'Eval' to write a record
Carsten  Rose's avatar
Carsten Rose committed
94
95
96
97
98
99
100
101
102
103
104
105
106
        $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
107
        $expected = "idbigint(20)NOPRIauto_incrementnamevarchar(128)YESfirstNamevarchar(128)YESadrIdint(11)NO0genderenum('','male','female')NOmalegroupsset('','a','b','c')NOmodifiedtimestampNOCURRENT_TIMESTAMPon update CURRENT_TIMESTAMPcreateddatetimeNO";
108

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

112
    /**
Marc Egger's avatar
Marc Egger committed
113
114
115
116
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
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
     */
    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
168
    /**
Marc Egger's avatar
Marc Egger committed
169
170
171
172
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
173
     */
Carsten  Rose's avatar
Carsten Rose committed
174
    public function testDbVars() {
Carsten  Rose's avatar
Carsten Rose committed
175
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
Carsten  Rose's avatar
Carsten Rose committed
176
177
178
179
180
181
182

        $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
183
    /**
Marc Egger's avatar
Marc Egger committed
184
185
186
187
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
188
     */
189
    public function testParseArray() {
Carsten  Rose's avatar
Carsten Rose committed
190
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
191
192
193

        $data = [
            'formName' => 'MyTestForm',
194
195
            'title' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee',
            'key1' => 'Hello',
Carsten  Rose's avatar
Carsten Rose committed
196
            'key2' => 'World',
197
198
199
        ];
        $expected = [
            'formName' => 'MyTestForm',
200
201
            'title' => 'Person: John Doe / Employee',
            'key1' => 'Hello',
Carsten  Rose's avatar
Carsten Rose committed
202
            'key2' => 'World',
203
204
205
        ];

        $this->assertEquals($expected, $eval->parseArray($data));
206
207
208

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

        $expected2 = $expected;
212
        $data['formName'] = 'SELECT * FROM unknown, garbage, WITH missing, parameter';
213
214
215
        $expected2['formName'] = $data['formName'];

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

219

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

        $this->assertEquals($expected, $eval->parseArray($data));
250
251
    }

Carsten  Rose's avatar
Carsten Rose committed
252
    /**
Marc Egger's avatar
Marc Egger committed
253
254
255
256
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
257
     */
258
    public function testSubstituteSql() {
Carsten  Rose's avatar
Carsten Rose committed
259
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
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
295
296
297
298
299
300

        $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
301
    /**
Marc Egger's avatar
Marc Egger committed
302
303
304
305
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
306
     */
307
    public function testSubstituteVar() {
Carsten  Rose's avatar
Carsten Rose committed
308
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
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

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

Carsten  Rose's avatar
Carsten Rose committed
346
        // Sanitize Class: digits
347
348
349
350
351
352
353
354
355
356
357
358
        $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);

Carsten  Rose's avatar
Carsten Rose committed
359
        // Sanitize Class: text
360
361
        $this->store->setVar('a', 'Hello world @-_.,;: /()', STORE_FORM, true);

362
        $this->assertEquals('!!digit!!', $eval->substitute('a:F:digit', $foundInStore));
363
364
365
366
367
368
369
370
371
372
373
374
375
        $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
376
    /**
Marc Egger's avatar
Marc Egger committed
377
378
379
380
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
381
     */
382
    public function testSubstituteVarEscape() {
Carsten  Rose's avatar
Carsten Rose committed
383
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

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

400
401
402
403
404
        // 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);

405
406
407
408
409
410
411
412
413
414
415

        // ', 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);

416
417
418
419
420
        // 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);

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

        // ", 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);

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//---
        $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);
469
//        $this->assertEquals('\\20hello world\\20', $eval->substitute('a:F:all:L', $foundInStore));
470
471
472
473
474

        $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);
475
//        $this->assertEquals('\20hel\3blo world\20', $eval->substitute('a:F:all:sL', $foundInStore));
476

477

478
479
480
481
482
483
484
        // The test needs a T3 installation to call the T3 password hash function - this is not available on alfred16
        // The password contains a dynamic salt - we can't force the salt and we can't force the hash
//        $this->store->setVar('a', 'password', STORE_FORM, true);
//        $result = $eval->substitute('a:F::p', $foundInStore);
//
//        $this->assertEquals(34, strlen($result));
//        $this->assertEquals('$P$', substr($result, 0, 3));
485

486
487
    }

Carsten  Rose's avatar
Carsten Rose committed
488
    /**
Marc Egger's avatar
Marc Egger committed
489
490
491
492
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
493
     */
494
    public function testSubstituteDefault() {
Carsten  Rose's avatar
Carsten Rose committed
495
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);
496
497
498
499

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

502
        $this->assertEquals('world', $eval->substitute('a-notused:F:all::world', $foundInStore));
503
504
        $this->assertEquals(TOKEN_FOUND_AS_DEFAULT, $foundInStore);

505
        $this->assertEquals('hello:world', $eval->substitute('a-notused:F:all::hello\\:world', $foundInStore));
506
507
508
509
        $this->assertEquals(TOKEN_FOUND_AS_DEFAULT, $foundInStore);

        $this->assertEquals(false, $eval->substitute('a-notused:F:all::', $foundInStore));
        $this->assertEquals('', $foundInStore);
510
511
512

    }

513
    /**
Marc Egger's avatar
Marc Egger committed
514
515
516
517
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
     */
    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"));
    }


    /**
Marc Egger's avatar
Marc Egger committed
540
541
542
543
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
     */
    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"));
    }

564
    /**
Marc Egger's avatar
Marc Egger committed
565
566
567
568
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
569
570
571
572
573
574
575
576
577
578
579
580
581
     */
    public function testParseActionEscape() {
        $eval = new Evaluate($this->store, $this->dbArray[DB_INDEX_DEFAULT]);

        $this->store->setVar('hidden', 'magic value', STORE_FORM, true);
        $this->store->setVar('a', 'start {{hidden:F:alnumx}} end', STORE_FORM, true);

        // Recursive replace (already tested)
        $this->assertEquals('go start magic value end stop', $eval->parse('go {{a:F:all}} stop'));

        // Stop Recursive replace
        $this->assertEquals('go start {{hidden:F:alnumx}} end stop', $eval->parse('go {{a:F:all:S}} stop'));

582
        // Check that there is no exception if not found in the first/second store but in third store
Carsten  Rose's avatar
Carsten Rose committed
583
584
        $this->store->setVar('neverUsedBefore', 'sunshine', STORE_FORM, true);
        $this->assertEquals('go sunshine stop', $eval->parse('go {{neverUsedBefore:SRF:all:X}} stop'));
585
586
587
588

    }

    /**
Marc Egger's avatar
Marc Egger committed
589
     * @expectedException \UserFormException
590
     *
Marc Egger's avatar
Marc Egger committed
591
592
593
594
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
595
596
597
598
599
600
     */
    public function testVariableNotFoundException() {

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

        $eval->parse('go {{unknownVar:S::X}} stop');
601
602
    }

Carsten  Rose's avatar
Carsten Rose committed
603
604
    /**
     */
Carsten  Rose's avatar
Carsten Rose committed
605
606
    protected function setUp() {

Carsten  Rose's avatar
Carsten Rose committed
607
608
609
610
611
        try {
            $this->store = Store::getInstance('form=TestFormName', true);
            parent::setUp();

            $this->store->setVar('form', 'TestFormName', STORE_TYPO3);
Marc Egger's avatar
Marc Egger committed
612
            $this->executeSQLFile(__DIR__ . '/Database/fixtures/Generic.sql', true);
Carsten  Rose's avatar
Carsten Rose committed
613
614
615
616

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