StoreTest.php 15.9 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

13
14
15
16
17
/**
 * Class StoreTest
 * @package qfq
 */
class StoreTest extends TestCase {
18

19
20
21
    /**
     * @var Store
     */
22
    private $store = null;
23

24
    private $setUpDone = false;
25

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

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

    }

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

        $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
60
61
62
63
    /**
     * @throws CodeException
     * @throws UserFormException
     */
64
    public function testGetVarStoreTypo3() {
65
        // T3 Bodytext
66
67
        $this->store->setVar(TYPO3_FORM, "testformnameDoNotChange", STORE_TYPO3);
        $this->assertEquals('testformnameDoNotChange', $this->store->getVar(TYPO3_FORM, STORE_TYPO3), "System: " . TYPO3_FORM);
68
69
70

    }

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

80
81
    }

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

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

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

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

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

99
100
    }

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

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

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

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

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

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        // 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
134
        // Test overwrite default sanitize class
135
        $this->assertEquals('!!digit!!', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANITIZE_ALLOW_DIGIT), "Param: overwrite default sanitize class");
136
137
138

        // 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
139
140
    }

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

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

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

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

        $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
165
        $this->assertEquals('female2', $this->store->getVar('gender', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'gender' from STORE_SIP");
166

167
    }
168

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

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

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

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

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

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

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

Carsten  Rose's avatar
Carsten Rose committed
205
206
207
208
    /**
     * @throws CodeException
     * @throws UserFormException
     */
209
210
211
    public function testSetVarArray() {
        $this->store->unsetStore(STORE_RECORD);
        $arr = ['a' => '1', 'apple' => 'green'];
212
        $this->store->setStore($arr, STORE_RECORD);
213
214
215
216
        $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
217
218
219
220
    /**
     * @throws CodeException
     * @throws UserFormException
     */
221
222
223
    public function testSetVarArrayEmpty() {
        $this->store->unsetStore(STORE_RECORD);
        $arr = array();
224
        $this->store->setStore($arr, STORE_RECORD);
225
226
        $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }
227

Carsten  Rose's avatar
Carsten Rose committed
228
229
230
231
232
    /**
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//    /**
//     * @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.");
//    }
//
274
    /**
Carsten  Rose's avatar
Carsten Rose committed
275
276
277
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
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
     */
    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));
    }

308
309
    /**
     * @param $body
Carsten  Rose's avatar
Carsten Rose committed
310
     * @return bool|string
311
312
313
314
315
316
317
318
319
320
321
     */
    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
322
323
324
325
326
    /**
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
327
328
329
330
    public function testConfigIniDefaultValues() {

        $expect = [

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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
            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_QFQ_LOG => SYSTEM_QFQ_LOG_FILE,
            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_SESSION_TIMEOUT_SECONDS => SYSTEM_SESSION_TIMEOUT_SECONDS_DEFAULT,
            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 => '',
            SYSTEM_SAVE_BUTTON_TOOLTIP => 'Save',
            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',
            F_FE_DATA_REQUIRED_ERROR => 'data required',
            F_FE_DATA_MATCH_ERROR => 'fields do not match',
            F_FE_DATA_ERROR => 'error',
            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>',
            SYSTEM_SQL_LOG => SYSTEM_SQL_LOG_FILE,
            SYSTEM_MAIL_LOG => SYSTEM_MAIL_LOG_FILE,
393
        ];
394

395
396
397
398
399
400
401
402
403
404
        $body = <<< EOT
            <?php
            return [
                // Comment
                'DB_1_USER' => '<DBUSER>',
                'DB_1_SERVER' => '<DBSERVER>',
                'DB_1_PASSWORD' => '<DBPW>',
                'DB_1_NAME' => '<DB>',
            ];
EOT;
405

406
407
408
409
        $fileName = $this->createFile($body);
        $this->store = Store::getInstance('', true, $fileName);
        unlink($fileName);

410
        $config = $this->store->getStore(STORE_SYSTEM);
411

412
        # The following won't be checked by content, cause they will change on different installations.
413
414
//        foreach ([ SYSTEM_SQL_LOG, SYSTEM_MAIL_LOG, SYSTEM_SITE_PATH, SYSTEM_EXT_PATH, SYSTEM_SEND_E_MAIL] as $key) {
        foreach ([ SYSTEM_SITE_PATH, SYSTEM_EXT_PATH, SYSTEM_SEND_E_MAIL] as $key) {
415
416
417
            $this->assertTrue(isset($config[$key]), "Missing default value for '$key' "  );
            unset ($config[$key]);
        }
418
        // check default values
419
        $this->assertEquals($expect, $config, "Retrieve system store.");
420
421
    }

422
    /**
423
     * @expectedException \qfq\UserFormException
424
     *
Carsten  Rose's avatar
Carsten Rose committed
425
426
     * @throws CodeException
     * @throws UserFormException
427
     */
428
429
    public function testGetStore() {
        $this->assertEquals(array(), $this->store->getStore('unknownstore'));
430
431
    }

432
}