2019-11-20 13:44:03 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GuzzleHttp\RequestOptions;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2023-06-18 03:36:45 -05:00
|
|
|
use PHPUnit\Framework\Attributes\DataProviderExternal;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2019-11-20 13:44:03 -06:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2023-06-18 03:36:45 -05:00
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\ApiTestDataProviders;
|
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\UrlBuilder;
|
2019-11-20 13:44:03 -06:00
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2021-01-24 02:25:36 -06:00
|
|
|
class ResolveShortUrlTest extends ApiTestCase
|
2019-11-20 13:44:03 -06:00
|
|
|
{
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideDisabledMeta')]
|
2020-01-26 12:21:51 -06:00
|
|
|
public function shortUrlIsProperlyResolvedEvenWhenNotEnabled(array $disabledMeta): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = sprintf('/short-urls/%s', $shortCode);
|
|
|
|
$this->callShortUrl($shortCode);
|
|
|
|
|
|
|
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $disabledMeta]);
|
|
|
|
$visitResp = $this->callShortUrl($shortCode);
|
|
|
|
$fetchResp = $this->callApiWithKey(self::METHOD_GET, $url);
|
|
|
|
|
2021-01-31 06:21:23 -06:00
|
|
|
self::assertEquals(self::STATUS_OK, $editResp->getStatusCode());
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
|
2020-01-26 12:21:51 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideDisabledMeta(): iterable
|
2020-01-26 12:21:51 -06:00
|
|
|
{
|
|
|
|
$now = Chronos::now();
|
|
|
|
|
2023-08-19 04:48:04 -05:00
|
|
|
yield 'future validSince' => [['validSince' => $now->addMonths(1)->toAtomString()]];
|
|
|
|
yield 'past validUntil' => [['validUntil' => $now->subMonths(1)->toAtomString()]];
|
2020-01-26 12:21:51 -06:00
|
|
|
yield 'maxVisits reached' => [['maxVisits' => 1]];
|
|
|
|
}
|
|
|
|
|
2023-06-18 03:36:45 -05:00
|
|
|
#[Test, DataProviderExternal(ApiTestDataProviders::class, 'invalidUrlsProvider')]
|
2020-02-02 06:15:08 -06:00
|
|
|
public function tryingToResolveInvalidUrlReturnsNotFoundError(
|
|
|
|
string $shortCode,
|
2024-10-28 16:27:30 -05:00
|
|
|
string|null $domain,
|
2021-01-10 02:02:05 -06:00
|
|
|
string $expectedDetail,
|
2021-05-23 05:31:10 -05:00
|
|
|
string $apiKey,
|
2020-02-02 06:15:08 -06:00
|
|
|
): void {
|
2023-06-18 03:36:45 -05:00
|
|
|
$resp = $this->callApiWithKey(
|
|
|
|
self::METHOD_GET,
|
|
|
|
UrlBuilder::buildShortUrlPath($shortCode, $domain),
|
|
|
|
apiKey: $apiKey,
|
|
|
|
);
|
2019-11-27 13:48:35 -06:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 13:44:03 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
2024-02-12 13:29:40 -06:00
|
|
|
self::assertEquals('https://shlink.io/api/error/short-url-not-found', $payload['type']);
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Short URL not found', $payload['title']);
|
|
|
|
self::assertEquals($shortCode, $payload['shortCode']);
|
|
|
|
self::assertEquals($domain, $payload['domain'] ?? null);
|
2019-11-20 13:44:03 -06:00
|
|
|
}
|
|
|
|
}
|