SidResolverTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session;
  7. use Magento\Framework\App\State;
  8. use Zend\Stdlib\Parameters;
  9. class SidResolverTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\Session\SidResolver
  13. */
  14. protected $model;
  15. /**
  16. * @var \Magento\Framework\Session\Generic
  17. */
  18. protected $session;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
  21. */
  22. protected $store;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
  25. */
  26. protected $scopeConfig;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface
  29. */
  30. protected $urlBuilder;
  31. /**
  32. * @var string
  33. */
  34. protected $customSessionName = 'csn';
  35. /**
  36. * @var string
  37. */
  38. protected $customSessionQueryParam = 'csqp';
  39. /**
  40. * @var \Magento\Framework\App\RequestInterface
  41. */
  42. protected $request;
  43. /**
  44. * @var State|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $appState;
  47. protected function setUp()
  48. {
  49. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  50. /** @var \Magento\Framework\Session\Generic _model */
  51. $this->session = $objectManager->get(\Magento\Framework\Session\Generic::class);
  52. $this->scopeConfig = $this->getMockBuilder(
  53. \Magento\Framework\App\Config\ScopeConfigInterface::class
  54. )->setMethods(
  55. ['getValue']
  56. )->disableOriginalConstructor()->getMockForAbstractClass();
  57. $this->urlBuilder = $this->getMockBuilder(
  58. \Magento\Framework\Url::class
  59. )->setMethods(
  60. ['isOwnOriginUrl']
  61. )->disableOriginalConstructor()->getMockForAbstractClass();
  62. $this->request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
  63. $this->appState = $this->getMockBuilder(State::class)
  64. ->setMethods(['getAreaCode'])
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->model = $objectManager->create(
  68. \Magento\Framework\Session\SidResolver::class,
  69. [
  70. 'scopeConfig' => $this->scopeConfig,
  71. 'urlBuilder' => $this->urlBuilder,
  72. 'sidNameMap' => [$this->customSessionName => $this->customSessionQueryParam],
  73. 'request' => $this->request,
  74. 'appState' => $this->appState,
  75. ]
  76. );
  77. }
  78. public function tearDown()
  79. {
  80. $this->request->setQuery(new Parameters());
  81. }
  82. /**
  83. * @param mixed $sid
  84. * @param bool $useFrontedSid
  85. * @param bool $isOwnOriginUrl
  86. * @param mixed $testSid
  87. * @dataProvider dataProviderTestGetSid
  88. */
  89. public function testGetSid($sid, $useFrontedSid, $isOwnOriginUrl, $testSid)
  90. {
  91. $this->appState->expects($this->atLeastOnce())
  92. ->method('getAreaCode')
  93. ->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND);
  94. $this->scopeConfig->expects(
  95. $this->any()
  96. )->method(
  97. 'isSetFlag'
  98. )->with(
  99. \Magento\Framework\Session\SidResolver::XML_PATH_USE_FRONTEND_SID,
  100. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  101. )->will(
  102. $this->returnValue($useFrontedSid)
  103. );
  104. $this->urlBuilder->expects($this->any())->method('isOwnOriginUrl')->will($this->returnValue($isOwnOriginUrl));
  105. if ($testSid) {
  106. $this->request->getQuery()->set($this->model->getSessionIdQueryParam($this->session), $testSid);
  107. }
  108. $this->assertEquals($sid, $this->model->getSid($this->session));
  109. $this->assertEquals($useFrontedSid, $this->model->getUseSessionInUrl());
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function dataProviderTestGetSid()
  115. {
  116. return [
  117. [null, false, false, 'test-sid'],
  118. [null, false, true, 'test-sid'],
  119. [null, false, false, 'test-sid'],
  120. [null, true, false, 'test-sid'],
  121. [null, false, true, 'test-sid'],
  122. ['test-sid', true, true, 'test-sid'],
  123. [null, true, true, null]
  124. ];
  125. }
  126. public function testGetSessionIdQueryParam()
  127. {
  128. $this->assertEquals(SidResolver::SESSION_ID_QUERY_PARAM, $this->model->getSessionIdQueryParam($this->session));
  129. }
  130. public function testGetSessionIdQueryParamCustom()
  131. {
  132. $this->session->destroy();
  133. $oldSessionName = $this->session->getName();
  134. $this->session->setName($this->customSessionName);
  135. $this->assertEquals($this->customSessionQueryParam, $this->model->getSessionIdQueryParam($this->session));
  136. $this->session->setName($oldSessionName);
  137. $this->session->start();
  138. }
  139. public function testSetGetUseSessionVar()
  140. {
  141. $this->assertFalse($this->model->getUseSessionVar());
  142. $this->model->setUseSessionVar(true);
  143. $this->assertTrue($this->model->getUseSessionVar());
  144. }
  145. /**
  146. * Variations of Use SID on frontend value.
  147. *
  148. * @return array
  149. */
  150. public function dataProviderSessionInUrl()
  151. {
  152. return [
  153. [true],
  154. [false],
  155. ];
  156. }
  157. /**
  158. * Testing "Use SID in URLs" flag.
  159. * Checking that the method returns config value if not explicitly
  160. * overridden.
  161. *
  162. * @param bool $configValue Use SID on frontend config value.
  163. * @dataProvider dataProviderSessionInUrl
  164. */
  165. public function testSetGetUseSessionInUrl($configValue)
  166. {
  167. $this->scopeConfig->expects(
  168. $this->any()
  169. )->method(
  170. 'isSetFlag'
  171. )->with(
  172. \Magento\Framework\Session\SidResolver::XML_PATH_USE_FRONTEND_SID,
  173. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  174. )->will(
  175. $this->returnValue($configValue)
  176. );
  177. $this->assertEquals($configValue, $this->model->getUseSessionInUrl());
  178. $this->model->setUseSessionInUrl(!$configValue);
  179. $this->assertEquals(!$configValue, $this->model->getUseSessionInUrl());
  180. }
  181. }