2024-11-15 10:17:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Geolocation\Middleware;
|
|
|
|
|
|
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
|
use PHPUnit\Framework\Assert;
|
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
|
|
|
use PHPUnit\Framework\Attributes\TestWith;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use RuntimeException;
|
|
|
|
|
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
|
2026-11-03 20:24:55 +01:00
|
|
|
use Shlinkio\Shlink\Core\Geolocation\Middleware\GeolocationMiddleware;
|
2026-11-02 14:00:46 +01:00
|
|
|
use Shlinkio\Shlink\Geolocation\Exception\LocationResolutionException;
|
|
|
|
|
use Shlinkio\Shlink\Geolocation\Model\Location;
|
|
|
|
|
use Shlinkio\Shlink\Geolocation\Resolver\LocationResolverInterface;
|
2024-11-15 10:17:09 +01:00
|
|
|
use Throwable;
|
|
|
|
|
|
2026-11-02 14:00:46 +01:00
|
|
|
use const Shlinkio\Shlink\Geolocation\IP_ADDRESS_REQUEST_ATTRIBUTE;
|
2024-11-22 08:59:42 +01:00
|
|
|
|
2026-11-03 20:24:55 +01:00
|
|
|
class GeolocationMiddlewareTest extends TestCase
|
2024-11-15 10:17:09 +01:00
|
|
|
{
|
2026-11-02 14:00:46 +01:00
|
|
|
private MockObject&LocationResolverInterface $ipLocationResolver;
|
2026-05-22 08:42:47 +02:00
|
|
|
private MockObject&LoggerInterface $logger;
|
|
|
|
|
private MockObject&RequestHandlerInterface $handler;
|
2024-11-15 10:17:09 +01:00
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
2026-11-02 14:00:46 +01:00
|
|
|
$this->ipLocationResolver = $this->createMock(LocationResolverInterface::class);
|
2024-11-15 10:17:09 +01:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
|
public function geolocationIsSkippedIfTrackingIsDisabled(): void
|
|
|
|
|
{
|
2026-11-02 14:00:46 +01:00
|
|
|
$this->ipLocationResolver->expects($this->never())->method('resolveRequestLocation');
|
2024-11-15 10:17:09 +01:00
|
|
|
$this->logger->expects($this->never())->method('warning');
|
|
|
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals();
|
|
|
|
|
$this->handler->expects($this->once())->method('handle')->with($request);
|
|
|
|
|
|
|
|
|
|
$this->middleware(disableTracking: true)->process($request, $this->handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
|
public function locationIsResolvedFromIpAddress(): void
|
|
|
|
|
{
|
2026-11-02 14:00:46 +01:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4');
|
|
|
|
|
|
2026-05-22 08:42:47 +02:00
|
|
|
$this->ipLocationResolver
|
|
|
|
|
->expects($this->once())
|
2026-11-02 14:00:46 +01:00
|
|
|
->method('resolveRequestLocation')
|
|
|
|
|
->with($request)
|
|
|
|
|
->willReturn(new Location(countryCode: 'ES'));
|
2024-11-15 10:17:09 +01:00
|
|
|
$this->logger->expects($this->never())->method('warning');
|
|
|
|
|
|
2026-05-22 08:42:47 +02:00
|
|
|
$this->handler
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('handle')
|
|
|
|
|
->with($this->callback(
|
|
|
|
|
static function (ServerRequestInterface $req): bool {
|
|
|
|
|
$location = $req->getAttribute(Location::class);
|
|
|
|
|
if (!$location instanceof Location) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert::assertEquals('ES', $location->countryCode);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
));
|
2024-11-15 10:17:09 +01:00
|
|
|
|
|
|
|
|
$this->middleware()->process($request, $this->handler);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 19:33:52 +01:00
|
|
|
/**
|
|
|
|
|
* @param non-empty-string $loggerMethod
|
|
|
|
|
*/
|
2024-11-15 10:17:09 +01:00
|
|
|
#[Test]
|
|
|
|
|
#[TestWith([
|
2026-11-02 14:00:46 +01:00
|
|
|
new LocationResolutionException(),
|
2024-11-15 10:17:09 +01:00
|
|
|
'warning',
|
2026-11-03 20:21:23 +01:00
|
|
|
'Tried to geolocate visitor, but it was not possible. {e}',
|
2024-11-15 10:17:09 +01:00
|
|
|
])]
|
|
|
|
|
#[TestWith([
|
|
|
|
|
new RuntimeException('Unknown'),
|
|
|
|
|
'error',
|
|
|
|
|
'An unexpected error occurred while trying to locate IP address. {e}',
|
|
|
|
|
])]
|
|
|
|
|
public function warningIsPrintedIfAnErrorOccurs(
|
|
|
|
|
Throwable $exception,
|
|
|
|
|
string $loggerMethod,
|
|
|
|
|
string $expectedLoggedMessage,
|
|
|
|
|
): void {
|
2026-11-02 14:00:46 +01:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4');
|
|
|
|
|
|
2024-11-15 10:17:09 +01:00
|
|
|
$this->ipLocationResolver
|
|
|
|
|
->expects($this->once())
|
2026-11-02 14:00:46 +01:00
|
|
|
->method('resolveRequestLocation')
|
|
|
|
|
->with($request)
|
2024-11-15 10:17:09 +01:00
|
|
|
->willThrowException($exception);
|
|
|
|
|
$this->logger->expects($this->once())->method($loggerMethod)->with($expectedLoggedMessage, ['e' => $exception]);
|
|
|
|
|
|
2026-05-22 08:42:47 +02:00
|
|
|
$this->handler
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('handle')
|
|
|
|
|
->with($this->callback(
|
|
|
|
|
static function (ServerRequestInterface $req): bool {
|
|
|
|
|
$location = $req->getAttribute(Location::class);
|
|
|
|
|
if (!$location instanceof Location) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert::assertEmpty($location->countryCode);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
));
|
2024-11-15 10:17:09 +01:00
|
|
|
|
|
|
|
|
$this->middleware()->process($request, $this->handler);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-03 20:24:55 +01:00
|
|
|
private function middleware(bool $disableTracking = false): GeolocationMiddleware
|
2024-11-15 10:17:09 +01:00
|
|
|
{
|
2026-11-03 20:24:55 +01:00
|
|
|
return new GeolocationMiddleware(
|
2024-11-15 10:17:09 +01:00
|
|
|
$this->ipLocationResolver,
|
|
|
|
|
$this->logger,
|
|
|
|
|
new TrackingOptions(disableTracking: $disableTracking),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|