|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; |
|
9
|
|
|
use GuzzleHttp\RequestOptions; |
|
10
|
|
|
use Laminas\Diactoros\Uri; |
|
11
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase; |
|
12
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait; |
|
13
|
|
|
|
|
14
|
|
|
use function GuzzleHttp\Psr7\build_query; |
|
15
|
|
|
use function sprintf; |
|
16
|
|
|
|
|
17
|
|
|
class EditShortUrlActionTest extends ApiTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
use ArraySubsetAsserts; |
|
20
|
|
|
use NotFoundUrlHelpersTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @test |
|
24
|
|
|
* @dataProvider provideMeta |
|
25
|
|
|
*/ |
|
26
|
|
|
public function metadataCanBeReset(array $meta): void |
|
27
|
|
|
{ |
|
28
|
|
|
$shortCode = 'abc123'; |
|
29
|
|
|
$url = sprintf('/short-urls/%s', $shortCode); |
|
30
|
|
|
$resetMeta = [ |
|
31
|
|
|
'validSince' => null, |
|
32
|
|
|
'validUntil' => null, |
|
33
|
|
|
'maxVisits' => null, |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
$editWithProvidedMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $meta]); |
|
37
|
|
|
$metaAfterEditing = $this->findShortUrlMetaByShortCode($shortCode); |
|
38
|
|
|
|
|
39
|
|
|
$editWithResetMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [ |
|
40
|
|
|
RequestOptions::JSON => $resetMeta, |
|
41
|
|
|
]); |
|
42
|
|
|
$metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editWithProvidedMeta->getStatusCode()); |
|
45
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editWithResetMeta->getStatusCode()); |
|
46
|
|
|
$this->assertEquals($resetMeta, $metaAfterResetting); |
|
47
|
|
|
self::assertArraySubset($meta, $metaAfterEditing); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function provideMeta(): iterable |
|
51
|
|
|
{ |
|
52
|
|
|
$now = Chronos::now(); |
|
53
|
|
|
|
|
54
|
|
|
yield [['validSince' => $now->addMonth()->toAtomString()]]; |
|
55
|
|
|
yield [['validUntil' => $now->subMonth()->toAtomString()]]; |
|
56
|
|
|
yield [['maxVisits' => 20]]; |
|
57
|
|
|
yield [['validUntil' => $now->addYear()->toAtomString(), 'maxVisits' => 100]]; |
|
58
|
|
|
yield [[ |
|
59
|
|
|
'validSince' => $now->subYear()->toAtomString(), |
|
60
|
|
|
'validUntil' => $now->addYear()->toAtomString(), |
|
61
|
|
|
'maxVisits' => 100, |
|
62
|
|
|
]]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function findShortUrlMetaByShortCode(string $shortCode): ?array |
|
66
|
|
|
{ |
|
67
|
|
|
$matchingShortUrl = $this->getJsonResponsePayload( |
|
68
|
|
|
$this->callApiWithKey(self::METHOD_GET, '/short-urls/' . $shortCode), |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $matchingShortUrl['meta'] ?? null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @test |
|
76
|
|
|
* @dataProvider provideLongUrls |
|
77
|
|
|
*/ |
|
78
|
|
|
public function longUrlCanBeEditedIfItIsValid(string $longUrl, int $expectedStatus, ?string $expectedError): void |
|
79
|
|
|
{ |
|
80
|
|
|
$shortCode = 'abc123'; |
|
81
|
|
|
$url = sprintf('/short-urls/%s', $shortCode); |
|
82
|
|
|
|
|
83
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => [ |
|
84
|
|
|
'longUrl' => $longUrl, |
|
85
|
|
|
]]); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals($expectedStatus, $resp->getStatusCode()); |
|
88
|
|
|
if ($expectedError !== null) { |
|
89
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
|
90
|
|
|
$this->assertEquals($expectedError, $payload['type']); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function provideLongUrls(): iterable |
|
95
|
|
|
{ |
|
96
|
|
|
yield 'valid URL' => ['https://shlink.io', self::STATUS_NO_CONTENT, null]; |
|
97
|
|
|
yield 'invalid URL' => ['htt:foo', self::STATUS_BAD_REQUEST, 'INVALID_URL']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @test |
|
102
|
|
|
* @dataProvider provideInvalidUrls |
|
103
|
|
|
*/ |
|
104
|
|
|
public function tryingToEditInvalidUrlReturnsNotFoundError( |
|
105
|
|
|
string $shortCode, |
|
106
|
|
|
?string $domain, |
|
107
|
|
|
string $expectedDetail |
|
108
|
|
|
): void { |
|
109
|
|
|
$url = $this->buildShortUrlPath($shortCode, $domain); |
|
110
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => []]); |
|
111
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); |
|
114
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']); |
|
115
|
|
|
$this->assertEquals('INVALID_SHORTCODE', $payload['type']); |
|
116
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']); |
|
117
|
|
|
$this->assertEquals('Short URL not found', $payload['title']); |
|
118
|
|
|
$this->assertEquals($shortCode, $payload['shortCode']); |
|
119
|
|
|
$this->assertEquals($domain, $payload['domain'] ?? null); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** @test */ |
|
123
|
|
|
public function providingInvalidDataReturnsBadRequest(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$expectedDetail = 'Provided data is not valid'; |
|
126
|
|
|
|
|
127
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => [ |
|
128
|
|
|
'maxVisits' => 'not_a_number', |
|
129
|
|
|
]]); |
|
130
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode()); |
|
133
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $payload['status']); |
|
134
|
|
|
$this->assertEquals('INVALID_ARGUMENT', $payload['type']); |
|
135
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']); |
|
136
|
|
|
$this->assertEquals('Invalid data', $payload['title']); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @test |
|
141
|
|
|
* @dataProvider provideDomains |
|
142
|
|
|
*/ |
|
143
|
|
|
public function metadataIsEditedOnProperShortUrlBasedOnDomain(?string $domain, string $expectedUrl): void |
|
144
|
|
|
{ |
|
145
|
|
|
$shortCode = 'ghi789'; |
|
146
|
|
|
$url = new Uri(sprintf('/short-urls/%s', $shortCode)); |
|
147
|
|
|
|
|
148
|
|
|
if ($domain !== null) { |
|
149
|
|
|
$url = $url->withQuery(build_query(['domain' => $domain])); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, (string) $url, [RequestOptions::JSON => [ |
|
153
|
|
|
'maxVisits' => 100, |
|
154
|
|
|
]]); |
|
155
|
|
|
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url)); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode()); |
|
158
|
|
|
$this->assertEquals($domain, $editedShortUrl['domain']); |
|
159
|
|
|
$this->assertEquals($expectedUrl, $editedShortUrl['longUrl']); |
|
160
|
|
|
$this->assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function provideDomains(): iterable |
|
164
|
|
|
{ |
|
165
|
|
|
yield 'domain' => [ |
|
166
|
|
|
'example.com', |
|
167
|
|
|
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/', |
|
168
|
|
|
]; |
|
169
|
|
|
yield 'no domain' => [null, 'https://shlink.io/documentation/']; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|