2018-11-17 12:23:25 -06:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2018-11-17 12:23:25 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-09-23 12:03:32 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Visit\Entity;
|
2018-11-17 12:23:25 -06:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2018-11-17 12:26:07 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-05-08 05:58:49 -05:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2018-11-17 12:23:25 -06:00
|
|
|
|
|
|
|
class VisitTest extends TestCase
|
|
|
|
{
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideUserAgents')]
|
2021-05-22 08:09:14 -05:00
|
|
|
public function isProperlyJsonSerialized(string $userAgent, bool $expectedToBePotentialBot): void
|
2018-11-17 12:23:25 -06:00
|
|
|
{
|
2023-01-22 04:27:16 -06:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createFake(), new Visitor($userAgent, 'some site', '1.2.3.4', ''));
|
2018-11-17 12:23:25 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals([
|
2018-11-17 12:23:25 -06:00
|
|
|
'referer' => 'some site',
|
2021-02-07 14:31:12 -06:00
|
|
|
'date' => $visit->getDate()->toAtomString(),
|
2021-05-22 08:09:14 -05:00
|
|
|
'userAgent' => $userAgent,
|
2018-11-17 12:23:25 -06:00
|
|
|
'visitLocation' => null,
|
2021-05-22 08:09:14 -05:00
|
|
|
'potentialBot' => $expectedToBePotentialBot,
|
2018-11-17 12:23:25 -06:00
|
|
|
], $visit->jsonSerialize());
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideUserAgents(): iterable
|
2021-05-22 08:09:14 -05:00
|
|
|
{
|
|
|
|
yield 'Chrome' => [
|
|
|
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
|
|
|
|
false,
|
|
|
|
];
|
|
|
|
yield 'Firefox' => ['Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0', false];
|
|
|
|
yield 'Facebook' => ['cf-facebook', true];
|
|
|
|
yield 'Twitter' => ['IDG Twitter Links Resolver', true];
|
|
|
|
yield 'Guzzle' => ['guzzlehttp', true];
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideAddresses')]
|
2020-05-08 08:43:09 -05:00
|
|
|
public function addressIsAnonymizedWhenRequested(bool $anonymize, ?string $address, ?string $expectedAddress): void
|
2020-05-08 05:58:49 -05:00
|
|
|
{
|
2021-02-07 14:31:12 -06:00
|
|
|
$visit = Visit::forValidShortUrl(
|
2023-01-22 04:27:16 -06:00
|
|
|
ShortUrl::createFake(),
|
2021-02-07 14:31:12 -06:00
|
|
|
new Visitor('Chrome', 'some site', $address, ''),
|
|
|
|
$anonymize,
|
|
|
|
);
|
2020-05-08 05:58:49 -05:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedAddress, $visit->getRemoteAddr());
|
2020-05-08 05:58:49 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideAddresses(): iterable
|
2020-05-08 05:58:49 -05:00
|
|
|
{
|
2020-05-08 08:43:09 -05: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 05:58:49 -05:00
|
|
|
}
|
2018-11-17 12:23:25 -06:00
|
|
|
}
|