統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Controller / Component / SessionComponentTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (7.144 KB)

1 635eef61 spyder1211
<?php
2
/**
3
 * SessionComponentTest 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Controller.Component
15
 * @since         CakePHP(tm) v 1.2.0.5436
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
19
App::uses('Controller', 'Controller');
20
App::uses('SessionComponent', 'Controller/Component');
21
22
/**
23
 * SessionTestController class
24
 *
25
 * @package       Cake.Test.Case.Controller.Component
26
 */
27
class SessionTestController extends Controller {
28
29
/**
30
 * uses property
31
 *
32
 * @var array
33
 */
34
        public $uses = array();
35
36
/**
37
 * sessionId method
38
 *
39
 * @return string
40
 */
41
        public function sessionId() {
42
                return $this->Session->id();
43
        }
44
45
}
46
47
/**
48
 * OrangeSessionTestController class
49
 *
50
 * @package       Cake.Test.Case.Controller.Component
51
 */
52
class OrangeSessionTestController extends Controller {
53
54
/**
55
 * uses property
56
 *
57
 * @var array
58
 */
59
        public $uses = array();
60
61
/**
62
 * sessionId method
63
 *
64
 * @return string
65
 */
66
        public function sessionId() {
67
                return $this->Session->id();
68
        }
69
70
}
71
72
/**
73
 * SessionComponentTest class
74
 *
75
 * @package       Cake.Test.Case.Controller.Component
76
 */
77
class SessionComponentTest extends CakeTestCase {
78
79
        protected static $_sessionBackup;
80
81
/**
82
 * fixtures
83
 *
84
 * @var string
85
 */
86
        public $fixtures = array('core.session');
87
88
/**
89
 * test case startup
90
 *
91
 * @return void
92
 */
93
        public static function setupBeforeClass() {
94
                static::$_sessionBackup = Configure::read('Session');
95
                Configure::write('Session', array(
96
                        'defaults' => 'php',
97
                        'timeout' => 100,
98
                        'cookie' => 'test'
99
                ));
100
        }
101
102
/**
103
 * cleanup after test case.
104
 *
105
 * @return void
106
 */
107
        public static function teardownAfterClass() {
108
                Configure::write('Session', static::$_sessionBackup);
109
        }
110
111
/**
112
 * setUp method
113
 *
114
 * @return void
115
 */
116
        public function setUp() {
117
                parent::setUp();
118
                $_SESSION = null;
119
                $this->ComponentCollection = new ComponentCollection();
120
        }
121
122
/**
123
 * tearDown method
124
 *
125
 * @return void
126
 */
127
        public function tearDown() {
128
                parent::tearDown();
129
                CakeSession::destroy();
130
        }
131
132
/**
133
 * ensure that session ids don't change when request action is called.
134
 *
135
 * @return void
136
 */
137
        public function testSessionIdConsistentAcrossRequestAction() {
138
                $Object = new Object();
139
                $Session = new SessionComponent($this->ComponentCollection);
140
                $expected = $Session->id();
141
142
                $result = $Object->requestAction('/session_test/sessionId');
143
                $this->assertEquals($expected, $result);
144
145
                $result = $Object->requestAction('/orange_session_test/sessionId');
146
                $this->assertEquals($expected, $result);
147
        }
148
149
/**
150
 * testSessionValid method
151
 *
152
 * @return void
153
 */
154
        public function testSessionValid() {
155
                $Session = new SessionComponent($this->ComponentCollection);
156
157
                $this->assertTrue($Session->valid());
158
159
                Configure::write('Session.checkAgent', true);
160
                $Session->userAgent('rweerw');
161
                $this->assertFalse($Session->valid());
162
163
                $Session = new SessionComponent($this->ComponentCollection);
164
                $Session->time = $Session->read('Config.time') + 1;
165
                $this->assertFalse($Session->valid());
166
        }
167
168
/**
169
 * testSessionError method
170
 *
171
 * @return void
172
 */
173
        public function testSessionError() {
174
                CakeSession::$lastError = null;
175
                $Session = new SessionComponent($this->ComponentCollection);
176
                $this->assertFalse($Session->error());
177
        }
178
179
/**
180
 * testSessionReadWrite method
181
 *
182
 * @return void
183
 */
184
        public function testSessionReadWrite() {
185
                $Session = new SessionComponent($this->ComponentCollection);
186
187
                $this->assertNull($Session->read('Test'));
188
189
                $this->assertTrue($Session->write('Test', 'some value'));
190
                $this->assertEquals('some value', $Session->read('Test'));
191
                $Session->delete('Test');
192
193
                $this->assertTrue($Session->write('Test.key.path', 'some value'));
194
                $this->assertEquals('some value', $Session->read('Test.key.path'));
195
                $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
196
                $this->assertTrue($Session->write('Test.key.path2', 'another value'));
197
                $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
198
                $Session->delete('Test');
199
200
                $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
201
                $this->assertTrue($Session->write('Test', $array));
202
                $this->assertEquals($Session->read('Test'), $array);
203
                $Session->delete('Test');
204
205
                $this->assertTrue($Session->write(array('Test'), 'some value'));
206
                $this->assertTrue($Session->write(array('Test' => 'some value')));
207
                $this->assertEquals('some value', $Session->read('Test'));
208
                $Session->delete('Test');
209
        }
210
211
/**
212
 * testSessionDelete method
213
 *
214
 * @return void
215
 */
216
        public function testSessionDelete() {
217
                $Session = new SessionComponent($this->ComponentCollection);
218
219
                $this->assertFalse($Session->delete('Test'));
220
221
                $Session->write('Test', 'some value');
222
                $this->assertTrue($Session->delete('Test'));
223
        }
224
225
/**
226
 * testSessionCheck method
227
 *
228
 * @return void
229
 */
230
        public function testSessionCheck() {
231
                $Session = new SessionComponent($this->ComponentCollection);
232
233
                $this->assertFalse($Session->check('Test'));
234
235
                $Session->write('Test', 'some value');
236
                $this->assertTrue($Session->check('Test'));
237
                $Session->delete('Test');
238
        }
239
240
/**
241
 * testSessionFlash method
242
 *
243
 * @return void
244
 */
245
        public function testSessionFlash() {
246
                $Session = new SessionComponent($this->ComponentCollection);
247
248
                $this->assertNull($Session->read('Message.flash'));
249
250
                $Session->setFlash('This is a test message');
251
                $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
252
253
                $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
254
                $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
255
256
                $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
257
                $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
258
259
                $Session->setFlash('This is a test message', 'non_existing_layout');
260
                $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
261
262
                $Session->delete('Message');
263
        }
264
265
/**
266
 * testSessionId method
267
 *
268
 * @return void
269
 */
270
        public function testSessionId() {
271
                unset($_SESSION);
272
                $Session = new SessionComponent($this->ComponentCollection);
273
                CakeSession::start();
274
                $this->assertEquals(session_id(), $Session->id());
275
        }
276
277
/**
278
 * testSessionDestroy method
279
 *
280
 * @return void
281
 */
282
        public function testSessionDestroy() {
283
                $Session = new SessionComponent($this->ComponentCollection);
284
285
                $Session->write('Test', 'some value');
286
                $this->assertEquals('some value', $Session->read('Test'));
287
                $Session->destroy('Test');
288
                $this->assertNull($Session->read('Test'));
289
        }
290
291
}