StoreTest.php 17 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/2/16
 * Time: 9:16 PM
 */

9
namespace qfq;
10

11
use PHPUnit\Framework\TestCase;
12

Carsten  Rose's avatar
Carsten Rose committed
13
14
#require_once(__DIR__ . '/../../../../Source/core/Constants.php');

15
16
17
18
19
/**
 * Class StoreTest
 * @package qfq
 */
class StoreTest extends TestCase {
20

21
22
23
    /**
     * @var Store
     */
24
    private $store = null;
25

26
    private $setUpDone = false;
27

Carsten  Rose's avatar
Carsten Rose committed
28
29
30
31
32
    /**
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
33
    public function setUp() {
34
        // Client Variables has to setup before the first instantiation of 'Store'
35
36
37
38
        $_GET[CLIENT_RECORD_ID] = '1234';
        $_GET['key01'] = '1234';
        $_POST['key02'] = '2345';
        $_POST['key03'] = '3456';
Carsten  Rose's avatar
Carsten Rose committed
39
        $_POST[CLIENT_FORM] = 'testformnameDoNotChange';
40
41
42
        $_GET['key04'] = '4567';
        $_POST['key04'] = '5678';

Carsten  Rose's avatar
Carsten Rose committed
43
        $this->store = Store::getInstance("\n; some comment\n" . TYPO3_FORM . "=testformnameDoNotChange\n", true, 'init');
44
45
46

    }

Carsten  Rose's avatar
Carsten Rose committed
47
48
49
50
51
    /**
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
52
    public function testGetInstance() {
53
54
        $a = Store::getInstance('', true);
        $b = Store::getInstance('', true);
55
56
57
58
59
60

        $this->assertFalse($a === null, "There should be a class");

        $this->assertEquals($a, $b, "Both classes should be the same");
    }

Carsten  Rose's avatar
Carsten Rose committed
61
62
63
64
    /**
     * @throws CodeException
     * @throws UserFormException
     */
65
    public function testGetVarStoreTypo3() {
66
        // T3 Bodytext
67
68
        $this->store->setVar(TYPO3_FORM, "testformnameDoNotChange", STORE_TYPO3);
        $this->assertEquals('testformnameDoNotChange', $this->store->getVar(TYPO3_FORM, STORE_TYPO3), "System: " . TYPO3_FORM);
69
70
71

    }

Carsten  Rose's avatar
Carsten Rose committed
72
73
74
75
    /**
     * @throws CodeException
     * @throws UserFormException
     */
76
    public function testGetVarStoreSystem() {
77
        // DBUSER in qfq.ini
78
79
        $val = $this->store->getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM);
        $this->assertTrue($val !== false && $val !== '', "SYSTEM_DB_INDEX_DATA found in qfq.ini");
80

81
82
    }

Carsten  Rose's avatar
Carsten Rose committed
83
84
85
86
    /**
     * @throws CodeException
     * @throws UserFormException
     */
87
    public function testSetVarStoreSystem() {
88

89
90
91
        // set new Sessionname
        $this->store->setVar(SYSTEM_SQL_LOG_MODE, "all", STORE_SYSTEM);

92
        // Sessionname: default value
93
        $this->assertEquals('all', $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM), "System: SQL_LOG");
94
95

        // set new Sessionname
96
        $this->store->setVar(SYSTEM_SQL_LOG_MODE, "modify", STORE_SYSTEM);
97

98
        $this->assertEquals('modify', $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM), "System: SQL_LOG");
99

100
101
    }

Carsten  Rose's avatar
Carsten Rose committed
102
103
104
105
    /**
     * @throws CodeException
     * @throws UserFormException
     */
106
    public function testGetVarStoreClient() {
107

Carsten  Rose's avatar
Carsten Rose committed
108
        # Violates SANITIZE class: sanitized string is always an empty string.
109
        # Access are cached:
110

Carsten  Rose's avatar
Carsten Rose committed
111
112
        # Test: Retrieve a variable, default sanitize class
        $this->assertEquals('1234', $this->store->getVar(CLIENT_RECORD_ID, STORE_CLIENT), "FormName: default sanitize class");
113

Carsten  Rose's avatar
Carsten Rose committed
114
115
        # violates default SANITIZE digit: sanitized string is always an empty string.
        $this->assertEquals('', $this->store->getVar(CLIENT_SIP, STORE_CLIENT), "sanitize class digit fails");
116
117
118

        // Test GET
        $this->assertEquals('1234', $this->store->getVar('key01', STORE_CLIENT), "Param: GET");
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        // Test POST
        $this->assertEquals('2345', $this->store->getVar('key02', STORE_CLIENT), "Param: POST");

        // Test not found
        $this->assertFalse($this->store->getVar('keyUnknown', STORE_CLIENT), "Param: unknown");

        // Test Cache value
        $_POST['key03'] = 'lost';
        $this->store->getVar('key03', STORE_CLIENT);
        $this->assertEquals('3456', $this->store->getVar('key03', STORE_CLIENT), "Param: read from cache");

        // Test Cache not found
        $this->store->getVar('keyUnknwon2');
        $this->assertFalse($this->store->getVar('keyUnknown2', STORE_CLIENT), "Param: unknown from cache");

Carsten  Rose's avatar
Carsten Rose committed
135
        // Test overwrite default sanitize class
136
        $this->assertEquals('!!digit!!', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANITIZE_ALLOW_DIGIT), "Param: overwrite default sanitize class");
137
138
139

        // Test: POST higher priority than GET
        $this->assertEquals('5678', $this->store->getVar('key04', STORE_CLIENT), "Param: POST higher priority than GET");
Carsten  Rose's avatar
Carsten Rose committed
140
141
    }

Carsten  Rose's avatar
Carsten Rose committed
142
143
144
145
146
147
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
Carsten  Rose's avatar
Carsten Rose committed
148
    public function testStorePriority() {
149
        //default prio  FSRVD
150

151
        $this->store->unsetStore(STORE_RECORD);
152
153
154
155

        $db = new Database();
        $tableDefinition = $db->getTableDefinition('Person');
        $this->store->fillStoreTableDefaultColumnType($tableDefinition);
156

157
//        $this->assertEquals('male', $this->store->getVar('gender','','alnumx'), "Get default definition from table person.gender");
158
159
160
161
162
163
164
165

        $this->store->setVar('gender', 'female', STORE_RECORD);
        $this->assertEquals('female', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_RECORD");

        $this->store->setVar('gender', 'male2', STORE_SIP);
        $this->assertEquals('male2', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_SIP");

        $this->store->setVar('gender', 'female2', STORE_FORM);
Carsten  Rose's avatar
Carsten Rose committed
166
        $this->assertEquals('female2', $this->store->getVar('gender', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'gender' from STORE_SIP");
167

168
    }
169

Carsten  Rose's avatar
Carsten Rose committed
170
171
172
173
    /**
     * @throws CodeException
     * @throws UserFormException
     */
Carsten  Rose's avatar
Carsten Rose committed
174
    public function testStoreDifferentSanitizeClass() {
175
176
        //default prio  FSRD

177
178
        $this->store->setVar('fruit', 'apple', STORE_RECORD);
        $this->assertEquals('apple', $this->store->getVar('fruit'), "Retrieve 'fruit' from STORE_RECORD");
179

180
        $this->store->setVar('color', 'green', STORE_FORM);
181
        $this->assertEquals('!!digit!!', $this->store->getVar('color'), "Retrieve 'color' from STORE_FORM");
Carsten  Rose's avatar
Carsten Rose committed
182
        $this->assertEquals('green', $this->store->getVar('color', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'color' from STORE_FORM");
183
184
    }

Carsten  Rose's avatar
Carsten Rose committed
185
186
187
188
    /**
     * @throws CodeException
     * @throws UserFormException
     */
189
    public function testGetVarStore0() {
190
191
        //default prio  FSRD

192
        $this->assertEquals(0, $this->store->getVar('fakename', STORE_ZERO), "Retrieve anything from STORE_ZERO");
193
194
    }

Carsten  Rose's avatar
Carsten Rose committed
195
196
197
198
    /**
     * @throws CodeException
     * @throws UserFormException
     */
199
200
201
    public function testUnsetStore() {
        $this->store->unsetStore(STORE_RECORD);

202
        $this->store->setStore(array(), STORE_RECORD);
203
204
205
        $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }

Carsten  Rose's avatar
Carsten Rose committed
206
207
208
209
    /**
     * @throws CodeException
     * @throws UserFormException
     */
210
211
212
    public function testSetVarArray() {
        $this->store->unsetStore(STORE_RECORD);
        $arr = ['a' => '1', 'apple' => 'green'];
213
        $this->store->setStore($arr, STORE_RECORD);
214
215
216
217
        $this->assertEquals('1', $this->store->getVar('a', STORE_RECORD), "Retrieve a value from store 'fake'");
        $this->assertEquals('green', $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }

Carsten  Rose's avatar
Carsten Rose committed
218
219
220
221
    /**
     * @throws CodeException
     * @throws UserFormException
     */
222
223
224
    public function testSetVarArrayEmpty() {
        $this->store->unsetStore(STORE_RECORD);
        $arr = array();
225
        $this->store->setStore($arr, STORE_RECORD);
226
227
        $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }
228

Carsten  Rose's avatar
Carsten Rose committed
229
230
231
232
233
    /**
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    public function testSetVar() {

        $this->store = Store::getInstance('', true);

        foreach ([STORE_SIP, STORE_CLIENT, STORE_EXTRA] AS $store) {
            // initial set
            $this->store->setVar('unitTest', '123', $store);
            $this->assertEquals('123', $this->store->getVar('unitTest', $store), "Retrieve var.");

            // redefine, using default 'overwrite=true'
            $this->store->setVar('unitTest', '1234', $store);
            $this->assertEquals('1234', $this->store->getVar('unitTest', $store), "Retrieve var.");

            // redefine, using explicit 'overwrite=true'
            $this->store->setVar('unitTest', '12345', $store, true);
            $this->assertEquals('12345', $this->store->getVar('unitTest', $store), "Retrieve var.");

            // redefine, using explicit 'overwrite=false' but with the same value (that is ok)
            $this->store->setVar('unitTest', '12345', $store, false);
            $this->assertEquals('12345', $this->store->getVar('unitTest', $store), "Retrieve var.");
        }
    }

Carsten  Rose's avatar
Carsten Rose committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//    /**
//     * @expectedException \qfq\UserFormException
//     *
//     * @throws CodeException
//     * @throws UserFormException
//     * @throws UserReportException
//     */
//    public function testConfigMandatoryValues() {
//
//        $fileName = $this->createFile('');
//        // the following produces Undefined index exception since you give an empty config.ini which does not contain the index.
//        // What do you want to test here?
//        $this->store = Store::getInstance('', true, $fileName);
//        unlink($fileName);
//
//        $this->assertEquals(false, $this->store->getStore(STORE_SYSTEM), "Retrieve system store.");
//    }
//
275
    /**
Carsten  Rose's avatar
Carsten Rose committed
276
277
278
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
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
     */
    public function testAppendToStore() {
        $this->store->unsetStore(STORE_RECORD);

        $this->store->appendToStore(array(), STORE_RECORD);
        $this->assertEquals(array(), $this->store->getStore(STORE_RECORD));

        $param = ['a' => 'hello', 'b' => 'world'];
        $this->store->appendToStore($param, STORE_RECORD);
        $this->assertEquals($param, $this->store->getStore(STORE_RECORD));

        $this->store->appendToStore(null, STORE_RECORD);
        $this->assertEquals($param, $this->store->getStore(STORE_RECORD));

        $this->store->appendToStore(array(), STORE_RECORD);
        $this->assertEquals($param, $this->store->getStore(STORE_RECORD));

        $param2 = ['c' => 'nice', 'd' => 'job'];
        $this->store->appendToStore($param2, STORE_RECORD);
        $this->assertEquals(array_merge($param, $param2), $this->store->getStore(STORE_RECORD));

        $param3 = ['a' => 'well', 'b' => 'done'];
        $this->store->appendToStore($param3, STORE_RECORD);
        $this->assertEquals(array_merge($param3, $param2), $this->store->getStore(STORE_RECORD));

        $this->store->setStore($param, STORE_RECORD, true);
        $this->store->appendToStore([$param2, $param3, $param3], STORE_RECORD);
        $this->assertEquals(array_merge($param, $param2), $this->store->getStore(STORE_RECORD));
    }

309
310
    /**
     * @param $body
Carsten  Rose's avatar
Carsten Rose committed
311
     * @return bool|string
312
313
314
315
316
317
318
319
320
321
322
     */
    private function createFile($body) {

        $tmpFileName = tempnam('/tmp', 'config.qfq.ini.');
        $handle = fopen($tmpFileName, "w");
        fwrite($handle, $body);
        fclose($handle);

        return $tmpFileName;
    }

Carsten  Rose's avatar
Carsten Rose committed
323
324
325
326
327
    /**
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
328
329
330
331
    public function testConfigIniDefaultValues() {

        $expect = [

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
            SYSTEM_DB_1_USER => '<DBUSER>',
            SYSTEM_DB_1_SERVER => '<DBSERVER>',
            SYSTEM_DB_1_PASSWORD => '<DBPW>',
            SYSTEM_DB_1_NAME => '<DB>',
            SYSTEM_DOCUMENTATION_QFQ => SYSTEM_DOCUMENTATION_QFQ_URL,
            SYSTEM_FLAG_PRODUCTION => 'yes',
            SYSTEM_THUMBNAIL_DIR_SECURE => SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT,
            SYSTEM_THUMBNAIL_DIR_PUBLIC => SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT,
            SYSTEM_CMD_INKSCAPE => 'inkscape',
            SYSTEM_CMD_CONVERT => 'convert',
            SYSTEM_CMD_WKHTMLTOPDF => '/opt/wkhtmltox/bin/wkhtmltopdf',
            SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
            SYSTEM_THROW_GENERAL_ERROR => 'no',
            SYSTEM_SQL_LOG_MODE => SQL_LOG_MODE_MODIFY,
            SYSTEM_SHOW_DEBUG_INFO => SYSTEM_SHOW_DEBUG_INFO_AUTO,
            SYSTEM_DB_INIT => 'set names utf8',
            SYSTEM_DB_UPDATE => SYSTEM_DB_UPDATE_AUTO,
            SYSTEM_DB_INDEX_DATA => '1',
            SYSTEM_DB_INDEX_QFQ => '1',
            SYSTEM_ESCAPE_TYPE_DEFAULT => 'm',
            SYSTEM_SECURITY_VARS_HONEYPOT => 'email,username,password',
            SYSTEM_SECURITY_ATTACK_DELAY => 5,
            SYSTEM_SECURITY_SHOW_MESSAGE => '0',
            SYSTEM_SECURITY_GET_MAX_LENGTH => 50,
            SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS => SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS_DEFAULT,
            SYSTEM_ENTER_AS_SUBMIT => 1,
            SYSTEM_EDIT_FORM_PAGE => 'form',
            SYSTEM_EXTRA_BUTTON_INFO_INLINE => '<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>',
            SYSTEM_EXTRA_BUTTON_INFO_BELOW => '<span class="glyphicon glyphicon-info-sign text-info" aria-hidden="true"></span>',
            SYSTEM_EXTRA_BUTTON_INFO_CLASS => '',
            SYSTEM_SAVE_BUTTON_TEXT => '',
Carsten  Rose's avatar
Carsten Rose committed
363
            SYSTEM_SAVE_BUTTON_TOOLTIP => '',
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
            SYSTEM_SAVE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            SYSTEM_SAVE_BUTTON_GLYPH_ICON => 'glyphicon-ok',
            SYSTEM_CLOSE_BUTTON_TEXT => '',
            SYSTEM_CLOSE_BUTTON_TOOLTIP => 'Close',
            SYSTEM_CLOSE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            SYSTEM_CLOSE_BUTTON_GLYPH_ICON => 'glyphicon-remove',
            SYSTEM_DELETE_BUTTON_TEXT => '',
            SYSTEM_DELETE_BUTTON_TOOLTIP => 'Delete',
            SYSTEM_DELETE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            SYSTEM_DELETE_BUTTON_GLYPH_ICON => 'glyphicon-trash',
            SYSTEM_NEW_BUTTON_TEXT => '',
            SYSTEM_NEW_BUTTON_TOOLTIP => 'New',
            SYSTEM_NEW_BUTTON_CLASS => 'btn btn-default navbar-btn',
            SYSTEM_NEW_BUTTON_GLYPH_ICON => 'glyphicon-plus',
            F_BS_COLUMNS => 'col-md-12 col-lg-10',
            F_BS_LABEL_COLUMNS => 'col-md-3 col-lg-3',
            F_BS_INPUT_COLUMNS => 'col-md-6 col-lg-6',
            F_BS_NOTE_COLUMNS => 'col-md-3 col-lg-3',
Carsten  Rose's avatar
Carsten Rose committed
382
383
            F_FE_DATA_REQUIRED_ERROR => F_FE_DATA_REQUIRED_ERROR_DEFAULT,
            F_FE_DATA_MATCH_ERROR => F_FE_DATA_MATCH_ERROR_DEFAULT,
384
            F_FE_DATA_ERROR => F_FE_DATA_ERROR_DEFAULT,
385
386
387
388
389
            F_CLASS_PILL => 'qfq-color-grey-1',
            F_CLASS_BODY => 'qfq-color-grey-2',
            F_BUTTON_ON_CHANGE_CLASS => 'btn-info alert-info',
            SYSTEM_DB_NAME_DATA => '<DB>',
            SYSTEM_DB_NAME_QFQ => '<DB>',
390
391
392
393
394
395
396
397
398
399
            'DB_2_USER' => '<DBUSER>',
            'DB_2_SERVER' => '<DBSERVER>',
            'DB_2_PASSWORD' => '<DBPW>',
            'DB_2_NAME' => '<DB>',
            'DB_3_USER' => '<DBUSER>',
            'DB_3_SERVER' => '<DBSERVER>',
            'DB_3_PASSWORD' => '<DBPW>',
            'DB_3_NAME' => '<DB>',
            'LDAP_1_RDN' => 'LDAP_1_RDN',
            'LDAP_1_PASSWORD' => 'LDAP_1_PASSWORD',
400
            SYSTEM_LABEL_ALIGN => SYSTEM_LABEL_ALIGN_LEFT,
Carsten  Rose's avatar
Carsten Rose committed
401
402
403
            F_FE_DATA_PATTERN_ERROR => F_FE_DATA_PATTERN_ERROR_DEFAULT,
            F_FE_DATA_PATTERN_ERROR_SYSTEM => F_FE_DATA_PATTERN_ERROR_DEFAULT,
            SYSTEM_SECURITY_FAILED_AUTH_DELAY => '3',
404
        ];
405

406
407
        $body = <<< EOT
            <?php
408
            
409
410
411
412
413
414
            return [
                // Comment
                'DB_1_USER' => '<DBUSER>',
                'DB_1_SERVER' => '<DBSERVER>',
                'DB_1_PASSWORD' => '<DBPW>',
                'DB_1_NAME' => '<DB>',
415
416
417
418
419
420
421
422
423
424
425
426
427
428

                'DB_2_USER' => '<DBUSER>',
                'DB_2_SERVER' => '<DBSERVER>',
                'DB_2_PASSWORD' => '<DBPW>',
                'DB_2_NAME' => '<DB>',

                'DB_3_USER' => '<DBUSER>',
                'DB_3_SERVER' => '<DBSERVER>',
                'DB_3_PASSWORD' => '<DBPW>',
                'DB_3_NAME' => '<DB>',

                'LDAP_1_RDN' => 'LDAP_1_RDN',
                'LDAP_1_PASSWORD' => 'LDAP_1_PASSWORD',
                
429
430
            ];
EOT;
431

432
433
434
435
        $fileName = $this->createFile($body);
        $this->store = Store::getInstance('', true, $fileName);
        unlink($fileName);

436
        $config = $this->store->getStore(STORE_SYSTEM);
437

438
        # The following won't be checked by content, cause they will change on different installations.
439
//        foreach ([ SYSTEM_SQL_LOG, SYSTEM_MAIL_LOG, SYSTEM_SITE_PATH, SYSTEM_EXT_PATH, SYSTEM_SEND_E_MAIL] as $key) {
Carsten  Rose's avatar
Carsten Rose committed
440
        foreach ([SYSTEM_SITE_PATH, SYSTEM_EXT_PATH, SYSTEM_SEND_E_MAIL, SYSTEM_SESSION_TIMEOUT_SECONDS, SYSTEM_QFQ_LOG,
Carsten  Rose's avatar
Carsten Rose committed
441
                     SYSTEM_SQL_LOG, SYSTEM_MAIL_LOG, SYSTEM_FILE_MAX_FILE_SIZE] as $key) {
Carsten  Rose's avatar
Carsten Rose committed
442
            $this->assertTrue(isset($config[$key]), "Missing default value for '$key' ");
443
444
            unset ($config[$key]);
        }
445
        // check default values
446
        $this->assertEquals($expect, $config, "Retrieve system store.");
447
448
    }

449
    /**
450
     * @expectedException \qfq\UserFormException
451
     *
Carsten  Rose's avatar
Carsten Rose committed
452
453
     * @throws CodeException
     * @throws UserFormException
454
     */
455
456
    public function testGetStore() {
        $this->assertEquals(array(), $this->store->getStore('unknownstore'));
457
458
    }

459
}