2019-11-20 22:31:18 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2020-01-26 11:29:04 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2019-11-20 22:31:18 +03:00
|
|
|
use GuzzleHttp\RequestOptions;
|
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
|
|
|
|
2020-01-26 11:29:04 +03:00
|
|
|
use function Functional\first;
|
|
|
|
use function sprintf;
|
|
|
|
|
2019-11-20 22:31:18 +03:00
|
|
|
class EditShortUrlActionTest extends ApiTestCase
|
|
|
|
{
|
2020-01-26 11:29:04 +03:00
|
|
|
use ArraySubsetAsserts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2020-01-26 21:21:51 +03:00
|
|
|
* @dataProvider provideMeta
|
2020-01-26 11:29:04 +03: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 21:21:51 +03:00
|
|
|
public function provideMeta(): iterable
|
2020-01-26 11:29:04 +03: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
|
|
|
|
{
|
|
|
|
// FIXME Call GET /short-urls/{shortCode} once issue https://github.com/shlinkio/shlink/issues/628 is fixed
|
|
|
|
$allShortUrls = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, '/short-urls'));
|
|
|
|
$list = $allShortUrls['shortUrls']['data'] ?? [];
|
|
|
|
$matchingShortUrl = first($list, fn (array $shortUrl) => $shortUrl['shortCode'] ?? '' === $shortCode);
|
|
|
|
|
|
|
|
return $matchingShortUrl['meta'] ?? null;
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:31:18 +03:00
|
|
|
/** @test */
|
|
|
|
public function tryingToEditInvalidUrlReturnsNotFoundError(): void
|
|
|
|
{
|
2019-11-27 22:48:35 +03:00
|
|
|
$expectedDetail = 'No URL found with short code "invalid"';
|
|
|
|
|
2019-11-20 22:31:18 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => []]);
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:31:18 +03:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
2019-11-27 22:48:35 +03: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 22:31:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function providingInvalidDataReturnsBadRequest(): void
|
|
|
|
{
|
2019-11-27 22:48:35 +03:00
|
|
|
$expectedDetail = 'Provided data is not valid';
|
|
|
|
|
2019-11-20 22:31:18 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => [
|
|
|
|
'maxVisits' => 'not_a_number',
|
|
|
|
]]);
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:31:18 +03:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode());
|
2019-11-27 22:48:35 +03: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 22:31:18 +03:00
|
|
|
}
|
|
|
|
}
|