12
12
*/
13
13
class ConfigTest extends \PHPUnit \Framework \TestCase
14
14
{
15
- /** @var \Magento\Framework\Session\Config */
16
- private $ _model ;
17
-
18
15
/** @var string */
19
16
private $ _cacheLimiter = 'private_no_expire ' ;
20
17
@@ -45,10 +42,6 @@ protected function setUp()
45
42
}
46
43
});
47
44
48
- $ this ->_model = $ this ->_objectManager ->create (
49
- \Magento \Framework \Session \Config::class,
50
- ['deploymentConfig ' => $ this ->deploymentConfigMock ]
51
- );
52
45
$ this ->defaultSavePath = $ this ->_objectManager
53
46
->get (\Magento \Framework \Filesystem \DirectoryList::class)
54
47
->getPath (DirectoryList::SESSION );
@@ -59,6 +52,7 @@ protected function setUp()
59
52
*/
60
53
public function testDefaultConfiguration ()
61
54
{
55
+ $ model = $ this ->getModel ();
62
56
/** @var \Magento\Framework\Filesystem $filesystem */
63
57
$ filesystem = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
64
58
\Magento \Framework \Filesystem::class
@@ -68,35 +62,37 @@ public function testDefaultConfiguration()
68
62
69
63
$ this ->assertEquals (
70
64
$ path ,
71
- $ this -> _model ->getSavePath ()
65
+ $ model ->getSavePath ()
72
66
);
73
67
$ this ->assertEquals (
74
68
\Magento \Framework \Session \Config::COOKIE_LIFETIME_DEFAULT ,
75
- $ this -> _model ->getCookieLifetime ()
69
+ $ model ->getCookieLifetime ()
76
70
);
77
- $ this ->assertEquals ($ this ->_cacheLimiter , $ this -> _model ->getCacheLimiter ());
78
- $ this ->assertEquals ('/ ' , $ this -> _model ->getCookiePath ());
79
- $ this ->assertEquals ('localhost ' , $ this -> _model ->getCookieDomain ());
80
- $ this ->assertEquals (false , $ this -> _model ->getCookieSecure ());
81
- $ this ->assertEquals (true , $ this -> _model ->getCookieHttpOnly ());
82
- $ this ->assertEquals ($ this -> _model -> getSavePath (), $ this -> _model ->getOption ('save_path ' ));
71
+ $ this ->assertEquals ($ this ->_cacheLimiter , $ model ->getCacheLimiter ());
72
+ $ this ->assertEquals ('/ ' , $ model ->getCookiePath ());
73
+ $ this ->assertEquals ('localhost ' , $ model ->getCookieDomain ());
74
+ $ this ->assertEquals (false , $ model ->getCookieSecure ());
75
+ $ this ->assertEquals (true , $ model ->getCookieHttpOnly ());
76
+ $ this ->assertEquals ($ model -> getSavePath (), $ model ->getOption ('save_path ' ));
83
77
}
84
78
85
79
public function testSetOptionsInvalidValue ()
86
80
{
87
- $ preValue = $ this ->_model ->getOptions ();
88
- $ this ->_model ->setOptions ('' );
89
- $ this ->assertEquals ($ preValue , $ this ->_model ->getOptions ());
81
+ $ model = $ this ->getModel ();
82
+ $ preValue = $ model ->getOptions ();
83
+ $ model ->setOptions ('' );
84
+ $ this ->assertEquals ($ preValue , $ model ->getOptions ());
90
85
}
91
86
92
87
/**
93
88
* @dataProvider optionsProvider
94
89
*/
95
90
public function testSetOptions ($ option , $ getter , $ value )
96
91
{
92
+ $ model = $ this ->getModel ();
97
93
$ options = [$ option => $ value ];
98
- $ this -> _model ->setOptions ($ options );
99
- $ this ->assertSame ($ value , $ this -> _model ->{$ getter }());
94
+ $ model ->setOptions ($ options );
95
+ $ this ->assertSame ($ value , $ model ->{$ getter }());
100
96
}
101
97
102
98
public function optionsProvider ()
@@ -129,158 +125,193 @@ public function optionsProvider()
129
125
130
126
public function testNameIsMutable ()
131
127
{
132
- $ this ->_model ->setName ('FOOBAR ' );
133
- $ this ->assertEquals ('FOOBAR ' , $ this ->_model ->getName ());
128
+ $ model = $ this ->getModel ();
129
+ $ model ->setName ('FOOBAR ' );
130
+ $ this ->assertEquals ('FOOBAR ' , $ model ->getName ());
134
131
}
135
132
136
133
public function testCookieLifetimeIsMutable ()
137
134
{
138
- $ this ->_model ->setCookieLifetime (20 );
139
- $ this ->assertEquals (20 , $ this ->_model ->getCookieLifetime ());
135
+ $ model = $ this ->getModel ();
136
+ $ model ->setCookieLifetime (20 );
137
+ $ this ->assertEquals (20 , $ model ->getCookieLifetime ());
140
138
}
141
139
142
140
public function testCookieLifetimeCanBeZero ()
143
141
{
144
- $ this ->_model ->setCookieLifetime (0 );
145
- $ this ->assertEquals (0 , $ this ->_model ->getCookieLifetime ());
142
+ $ model = $ this ->getModel ();
143
+ $ model ->setCookieLifetime (0 );
144
+ $ this ->assertEquals (0 , $ model ->getCookieLifetime ());
146
145
}
147
146
148
147
public function testSettingInvalidCookieLifetime ()
149
148
{
150
- $ preVal = $ this ->_model ->getCookieLifetime ();
151
- $ this ->_model ->setCookieLifetime ('foobar_bogus ' );
152
- $ this ->assertEquals ($ preVal , $ this ->_model ->getCookieLifetime ());
149
+ $ model = $ this ->getModel ();
150
+ $ preVal = $ model ->getCookieLifetime ();
151
+ $ model ->setCookieLifetime ('foobar_bogus ' );
152
+ $ this ->assertEquals ($ preVal , $ model ->getCookieLifetime ());
153
153
}
154
154
155
155
public function testSettingInvalidCookieLifetime2 ()
156
156
{
157
- $ preVal = $ this ->_model ->getCookieLifetime ();
158
- $ this ->_model ->setCookieLifetime (-1 );
159
- $ this ->assertEquals ($ preVal , $ this ->_model ->getCookieLifetime ());
157
+ $ model = $ this ->getModel ();
158
+ $ preVal = $ model ->getCookieLifetime ();
159
+ $ model ->setCookieLifetime (-1 );
160
+ $ this ->assertEquals ($ preVal , $ model ->getCookieLifetime ());
160
161
}
161
162
162
163
public function testWrongMethodCall ()
163
164
{
165
+ $ model = $ this ->getModel ();
164
166
$ this ->expectException (
165
167
'\BadMethodCallException ' ,
166
168
'Method "methodThatNotExist" does not exist in Magento\Framework\Session\Config '
167
169
);
168
- $ this -> _model ->methodThatNotExist ();
170
+ $ model ->methodThatNotExist ();
169
171
}
170
172
171
173
public function testCookieSecureDefaultsToIniSettings ()
172
174
{
173
- $ this ->assertSame ((bool )ini_get ('session.cookie_secure ' ), $ this ->_model ->getCookieSecure ());
175
+ $ model = $ this ->getModel ();
176
+ $ this ->assertSame ((bool )ini_get ('session.cookie_secure ' ), $ model ->getCookieSecure ());
174
177
}
175
178
176
179
public function testSetCookieSecureInOptions ()
177
180
{
178
- $ this ->_model ->setCookieSecure (true );
179
- $ this ->assertTrue ($ this ->_model ->getCookieSecure ());
181
+ $ model = $ this ->getModel ();
182
+ $ model ->setCookieSecure (true );
183
+ $ this ->assertTrue ($ model ->getCookieSecure ());
180
184
}
181
185
182
186
public function testCookieDomainIsMutable ()
183
187
{
184
- $ this ->_model ->setCookieDomain ('example.com ' );
185
- $ this ->assertEquals ('example.com ' , $ this ->_model ->getCookieDomain ());
188
+ $ model = $ this ->getModel ();
189
+ $ model ->setCookieDomain ('example.com ' );
190
+ $ this ->assertEquals ('example.com ' , $ model ->getCookieDomain ());
186
191
}
187
192
188
193
public function testCookieDomainCanBeEmpty ()
189
194
{
190
- $ this ->_model ->setCookieDomain ('' );
191
- $ this ->assertEquals ('' , $ this ->_model ->getCookieDomain ());
195
+ $ model = $ this ->getModel ();
196
+ $ model ->setCookieDomain ('' );
197
+ $ this ->assertEquals ('' , $ model ->getCookieDomain ());
192
198
}
193
199
194
200
public function testSettingInvalidCookieDomain ()
195
201
{
196
- $ preVal = $ this ->_model ->getCookieDomain ();
197
- $ this ->_model ->setCookieDomain (24 );
198
- $ this ->assertEquals ($ preVal , $ this ->_model ->getCookieDomain ());
202
+ $ model = $ this ->getModel ();
203
+ $ preVal = $ model ->getCookieDomain ();
204
+ $ model ->setCookieDomain (24 );
205
+ $ this ->assertEquals ($ preVal , $ model ->getCookieDomain ());
199
206
}
200
207
201
208
public function testSettingInvalidCookieDomain2 ()
202
209
{
203
- $ preVal = $ this ->_model ->getCookieDomain ();
204
- $ this ->_model ->setCookieDomain ('D: \\WINDOWS \\System32 \\drivers \\etc \\hosts ' );
205
- $ this ->assertEquals ($ preVal , $ this ->_model ->getCookieDomain ());
210
+ $ model = $ this ->getModel ();
211
+ $ preVal = $ model ->getCookieDomain ();
212
+ $ model ->setCookieDomain ('D: \\WINDOWS \\System32 \\drivers \\etc \\hosts ' );
213
+ $ this ->assertEquals ($ preVal , $ model ->getCookieDomain ());
206
214
}
207
215
208
216
public function testSetCookieHttpOnlyInOptions ()
209
217
{
210
- $ this ->_model ->setCookieHttpOnly (true );
211
- $ this ->assertTrue ($ this ->_model ->getCookieHttpOnly ());
218
+ $ model = $ this ->getModel ();
219
+ $ model ->setCookieHttpOnly (true );
220
+ $ this ->assertTrue ($ model ->getCookieHttpOnly ());
212
221
}
213
222
214
223
public function testUseCookiesDefaultsToIniSettings ()
215
224
{
216
- $ this ->assertSame ((bool )ini_get ('session.use_cookies ' ), $ this ->_model ->getUseCookies ());
225
+ $ model = $ this ->getModel ();
226
+ $ this ->assertSame ((bool )ini_get ('session.use_cookies ' ), $ model ->getUseCookies ());
217
227
}
218
228
219
229
public function testSetUseCookiesInOptions ()
220
230
{
221
- $ this ->_model ->setUseCookies (true );
222
- $ this ->assertTrue ($ this ->_model ->getUseCookies ());
231
+ $ model = $ this ->getModel ();
232
+ $ model ->setUseCookies (true );
233
+ $ this ->assertTrue ($ model ->getUseCookies ());
223
234
}
224
235
225
236
public function testUseOnlyCookiesDefaultsToIniSettings ()
226
237
{
227
- $ this ->assertSame ((bool )ini_get ('session.use_only_cookies ' ), $ this ->_model ->getUseOnlyCookies ());
238
+ $ model = $ this ->getModel ();
239
+ $ this ->assertSame ((bool )ini_get ('session.use_only_cookies ' ), $ model ->getUseOnlyCookies ());
228
240
}
229
241
230
242
public function testSetUseOnlyCookiesInOptions ()
231
243
{
232
- $ this ->_model ->setOption ('use_only_cookies ' , true );
233
- $ this ->assertTrue ((bool )$ this ->_model ->getOption ('use_only_cookies ' ));
244
+ $ model = $ this ->getModel ();
245
+ $ model ->setOption ('use_only_cookies ' , true );
246
+ $ this ->assertTrue ((bool )$ model ->getOption ('use_only_cookies ' ));
234
247
}
235
248
236
249
public function testRefererCheckDefaultsToIniSettings ()
237
250
{
238
- $ this ->assertSame (ini_get ('session.referer_check ' ), $ this ->_model ->getRefererCheck ());
251
+ $ model = $ this ->getModel ();
252
+ $ this ->assertSame (ini_get ('session.referer_check ' ), $ model ->getRefererCheck ());
239
253
}
240
254
241
255
public function testRefererCheckIsMutable ()
242
256
{
243
- $ this ->_model ->setOption ('referer_check ' , 'FOOBAR ' );
244
- $ this ->assertEquals ('FOOBAR ' , $ this ->_model ->getOption ('referer_check ' ));
257
+ $ model = $ this ->getModel ();
258
+ $ model ->setOption ('referer_check ' , 'FOOBAR ' );
259
+ $ this ->assertEquals ('FOOBAR ' , $ model ->getOption ('referer_check ' ));
245
260
}
246
261
247
262
public function testRefererCheckMayBeEmpty ()
248
263
{
249
- $ this ->_model ->setOption ('referer_check ' , '' );
250
- $ this ->assertEquals ('' , $ this ->_model ->getOption ('referer_check ' ));
264
+ $ model = $ this ->getModel ();
265
+ $ model ->setOption ('referer_check ' , '' );
266
+ $ this ->assertEquals ('' , $ model ->getOption ('referer_check ' ));
251
267
}
252
268
253
269
public function testSetSavePath ()
254
270
{
255
- $ this ->_model ->setSavePath ('some_save_path ' );
256
- $ this ->assertEquals ($ this ->_model ->getOption ('save_path ' ), 'some_save_path ' );
271
+ $ model = $ this ->getModel ();
272
+ $ model ->setSavePath ('some_save_path ' );
273
+ $ this ->assertEquals ($ model ->getOption ('save_path ' ), 'some_save_path ' );
257
274
}
258
275
259
276
/**
260
- * @dataProvider savePathDataProvider
277
+ * @param string|null $existingSavePath
278
+ * @param $givenSavePath
279
+ * @param $expectedSavePath
280
+ * @param $givenSaveHandler
281
+ * @param $expectedSaveHandler
282
+ * @dataProvider constructorDataProvider
261
283
*/
262
- public function testConstructorSavePath ($ existing , $ given , $ expected )
263
- {
284
+ public function testConstructor (
285
+ $ existingSavePath ,
286
+ $ givenSavePath ,
287
+ $ expectedSavePath ,
288
+ $ givenSaveHandler ,
289
+ $ expectedSaveHandler
290
+ ) {
264
291
$ sessionSavePath = ini_get ('session.save_path ' );
265
- if ($ expected === 'default ' ) {
266
- $ expected = $ this ->defaultSavePath . '/ ' ;
292
+ $ sessionSaveHandler = ini_get ('session.save_handler ' );
293
+ if ($ expectedSavePath === 'default ' ) {
294
+ $ expectedSavePath = $ this ->defaultSavePath . '/ ' ;
295
+ }
296
+ if ($ expectedSaveHandler === 'php ' ) {
297
+ $ expectedSaveHandler = $ sessionSaveHandler ;
267
298
}
268
- $ setup = ini_set ('session.save_path ' , $ existing );
299
+ $ setup = ini_set ('session.save_path ' , $ existingSavePath );
269
300
if ($ setup === false ) {
270
301
$ this ->markTestSkipped ('Cannot set session.save_path with ini_set ' );
271
302
}
272
303
273
304
$ deploymentConfigMock = $ this ->createMock (\Magento \Framework \App \DeploymentConfig::class);
274
305
$ deploymentConfigMock
275
306
->method ('get ' )
276
- ->willReturnCallback (function ($ configPath ) use ($ given ) {
307
+ ->willReturnCallback (function ($ configPath ) use ($ givenSavePath , $ givenSaveHandler ) {
277
308
switch ($ configPath ) {
278
309
case Config::PARAM_SESSION_SAVE_METHOD :
279
- return ' files ' ;
310
+ return $ givenSaveHandler ;
280
311
case Config::PARAM_SESSION_CACHE_LIMITER :
281
312
return $ this ->_cacheLimiter ;
282
313
case Config::PARAM_SESSION_SAVE_PATH :
283
- return $ given ;
314
+ return $ givenSavePath ;
284
315
default :
285
316
return null ;
286
317
}
@@ -290,23 +321,32 @@ public function testConstructorSavePath($existing, $given, $expected)
290
321
\Magento \Framework \Session \Config::class,
291
322
['deploymentConfig ' => $ deploymentConfigMock ]
292
323
);
293
- $ this ->assertEquals ($ expected , $ model ->getOption ('save_path ' ));
324
+ $ this ->assertEquals ($ expectedSavePath , $ model ->getOption ('save_path ' ));
325
+ $ this ->assertEquals ($ expectedSaveHandler , $ model ->getOption ('session.save_handler ' ));
294
326
295
327
if ($ sessionSavePath != ini_get ('session.save_path ' )) {
296
328
ini_set ('session.save_path ' , $ sessionSavePath );
297
329
}
298
330
}
299
331
300
- public function savePathDataProvider ()
332
+ public function constructorDataProvider ()
301
333
{
302
334
// preset value (null = not set), input value (null = not set), expected value
303
335
$ savePathGiven = 'explicit_save_path ' ;
304
336
$ presetPath = 'preset_save_path ' ;
305
337
return [
306
- [null , $ savePathGiven , $ savePathGiven ],
307
- [null , null , 'default ' ],
308
- [$ presetPath , $ savePathGiven , $ savePathGiven ],
309
- [$ presetPath , null , $ presetPath ],
338
+ [null , $ savePathGiven , $ savePathGiven, ' db ' , ' db ' ],
339
+ [null , null , 'default ' , null , ' php ' ],
340
+ [$ presetPath , $ savePathGiven , $ savePathGiven, ' redis ' , ' redis ' ],
341
+ [$ presetPath , null , $ presetPath, ' files ' , ' files ' ],
310
342
];
311
343
}
344
+
345
+ private function getModel (): \Magento \Framework \Session \Config
346
+ {
347
+ return $ this ->_objectManager ->create (
348
+ \Magento \Framework \Session \Config::class,
349
+ ['deploymentConfig ' => $ this ->deploymentConfigMock ]
350
+ );
351
+ }
312
352
}
0 commit comments