10
10
11
11
namespace Magento \Framework \Session {
12
12
13
+ use Magento \Framework \App \DeploymentConfig ;
13
14
use Magento \Framework \App \State ;
15
+ use Magento \Framework \Session \Config \ConfigInterface ;
16
+
14
17
// @codingStandardsIgnoreEnd
15
18
16
19
/**
@@ -36,30 +39,72 @@ function headers_sent()
36
39
return call_user_func_array ('\headers_sent ' , func_get_args ());
37
40
}
38
41
42
+ /**
43
+ * Mock ini_set global function
44
+ *
45
+ * @param string $varName
46
+ * @param string $newValue
47
+ * @return bool|string
48
+ */
49
+ function ini_set ($ varName , $ newValue )
50
+ {
51
+ global $ mockPHPFunctions ;
52
+ if ($ mockPHPFunctions ) {
53
+ SessionManagerTest::$ isIniSetInvoked [$ varName ] = $ newValue ;
54
+ return true ;
55
+ }
56
+ return call_user_func_array ('\ini_set ' , func_get_args ());
57
+ }
58
+
59
+ /**
60
+ * Mock session_set_save_handler global function
61
+ *
62
+ * @return bool
63
+ */
64
+ function session_set_save_handler ()
65
+ {
66
+ global $ mockPHPFunctions ;
67
+ if ($ mockPHPFunctions ) {
68
+ SessionManagerTest::$ isSessionSetSaveHandlerInvoked = true ;
69
+ return true ;
70
+ }
71
+ return call_user_func_array ('\session_set_save_handler ' , func_get_args ());
72
+ }
73
+
39
74
/**
40
75
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41
76
*/
42
77
class SessionManagerTest extends \PHPUnit \Framework \TestCase
43
78
{
79
+ /**
80
+ * @var string[]
81
+ */
82
+ public static $ isIniSetInvoked = [];
83
+
84
+ /**
85
+ * @var bool
86
+ */
87
+ public static $ isSessionSetSaveHandlerInvoked ;
88
+
44
89
/**
45
90
* @var \Magento\Framework\Session\SessionManagerInterface
46
91
*/
47
- protected $ _model ;
92
+ private $ model ;
48
93
49
94
/**
50
95
* @var \Magento\Framework\Session\SidResolverInterface
51
96
*/
52
- protected $ _sidResolver ;
97
+ private $ sidResolver ;
53
98
54
99
/**
55
100
* @var string
56
101
*/
57
- protected $ sessionName ;
102
+ private $ sessionName ;
58
103
59
104
/**
60
- * @var \Magento\Framework\ObjectManagerInterface
105
+ * @var \Magento\TestFramework\ObjectManager
61
106
*/
62
- protected $ objectManager ;
107
+ private $ objectManager ;
63
108
64
109
/**
65
110
* @var \Magento\Framework\App\RequestInterface
@@ -79,6 +124,21 @@ protected function setUp()
79
124
ini_set ('session.name ' , $ this ->sessionName );
80
125
81
126
$ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
127
+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
128
+ $ deploymentConfigMock ->method ('get ' )
129
+ ->willReturnCallback (function ($ configPath ) {
130
+ switch ($ configPath ) {
131
+ case Config::PARAM_SESSION_SAVE_METHOD :
132
+ return 'db ' ;
133
+ case Config::PARAM_SESSION_CACHE_LIMITER :
134
+ return 'private_no_expire ' ;
135
+ case Config::PARAM_SESSION_SAVE_PATH :
136
+ return 'explicit_save_path ' ;
137
+ default :
138
+ return null ;
139
+ }
140
+ });
141
+ $ this ->objectManager ->addSharedInstance ($ deploymentConfigMock , DeploymentConfig::class);
82
142
83
143
/** @var \Magento\Framework\Session\SidResolverInterface $sidResolver */
84
144
$ this ->appState = $ this ->getMockBuilder (State::class)
@@ -87,138 +147,142 @@ protected function setUp()
87
147
->getMock ();
88
148
89
149
/** @var \Magento\Framework\Session\SidResolver $sidResolver */
90
- $ this ->_sidResolver = $ this ->objectManager ->create (
150
+ $ this ->sidResolver = $ this ->objectManager ->create (
91
151
\Magento \Framework \Session \SidResolver::class,
92
152
[
93
153
'appState ' => $ this ->appState ,
94
154
]
95
155
);
96
156
97
157
$ this ->request = $ this ->objectManager ->get (\Magento \Framework \App \RequestInterface::class);
98
-
99
- /** @var \Magento\Framework\Session\SessionManager _model */
100
- $ this ->_model = $ this ->objectManager ->create (
101
- \Magento \Framework \Session \SessionManager::class,
102
- [
103
- $ this ->objectManager ->get (\Magento \Framework \App \Request \Http::class),
104
- $ this ->_sidResolver ,
105
- $ this ->objectManager ->get (\Magento \Framework \Session \Config \ConfigInterface::class),
106
- $ this ->objectManager ->get (\Magento \Framework \Session \SaveHandlerInterface::class),
107
- $ this ->objectManager ->get (\Magento \Framework \Session \ValidatorInterface::class),
108
- $ this ->objectManager ->get (\Magento \Framework \Session \StorageInterface::class)
109
- ]
110
- );
111
158
}
112
159
113
160
protected function tearDown ()
114
161
{
115
162
global $ mockPHPFunctions ;
116
163
$ mockPHPFunctions = false ;
117
- $ this ->_model ->destroy ();
164
+ self ::$ isIniSetInvoked = [];
165
+ self ::$ isSessionSetSaveHandlerInvoked = false ;
166
+ if ($ this ->model !== null ) {
167
+ $ this ->model ->destroy ();
168
+ $ this ->model = null ;
169
+ }
170
+ $ this ->objectManager ->removeSharedInstance (DeploymentConfig::class);
118
171
}
119
172
120
173
public function testSessionNameFromIni ()
121
174
{
122
- $ this ->_model ->start ();
123
- $ this ->assertSame ($ this ->sessionName , $ this ->_model ->getName ());
124
- $ this ->_model ->destroy ();
175
+ $ this ->initializeModel ();
176
+ $ this ->model ->start ();
177
+ $ this ->assertSame ($ this ->sessionName , $ this ->model ->getName ());
178
+ $ this ->model ->destroy ();
125
179
}
126
180
127
181
public function testSessionUseOnlyCookies ()
128
182
{
183
+ $ this ->initializeModel ();
129
184
$ expectedValue = '1 ' ;
130
185
$ sessionUseOnlyCookies = ini_get ('session.use_only_cookies ' );
131
186
$ this ->assertSame ($ expectedValue , $ sessionUseOnlyCookies );
132
187
}
133
188
134
189
public function testGetData ()
135
190
{
136
- $ this ->_model ->setData (['test_key ' => 'test_value ' ]);
137
- $ this ->assertEquals ('test_value ' , $ this ->_model ->getData ('test_key ' , true ));
138
- $ this ->assertNull ($ this ->_model ->getData ('test_key ' ));
191
+ $ this ->initializeModel ();
192
+ $ this ->model ->setData (['test_key ' => 'test_value ' ]);
193
+ $ this ->assertEquals ('test_value ' , $ this ->model ->getData ('test_key ' , true ));
194
+ $ this ->assertNull ($ this ->model ->getData ('test_key ' ));
139
195
}
140
196
141
197
public function testGetSessionId ()
142
198
{
143
- $ this ->assertEquals (session_id (), $ this ->_model ->getSessionId ());
199
+ $ this ->initializeModel ();
200
+ $ this ->assertEquals (session_id (), $ this ->model ->getSessionId ());
144
201
}
145
202
146
203
public function testGetName ()
147
204
{
148
- $ this ->assertEquals (session_name (), $ this ->_model ->getName ());
205
+ $ this ->initializeModel ();
206
+ $ this ->assertEquals (session_name (), $ this ->model ->getName ());
149
207
}
150
208
151
209
public function testSetName ()
152
210
{
153
- $ this ->_model ->destroy ();
154
- $ this ->_model ->setName ('test ' );
155
- $ this ->_model ->start ();
156
- $ this ->assertEquals ('test ' , $ this ->_model ->getName ());
211
+ $ this ->initializeModel ();
212
+ $ this ->model ->destroy ();
213
+ $ this ->model ->setName ('test ' );
214
+ $ this ->model ->start ();
215
+ $ this ->assertEquals ('test ' , $ this ->model ->getName ());
157
216
}
158
217
159
218
public function testDestroy ()
160
219
{
220
+ $ this ->initializeModel ();
161
221
$ data = ['key ' => 'value ' ];
162
- $ this ->_model ->setData ($ data );
222
+ $ this ->model ->setData ($ data );
163
223
164
- $ this ->assertEquals ($ data , $ this ->_model ->getData ());
165
- $ this ->_model ->destroy ();
224
+ $ this ->assertEquals ($ data , $ this ->model ->getData ());
225
+ $ this ->model ->destroy ();
166
226
167
- $ this ->assertEquals ([], $ this ->_model ->getData ());
227
+ $ this ->assertEquals ([], $ this ->model ->getData ());
168
228
}
169
229
170
230
public function testSetSessionId ()
171
231
{
172
- $ sessionId = $ this ->_model ->getSessionId ();
232
+ $ this ->initializeModel ();
233
+ $ sessionId = $ this ->model ->getSessionId ();
173
234
$ this ->appState ->expects ($ this ->atLeastOnce ())
174
235
->method ('getAreaCode ' )
175
236
->willReturn (\Magento \Framework \App \Area::AREA_FRONTEND );
176
- $ this ->_model ->setSessionId ($ this ->_sidResolver ->getSid ($ this ->_model ));
177
- $ this ->assertEquals ($ sessionId , $ this ->_model ->getSessionId ());
237
+ $ this ->model ->setSessionId ($ this ->sidResolver ->getSid ($ this ->model ));
238
+ $ this ->assertEquals ($ sessionId , $ this ->model ->getSessionId ());
178
239
179
- $ this ->_model ->setSessionId ('test ' );
180
- $ this ->assertEquals ('test ' , $ this ->_model ->getSessionId ());
240
+ $ this ->model ->setSessionId ('test ' );
241
+ $ this ->assertEquals ('test ' , $ this ->model ->getSessionId ());
181
242
}
182
243
183
244
/**
184
245
* @magentoConfigFixture current_store web/session/use_frontend_sid 1
185
246
*/
186
247
public function testSetSessionIdFromParam ()
187
248
{
249
+ $ this ->initializeModel ();
188
250
$ this ->appState ->expects ($ this ->atLeastOnce ())
189
251
->method ('getAreaCode ' )
190
252
->willReturn (\Magento \Framework \App \Area::AREA_FRONTEND );
191
- $ this ->assertNotEquals ('test_id ' , $ this ->_model ->getSessionId ());
192
- $ this ->request ->getQuery ()->set ($ this ->_sidResolver ->getSessionIdQueryParam ($ this ->_model ), 'test-id ' );
193
- $ this ->_model ->setSessionId ($ this ->_sidResolver ->getSid ($ this ->_model ));
194
- $ this ->assertEquals ('test-id ' , $ this ->_model ->getSessionId ());
253
+ $ this ->assertNotEquals ('test_id ' , $ this ->model ->getSessionId ());
254
+ $ this ->request ->getQuery ()->set ($ this ->sidResolver ->getSessionIdQueryParam ($ this ->model ), 'test-id ' );
255
+ $ this ->model ->setSessionId ($ this ->sidResolver ->getSid ($ this ->model ));
256
+ $ this ->assertEquals ('test-id ' , $ this ->model ->getSessionId ());
195
257
/* Use not valid identifier */
196
- $ this ->request ->getQuery ()->set ($ this ->_sidResolver ->getSessionIdQueryParam ($ this ->_model ), 'test_id ' );
197
- $ this ->_model ->setSessionId ($ this ->_sidResolver ->getSid ($ this ->_model ));
198
- $ this ->assertEquals ('test-id ' , $ this ->_model ->getSessionId ());
258
+ $ this ->request ->getQuery ()->set ($ this ->sidResolver ->getSessionIdQueryParam ($ this ->model ), 'test_id ' );
259
+ $ this ->model ->setSessionId ($ this ->sidResolver ->getSid ($ this ->model ));
260
+ $ this ->assertEquals ('test-id ' , $ this ->model ->getSessionId ());
199
261
}
200
262
201
263
public function testGetSessionIdForHost ()
202
264
{
265
+ $ this ->initializeModel ();
203
266
$ _SERVER ['HTTP_HOST ' ] = 'localhost ' ;
204
- $ this ->_model ->start ();
205
- $ this ->assertEmpty ($ this ->_model ->getSessionIdForHost ('localhost ' ));
206
- $ this ->assertNotEmpty ($ this ->_model ->getSessionIdForHost ('test ' ));
207
- $ this ->_model ->destroy ();
267
+ $ this ->model ->start ();
268
+ $ this ->assertEmpty ($ this ->model ->getSessionIdForHost ('localhost ' ));
269
+ $ this ->assertNotEmpty ($ this ->model ->getSessionIdForHost ('test ' ));
270
+ $ this ->model ->destroy ();
208
271
}
209
272
210
273
public function testIsValidForHost ()
211
274
{
275
+ $ this ->initializeModel ();
212
276
$ _SERVER ['HTTP_HOST ' ] = 'localhost ' ;
213
- $ this ->_model ->start ();
277
+ $ this ->model ->start ();
214
278
215
- $ reflection = new \ReflectionMethod ($ this ->_model , '_addHost ' );
279
+ $ reflection = new \ReflectionMethod ($ this ->model , '_addHost ' );
216
280
$ reflection ->setAccessible (true );
217
- $ reflection ->invoke ($ this ->_model );
281
+ $ reflection ->invoke ($ this ->model );
218
282
219
- $ this ->assertFalse ($ this ->_model ->isValidForHost ('test.com ' ));
220
- $ this ->assertTrue ($ this ->_model ->isValidForHost ('localhost ' ));
221
- $ this ->_model ->destroy ();
283
+ $ this ->assertFalse ($ this ->model ->isValidForHost ('test.com ' ));
284
+ $ this ->assertTrue ($ this ->model ->isValidForHost ('localhost ' ));
285
+ $ this ->model ->destroy ();
222
286
}
223
287
224
288
/**
@@ -236,9 +300,9 @@ public function testStartAreaNotSet()
236
300
*
237
301
* @var \Magento\Framework\Session\SessionManager _model
238
302
*/
239
- $ this ->_model = new \Magento \Framework \Session \SessionManager (
303
+ $ this ->model = new \Magento \Framework \Session \SessionManager (
240
304
$ this ->objectManager ->get (\Magento \Framework \App \Request \Http::class),
241
- $ this ->_sidResolver ,
305
+ $ this ->sidResolver ,
242
306
$ this ->objectManager ->get (\Magento \Framework \Session \Config \ConfigInterface::class),
243
307
$ this ->objectManager ->get (\Magento \Framework \Session \SaveHandlerInterface::class),
244
308
$ this ->objectManager ->get (\Magento \Framework \Session \ValidatorInterface::class),
@@ -250,7 +314,44 @@ public function testStartAreaNotSet()
250
314
251
315
global $ mockPHPFunctions ;
252
316
$ mockPHPFunctions = true ;
253
- $ this ->_model ->start ();
317
+ $ this ->model ->start ();
318
+ }
319
+
320
+ public function testConstructor ()
321
+ {
322
+ global $ mockPHPFunctions ;
323
+ $ mockPHPFunctions = true ;
324
+ $ this ->model = $ this ->objectManager ->create (
325
+ \Magento \Framework \Session \SessionManager::class,
326
+ [
327
+ 'sidResolver ' => $ this ->sidResolver
328
+ ]
329
+ );
330
+ $ sessionConfig = $ this ->objectManager ->get (ConfigInterface::class);
331
+ $ this ->assertEquals ('db ' , $ sessionConfig ->getOption ('session.save_handler ' ));
332
+ $ this ->assertEquals ('private_no_expire ' , $ sessionConfig ->getOption ('session.cache_limiter ' ));
333
+ $ this ->assertEquals ('explicit_save_path ' , $ sessionConfig ->getOption ('session.save_path ' ));
334
+ $ this ->assertArrayHasKey ('session.use_only_cookies ' , self ::$ isIniSetInvoked );
335
+ $ this ->assertEquals ('1 ' , self ::$ isIniSetInvoked ['session.use_only_cookies ' ]);
336
+ foreach ($ sessionConfig ->getOptions () as $ option => $ value ) {
337
+ if ($ option =='session.save_handler ' ) {
338
+ $ this ->assertArrayNotHasKey ('session.save_handler ' , self ::$ isIniSetInvoked );
339
+ } else {
340
+ $ this ->assertArrayHasKey ($ option , self ::$ isIniSetInvoked );
341
+ $ this ->assertEquals ($ value , self ::$ isIniSetInvoked [$ option ]);
342
+ }
343
+ }
344
+ $ this ->assertTrue (self ::$ isSessionSetSaveHandlerInvoked );
345
+ }
346
+
347
+ private function initializeModel (): void
348
+ {
349
+ $ this ->model = $ this ->objectManager ->create (
350
+ \Magento \Framework \Session \SessionManager::class,
351
+ [
352
+ 'sidResolver ' => $ this ->sidResolver
353
+ ]
354
+ );
254
355
}
255
356
}
256
357
}
0 commit comments