Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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