Skip to content

Commit 03bc835

Browse files
Merge remote-tracking branch 'origin/MAGETWO-52143-redis-2.1' into develop
2 parents 52ea72b + d607770 commit 03bc835

File tree

6 files changed

+135
-122
lines changed

6 files changed

+135
-122
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,12 @@ protected function setUp()
3636
$sessionManager->writeClose();
3737
}
3838
$this->deploymentConfigMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
39+
3940
$this->deploymentConfigMock->expects($this->at(0))
40-
->method('get')
41-
->with($this->equalTo(Config::PARAM_SESSION_SAVE_METHOD), $this->anything())
42-
->will($this->returnValue('files'));
43-
$this->deploymentConfigMock->expects($this->at(1))
4441
->method('get')
4542
->with(Config::PARAM_SESSION_SAVE_PATH)
4643
->will($this->returnValue(null));
47-
$this->deploymentConfigMock->expects($this->at(2))
44+
$this->deploymentConfigMock->expects($this->at(1))
4845
->method('get')
4946
->with(Config::PARAM_SESSION_CACHE_LIMITER)
5047
->will($this->returnValue($this->_cacheLimiter));
@@ -57,7 +54,7 @@ protected function setUp()
5754
->get('Magento\Framework\Filesystem\DirectoryList')
5855
->getPath(DirectoryList::SESSION);
5956
}
60-
57+
6158
/**
6259
* @magentoAppIsolation enabled
6360
*/
@@ -86,11 +83,6 @@ public function testDefaultConfiguration()
8683
$this->assertEquals($this->_model->getSavePath(), $this->_model->getOption('save_path'));
8784
}
8885

89-
public function testGetSessionSaveMethod()
90-
{
91-
$this->assertEquals('files', $this->_model->getSaveHandler());
92-
}
93-
9486
/**
9587
* Unable to add integration tests for testGetLifetimePathNonDefault
9688
*
@@ -122,7 +114,6 @@ public function optionsProvider()
122114
return [
123115
['save_path', 'getSavePath', __DIR__],
124116
['name', 'getName', 'FOOBAR'],
125-
['save_handler', 'getSaveHandler', 'user'],
126117
['gc_probability', 'getGcProbability', 42],
127118
['gc_divisor', 'getGcDivisor', 3],
128119
['gc_maxlifetime', 'getGcMaxlifetime', 180],
@@ -152,12 +143,6 @@ public function testNameIsMutable()
152143
$this->assertEquals('FOOBAR', $this->_model->getName());
153144
}
154145

155-
public function testSaveHandlerIsMutable()
156-
{
157-
$this->_model->setSaveHandler('user');
158-
$this->assertEquals('user', $this->_model->getSaveHandler());
159-
}
160-
161146
public function testCookieLifetimeIsMutable()
162147
{
163148
$this->_model->setCookieLifetime(20);
@@ -295,7 +280,7 @@ public function testConstructorSavePath($existing, $given, $expected)
295280
$this->markTestSkipped('Cannot set session.save_path with ini_set');
296281
}
297282

298-
$this->deploymentConfigMock->expects($this->at(1))
283+
$this->deploymentConfigMock->expects($this->at(0))
299284
->method('get')
300285
->with(Config::PARAM_SESSION_SAVE_PATH)
301286
->will($this->returnValue($given));
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Session;
7+
8+
use Magento\Framework\App\DeploymentConfig;
9+
use Magento\Framework\Session\Config\ConfigInterface;
10+
use Magento\Framework\Session\SaveHandler;
11+
use Magento\Framework\App\ObjectManager;
12+
13+
class SaveHandlerTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* Tests that the session handler is correctly set when object is created.
17+
*
18+
* @dataProvider saveHandlerProvider
19+
* @param string $deploymentConfigHandler
20+
* @param string $iniHandler
21+
*/
22+
public function testSetSaveHandler($deploymentConfigHandler, $iniHandler)
23+
{
24+
// Set expected session.save_handler config
25+
if ($deploymentConfigHandler) {
26+
if ($deploymentConfigHandler !== 'files') {
27+
$expected = 'user';
28+
} else {
29+
$expected = $deploymentConfigHandler;
30+
}
31+
} else if ($iniHandler) {
32+
$expected = $iniHandler;
33+
} else {
34+
$expected = SaveHandlerInterface::DEFAULT_HANDLER;
35+
}
36+
37+
// Set ini configuration
38+
if ($iniHandler) {
39+
$oldIni = ini_set('session.save_handler', $iniHandler);
40+
}
41+
42+
/** @var DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject $deploymentConfigMock */
43+
$deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$deploymentConfigMock->expects($this->once())
47+
->method('get')
48+
->with(Config::PARAM_SESSION_SAVE_METHOD, SaveHandlerInterface::DEFAULT_HANDLER)
49+
->willReturn($deploymentConfigHandler ?: SaveHandlerInterface::DEFAULT_HANDLER);
50+
51+
new SaveHandler(
52+
ObjectManager::getInstance()->get(SaveHandlerFactory::class),
53+
$deploymentConfigMock
54+
);
55+
56+
// Test expectation
57+
$this->assertEquals(
58+
$expected,
59+
ObjectManager::getInstance()->get(ConfigInterface::class)->getOption('session.save_handler')
60+
);
61+
62+
// Reset ini configuration
63+
if (isset($oldIni)) {
64+
ini_set('session.save_handler', $oldIni);
65+
}
66+
}
67+
68+
public function saveHandlerProvider()
69+
{
70+
return [
71+
['db', false],
72+
['db', 'files'],
73+
[false, 'files'],
74+
[false, false],
75+
];
76+
}
77+
}

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ class Config implements ConfigInterface
7676
/** @var string */
7777
protected $lifetimePath;
7878

79-
/** @var string */
80-
private $saveHandlerName;
81-
8279
/** @var \Magento\Framework\ValidatorFactory */
8380
protected $_validatorFactory;
8481

@@ -110,20 +107,6 @@ public function __construct(
110107
$this->_scopeType = $scopeType;
111108
$this->lifetimePath = $lifetimePath;
112109

113-
/**
114-
* Session handler
115-
*
116-
* Save handler may be set to custom value in deployment config, which will override everything else.
117-
* Otherwise, try to read PHP settings for session.save_handler value. Otherwise, use 'files' as default.
118-
*/
119-
$defaultSaveHandler = $this->getStorageOption('session.save_handler')
120-
?: SaveHandlerInterface::DEFAULT_HANDLER;
121-
$saveMethod = $deploymentConfig->get(
122-
self::PARAM_SESSION_SAVE_METHOD,
123-
$defaultSaveHandler
124-
);
125-
$this->setSaveHandler($saveMethod);
126-
127110
/**
128111
* Session path
129112
*/
@@ -271,38 +254,6 @@ public function getName()
271254
return (string)$this->getOption('session.name');
272255
}
273256

274-
/**
275-
* {@inheritdoc}
276-
*/
277-
public function setSaveHandler($saveHandler)
278-
{
279-
$this->setSaveHandlerName($saveHandler);
280-
if ($saveHandler === 'db' || $saveHandler === 'redis') {
281-
$saveHandler = 'user';
282-
}
283-
$this->setOption('session.save_handler', $saveHandler);
284-
return $this;
285-
}
286-
287-
/**
288-
* Set save handler name
289-
*
290-
* @param string $saveHandlerName
291-
* @return void
292-
*/
293-
private function setSaveHandlerName($saveHandlerName)
294-
{
295-
$this->saveHandlerName = $saveHandlerName;
296-
}
297-
298-
/**
299-
* {@inheritdoc}
300-
*/
301-
public function getSaveHandlerName()
302-
{
303-
return $this->saveHandlerName;
304-
}
305-
306257
/**
307258
* Set session.save_path
308259
*

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,4 @@ public function setUseCookies($useCookies);
170170
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
171171
*/
172172
public function getUseCookies();
173-
174-
/**
175-
* Get save handler name
176-
*
177-
* @return string
178-
*/
179-
public function getSaveHandlerName();
180-
181-
/**
182-
* Set session.save_handler
183-
*
184-
* @param string $saveHandler
185-
* @return $this
186-
*/
187-
public function setSaveHandler($saveHandler);
188173
}

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Framework\Session;
77

8+
use Magento\Framework\App\DeploymentConfig;
89
use Magento\Framework\Exception\SessionException;
910
use Magento\Framework\Session\Config\ConfigInterface;
1011

@@ -20,24 +21,40 @@ class SaveHandler implements SaveHandlerInterface
2021
*/
2122
protected $saveHandlerAdapter;
2223

24+
/**
25+
* Config
26+
*
27+
* @var ConfigInterface
28+
*/
29+
private $config;
30+
2331
/**
2432
* Constructor
2533
*
2634
* @param SaveHandlerFactory $saveHandlerFactory
27-
* @param ConfigInterface $config
35+
* @param DeploymentConfig $deploymentConfig
2836
* @param string $default
2937
*/
3038
public function __construct(
3139
SaveHandlerFactory $saveHandlerFactory,
32-
ConfigInterface $config,
40+
DeploymentConfig $deploymentConfig,
3341
$default = self::DEFAULT_HANDLER
3442
) {
35-
$saveMethod = $config->getSaveHandlerName();
43+
/**
44+
* Session handler
45+
*
46+
* Save handler may be set to custom value in deployment config, which will override everything else.
47+
* Otherwise, try to read PHP settings for session.save_handler value. Otherwise, use 'files' as default.
48+
*/
49+
$defaultSaveHandler = ini_get('session.save_handler') ?: SaveHandlerInterface::DEFAULT_HANDLER;
50+
$saveMethod = $deploymentConfig->get(Config::PARAM_SESSION_SAVE_METHOD, $defaultSaveHandler);
51+
$this->setSaveHandler($saveMethod);
52+
3653
try {
3754
$connection = $saveHandlerFactory->create($saveMethod);
3855
} catch (SessionException $e) {
3956
$connection = $saveHandlerFactory->create($default);
40-
$config->setSaveHandler($default);
57+
$this->setSaveHandler($default);
4158
}
4259
$this->saveHandlerAdapter = $connection;
4360
}
@@ -110,4 +127,35 @@ public function gc($maxLifetime)
110127
{
111128
return $this->saveHandlerAdapter->gc($maxLifetime);
112129
}
130+
131+
/**
132+
* Get config
133+
*
134+
* @return ConfigInterface
135+
* @deprecated
136+
*/
137+
private function getConfig()
138+
{
139+
if (!($this->config instanceof ConfigInterface)) {
140+
return \Magento\Framework\App\ObjectManager::getInstance()->get(
141+
ConfigInterface::class
142+
);
143+
}
144+
return $this->config;
145+
}
146+
147+
/**
148+
* Set session.save_handler option
149+
*
150+
* @param string $saveHandler
151+
* @return $this
152+
*/
153+
private function setSaveHandler($saveHandler)
154+
{
155+
if ($saveHandler === 'db' || $saveHandler === 'redis') {
156+
$saveHandler = 'user';
157+
}
158+
$this->getConfig()->setOption('session.save_handler', $saveHandler);
159+
return $this;
160+
}
113161
}

0 commit comments

Comments
 (0)