diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-12-29 12:50:23 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-12-29 13:01:04 +0100 |
commit | 0640c1a6db6d9a13e5d0079f0bf42497010edbc7 (patch) | |
tree | 17002fc6a277d21f728f103434bd8a70561affa5 /tests/api/controllers/links/PostLinkTest.php | |
parent | fe58bdcd9e0ddca8a2a99142dc9eaee8845efa67 (diff) | |
download | Shaarli-0640c1a6db6d9a13e5d0079f0bf42497010edbc7.tar.gz Shaarli-0640c1a6db6d9a13e5d0079f0bf42497010edbc7.tar.zst Shaarli-0640c1a6db6d9a13e5d0079f0bf42497010edbc7.zip |
API: POST/PUT Link - properly parse tags string
Even though the documentation specify that tags should be passed as an array, tags string is actually allowed. So this adds a proper parsing with configured separator.
Related to #1651
Diffstat (limited to 'tests/api/controllers/links/PostLinkTest.php')
-rw-r--r-- | tests/api/controllers/links/PostLinkTest.php | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php index e12f803b..f755e2d2 100644 --- a/tests/api/controllers/links/PostLinkTest.php +++ b/tests/api/controllers/links/PostLinkTest.php | |||
@@ -229,4 +229,52 @@ class PostLinkTest extends TestCase | |||
229 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | 229 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) |
230 | ); | 230 | ); |
231 | } | 231 | } |
232 | |||
233 | /** | ||
234 | * Test link creation with a tag string provided | ||
235 | */ | ||
236 | public function testPostLinkWithTagString(): void | ||
237 | { | ||
238 | $link = [ | ||
239 | 'tags' => 'one two', | ||
240 | ]; | ||
241 | $env = Environment::mock([ | ||
242 | 'REQUEST_METHOD' => 'POST', | ||
243 | 'CONTENT_TYPE' => 'application/json' | ||
244 | ]); | ||
245 | |||
246 | $request = Request::createFromEnvironment($env); | ||
247 | $request = $request->withParsedBody($link); | ||
248 | $response = $this->controller->postLink($request, new Response()); | ||
249 | |||
250 | $this->assertEquals(201, $response->getStatusCode()); | ||
251 | $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]); | ||
252 | $data = json_decode((string) $response->getBody(), true); | ||
253 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
254 | $this->assertEquals(['one', 'two'], $data['tags']); | ||
255 | } | ||
256 | |||
257 | /** | ||
258 | * Test link creation with a tag string provided | ||
259 | */ | ||
260 | public function testPostLinkWithTagString2(): void | ||
261 | { | ||
262 | $link = [ | ||
263 | 'tags' => ['one two'], | ||
264 | ]; | ||
265 | $env = Environment::mock([ | ||
266 | 'REQUEST_METHOD' => 'POST', | ||
267 | 'CONTENT_TYPE' => 'application/json' | ||
268 | ]); | ||
269 | |||
270 | $request = Request::createFromEnvironment($env); | ||
271 | $request = $request->withParsedBody($link); | ||
272 | $response = $this->controller->postLink($request, new Response()); | ||
273 | |||
274 | $this->assertEquals(201, $response->getStatusCode()); | ||
275 | $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]); | ||
276 | $data = json_decode((string) $response->getBody(), true); | ||
277 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
278 | $this->assertEquals(['one', 'two'], $data['tags']); | ||
279 | } | ||
232 | } | 280 | } |