blob: e9bbdd9f38b45a84df5309451f29511ce0862e28 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Tests\Unit\Session;
use Piwik\Session\SessionFingerprint;
class SessionFingerprintTest extends \PHPUnit_Framework_TestCase
{
const TEST_TIME_VALUE = 4567;
/**
* @var SessionFingerprint
*/
private $testInstance;
public function setUp()
{
parent::setUp();
$this->testInstance = new SessionFingerprint();
}
public function test_getUser_ReturnsUserNameSessionVar_WhenSessionVarIsSet()
{
$_SESSION[SessionFingerprint::USER_NAME_SESSION_VAR_NAME] = 'testuser';
$this->assertEquals('testuser', $this->testInstance->getUser());
}
public function test_getUser_ReturnsNull_WhenSessionVarIsNotSet()
{
$this->assertNull($this->testInstance->getUser());
}
public function test_getUserInfo_ReturnsUserInfoSessionVar_WhenSessionVarIsSet()
{
$sessionVarValue = [
'ip' => 'someip',
];
$_SESSION[SessionFingerprint::SESSION_INFO_SESSION_VAR_NAME] = $sessionVarValue;
$this->assertEquals($sessionVarValue, $this->testInstance->getUserInfo());
}
public function test_getUserInfo_ReturnsNull_WhenSessionVarIsNotSet()
{
$this->assertNull($this->testInstance->getUserInfo());
}
public function test_initialize_SetsSessionVarsToCurrentRequest()
{
$this->testInstance->initialize('testuser', self::TEST_TIME_VALUE);
$this->assertEquals('testuser', $_SESSION[SessionFingerprint::USER_NAME_SESSION_VAR_NAME]);
$this->assertEquals(
['ts' => self::TEST_TIME_VALUE],
$_SESSION[SessionFingerprint::SESSION_INFO_SESSION_VAR_NAME]
);
}
public function test_getSessionStartTime_()
{
$_SESSION[SessionFingerprint::SESSION_INFO_SESSION_VAR_NAME] = [
'ts' => 123.
];
$this->assertEquals(123, $this->testInstance->getSessionStartTime());
}
public function test_getSessionStartTime_ReturnsNull_IfThereIsNoSessionInfo()
{
$this->assertNull($this->testInstance->getSessionStartTime());
}
public function test_getSessionStartTime_ReturnsNull_IfThereIsNoSessionStartTime()
{
$_SESSION[SessionFingerprint::SESSION_INFO_SESSION_VAR_NAME] = [];
$this->assertNull($this->testInstance->getSessionStartTime());
}
public function test_destroy_RemovesSessionFingerprintSessionVars()
{
$_SESSION['someotherdata'] = 'somedata';
$_SESSION[SessionFingerprint::USER_NAME_SESSION_VAR_NAME] = 'someuser';
$_SESSION[SessionFingerprint::SESSION_INFO_SESSION_VAR_NAME] = [
'some' => 'data',
];
$this->testInstance->clear();
$this->assertEquals(['someotherdata' => 'somedata'], $_SESSION);
}
}
|