Skip to content

Commit e7bf481

Browse files
author
Igor Melnikov
committed
MAGETWO-51809: Port Redis session adapter to Magento 2.0.* and make it make it backwards compatible
Porting changes
1 parent 5702983 commit e7bf481

File tree

16 files changed

+1017
-1189
lines changed

16 files changed

+1017
-1189
lines changed

app/code/Magento/Backend/Model/Session/AdminConfig.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
/**
1616
* Magento Backend session configuration
17-
*
18-
* @method Config setSaveHandler()
1917
*/
2018
class AdminConfig extends Config
2119
{

app/etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141
<preference for="Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface" type="Magento\Framework\Stdlib\DateTime\DateTimeFormatter"/>
142142
<preference for="Magento\Framework\Api\Search\SearchInterface" type="Magento\Framework\Search\Search"/>
143143
<preference for="Magento\Framework\View\Design\FileResolution\Fallback\ResolverInterface" type="Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple" />
144+
<preference for="Cm\RedisSession\Handler\ConfigInterface" type="Magento\Framework\Session\SaveHandler\Redis\Config"/>
145+
<preference for="Cm\RedisSession\Handler\LoggerInterface" type="Magento\Framework\Session\SaveHandler\Redis\Logger"/>
144146
<type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" />
145147
<type name="Magento\Framework\Logger\Handler\Base">
146148
<arguments>
@@ -186,9 +188,16 @@
186188
<arguments>
187189
<argument name="handlers" xsi:type="array">
188190
<item name="db" xsi:type="string">Magento\Framework\Session\SaveHandler\DbTable</item>
191+
<item name="redis" xsi:type="string">Magento\Framework\Session\SaveHandler\Redis</item>
189192
</argument>
190193
</arguments>
191194
</type>
195+
<type name="Magento\Framework\Session\SaveHandler\Redis">
196+
<arguments>
197+
<argument name="config" xsi:type="object">Cm\RedisSession\Handler\ConfigInterface</argument>
198+
<argument name="logger" xsi:type="object">Cm\RedisSession\Handler\LoggerInterface</argument>
199+
</arguments>
200+
</type>
192201
<virtualType name="interceptionConfigScope" type="Magento\Framework\Config\Scope">
193202
<arguments>
194203
<argument name="defaultScope" xsi:type="string">global</argument>

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"zendframework/zend-log": "~2.4.6",
3939
"zendframework/zend-http": "~2.4.6",
4040
"magento/zendframework1": "1.12.16",
41+
"colinmollenhour/credis": "1.6",
42+
"colinmollenhour/php-redis-session-abstract": "1.1",
4143
"composer/composer": "1.0.0-alpha10",
4244
"monolog/monolog": "1.16.0",
4345
"oyejorge/less.php": "1.7.0.3",
@@ -188,7 +190,6 @@
188190
"magento/framework": "100.0.7",
189191
"trentrichardson/jquery-timepicker-addon": "1.4.3",
190192
"colinmollenhour/cache-backend-redis": "1.8",
191-
"colinmollenhour/credis": "1.5",
192193
"components/jquery": "1.11.0",
193194
"blueimp/jquery-file-upload": "5.6.14",
194195
"components/jqueryui": "1.10.4",
@@ -199,7 +200,6 @@
199200
"component_paths": {
200201
"trentrichardson/jquery-timepicker-addon": "lib/web/jquery/jquery-ui-timepicker-addon.js",
201202
"colinmollenhour/cache-backend-redis": "lib/internal/Cm/Cache/Backend/Redis.php",
202-
"colinmollenhour/credis": "lib/internal/Credis",
203203
"components/jquery": [
204204
"lib/web/jquery.js",
205205
"lib/web/jquery/jquery.min.js",

composer.lock

Lines changed: 79 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 18 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));
@@ -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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
/** @var \Magento\Framework\Session\Config\ConfigInterface */
16+
private $sessionConfig;
17+
18+
/** @var \Magento\Framework\App\DeploymentConfig */
19+
private $deploymentConfig;
20+
21+
public function setUp()
22+
{
23+
$this->sessionConfig = ObjectManager::getInstance()->get(ConfigInterface::class);
24+
$this->deploymentConfig = ObjectManager::getInstance()->get(DeploymentConfig::class);
25+
}
26+
27+
/**
28+
* Tests that the session handler is correctly set when object is created.
29+
*
30+
* @dataProvider saveHandlerProvider
31+
* @param string $deploymentConfigHandler
32+
* @param string $iniHandler
33+
*/
34+
public function testSetSaveHandler($deploymentConfigHandler, $iniHandler)
35+
{
36+
// Set expected session.save_handler config
37+
if ($deploymentConfigHandler) {
38+
if ($deploymentConfigHandler !== 'files') {
39+
$expected = 'user';
40+
} else {
41+
$expected = $deploymentConfigHandler;
42+
}
43+
} else if ($iniHandler) {
44+
$expected = $iniHandler;
45+
} else {
46+
$expected = SaveHandlerInterface::DEFAULT_HANDLER;
47+
}
48+
49+
// Set ini configuration
50+
if ($iniHandler) {
51+
$oldIni = ini_set('session.save_handler', $iniHandler);
52+
}
53+
54+
/** @var DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject $deploymentConfigMock */
55+
$deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$deploymentConfigMock->expects($this->once())
59+
->method('get')
60+
->with(Config::PARAM_SESSION_SAVE_METHOD, SaveHandlerInterface::DEFAULT_HANDLER)
61+
->willReturn($deploymentConfigHandler ?: SaveHandlerInterface::DEFAULT_HANDLER);
62+
63+
new SaveHandler(
64+
ObjectManager::getInstance()->get(SaveHandlerFactory::class),
65+
$deploymentConfigMock
66+
);
67+
68+
// Test expectation
69+
$this->assertEquals(
70+
$expected,
71+
ObjectManager::getInstance()->get(ConfigInterface::class)->getOption('session.save_handler')
72+
);
73+
74+
// Reset ini configuration
75+
if (isset($oldIni)) {
76+
ini_set('session.save_handler', $oldIni);
77+
}
78+
}
79+
80+
public function saveHandlerProvider()
81+
{
82+
return [
83+
['db', false],
84+
['db', 'files'],
85+
[false, 'files'],
86+
[false, false],
87+
];
88+
}
89+
}

0 commit comments

Comments
 (0)