StoreTest.php 6.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    }

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

        $this->store->setVarArray(array(), STORE_RECORD);
        $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'];
        $this->store->setVarArray($arr, STORE_RECORD);
        $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();
        $this->store->setVarArray($arr, STORE_RECORD);
        $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
    }
178

179
    /**
180
     * @expectedException \qfq\UserFormException
181
182
     *
     */
183
184
    public function testGetStore() {
        $this->assertEquals(array(), $this->store->getStore('unknownstore'));
185
186
    }

187
}