StoreTest.php 7.04 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" . TYPO3_DEBUG_SAVE . "=6\n" . TYPO3_DEBUG_LOAD . "=7\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

        // T3 Bodytext
59
60
        $this->store->setVar(TYPO3_DEBUG_SAVE, "6", STORE_TYPO3);
        $this->assertEquals('6', $this->store->getVar(TYPO3_DEBUG_SAVE, STORE_TYPO3), "System: " . TYPO3_DEBUG_SAVE);
61
62

        // T3 Bodytext
63
64
        $this->store->setVar(TYPO3_DEBUG_LOAD, "7", STORE_TYPO3);
        $this->assertEquals('7', $this->store->getVar(TYPO3_DEBUG_LOAD, STORE_TYPO3), "System: " . TYPO3_DEBUG_LOAD);
65
66
67

    }

68
    public function testGetVarStoreSystem() {
69
        // DBUSER in qfq.ini
70
        $dbuser = $this->store->getVar(SYSTEM_DB_USER, STORE_SYSTEM);
71
        $this->assertTrue($dbuser !== false && $dbuser !== '', "System: DBUSER found in qfq.ini");
72

73
74
    }

75
    public function testSetVarStoreSystem() {
76
77

        // Sessionname: default value
78
        $this->assertEquals('qfq', $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM), "System: SESSIONNAME");
79
80

        // set new Sessionname
81
        $this->store->setVar(SYSTEM_SESSION_NAME, "anothersessionname", STORE_SYSTEM);
82

83
        $this->assertEquals('anothersessionname', $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM), "System: SESSIONNAME");
84

85
86
    }

87

88
    public function testGetVarStoreClient() {
89

Carsten  Rose's avatar
Carsten Rose committed
90
        # Violates SANITIZE class: sanitized string is always an empty string.
91
        # Access are cached:
92

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

Carsten  Rose's avatar
Carsten Rose committed
96
97
        # violates default SANITIZE digit: sanitized string is always an empty string.
        $this->assertEquals('', $this->store->getVar(CLIENT_SIP, STORE_CLIENT), "sanitize class digit fails");
98
99
100

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

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        // 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
117
118
        // Test overwrite default sanitize class
        $this->assertEquals('', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANITIZE_ALLOW_DIGIT), "Param: overwrite default sanitize class");
119
120
121

        // 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
122
123
    }

124

Carsten  Rose's avatar
Carsten Rose committed
125
    public function testStorePriority() {
126
127
        //default prio  FSRD

128
        $this->store->fillStoreTableDefaultColumnType('Person');
129
130
131
132
133
134
135
136
137
        $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
138
        $this->assertEquals('female2', $this->store->getVar('gender', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'gender' from STORE_SIP");
139

140
    }
141

Carsten  Rose's avatar
Carsten Rose committed
142
    public function testStoreDifferentSanitizeClass() {
143
144
        //default prio  FSRD

145
146
        $this->store->setVar('fruit', 'apple', STORE_RECORD);
        $this->assertEquals('apple', $this->store->getVar('fruit'), "Retrieve 'fruit' from STORE_RECORD");
147

148
149
        $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
150
        $this->assertEquals('green', $this->store->getVar('color', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'color' from STORE_FORM");
151
152
    }

153
    public function testGetVarStore0() {
154
155
        //default prio  FSRD

156
        $this->assertEquals(0, $this->store->getVar('fakename', STORE_ZERO), "Retrieve anything from STORE_ZERO");
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    }

    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.");
    }
181

182
183
184
185
    /**
     * @expectedException \qfq\UserException
     *
     */
186
187
    public function testGetStore() {
        $this->assertEquals(array(), $this->store->getStore('unknownstore'));
188
189
    }

190
}