2019-07-13 13:04:21 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-07-13 13:04:21 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-12-28 15:07:11 +03:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-07-20 13:11:07 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
|
|
|
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
2021-02-09 22:25:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
|
2021-01-17 13:42:35 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
2021-02-09 22:25:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\LocateVisit;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2019-08-11 00:26:39 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2019-08-10 14:56:06 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
class LocateVisitTest extends TestCase
|
2019-07-13 13:04:21 +03:00
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
private LocateVisit $locateVisit;
|
2019-12-30 00:48:40 +03:00
|
|
|
private ObjectProphecy $ipLocationResolver;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $logger;
|
|
|
|
private ObjectProphecy $dbUpdater;
|
|
|
|
private ObjectProphecy $eventDispatcher;
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->ipLocationResolver = $this->prophesize(IpLocationResolverInterface::class);
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
$this->logger = $this->prophesize(LoggerInterface::class);
|
2019-07-20 13:11:07 +03:00
|
|
|
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
2019-12-28 15:07:11 +03:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
$this->locateVisit = new LocateVisit(
|
2019-07-13 13:04:21 +03:00
|
|
|
$this->ipLocationResolver->reveal(),
|
|
|
|
$this->em->reveal(),
|
2019-07-20 13:11:07 +03:00
|
|
|
$this->logger->reveal(),
|
2019-12-28 15:07:11 +03:00
|
|
|
$this->dbUpdater->reveal(),
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->eventDispatcher->reveal(),
|
2019-07-13 13:04:21 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function invalidVisitLogsWarning(): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-13 13:04:21 +03:00
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn(null);
|
2019-12-27 18:15:14 +03:00
|
|
|
$logWarning = $this->logger->warning('Tried to locate visit with id "{visitId}", but it does not exist.', [
|
|
|
|
'visitId' => 123,
|
|
|
|
]);
|
2020-01-01 22:48:31 +03:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 15:07:11 +03:00
|
|
|
});
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
2019-11-20 22:03:06 +03:00
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
2019-07-13 13:04:21 +03:00
|
|
|
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
|
|
$logWarning->shouldHaveBeenCalled();
|
2019-12-28 15:07:11 +03:00
|
|
|
$dispatch->shouldNotHaveBeenCalled();
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|
|
|
|
|
2019-07-13 16:43:54 +03:00
|
|
|
/** @test */
|
|
|
|
public function invalidAddressLogsWarning(): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-13 16:43:54 +03:00
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn(
|
2021-02-07 23:31:12 +03:00
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
2019-07-13 16:43:54 +03:00
|
|
|
);
|
|
|
|
$resolveLocation = $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->willThrow(
|
2020-01-01 22:48:31 +03:00
|
|
|
WrongIpException::class,
|
2019-07-13 16:43:54 +03:00
|
|
|
);
|
|
|
|
$logWarning = $this->logger->warning(
|
2019-12-27 18:15:14 +03:00
|
|
|
Argument::containingString('Tried to locate visit with id "{visitId}", but its address seems to be wrong.'),
|
2020-01-01 22:48:31 +03:00
|
|
|
Argument::type('array'),
|
2019-07-13 16:43:54 +03:00
|
|
|
);
|
2020-01-01 22:48:31 +03:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 15:07:11 +03:00
|
|
|
});
|
2019-07-13 16:43:54 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveLocation->shouldHaveBeenCalledOnce();
|
|
|
|
$logWarning->shouldHaveBeenCalled();
|
2019-11-20 22:03:06 +03:00
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
2019-12-28 15:07:11 +03:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-13 16:43:54 +03:00
|
|
|
}
|
|
|
|
|
2019-07-13 13:04:21 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideNonLocatableVisits
|
|
|
|
*/
|
|
|
|
public function nonLocatableVisitsResolveToEmptyLocations(Visit $visit): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-13 13:04:21 +03:00
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
2020-01-01 22:48:31 +03:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2019-07-13 13:04:21 +03:00
|
|
|
});
|
|
|
|
$resolveIp = $this->ipLocationResolver->resolveIpLocation(Argument::any());
|
2020-01-01 22:48:31 +03:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 15:07:11 +03:00
|
|
|
});
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($visit->getVisitLocation(), new VisitLocation(Location::emptyInstance()));
|
2019-07-13 13:04:21 +03:00
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$flush->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIp->shouldNotHaveBeenCalled();
|
|
|
|
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
2019-12-28 15:07:11 +03:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideNonLocatableVisits(): iterable
|
|
|
|
{
|
2021-01-30 16:18:44 +03:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2021-02-07 23:31:12 +03:00
|
|
|
yield 'null IP' => [Visit::forValidShortUrl($shortUrl, new Visitor('', '', null, ''))];
|
|
|
|
yield 'empty IP' => [Visit::forValidShortUrl($shortUrl, new Visitor('', '', '', ''))];
|
|
|
|
yield 'localhost' => [Visit::forValidShortUrl($shortUrl, new Visitor('', '', IpAddress::LOCALHOST, ''))];
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:12:30 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideIpAddresses
|
|
|
|
*/
|
2021-02-09 22:25:28 +03:00
|
|
|
public function locatableVisitsResolveToLocation(
|
|
|
|
Visit $visit,
|
|
|
|
?string $originalIpAddress,
|
|
|
|
int $expectedDispatchCalls
|
|
|
|
): void {
|
|
|
|
$ipAddr = $originalIpAddress ?? $visit->getRemoteAddr();
|
2019-07-13 13:04:21 +03:00
|
|
|
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123', $originalIpAddress);
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
2020-01-01 22:48:31 +03:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2019-07-13 13:04:21 +03:00
|
|
|
});
|
|
|
|
$resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
2020-01-01 22:48:31 +03:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 15:07:11 +03:00
|
|
|
});
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($visit->getVisitLocation(), new VisitLocation($location));
|
2019-07-13 13:04:21 +03:00
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$flush->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIp->shouldHaveBeenCalledOnce();
|
|
|
|
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
2021-02-09 22:25:28 +03:00
|
|
|
$dispatch->shouldHaveBeenCalledTimes($expectedDispatchCalls);
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|
2019-07-20 13:11:07 +03:00
|
|
|
|
2020-03-22 13:12:30 +03:00
|
|
|
public function provideIpAddresses(): iterable
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
yield 'no original IP address' => [
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
null,
|
|
|
|
1,
|
|
|
|
];
|
|
|
|
yield 'original IP address' => [
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
'1.2.3.4',
|
|
|
|
1,
|
|
|
|
];
|
|
|
|
yield 'base url' => [Visit::forBasePath(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4', 0];
|
|
|
|
yield 'invalid short url' => [Visit::forInvalidShortUrl(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4', 0];
|
|
|
|
yield 'regular not found' => [Visit::forRegularNotFound(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4', 0];
|
2020-03-22 13:12:30 +03:00
|
|
|
}
|
|
|
|
|
2019-07-20 13:11:07 +03:00
|
|
|
/** @test */
|
2019-12-27 18:15:14 +03:00
|
|
|
public function errorWhenUpdatingGeoLiteWithExistingCopyLogsWarning(): void
|
2019-07-20 13:11:07 +03:00
|
|
|
{
|
2021-02-06 23:37:58 +03:00
|
|
|
$e = GeolocationDbUpdateFailedException::withOlderDb();
|
2019-07-20 13:11:07 +03:00
|
|
|
$ipAddr = '1.2.3.0';
|
2021-02-07 23:31:12 +03:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $ipAddr, ''));
|
2019-07-20 13:11:07 +03:00
|
|
|
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-20 13:11:07 +03:00
|
|
|
|
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
2020-01-01 22:48:31 +03:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2019-07-20 13:11:07 +03:00
|
|
|
});
|
|
|
|
$resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
|
|
|
$checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
2020-01-01 22:48:31 +03:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 15:07:11 +03:00
|
|
|
});
|
2019-07-20 13:11:07 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($visit->getVisitLocation(), new VisitLocation($location));
|
2019-07-20 13:11:07 +03:00
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$flush->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIp->shouldHaveBeenCalledOnce();
|
|
|
|
$checkUpdateDb->shouldHaveBeenCalledOnce();
|
|
|
|
$this->logger->warning(
|
|
|
|
'GeoLite2 database update failed. Proceeding with old version. {e}',
|
2020-01-01 22:48:31 +03:00
|
|
|
['e' => $e],
|
2019-07-20 13:11:07 +03:00
|
|
|
)->shouldHaveBeenCalledOnce();
|
2019-12-28 15:07:11 +03:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-20 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
2019-12-27 18:15:14 +03:00
|
|
|
public function errorWhenDownloadingGeoLiteCancelsLocation(): void
|
2019-07-20 13:11:07 +03:00
|
|
|
{
|
2021-02-06 23:37:58 +03:00
|
|
|
$e = GeolocationDbUpdateFailedException::withoutOlderDb();
|
2019-07-20 13:11:07 +03:00
|
|
|
$ipAddr = '1.2.3.0';
|
2021-02-07 23:31:12 +03:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $ipAddr, ''));
|
2019-07-20 13:11:07 +03:00
|
|
|
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-20 13:11:07 +03:00
|
|
|
|
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
2020-01-01 22:48:31 +03:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2019-07-20 13:11:07 +03:00
|
|
|
});
|
|
|
|
$resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
|
|
|
$checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
|
|
|
$logError = $this->logger->error(
|
2019-12-27 18:15:14 +03:00
|
|
|
'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
|
2020-01-01 22:48:31 +03:00
|
|
|
['e' => $e, 'visitId' => 123],
|
2019-07-20 13:11:07 +03:00
|
|
|
);
|
2020-01-01 22:48:31 +03:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 15:07:11 +03:00
|
|
|
});
|
2019-07-20 13:11:07 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertNull($visit->getVisitLocation());
|
2019-07-20 13:11:07 +03:00
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$flush->shouldNotHaveBeenCalled();
|
|
|
|
$resolveIp->shouldNotHaveBeenCalled();
|
|
|
|
$checkUpdateDb->shouldHaveBeenCalledOnce();
|
|
|
|
$logError->shouldHaveBeenCalledOnce();
|
2019-12-28 15:07:11 +03:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-20 13:11:07 +03:00
|
|
|
}
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|