aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-02-13 08:41:12 +0100
committerGitHub <noreply@github.com>2017-02-13 08:41:12 +0100
commit65e56cbe49a7eb2a0cb09dda85daab3b921472ee (patch)
tree3e1d42f51fc179d06cfe50531a755fd37df41468
parent5f3f19f1c0f1a4feac20c40fe9ee8437469450f3 (diff)
parentc37a6f820b0a213ee2d5980a96aafac262aeb97a (diff)
downloadShaarli-65e56cbe49a7eb2a0cb09dda85daab3b921472ee.tar.gz
Shaarli-65e56cbe49a7eb2a0cb09dda85daab3b921472ee.tar.zst
Shaarli-65e56cbe49a7eb2a0cb09dda85daab3b921472ee.zip
Merge pull request #769 from ArthurHoaro/api/getlinks-visibility
REST API - getLinks: support the visibility parameter
-rw-r--r--application/api/controllers/Links.php5
-rw-r--r--tests/api/controllers/LinksTest.php50
2 files changed, 24 insertions, 31 deletions
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php
index 01b7e783..0a7968e3 100644
--- a/application/api/controllers/Links.php
+++ b/application/api/controllers/Links.php
@@ -34,15 +34,14 @@ class Links extends ApiController
34 */ 34 */
35 public function getLinks($request, $response) 35 public function getLinks($request, $response)
36 { 36 {
37 $private = $request->getParam('private'); 37 $private = $request->getParam('visibility');
38 $links = $this->linkDb->filterSearch( 38 $links = $this->linkDb->filterSearch(
39 [ 39 [
40 'searchtags' => $request->getParam('searchtags', ''), 40 'searchtags' => $request->getParam('searchtags', ''),
41 'searchterm' => $request->getParam('searchterm', ''), 41 'searchterm' => $request->getParam('searchterm', ''),
42 ], 42 ],
43 false, 43 false,
44 // to updated in another PR depending on the API doc 44 $private
45 ($private === 'true' || $private === '1') ? 'private' : 'all'
46 ); 45 );
47 46
48 // Return links from the {offset}th link, starting from 0. 47 // Return links from the {offset}th link, starting from 0.
diff --git a/tests/api/controllers/LinksTest.php b/tests/api/controllers/LinksTest.php
index 4ead26b9..284c3a9d 100644
--- a/tests/api/controllers/LinksTest.php
+++ b/tests/api/controllers/LinksTest.php
@@ -188,25 +188,33 @@ class LinksTest extends \PHPUnit_Framework_TestCase
188 } 188 }
189 189
190 /** 190 /**
191 * Test getLinks with private attribute to 1 or true. 191 * Test getLinks with visibility parameter set to all
192 */ 192 */
193 public function testGetLinksPrivate() 193 public function testGetLinksVisibilityAll()
194 { 194 {
195 $env = Environment::mock([ 195 $env = Environment::mock(
196 'REQUEST_METHOD' => 'GET', 196 [
197 'QUERY_STRING' => 'private=true' 197 'REQUEST_METHOD' => 'GET',
198 ]); 198 'QUERY_STRING' => 'visibility=all'
199 ]
200 );
199 $request = Request::createFromEnvironment($env); 201 $request = Request::createFromEnvironment($env);
200 $response = $this->controller->getLinks($request, new Response()); 202 $response = $this->controller->getLinks($request, new Response());
201 $this->assertEquals(200, $response->getStatusCode()); 203 $this->assertEquals(200, $response->getStatusCode());
202 $data = json_decode((string) $response->getBody(), true); 204 $data = json_decode((string)$response->getBody(), true);
203 $this->assertEquals($this->refDB->countPrivateLinks(), count($data)); 205 $this->assertEquals($this->refDB->countLinks(), count($data));
204 $this->assertEquals(6, $data[0]['id']); 206 $this->assertEquals(41, $data[0]['id']);
205 $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); 207 $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
208 }
206 209
210 /**
211 * Test getLinks with visibility parameter set to private
212 */
213 public function testGetLinksVisibilityPrivate()
214 {
207 $env = Environment::mock([ 215 $env = Environment::mock([
208 'REQUEST_METHOD' => 'GET', 216 'REQUEST_METHOD' => 'GET',
209 'QUERY_STRING' => 'private=1' 217 'QUERY_STRING' => 'visibility=private'
210 ]); 218 ]);
211 $request = Request::createFromEnvironment($env); 219 $request = Request::createFromEnvironment($env);
212 $response = $this->controller->getLinks($request, new Response()); 220 $response = $this->controller->getLinks($request, new Response());
@@ -218,35 +226,21 @@ class LinksTest extends \PHPUnit_Framework_TestCase
218 } 226 }
219 227
220 /** 228 /**
221 * Test getLinks with private attribute to false or 0 229 * Test getLinks with visibility parameter set to public
222 */ 230 */
223 public function testGetLinksNotPrivate() 231 public function testGetLinksVisibilityPublic()
224 { 232 {
225 $env = Environment::mock( 233 $env = Environment::mock(
226 [ 234 [
227 'REQUEST_METHOD' => 'GET', 235 'REQUEST_METHOD' => 'GET',
228 'QUERY_STRING' => 'private=0' 236 'QUERY_STRING' => 'visibility=public'
229 ]
230 );
231 $request = Request::createFromEnvironment($env);
232 $response = $this->controller->getLinks($request, new Response());
233 $this->assertEquals(200, $response->getStatusCode());
234 $data = json_decode((string)$response->getBody(), true);
235 $this->assertEquals($this->refDB->countLinks(), count($data));
236 $this->assertEquals(41, $data[0]['id']);
237 $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
238
239 $env = Environment::mock(
240 [
241 'REQUEST_METHOD' => 'GET',
242 'QUERY_STRING' => 'private=false'
243 ] 237 ]
244 ); 238 );
245 $request = Request::createFromEnvironment($env); 239 $request = Request::createFromEnvironment($env);
246 $response = $this->controller->getLinks($request, new Response()); 240 $response = $this->controller->getLinks($request, new Response());
247 $this->assertEquals(200, $response->getStatusCode()); 241 $this->assertEquals(200, $response->getStatusCode());
248 $data = json_decode((string)$response->getBody(), true); 242 $data = json_decode((string)$response->getBody(), true);
249 $this->assertEquals($this->refDB->countLinks(), count($data)); 243 $this->assertEquals($this->refDB->countPublicLinks(), count($data));
250 $this->assertEquals(41, $data[0]['id']); 244 $this->assertEquals(41, $data[0]['id']);
251 $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); 245 $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
252 } 246 }