From 16e3d006e9e9386001881053f610657525feb188 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 24 Dec 2016 10:30:21 +0100 Subject: REST API: implements getLink by ID service See http://shaarli.github.io/api-documentation/#links-link-get --- tests/api/controllers/GetLinksTest.php | 388 +++++++++++++++++++++++++++++++++ 1 file changed, 388 insertions(+) create mode 100644 tests/api/controllers/GetLinksTest.php (limited to 'tests/api/controllers/GetLinksTest.php') diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/GetLinksTest.php new file mode 100644 index 00000000..da54fcf1 --- /dev/null +++ b/tests/api/controllers/GetLinksTest.php @@ -0,0 +1,388 @@ +conf = new \ConfigManager('tests/utils/config/configJson'); + $this->refDB = new \ReferenceLinkDB(); + $this->refDB->write(self::$testDatastore); + + $this->container = new Container(); + $this->container['conf'] = $this->conf; + $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); + + $this->controller = new Links($this->container); + } + + /** + * After every test, remove the test datastore. + */ + public function tearDown() + { + @unlink(self::$testDatastore); + } + + /** + * Test basic getLinks service: returns all links. + */ + public function testGetLinks() + { + // Used by index_url(). + $_SERVER['SERVER_NAME'] = 'domain.tld'; + $_SERVER['SERVER_PORT'] = 80; + $_SERVER['SCRIPT_NAME'] = '/'; + + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + ]); + $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)); + + // Check order + $order = [41, 8, 6, 7, 0, 1, 4, 42]; + $cpt = 0; + foreach ($data as $link) { + $this->assertEquals(self::NB_FIELDS_LINK, count($link)); + $this->assertEquals($order[$cpt++], $link['id']); + } + + // Check first element fields + $first = $data[0]; + $this->assertEquals('http://domain.tld/?WDWyig', $first['url']); + $this->assertEquals('WDWyig', $first['shorturl']); + $this->assertEquals('Link title: @website', $first['title']); + $this->assertEquals( + 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', + $first['description'] + ); + $this->assertEquals('sTuff', $first['tags'][0]); + $this->assertEquals(false, $first['private']); + $this->assertEquals( + \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), + $first['created'] + ); + $this->assertEmpty($first['updated']); + + // Multi tags + $link = $data[1]; + $this->assertEquals(7, count($link['tags'])); + + // Update date + $this->assertEquals( + \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM), + $link['updated'] + ); + } + + /** + * Test getLinks service with offset and limit parameter: + * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC). + */ + public function testGetLinksOffsetLimit() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'offset=1&limit=1' + ]); + $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(1, count($data)); + $this->assertEquals(8, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + } + + /** + * Test getLinks with limit=all (return all link). + */ + public function testGetLinksLimitAll() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'limit=all' + ]); + $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)); + // Check order + $order = [41, 8, 6, 7, 0, 1, 4, 42]; + $cpt = 0; + foreach ($data as $link) { + $this->assertEquals(self::NB_FIELDS_LINK, count($link)); + $this->assertEquals($order[$cpt++], $link['id']); + } + } + + /** + * Test getLinks service with offset and limit parameter: + * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC). + */ + public function testGetLinksOffsetTooHigh() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'offset=100' + ]); + $request = Request::createFromEnvironment($env); + $response = $this->controller->getLinks($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEmpty(count($data)); + } + + /** + * Test getLinks with visibility parameter set to all + */ + public function testGetLinksVisibilityAll() + { + $env = Environment::mock( + [ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'visibility=all' + ] + ); + $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(self::NB_FIELDS_LINK, count($data[0])); + } + + /** + * Test getLinks with visibility parameter set to private + */ + public function testGetLinksVisibilityPrivate() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'visibility=private' + ]); + $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->countPrivateLinks(), count($data)); + $this->assertEquals(6, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + } + + /** + * Test getLinks with visibility parameter set to public + */ + public function testGetLinksVisibilityPublic() + { + $env = Environment::mock( + [ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'visibility=public' + ] + ); + $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->countPublicLinks(), count($data)); + $this->assertEquals(41, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + } + + /** + * Test getLinks service with offset and limit parameter: + * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC). + */ + public function testGetLinksSearchTerm() + { + // Only in description - 1 result + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm=Tropical' + ]); + $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(1, count($data)); + $this->assertEquals(1, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + + // Only in tags - 1 result + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm=tag3' + ]); + $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(1, count($data)); + $this->assertEquals(0, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + + // Multiple results (2) + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm=stallman' + ]); + $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(2, count($data)); + $this->assertEquals(41, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + $this->assertEquals(8, $data[1]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[1])); + + // Multiword - 2 results + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm=stallman+software' + ]); + $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(2, count($data)); + $this->assertEquals(41, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + $this->assertEquals(8, $data[1]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[1])); + + // URL encoding + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm='. urlencode('@web') + ]); + $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(2, count($data)); + $this->assertEquals(41, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + $this->assertEquals(8, $data[1]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[1])); + } + + public function testGetLinksSearchTermNoResult() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm=nope' + ]); + $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(0, count($data)); + } + + public function testGetLinksSearchTags() + { + // Single tag + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=dev', + ]); + $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(2, count($data)); + $this->assertEquals(0, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + $this->assertEquals(4, $data[1]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[1])); + + // Multitag + exclude + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=stuff+-gnu', + ]); + $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(1, count($data)); + $this->assertEquals(41, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + } + + /** + * Test getLinks service with search tags+terms. + */ + public function testGetLinksSearchTermsAndTags() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchterm=poke&searchtags=dev', + ]); + $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(1, count($data)); + $this->assertEquals(0, $data[0]['id']); + $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + } +} -- cgit v1.2.3 From 3c66e56435359dc678048193e8ee239d06f79b64 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Fri, 3 Mar 2017 23:06:12 +0100 Subject: application: introduce the Shaarli\Config namespace Namespaces have been introduced with the REST API, and should be generalized to the whole codebase to manage object scope and benefit from autoloading. See: - https://secure.php.net/manual/en/language.namespaces.php - http://www.php-fig.org/psr/psr-4/ Signed-off-by: VirtualTam --- tests/api/controllers/GetLinksTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/api/controllers/GetLinksTest.php') diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/GetLinksTest.php index da54fcf1..10330cd9 100644 --- a/tests/api/controllers/GetLinksTest.php +++ b/tests/api/controllers/GetLinksTest.php @@ -1,7 +1,7 @@ conf = new \ConfigManager('tests/utils/config/configJson'); + $this->conf = new ConfigManager('tests/utils/config/configJson'); $this->refDB = new \ReferenceLinkDB(); $this->refDB->write(self::$testDatastore); -- cgit v1.2.3 From 813849e5216cb87121e0f778a734575be6a36052 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 7 May 2017 16:50:20 +0200 Subject: Add history entries for API endpoint CHANGED: datetime is now store as an object in history store file --- tests/api/controllers/GetLinksTest.php | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/api/controllers/GetLinksTest.php') diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/GetLinksTest.php index 10330cd9..84ae7f7a 100644 --- a/tests/api/controllers/GetLinksTest.php +++ b/tests/api/controllers/GetLinksTest.php @@ -61,6 +61,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase $this->container = new Container(); $this->container['conf'] = $this->conf; $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); + $this->container['history'] = null; $this->controller = new Links($this->container); } -- cgit v1.2.3 From 7d86f40bdb2135655b5b4fe8cbcc1ac102114f86 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 1 Apr 2017 12:17:37 +0200 Subject: Empty tag search will look for not tagged links Fixes #784 From now, searching for tags with an empty value will return only not tagged links, with the search bar showing `x results [not tagged]`. Note that using the api, the searchtags request parameter must be set to `false` to get the same result. - [ ] Update API doc --- tests/api/controllers/GetLinksTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/api/controllers/GetLinksTest.php') diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/GetLinksTest.php index 10330cd9..f1b262bc 100644 --- a/tests/api/controllers/GetLinksTest.php +++ b/tests/api/controllers/GetLinksTest.php @@ -94,7 +94,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->refDB->countLinks(), count($data)); // Check order - $order = [41, 8, 6, 7, 0, 1, 4, 42]; + $order = [41, 8, 6, 7, 0, 1, 9, 4, 42]; $cpt = 0; foreach ($data as $link) { $this->assertEquals(self::NB_FIELDS_LINK, count($link)); @@ -163,7 +163,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase $data = json_decode((string) $response->getBody(), true); $this->assertEquals($this->refDB->countLinks(), count($data)); // Check order - $order = [41, 8, 6, 7, 0, 1, 4, 42]; + $order = [41, 8, 6, 7, 0, 1, 9, 4, 42]; $cpt = 0; foreach ($data as $link) { $this->assertEquals(self::NB_FIELDS_LINK, count($link)); -- cgit v1.2.3 From 341527bae96e0d1c6a0cb5dfc790eacb3ef58bf8 Mon Sep 17 00:00:00 2001 From: Willi Eggeling Date: Sat, 26 Aug 2017 23:05:02 +0200 Subject: wildcard tag search support - when searching for tags you can now include '*' as wildcard placeholder - new search reduces overall overhead when filtering for tags - fixed combination with description tag search ('#' prefix) - tests added --- tests/api/controllers/GetLinksTest.php | 83 ++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'tests/api/controllers/GetLinksTest.php') diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/GetLinksTest.php index 4cb70224..d22ed3bf 100644 --- a/tests/api/controllers/GetLinksTest.php +++ b/tests/api/controllers/GetLinksTest.php @@ -367,6 +367,89 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase $this->assertEquals(1, count($data)); $this->assertEquals(41, $data[0]['id']); $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); + + // wildcard: placeholder at the start + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=*Tuff', + ]); + $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(2, count($data)); + $this->assertEquals(41, $data[0]['id']); + + // wildcard: placeholder at the end + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=c*', + ]); + $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(4, count($data)); + $this->assertEquals(6, $data[0]['id']); + + // wildcard: placeholder at the middle + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=w*b', + ]); + $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(4, count($data)); + $this->assertEquals(6, $data[0]['id']); + + // wildcard: match all + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=*', + ]); + $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(9, count($data)); + $this->assertEquals(41, $data[0]['id']); + + // wildcard: optional ('*' does not need to expand) + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=*stuff*', + ]); + $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(2, count($data)); + $this->assertEquals(41, $data[0]['id']); + + // wildcard: exclusions + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=*a*+-*e*', + ]); + $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(1, count($data)); + $this->assertEquals(41, $data[0]['id']); // finds '#hashtag' in descr. + + // wildcard: exclude all + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'searchtags=-*', + ]); + $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(0, count($data)); } /** -- cgit v1.2.3