Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SidResolverTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Zend\Stdlib\Parameters;
10 
11 class SidResolverTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $model;
17 
21  protected $session;
22 
26  protected $store;
27 
31  protected $scopeConfig;
32 
36  protected $urlBuilder;
37 
41  protected $customSessionName = 'csn';
42 
46  protected $customSessionQueryParam = 'csqp';
47 
51  protected $request;
52 
56  private $appState;
57 
58  protected function setUp()
59  {
61 
63  $this->session = $objectManager->get(\Magento\Framework\Session\Generic::class);
64 
65  $this->scopeConfig = $this->getMockBuilder(
66  \Magento\Framework\App\Config\ScopeConfigInterface::class
67  )->setMethods(
68  ['getValue']
69  )->disableOriginalConstructor()->getMockForAbstractClass();
70 
71  $this->urlBuilder = $this->getMockBuilder(
72  \Magento\Framework\Url::class
73  )->setMethods(
74  ['isOwnOriginUrl']
75  )->disableOriginalConstructor()->getMockForAbstractClass();
76 
77  $this->request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
78 
79  $this->appState = $this->getMockBuilder(State::class)
80  ->setMethods(['getAreaCode'])
81  ->disableOriginalConstructor()
82  ->getMock();
83 
84  $this->model = $objectManager->create(
85  \Magento\Framework\Session\SidResolver::class,
86  [
87  'scopeConfig' => $this->scopeConfig,
88  'urlBuilder' => $this->urlBuilder,
89  'sidNameMap' => [$this->customSessionName => $this->customSessionQueryParam],
90  'request' => $this->request,
91  'appState' => $this->appState,
92  ]
93  );
94  }
95 
96  public function tearDown()
97  {
98  $this->request->setQuery(new Parameters());
99  }
100 
108  public function testGetSid($sid, $useFrontedSid, $isOwnOriginUrl, $testSid)
109  {
110  $this->appState->expects($this->atLeastOnce())
111  ->method('getAreaCode')
112  ->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND);
113 
114  $this->scopeConfig->expects(
115  $this->any()
116  )->method(
117  'getValue'
118  )->with(
120  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
121  )->will(
122  $this->returnValue($useFrontedSid)
123  );
124 
125  $this->urlBuilder->expects($this->any())->method('isOwnOriginUrl')->will($this->returnValue($isOwnOriginUrl));
126 
127  if ($testSid) {
128  $this->request->getQuery()->set($this->model->getSessionIdQueryParam($this->session), $testSid);
129  }
130  $this->assertEquals($sid, $this->model->getSid($this->session));
131  $this->assertEquals($useFrontedSid, $this->model->getUseSessionInUrl());
132  }
133 
137  public function dataProviderTestGetSid()
138  {
139  return [
140  [null, false, false, 'test-sid'],
141  [null, false, true, 'test-sid'],
142  [null, false, false, 'test-sid'],
143  [null, true, false, 'test-sid'],
144  [null, false, true, 'test-sid'],
145  ['test-sid', true, true, 'test-sid'],
146  [null, true, true, null]
147  ];
148  }
149 
150  public function testGetSessionIdQueryParam()
151  {
152  $this->assertEquals(SidResolver::SESSION_ID_QUERY_PARAM, $this->model->getSessionIdQueryParam($this->session));
153  }
154 
156  {
157  $this->session->destroy();
158  $oldSessionName = $this->session->getName();
159  $this->session->setName($this->customSessionName);
160  $this->assertEquals($this->customSessionQueryParam, $this->model->getSessionIdQueryParam($this->session));
161  $this->session->setName($oldSessionName);
162  $this->session->start();
163  }
164 
165  public function testSetGetUseSessionVar()
166  {
167  $this->assertFalse($this->model->getUseSessionVar());
168  $this->model->setUseSessionVar(true);
169  $this->assertTrue($this->model->getUseSessionVar());
170  }
171 
177  public function dataProviderSessionInUrl()
178  {
179  return [
180  [true],
181  [false],
182  ];
183  }
184 
193  public function testSetGetUseSessionInUrl($configValue)
194  {
195  $this->scopeConfig->expects(
196  $this->any()
197  )->method(
198  'getValue'
199  )->with(
201  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
202  )->will(
203  $this->returnValue($configValue)
204  );
205 
206  $this->assertEquals($configValue, $this->model->getUseSessionInUrl());
207  $this->model->setUseSessionInUrl(!$configValue);
208  $this->assertEquals(!$configValue, $this->model->getUseSessionInUrl());
209  }
210 }
$objectManager
Definition: bootstrap.php:17
testGetSid($sid, $useFrontedSid, $isOwnOriginUrl, $testSid)