2019-11-20 22:38:19 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2020-02-02 15:15:08 +03:00
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait;
|
2019-11-20 22:38:19 +03:00
|
|
|
|
2021-01-24 11:25:36 +03:00
|
|
|
class DeleteShortUrlTest extends ApiTestCase
|
2019-11-20 22:38:19 +03:00
|
|
|
{
|
2020-02-02 15:15:08 +03:00
|
|
|
use NotFoundUrlHelpersTrait;
|
2019-11-27 22:48:35 +03:00
|
|
|
|
2020-02-02 15:15:08 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidUrls
|
|
|
|
*/
|
|
|
|
public function notFoundErrorIsReturnWhenDeletingInvalidUrl(
|
|
|
|
string $shortCode,
|
|
|
|
?string $domain,
|
2021-01-10 11:02:05 +03:00
|
|
|
string $expectedDetail,
|
2021-05-23 13:31:10 +03:00
|
|
|
string $apiKey,
|
2020-02-02 15:15:08 +03:00
|
|
|
): void {
|
2021-01-10 11:02:05 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain), [], $apiKey);
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:38:19 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
self::assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
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 22:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
2019-11-25 01:56:02 +03:00
|
|
|
public function unprocessableEntityIsReturnedWhenTryingToDeleteUrlWithTooManyVisits(): void
|
2019-11-20 22:38:19 +03:00
|
|
|
{
|
|
|
|
// Generate visits first
|
|
|
|
for ($i = 0; $i < 20; $i++) {
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_FOUND, $this->callShortUrl('abc123')->getStatusCode());
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|
2021-11-30 23:37:35 +03:00
|
|
|
$expectedDetail = 'Impossible to delete short URL with short code "abc123", since it has more than "15" '
|
|
|
|
. 'visits.';
|
2019-11-20 22:38:19 +03:00
|
|
|
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/abc123');
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:38:19 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $payload['status']);
|
2021-12-15 00:21:53 +03:00
|
|
|
self::assertEquals('INVALID_SHORT_URL_DELETION', $payload['type']);
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Cannot delete short URL', $payload['title']);
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|
2020-02-02 12:28:10 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function properShortUrlIsDeletedWhenDomainIsProvided(): void
|
|
|
|
{
|
2020-02-02 14:58:26 +03:00
|
|
|
$fetchWithDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
|
|
|
|
$fetchWithoutDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
|
2020-02-02 12:28:10 +03:00
|
|
|
$deleteResp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/ghi789?domain=example.com');
|
2020-02-02 14:58:26 +03:00
|
|
|
$fetchWithDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
|
|
|
|
$fetchWithoutDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
|
2020-02-02 12:28:10 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_OK, $fetchWithDomainBefore->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $fetchWithoutDomainBefore->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NO_CONTENT, $deleteResp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $fetchWithDomainAfter->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $fetchWithoutDomainAfter->getStatusCode());
|
2020-02-02 12:28:10 +03:00
|
|
|
}
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|