Passed
Pull Request — 4.0 (#7825)
by Damian
05:02
created

SessionTest::testClearElementThatDoesExist()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 8.9713
1
<?php
2
3
namespace SilverStripe\Control\Tests;
4
5
use SilverStripe\Control\Session;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Control\HTTPRequest;
8
9
/**
10
 * Tests to cover the {@link Session} class
11
 */
12
class SessionTest extends SapphireTest
13
{
14
    /**
15
     * @var Session
16
     */
17
    protected $session = null;
18
19
    protected function setUp()
20
    {
21
        $this->session = new Session([]);
22
        return parent::setUp();
23
    }
24
25
    public function testGetSetBasics()
26
    {
27
        $this->session->set('Test', 'Test');
28
29
        $this->assertEquals($this->session->get('Test'), 'Test');
30
    }
31
32
    public function testClearElement()
33
    {
34
        $this->session->set('Test', 'Test');
35
        $this->session->clear('Test');
36
37
        $this->assertEquals($this->session->get('Test'), '');
38
    }
39
40
    public function testClearAllElements()
41
    {
42
        $this->session->set('Test', 'Test');
43
        $this->session->set('Test-1', 'Test-1');
44
45
        $this->session->clearAll();
46
47
        // should session get return null? The array key should probably be
48
        // unset from the data array
49
        $this->assertEquals($this->session->get('Test'), '');
50
        $this->assertEquals($this->session->get('Test-1'), '');
51
    }
52
53
    public function testGetAllElements()
54
    {
55
        $this->session->clearAll(); // Remove all session that might've been set by the test harness
56
57
        $this->session->set('Test', 'Test');
58
        $this->session->set('Test-2', 'Test-2');
59
60
        $session = $this->session->getAll();
61
        unset($session['HTTP_USER_AGENT']);
62
63
        $this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
64
    }
65
66
    public function testSettingExistingDoesntClear()
67
    {
68
        $s = new Session(array('something' => array('does' => 'exist')));
69
70
        $s->set('something.does', 'exist');
71
        $result = $s->changedData();
72
        unset($result['HTTP_USER_AGENT']);
73
        $this->assertEquals(array(), $result);
74
    }
75
76
    /**
77
     * Check that changedData isn't populated with junk when clearing non-existent entries.
78
     */
79
    public function testClearElementThatDoesntExist()
80
    {
81
        $s = new Session(['something' => ['does' => 'exist']]);
82
        $s->clear('something.doesnt.exist');
83
84
        // Clear without existing data
85
        $data = $s->get('something.doesnt.exist');
86
        $this->assertEquals(array(), $s->changedData());
87
        $this->assertNull($data);
88
89
        // Clear with existing change
90
        $s->set('something-else', 'val');
91
        $s->clear('something-new');
92
        $data = $s->get('something-else');
93
        $this->assertEquals(['something-else' => true], $s->changedData());
94
        $this->assertEquals('val', $data);
95
    }
96
97
    /**
98
     * Check that changedData is populated with clearing data.
99
     */
100
    public function testClearElementThatDoesExist()
101
    {
102
        $s = new Session(['something' => ['does' => 'exist']]);
103
104
        // Ensure keys are properly removed and not simply nullified
105
        $s->clear('something.does');
106
        $this->assertEquals(
107
            ['something' => ['does' => true]],
108
            $s->changedData()
109
        );
110
        $this->assertEquals(
111
            [], // 'does' removed
112
            $s->get('something')
113
        );
114
115
        // Clear at more specific level should also clear other changes
116
        $s->clear('something');
117
        $this->assertEquals(
118
            ['something' => true],
119
            $s->changedData()
120
        );
121
        $this->assertEquals(
122
            null, // Should be removed not just empty array
123
            $s->get('something')
124
        );
125
    }
126
127
    public function testUserAgentLockout()
128
    {
129
        // Set a user agent
130
        $req1 = new HTTPRequest('GET', '/');
131
        $req1->addHeader('User-Agent', 'Test Agent');
132
133
        // Generate our session
134
        $s = new Session(array());
135
        $s->init($req1);
136
        $s->set('val', 123);
137
        $s->finalize($req1);
138
139
        // Change our UA
140
        $req2 = new HTTPRequest('GET', '/');
141
        $req2->addHeader('User-Agent', 'Fake Agent');
142
143
        // Verify the new session reset our values
144
        $s2 = new Session($s);
145
        $s2->init($req2);
146
        $this->assertNotEquals($s2->get('val'), 123);
147
    }
148
149
    public function testSave()
150
    {
151
        $request = new HTTPRequest('GET', '/');
152
153
        // Test change of nested array type
154
        $s = new Session($_SESSION = ['something' => ['some' => 'value', 'another' => 'item']]);
155
        $s->set('something', 'string');
156
        $s->save($request);
157
        $this->assertEquals(
158
            ['something' => 'string'],
159
            $_SESSION
160
        );
161
162
        // Test multiple changes combine safely
163
        $s = new Session($_SESSION = ['something' => ['some' => 'value', 'another' => 'item']]);
164
        $s->set('something.another', 'newanother');
165
        $s->clear('something.some');
166
        $s->set('something.newkey', 'new value');
167
        $s->save($request);
168
        $this->assertEquals(
169
            [
170
                'something' => [
171
                    'another' => 'newanother',
172
                    'newkey' => 'new value',
173
                ]
174
            ],
175
            $_SESSION
176
        );
177
178
        // Test cleared keys are restorable
179
        $s = new Session($_SESSION = ['bookmarks' => [ 1 => 1, 2 => 2]]);
180
        $s->clear('bookmarks');
181
        $s->set('bookmarks', [
182
            1 => 1,
183
            3 => 3,
184
        ]);
185
        $s->save($request);
186
        $this->assertEquals(
187
            [
188
                'bookmarks' => [
189
                    1 => 1,
190
                    3 => 3,
191
                ]
192
            ],
193
            $_SESSION
194
        );
195
    }
196
}
197