Skip to content

Commit 12172ea

Browse files
committed
Merge pull request #15 from magento-extensibility/MAGETWO-31851-session-save-path
[Extensibility] Magetwo 31851 session save path
2 parents 94f9665 + 8289225 commit 12172ea

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
2727
*/
2828
protected $_objectManager;
2929

30+
/**
31+
* @var string Default value for session.save_path setting
32+
*/
33+
protected $defaultSavePath;
34+
35+
/**
36+
* @var \Magento\Framework\App\DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
protected $deploymentConfigMock;
39+
3040
protected function setUp()
3141
{
3242
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
@@ -35,24 +45,27 @@ protected function setUp()
3545
if ($sessionManager->isSessionExists()) {
3646
$sessionManager->writeClose();
3747
}
38-
$deploymentConfigMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
39-
$deploymentConfigMock->expects($this->at(0))
48+
$this->deploymentConfigMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
49+
$this->deploymentConfigMock->expects($this->at(0))
4050
->method('get')
4151
->with(Config::PARAM_SESSION_SAVE_METHOD, 'files')
4252
->will($this->returnValue('files'));
43-
$deploymentConfigMock->expects($this->at(1))
53+
$this->deploymentConfigMock->expects($this->at(1))
4454
->method('get')
4555
->with(Config::PARAM_SESSION_SAVE_PATH)
4656
->will($this->returnValue(null));
47-
$deploymentConfigMock->expects($this->at(2))
57+
$this->deploymentConfigMock->expects($this->at(2))
4858
->method('get')
4959
->with(Config::PARAM_SESSION_CACHE_LIMITER)
5060
->will($this->returnValue($this->_cacheLimiter));
5161

5262
$this->_model = $this->_objectManager->create(
5363
'Magento\Framework\Session\Config',
54-
['deploymentConfig' => $deploymentConfigMock]
64+
['deploymentConfig' => $this->deploymentConfigMock]
5565
);
66+
$this->defaultSavePath = $this->_objectManager
67+
->get('Magento\Framework\Filesystem\DirectoryList')
68+
->getPath(DirectoryList::SESSION);
5669
}
5770

5871
protected function tearDown()
@@ -283,4 +296,47 @@ public function testSetSavePath()
283296
$this->_model->setSavePath('some_save_path');
284297
$this->assertEquals($this->_model->getOption('save_path'), 'some_save_path');
285298
}
299+
300+
/**
301+
* @dataProvider savePathDataProvider
302+
*/
303+
public function testConstructorSavePath($existing, $given, $expected)
304+
{
305+
$sessionSavePath = ini_get('session.save_path');
306+
if ($expected === 'default') {
307+
$expected = $this->defaultSavePath . '/';
308+
}
309+
$setup = ini_set('session.save_path', $existing);
310+
if ($setup === false) {
311+
$this->markTestSkipped('Cannot set session.save_path with ini_set');
312+
}
313+
314+
$this->deploymentConfigMock->expects($this->at(1))
315+
->method('get')
316+
->with(Config::PARAM_SESSION_SAVE_PATH)
317+
->will($this->returnValue($given));
318+
319+
$this->_model = $this->_objectManager->create(
320+
'Magento\Framework\Session\Config',
321+
['deploymentConfig' => $this->deploymentConfigMock]
322+
);
323+
$this->assertEquals($expected, $this->_model->getOption('save_path'));
324+
325+
if ($sessionSavePath != ini_get('session.save_path')) {
326+
ini_set('session.save_path', $sessionSavePath);
327+
}
328+
}
329+
330+
public function savePathDataProvider()
331+
{
332+
// preset value (null = not set), input value (null = not set), expected value
333+
$savePathGiven = 'explicit_save_path';
334+
$presetPath = 'preset_save_path';
335+
return [
336+
[null, $savePathGiven, $savePathGiven],
337+
[null, null, 'default'],
338+
[$presetPath, $savePathGiven, $savePathGiven],
339+
[$presetPath, null, $presetPath],
340+
];
341+
}
286342
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ public function constructorDataProvider()
364364
true,
365365
[
366366
'session.save_handler' => 'files',
367-
'session.save_path' => null,
368367
'session.cache_limiter' => 'files',
369368
'session.cookie_lifetime' => 7200,
370369
'session.cookie_path' => '/',
@@ -377,7 +376,6 @@ public function constructorDataProvider()
377376
false,
378377
[
379378
'session.save_handler' => 'files',
380-
'session.save_path' => null,
381379
'session.cache_limiter' => 'files',
382380
'session.cookie_httponly' => false,
383381
],
@@ -387,7 +385,6 @@ public function constructorDataProvider()
387385
true,
388386
[
389387
'session.save_handler' => 'files',
390-
'session.save_path' => null,
391388
'session.cache_limiter' => 'files',
392389
'session.cookie_lifetime' => 3600,
393390
'session.cookie_path' => '/',

lib/internal/Magento/Framework/Session/Config.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ public function __construct(
136136

137137
$this->setSaveHandler($saveMethod === 'db' ? 'user' : $saveMethod);
138138

139-
if (!$savePath) {
139+
if (!$savePath && !ini_get('session.save_path')) {
140140
$sessionDir = $filesystem->getDirectoryWrite(DirectoryList::SESSION);
141141
$savePath = $sessionDir->getAbsolutePath();
142142
$sessionDir->create();
143143
}
144-
$this->setSavePath($savePath);
145-
144+
if ($savePath) {
145+
$this->setSavePath($savePath);
146+
}
146147
if ($cacheLimiter) {
147148
$this->setOption('session.cache_limiter', $cacheLimiter);
148149
}

0 commit comments

Comments
 (0)