2018-11-17 21:23:25 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-11-17 21:23:25 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Entity;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
2018-11-17 21:26:07 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-05-08 13:58:49 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2018-11-17 21:23:25 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
|
|
|
|
|
|
|
class VisitTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDates
|
|
|
|
*/
|
2019-02-17 22:28:34 +03:00
|
|
|
public function isProperlyJsonSerialized(?Chronos $date): void
|
2018-11-17 21:23:25 +03:00
|
|
|
{
|
2020-05-08 13:58:49 +03:00
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('Chrome', 'some site', '1.2.3.4'), true, $date);
|
2018-11-17 21:23:25 +03:00
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'referer' => 'some site',
|
|
|
|
'date' => ($date ?? $visit->getDate())->toAtomString(),
|
|
|
|
'userAgent' => 'Chrome',
|
|
|
|
'visitLocation' => null,
|
|
|
|
], $visit->jsonSerialize());
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
public function provideDates(): iterable
|
2018-11-17 21:23:25 +03:00
|
|
|
{
|
2019-02-17 22:28:34 +03:00
|
|
|
yield 'null date' => [null];
|
|
|
|
yield 'not null date' => [Chronos::now()->subDays(10)];
|
2018-11-17 21:23:25 +03:00
|
|
|
}
|
2020-05-08 13:58:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideAddresses
|
|
|
|
*/
|
2020-05-08 16:43:09 +03:00
|
|
|
public function addressIsAnonymizedWhenRequested(bool $anonymize, ?string $address, ?string $expectedAddress): void
|
2020-05-08 13:58:49 +03:00
|
|
|
{
|
2020-05-08 16:43:09 +03:00
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('Chrome', 'some site', $address), $anonymize);
|
2020-05-08 13:58:49 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expectedAddress, $visit->getRemoteAddr());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideAddresses(): iterable
|
|
|
|
{
|
2020-05-08 16:43:09 +03:00
|
|
|
yield 'anonymized null address' => [true, null, null];
|
|
|
|
yield 'non-anonymized null address' => [false, null, null];
|
|
|
|
yield 'anonymized localhost' => [true, IpAddress::LOCALHOST, IpAddress::LOCALHOST];
|
|
|
|
yield 'non-anonymized localhost' => [false, IpAddress::LOCALHOST, IpAddress::LOCALHOST];
|
|
|
|
yield 'anonymized regular address' => [true, '1.2.3.4', '1.2.3.0'];
|
|
|
|
yield 'non-anonymized regular address' => [false, '1.2.3.4', '1.2.3.4'];
|
2020-05-08 13:58:49 +03:00
|
|
|
}
|
2018-11-17 21:23:25 +03:00
|
|
|
}
|