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

https://gitlab.com/yousafsyed/easternglamor · PHP · 216 lines · 144 code · 33 blank · 39 comment · 2 complexity · 4c70be6b4fa12eb41e84b0ea5e6d77f9 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreStart
  7. namespace {
  8. $mockPHPFunctions = false;
  9. }
  10. namespace Magento\Framework\Session {
  11. // @codingStandardsIgnoreEnd
  12. /**
  13. * Mock session_status if in test mode, or continue normal execution otherwise
  14. *
  15. * @return int Session status code
  16. */
  17. function session_status()
  18. {
  19. global $mockPHPFunctions;
  20. if ($mockPHPFunctions) {
  21. return PHP_SESSION_NONE;
  22. }
  23. return call_user_func_array('\session_status', func_get_args());
  24. }
  25. function headers_sent()
  26. {
  27. global $mockPHPFunctions;
  28. if ($mockPHPFunctions) {
  29. return false;
  30. }
  31. return call_user_func_array('\headers_sent', func_get_args());
  32. }
  33. class SessionManagerTest extends \PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * @var \Magento\Framework\Session\SessionManagerInterface
  37. */
  38. protected $_model;
  39. /**
  40. * @var \Magento\Framework\Session\SidResolverInterface
  41. */
  42. protected $_sidResolver;
  43. /**
  44. * @var string
  45. */
  46. protected $sessionName;
  47. /**
  48. * @var \Magento\Framework\ObjectManagerInterface
  49. */
  50. protected $objectManager;
  51. protected function setUp()
  52. {
  53. $this->sessionName = 'frontEndSession';
  54. ini_set('session.use_only_cookies', '0');
  55. ini_set('session.name', $this->sessionName);
  56. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  57. /** @var \Magento\Framework\Session\SidResolverInterface $sidResolver */
  58. $this->_sidResolver = $this->objectManager->get('Magento\Framework\Session\SidResolverInterface');
  59. $this->request = $this->objectManager->get('Magento\Framework\App\RequestInterface');
  60. /** @var \Magento\Framework\Session\SessionManager _model */
  61. $this->_model = $this->objectManager->create(
  62. 'Magento\Framework\Session\SessionManager',
  63. [
  64. $this->objectManager->get('Magento\Framework\App\Request\Http'),
  65. $this->_sidResolver,
  66. $this->objectManager->get('Magento\Framework\Session\Config\ConfigInterface'),
  67. $this->objectManager->get('Magento\Framework\Session\SaveHandlerInterface'),
  68. $this->objectManager->get('Magento\Framework\Session\ValidatorInterface'),
  69. $this->objectManager->get('Magento\Framework\Session\StorageInterface')
  70. ]
  71. );
  72. }
  73. public function testSessionNameFromIni()
  74. {
  75. $this->_model->start();
  76. $this->assertSame($this->sessionName, $this->_model->getName());
  77. $this->_model->destroy();
  78. }
  79. public function testSessionUseOnlyCookies()
  80. {
  81. $expectedValue = '1';
  82. $sessionUseOnlyCookies = ini_get('session.use_only_cookies');
  83. $this->assertSame($expectedValue, $sessionUseOnlyCookies);
  84. }
  85. public function testGetData()
  86. {
  87. $this->_model->setData(['test_key' => 'test_value']);
  88. $this->assertEquals('test_value', $this->_model->getData('test_key', true));
  89. $this->assertNull($this->_model->getData('test_key'));
  90. }
  91. public function testGetSessionId()
  92. {
  93. $this->assertEquals(session_id(), $this->_model->getSessionId());
  94. }
  95. public function testGetName()
  96. {
  97. $this->assertEquals(session_name(), $this->_model->getName());
  98. }
  99. public function testSetName()
  100. {
  101. $this->_model->setName('test');
  102. $this->assertEquals('test', $this->_model->getName());
  103. }
  104. public function testDestroy()
  105. {
  106. $data = ['key' => 'value'];
  107. $this->_model->setData($data);
  108. $this->assertEquals($data, $this->_model->getData());
  109. $this->_model->destroy();
  110. $this->assertEquals([], $this->_model->getData());
  111. }
  112. public function testSetSessionId()
  113. {
  114. $sessionId = $this->_model->getSessionId();
  115. $this->_model->setSessionId($this->_sidResolver->getSid($this->_model));
  116. $this->assertEquals($sessionId, $this->_model->getSessionId());
  117. $this->_model->setSessionId('test');
  118. $this->assertEquals('test', $this->_model->getSessionId());
  119. }
  120. /**
  121. * @magentoConfigFixture current_store web/session/use_frontend_sid 1
  122. */
  123. public function testSetSessionIdFromParam()
  124. {
  125. $this->assertNotEquals('test_id', $this->_model->getSessionId());
  126. $this->request->getQuery()->set($this->_sidResolver->getSessionIdQueryParam($this->_model), 'test-id');
  127. $this->_model->setSessionId($this->_sidResolver->getSid($this->_model));
  128. $this->assertEquals('test-id', $this->_model->getSessionId());
  129. /* Use not valid identifier */
  130. $this->request->getQuery()->set($this->_sidResolver->getSessionIdQueryParam($this->_model), 'test_id');
  131. $this->_model->setSessionId($this->_sidResolver->getSid($this->_model));
  132. $this->assertEquals('test-id', $this->_model->getSessionId());
  133. }
  134. public function testGetSessionIdForHost()
  135. {
  136. $_SERVER['HTTP_HOST'] = 'localhost';
  137. $this->_model->start();
  138. $this->assertEmpty($this->_model->getSessionIdForHost('localhost'));
  139. $this->assertNotEmpty($this->_model->getSessionIdForHost('test'));
  140. $this->_model->destroy();
  141. }
  142. public function testIsValidForHost()
  143. {
  144. $_SERVER['HTTP_HOST'] = 'localhost';
  145. $this->_model->start();
  146. $reflection = new \ReflectionMethod($this->_model, '_addHost');
  147. $reflection->setAccessible(true);
  148. $reflection->invoke($this->_model);
  149. $this->assertFalse($this->_model->isValidForHost('test.com'));
  150. $this->assertTrue($this->_model->isValidForHost('localhost'));
  151. $this->_model->destroy();
  152. }
  153. /**
  154. * @expectedException \Magento\Framework\Exception\SessionException
  155. * @expectedExceptionMessage Area code not set: Area code must be set before starting a session.
  156. */
  157. public function testStartAreaNotSet()
  158. {
  159. $scope = $this->objectManager->get('Magento\Framework\Config\ScopeInterface');
  160. $appState = new \Magento\Framework\App\State($scope);
  161. /**
  162. * Must be created by "new" in order to get a real Magento\Framework\App\State object that
  163. * is not overridden in the TestFramework
  164. *
  165. * @var \Magento\Framework\Session\SessionManager _model
  166. */
  167. $this->_model = new \Magento\Framework\Session\SessionManager(
  168. $this->objectManager->get('Magento\Framework\App\Request\Http'),
  169. $this->_sidResolver,
  170. $this->objectManager->get('Magento\Framework\Session\Config\ConfigInterface'),
  171. $this->objectManager->get('Magento\Framework\Session\SaveHandlerInterface'),
  172. $this->objectManager->get('Magento\Framework\Session\ValidatorInterface'),
  173. $this->objectManager->get('Magento\Framework\Session\StorageInterface'),
  174. $this->objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface'),
  175. $this->objectManager->get('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory'),
  176. $appState
  177. );
  178. global $mockPHPFunctions;
  179. $mockPHPFunctions = true;
  180. $this->_model->start();
  181. }
  182. }
  183. }