Merge pull request #769 from ArthurHoaro/api/getlinks-visibility

REST API - getLinks: support the visibility parameter
This commit is contained in:
ArthurHoaro 2017-02-13 08:41:12 +01:00 committed by GitHub
commit 65e56cbe49
2 changed files with 24 additions and 31 deletions

View file

@ -34,15 +34,14 @@ class Links extends ApiController
*/ */
public function getLinks($request, $response) public function getLinks($request, $response)
{ {
$private = $request->getParam('private'); $private = $request->getParam('visibility');
$links = $this->linkDb->filterSearch( $links = $this->linkDb->filterSearch(
[ [
'searchtags' => $request->getParam('searchtags', ''), 'searchtags' => $request->getParam('searchtags', ''),
'searchterm' => $request->getParam('searchterm', ''), 'searchterm' => $request->getParam('searchterm', ''),
], ],
false, false,
// to updated in another PR depending on the API doc $private
($private === 'true' || $private === '1') ? 'private' : 'all'
); );
// Return links from the {offset}th link, starting from 0. // Return links from the {offset}th link, starting from 0.

View file

@ -188,25 +188,33 @@ class LinksTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test getLinks with private attribute to 1 or true. * Test getLinks with visibility parameter set to all
*/ */
public function testGetLinksPrivate() public function testGetLinksVisibilityAll()
{ {
$env = Environment::mock([ $env = Environment::mock(
'REQUEST_METHOD' => 'GET', [
'QUERY_STRING' => 'private=true' 'REQUEST_METHOD' => 'GET',
]); 'QUERY_STRING' => 'visibility=all'
]
);
$request = Request::createFromEnvironment($env); $request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response()); $response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true); $data = json_decode((string)$response->getBody(), true);
$this->assertEquals($this->refDB->countPrivateLinks(), count($data)); $this->assertEquals($this->refDB->countLinks(), count($data));
$this->assertEquals(6, $data[0]['id']); $this->assertEquals(41, $data[0]['id']);
$this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
}
/**
* Test getLinks with visibility parameter set to private
*/
public function testGetLinksVisibilityPrivate()
{
$env = Environment::mock([ $env = Environment::mock([
'REQUEST_METHOD' => 'GET', 'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'private=1' 'QUERY_STRING' => 'visibility=private'
]); ]);
$request = Request::createFromEnvironment($env); $request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response()); $response = $this->controller->getLinks($request, new Response());
@ -218,35 +226,21 @@ class LinksTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test getLinks with private attribute to false or 0 * Test getLinks with visibility parameter set to public
*/ */
public function testGetLinksNotPrivate() public function testGetLinksVisibilityPublic()
{ {
$env = Environment::mock( $env = Environment::mock(
[ [
'REQUEST_METHOD' => 'GET', 'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'private=0' 'QUERY_STRING' => 'visibility=public'
] ]
); );
$request = Request::createFromEnvironment($env); $request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response()); $response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string)$response->getBody(), true); $data = json_decode((string)$response->getBody(), true);
$this->assertEquals($this->refDB->countLinks(), count($data)); $this->assertEquals($this->refDB->countPublicLinks(), count($data));
$this->assertEquals(41, $data[0]['id']);
$this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
$env = Environment::mock(
[
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'private=false'
]
);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string)$response->getBody(), true);
$this->assertEquals($this->refDB->countLinks(), count($data));
$this->assertEquals(41, $data[0]['id']); $this->assertEquals(41, $data[0]['id']);
$this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
} }