diff options
-rw-r--r-- | application/api/ApiUtils.php | 26 | ||||
-rw-r--r-- | application/api/controllers/Links.php | 42 | ||||
-rw-r--r-- | index.php | 1 | ||||
-rw-r--r-- | tests/api/ApiUtilsTest.php | 78 | ||||
-rw-r--r-- | tests/api/controllers/PutLinkTest.php | 199 |
5 files changed, 346 insertions, 0 deletions
diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index b8155a34..f154bb52 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php | |||
@@ -108,4 +108,30 @@ class ApiUtils | |||
108 | ]; | 108 | ]; |
109 | return $link; | 109 | return $link; |
110 | } | 110 | } |
111 | |||
112 | /** | ||
113 | * Update link fields using an updated link object. | ||
114 | * | ||
115 | * @param array $oldLink data | ||
116 | * @param array $newLink data | ||
117 | * | ||
118 | * @return array $oldLink updated with $newLink values | ||
119 | */ | ||
120 | public static function updateLink($oldLink, $newLink) | ||
121 | { | ||
122 | foreach (['title', 'url', 'description', 'tags', 'private'] as $field) { | ||
123 | $oldLink[$field] = $newLink[$field]; | ||
124 | } | ||
125 | $oldLink['updated'] = new \DateTime(); | ||
126 | |||
127 | if (empty($oldLink['url'])) { | ||
128 | $oldLink['url'] = '?' . $oldLink['shorturl']; | ||
129 | } | ||
130 | |||
131 | if (empty($oldLink['title'])) { | ||
132 | $oldLink['title'] = $oldLink['url']; | ||
133 | } | ||
134 | |||
135 | return $oldLink; | ||
136 | } | ||
111 | } | 137 | } |
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php index 0db10fd0..1c68b062 100644 --- a/application/api/controllers/Links.php +++ b/application/api/controllers/Links.php | |||
@@ -146,4 +146,46 @@ class Links extends ApiController | |||
146 | return $response->withAddedHeader('Location', $redirect) | 146 | return $response->withAddedHeader('Location', $redirect) |
147 | ->withJson($out, 201, $this->jsonStyle); | 147 | ->withJson($out, 201, $this->jsonStyle); |
148 | } | 148 | } |
149 | |||
150 | /** | ||
151 | * Updates an existing link from posted request body. | ||
152 | * | ||
153 | * @param Request $request Slim request. | ||
154 | * @param Response $response Slim response. | ||
155 | * @param array $args Path parameters. including the ID. | ||
156 | * | ||
157 | * @return Response response. | ||
158 | * | ||
159 | * @throws ApiLinkNotFoundException generating a 404 error. | ||
160 | */ | ||
161 | public function putLink($request, $response, $args) | ||
162 | { | ||
163 | if (! isset($this->linkDb[$args['id']])) { | ||
164 | throw new ApiLinkNotFoundException(); | ||
165 | } | ||
166 | |||
167 | $index = index_url($this->ci['environment']); | ||
168 | $data = $request->getParsedBody(); | ||
169 | |||
170 | $requestLink = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links')); | ||
171 | // duplicate URL on a different link, return 409 Conflict | ||
172 | if (! empty($requestLink['url']) | ||
173 | && ! empty($dup = $this->linkDb->getLinkFromUrl($requestLink['url'])) | ||
174 | && $dup['id'] != $args['id'] | ||
175 | ) { | ||
176 | return $response->withJson( | ||
177 | ApiUtils::formatLink($dup, $index), | ||
178 | 409, | ||
179 | $this->jsonStyle | ||
180 | ); | ||
181 | } | ||
182 | |||
183 | $responseLink = $this->linkDb[$args['id']]; | ||
184 | $responseLink = ApiUtils::updateLink($responseLink, $requestLink); | ||
185 | $this->linkDb[$responseLink['id']] = $responseLink; | ||
186 | $this->linkDb->save($this->conf->get('resource.page_cache')); | ||
187 | |||
188 | $out = ApiUtils::formatLink($responseLink, $index); | ||
189 | return $response->withJson($out, 200, $this->jsonStyle); | ||
190 | } | ||
149 | } | 191 | } |
@@ -2251,6 +2251,7 @@ $app->group('/api/v1', function() { | |||
2251 | $this->get('/links', '\Shaarli\Api\Controllers\Links:getLinks')->setName('getLinks'); | 2251 | $this->get('/links', '\Shaarli\Api\Controllers\Links:getLinks')->setName('getLinks'); |
2252 | $this->get('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:getLink')->setName('getLink'); | 2252 | $this->get('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:getLink')->setName('getLink'); |
2253 | $this->post('/links', '\Shaarli\Api\Controllers\Links:postLink')->setName('postLink'); | 2253 | $this->post('/links', '\Shaarli\Api\Controllers\Links:postLink')->setName('postLink'); |
2254 | $this->put('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:putLink')->setName('putLink'); | ||
2254 | })->add('\Shaarli\Api\ApiMiddleware'); | 2255 | })->add('\Shaarli\Api\ApiMiddleware'); |
2255 | 2256 | ||
2256 | $response = $app->run(true); | 2257 | $response = $app->run(true); |
diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php index b4431d1b..62baf4c5 100644 --- a/tests/api/ApiUtilsTest.php +++ b/tests/api/ApiUtilsTest.php | |||
@@ -271,4 +271,82 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase | |||
271 | 271 | ||
272 | $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); | 272 | $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); |
273 | } | 273 | } |
274 | |||
275 | /** | ||
276 | * Test updateLink with valid data, and also unnecessary fields. | ||
277 | */ | ||
278 | public function testUpdateLink() | ||
279 | { | ||
280 | $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); | ||
281 | $old = [ | ||
282 | 'id' => 12, | ||
283 | 'url' => '?abc', | ||
284 | 'shorturl' => 'abc', | ||
285 | 'title' => 'Note', | ||
286 | 'description' => '', | ||
287 | 'tags' => '', | ||
288 | 'private' => '', | ||
289 | 'created' => $created, | ||
290 | ]; | ||
291 | |||
292 | $new = [ | ||
293 | 'id' => 13, | ||
294 | 'shorturl' => 'nope', | ||
295 | 'url' => 'http://somewhere.else', | ||
296 | 'title' => 'Le Cid', | ||
297 | 'description' => 'Percé jusques au fond du cœur [...]', | ||
298 | 'tags' => 'corneille rodrigue', | ||
299 | 'private' => true, | ||
300 | 'created' => 'creation', | ||
301 | 'updated' => 'updation', | ||
302 | ]; | ||
303 | |||
304 | $result = ApiUtils::updateLink($old, $new); | ||
305 | $this->assertEquals(12, $result['id']); | ||
306 | $this->assertEquals('http://somewhere.else', $result['url']); | ||
307 | $this->assertEquals('abc', $result['shorturl']); | ||
308 | $this->assertEquals('Le Cid', $result['title']); | ||
309 | $this->assertEquals('Percé jusques au fond du cœur [...]', $result['description']); | ||
310 | $this->assertEquals('corneille rodrigue', $result['tags']); | ||
311 | $this->assertEquals(true, $result['private']); | ||
312 | $this->assertEquals($created, $result['created']); | ||
313 | $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']); | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * Test updateLink with minimal data. | ||
318 | */ | ||
319 | public function testUpdateLinkMinimal() | ||
320 | { | ||
321 | $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); | ||
322 | $old = [ | ||
323 | 'id' => 12, | ||
324 | 'url' => '?abc', | ||
325 | 'shorturl' => 'abc', | ||
326 | 'title' => 'Note', | ||
327 | 'description' => 'Interesting description!', | ||
328 | 'tags' => 'doggo', | ||
329 | 'private' => true, | ||
330 | 'created' => $created, | ||
331 | ]; | ||
332 | |||
333 | $new = [ | ||
334 | 'url' => '', | ||
335 | 'title' => '', | ||
336 | 'description' => '', | ||
337 | 'tags' => '', | ||
338 | 'private' => false, | ||
339 | ]; | ||
340 | |||
341 | $result = ApiUtils::updateLink($old, $new); | ||
342 | $this->assertEquals(12, $result['id']); | ||
343 | $this->assertEquals('?abc', $result['url']); | ||
344 | $this->assertEquals('abc', $result['shorturl']); | ||
345 | $this->assertEquals('?abc', $result['title']); | ||
346 | $this->assertEquals('', $result['description']); | ||
347 | $this->assertEquals('', $result['tags']); | ||
348 | $this->assertEquals(false, $result['private']); | ||
349 | $this->assertEquals($created, $result['created']); | ||
350 | $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']); | ||
351 | } | ||
274 | } | 352 | } |
diff --git a/tests/api/controllers/PutLinkTest.php b/tests/api/controllers/PutLinkTest.php new file mode 100644 index 00000000..4096c1a7 --- /dev/null +++ b/tests/api/controllers/PutLinkTest.php | |||
@@ -0,0 +1,199 @@ | |||
1 | <?php | ||
2 | |||
3 | |||
4 | namespace Shaarli\Api\Controllers; | ||
5 | |||
6 | |||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Slim\Container; | ||
9 | use Slim\Http\Environment; | ||
10 | use Slim\Http\Request; | ||
11 | use Slim\Http\Response; | ||
12 | |||
13 | class PutLinkTest extends \PHPUnit_Framework_TestCase | ||
14 | { | ||
15 | /** | ||
16 | * @var string datastore to test write operations | ||
17 | */ | ||
18 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
19 | |||
20 | /** | ||
21 | * @var ConfigManager instance | ||
22 | */ | ||
23 | protected $conf; | ||
24 | |||
25 | /** | ||
26 | * @var \ReferenceLinkDB instance. | ||
27 | */ | ||
28 | protected $refDB = null; | ||
29 | |||
30 | /** | ||
31 | * @var Container instance. | ||
32 | */ | ||
33 | protected $container; | ||
34 | |||
35 | /** | ||
36 | * @var Links controller instance. | ||
37 | */ | ||
38 | protected $controller; | ||
39 | |||
40 | /** | ||
41 | * Number of JSON field per link. | ||
42 | */ | ||
43 | const NB_FIELDS_LINK = 9; | ||
44 | |||
45 | /** | ||
46 | * Before every test, instantiate a new Api with its config, plugins and links. | ||
47 | */ | ||
48 | public function setUp() | ||
49 | { | ||
50 | $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); | ||
51 | $this->refDB = new \ReferenceLinkDB(); | ||
52 | $this->refDB->write(self::$testDatastore); | ||
53 | |||
54 | $this->container = new Container(); | ||
55 | $this->container['conf'] = $this->conf; | ||
56 | $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); | ||
57 | |||
58 | $this->controller = new Links($this->container); | ||
59 | |||
60 | // Used by index_url(). | ||
61 | $this->controller->getCi()['environment'] = [ | ||
62 | 'SERVER_NAME' => 'domain.tld', | ||
63 | 'SERVER_PORT' => 80, | ||
64 | 'SCRIPT_NAME' => '/', | ||
65 | ]; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * After every test, remove the test datastore. | ||
70 | */ | ||
71 | public function tearDown() | ||
72 | { | ||
73 | @unlink(self::$testDatastore); | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Test link update without value: reset the link to default values | ||
78 | */ | ||
79 | public function testPutLinkMinimal() | ||
80 | { | ||
81 | $env = Environment::mock([ | ||
82 | 'REQUEST_METHOD' => 'PUT', | ||
83 | ]); | ||
84 | $id = '41'; | ||
85 | $request = Request::createFromEnvironment($env); | ||
86 | |||
87 | $response = $this->controller->putLink($request, new Response(), ['id' => $id]); | ||
88 | $this->assertEquals(200, $response->getStatusCode()); | ||
89 | $data = json_decode((string) $response->getBody(), true); | ||
90 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
91 | $this->assertEquals($id, $data['id']); | ||
92 | $this->assertEquals('WDWyig', $data['shorturl']); | ||
93 | $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); | ||
94 | $this->assertEquals('?WDWyig', $data['title']); | ||
95 | $this->assertEquals('', $data['description']); | ||
96 | $this->assertEquals([], $data['tags']); | ||
97 | $this->assertEquals(false, $data['private']); | ||
98 | $this->assertEquals( | ||
99 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | ||
100 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
101 | ); | ||
102 | $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])); | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Test link update with new values | ||
107 | */ | ||
108 | public function testPutLinkWithValues() | ||
109 | { | ||
110 | $env = Environment::mock([ | ||
111 | 'REQUEST_METHOD' => 'PUT', | ||
112 | 'CONTENT_TYPE' => 'application/json' | ||
113 | ]); | ||
114 | $id = 41; | ||
115 | $update = [ | ||
116 | 'url' => 'http://somewhere.else', | ||
117 | 'title' => 'Le Cid', | ||
118 | 'description' => 'Percé jusques au fond du cœur [...]', | ||
119 | 'tags' => ['corneille', 'rodrigue'], | ||
120 | 'private' => true, | ||
121 | ]; | ||
122 | $request = Request::createFromEnvironment($env); | ||
123 | $request = $request->withParsedBody($update); | ||
124 | |||
125 | $response = $this->controller->putLink($request, new Response(), ['id' => $id]); | ||
126 | $this->assertEquals(200, $response->getStatusCode()); | ||
127 | $data = json_decode((string) $response->getBody(), true); | ||
128 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
129 | $this->assertEquals($id, $data['id']); | ||
130 | $this->assertEquals('WDWyig', $data['shorturl']); | ||
131 | $this->assertEquals('http://somewhere.else', $data['url']); | ||
132 | $this->assertEquals('Le Cid', $data['title']); | ||
133 | $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']); | ||
134 | $this->assertEquals(['corneille', 'rodrigue'], $data['tags']); | ||
135 | $this->assertEquals(true, $data['private']); | ||
136 | $this->assertEquals( | ||
137 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | ||
138 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
139 | ); | ||
140 | $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])); | ||
141 | } | ||
142 | |||
143 | /** | ||
144 | * Test link update with an existing URL: 409 Conflict with the existing link as body | ||
145 | */ | ||
146 | public function testPutLinkDuplicate() | ||
147 | { | ||
148 | $link = [ | ||
149 | 'url' => 'mediagoblin.org/', | ||
150 | 'title' => 'new entry', | ||
151 | 'description' => 'shaare description', | ||
152 | 'tags' => ['one', 'two'], | ||
153 | 'private' => true, | ||
154 | ]; | ||
155 | $env = Environment::mock([ | ||
156 | 'REQUEST_METHOD' => 'PUT', | ||
157 | 'CONTENT_TYPE' => 'application/json' | ||
158 | ]); | ||
159 | |||
160 | $request = Request::createFromEnvironment($env); | ||
161 | $request = $request->withParsedBody($link); | ||
162 | $response = $this->controller->putLink($request, new Response(), ['id' => 41]); | ||
163 | |||
164 | $this->assertEquals(409, $response->getStatusCode()); | ||
165 | $data = json_decode((string) $response->getBody(), true); | ||
166 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
167 | $this->assertEquals(7, $data['id']); | ||
168 | $this->assertEquals('IuWvgA', $data['shorturl']); | ||
169 | $this->assertEquals('http://mediagoblin.org/', $data['url']); | ||
170 | $this->assertEquals('MediaGoblin', $data['title']); | ||
171 | $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']); | ||
172 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | ||
173 | $this->assertEquals(false, $data['private']); | ||
174 | $this->assertEquals( | ||
175 | \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), | ||
176 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
177 | ); | ||
178 | $this->assertEquals( | ||
179 | \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), | ||
180 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | ||
181 | ); | ||
182 | } | ||
183 | |||
184 | /** | ||
185 | * Test link update on non existent link => ApiLinkNotFoundException. | ||
186 | * | ||
187 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
188 | * @expectedExceptionMessage Link not found | ||
189 | */ | ||
190 | public function testGetLink404() | ||
191 | { | ||
192 | $env = Environment::mock([ | ||
193 | 'REQUEST_METHOD' => 'PUT', | ||
194 | ]); | ||
195 | $request = Request::createFromEnvironment($env); | ||
196 | |||
197 | $this->controller->putLink($request, new Response(), ['id' => -1]); | ||
198 | } | ||
199 | } | ||