/
DatabaseSessionTest.php
182 lines (161 loc) · 3.83 KB
/
DatabaseSessionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* DatabaseSessionTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Model.Datasource.Session
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Model', 'Model');
App::uses('CakeSession', 'Model/Datasource');
App::uses('DatabaseSession', 'Model/Datasource/Session');
class_exists('CakeSession');
class SessionTestModel extends Model {
public $name = 'SessionTestModel';
public $useTable = 'sessions';
}
/**
* Database session test.
*
* @package Cake.Test.Case.Model.Datasource.Session
*/
class DatabaseSessionTest extends CakeTestCase {
protected static $_sessionBackup;
/**
* fixtures
*
* @var string
*/
public $fixtures = array('core.session');
/**
* test case startup
*
* @return void
*/
public static function setupBeforeClass() {
self::$_sessionBackup = Configure::read('Session');
Configure::write('Session.handler', array(
'model' => 'SessionTestModel',
));
Configure::write('Session.timeout', 100);
}
/**
* cleanup after test case.
*
* @return void
*/
public static function teardownAfterClass() {
Configure::write('Session', self::$_sessionBackup);
}
/**
* setup
*
* @return void
*/
public function setup() {
$this->storage = new DatabaseSession();
}
/**
* teardown
*
* @return void
*/
public function teardown() {
unset($this->storage);
ClassRegistry::flush();
}
/**
* test that constructor sets the right things up.
*
* @return void
*/
public function testConstructionSettings() {
ClassRegistry::flush();
$storage = new DatabaseSession();
$session = ClassRegistry::getObject('session');
$this->assertInstanceOf('SessionTestModel', $session);
$this->assertEquals('Session', $session->alias);
$this->assertEquals('test', $session->useDbConfig);
$this->assertEquals('sessions', $session->useTable);
}
/**
* test opening the session
*
* @return void
*/
public function testOpen() {
$this->assertTrue($this->storage->open());
}
/**
* test write()
*
* @return void
*/
public function testWrite() {
$result = $this->storage->write('foo', 'Some value');
$expected = array(
'Session' => array(
'id' => 'foo',
'data' => 'Some value',
'expires' => time() + (Configure::read('Session.timeout') * 60)
)
);
$this->assertEquals($expected, $result);
}
/**
* testReadAndWriteWithDatabaseStorage method
*
* @return void
*/
public function testWriteEmptySessionId() {
$result = $this->storage->write('', 'This is a Test');
$this->assertFalse($result);
}
/**
* test read()
*
* @return void
*/
public function testRead() {
$this->storage->write('foo', 'Some value');
$result = $this->storage->read('foo');
$expected = 'Some value';
$this->assertEquals($expected, $result);
$result = $this->storage->read('made up value');
$this->assertFalse($result);
}
/**
* test blowing up the session.
*
* @return void
*/
public function testDestroy() {
$this->storage->write('foo', 'Some value');
$this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
$this->assertFalse($this->storage->read('foo'), 'Value still present.');
}
/**
* test the garbage collector
*
* @return void
*/
public function testGc() {
ClassRegistry::flush();
Configure::write('Session.timeout', 0);
$storage = new DatabaseSession();
$storage->write('foo', 'Some value');
sleep(1);
$storage->gc();
$this->assertFalse($storage->read('foo'));
}
}