|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Tests to cover the {@link Session} class |
|
5
|
|
|
* |
|
6
|
|
|
* @package framework |
|
7
|
|
|
* @subpackage tests |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
class SessionTest extends SapphireTest { |
|
11
|
|
|
|
|
12
|
|
|
public function testGetSetBasics() { |
|
13
|
|
|
Session::set('Test', 'Test'); |
|
14
|
|
|
|
|
15
|
|
|
$this->assertEquals(Session::get('Test'), 'Test'); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function testClearElement() { |
|
19
|
|
|
Session::set('Test', 'Test'); |
|
20
|
|
|
Session::clear('Test'); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertEquals(Session::get('Test'), ''); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testClearAllElements() { |
|
26
|
|
|
Session::set('Test', 'Test'); |
|
27
|
|
|
Session::set('Test-1', 'Test-1'); |
|
28
|
|
|
|
|
29
|
|
|
Session::clear_all(); |
|
30
|
|
|
|
|
31
|
|
|
// should session get return null? The array key should probably be |
|
32
|
|
|
// unset from the data array |
|
33
|
|
|
$this->assertEquals(Session::get('Test'), ''); |
|
34
|
|
|
$this->assertEquals(Session::get('Test-1'), ''); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGetAllElements() { |
|
38
|
|
|
Session::clear_all(); // Remove all session that might've been set by the test harness |
|
39
|
|
|
|
|
40
|
|
|
Session::set('Test', 'Test'); |
|
41
|
|
|
Session::set('Test-2', 'Test-2'); |
|
42
|
|
|
|
|
43
|
|
|
$session = Session::get_all(); |
|
44
|
|
|
unset($session['HTTP_USER_AGENT']); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2')); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testSettingExistingDoesntClear() { |
|
50
|
|
|
$s = Injector::inst()->create('Session', array('something' => array('does' => 'exist'))); |
|
51
|
|
|
|
|
52
|
|
|
$s->inst_set('something.does', 'exist'); |
|
53
|
|
|
$result = $s->inst_changedData(); |
|
54
|
|
|
unset($result['HTTP_USER_AGENT']); |
|
55
|
|
|
$this->assertEquals(array(), $result); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check that changedData isn't populated with junk when clearing non-existent entries. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testClearElementThatDoesntExist() { |
|
62
|
|
|
$s = Injector::inst()->create('Session', array('something' => array('does' => 'exist'))); |
|
63
|
|
|
|
|
64
|
|
|
$s->inst_clear('something.doesnt.exist'); |
|
65
|
|
|
$result = $s->inst_changedData(); |
|
66
|
|
|
unset($result['HTTP_USER_AGENT']); |
|
67
|
|
|
$this->assertEquals(array(), $result); |
|
68
|
|
|
|
|
69
|
|
|
$s->inst_set('something-else', 'val'); |
|
70
|
|
|
$s->inst_clear('something-new'); |
|
71
|
|
|
$result = $s->inst_changedData(); |
|
72
|
|
|
unset($result['HTTP_USER_AGENT']); |
|
73
|
|
|
$this->assertEquals(array('something-else' => 'val'), $result); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check that changedData is populated with clearing data. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testClearElementThatDoesExist() { |
|
80
|
|
|
$s = Injector::inst()->create('Session', array('something' => array('does' => 'exist'))); |
|
81
|
|
|
|
|
82
|
|
|
$s->inst_clear('something.does'); |
|
83
|
|
|
$result = $s->inst_changedData(); |
|
84
|
|
|
unset($result['HTTP_USER_AGENT']); |
|
85
|
|
|
$this->assertEquals(array('something' => array('does' => null)), $result); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testNonStandardPath(){ |
|
|
|
|
|
|
89
|
|
|
Config::inst()->update('Session', 'store_path', (realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'))); |
|
90
|
|
|
Session::start(); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals(Config::inst()->get('Session', 'store_path'), ''); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testUserAgentLockout() { |
|
|
|
|
|
|
96
|
|
|
// Set a user agent |
|
97
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Test Agent'; |
|
98
|
|
|
|
|
99
|
|
|
// Generate our session |
|
100
|
|
|
/** @var Session $s */ |
|
101
|
|
|
$s = Injector::inst()->create('Session', array()); |
|
102
|
|
|
$s->inst_set('val', 123); |
|
103
|
|
|
$s->inst_finalize(); |
|
104
|
|
|
$data = $s->inst_getAll(); |
|
105
|
|
|
|
|
106
|
|
|
// Change our UA |
|
107
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Fake Agent'; |
|
108
|
|
|
|
|
109
|
|
|
// Verify the new session reset our values (passed by constructor) |
|
110
|
|
|
/** @var Session $s2 */ |
|
111
|
|
|
$s2 = Injector::inst()->create('Session', $data); |
|
112
|
|
|
$this->assertNotEquals($s2->inst_get('val'), 123); |
|
113
|
|
|
|
|
114
|
|
|
// Verify a started session resets our values (initiated by $_SESSION object) |
|
115
|
|
|
/** @var Session $s3 */ |
|
116
|
|
|
$s3 = Injector::inst()->create('Session', []); |
|
117
|
|
|
foreach ($data as $key => $value) { |
|
118
|
|
|
$s3->inst_set($key, $value); |
|
119
|
|
|
} |
|
120
|
|
|
$s3->inst_start(); |
|
121
|
|
|
$this->assertNotEquals($s3->inst_get('val'), 123); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: