Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 0ae6108

Browse files
author
Joan He
committed
MAGETWO-89567: Cannot set 'user' save handler by ini_set()
1 parent f2f2027 commit 0ae6108

File tree

3 files changed

+207
-147
lines changed

3 files changed

+207
-147
lines changed

dev/tests/integration/testsuite/Magento/Framework/Session/ConfigTest.php

Lines changed: 116 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
*/
1313
class ConfigTest extends \PHPUnit\Framework\TestCase
1414
{
15-
/** @var \Magento\Framework\Session\Config */
16-
private $_model;
17-
1815
/** @var string */
1916
private $_cacheLimiter = 'private_no_expire';
2017

@@ -45,10 +42,6 @@ protected function setUp()
4542
}
4643
});
4744

48-
$this->_model = $this->_objectManager->create(
49-
\Magento\Framework\Session\Config::class,
50-
['deploymentConfig' => $this->deploymentConfigMock]
51-
);
5245
$this->defaultSavePath = $this->_objectManager
5346
->get(\Magento\Framework\Filesystem\DirectoryList::class)
5447
->getPath(DirectoryList::SESSION);
@@ -59,6 +52,7 @@ protected function setUp()
5952
*/
6053
public function testDefaultConfiguration()
6154
{
55+
$model = $this->getModel();
6256
/** @var \Magento\Framework\Filesystem $filesystem */
6357
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
6458
\Magento\Framework\Filesystem::class
@@ -68,35 +62,37 @@ public function testDefaultConfiguration()
6862

6963
$this->assertEquals(
7064
$path,
71-
$this->_model->getSavePath()
65+
$model->getSavePath()
7266
);
7367
$this->assertEquals(
7468
\Magento\Framework\Session\Config::COOKIE_LIFETIME_DEFAULT,
75-
$this->_model->getCookieLifetime()
69+
$model->getCookieLifetime()
7670
);
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'));
8377
}
8478

8579
public function testSetOptionsInvalidValue()
8680
{
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());
9085
}
9186

9287
/**
9388
* @dataProvider optionsProvider
9489
*/
9590
public function testSetOptions($option, $getter, $value)
9691
{
92+
$model = $this->getModel();
9793
$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}());
10096
}
10197

10298
public function optionsProvider()
@@ -129,158 +125,193 @@ public function optionsProvider()
129125

130126
public function testNameIsMutable()
131127
{
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());
134131
}
135132

136133
public function testCookieLifetimeIsMutable()
137134
{
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());
140138
}
141139

142140
public function testCookieLifetimeCanBeZero()
143141
{
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());
146145
}
147146

148147
public function testSettingInvalidCookieLifetime()
149148
{
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());
153153
}
154154

155155
public function testSettingInvalidCookieLifetime2()
156156
{
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());
160161
}
161162

162163
public function testWrongMethodCall()
163164
{
165+
$model = $this->getModel();
164166
$this->expectException(
165167
'\BadMethodCallException',
166168
'Method "methodThatNotExist" does not exist in Magento\Framework\Session\Config'
167169
);
168-
$this->_model->methodThatNotExist();
170+
$model->methodThatNotExist();
169171
}
170172

171173
public function testCookieSecureDefaultsToIniSettings()
172174
{
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());
174177
}
175178

176179
public function testSetCookieSecureInOptions()
177180
{
178-
$this->_model->setCookieSecure(true);
179-
$this->assertTrue($this->_model->getCookieSecure());
181+
$model = $this->getModel();
182+
$model->setCookieSecure(true);
183+
$this->assertTrue($model->getCookieSecure());
180184
}
181185

182186
public function testCookieDomainIsMutable()
183187
{
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());
186191
}
187192

188193
public function testCookieDomainCanBeEmpty()
189194
{
190-
$this->_model->setCookieDomain('');
191-
$this->assertEquals('', $this->_model->getCookieDomain());
195+
$model = $this->getModel();
196+
$model->setCookieDomain('');
197+
$this->assertEquals('', $model->getCookieDomain());
192198
}
193199

194200
public function testSettingInvalidCookieDomain()
195201
{
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());
199206
}
200207

201208
public function testSettingInvalidCookieDomain2()
202209
{
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());
206214
}
207215

208216
public function testSetCookieHttpOnlyInOptions()
209217
{
210-
$this->_model->setCookieHttpOnly(true);
211-
$this->assertTrue($this->_model->getCookieHttpOnly());
218+
$model = $this->getModel();
219+
$model->setCookieHttpOnly(true);
220+
$this->assertTrue($model->getCookieHttpOnly());
212221
}
213222

214223
public function testUseCookiesDefaultsToIniSettings()
215224
{
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());
217227
}
218228

219229
public function testSetUseCookiesInOptions()
220230
{
221-
$this->_model->setUseCookies(true);
222-
$this->assertTrue($this->_model->getUseCookies());
231+
$model = $this->getModel();
232+
$model->setUseCookies(true);
233+
$this->assertTrue($model->getUseCookies());
223234
}
224235

225236
public function testUseOnlyCookiesDefaultsToIniSettings()
226237
{
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());
228240
}
229241

230242
public function testSetUseOnlyCookiesInOptions()
231243
{
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'));
234247
}
235248

236249
public function testRefererCheckDefaultsToIniSettings()
237250
{
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());
239253
}
240254

241255
public function testRefererCheckIsMutable()
242256
{
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'));
245260
}
246261

247262
public function testRefererCheckMayBeEmpty()
248263
{
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'));
251267
}
252268

253269
public function testSetSavePath()
254270
{
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');
257274
}
258275

259276
/**
260-
* @dataProvider savePathDataProvider
277+
* @param string|null $existingSavePath
278+
* @param $givenSavePath
279+
* @param $expectedSavePath
280+
* @param $givenSaveHandler
281+
* @param $expectedSaveHandler
282+
* @dataProvider constructorDataProvider
261283
*/
262-
public function testConstructorSavePath($existing, $given, $expected)
263-
{
284+
public function testConstructor(
285+
$existingSavePath,
286+
$givenSavePath,
287+
$expectedSavePath,
288+
$givenSaveHandler,
289+
$expectedSaveHandler
290+
) {
264291
$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;
267298
}
268-
$setup = ini_set('session.save_path', $existing);
299+
$setup = ini_set('session.save_path', $existingSavePath);
269300
if ($setup === false) {
270301
$this->markTestSkipped('Cannot set session.save_path with ini_set');
271302
}
272303

273304
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
274305
$deploymentConfigMock
275306
->method('get')
276-
->willReturnCallback(function ($configPath) use ($given) {
307+
->willReturnCallback(function ($configPath) use ($givenSavePath, $givenSaveHandler) {
277308
switch ($configPath) {
278309
case Config::PARAM_SESSION_SAVE_METHOD:
279-
return 'files';
310+
return $givenSaveHandler;
280311
case Config::PARAM_SESSION_CACHE_LIMITER:
281312
return $this->_cacheLimiter;
282313
case Config::PARAM_SESSION_SAVE_PATH:
283-
return $given;
314+
return $givenSavePath;
284315
default:
285316
return null;
286317
}
@@ -290,23 +321,32 @@ public function testConstructorSavePath($existing, $given, $expected)
290321
\Magento\Framework\Session\Config::class,
291322
['deploymentConfig' => $deploymentConfigMock]
292323
);
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'));
294326

295327
if ($sessionSavePath != ini_get('session.save_path')) {
296328
ini_set('session.save_path', $sessionSavePath);
297329
}
298330
}
299331

300-
public function savePathDataProvider()
332+
public function constructorDataProvider()
301333
{
302334
// preset value (null = not set), input value (null = not set), expected value
303335
$savePathGiven = 'explicit_save_path';
304336
$presetPath = 'preset_save_path';
305337
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'],
310342
];
311343
}
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+
}
312352
}

0 commit comments

Comments
 (0)