EvaluateTest.php 13.6 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
use qfq\Store;
12
use qfq;
Carsten  Rose's avatar
Carsten Rose committed
13
14
15
16
17
18

require_once(__DIR__ . '/AbstractDatabaseTest.php');
require_once(__DIR__ . '/../../qfq/Evaluate.php');
require_once(__DIR__ . '/../../qfq/Database.php');
require_once(__DIR__ . '/../../qfq/store/Store.php');

19
class EvaluateTest extends \AbstractDatabaseTest {
Carsten  Rose's avatar
Carsten Rose committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    public function testVars() {
        $eval = new \qfq\Evaluate($this->store, $this->db);

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

45
    public function testParse() {
Carsten  Rose's avatar
Carsten Rose committed
46
47
48
49
50
51
52
53
54
55
56
57
58
        $eval = new \qfq\Evaluate($this->store, $this->db);

        // database: lower case
        $this->assertEquals('1DoeJohnmalec2SmithJanefemalea,c', $eval->parse('{{select * from Person where id < 3 order by id}}'));

        // database: upper case
        $this->assertEquals('1DoeJohnmalec2SmithJanefemalea,c', $eval->parse('{{SELECT * FROM Person WHERE id < 3 ORDER BY id}}'));


        $this->store->setVar('sql', 'SELECT * FROM Person WHERE id < 3 ORDER BY id', 'C');
        $this->assertEquals('SELECT * FROM Person WHERE id < 3 ORDER BY id', $eval->parse('{{sql:C:all}}'));
        $this->assertEquals('1DoeJohnmalec2SmithJanefemalea,c', $eval->parse('{{{{sql:C:all}}}}'));

59
        // Get 2 row Array
Carsten  Rose's avatar
Carsten Rose committed
60
61
62
63
64
65
66
67
68
69
        $expected = [
            [
                'id' => '1',
                'name' => 'Doe'
            ],
            [
                'id' => '2',
                'name' => 'Smith'
            ],
        ];
70
        $this->assertEquals($expected, $eval->parse('{{!SELECT id, name FROM Person WHERE id < 3 ORDER BY id}}'));
Carsten  Rose's avatar
Carsten Rose committed
71

72
73
74
75
        // 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
76
77
78
79
80
81
82
83
84
85
86
87
88
        $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
89
90
        $expected = "idbigint(20)NOPRIauto_incrementnamevarchar(128)YESfirstnamevarchar(128)YESgenderenum('','male','female')NOmalegroupsset('','a','b','c')NO";

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

93

Carsten  Rose's avatar
Carsten Rose committed
94
95
96
97
98
99
100
101
102
103
104
    }

    public function testDbVars() {
        $eval = new \qfq\Evaluate($this->store, $this->db);

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

105
106
107
108
109
    public function testParseArray() {
        $eval = new \qfq\Evaluate($this->store, $this->db);

        $data = [
            'formName' => 'MyTestForm',
110
            'title' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee'
111
112
113
114
115
116
117
        ];
        $expected = [
            'formName' => 'MyTestForm',
            'title' => 'Person: John Doe / Employee'
        ];

        $this->assertEquals($expected, $eval->parseArray($data));
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
    }

    public function testParseArrayOfArray() {
        $eval = new \qfq\Evaluate($this->store, $this->db);
        $data = [
            [
                'formName' => 'MyTestForm',
                'title' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee'
            ],
            [
                'order' => '100',
                'class' => 'Person: {{SELECT firstname, " ", name FROM Person LIMIT 1}} / Employee'
            ]
        ];
        $expected = [
            [
                'formName' => 'MyTestForm',
                'title' => 'Person: John Doe / Employee'
            ],
            [
                'order' => '100',
                'class' => 'Person: John Doe / Employee'
            ]
        ];

        $this->assertEquals($expected, $eval->parseArray($data));
144
145
    }

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    public function testSubstituteSql() {
        $eval = new \qfq\Evaluate($this->store, $this->db);

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

    public function testSubstituteVar() {
        $eval = new \qfq\Evaluate($this->store, $this->db);

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

        $this->assertEquals('', $eval->substitute('a:F:digit', $foundInStore));
        $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);

    }

    public function testSubstituteVarEscape() {
        $eval = new \qfq\Evaluate($this->store, $this->db);

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


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


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


    }


Carsten  Rose's avatar
Carsten Rose committed
313
314
    protected function setUp() {

315
        $this->store = Store::getInstance('form=TestFormName', true);
Carsten  Rose's avatar
Carsten Rose committed
316
317
        parent::setUp();

318
        $this->store->setVar('form', 'TestFormName', STORE_TYPO3);
Carsten  Rose's avatar
Carsten Rose committed
319
320
321
322
        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);

    }
}