aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/security/SessionManagerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/security/SessionManagerTest.php')
-rw-r--r--tests/security/SessionManagerTest.php71
1 files changed, 62 insertions, 9 deletions
diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php
index f264505e..60695dcf 100644
--- a/tests/security/SessionManagerTest.php
+++ b/tests/security/SessionManagerTest.php
@@ -1,12 +1,8 @@
1<?php 1<?php
2require_once 'tests/utils/FakeConfigManager.php';
3 2
4// Initialize reference data _before_ PHPUnit starts a session 3namespace Shaarli\Security;
5require_once 'tests/utils/ReferenceSessionIdHashes.php';
6ReferenceSessionIdHashes::genAllHashes();
7 4
8use PHPUnit\Framework\TestCase; 5use PHPUnit\Framework\TestCase;
9use Shaarli\Security\SessionManager;
10 6
11/** 7/**
12 * Test coverage for SessionManager 8 * Test coverage for SessionManager
@@ -30,7 +26,7 @@ class SessionManagerTest extends TestCase
30 */ 26 */
31 public static function setUpBeforeClass() 27 public static function setUpBeforeClass()
32 { 28 {
33 self::$sidHashes = ReferenceSessionIdHashes::getHashes(); 29 self::$sidHashes = \ReferenceSessionIdHashes::getHashes();
34 } 30 }
35 31
36 /** 32 /**
@@ -38,13 +34,13 @@ class SessionManagerTest extends TestCase
38 */ 34 */
39 public function setUp() 35 public function setUp()
40 { 36 {
41 $this->conf = new FakeConfigManager([ 37 $this->conf = new \FakeConfigManager([
42 'credentials.login' => 'johndoe', 38 'credentials.login' => 'johndoe',
43 'credentials.salt' => 'salt', 39 'credentials.salt' => 'salt',
44 'security.session_protection_disabled' => false, 40 'security.session_protection_disabled' => false,
45 ]); 41 ]);
46 $this->session = []; 42 $this->session = [];
47 $this->sessionManager = new SessionManager($this->session, $this->conf); 43 $this->sessionManager = new SessionManager($this->session, $this->conf, 'session_path');
48 } 44 }
49 45
50 /** 46 /**
@@ -69,7 +65,7 @@ class SessionManagerTest extends TestCase
69 $token => 1, 65 $token => 1,
70 ], 66 ],
71 ]; 67 ];
72 $sessionManager = new SessionManager($session, $this->conf); 68 $sessionManager = new SessionManager($session, $this->conf, 'session_path');
73 69
74 // check and destroy the token 70 // check and destroy the token
75 $this->assertTrue($sessionManager->checkToken($token)); 71 $this->assertTrue($sessionManager->checkToken($token));
@@ -269,4 +265,61 @@ class SessionManagerTest extends TestCase
269 $this->session['ip'] = 'ip_id_one'; 265 $this->session['ip'] = 'ip_id_one';
270 $this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two')); 266 $this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two'));
271 } 267 }
268
269 /**
270 * Test creating an entry in the session array
271 */
272 public function testSetSessionParameterCreate(): void
273 {
274 $this->sessionManager->setSessionParameter('abc', 'def');
275
276 static::assertSame('def', $this->session['abc']);
277 }
278
279 /**
280 * Test updating an entry in the session array
281 */
282 public function testSetSessionParameterUpdate(): void
283 {
284 $this->session['abc'] = 'ghi';
285
286 $this->sessionManager->setSessionParameter('abc', 'def');
287
288 static::assertSame('def', $this->session['abc']);
289 }
290
291 /**
292 * Test updating an entry in the session array with null value
293 */
294 public function testSetSessionParameterUpdateNull(): void
295 {
296 $this->session['abc'] = 'ghi';
297
298 $this->sessionManager->setSessionParameter('abc', null);
299
300 static::assertArrayHasKey('abc', $this->session);
301 static::assertNull($this->session['abc']);
302 }
303
304 /**
305 * Test deleting an existing entry in the session array
306 */
307 public function testDeleteSessionParameter(): void
308 {
309 $this->session['abc'] = 'def';
310
311 $this->sessionManager->deleteSessionParameter('abc');
312
313 static::assertArrayNotHasKey('abc', $this->session);
314 }
315
316 /**
317 * Test deleting a non existent entry in the session array
318 */
319 public function testDeleteSessionParameterNotExisting(): void
320 {
321 $this->sessionManager->deleteSessionParameter('abc');
322
323 static::assertArrayNotHasKey('abc', $this->session);
324 }
272} 325}