StoreTest.php 15.3 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 qfq;
12

Carsten  Rose's avatar
Carsten Rose committed
13
//use qfq\exceptions\CodeException;
14

15
require_once(__DIR__ . '/../../qfq/store/Store.php');
16
require_once(__DIR__ . '/../../qfq/Constants.php');
Carsten  Rose's avatar
Carsten Rose committed
17
require_once(__DIR__ . '/../../qfq/exceptions/CodeException.php');
18
19
20
21


class StoreTest extends \PHPUnit_Framework_TestCase {

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

27
    private $setUpDone = false;
28
29

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

40
        $this->store = Store::getInstance("\n; some comment\n" . TYPO3_FORM . "=testformnameDoNotChange\n", true);
41
42
43

    }

44
    public function testGetInstance() {
45
46
        $a = Store::getInstance('', true);
        $b = Store::getInstance('', true);
47
48
49
50
51
52

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

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

53
    public function testGetVarStoreTypo3() {
54
        // T3 Bodytext
55
56
        $this->store->setVar(TYPO3_FORM, "testformnameDoNotChange", STORE_TYPO3);
        $this->assertEquals('testformnameDoNotChange', $this->store->getVar(TYPO3_FORM, STORE_TYPO3), "System: " . TYPO3_FORM);
57
58
59

    }

60
    public function testGetVarStoreSystem() {
61
        // DBUSER in qfq.ini
62
        $dbuser = $this->store->getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM);
63
        $this->assertTrue($dbuser !== false && $dbuser !== '', "System: DBUSER found in qfq.ini");
64

65
66
    }

67
    public function testSetVarStoreSystem() {
68

69
70
71
        // set new Sessionname
        $this->store->setVar(SYSTEM_SQL_LOG_MODE, "all", STORE_SYSTEM);

72
        // Sessionname: default value
73
        $this->assertEquals('all', $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM), "System: SQL_LOG");
74
75

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

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

80
81
    }

82

83
    public function testGetVarStoreClient() {
84

Carsten  Rose's avatar
Carsten Rose committed
85
        # Violates SANITIZE class: sanitized string is always an empty string.
86
        # Access are cached:
87

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

Carsten  Rose's avatar
Carsten Rose committed
91
92
        # violates default SANITIZE digit: sanitized string is always an empty string.
        $this->assertEquals('', $this->store->getVar(CLIENT_SIP, STORE_CLIENT), "sanitize class digit fails");
93
94
95

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

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        // 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
112
        // Test overwrite default sanitize class
113
        $this->assertEquals('!!digit!!', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANITIZE_ALLOW_DIGIT), "Param: overwrite default sanitize class");
114
115
116

        // 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
117
118
    }

119

Carsten  Rose's avatar
Carsten Rose committed
120
    public function testStorePriority() {
121
        //default prio  FSRVD
122

123
        $this->store->unsetStore(STORE_RECORD);
124
        $this->store->fillStoreTableDefaultColumnType('Person');
125

126
//        $this->assertEquals('male', $this->store->getVar('gender','','alnumx'), "Get default definition from table person.gender");
127
128
129
130
131
132
133
134

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

137
    }
138

Carsten  Rose's avatar
Carsten Rose committed
139
    public function testStoreDifferentSanitizeClass() {
140
141
        //default prio  FSRD

142
143
        $this->store->setVar('fruit', 'apple', STORE_RECORD);
        $this->assertEquals('apple', $this->store->getVar('fruit'), "Retrieve 'fruit' from STORE_RECORD");
144

145
        $this->store->setVar('color', 'green', STORE_FORM);
146
        $this->assertEquals('!!digit!!', $this->store->getVar('color'), "Retrieve 'color' from STORE_FORM");
Carsten  Rose's avatar
Carsten Rose committed
147
        $this->assertEquals('green', $this->store->getVar('color', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'color' from STORE_FORM");
148
149
    }

150
    public function testGetVarStore0() {
151
152
        //default prio  FSRD

153
        $this->assertEquals(0, $this->store->getVar('fakename', STORE_ZERO), "Retrieve anything from STORE_ZERO");
154
155
156
157
158
    }

    public function testUnsetStore() {
        $this->store->unsetStore(STORE_RECORD);

159
        $this->store->setStore(array(), STORE_RECORD);
160
161
162
163
164
165
        $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }

    public function testSetVarArray() {
        $this->store->unsetStore(STORE_RECORD);
        $arr = ['a' => '1', 'apple' => 'green'];
166
        $this->store->setStore($arr, STORE_RECORD);
167
168
169
170
171
172
173
174
        $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.");
    }


    public function testSetVarArrayEmpty() {
        $this->store->unsetStore(STORE_RECORD);
        $arr = array();
175
        $this->store->setStore($arr, STORE_RECORD);
176
177
        $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }
178

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    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.");
        }
    }

202
203
204
205
206
207
208
209
210
211
212
213
214
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testConfigMandatoryValues() {

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

        $this->assertEquals(false, $this->store->getStore(STORE_SYSTEM), "Retrieve system store.");
    }

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

250
251
    /**
     * @param $body
Carsten  Rose's avatar
Carsten Rose committed
252
     *
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
     * @return string
     */
    private function createFile($body) {

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

        return $tmpFileName;
    }

    public function testConfigIniDefaultValues() {

        $body = <<< EOT
                ; Comment
269
270
271
272
                DB_1_USER = <DBUSER>
                DB_1_SERVER = <DBSERVER>
                DB_1_PASSWORD = <DBPW>
                DB_1_NAME = <DB>
273
274
275
276
277
278
279
280
281

                DB_INIT = set names utf8

                SQL_LOG = sql.log
                ; all, modify
                SQL_LOG_MODE = modify
EOT;

        $expect = [
Carsten  Rose's avatar
Carsten Rose committed
282
283
284
285
286
287
288
289
            SYSTEM_DB_1_USER => '<DBUSER>',
            SYSTEM_DB_1_SERVER => '<DBSERVER>',
            SYSTEM_DB_1_PASSWORD => '<DBPW>',
            SYSTEM_DB_1_NAME => '<DB>',

            SYSTEM_DB_INIT => 'set names utf8',
            SYSTEM_SQL_LOG_MODE => 'modify',

290
291
            SYSTEM_DB_INDEX_DATA => '1',
            SYSTEM_DB_INDEX_QFQ => '1',
Carsten  Rose's avatar
Carsten Rose committed
292
293

            SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
294
            SYSTEM_SHOW_DEBUG_INFO => SYSTEM_SHOW_DEBUG_INFO_AUTO,
Carsten  Rose's avatar
Carsten Rose committed
295

Carsten  Rose's avatar
Carsten Rose committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
            F_BS_COLUMNS => '12',
            F_BS_LABEL_COLUMNS => '3',
            F_BS_INPUT_COLUMNS => '6',
            F_BS_NOTE_COLUMNS => '3',
            F_CLASS_PILL => 'qfq-color-grey-1',
            F_CLASS_BODY => 'qfq-color-grey-2',

            F_SAVE_BUTTON_TEXT => '',
            F_SAVE_BUTTON_TOOLTIP => 'Save',
            F_SAVE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_SAVE_BUTTON_GLYPH_ICON => 'glyphicon-ok',

            F_CLOSE_BUTTON_TEXT => '',
            F_CLOSE_BUTTON_TOOLTIP => 'Close',
            F_CLOSE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_CLOSE_BUTTON_GLYPH_ICON => 'glyphicon-remove',

            F_DELETE_BUTTON_TEXT => '',
            F_DELETE_BUTTON_TOOLTIP => 'Delete',
            F_DELETE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_DELETE_BUTTON_GLYPH_ICON => 'glyphicon-trash',

            F_NEW_BUTTON_TEXT => '',
            F_NEW_BUTTON_TOOLTIP => 'New',
            F_NEW_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_NEW_BUTTON_GLYPH_ICON => 'glyphicon-plus',

            F_BUTTON_ON_CHANGE_CLASS => 'btn-info alert-info',
            SYSTEM_EDIT_FORM_PAGE => 'form',
            SYSTEM_SECURITY_VARS_HONEYPOT => 'email,username,password',
326
            SYSTEM_SECURITY_ATTACK_DELAY => 5,
Carsten  Rose's avatar
Carsten Rose committed
327
            SYSTEM_SECURITY_SHOW_MESSAGE => '0',
328
            SYSTEM_SECURITY_GET_MAX_LENGTH => 50,
329
            SYSTEM_ESCAPE_TYPE_DEFAULT => 'm',
330
331
            SYSTEM_GFX_EXTRA_BUTTON_INFO_INLINE => '<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>',
            SYSTEM_GFX_EXTRA_BUTTON_INFO_BELOW => '<span class="glyphicon glyphicon-info-sign text-info" aria-hidden="true"></span>',
Carsten  Rose's avatar
Carsten Rose committed
332
            SYSTEM_EXTRA_BUTTON_INFO_CLASS => '',
333

Carsten  Rose's avatar
Carsten Rose committed
334
            SYSTEM_DB_UPDATE => SYSTEM_DB_UPDATE_AUTO,
Carsten  Rose's avatar
Carsten Rose committed
335
            SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS => SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS_DEFAULT,
Carsten  Rose's avatar
Carsten Rose committed
336

Carsten  Rose's avatar
Carsten Rose committed
337
            DOCUMENTATION_QFQ => DOCUMENTATION_QFQ_URL,
Carsten  Rose's avatar
Carsten Rose committed
338
            SYSTEM_ENTER_AS_SUBMIT => 1,
339
340
341
342
            SYSTEM_CMD_INKSCAPE => 'inkscape',
            SYSTEM_CMD_CONVERT => 'convert',
            SYSTEM_THUMBNAIL_DIR_PUBLIC => SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT,
            SYSTEM_THUMBNAIL_DIR_SECURE => SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT,
343
344
345
346
347
348
349
        ];

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

        $value = $this->store->getStore(STORE_SYSTEM);
350
        // these values are different between runtime and PHPUnit environment: do not check
351
352
353
        unset($value[SYSTEM_SQL_LOG]);
        unset($value[SYSTEM_PATH_EXT]);
        unset($value[SYSTEM_SITE_PATH]);
Carsten  Rose's avatar
Carsten Rose committed
354
355
        unset($value[SYSTEM_MAIL_LOG]);
        unset($value[SYSTEM_SEND_E_MAIL]);
356
357
358
359

        // check default values
        $this->assertEquals($expect, $value, "Retrieve system store.");

360
        //
361
362
363
        $body .= PHP_EOL . SYSTEM_FORM_BS_LABEL_COLUMNS . ' = 4';
        $body .= PHP_EOL . SYSTEM_FORM_BS_INPUT_COLUMNS . ' = 5';
        $body .= PHP_EOL . SYSTEM_FORM_BS_NOTE_COLUMNS . ' = 6';
364
365
366
367
368
369
370
371
        $body .= PHP_EOL . SYSTEM_FORM_DATA_PATTERN_ERROR . ' = pattern error';
        $body .= PHP_EOL . SYSTEM_FORM_DATA_REQUIRED_ERROR . ' = required error';
        $body .= PHP_EOL . SYSTEM_FORM_DATA_MATCH_ERROR . ' = match error';
        $body .= PHP_EOL . SYSTEM_FORM_DATA_ERROR . ' = error';
        $body .= PHP_EOL . SYSTEM_CSS_CLASS_QFQ_FORM . ' = main-class';
        $body .= PHP_EOL . SYSTEM_CSS_CLASS_QFQ_FORM_PILL . ' = pill-class';
        $body .= PHP_EOL . SYSTEM_CSS_CLASS_QFQ_FORM_BODY . ' = body-class';

372
373
374
        $expect[F_BS_LABEL_COLUMNS] = '4';
        $expect[F_BS_INPUT_COLUMNS] = '5';
        $expect[F_BS_NOTE_COLUMNS] = '6';
375
376
377
378
379
380
381
382
        $expect[F_FE_DATA_PATTERN_ERROR] = 'pattern error';
        $expect[F_FE_DATA_REQUIRED_ERROR] = 'required error';
        $expect[F_FE_DATA_MATCH_ERROR] = 'match error';
        $expect[F_FE_DATA_ERROR] = 'error';
        $expect[F_CLASS] = 'main-class';
        $expect[F_CLASS_PILL] = 'pill-class';
        $expect[F_CLASS_BODY] = 'body-class';

383
384
385
386
387
        $fileName = $this->createFile($body);
        $this->store = Store::getInstance('', true, $fileName);
        unlink($fileName);

        $value = $this->store->getStore(STORE_SYSTEM);
388
        // these values are different between runtime and PHPUnit environment: do not check
389
390
391
        unset($value[SYSTEM_SQL_LOG]);
        unset($value[SYSTEM_PATH_EXT]);
        unset($value[SYSTEM_SITE_PATH]);
Carsten  Rose's avatar
Carsten Rose committed
392
393
        unset($value[SYSTEM_MAIL_LOG]);
        unset($value[SYSTEM_SEND_E_MAIL]);
394
395
396
397
398

        // check default values
        $this->assertEquals($expect, $value, "Check explizit defined values.");
    }

399
    /**
400
     * @expectedException \qfq\UserFormException
401
402
     *
     */
403
404
    public function testGetStore() {
        $this->assertEquals(array(), $this->store->getStore('unknownstore'));
405
406
    }

407
}