From cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 1 Apr 2017 11:11:25 +0200 Subject: REST API: implement PUT method * Related to #609 * Documentation: http://shaarli.github.io/api-documentation/#links-link-put --- tests/api/ApiUtilsTest.php | 78 +++++++++++++ tests/api/controllers/PutLinkTest.php | 199 ++++++++++++++++++++++++++++++++++ 2 files changed, 277 insertions(+) create mode 100644 tests/api/controllers/PutLinkTest.php (limited to 'tests/api') 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 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); } + + /** + * Test updateLink with valid data, and also unnecessary fields. + */ + public function testUpdateLink() + { + $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); + $old = [ + 'id' => 12, + 'url' => '?abc', + 'shorturl' => 'abc', + 'title' => 'Note', + 'description' => '', + 'tags' => '', + 'private' => '', + 'created' => $created, + ]; + + $new = [ + 'id' => 13, + 'shorturl' => 'nope', + 'url' => 'http://somewhere.else', + 'title' => 'Le Cid', + 'description' => 'Percé jusques au fond du cœur [...]', + 'tags' => 'corneille rodrigue', + 'private' => true, + 'created' => 'creation', + 'updated' => 'updation', + ]; + + $result = ApiUtils::updateLink($old, $new); + $this->assertEquals(12, $result['id']); + $this->assertEquals('http://somewhere.else', $result['url']); + $this->assertEquals('abc', $result['shorturl']); + $this->assertEquals('Le Cid', $result['title']); + $this->assertEquals('Percé jusques au fond du cœur [...]', $result['description']); + $this->assertEquals('corneille rodrigue', $result['tags']); + $this->assertEquals(true, $result['private']); + $this->assertEquals($created, $result['created']); + $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']); + } + + /** + * Test updateLink with minimal data. + */ + public function testUpdateLinkMinimal() + { + $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); + $old = [ + 'id' => 12, + 'url' => '?abc', + 'shorturl' => 'abc', + 'title' => 'Note', + 'description' => 'Interesting description!', + 'tags' => 'doggo', + 'private' => true, + 'created' => $created, + ]; + + $new = [ + 'url' => '', + 'title' => '', + 'description' => '', + 'tags' => '', + 'private' => false, + ]; + + $result = ApiUtils::updateLink($old, $new); + $this->assertEquals(12, $result['id']); + $this->assertEquals('?abc', $result['url']); + $this->assertEquals('abc', $result['shorturl']); + $this->assertEquals('?abc', $result['title']); + $this->assertEquals('', $result['description']); + $this->assertEquals('', $result['tags']); + $this->assertEquals(false, $result['private']); + $this->assertEquals($created, $result['created']); + $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']); + } } 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 @@ +conf = new ConfigManager('tests/utils/config/configJson.json.php'); + $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); + + // Used by index_url(). + $this->controller->getCi()['environment'] = [ + 'SERVER_NAME' => 'domain.tld', + 'SERVER_PORT' => 80, + 'SCRIPT_NAME' => '/', + ]; + } + + /** + * After every test, remove the test datastore. + */ + public function tearDown() + { + @unlink(self::$testDatastore); + } + + /** + * Test link update without value: reset the link to default values + */ + public function testPutLinkMinimal() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'PUT', + ]); + $id = '41'; + $request = Request::createFromEnvironment($env); + + $response = $this->controller->putLink($request, new Response(), ['id' => $id]); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(self::NB_FIELDS_LINK, count($data)); + $this->assertEquals($id, $data['id']); + $this->assertEquals('WDWyig', $data['shorturl']); + $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); + $this->assertEquals('?WDWyig', $data['title']); + $this->assertEquals('', $data['description']); + $this->assertEquals([], $data['tags']); + $this->assertEquals(false, $data['private']); + $this->assertEquals( + \DateTime::createFromFormat('Ymd_His', '20150310_114651'), + \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) + ); + $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])); + } + + /** + * Test link update with new values + */ + public function testPutLinkWithValues() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'PUT', + 'CONTENT_TYPE' => 'application/json' + ]); + $id = 41; + $update = [ + 'url' => 'http://somewhere.else', + 'title' => 'Le Cid', + 'description' => 'Percé jusques au fond du cœur [...]', + 'tags' => ['corneille', 'rodrigue'], + 'private' => true, + ]; + $request = Request::createFromEnvironment($env); + $request = $request->withParsedBody($update); + + $response = $this->controller->putLink($request, new Response(), ['id' => $id]); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(self::NB_FIELDS_LINK, count($data)); + $this->assertEquals($id, $data['id']); + $this->assertEquals('WDWyig', $data['shorturl']); + $this->assertEquals('http://somewhere.else', $data['url']); + $this->assertEquals('Le Cid', $data['title']); + $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']); + $this->assertEquals(['corneille', 'rodrigue'], $data['tags']); + $this->assertEquals(true, $data['private']); + $this->assertEquals( + \DateTime::createFromFormat('Ymd_His', '20150310_114651'), + \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) + ); + $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])); + } + + /** + * Test link update with an existing URL: 409 Conflict with the existing link as body + */ + public function testPutLinkDuplicate() + { + $link = [ + 'url' => 'mediagoblin.org/', + 'title' => 'new entry', + 'description' => 'shaare description', + 'tags' => ['one', 'two'], + 'private' => true, + ]; + $env = Environment::mock([ + 'REQUEST_METHOD' => 'PUT', + 'CONTENT_TYPE' => 'application/json' + ]); + + $request = Request::createFromEnvironment($env); + $request = $request->withParsedBody($link); + $response = $this->controller->putLink($request, new Response(), ['id' => 41]); + + $this->assertEquals(409, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + $this->assertEquals(self::NB_FIELDS_LINK, count($data)); + $this->assertEquals(7, $data['id']); + $this->assertEquals('IuWvgA', $data['shorturl']); + $this->assertEquals('http://mediagoblin.org/', $data['url']); + $this->assertEquals('MediaGoblin', $data['title']); + $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']); + $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); + $this->assertEquals(false, $data['private']); + $this->assertEquals( + \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), + \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) + ); + $this->assertEquals( + \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), + \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) + ); + } + + /** + * Test link update on non existent link => ApiLinkNotFoundException. + * + * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException + * @expectedExceptionMessage Link not found + */ + public function testGetLink404() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'PUT', + ]); + $request = Request::createFromEnvironment($env); + + $this->controller->putLink($request, new Response(), ['id' => -1]); + } +} -- cgit v1.2.3