summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG6
-rw-r--r--external/folderapi.php41
-rw-r--r--external/newsapiresult.php5
-rw-r--r--tests/unit/external/FolderAPITest.php170
-rw-r--r--tests/unit/external/NewsAPIResultTest.php13
5 files changed, 228 insertions, 7 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 3b012bd5f..098272f31 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,9 @@
+ownCloud-news (1.0-beta.2)
+
+* Always open links in new tabs
+* Better exception handling for controllers
+* Implemented API
+
ownCloud-news (1.0-beta.1)
* Fix a bug that would cause PHP 5.3 to fail while parsing utf-8
diff --git a/external/folderapi.php b/external/folderapi.php
index a7ea2f257..d9515b3ce 100644
--- a/external/folderapi.php
+++ b/external/folderapi.php
@@ -31,6 +31,7 @@ use \OCA\AppFramework\Http\Request;
use \OCA\News\BusinessLayer\FolderBusinessLayer;
use \OCA\News\BusinessLayer\BusinessLayerException;
+use \OCA\News\BusinessLayer\BusinessLayerExistsException;
class FolderAPI extends Controller {
@@ -59,18 +60,52 @@ class FolderAPI extends Controller {
}
- public function create() {
+ public function create() {
+ $userId = $this->api->getUserId();
+ $folderName = $this->params('name');
+ $result = array(
+ 'folders' => array()
+ );
+ try {
+ $folder = $this->folderBusinessLayer->create($folderName, $userId);
+ array_push($result['folders'], $folder->toAPI());
+
+ return new NewsAPIResult($result);
+ } catch(BusinessLayerExistsException $ex) {
+ return new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR,
+ $ex->getMessage());
+ }
}
public function delete() {
-
+ $userId = $this->api->getUserId();
+ $folderId = $this->params('folderId');
+
+ try {
+ $this->folderBusinessLayer->delete($folderId, $userId);
+ return new NewsAPIResult();
+ } catch(BusinessLayerException $ex) {
+ return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND,
+ $ex->getMessage());
+ }
}
public function update() {
-
+ $userId = $this->api->getUserId();
+ $folderId = $this->params('folderId');
+ $folderName = $this->params('name');
+
+ try {
+ $this->folderBusinessLayer->rename($folderId, $folderName, $userId);
+ return new NewsAPIResult();
+ } catch(BusinessLayerException $ex) {
+ return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND,
+ $ex->getMessage());
+ }
}
+
}
diff --git a/external/newsapiresult.php b/external/newsapiresult.php
index 7147d32bd..e4034bc20 100644
--- a/external/newsapiresult.php
+++ b/external/newsapiresult.php
@@ -31,8 +31,9 @@ class NewsAPIResult extends APIResult {
const EXISTS_ERROR = 409;
- public function __construct($data, $statusCode=NewsAPIResult::OK) {
- parent::__construct($data, $statusCode);
+ public function __construct($data=null, $statusCode=NewsAPIResult::OK,
+ $message=null) {
+ parent::__construct($data, $statusCode, $message);
}
diff --git a/tests/unit/external/FolderAPITest.php b/tests/unit/external/FolderAPITest.php
index 1ab8bbc1c..ef78d3dd8 100644
--- a/tests/unit/external/FolderAPITest.php
+++ b/tests/unit/external/FolderAPITest.php
@@ -25,7 +25,10 @@
namespace OCA\News\External;
+use \OCA\AppFramework\Http\Request;
+
use \OCA\News\BusinessLayer\BusinessLayerException;
+use \OCA\News\BusinessLayer\BusinessLayerExistsException;
use \OCA\News\Db\Folder;
use \OCA\News\Db\Feed;
@@ -41,6 +44,7 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase {
private $api;
private $user;
private $request;
+ private $msg;
protected function setUp() {
$this->api = $this->getMockBuilder(
@@ -61,6 +65,7 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase {
$this->folderBusinessLayer
);
$this->user = 'tom';
+ $this->msg = 'test';
}
@@ -85,5 +90,170 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase {
}
+ public function testCreate() {
+ $folderName = 'test';
+ $folder = new Folder();
+ $folder->setName($folderName);
+ $folders = array(
+ $folder
+ );
+ $this->folderAPI = new FolderAPI(
+ $this->api,
+ new Request(array('params' => array(
+ 'name' => $folderName
+ ))),
+ $this->folderBusinessLayer
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->folderBusinessLayer->expects($this->once())
+ ->method('create')
+ ->with($this->equalTo($folderName), $this->equalTo($this->user))
+ ->will($this->returnValue($folder));
+
+ $response = $this->folderAPI->create();
+
+ $this->assertEquals(array(
+ 'folders' => array($folders[0]->toAPI())
+ ), $response->getData());
+ }
+
+
+ public function testCreateAlreadyExists() {
+ $msg = 'exists';
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->folderBusinessLayer->expects($this->once())
+ ->method('create')
+ ->will($this->throwException(new BusinessLayerExistsException($msg)));
+
+ $response = $this->folderAPI->create();
+
+ $this->assertNull($response->getData());
+ $this->assertEquals(NewsAPIResult::EXISTS_ERROR, $response->getStatusCode());
+ $this->assertEquals($msg, $response->getMessage());
+ }
+
+
+ public function testDelete() {
+ $folderId = 23;
+
+ $this->folderAPI = new FolderAPI(
+ $this->api,
+ new Request(array('urlParams' => array(
+ 'folderId' => $folderId
+ ))),
+ $this->folderBusinessLayer
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->folderBusinessLayer->expects($this->once())
+ ->method('delete')
+ ->with($this->equalTo($folderId), $this->equalTo($this->user));
+
+ $response = $this->folderAPI->delete();
+
+ $this->assertNull($response->getData());
+ }
+
+
+ public function testDeleteDoesNotExist() {
+ $folderId = 23;
+
+ $this->folderAPI = new FolderAPI(
+ $this->api,
+ new Request(array('urlParams' => array(
+ 'folderId' => $folderId
+ ))),
+ $this->folderBusinessLayer
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->folderBusinessLayer->expects($this->once())
+ ->method('delete')
+ ->will($this->throwException(new BusinessLayerException($this->msg)));
+
+ $response = $this->folderAPI->delete();
+
+ $this->assertNull($response->getData());
+ $this->assertEquals($this->msg, $response->getMessage());
+ $this->assertEquals(NewsAPIResult::NOT_FOUND, $response->getStatusCode());
+ }
+
+
+ public function testUpdate() {
+ $folderId = 23;
+ $folderName = 'test';
+
+ $this->folderAPI = new FolderAPI(
+ $this->api,
+ new Request(
+ array(
+ 'urlParams' => array(
+ 'folderId' => $folderId
+ ),
+
+ 'params' => array(
+ 'name' => $folderName
+ )
+ )
+ ),
+ $this->folderBusinessLayer
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->folderBusinessLayer->expects($this->once())
+ ->method('rename')
+ ->with($this->equalTo($folderId),
+ $this->equalTo($folderName),
+ $this->equalTo($this->user));
+
+ $response = $this->folderAPI->update();
+
+ $this->assertNull($response->getData());
+ $this->assertNull($response->getMessage());
+ $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode());
+ }
+
+
+ public function testUpdateDoesNotExist() {
+ $folderId = 23;
+ $folderName = 'test';
+
+ $this->folderAPI = new FolderAPI(
+ $this->api,
+ new Request(
+ array('urlParams' => array(
+ 'folderId' => $folderId
+ ),
+ array('params' => array(
+ 'name' => $folderName
+ ))
+ )),
+ $this->folderBusinessLayer
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->folderBusinessLayer->expects($this->once())
+ ->method('rename')
+ ->will($this->throwException(new BusinessLayerException($this->msg)));
+
+ $response = $this->folderAPI->update();
+
+ $this->assertNull($response->getData());
+ $this->assertEquals($this->msg, $response->getMessage());
+ $this->assertEquals(NewsAPIResult::NOT_FOUND, $response->getStatusCode());
+ }
} \ No newline at end of file
diff --git a/tests/unit/external/NewsAPIResultTest.php b/tests/unit/external/NewsAPIResultTest.php
index 426198183..6d0e41669 100644
--- a/tests/unit/external/NewsAPIResultTest.php
+++ b/tests/unit/external/NewsAPIResultTest.php
@@ -32,9 +32,18 @@ class NewsAPIResultTest extends \PHPUnit_Framework_TestCase {
public function testExistsError(){
- $result = new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR);
+ $result = new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR, 'hi');
$this->assertEquals(409, $result->getStatusCode());
+ $this->assertEquals('hi', $result->getMessage());
}
-} \ No newline at end of file
+ public function testNoInput(){
+ $result = new NewsAPIResult();
+ $this->assertNull($result->getData());
+ $this->assertEquals(100, $result->getStatusCode());
+ $this->assertNull($result->getMessage());
+ }
+
+
+}