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 provideInvalidUrls |
77
|
|
|
*/ |
78
|
|
|
public function tryingToEditInvalidUrlReturnsNotFoundError( |
79
|
|
|
string $shortCode, |
80
|
|
|
?string $domain, |
81
|
|
|
string $expectedDetail |
82
|
|
|
): void { |
83
|
|
|
$url = $this->buildShortUrlPath($shortCode, $domain); |
84
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => []]); |
85
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
86
|
|
|
|
87
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); |
88
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']); |
89
|
|
|
$this->assertEquals('INVALID_SHORTCODE', $payload['type']); |
90
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']); |
91
|
|
|
$this->assertEquals('Short URL not found', $payload['title']); |
92
|
|
|
$this->assertEquals($shortCode, $payload['shortCode']); |
93
|
|
|
$this->assertEquals($domain, $payload['domain'] ?? null); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @test */ |
97
|
|
|
public function providingInvalidDataReturnsBadRequest(): void |
98
|
|
|
{ |
99
|
|
|
$expectedDetail = 'Provided data is not valid'; |
100
|
|
|
|
101
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => [ |
102
|
|
|
'maxVisits' => 'not_a_number', |
103
|
|
|
]]); |
104
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
105
|
|
|
|
106
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode()); |
107
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $payload['status']); |
108
|
|
|
$this->assertEquals('INVALID_ARGUMENT', $payload['type']); |
109
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']); |
110
|
|
|
$this->assertEquals('Invalid data', $payload['title']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @test |
115
|
|
|
* @dataProvider provideDomains |
116
|
|
|
*/ |
117
|
|
|
public function metadataIsEditedOnProperShortUrlBasedOnDomain(?string $domain, string $expectedUrl): void |
118
|
|
|
{ |
119
|
|
|
$shortCode = 'ghi789'; |
120
|
|
|
$url = new Uri(sprintf('/short-urls/%s', $shortCode)); |
121
|
|
|
|
122
|
|
|
if ($domain !== null) { |
123
|
|
|
$url = $url->withQuery(build_query(['domain' => $domain])); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, (string) $url, [RequestOptions::JSON => [ |
127
|
|
|
'maxVisits' => 100, |
128
|
|
|
]]); |
129
|
|
|
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url)); |
130
|
|
|
|
131
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode()); |
132
|
|
|
$this->assertEquals($domain, $editedShortUrl['domain']); |
133
|
|
|
$this->assertEquals($expectedUrl, $editedShortUrl['longUrl']); |
134
|
|
|
$this->assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function provideDomains(): iterable |
138
|
|
|
{ |
139
|
|
|
yield 'domain' => [ |
140
|
|
|
'example.com', |
141
|
|
|
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/', |
142
|
|
|
]; |
143
|
|
|
yield 'no domain' => [null, 'https://shlink.io/documentation/']; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|