StoreTest.php 10.7 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
use qfq\Store;
13

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

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


class StoreTest extends \PHPUnit_Framework_TestCase {

23
24
25
    /**
     * @var Store
     */
26
    private $store = null;
27
    private $setUpDone = false;
28
29
30
31

    public function setUp() {
        // Client Variables has to setup before the first instantiation of 'Store' 
        $_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_USER, 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
113
        // Test overwrite default sanitize class
        $this->assertEquals('', $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
122
        //default prio  FSRD

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

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

        $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
146
        $this->store->setVar('color', 'green', STORE_FORM);
        $this->assertEquals(false, $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
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
    /**
     * @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.");
    }

    /**
     * @param $body
     * @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
                DB_USER = <DBUSER>
                DB_SERVER = <DBSERVER>
                DB_PASSWORD = <DBPW>
                DB_NAME = <DB>

                DB_NAME_TEST = <TESTDB>

                DB_INIT = set names utf8

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

        $expect = [
            SYSTEM_DB_USER => '<DBUSER>',
            SYSTEM_DB_SERVER => '<DBSERVER>',
            SYSTEM_DB_PASSWORD => '<DBPW>',
            SYSTEM_DB_NAME => '<DB>',
            SYSTEM_DB_NAME_TEST => '<TESTDB>',
            SYSTEM_DB_INIT => 'set names utf8',
            SYSTEM_SQL_LOG_MODE => 'modify',
            SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
233
            SYSTEM_SHOW_DEBUG_INFO => 'auto',
234
235
236
            F_BS_LABEL_COLUMNS => '3',
            F_BS_INPUT_COLUMNS => '6',
            F_BS_NOTE_COLUMNS => '3',
237
238
            F_CLASS_PILL => 'qfq-color-grey-1',
            F_CLASS_BODY => 'qfq-color-grey-2',
239
            F_BUTTON_ON_CHANGE_CLASS => 'btn-info alert-info',
240
241
242
243
244
245
246
        ];

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

        $value = $this->store->getStore(STORE_SYSTEM);
247
        // these values are different between runtime and PHPUnit environment: do not check
248
249
250
251
252
253
254
255
        unset($value[SYSTEM_SQL_LOG]);
        unset($value[SYSTEM_PATH_EXT]);
        unset($value[SYSTEM_SITE_PATH]);

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


256
        //
257
258
259
        $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';
260
261
262
263
264
265
266
267
        $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';

268
269
270
        $expect[F_BS_LABEL_COLUMNS] = '4';
        $expect[F_BS_INPUT_COLUMNS] = '5';
        $expect[F_BS_NOTE_COLUMNS] = '6';
271
272
273
274
275
276
277
278
        $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';

279
280
281
282
283
        $fileName = $this->createFile($body);
        $this->store = Store::getInstance('', true, $fileName);
        unlink($fileName);

        $value = $this->store->getStore(STORE_SYSTEM);
284
        // these values are different between runtime and PHPUnit environment: do not check
285
286
287
288
289
290
291
292
        unset($value[SYSTEM_SQL_LOG]);
        unset($value[SYSTEM_PATH_EXT]);
        unset($value[SYSTEM_SITE_PATH]);

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

293
    /**
294
     * @expectedException \qfq\UserFormException
295
296
     *
     */
297
298
    public function testGetStore() {
        $this->assertEquals(array(), $this->store->getStore('unknownstore'));
299
300
    }

301

302
}