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
100
101
102
103
104
105
106
107
108
109
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Tracker;
use Piwik\Config;
use Piwik\Date;
use Piwik\Plugins\SitesManager\API;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tracker\Action;
use Piwik\Tracker\FingerprintSalt;
use Piwik\Tracker\PageUrl;
use Piwik\Tracker\Request;
use Piwik\Translate;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
* @group ActionTest
*/
class FingerprintSaltTest extends IntegrationTestCase
{
/**
* @var FingerprintSalt
*/
private $fingerprintSalt;
public function setUp(): void
{
parent::setUp();
$this->fingerprintSalt = new FingerprintSalt();
}
public function test_generateSalt()
{
$salt = $this->fingerprintSalt->generateSalt();
$this->assertEquals(32, strlen($salt));
$this->assertTrue(ctype_alnum($salt));
}
public function test_generateSalt_isRandom()
{
$this->assertNotSame($this->fingerprintSalt->generateSalt(), $this->fingerprintSalt->generateSalt());
}
public function test_getDateString()
{
$date = Date::factory('2020-05-05 14:04:05');
$this->assertSame('2020-05-06',$this->fingerprintSalt->getDateString($date, 'Pacific/Auckland'));
$this->assertSame('2020-05-05',$this->fingerprintSalt->getDateString($date, 'Europe/Berlin'));
}
public function test_getSalt_remembersSaltPerSite()
{
$salt05_1 = $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1);
$salt06_1 = $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1);
$salt05_2 = $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2);
$salt06_2 = $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 2);
$this->assertNotSame($salt05_1, $salt06_1);
$this->assertNotSame($salt05_2, $salt06_2);
$this->assertNotSame($salt06_1, $salt06_2);
$this->assertSame($salt05_1, $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1));
$this->assertSame($salt06_1, $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1));
$this->assertSame($salt05_2, $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2));
}
public function test_deleteOldSalts_whenNothingToDelete()
{
$this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1);
$this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1);
Date::$now = time() - FingerprintSalt::DELETE_FINGERPRINT_OLDER_THAN_SECONDS + 30;// they would expire in 30 seconds
$this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2);
$this->fingerprintSalt->getSalt('2020-05-06', $idSite = 2);
Date::$now = time();
$this->assertSame(array(), $this->fingerprintSalt->deleteOldSalts());
}
public function test_deleteOldSalts_someToBeDeleted()
{
$this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1);
Date::$now = time() - FingerprintSalt::DELETE_FINGERPRINT_OLDER_THAN_SECONDS - 30; // these entries should be expired
$this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1);
$this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2);
$this->fingerprintSalt->getSalt('2020-05-06', $idSite = 2);
Date::$now = time();
$this->assertSame(array(
'fingerprint_salt_1_2020-05-06',
'fingerprint_salt_2_2020-05-05',
'fingerprint_salt_2_2020-05-06'
), $this->fingerprintSalt->deleteOldSalts());
// executing it again wont delete anything
$this->assertSame(array(), $this->fingerprintSalt->deleteOldSalts());
}
}
|