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

Commit 6f048ff

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

File tree

1 file changed

+162
-61
lines changed

1 file changed

+162
-61
lines changed

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

Lines changed: 162 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
namespace Magento\Framework\Session {
1212

13+
use Magento\Framework\App\DeploymentConfig;
1314
use Magento\Framework\App\State;
15+
use Magento\Framework\Session\Config\ConfigInterface;
16+
1417
// @codingStandardsIgnoreEnd
1518

1619
/**
@@ -36,30 +39,72 @@ function headers_sent()
3639
return call_user_func_array('\headers_sent', func_get_args());
3740
}
3841

42+
/**
43+
* Mock ini_set global function
44+
*
45+
* @param string $varName
46+
* @param string $newValue
47+
* @return bool|string
48+
*/
49+
function ini_set($varName, $newValue)
50+
{
51+
global $mockPHPFunctions;
52+
if ($mockPHPFunctions) {
53+
SessionManagerTest::$isIniSetInvoked[$varName] = $newValue;
54+
return true;
55+
}
56+
return call_user_func_array('\ini_set', func_get_args());
57+
}
58+
59+
/**
60+
* Mock session_set_save_handler global function
61+
*
62+
* @return bool
63+
*/
64+
function session_set_save_handler()
65+
{
66+
global $mockPHPFunctions;
67+
if ($mockPHPFunctions) {
68+
SessionManagerTest::$isSessionSetSaveHandlerInvoked = true;
69+
return true;
70+
}
71+
return call_user_func_array('\session_set_save_handler', func_get_args());
72+
}
73+
3974
/**
4075
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4176
*/
4277
class SessionManagerTest extends \PHPUnit\Framework\TestCase
4378
{
79+
/**
80+
* @var string[]
81+
*/
82+
public static $isIniSetInvoked = [];
83+
84+
/**
85+
* @var bool
86+
*/
87+
public static $isSessionSetSaveHandlerInvoked;
88+
4489
/**
4590
* @var \Magento\Framework\Session\SessionManagerInterface
4691
*/
47-
protected $_model;
92+
private $model;
4893

4994
/**
5095
* @var \Magento\Framework\Session\SidResolverInterface
5196
*/
52-
protected $_sidResolver;
97+
private $sidResolver;
5398

5499
/**
55100
* @var string
56101
*/
57-
protected $sessionName;
102+
private $sessionName;
58103

59104
/**
60-
* @var \Magento\Framework\ObjectManagerInterface
105+
* @var \Magento\TestFramework\ObjectManager
61106
*/
62-
protected $objectManager;
107+
private $objectManager;
63108

64109
/**
65110
* @var \Magento\Framework\App\RequestInterface
@@ -79,6 +124,21 @@ protected function setUp()
79124
ini_set('session.name', $this->sessionName);
80125

81126
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
127+
$deploymentConfigMock = $this->createMock(DeploymentConfig::class);
128+
$deploymentConfigMock->method('get')
129+
->willReturnCallback(function ($configPath) {
130+
switch ($configPath) {
131+
case Config::PARAM_SESSION_SAVE_METHOD:
132+
return 'db';
133+
case Config::PARAM_SESSION_CACHE_LIMITER:
134+
return 'private_no_expire';
135+
case Config::PARAM_SESSION_SAVE_PATH:
136+
return 'explicit_save_path';
137+
default:
138+
return null;
139+
}
140+
});
141+
$this->objectManager->addSharedInstance($deploymentConfigMock, DeploymentConfig::class);
82142

83143
/** @var \Magento\Framework\Session\SidResolverInterface $sidResolver */
84144
$this->appState = $this->getMockBuilder(State::class)
@@ -87,138 +147,142 @@ protected function setUp()
87147
->getMock();
88148

89149
/** @var \Magento\Framework\Session\SidResolver $sidResolver */
90-
$this->_sidResolver = $this->objectManager->create(
150+
$this->sidResolver = $this->objectManager->create(
91151
\Magento\Framework\Session\SidResolver::class,
92152
[
93153
'appState' => $this->appState,
94154
]
95155
);
96156

97157
$this->request = $this->objectManager->get(\Magento\Framework\App\RequestInterface::class);
98-
99-
/** @var \Magento\Framework\Session\SessionManager _model */
100-
$this->_model = $this->objectManager->create(
101-
\Magento\Framework\Session\SessionManager::class,
102-
[
103-
$this->objectManager->get(\Magento\Framework\App\Request\Http::class),
104-
$this->_sidResolver,
105-
$this->objectManager->get(\Magento\Framework\Session\Config\ConfigInterface::class),
106-
$this->objectManager->get(\Magento\Framework\Session\SaveHandlerInterface::class),
107-
$this->objectManager->get(\Magento\Framework\Session\ValidatorInterface::class),
108-
$this->objectManager->get(\Magento\Framework\Session\StorageInterface::class)
109-
]
110-
);
111158
}
112159

113160
protected function tearDown()
114161
{
115162
global $mockPHPFunctions;
116163
$mockPHPFunctions = false;
117-
$this->_model->destroy();
164+
self::$isIniSetInvoked = [];
165+
self::$isSessionSetSaveHandlerInvoked = false;
166+
if ($this->model !== null) {
167+
$this->model->destroy();
168+
$this->model = null;
169+
}
170+
$this->objectManager->removeSharedInstance(DeploymentConfig::class);
118171
}
119172

120173
public function testSessionNameFromIni()
121174
{
122-
$this->_model->start();
123-
$this->assertSame($this->sessionName, $this->_model->getName());
124-
$this->_model->destroy();
175+
$this->initializeModel();
176+
$this->model->start();
177+
$this->assertSame($this->sessionName, $this->model->getName());
178+
$this->model->destroy();
125179
}
126180

127181
public function testSessionUseOnlyCookies()
128182
{
183+
$this->initializeModel();
129184
$expectedValue = '1';
130185
$sessionUseOnlyCookies = ini_get('session.use_only_cookies');
131186
$this->assertSame($expectedValue, $sessionUseOnlyCookies);
132187
}
133188

134189
public function testGetData()
135190
{
136-
$this->_model->setData(['test_key' => 'test_value']);
137-
$this->assertEquals('test_value', $this->_model->getData('test_key', true));
138-
$this->assertNull($this->_model->getData('test_key'));
191+
$this->initializeModel();
192+
$this->model->setData(['test_key' => 'test_value']);
193+
$this->assertEquals('test_value', $this->model->getData('test_key', true));
194+
$this->assertNull($this->model->getData('test_key'));
139195
}
140196

141197
public function testGetSessionId()
142198
{
143-
$this->assertEquals(session_id(), $this->_model->getSessionId());
199+
$this->initializeModel();
200+
$this->assertEquals(session_id(), $this->model->getSessionId());
144201
}
145202

146203
public function testGetName()
147204
{
148-
$this->assertEquals(session_name(), $this->_model->getName());
205+
$this->initializeModel();
206+
$this->assertEquals(session_name(), $this->model->getName());
149207
}
150208

151209
public function testSetName()
152210
{
153-
$this->_model->destroy();
154-
$this->_model->setName('test');
155-
$this->_model->start();
156-
$this->assertEquals('test', $this->_model->getName());
211+
$this->initializeModel();
212+
$this->model->destroy();
213+
$this->model->setName('test');
214+
$this->model->start();
215+
$this->assertEquals('test', $this->model->getName());
157216
}
158217

159218
public function testDestroy()
160219
{
220+
$this->initializeModel();
161221
$data = ['key' => 'value'];
162-
$this->_model->setData($data);
222+
$this->model->setData($data);
163223

164-
$this->assertEquals($data, $this->_model->getData());
165-
$this->_model->destroy();
224+
$this->assertEquals($data, $this->model->getData());
225+
$this->model->destroy();
166226

167-
$this->assertEquals([], $this->_model->getData());
227+
$this->assertEquals([], $this->model->getData());
168228
}
169229

170230
public function testSetSessionId()
171231
{
172-
$sessionId = $this->_model->getSessionId();
232+
$this->initializeModel();
233+
$sessionId = $this->model->getSessionId();
173234
$this->appState->expects($this->atLeastOnce())
174235
->method('getAreaCode')
175236
->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND);
176-
$this->_model->setSessionId($this->_sidResolver->getSid($this->_model));
177-
$this->assertEquals($sessionId, $this->_model->getSessionId());
237+
$this->model->setSessionId($this->sidResolver->getSid($this->model));
238+
$this->assertEquals($sessionId, $this->model->getSessionId());
178239

179-
$this->_model->setSessionId('test');
180-
$this->assertEquals('test', $this->_model->getSessionId());
240+
$this->model->setSessionId('test');
241+
$this->assertEquals('test', $this->model->getSessionId());
181242
}
182243

183244
/**
184245
* @magentoConfigFixture current_store web/session/use_frontend_sid 1
185246
*/
186247
public function testSetSessionIdFromParam()
187248
{
249+
$this->initializeModel();
188250
$this->appState->expects($this->atLeastOnce())
189251
->method('getAreaCode')
190252
->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND);
191-
$this->assertNotEquals('test_id', $this->_model->getSessionId());
192-
$this->request->getQuery()->set($this->_sidResolver->getSessionIdQueryParam($this->_model), 'test-id');
193-
$this->_model->setSessionId($this->_sidResolver->getSid($this->_model));
194-
$this->assertEquals('test-id', $this->_model->getSessionId());
253+
$this->assertNotEquals('test_id', $this->model->getSessionId());
254+
$this->request->getQuery()->set($this->sidResolver->getSessionIdQueryParam($this->model), 'test-id');
255+
$this->model->setSessionId($this->sidResolver->getSid($this->model));
256+
$this->assertEquals('test-id', $this->model->getSessionId());
195257
/* Use not valid identifier */
196-
$this->request->getQuery()->set($this->_sidResolver->getSessionIdQueryParam($this->_model), 'test_id');
197-
$this->_model->setSessionId($this->_sidResolver->getSid($this->_model));
198-
$this->assertEquals('test-id', $this->_model->getSessionId());
258+
$this->request->getQuery()->set($this->sidResolver->getSessionIdQueryParam($this->model), 'test_id');
259+
$this->model->setSessionId($this->sidResolver->getSid($this->model));
260+
$this->assertEquals('test-id', $this->model->getSessionId());
199261
}
200262

201263
public function testGetSessionIdForHost()
202264
{
265+
$this->initializeModel();
203266
$_SERVER['HTTP_HOST'] = 'localhost';
204-
$this->_model->start();
205-
$this->assertEmpty($this->_model->getSessionIdForHost('localhost'));
206-
$this->assertNotEmpty($this->_model->getSessionIdForHost('test'));
207-
$this->_model->destroy();
267+
$this->model->start();
268+
$this->assertEmpty($this->model->getSessionIdForHost('localhost'));
269+
$this->assertNotEmpty($this->model->getSessionIdForHost('test'));
270+
$this->model->destroy();
208271
}
209272

210273
public function testIsValidForHost()
211274
{
275+
$this->initializeModel();
212276
$_SERVER['HTTP_HOST'] = 'localhost';
213-
$this->_model->start();
277+
$this->model->start();
214278

215-
$reflection = new \ReflectionMethod($this->_model, '_addHost');
279+
$reflection = new \ReflectionMethod($this->model, '_addHost');
216280
$reflection->setAccessible(true);
217-
$reflection->invoke($this->_model);
281+
$reflection->invoke($this->model);
218282

219-
$this->assertFalse($this->_model->isValidForHost('test.com'));
220-
$this->assertTrue($this->_model->isValidForHost('localhost'));
221-
$this->_model->destroy();
283+
$this->assertFalse($this->model->isValidForHost('test.com'));
284+
$this->assertTrue($this->model->isValidForHost('localhost'));
285+
$this->model->destroy();
222286
}
223287

224288
/**
@@ -236,9 +300,9 @@ public function testStartAreaNotSet()
236300
*
237301
* @var \Magento\Framework\Session\SessionManager _model
238302
*/
239-
$this->_model = new \Magento\Framework\Session\SessionManager(
303+
$this->model = new \Magento\Framework\Session\SessionManager(
240304
$this->objectManager->get(\Magento\Framework\App\Request\Http::class),
241-
$this->_sidResolver,
305+
$this->sidResolver,
242306
$this->objectManager->get(\Magento\Framework\Session\Config\ConfigInterface::class),
243307
$this->objectManager->get(\Magento\Framework\Session\SaveHandlerInterface::class),
244308
$this->objectManager->get(\Magento\Framework\Session\ValidatorInterface::class),
@@ -250,7 +314,44 @@ public function testStartAreaNotSet()
250314

251315
global $mockPHPFunctions;
252316
$mockPHPFunctions = true;
253-
$this->_model->start();
317+
$this->model->start();
318+
}
319+
320+
public function testConstructor()
321+
{
322+
global $mockPHPFunctions;
323+
$mockPHPFunctions = true;
324+
$this->model = $this->objectManager->create(
325+
\Magento\Framework\Session\SessionManager::class,
326+
[
327+
'sidResolver' => $this->sidResolver
328+
]
329+
);
330+
$sessionConfig = $this->objectManager->get(ConfigInterface::class);
331+
$this->assertEquals('db', $sessionConfig->getOption('session.save_handler'));
332+
$this->assertEquals('private_no_expire', $sessionConfig->getOption('session.cache_limiter'));
333+
$this->assertEquals('explicit_save_path', $sessionConfig->getOption('session.save_path'));
334+
$this->assertArrayHasKey('session.use_only_cookies', self::$isIniSetInvoked);
335+
$this->assertEquals('1', self::$isIniSetInvoked['session.use_only_cookies']);
336+
foreach ($sessionConfig->getOptions() as $option => $value) {
337+
if ($option=='session.save_handler') {
338+
$this->assertArrayNotHasKey('session.save_handler', self::$isIniSetInvoked);
339+
} else {
340+
$this->assertArrayHasKey($option, self::$isIniSetInvoked);
341+
$this->assertEquals($value, self::$isIniSetInvoked[$option]);
342+
}
343+
}
344+
$this->assertTrue(self::$isSessionSetSaveHandlerInvoked);
345+
}
346+
347+
private function initializeModel(): void
348+
{
349+
$this->model = $this->objectManager->create(
350+
\Magento\Framework\Session\SessionManager::class,
351+
[
352+
'sidResolver' => $this->sidResolver
353+
]
354+
);
254355
}
255356
}
256357
}

0 commit comments

Comments
 (0)