2019-01-27 14:35:00 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-01-27 14:35:00 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2019-12-17 01:03:32 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GuzzleHttp\RequestOptions;
|
2019-08-11 17:30:46 +03:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2019-01-27 14:35:00 +03:00
|
|
|
|
2019-12-17 01:03:32 +03:00
|
|
|
use function count;
|
|
|
|
|
2019-01-27 14:35:00 +03:00
|
|
|
class ListShortUrlsTest extends ApiTestCase
|
|
|
|
{
|
2021-02-04 23:27:16 +03:00
|
|
|
private const SHORT_URL_SHLINK_WITH_TITLE = [
|
2019-12-17 01:03:32 +03:00
|
|
|
'shortCode' => 'abc123',
|
|
|
|
'shortUrl' => 'http://doma.in/abc123',
|
|
|
|
'longUrl' => 'https://shlink.io',
|
|
|
|
'dateCreated' => '2018-05-01T00:00:00+00:00',
|
|
|
|
'visitsCount' => 3,
|
|
|
|
'tags' => ['foo'],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
2020-02-02 10:57:04 +03:00
|
|
|
'domain' => null,
|
2021-02-04 23:27:16 +03:00
|
|
|
'title' => 'My cool title',
|
2021-05-22 08:35:47 +03:00
|
|
|
'crawlable' => true,
|
2019-12-17 01:03:32 +03:00
|
|
|
];
|
2020-02-02 11:51:17 +03:00
|
|
|
private const SHORT_URL_DOCS = [
|
|
|
|
'shortCode' => 'ghi789',
|
|
|
|
'shortUrl' => 'http://doma.in/ghi789',
|
|
|
|
'longUrl' => 'https://shlink.io/documentation/',
|
|
|
|
'dateCreated' => '2018-05-01T00:00:00+00:00',
|
2020-02-02 12:46:38 +03:00
|
|
|
'visitsCount' => 2,
|
2020-02-02 11:51:17 +03:00
|
|
|
'tags' => [],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
|
|
|
'domain' => null,
|
2021-02-02 22:51:28 +03:00
|
|
|
'title' => null,
|
2021-05-22 08:35:47 +03:00
|
|
|
'crawlable' => false,
|
2020-02-02 11:51:17 +03:00
|
|
|
];
|
2019-12-17 01:03:32 +03:00
|
|
|
private const SHORT_URL_CUSTOM_SLUG_AND_DOMAIN = [
|
|
|
|
'shortCode' => 'custom-with-domain',
|
|
|
|
'shortUrl' => 'http://some-domain.com/custom-with-domain',
|
|
|
|
'longUrl' => 'https://google.com',
|
|
|
|
'dateCreated' => '2018-10-20T00:00:00+00:00',
|
|
|
|
'visitsCount' => 0,
|
|
|
|
'tags' => [],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
2020-02-02 10:57:04 +03:00
|
|
|
'domain' => 'some-domain.com',
|
2021-02-02 22:51:28 +03:00
|
|
|
'title' => null,
|
2021-05-22 08:35:47 +03:00
|
|
|
'crawlable' => false,
|
2019-12-17 01:03:32 +03:00
|
|
|
];
|
|
|
|
private const SHORT_URL_META = [
|
|
|
|
'shortCode' => 'def456',
|
|
|
|
'shortUrl' => 'http://doma.in/def456',
|
|
|
|
'longUrl' =>
|
|
|
|
'https://blog.alejandrocelaya.com/2017/12/09'
|
|
|
|
. '/acmailer-7-0-the-most-important-release-in-a-long-time/',
|
2019-12-18 17:26:23 +03:00
|
|
|
'dateCreated' => '2019-01-01T00:00:10+00:00',
|
2019-12-17 01:03:32 +03:00
|
|
|
'visitsCount' => 2,
|
|
|
|
'tags' => ['bar', 'foo'],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => '2020-05-01T00:00:00+00:00',
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
2020-02-02 10:57:04 +03:00
|
|
|
'domain' => null,
|
2021-02-02 22:51:28 +03:00
|
|
|
'title' => null,
|
2021-05-22 08:35:47 +03:00
|
|
|
'crawlable' => false,
|
2019-12-17 01:03:32 +03:00
|
|
|
];
|
|
|
|
private const SHORT_URL_CUSTOM_SLUG = [
|
|
|
|
'shortCode' => 'custom',
|
|
|
|
'shortUrl' => 'http://doma.in/custom',
|
|
|
|
'longUrl' => 'https://shlink.io',
|
2019-12-18 17:26:23 +03:00
|
|
|
'dateCreated' => '2019-01-01T00:00:20+00:00',
|
2019-12-17 01:03:32 +03:00
|
|
|
'visitsCount' => 0,
|
|
|
|
'tags' => [],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => 2,
|
|
|
|
],
|
2020-02-02 10:57:04 +03:00
|
|
|
'domain' => null,
|
2021-02-02 22:51:28 +03:00
|
|
|
'title' => null,
|
2021-05-22 08:35:47 +03:00
|
|
|
'crawlable' => false,
|
2019-12-17 01:03:32 +03:00
|
|
|
];
|
|
|
|
private const SHORT_URL_CUSTOM_DOMAIN = [
|
|
|
|
'shortCode' => 'ghi789',
|
|
|
|
'shortUrl' => 'http://example.com/ghi789',
|
|
|
|
'longUrl' =>
|
|
|
|
'https://blog.alejandrocelaya.com/2019/04/27'
|
|
|
|
. '/considerations-to-properly-use-open-source-software-projects/',
|
2019-12-18 17:26:23 +03:00
|
|
|
'dateCreated' => '2019-01-01T00:00:30+00:00',
|
2019-12-17 01:03:32 +03:00
|
|
|
'visitsCount' => 0,
|
2021-01-10 11:54:19 +03:00
|
|
|
'tags' => ['foo'],
|
2019-12-17 01:03:32 +03:00
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
2020-02-02 10:57:04 +03:00
|
|
|
'domain' => 'example.com',
|
2021-02-02 22:51:28 +03:00
|
|
|
'title' => null,
|
2021-05-22 08:35:47 +03:00
|
|
|
'crawlable' => false,
|
2019-12-17 01:03:32 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFilteredLists
|
|
|
|
*/
|
2021-01-10 10:49:31 +03:00
|
|
|
public function shortUrlsAreProperlyListed(array $query, array $expectedShortUrls, string $apiKey): void
|
2019-01-27 14:35:00 +03:00
|
|
|
{
|
2021-01-10 10:49:31 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/short-urls', [RequestOptions::QUERY => $query], $apiKey);
|
2019-01-27 14:35:00 +03:00
|
|
|
$respPayload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_OK, $resp->getStatusCode());
|
|
|
|
self::assertEquals([
|
2019-01-27 14:35:00 +03:00
|
|
|
'shortUrls' => [
|
2019-12-17 01:03:32 +03:00
|
|
|
'data' => $expectedShortUrls,
|
|
|
|
'pagination' => $this->buildPagination(count($expectedShortUrls)),
|
2019-01-27 14:35:00 +03:00
|
|
|
],
|
|
|
|
], $respPayload);
|
|
|
|
}
|
2019-12-17 01:03:32 +03:00
|
|
|
|
|
|
|
public function provideFilteredLists(): iterable
|
|
|
|
{
|
|
|
|
yield [[], [
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2020-02-02 11:51:17 +03:00
|
|
|
self::SHORT_URL_DOCS,
|
2019-12-17 01:03:32 +03:00
|
|
|
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['orderBy' => 'shortCode'], [
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2019-12-17 01:03:32 +03:00
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
|
|
|
self::SHORT_URL_META,
|
2020-02-02 11:51:17 +03:00
|
|
|
self::SHORT_URL_DOCS,
|
2019-12-17 01:03:32 +03:00
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2020-09-21 23:03:43 +03:00
|
|
|
yield [['orderBy' => ['shortCode' => 'DESC']], [ // Deprecated
|
2020-02-02 11:51:17 +03:00
|
|
|
self::SHORT_URL_DOCS,
|
2020-01-28 13:20:48 +03:00
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2020-09-21 23:03:43 +03:00
|
|
|
yield [['orderBy' => 'shortCode-DESC'], [
|
2020-09-20 14:21:21 +03:00
|
|
|
self::SHORT_URL_DOCS,
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2021-02-05 00:07:54 +03:00
|
|
|
yield [['orderBy' => 'title-DESC'], [
|
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
|
|
|
self::SHORT_URL_DOCS,
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [
|
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2020-02-02 11:51:17 +03:00
|
|
|
self::SHORT_URL_DOCS,
|
2019-12-17 01:03:32 +03:00
|
|
|
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['tags' => ['foo']], [
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2019-12-17 01:03:32 +03:00
|
|
|
self::SHORT_URL_META,
|
2021-01-10 11:54:19 +03:00
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['tags' => ['bar']], [
|
|
|
|
self::SHORT_URL_META,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['tags' => ['foo'], 'endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
yield [['searchTerm' => 'alejandro'], [
|
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
2021-02-04 23:27:16 +03:00
|
|
|
yield [['searchTerm' => 'cool'], [
|
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
|
|
|
], 'valid_api_key'];
|
2020-02-02 22:07:19 +03:00
|
|
|
yield [['searchTerm' => 'example.com'], [
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
2021-01-10 10:49:31 +03:00
|
|
|
], 'valid_api_key'];
|
|
|
|
yield [[], [
|
2021-02-04 23:27:16 +03:00
|
|
|
self::SHORT_URL_SHLINK_WITH_TITLE,
|
2021-01-10 10:49:31 +03:00
|
|
|
self::SHORT_URL_META,
|
|
|
|
self::SHORT_URL_CUSTOM_SLUG,
|
|
|
|
], 'author_api_key'];
|
|
|
|
yield [[], [
|
|
|
|
self::SHORT_URL_CUSTOM_DOMAIN,
|
|
|
|
], 'domain_api_key'];
|
2019-12-17 01:03:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildPagination(int $itemsCount): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'currentPage' => 1,
|
|
|
|
'pagesCount' => 1,
|
|
|
|
'itemsPerPage' => 10,
|
|
|
|
'itemsInCurrentPage' => $itemsCount,
|
|
|
|
'totalItems' => $itemsCount,
|
|
|
|
];
|
|
|
|
}
|
2019-01-27 14:35:00 +03:00
|
|
|
}
|