2021-07-15 10:43:29 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Visit;
|
|
|
|
|
|
|
|
use Fig\Http\Message\RequestMethodInterface;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 13:45:56 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-07-15 10:43:29 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2021-10-10 15:00:22 -05:00
|
|
|
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
2024-10-16 01:55:38 -05:00
|
|
|
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
|
2021-07-15 10:43:29 -05:00
|
|
|
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2021-07-15 10:43:29 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\RequestTracker;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
|
|
|
|
|
|
|
class RequestTrackerTest extends TestCase
|
|
|
|
{
|
|
|
|
private const LONG_URL = 'https://domain.com/foo/bar?some=thing';
|
|
|
|
|
|
|
|
private RequestTracker $requestTracker;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & VisitsTrackerInterface $visitsTracker;
|
|
|
|
private MockObject & NotFoundType $notFoundType;
|
2021-07-15 10:43:29 -05:00
|
|
|
private ServerRequestInterface $request;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->visitsTracker = $this->createMock(VisitsTrackerInterface::class);
|
2021-07-15 10:43:29 -05:00
|
|
|
$this->requestTracker = new RequestTracker(
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->visitsTracker,
|
2022-09-17 05:57:04 -05:00
|
|
|
new TrackingOptions(
|
|
|
|
disableTrackParam: 'foobar',
|
|
|
|
disableTrackingFrom: ['80.90.100.110', '192.168.10.0/24', '1.2.*.*'],
|
|
|
|
),
|
2021-07-15 10:43:29 -05:00
|
|
|
);
|
|
|
|
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->notFoundType = $this->createMock(NotFoundType::class);
|
2021-07-15 10:43:29 -05:00
|
|
|
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
NotFoundType::class,
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->notFoundType,
|
2021-07-15 10:43:29 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideNonTrackingRequests')]
|
2021-07-15 10:43:29 -05:00
|
|
|
public function trackingIsDisabledWhenRequestDoesNotMeetConditions(ServerRequestInterface $request): void
|
|
|
|
{
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->visitsTracker->expects($this->never())->method('track');
|
2021-07-15 10:43:29 -05:00
|
|
|
|
2022-10-23 13:45:56 -05:00
|
|
|
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
2021-07-15 10:43:29 -05:00
|
|
|
$this->requestTracker->trackIfApplicable($shortUrl, $request);
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideNonTrackingRequests(): iterable
|
2021-07-15 10:43:29 -05:00
|
|
|
{
|
|
|
|
yield 'forwarded from head' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE,
|
|
|
|
RequestMethodInterface::METHOD_HEAD,
|
|
|
|
)];
|
|
|
|
yield 'disable track param' => [ServerRequestFactory::fromGlobals()->withQueryParams(['foobar' => 'foo'])];
|
|
|
|
yield 'disable track param as null' => [
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['foobar' => null]),
|
|
|
|
];
|
2021-10-10 15:00:22 -05:00
|
|
|
yield 'exact remote address' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
|
|
|
'80.90.100.110',
|
|
|
|
)];
|
|
|
|
yield 'matching wildcard remote address' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
|
|
|
'1.2.3.4',
|
|
|
|
)];
|
|
|
|
yield 'matching CIDR block remote address' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
|
|
|
'192.168.10.100',
|
|
|
|
)];
|
2021-07-15 10:43:29 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-07-15 10:43:29 -05:00
|
|
|
public function trackingHappensOverShortUrlsWhenRequestMeetsConditions(): void
|
|
|
|
{
|
|
|
|
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->visitsTracker->expects($this->once())->method('track')->with(
|
|
|
|
$shortUrl,
|
|
|
|
$this->isInstanceOf(Visitor::class),
|
|
|
|
);
|
2021-07-15 10:43:29 -05:00
|
|
|
|
|
|
|
$this->requestTracker->trackIfApplicable($shortUrl, $this->request);
|
|
|
|
}
|
|
|
|
|
2024-07-06 03:12:05 -05:00
|
|
|
#[Test]
|
|
|
|
public function trackingHappensOverShortUrlsWhenRemoteAddressIsInvalid(): void
|
|
|
|
{
|
|
|
|
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
|
|
|
$this->visitsTracker->expects($this->once())->method('track')->with(
|
|
|
|
$shortUrl,
|
|
|
|
$this->isInstanceOf(Visitor::class),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->requestTracker->trackIfApplicable($shortUrl, ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
|
|
|
'invalid',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-07-15 10:43:29 -05:00
|
|
|
public function baseUrlErrorIsTracked(): void
|
|
|
|
{
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->notFoundType->expects($this->once())->method('isBaseUrl')->willReturn(true);
|
|
|
|
$this->notFoundType->expects($this->never())->method('isRegularNotFound');
|
|
|
|
$this->notFoundType->expects($this->never())->method('isInvalidShortUrl');
|
|
|
|
$this->visitsTracker->expects($this->once())->method('trackBaseUrlVisit')->with(
|
|
|
|
$this->isInstanceOf(Visitor::class),
|
|
|
|
);
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackRegularNotFoundVisit');
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackInvalidShortUrlVisit');
|
2021-07-15 10:43:29 -05:00
|
|
|
|
|
|
|
$this->requestTracker->trackNotFoundIfApplicable($this->request);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-07-15 10:43:29 -05:00
|
|
|
public function regularNotFoundErrorIsTracked(): void
|
|
|
|
{
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->notFoundType->expects($this->once())->method('isBaseUrl')->willReturn(false);
|
|
|
|
$this->notFoundType->expects($this->once())->method('isRegularNotFound')->willReturn(true);
|
|
|
|
$this->notFoundType->expects($this->never())->method('isInvalidShortUrl');
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackBaseUrlVisit');
|
|
|
|
$this->visitsTracker->expects($this->once())->method('trackRegularNotFoundVisit')->with(
|
|
|
|
$this->isInstanceOf(Visitor::class),
|
|
|
|
);
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackInvalidShortUrlVisit');
|
2021-07-15 10:43:29 -05:00
|
|
|
|
|
|
|
$this->requestTracker->trackNotFoundIfApplicable($this->request);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-07-15 10:43:29 -05:00
|
|
|
public function invalidShortUrlErrorIsTracked(): void
|
|
|
|
{
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->notFoundType->expects($this->once())->method('isBaseUrl')->willReturn(false);
|
|
|
|
$this->notFoundType->expects($this->once())->method('isRegularNotFound')->willReturn(false);
|
|
|
|
$this->notFoundType->expects($this->once())->method('isInvalidShortUrl')->willReturn(true);
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackBaseUrlVisit');
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackRegularNotFoundVisit');
|
|
|
|
$this->visitsTracker->expects($this->once())->method('trackInvalidShortUrlVisit')->with(
|
|
|
|
$this->isInstanceOf(Visitor::class),
|
|
|
|
);
|
2021-07-15 10:43:29 -05:00
|
|
|
|
|
|
|
$this->requestTracker->trackNotFoundIfApplicable($this->request);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideNonTrackingRequests')]
|
2021-07-15 10:43:29 -05:00
|
|
|
public function notFoundIsNotTrackedIfRequestDoesNotMeetConditions(ServerRequestInterface $request): void
|
|
|
|
{
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->visitsTracker->expects($this->never())->method('trackBaseUrlVisit');
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackRegularNotFoundVisit');
|
|
|
|
$this->visitsTracker->expects($this->never())->method('trackInvalidShortUrlVisit');
|
2021-07-15 10:43:29 -05:00
|
|
|
|
2022-10-23 13:45:56 -05:00
|
|
|
$this->requestTracker->trackNotFoundIfApplicable($request);
|
2021-07-15 10:43:29 -05:00
|
|
|
}
|
|
|
|
}
|