2019-11-20 13:31:18 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2020-01-26 02:29:04 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2019-11-20 13:31:18 -06:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2020-02-02 02:51:17 -06:00
|
|
|
use Laminas\Diactoros\Uri;
|
2019-11-20 13:31:18 -06:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
|
|
|
|
2020-02-02 02:51:17 -06:00
|
|
|
use function GuzzleHttp\Psr7\build_query;
|
2020-01-26 02:29:04 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2019-11-20 13:31:18 -06:00
|
|
|
class EditShortUrlActionTest extends ApiTestCase
|
|
|
|
{
|
2020-01-26 02:29:04 -06:00
|
|
|
use ArraySubsetAsserts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2020-01-26 12:21:51 -06:00
|
|
|
* @dataProvider provideMeta
|
2020-01-26 02:29:04 -06:00
|
|
|
*/
|
|
|
|
public function metadataCanBeReset(array $meta): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = sprintf('/short-urls/%s', $shortCode);
|
|
|
|
$resetMeta = [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$editWithProvidedMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $meta]);
|
|
|
|
$metaAfterEditing = $this->findShortUrlMetaByShortCode($shortCode);
|
|
|
|
|
|
|
|
$editWithResetMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [
|
|
|
|
RequestOptions::JSON => $resetMeta,
|
|
|
|
]);
|
|
|
|
$metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editWithProvidedMeta->getStatusCode());
|
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editWithResetMeta->getStatusCode());
|
|
|
|
$this->assertEquals($resetMeta, $metaAfterResetting);
|
|
|
|
self::assertArraySubset($meta, $metaAfterEditing);
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
public function provideMeta(): iterable
|
2020-01-26 02:29:04 -06:00
|
|
|
{
|
|
|
|
$now = Chronos::now();
|
|
|
|
|
|
|
|
yield [['validSince' => $now->addMonth()->toAtomString()]];
|
|
|
|
yield [['validUntil' => $now->subMonth()->toAtomString()]];
|
|
|
|
yield [['maxVisits' => 20]];
|
|
|
|
yield [['validUntil' => $now->addYear()->toAtomString(), 'maxVisits' => 100]];
|
|
|
|
yield [[
|
|
|
|
'validSince' => $now->subYear()->toAtomString(),
|
|
|
|
'validUntil' => $now->addYear()->toAtomString(),
|
|
|
|
'maxVisits' => 100,
|
|
|
|
]];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findShortUrlMetaByShortCode(string $shortCode): ?array
|
|
|
|
{
|
2020-02-02 02:51:17 -06:00
|
|
|
$matchingShortUrl = $this->getJsonResponsePayload(
|
|
|
|
$this->callApiWithKey(self::METHOD_GET, '/short-urls/' . $shortCode),
|
|
|
|
);
|
2020-01-26 02:29:04 -06:00
|
|
|
|
|
|
|
return $matchingShortUrl['meta'] ?? null;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:31:18 -06:00
|
|
|
/** @test */
|
|
|
|
public function tryingToEditInvalidUrlReturnsNotFoundError(): void
|
|
|
|
{
|
2019-11-27 13:48:35 -06:00
|
|
|
$expectedDetail = 'No URL found with short code "invalid"';
|
|
|
|
|
2019-11-20 13:31:18 -06:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => []]);
|
2019-11-27 13:48:35 -06:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 13:31:18 -06:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
2019-11-27 13:48:35 -06:00
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
$this->assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
$this->assertEquals('Short URL not found', $payload['title']);
|
|
|
|
$this->assertEquals('invalid', $payload['shortCode']);
|
2019-11-20 13:31:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function providingInvalidDataReturnsBadRequest(): void
|
|
|
|
{
|
2019-11-27 13:48:35 -06:00
|
|
|
$expectedDetail = 'Provided data is not valid';
|
|
|
|
|
2019-11-20 13:31:18 -06:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => [
|
|
|
|
'maxVisits' => 'not_a_number',
|
|
|
|
]]);
|
2019-11-27 13:48:35 -06:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 13:31:18 -06:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode());
|
2019-11-27 13:48:35 -06:00
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $payload['status']);
|
|
|
|
$this->assertEquals('INVALID_ARGUMENT', $payload['type']);
|
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
$this->assertEquals('Invalid data', $payload['title']);
|
2019-11-20 13:31:18 -06:00
|
|
|
}
|
2020-02-02 02:51:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDomains
|
|
|
|
*/
|
|
|
|
public function metadataIsEditedOnProperShortUrlBasedOnDomain(?string $domain, string $expectedUrl): void
|
|
|
|
{
|
|
|
|
$shortCode = 'ghi789';
|
|
|
|
$url = new Uri(sprintf('/short-urls/%s', $shortCode));
|
|
|
|
|
|
|
|
if ($domain !== null) {
|
|
|
|
$url = $url->withQuery(build_query(['domain' => $domain]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, (string) $url, [RequestOptions::JSON => [
|
|
|
|
'maxVisits' => 100,
|
|
|
|
]]);
|
|
|
|
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url));
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
|
|
|
|
$this->assertEquals($domain, $editedShortUrl['domain']);
|
|
|
|
$this->assertEquals($expectedUrl, $editedShortUrl['longUrl']);
|
|
|
|
$this->assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDomains(): iterable
|
|
|
|
{
|
|
|
|
yield 'domain' => [null, 'https://shlink.io/documentation/'];
|
|
|
|
yield 'no domain' => [
|
|
|
|
'example.com',
|
|
|
|
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/',
|
|
|
|
];
|
|
|
|
}
|
2019-11-20 13:31:18 -06:00
|
|
|
}
|