|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests\Session; |
|
5
|
|
|
|
|
6
|
|
|
use Nyholm\Psr7\Response; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Message\UriInterface; |
|
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
13
|
|
|
use Yiisoft\Yii\Web\Session\SessionException; |
|
14
|
|
|
use Yiisoft\Yii\Web\Session\SessionInterface; |
|
15
|
|
|
use Yiisoft\Yii\Web\Session\SessionMiddleware; |
|
16
|
|
|
|
|
17
|
|
|
class SessionMiddlewareTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
private const COOKIE_PARAMETERS = [ |
|
20
|
|
|
'path' => 'examplePath', |
|
21
|
|
|
'domain' => 'exampleDomain', |
|
22
|
|
|
'httponly' => 'httponly', |
|
23
|
|
|
'samesite' => 'Strict', |
|
24
|
|
|
'lifetime' => 3600, |
|
25
|
|
|
'secure' => true, |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
private const CURRENT_SID = 'exampleCurrentSidValue'; |
|
29
|
|
|
private const REQUEST_SID = 'exampleRequestSidValue'; |
|
30
|
|
|
private const SESSION_NAME = 'exampleSessionName'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var RequestHandlerInterface|PHPUnit_Framework_MockObject_MockObject |
|
34
|
|
|
*/ |
|
35
|
|
|
private $requestHandlerMock; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var SessionInterface|PHPUnit_Framework_MockObject_MockObject |
|
39
|
|
|
*/ |
|
40
|
|
|
private $sessionMock; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ServerRequestInterface|PHPUnit_Framework_MockObject_MockObject |
|
44
|
|
|
*/ |
|
45
|
|
|
private $requestMock; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var UriInterface|PHPUnit_Framework_MockObject_MockObject |
|
49
|
|
|
*/ |
|
50
|
|
|
private $uriMock; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var SessionMiddleware |
|
54
|
|
|
*/ |
|
55
|
|
|
private $sessionMiddleware; |
|
56
|
|
|
|
|
57
|
|
|
public function setUp(): void |
|
58
|
|
|
{ |
|
59
|
|
|
parent::setUp(); |
|
60
|
|
|
$this->requestHandlerMock = $this->createMock(RequestHandlerInterface::class); |
|
61
|
|
|
$this->sessionMock = $this->createMock(SessionInterface::class); |
|
62
|
|
|
$this->sessionMiddleware = new SessionMiddleware($this->sessionMock); |
|
63
|
|
|
$this->requestMock = $this->createMock(ServerRequestInterface::class); |
|
64
|
|
|
$this->uriMock = $this->createMock(UriInterface::class); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testProcessDiscardsSessionWhenRequestHandlerThrowsException(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->requestHandlerMock |
|
70
|
|
|
->expects($this->once()) |
|
71
|
|
|
->method('handle') |
|
72
|
|
|
->willThrowException(new \Exception()); |
|
73
|
|
|
|
|
74
|
|
|
$this->sessionMock |
|
75
|
|
|
->expects($this->once()) |
|
76
|
|
|
->method('discard'); |
|
77
|
|
|
|
|
78
|
|
|
$this->expectException(\Exception::class); |
|
79
|
|
|
$this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testProcessThrowsSessionExceptionWhenConnectionIsNotUsingHttps(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->setUpSessionMock(); |
|
85
|
|
|
$this->setUpRequestMock(false); |
|
86
|
|
|
$this->expectException(SessionException::class); |
|
87
|
|
|
$this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testProcessGetsDomainFromRequestWhenDomainCookieParameterNotProvided(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->setUpSessionMock(false); |
|
93
|
|
|
$this->setUpRequestMock(); |
|
94
|
|
|
|
|
95
|
|
|
$this->uriMock |
|
96
|
|
|
->expects($this->once()) |
|
97
|
|
|
->method('getHost') |
|
98
|
|
|
->willReturn('domain'); |
|
99
|
|
|
|
|
100
|
|
|
$response = new Response(); |
|
101
|
|
|
$this->setUpRequestHandlerMock($response); |
|
102
|
|
|
$this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testProcessDoesNotAlterResponseIfSessionIsNotActive(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$this->setUpSessionMock(true, false); |
|
108
|
|
|
$this->setUpRequestMock(); |
|
109
|
|
|
|
|
110
|
|
|
$response = new Response(); |
|
111
|
|
|
$this->setUpRequestHandlerMock($response); |
|
112
|
|
|
|
|
113
|
|
|
$result = $this->sessionMiddleware->process($this->requestMock, $this->requestHandlerMock); |
|
114
|
|
|
$this->assertEquals($response, $result); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function setUpRequestHandlerMock(ResponseInterface $response): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->requestHandlerMock |
|
120
|
|
|
->expects($this->once()) |
|
121
|
|
|
->method('handle') |
|
122
|
|
|
->willReturn($response); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function setUpSessionMock(bool $cookieDomainProvided = true, bool $isActive = true): void |
|
126
|
|
|
{ |
|
127
|
|
|
$this->sessionMock |
|
128
|
|
|
->expects($this->any()) |
|
129
|
|
|
->method('isActive') |
|
130
|
|
|
->willReturn($isActive); |
|
131
|
|
|
|
|
132
|
|
|
$this->sessionMock |
|
133
|
|
|
->expects($this->any()) |
|
134
|
|
|
->method('getName') |
|
135
|
|
|
->willReturn(self::SESSION_NAME); |
|
136
|
|
|
|
|
137
|
|
|
$this->sessionMock |
|
138
|
|
|
->expects($this->any()) |
|
139
|
|
|
->method('getID') |
|
140
|
|
|
->willReturn(self::CURRENT_SID); |
|
141
|
|
|
|
|
142
|
|
|
$cookieParams = self::COOKIE_PARAMETERS; |
|
143
|
|
|
if (!$cookieDomainProvided) { |
|
144
|
|
|
$cookieParams['domain'] = ''; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$this->sessionMock |
|
148
|
|
|
->expects($this->any()) |
|
149
|
|
|
->method('getCookieParameters') |
|
150
|
|
|
->willReturn($cookieParams); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
private function setUpRequestMock(bool $isConnectionSecure = true): void |
|
154
|
|
|
{ |
|
155
|
|
|
$uriScheme = $isConnectionSecure ? 'https' : 'http'; |
|
156
|
|
|
$this->setUpUriMock($uriScheme); |
|
157
|
|
|
|
|
158
|
|
|
$this->requestMock |
|
159
|
|
|
->expects($this->any()) |
|
160
|
|
|
->method('getUri') |
|
161
|
|
|
->willReturn($this->uriMock); |
|
162
|
|
|
|
|
163
|
|
|
$requestCookieParams = [ |
|
164
|
|
|
self::SESSION_NAME => self::REQUEST_SID, |
|
165
|
|
|
]; |
|
166
|
|
|
$this->requestMock |
|
167
|
|
|
->expects($this->any()) |
|
168
|
|
|
->method('getCookieParams') |
|
169
|
|
|
->willReturn($requestCookieParams); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
private function setUpUriMock(string $uriScheme): void |
|
173
|
|
|
{ |
|
174
|
|
|
$this->uriMock |
|
175
|
|
|
->expects($this->any()) |
|
176
|
|
|
->method('getScheme') |
|
177
|
|
|
->willReturn($uriScheme); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|