|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use Page; |
|
6
|
|
|
use Smr\Container\DiContainer; |
|
7
|
|
|
use Smr\Session; |
|
8
|
|
|
use SmrAccount; |
|
9
|
|
|
use SmrTest\BaseIntegrationSpec; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @covers Smr\Session |
|
13
|
|
|
*/ |
|
14
|
|
|
class SessionIntegrationTest extends BaseIntegrationSpec { |
|
15
|
|
|
|
|
16
|
|
|
private Session $session; |
|
17
|
|
|
|
|
18
|
|
|
protected function tablesToTruncate(): array { |
|
19
|
|
|
return ['debug']; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function setUp(): void { |
|
23
|
|
|
// Start each test with a fresh container (and Smr\Session). |
|
24
|
|
|
// This ensures the independence of each test. |
|
25
|
|
|
DiContainer::initialize(false); |
|
26
|
|
|
$this->session = Session::getInstance(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function tearDown(): void { |
|
30
|
|
|
// Clear superglobals to avoid impacting other tests |
|
31
|
|
|
$_REQUEST = []; |
|
32
|
|
|
$_COOKIE = []; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function test_game(): void { |
|
36
|
|
|
// Sessions are initialized with no game |
|
37
|
|
|
self::assertFalse($this->session->hasGame()); |
|
38
|
|
|
self::assertSame(0, $this->session->getGameID()); |
|
39
|
|
|
|
|
40
|
|
|
// Now update the game |
|
41
|
|
|
$gameID = 3; |
|
42
|
|
|
$this->session->updateGame($gameID); |
|
43
|
|
|
self::assertTrue($this->session->hasGame()); |
|
44
|
|
|
self::assertSame($gameID, $this->session->getGameID()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function test_account(): void { |
|
48
|
|
|
// Sessions are initialized with no account |
|
49
|
|
|
self::assertFalse($this->session->hasAccount()); |
|
50
|
|
|
self::assertSame(0, $this->session->getAccountID()); |
|
51
|
|
|
|
|
52
|
|
|
// Now update the account |
|
53
|
|
|
$account = $this->createMock(SmrAccount::class); |
|
54
|
|
|
$account |
|
55
|
|
|
->method('getAccountID') |
|
56
|
|
|
->willReturn(7); |
|
57
|
|
|
$this->session->setAccount($account); |
|
58
|
|
|
self::assertTrue($this->session->hasAccount()); |
|
59
|
|
|
self::assertSame(7, $this->session->getAccountID()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function test_getSN(): void { |
|
63
|
|
|
// If there is no 'sn' parameter of the $_REQUEST superglobal, |
|
64
|
|
|
// then we get an empty SN. |
|
65
|
|
|
self::assertSame('', $this->session->getSN()); |
|
66
|
|
|
|
|
67
|
|
|
// Now create a new Session with a specific 'sn' parameter set. |
|
68
|
|
|
$sn = 'some_sn'; |
|
69
|
|
|
$_REQUEST['sn'] = $sn; |
|
70
|
|
|
$session = DiContainer::make(Session::class); |
|
71
|
|
|
self::assertSame($sn, $session->getSN()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function test_getSessionID(): void { |
|
75
|
|
|
// The default Session ID is a random 32-length string |
|
76
|
|
|
self::assertSame(32, strlen($this->session->getSessionID())); |
|
77
|
|
|
|
|
78
|
|
|
// Create a Session with a specific ID |
|
79
|
|
|
$sessionID = md5('hello'); |
|
80
|
|
|
$_COOKIE['session_id'] = $sessionID; |
|
81
|
|
|
$session = DiContainer::make(Session::class); |
|
82
|
|
|
self::assertSame($sessionID, $session->getSessionID()); |
|
83
|
|
|
|
|
84
|
|
|
// If we try to use a session ID with fewer than 32 chars, |
|
85
|
|
|
// we get a random ID instead |
|
86
|
|
|
$sessionID = 'hello'; |
|
87
|
|
|
$_COOKIE['session_id'] = $sessionID; |
|
88
|
|
|
$session = DiContainer::make(Session::class); |
|
89
|
|
|
self::assertNotEquals($sessionID, $session->getSessionID()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function test_ajax(): void { |
|
93
|
|
|
// If $_REQUEST is empty, ajax is false |
|
94
|
|
|
self::assertFalse($this->session->ajax); |
|
95
|
|
|
|
|
96
|
|
|
// Test other values in $_REQUEST |
|
97
|
|
|
$_REQUEST['ajax'] = 1; |
|
98
|
|
|
self::assertTrue(DiContainer::make(Session::class)->ajax); |
|
99
|
|
|
$_REQUEST['ajax'] = 'anything other than 1'; |
|
100
|
|
|
self::assertFalse(DiContainer::make(Session::class)->ajax); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function test_current_var(): void { |
|
104
|
|
|
// With an empty session, there should be no current var |
|
105
|
|
|
self::assertFalse($this->session->hasCurrentVar()); |
|
106
|
|
|
|
|
107
|
|
|
// Add a page to the session so that we can find it later. |
|
108
|
|
|
// (This mimics Page::href but with better access to the SN.) |
|
109
|
|
|
$page = Page::create('some_page'); |
|
110
|
|
|
$sn = $this->session->addLink($page); |
|
111
|
|
|
$sessionID = $this->session->getSessionID(); // needed for later |
|
112
|
|
|
$this->session->update(); |
|
113
|
|
|
|
|
114
|
|
|
// Create a new Session, requesting the SN we just made |
|
115
|
|
|
$_REQUEST['sn'] = $sn; |
|
116
|
|
|
$_COOKIE['session_id'] = $sessionID; |
|
117
|
|
|
$session = DiContainer::make(Session::class); |
|
118
|
|
|
|
|
119
|
|
|
// Now we should be able to find this sn in the var |
|
120
|
|
|
self::assertTrue($session->hasCurrentVar()); |
|
121
|
|
|
|
|
122
|
|
|
// The current var should now be accessible |
|
123
|
|
|
$var = $session->getCurrentVar(); |
|
124
|
|
|
self::assertSame('some_page', $var->file); |
|
125
|
|
|
|
|
126
|
|
|
// We can now change the current var |
|
127
|
|
|
$page2 = Page::create('another_page'); |
|
128
|
|
|
$session->setCurrentVar($page2); |
|
129
|
|
|
// Old references to $var should not be modified |
|
130
|
|
|
self::assertSame('some_page', $var->file); |
|
131
|
|
|
// But a new reference to $var should be updated |
|
132
|
|
|
$var2 = $session->getCurrentVar(); |
|
133
|
|
|
self::assertSame('another_page', $var2->file); |
|
134
|
|
|
|
|
135
|
|
|
// If we make a new session, but keep the same SN (e.g. ajax), |
|
136
|
|
|
// we should still get the updated var, even though it wasn't |
|
137
|
|
|
// the one originally associated with this SN. |
|
138
|
|
|
$session->update(); |
|
139
|
|
|
$_REQUEST['ajax'] = 1; |
|
140
|
|
|
$session = DiContainer::make(Session::class); |
|
141
|
|
|
self::assertEquals($var2, $session->getCurrentVar()); |
|
142
|
|
|
|
|
143
|
|
|
// If we destroy the Session, then the current var should no longer |
|
144
|
|
|
// be accessible to a new Session. |
|
145
|
|
|
$session->destroy(); |
|
146
|
|
|
$session = DiContainer::make(Session::class); |
|
147
|
|
|
self::assertFalse($session->hasCurrentVar()); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function test_addLink(): void { |
|
151
|
|
|
// If we add two different pages, we should get different SNs. |
|
152
|
|
|
$page = Page::create('some_page'); |
|
153
|
|
|
$sn = $this->session->addLink($page); |
|
154
|
|
|
|
|
155
|
|
|
$page2 = Page::create('another_page'); |
|
156
|
|
|
self::assertNotEquals($sn, $this->session->addLink($page2)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function test_addLink_page_already_added(): void { |
|
160
|
|
|
// If we add the same page object twice, it will give the same SN. |
|
161
|
|
|
$page = Page::create('some_page'); |
|
162
|
|
|
$sn = $this->session->addLink($page); |
|
163
|
|
|
self::assertSame($sn, $this->session->addLink($page)); |
|
164
|
|
|
|
|
165
|
|
|
// This works if the pages are equal, but not the same object. |
|
166
|
|
|
$page2 = clone $page; |
|
167
|
|
|
self::assertNotSame($page, $page2); |
|
168
|
|
|
self::assertSame($sn, $this->session->addLink($page2)); |
|
169
|
|
|
|
|
170
|
|
|
// It also works if we modify the page object (though this isn't |
|
171
|
|
|
// recommended, we clone when adding from Page::href to avoid this). |
|
172
|
|
|
$page['bla'] = true; |
|
173
|
|
|
self::assertSame($sn, $this->session->addLink($page)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function test_clearLinks(): void { |
|
177
|
|
|
srand(0); // seed rng to avoid getting the same random SN twice |
|
178
|
|
|
$page = Page::create('some_page'); |
|
179
|
|
|
$sn = $this->session->addLink($page); |
|
180
|
|
|
|
|
181
|
|
|
// After clearing links, the same page will return a different SN. |
|
182
|
|
|
$this->session->clearLinks(); |
|
183
|
|
|
self::assertNotSame($sn, $this->session->addLink($page)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function test_getRequestVar(): void { |
|
187
|
|
|
// Initialize the current var so that we can update it |
|
188
|
|
|
$page = Page::create('some_page'); |
|
189
|
|
|
$this->session->setCurrentVar($page); |
|
190
|
|
|
|
|
191
|
|
|
// Prepare request values |
|
192
|
|
|
$_REQUEST = [ |
|
193
|
|
|
'str' => 'foo', |
|
194
|
|
|
'int' => 4, |
|
195
|
|
|
'arr' => [5, 6], |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
// Check the following conditions: |
|
199
|
|
|
// 1. The index is not set in the current var beforehand |
|
200
|
|
|
// 2. We return the expected value from getRequestVar |
|
201
|
|
|
// 3. The value is stored in the current var afterwards |
|
202
|
|
|
self::assertArrayNotHasKey('str', $this->session->getCurrentVar()); |
|
203
|
|
|
self::assertSame('foo', $this->session->getRequestVar('str')); |
|
204
|
|
|
self::assertSame('foo', $this->session->getCurrentVar()['str']); |
|
205
|
|
|
|
|
206
|
|
|
self::assertArrayNotHasKey('int', $this->session->getCurrentVar()); |
|
207
|
|
|
self::assertSame(4, $this->session->getRequestVarInt('int')); |
|
208
|
|
|
self::assertSame(4, $this->session->getCurrentVar()['int']); |
|
209
|
|
|
|
|
210
|
|
|
self::assertArrayNotHasKey('arr', $this->session->getCurrentVar()); |
|
211
|
|
|
self::assertSame([5, 6], $this->session->getRequestVarIntArray('arr')); |
|
212
|
|
|
self::assertSame([5, 6], $this->session->getCurrentVar()['arr']); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|