1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Entity; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress; |
10
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
11
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit; |
12
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor; |
13
|
|
|
|
14
|
|
|
class VisitTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
* @dataProvider provideDates |
19
|
|
|
*/ |
20
|
|
|
public function isProperlyJsonSerialized(?Chronos $date): void |
21
|
|
|
{ |
22
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('Chrome', 'some site', '1.2.3.4'), true, $date); |
23
|
|
|
|
24
|
|
|
$this->assertEquals([ |
25
|
|
|
'referer' => 'some site', |
26
|
|
|
'date' => ($date ?? $visit->getDate())->toAtomString(), |
27
|
|
|
'userAgent' => 'Chrome', |
28
|
|
|
'visitLocation' => null, |
29
|
|
|
], $visit->jsonSerialize()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function provideDates(): iterable |
33
|
|
|
{ |
34
|
|
|
yield 'null date' => [null]; |
35
|
|
|
yield 'not null date' => [Chronos::now()->subDays(10)]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
* @dataProvider provideAddresses |
41
|
|
|
*/ |
42
|
|
|
public function addressIsAnonymizedWhenRequested(bool $anonymize, ?string $address, ?string $expectedAddress): void |
43
|
|
|
{ |
44
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('Chrome', 'some site', $address), $anonymize); |
45
|
|
|
|
46
|
|
|
$this->assertEquals($expectedAddress, $visit->getRemoteAddr()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function provideAddresses(): iterable |
50
|
|
|
{ |
51
|
|
|
yield 'anonymized null address' => [true, null, null]; |
52
|
|
|
yield 'non-anonymized null address' => [false, null, null]; |
53
|
|
|
yield 'anonymized localhost' => [true, IpAddress::LOCALHOST, IpAddress::LOCALHOST]; |
54
|
|
|
yield 'non-anonymized localhost' => [false, IpAddress::LOCALHOST, IpAddress::LOCALHOST]; |
55
|
|
|
yield 'anonymized regular address' => [true, '1.2.3.4', '1.2.3.0']; |
56
|
|
|
yield 'non-anonymized regular address' => [false, '1.2.3.4', '1.2.3.4']; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|