2021-02-09 16:56:46 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use GuzzleHttp\RequestOptions;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2021-12-08 10:36:40 -06:00
|
|
|
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
2024-02-10 11:26:19 -06:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitType;
|
2021-02-09 16:56:46 -06:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
|
|
|
|
|
|
|
class OrphanVisitsTest extends ApiTestCase
|
|
|
|
{
|
|
|
|
private const INVALID_SHORT_URL = [
|
2023-01-19 02:05:52 -06:00
|
|
|
'referer' => 'https://s.test/foo',
|
2021-02-09 16:56:46 -06:00
|
|
|
'date' => '2020-03-01T00:00:00+00:00',
|
2021-05-22 08:09:14 -05:00
|
|
|
'userAgent' => 'cf-facebook',
|
2021-02-09 16:56:46 -06:00
|
|
|
'visitLocation' => null,
|
2021-05-22 08:09:14 -05:00
|
|
|
'potentialBot' => true,
|
2021-02-09 16:56:46 -06:00
|
|
|
'visitedUrl' => 'foo.com',
|
|
|
|
'type' => 'invalid_short_url',
|
|
|
|
];
|
|
|
|
private const REGULAR_NOT_FOUND = [
|
2023-01-19 02:05:52 -06:00
|
|
|
'referer' => 'https://s.test/foo/bar',
|
2021-02-09 16:56:46 -06:00
|
|
|
'date' => '2020-02-01T00:00:00+00:00',
|
|
|
|
'userAgent' => 'shlink-tests-agent',
|
|
|
|
'visitLocation' => null,
|
2021-05-22 08:09:14 -05:00
|
|
|
'potentialBot' => false,
|
2021-02-09 16:56:46 -06:00
|
|
|
'visitedUrl' => '',
|
|
|
|
'type' => 'regular_404',
|
|
|
|
];
|
|
|
|
private const BASE_URL = [
|
2023-01-19 02:05:52 -06:00
|
|
|
'referer' => 'https://s.test',
|
2021-02-09 16:56:46 -06:00
|
|
|
'date' => '2020-01-01T00:00:00+00:00',
|
|
|
|
'userAgent' => 'shlink-tests-agent',
|
|
|
|
'visitLocation' => null,
|
2021-05-22 08:09:14 -05:00
|
|
|
'potentialBot' => false,
|
2021-02-09 16:56:46 -06:00
|
|
|
'visitedUrl' => '',
|
|
|
|
'type' => 'base_url',
|
|
|
|
];
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideQueries')]
|
2021-05-22 14:05:54 -05:00
|
|
|
public function properVisitsAreReturnedBasedInQuery(
|
|
|
|
array $query,
|
|
|
|
int $totalItems,
|
|
|
|
int $expectedAmount,
|
2021-05-23 05:31:10 -05:00
|
|
|
array $expectedVisits,
|
2021-05-22 14:05:54 -05:00
|
|
|
): void {
|
2021-02-09 16:56:46 -06:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/visits/orphan', [RequestOptions::QUERY => $query]);
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
$visits = $payload['visits']['data'] ?? [];
|
|
|
|
|
2021-12-08 10:36:40 -06:00
|
|
|
self::assertEquals($totalItems, $payload['visits']['pagination']['totalItems'] ?? Paginator::ALL_ITEMS);
|
2021-02-09 16:56:46 -06:00
|
|
|
self::assertCount($expectedAmount, $visits);
|
|
|
|
self::assertEquals($expectedVisits, $visits);
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideQueries(): iterable
|
2021-02-09 16:56:46 -06:00
|
|
|
{
|
2021-05-22 14:05:54 -05:00
|
|
|
yield 'all data' => [[], 3, 3, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND, self::BASE_URL]];
|
|
|
|
yield 'limit items' => [['itemsPerPage' => 2], 3, 2, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND]];
|
|
|
|
yield 'limit items and page' => [['itemsPerPage' => 2, 'page' => 2], 3, 1, [self::BASE_URL]];
|
|
|
|
yield 'exclude bots' => [['excludeBots' => true], 2, 2, [self::REGULAR_NOT_FOUND, self::BASE_URL]];
|
|
|
|
yield 'exclude bots and limit items' => [
|
|
|
|
['excludeBots' => true, 'itemsPerPage' => 1],
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
[self::REGULAR_NOT_FOUND],
|
|
|
|
];
|
2024-02-10 11:26:19 -06:00
|
|
|
yield 'base_url only' => [['type' => OrphanVisitType::BASE_URL->value], 1, 1, [self::BASE_URL]];
|
|
|
|
yield 'regular_404 only' => [['type' => OrphanVisitType::REGULAR_404->value], 1, 1, [self::REGULAR_NOT_FOUND]];
|
|
|
|
yield 'invalid_short_url only' => [
|
|
|
|
['type' => OrphanVisitType::INVALID_SHORT_URL->value],
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
[self::INVALID_SHORT_URL],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function errorIsReturnedForInvalidType(): void
|
|
|
|
{
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/visits/orphan', [
|
|
|
|
RequestOptions::QUERY => ['type' => 'invalid'],
|
|
|
|
]);
|
|
|
|
self::assertEquals(400, $resp->getStatusCode());
|
2021-02-09 16:56:46 -06:00
|
|
|
}
|
2023-05-31 02:22:40 -05:00
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function noVisitsAreReturnedForRestrictedApiKey(): void
|
|
|
|
{
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/visits/orphan', apiKey: 'no_orphans_api_key');
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
$visits = $payload['visits']['data'] ?? null;
|
|
|
|
|
|
|
|
self::assertIsArray($visits);
|
|
|
|
self::assertEmpty($visits);
|
|
|
|
self::assertEquals(0, $payload['visits']['pagination']['totalItems'] ?? Paginator::ALL_ITEMS);
|
|
|
|
}
|
2021-02-09 16:56:46 -06:00
|
|
|
}
|