From d3f42ca487287447efb81061609644108044a038 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 19 May 2018 15:04:04 +0200 Subject: Implements Tags endpoints for Shaarli's REST API Endpoints: * List All Tags [GET] * Get a tag [GET] * Update a tag [PUT] * Delete a tag [DELETE] Fixes #904 References shaarli/api-documentation#34 --- tests/api/controllers/tags/GetTagsTest.php | 209 +++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 tests/api/controllers/tags/GetTagsTest.php (limited to 'tests/api/controllers/tags/GetTagsTest.php') diff --git a/tests/api/controllers/tags/GetTagsTest.php b/tests/api/controllers/tags/GetTagsTest.php new file mode 100644 index 00000000..cf066bc3 --- /dev/null +++ b/tests/api/controllers/tags/GetTagsTest.php @@ -0,0 +1,209 @@ +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->linkDB = new \LinkDB(self::$testDatastore, true, false); + $this->container['db'] = $this->linkDB; + $this->container['history'] = null; + + $this->controller = new Tags($this->container); + } + + /** + * After every test, remove the test datastore. + */ + public function tearDown() + { + @unlink(self::$testDatastore); + } + + /** + * Test basic getLinks service: returns all tags. + */ + public function testGetTagsAll() + { + $tags = $this->linkDB->linksCountPerTag(); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + ]); + $request = Request::createFromEnvironment($env); + + $response = $this->controller->getTags($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(count($tags), count($data)); + + // Check order + $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); + $this->assertEquals('web', $data[0]['name']); + $this->assertEquals(4, $data[0]['occurrences']); + $this->assertEquals(self::NB_FIELDS_TAG, count($data[1])); + $this->assertEquals('cartoon', $data[1]['name']); + $this->assertEquals(3, $data[1]['occurrences']); + // Case insensitive + $this->assertEquals(self::NB_FIELDS_TAG, count($data[2])); + $this->assertEquals('sTuff', $data[2]['name']); + $this->assertEquals(2, $data[2]['occurrences']); + // End + $this->assertEquals(self::NB_FIELDS_TAG, count($data[count($data) - 1])); + $this->assertEquals('ut', $data[count($data) - 1]['name']); + $this->assertEquals(1, $data[count($data) - 1]['occurrences']); + } + + /** + * Test getTags service with offset and limit parameter: + * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences + */ + public function testGetTagsOffsetLimit() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'offset=1&limit=1' + ]); + $request = Request::createFromEnvironment($env); + $response = $this->controller->getTags($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(1, count($data)); + $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); + $this->assertEquals('cartoon', $data[0]['name']); + $this->assertEquals(3, $data[0]['occurrences']); + } + + /** + * Test getTags with limit=all (return all tags). + */ + public function testGetTagsLimitAll() + { + $tags = $this->linkDB->linksCountPerTag(); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'limit=all' + ]); + $request = Request::createFromEnvironment($env); + $response = $this->controller->getTags($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(count($tags), count($data)); + } + + /** + * Test getTags service with offset and limit parameter: + * limit=1 and offset=1 should not return any tag + */ + public function testGetTagsOffsetTooHigh() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'offset=100' + ]); + $request = Request::createFromEnvironment($env); + $response = $this->controller->getTags($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEmpty(count($data)); + } + + /** + * Test getTags with visibility parameter set to private + */ + public function testGetTagsVisibilityPrivate() + { + $tags = $this->linkDB->linksCountPerTag([], 'private'); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'visibility=private' + ]); + $request = Request::createFromEnvironment($env); + $response = $this->controller->getTags($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(count($tags), count($data)); + $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); + $this->assertEquals('css', $data[0]['name']); + $this->assertEquals(1, $data[0]['occurrences']); + } + + /** + * Test getTags with visibility parameter set to public + */ + public function testGetTagsVisibilityPublic() + { + $tags = $this->linkDB->linksCountPerTag([], 'public'); + $env = Environment::mock( + [ + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => 'visibility=public' + ] + ); + $request = Request::createFromEnvironment($env); + $response = $this->controller->getTags($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string)$response->getBody(), true); + $this->assertEquals(count($tags), count($data)); + $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); + $this->assertEquals('web', $data[0]['name']); + $this->assertEquals(3, $data[0]['occurrences']); + } +} -- cgit v1.2.3