From 0096a778ac1a8d118392f6b875cbb83480dffb52 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 15 Jul 2021 17:43:29 +0200 Subject: [PATCH] Created RequestTracker test --- .../Core/test/Action/RedirectActionTest.php | 33 ---- .../NotFoundTrackerMiddlewareTest.php | 51 ------ module/Core/test/Visit/RequestTrackerTest.php | 147 ++++++++++++++++++ 3 files changed, 147 insertions(+), 84 deletions(-) create mode 100644 module/Core/test/Visit/RequestTrackerTest.php diff --git a/module/Core/test/Action/RedirectActionTest.php b/module/Core/test/Action/RedirectActionTest.php index 3932810e..b3017fad 100644 --- a/module/Core/test/Action/RedirectActionTest.php +++ b/module/Core/test/Action/RedirectActionTest.php @@ -4,10 +4,8 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Core\Action; -use Fig\Http\Message\RequestMethodInterface; use Laminas\Diactoros\Response; use Laminas\Diactoros\ServerRequest; -use Mezzio\Router\Middleware\ImplicitHeadMiddleware; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -72,14 +70,6 @@ class RedirectActionTest extends TestCase $track->shouldHaveBeenCalledOnce(); } -// public function provideQueries(): iterable -// { -// yield [[]]; -// yield [['foobar' => 'notrack']]; -// yield [['foobar' => 'barfoo']]; -// yield [['foobar' => null]]; -// } - /** @test */ public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void { @@ -97,27 +87,4 @@ class RedirectActionTest extends TestCase $handle->shouldHaveBeenCalledOnce(); } - -// /** @test */ -// public function trackingIsDisabledWhenRequestIsForwardedFromHead(): void -// { -// $shortCode = 'abc123'; -// $shortUrl = ShortUrl::withLongUrl(self::LONG_URL); -// $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn($shortUrl); -// $track = $this->requestTracker->trackIfApplicable(Argument::cetera())->will(function (): void { -// }); -// $buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn( -// new Response\RedirectResponse(''), -// ); -// -// $request = (new ServerRequest())->withAttribute('shortCode', $shortCode) -// ->withAttribute( -// ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE, -// RequestMethodInterface::METHOD_HEAD, -// ); -// $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal()); -// -// $buildResp->shouldHaveBeenCalled(); -// $track->shouldNotHaveBeenCalled(); -// } } diff --git a/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php b/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php index 1a29a4b5..81fef1a6 100644 --- a/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php +++ b/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php @@ -49,55 +49,4 @@ class NotFoundTrackerMiddlewareTest extends TestCase $this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce(); $this->handler->handle($this->request)->shouldHaveBeenCalledOnce(); } - -// /** @test */ -// public function baseUrlErrorIsTracked(): void -// { -// $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(true); -// $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false); -// $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false); -// -// $this->middleware->process($this->request, $this->handler->reveal()); -// -// $isBaseUrl->shouldHaveBeenCalledOnce(); -// $isRegularNotFound->shouldNotHaveBeenCalled(); -// $isInvalidShortUrl->shouldNotHaveBeenCalled(); -// $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); -// $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); -// $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); -// } -// -// /** @test */ -// public function regularNotFoundErrorIsTracked(): void -// { -// $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false); -// $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(true); -// $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false); -// -// $this->middleware->process($this->request, $this->handler->reveal()); -// -// $isBaseUrl->shouldHaveBeenCalledOnce(); -// $isRegularNotFound->shouldHaveBeenCalledOnce(); -// $isInvalidShortUrl->shouldNotHaveBeenCalled(); -// $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); -// $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); -// $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); -// } -// -// /** @test */ -// public function invalidShortUrlErrorIsTracked(): void -// { -// $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false); -// $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false); -// $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(true); -// -// $this->middleware->process($this->request, $this->handler->reveal()); -// -// $isBaseUrl->shouldHaveBeenCalledOnce(); -// $isRegularNotFound->shouldHaveBeenCalledOnce(); -// $isInvalidShortUrl->shouldHaveBeenCalledOnce(); -// $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); -// $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); -// $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); -// } } diff --git a/module/Core/test/Visit/RequestTrackerTest.php b/module/Core/test/Visit/RequestTrackerTest.php new file mode 100644 index 00000000..46faf9fd --- /dev/null +++ b/module/Core/test/Visit/RequestTrackerTest.php @@ -0,0 +1,147 @@ +notFoundType = $this->prophesize(NotFoundType::class); + $this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class); + + $this->requestTracker = new RequestTracker( + $this->visitsTracker->reveal(), + new TrackingOptions(['disable_track_param' => 'foobar']), + ); + + $this->request = ServerRequestFactory::fromGlobals()->withAttribute( + NotFoundType::class, + $this->notFoundType->reveal(), + ); + } + + /** + * @test + * @dataProvider provideNonTrackingRequests + */ + public function trackingIsDisabledWhenRequestDoesNotMeetConditions(ServerRequestInterface $request): void + { + $shortUrl = ShortUrl::withLongUrl(self::LONG_URL); + + $this->requestTracker->trackIfApplicable($shortUrl, $request); + + $this->visitsTracker->track(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + public function provideNonTrackingRequests(): iterable + { + 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]), + ]; + } + + /** @test */ + public function trackingHappensOverShortUrlsWhenRequestMeetsConditions(): void + { + $shortUrl = ShortUrl::withLongUrl(self::LONG_URL); + + $this->requestTracker->trackIfApplicable($shortUrl, $this->request); + + $this->visitsTracker->track($shortUrl, Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + } + + /** @test */ + public function baseUrlErrorIsTracked(): void + { + $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(true); + $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false); + $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false); + + $this->requestTracker->trackNotFoundIfApplicable($this->request); + + $isBaseUrl->shouldHaveBeenCalledOnce(); + $isRegularNotFound->shouldNotHaveBeenCalled(); + $isInvalidShortUrl->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function regularNotFoundErrorIsTracked(): void + { + $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false); + $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(true); + $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false); + + $this->requestTracker->trackNotFoundIfApplicable($this->request); + + $isBaseUrl->shouldHaveBeenCalledOnce(); + $isRegularNotFound->shouldHaveBeenCalledOnce(); + $isInvalidShortUrl->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function invalidShortUrlErrorIsTracked(): void + { + $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false); + $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false); + $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(true); + + $this->requestTracker->trackNotFoundIfApplicable($this->request); + + $isBaseUrl->shouldHaveBeenCalledOnce(); + $isRegularNotFound->shouldHaveBeenCalledOnce(); + $isInvalidShortUrl->shouldHaveBeenCalledOnce(); + $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + } + + /** + * @test + * @dataProvider provideNonTrackingRequests + */ + public function notFoundIsNotTrackedIfRequestDoesNotMeetConditions(ServerRequestInterface $request): void + { + $this->requestTracker->trackNotFoundIfApplicable($request); + + $this->visitsTracker->trackBaseUrlVisit(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::cetera())->shouldNotHaveBeenCalled(); + } +}