From 7675b0184f492e0da2565cd2e770caf8ba6e2752 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 27 Sep 2013 20:06:58 +0200 Subject: external to api folder --- api/feedapi.php | 226 +++++++++++ api/folderapi.php | 178 +++++++++ api/itemapi.php | 297 +++++++++++++++ api/newsapi.php | 111 ++++++ dependencyinjection/dicontainer.php | 8 +- external/feedapi.php | 226 ----------- external/folderapi.php | 178 --------- external/itemapi.php | 297 --------------- external/newsapi.php | 111 ------ tests/unit/api/FeedAPITest.php | 510 +++++++++++++++++++++++++ tests/unit/api/FolderAPITest.php | 441 ++++++++++++++++++++++ tests/unit/api/ItemAPITest.php | 690 ++++++++++++++++++++++++++++++++++ tests/unit/api/NewsAPITest.php | 142 +++++++ tests/unit/external/FeedAPITest.php | 510 ------------------------- tests/unit/external/FolderAPITest.php | 441 ---------------------- tests/unit/external/ItemAPITest.php | 690 ---------------------------------- tests/unit/external/NewsAPITest.php | 142 ------- 17 files changed, 2599 insertions(+), 2599 deletions(-) create mode 100644 api/feedapi.php create mode 100644 api/folderapi.php create mode 100644 api/itemapi.php create mode 100644 api/newsapi.php delete mode 100644 external/feedapi.php delete mode 100644 external/folderapi.php delete mode 100644 external/itemapi.php delete mode 100644 external/newsapi.php create mode 100644 tests/unit/api/FeedAPITest.php create mode 100644 tests/unit/api/FolderAPITest.php create mode 100644 tests/unit/api/ItemAPITest.php create mode 100644 tests/unit/api/NewsAPITest.php delete mode 100644 tests/unit/external/FeedAPITest.php delete mode 100644 tests/unit/external/FolderAPITest.php delete mode 100644 tests/unit/external/ItemAPITest.php delete mode 100644 tests/unit/external/NewsAPITest.php diff --git a/api/feedapi.php b/api/feedapi.php new file mode 100644 index 000000000..4ad9b3307 --- /dev/null +++ b/api/feedapi.php @@ -0,0 +1,226 @@ +. +* +*/ + +namespace OCA\News\API; + +use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Controller\Controller; +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; + +use \OCA\News\BusinessLayer\FeedBusinessLayer; +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; + + +class FeedAPI extends Controller { + + private $itemBusinessLayer; + private $feedBusinessLayer; + private $folderBusinessLayer; + + public function __construct(API $api, + Request $request, + FolderBusinessLayer $folderBusinessLayer, + FeedBusinessLayer $feedBusinessLayer, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api, $request); + $this->folderBusinessLayer = $folderBusinessLayer; + $this->feedBusinessLayer = $feedBusinessLayer; + $this->itemBusinessLayer = $itemBusinessLayer; + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function getAll() { + $userId = $this->api->getUserId(); + + $result = array( + 'feeds' => array(), + 'starredCount' => $this->itemBusinessLayer->starredCount($userId) + ); + + foreach ($this->feedBusinessLayer->findAll($userId) as $feed) { + array_push($result['feeds'], $feed->toAPI()); + } + + // check case when there are no items + try { + $result['newestItemId'] = + $this->itemBusinessLayer->getNewestItemId($userId); + } catch(BusinessLayerException $ex) {} + + return new JSONResponse($result); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function create() { + $userId = $this->api->getUserId(); + $feedUrl = $this->params('url'); + $folderId = (int) $this->params('folderId', 0); + + try { + $this->feedBusinessLayer->purgeDeleted($userId, false); + + $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId); + $result = array( + 'feeds' => array($feed->toAPI()) + ); + + try { + $result['newestItemId'] = + $this->itemBusinessLayer->getNewestItemId($userId); + } catch(BusinessLayerException $ex) {} + + return new JSONResponse($result); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function delete() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + + try { + $this->feedBusinessLayer->delete($feedId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function read() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $userId); + return new JSONResponse(); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function move() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $folderId = (int) $this->params('folderId'); + + try { + $this->feedBusinessLayer->move($feedId, $folderId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @CSRFExemption + * @Ajax + * @API + */ + public function getAllFromAllUsers() { + $feeds = $this->feedBusinessLayer->findAllFromAllUsers(); + $result = array('feeds' => array()); + + foreach ($feeds as $feed) { + array_push($result['feeds'], array( + 'id' => $feed->getId(), + 'userId' => $feed->getUserId() + )); + } + + return new JSONResponse($result); + } + + + /** + * @CSRFExemption + * @Ajax + * @API + */ + public function update() { + $userId = $this->params('userId'); + $feedId = (int) $this->params('feedId'); + + try { + $this->feedBusinessLayer->update($feedId, $userId); + // ignore update failure (feed could not be reachable etc, we dont care) + } catch(\Exception $ex) { + $this->api->log('Could not update feed ' . $ex->getMessage(), + 'debug'); + } + return new JSONResponse(); + + } + + +} diff --git a/api/folderapi.php b/api/folderapi.php new file mode 100644 index 000000000..12e400a76 --- /dev/null +++ b/api/folderapi.php @@ -0,0 +1,178 @@ +. +* +*/ + +namespace OCA\News\API; + +use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Controller\Controller; +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; + +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\BusinessLayer\BusinessLayerValidationException; + + +class FolderAPI extends Controller { + + private $folderBusinessLayer; + private $itemBusinessLayer; + + public function __construct(API $api, + Request $request, + FolderBusinessLayer $folderBusinessLayer, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api, $request); + $this->folderBusinessLayer = $folderBusinessLayer; + $this->itemBusinessLayer = $itemBusinessLayer; + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function getAll() { + $userId = $this->api->getUserId(); + $result = array( + 'folders' => array() + ); + + foreach ($this->folderBusinessLayer->findAll($userId) as $folder) { + array_push($result['folders'], $folder->toAPI()); + } + + return new JSONResponse($result); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function create() { + $userId = $this->api->getUserId(); + $folderName = $this->params('name'); + $result = array( + 'folders' => array() + ); + + try { + $this->folderBusinessLayer->purgeDeleted($userId, false); + $folder = $this->folderBusinessLayer->create($folderName, $userId); + array_push($result['folders'], $folder->toAPI()); + + return new JSONResponse($result); + + } catch(BusinessLayerValidationException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_UNPROCESSABLE_ENTITY); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + } + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @API + * @Ajax + */ + public function delete() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + + try { + $this->folderBusinessLayer->delete($folderId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function update() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + $folderName = $this->params('name'); + + try { + $this->folderBusinessLayer->rename($folderId, $folderName, $userId); + return new JSONResponse(); + + } catch(BusinessLayerValidationException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_UNPROCESSABLE_ENTITY); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function read() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $userId); + return new JSONResponse(); + } + + +} diff --git a/api/itemapi.php b/api/itemapi.php new file mode 100644 index 000000000..335132d60 --- /dev/null +++ b/api/itemapi.php @@ -0,0 +1,297 @@ +. +* +*/ + +namespace OCA\News\API; + +use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Controller\Controller; +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; + +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; + + +class ItemAPI extends Controller { + + private $itemBusinessLayer; + + public function __construct(API $api, + Request $request, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api, $request); + $this->itemBusinessLayer = $itemBusinessLayer; + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function getAll() { + $result = array( + 'items' => array() + ); + + $userId = $this->api->getUserId(); + $batchSize = (int) $this->params('batchSize', 20); + $offset = (int) $this->params('offset', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + $showAll = $this->params('getRead'); + + if($showAll === 'true' || $showAll === true) { + $showAll = true; + } else { + $showAll = false; + } + + $items = $this->itemBusinessLayer->findAll( + $id, + $type, + $batchSize, + $offset, + $showAll, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new JSONResponse($result); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function getUpdated() { + $result = array( + 'items' => array() + ); + + $userId = $this->api->getUserId(); + $lastModified = (int) $this->params('lastModified', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + + $items = $this->itemBusinessLayer->findAllNew( + $id, + $type, + $lastModified, + true, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new JSONResponse($result); + } + + + private function setRead($isRead) { + $userId = $this->api->getUserId(); + $itemId = (int) $this->params('itemId'); + try { + $this->itemBusinessLayer->read($itemId, $isRead, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex){ + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + private function setStarred($isStarred) { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $guidHash = $this->params('guidHash'); + try { + $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex){ + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function read() { + return $this->setRead(true); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function unread() { + return $this->setRead(false); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function star() { + return $this->setStarred(true); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function unstar() { + return $this->setStarred(false); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function readAll() { + $userId = $this->api->getUserId(); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readAll($newestItemId, $userId); + return new JSONResponse(); + } + + + private function setMultipleRead($isRead) { + $userId = $this->api->getUserId(); + $items = $this->params('items'); + + foreach($items as $id) { + try { + $this->itemBusinessLayer->read($id, $isRead, $userId); + } catch(BusinessLayerException $ex) { + continue; + } + } + + return new JSONResponse(); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function readMultiple() { + return $this->setMultipleRead(true); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function unreadMultiple() { + return $this->setMultipleRead(false); + } + + + private function setMultipleStarred($isStarred) { + $userId = $this->api->getUserId(); + $items = $this->params('items'); + + foreach($items as $item) { + try { + $this->itemBusinessLayer->star($item['feedId'], + $item['guidHash'], $isStarred, $userId); + } catch(BusinessLayerException $ex) { + continue; + } + } + + return new JSONResponse(); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function starMultiple() { + return $this->setMultipleStarred(true); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function unstarMultiple() { + return $this->setMultipleStarred(false); + } + + +} diff --git a/api/newsapi.php b/api/newsapi.php new file mode 100644 index 000000000..aaed92926 --- /dev/null +++ b/api/newsapi.php @@ -0,0 +1,111 @@ +. +* +*/ + +namespace OCA\News\API; + +use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Controller\Controller; +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Response; +use \OCA\AppFramework\Http\Http; + +use \OCA\News\Utility\Updater; + + +class NewsAPI extends Controller { + + private $updater; + + public function __construct(API $api, Request $request, Updater $updater){ + parent::__construct($api, $request); + $this->updater = $updater; + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function version() { + $version = $this->api->getAppValue('installed_version'); + $response = new JSONResponse(array('version' => $version)); + return $response; + } + + + /** + * @CSRFExemption + * @Ajax + * @API + */ + public function beforeUpdate() { + $this->updater->beforeUpdate(); + return new JSONResponse(); + } + + + /** + * @CSRFExemption + * @Ajax + * @API + */ + public function afterUpdate() { + $this->updater->afterUpdate(); + return new JSONResponse(); + } + + + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @IsLoggedInExemption + * @Ajax + */ + public function cors() { + // needed for webapps access due to cross origin request policy + if(isset($this->request->server['HTTP_ORIGIN'])) { + $origin = $this->request->server['HTTP_ORIGIN']; + } else { + $origin = '*'; + } + + $response = new Response(); + $response->addHeader('Access-Control-Allow-Origin', $origin); + $response->addHeader('Access-Control-Allow-Methods', + 'PUT, POST, GET, DELETE'); + $response->addHeader('Access-Control-Allow-Credentials', 'true'); + $response->addHeader('Access-Control-Max-Age', '1728000'); + $response->addHeader('Access-Control-Allow-Headers', + 'Authorization, Content-Type'); + return $response; + } + + +} diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php index ce095ccbf..480635d8e 100644 --- a/dependencyinjection/dicontainer.php +++ b/dependencyinjection/dicontainer.php @@ -47,10 +47,10 @@ use \OCA\News\Db\ItemMapper; use \OCA\News\Db\StatusFlag; use \OCA\News\Db\MapperFactory; -use \OCA\News\External\NewsAPI; -use \OCA\News\External\FolderAPI; -use \OCA\News\External\FeedAPI; -use \OCA\News\External\ItemAPI; +use \OCA\News\API\NewsAPI; +use \OCA\News\API\FolderAPI; +use \OCA\News\API\FeedAPI; +use \OCA\News\API\ItemAPI; use \OCA\News\Utility\Config; use \OCA\News\Utility\OPMLExporter; diff --git a/external/feedapi.php b/external/feedapi.php deleted file mode 100644 index be228d490..000000000 --- a/external/feedapi.php +++ /dev/null @@ -1,226 +0,0 @@ -. -* -*/ - -namespace OCA\News\External; - -use \OCA\AppFramework\Core\API; -use \OCA\AppFramework\Controller\Controller; -use \OCA\AppFramework\Http\Request; -use \OCA\AppFramework\Http\JSONResponse; -use \OCA\AppFramework\Http\Http; - -use \OCA\News\BusinessLayer\FeedBusinessLayer; -use \OCA\News\BusinessLayer\FolderBusinessLayer; -use \OCA\News\BusinessLayer\ItemBusinessLayer; -use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\BusinessLayer\BusinessLayerConflictException; - - -class FeedAPI extends Controller { - - private $itemBusinessLayer; - private $feedBusinessLayer; - private $folderBusinessLayer; - - public function __construct(API $api, - Request $request, - FolderBusinessLayer $folderBusinessLayer, - FeedBusinessLayer $feedBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api, $request); - $this->folderBusinessLayer = $folderBusinessLayer; - $this->feedBusinessLayer = $feedBusinessLayer; - $this->itemBusinessLayer = $itemBusinessLayer; - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function getAll() { - $userId = $this->api->getUserId(); - - $result = array( - 'feeds' => array(), - 'starredCount' => $this->itemBusinessLayer->starredCount($userId) - ); - - foreach ($this->feedBusinessLayer->findAll($userId) as $feed) { - array_push($result['feeds'], $feed->toAPI()); - } - - // check case when there are no items - try { - $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); - } catch(BusinessLayerException $ex) {} - - return new JSONResponse($result); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function create() { - $userId = $this->api->getUserId(); - $feedUrl = $this->params('url'); - $folderId = (int) $this->params('folderId', 0); - - try { - $this->feedBusinessLayer->purgeDeleted($userId, false); - - $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId); - $result = array( - 'feeds' => array($feed->toAPI()) - ); - - try { - $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($userId); - } catch(BusinessLayerException $ex) {} - - return new JSONResponse($result); - - } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_CONFLICT); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function delete() { - $userId = $this->api->getUserId(); - $feedId = (int) $this->params('feedId'); - - try { - $this->feedBusinessLayer->delete($feedId, $userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function read() { - $userId = $this->api->getUserId(); - $feedId = (int) $this->params('feedId'); - $newestItemId = (int) $this->params('newestItemId'); - - $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $userId); - return new JSONResponse(); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function move() { - $userId = $this->api->getUserId(); - $feedId = (int) $this->params('feedId'); - $folderId = (int) $this->params('folderId'); - - try { - $this->feedBusinessLayer->move($feedId, $folderId, $userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - /** - * @CSRFExemption - * @Ajax - * @API - */ - public function getAllFromAllUsers() { - $feeds = $this->feedBusinessLayer->findAllFromAllUsers(); - $result = array('feeds' => array()); - - foreach ($feeds as $feed) { - array_push($result['feeds'], array( - 'id' => $feed->getId(), - 'userId' => $feed->getUserId() - )); - } - - return new JSONResponse($result); - } - - - /** - * @CSRFExemption - * @Ajax - * @API - */ - public function update() { - $userId = $this->params('userId'); - $feedId = (int) $this->params('feedId'); - - try { - $this->feedBusinessLayer->update($feedId, $userId); - // ignore update failure (feed could not be reachable etc, we dont care) - } catch(\Exception $ex) { - $this->api->log('Could not update feed ' . $ex->getMessage(), - 'debug'); - } - return new JSONResponse(); - - } - - -} diff --git a/external/folderapi.php b/external/folderapi.php deleted file mode 100644 index 5d2555e10..000000000 --- a/external/folderapi.php +++ /dev/null @@ -1,178 +0,0 @@ -. -* -*/ - -namespace OCA\News\External; - -use \OCA\AppFramework\Core\API; -use \OCA\AppFramework\Controller\Controller; -use \OCA\AppFramework\Http\Request; -use \OCA\AppFramework\Http\JSONResponse; -use \OCA\AppFramework\Http\Http; - -use \OCA\News\BusinessLayer\FolderBusinessLayer; -use \OCA\News\BusinessLayer\ItemBusinessLayer; -use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\BusinessLayer\BusinessLayerConflictException; -use \OCA\News\BusinessLayer\BusinessLayerValidationException; - - -class FolderAPI extends Controller { - - private $folderBusinessLayer; - private $itemBusinessLayer; - - public function __construct(API $api, - Request $request, - FolderBusinessLayer $folderBusinessLayer, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api, $request); - $this->folderBusinessLayer = $folderBusinessLayer; - $this->itemBusinessLayer = $itemBusinessLayer; - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function getAll() { - $userId = $this->api->getUserId(); - $result = array( - 'folders' => array() - ); - - foreach ($this->folderBusinessLayer->findAll($userId) as $folder) { - array_push($result['folders'], $folder->toAPI()); - } - - return new JSONResponse($result); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function create() { - $userId = $this->api->getUserId(); - $folderName = $this->params('name'); - $result = array( - 'folders' => array() - ); - - try { - $this->folderBusinessLayer->purgeDeleted($userId, false); - $folder = $this->folderBusinessLayer->create($folderName, $userId); - array_push($result['folders'], $folder->toAPI()); - - return new JSONResponse($result); - - } catch(BusinessLayerValidationException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_UNPROCESSABLE_ENTITY); - - } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_CONFLICT); - } - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @API - * @Ajax - */ - public function delete() { - $userId = $this->api->getUserId(); - $folderId = (int) $this->params('folderId'); - - try { - $this->folderBusinessLayer->delete($folderId, $userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function update() { - $userId = $this->api->getUserId(); - $folderId = (int) $this->params('folderId'); - $folderName = $this->params('name'); - - try { - $this->folderBusinessLayer->rename($folderId, $folderName, $userId); - return new JSONResponse(); - - } catch(BusinessLayerValidationException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_UNPROCESSABLE_ENTITY); - - } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_CONFLICT); - - } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function read() { - $userId = $this->api->getUserId(); - $folderId = (int) $this->params('folderId'); - $newestItemId = (int) $this->params('newestItemId'); - - $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $userId); - return new JSONResponse(); - } - - -} diff --git a/external/itemapi.php b/external/itemapi.php deleted file mode 100644 index 8fd4b3767..000000000 --- a/external/itemapi.php +++ /dev/null @@ -1,297 +0,0 @@ -. -* -*/ - -namespace OCA\News\External; - -use \OCA\AppFramework\Core\API; -use \OCA\AppFramework\Controller\Controller; -use \OCA\AppFramework\Http\Request; -use \OCA\AppFramework\Http\JSONResponse; -use \OCA\AppFramework\Http\Http; - -use \OCA\News\BusinessLayer\ItemBusinessLayer; -use \OCA\News\BusinessLayer\BusinessLayerException; - - -class ItemAPI extends Controller { - - private $itemBusinessLayer; - - public function __construct(API $api, - Request $request, - ItemBusinessLayer $itemBusinessLayer){ - parent::__construct($api, $request); - $this->itemBusinessLayer = $itemBusinessLayer; - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function getAll() { - $result = array( - 'items' => array() - ); - - $userId = $this->api->getUserId(); - $batchSize = (int) $this->params('batchSize', 20); - $offset = (int) $this->params('offset', 0); - $type = (int) $this->params('type'); - $id = (int) $this->params('id'); - $showAll = $this->params('getRead'); - - if($showAll === 'true' || $showAll === true) { - $showAll = true; - } else { - $showAll = false; - } - - $items = $this->itemBusinessLayer->findAll( - $id, - $type, - $batchSize, - $offset, - $showAll, - $userId - ); - - foreach ($items as $item) { - array_push($result['items'], $item->toAPI()); - } - - return new JSONResponse($result); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function getUpdated() { - $result = array( - 'items' => array() - ); - - $userId = $this->api->getUserId(); - $lastModified = (int) $this->params('lastModified', 0); - $type = (int) $this->params('type'); - $id = (int) $this->params('id'); - - $items = $this->itemBusinessLayer->findAllNew( - $id, - $type, - $lastModified, - true, - $userId - ); - - foreach ($items as $item) { - array_push($result['items'], $item->toAPI()); - } - - return new JSONResponse($result); - } - - - private function setRead($isRead) { - $userId = $this->api->getUserId(); - $itemId = (int) $this->params('itemId'); - try { - $this->itemBusinessLayer->read($itemId, $isRead, $userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex){ - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - private function setStarred($isStarred) { - $userId = $this->api->getUserId(); - $feedId = (int) $this->params('feedId'); - $guidHash = $this->params('guidHash'); - try { - $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex){ - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); - } - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function read() { - return $this->setRead(true); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function unread() { - return $this->setRead(false); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function star() { - return $this->setStarred(true); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function unstar() { - return $this->setStarred(false); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function readAll() { - $userId = $this->api->getUserId(); - $newestItemId = (int) $this->params('newestItemId'); - - $this->itemBusinessLayer->readAll($newestItemId, $userId); - return new JSONResponse(); - } - - - private function setMultipleRead($isRead) { - $userId = $this->api->getUserId(); - $items = $this->params('items'); - - foreach($items as $id) { - try { - $this->itemBusinessLayer->read($id, $isRead, $userId); - } catch(BusinessLayerException $ex) { - continue; - } - } - - return new JSONResponse(); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function readMultiple() { - return $this->setMultipleRead(true); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function unreadMultiple() { - return $this->setMultipleRead(false); - } - - - private function setMultipleStarred($isStarred) { - $userId = $this->api->getUserId(); - $items = $this->params('items'); - - foreach($items as $item) { - try { - $this->itemBusinessLayer->star($item['feedId'], - $item['guidHash'], $isStarred, $userId); - } catch(BusinessLayerException $ex) { - continue; - } - } - - return new JSONResponse(); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function starMultiple() { - return $this->setMultipleStarred(true); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function unstarMultiple() { - return $this->setMultipleStarred(false); - } - - -} diff --git a/external/newsapi.php b/external/newsapi.php deleted file mode 100644 index 51a2d9cf2..000000000 --- a/external/newsapi.php +++ /dev/null @@ -1,111 +0,0 @@ -. -* -*/ - -namespace OCA\News\External; - -use \OCA\AppFramework\Core\API; -use \OCA\AppFramework\Controller\Controller; -use \OCA\AppFramework\Http\Request; -use \OCA\AppFramework\Http\JSONResponse; -use \OCA\AppFramework\Http\Response; -use \OCA\AppFramework\Http\Http; - -use \OCA\News\Utility\Updater; - - -class NewsAPI extends Controller { - - private $updater; - - public function __construct(API $api, Request $request, Updater $updater){ - parent::__construct($api, $request); - $this->updater = $updater; - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @Ajax - * @API - */ - public function version() { - $version = $this->api->getAppValue('installed_version'); - $response = new JSONResponse(array('version' => $version)); - return $response; - } - - - /** - * @CSRFExemption - * @Ajax - * @API - */ - public function beforeUpdate() { - $this->updater->beforeUpdate(); - return new JSONResponse(); - } - - - /** - * @CSRFExemption - * @Ajax - * @API - */ - public function afterUpdate() { - $this->updater->afterUpdate(); - return new JSONResponse(); - } - - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - * @IsLoggedInExemption - * @Ajax - */ - public function cors() { - // needed for webapps access due to cross origin request policy - if(isset($this->request->server['HTTP_ORIGIN'])) { - $origin = $this->request->server['HTTP_ORIGIN']; - } else { - $origin = '*'; - } - - $response = new Response(); - $response->addHeader('Access-Control-Allow-Origin', $origin); - $response->addHeader('Access-Control-Allow-Methods', - 'PUT, POST, GET, DELETE'); - $response->addHeader('Access-Control-Allow-Credentials', 'true'); - $response->addHeader('Access-Control-Max-Age', '1728000'); - $response->addHeader('Access-Control-Allow-Headers', - 'Authorization, Content-Type'); - return $response; - } - - -} diff --git a/tests/unit/api/FeedAPITest.php b/tests/unit/api/FeedAPITest.php new file mode 100644 index 000000000..e3b5b5587 --- /dev/null +++ b/tests/unit/api/FeedAPITest.php @@ -0,0 +1,510 @@ +. +* +*/ + +namespace OCA\News\API; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; +use \OCA\AppFramework\Utility\ControllerTestUtility; + +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FeedAPITest extends ControllerTestUtility { + + private $folderBusinessLayer; + private $feedBusinessLayer; + private $itemBusinessLayer; + private $feedAPI; + private $api; + private $user; + private $request; + private $msg; + + protected function setUp() { + $this->api = $this->getMockBuilder( + '\OCA\AppFramework\Core\API') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder( + '\OCA\AppFramework\Http\Request') + ->disableOriginalConstructor() + ->getMock(); + $this->folderBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\FolderBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->feedBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\FeedBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->itemBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\ItemBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->feedAPI = new FeedAPI( + $this->api, + $this->request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + $this->user = 'tom'; + $this->msg = 'hohoho'; + } + + + private function assertDefaultAnnotations($methodName){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', + 'Ajax', 'CSRFExemption', 'API'); + $this->assertAnnotations($this->feedAPI, $methodName, $annotations); + } + + + public function testGetAllAnnotations(){ + $this->assertDefaultAnnotations('getAll'); + } + + + public function testCreateAnnotations(){ + $this->assertDefaultAnnotations('create'); + } + + + public function testDeleteAnnotations(){ + $this->assertDefaultAnnotations('delete'); + } + + + public function testMoveAnnotations(){ + $this->assertDefaultAnnotations('move'); + } + + + public function testReadAnnotations(){ + $this->assertDefaultAnnotations('read'); + } + + + public function testGetAllFromUsersAnnotations(){ + $annotations = array('Ajax', 'CSRFExemption', 'API'); + $this->assertAnnotations($this->feedAPI, 'getAllFromAllUsers', $annotations); + } + + + public function testUpdateAnnotations(){ + $annotations = array('Ajax', 'CSRFExemption', 'API'); + $this->assertAnnotations($this->feedAPI, 'update', $annotations); + } + + + public function testGetAll() { + $feeds = array( + new Feed() + ); + $starredCount = 3; + $newestItemId = 2; + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->itemBusinessLayer->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($starredCount)); + $this->itemBusinessLayer->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($newestItemId)); + $this->feedBusinessLayer->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $response = $this->feedAPI->getAll(); + + $this->assertEquals(array( + 'feeds' => array($feeds[0]->toAPI()), + 'starredCount' => $starredCount, + 'newestItemId' => $newestItemId + ), $response->getData()); + } + + + public function testGetAllNoNewestItemId() { + $feeds = array( + new Feed() + ); + $starredCount = 3; + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->itemBusinessLayer->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($starredCount)); + $this->itemBusinessLayer->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->throwException(new BusinessLayerException(''))); + $this->feedBusinessLayer->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $response = $this->feedAPI->getAll(); + + $this->assertEquals(array( + 'feeds' => array($feeds[0]->toAPI()), + 'starredCount' => $starredCount, + ), $response->getData()); + } + + + public function testDelete() { + $request = new Request(array('urlParams' => array( + 'feedId' => 2 + ))); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo(2), + $this->equalTo($this->user)); + + $response = $this->feedAPI->delete(); + + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + + public function testDeleteDoesNotExist() { + $request = new Request(array('urlParams' => array( + 'feedId' => 2 + ))); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('delete') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->feedAPI->delete(); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testCreate() { + $feeds = array( + new Feed() + ); + $request = new Request(array('params' => array( + 'url' => 'ho', + 'folderId' => 3 + ))); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedBusinessLayer->expects($this->once()) + ->method('create') + ->with( + $this->equalTo('ho'), + $this->equalTo(3), + $this->equalTo($this->user)) + ->will($this->returnValue($feeds[0])); + $this->itemBusinessLayer->expects($this->once()) + ->method('getNewestItemId') + ->will($this->returnValue(3)); + + $response = $this->feedAPI->create(); + + $this->assertEquals(array( + 'feeds' => array($feeds[0]->toAPI()), + 'newestItemId' => 3 + ), $response->getData()); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + + public function testCreateNoItems() { + $feeds = array( + new Feed() + ); + $request = new Request(array('params' => array( + 'url' => 'ho', + 'folderId' => 3 + ))); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedBusinessLayer->expects($this->once()) + ->method('create') + ->with( + $this->equalTo('ho'), + $this->equalTo(3), + $this->equalTo($this->user)) + ->will($this->returnValue($feeds[0])); + $this->itemBusinessLayer->expects($this->once()) + ->method('getNewestItemId') + ->will($this->throwException(new BusinessLayerException(''))); + + $response = $this->feedAPI->create(); + + $this->assertEquals(array( + 'feeds' => array($feeds[0]->toAPI()) + ), $response->getData()); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + + + public function testCreateExists() { + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedBusinessLayer->expects($this->once()) + ->method('create') + ->will($this->throwException(new BusinessLayerConflictException($this->msg))); + + $response = $this->feedAPI->create(); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); + } + + + public function testCreateError() { + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('create') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->feedAPI->create(); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testRead() { + $request = new Request(array( + 'urlParams' => array( + 'feedId' => 3 + ), + 'params' => array( + 'newestItemId' => 30, + ) + )); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->itemBusinessLayer->expects($this->once()) + ->method('readFeed') + ->with( + $this->equalTo(3), + $this->equalTo(30), + $this->equalTo($this->user)); + + $response = $this->feedAPI->read(); + + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + + public function testMove() { + $request = new Request(array( + 'urlParams' => array( + 'feedId' => 3 + ), + 'params' => array( + 'folderId' => 30, + ) + )); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('move') + ->with( + $this->equalTo(3), + $this->equalTo(30), + $this->equalTo($this->user)); + + $response = $this->feedAPI->move(); + + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + + public function testMoveDoesNotExist() { + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('move') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->feedAPI->move(); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testGetAllFromAllUsers(){ + $feed = new Feed(); + $feed->setUrl(3); + $feed->setId(1); + $feed->setUserId('john'); + $feeds = array($feed); + $this->feedBusinessLayer->expects($this->once()) + ->method('findAllFromAllUsers') + ->will($this->returnValue($feeds)); + $response = $this->feedAPI->getAllFromAllUsers(); + $this->assertTrue($response instanceof JSONResponse); + $this->assertEquals('{"feeds":[{"id":1,"userId":"john"}]}', $response->render()); + } + + + public function testUpdate() { + $feedId = 3; + $userId = 'hi'; + $request = new Request(array('params' => array( + 'feedId' => $feedId, + 'userId' => $userId + ))); + $this->feedAPI = new FeedAPI( + $this->api, + $request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + $this->feedBusinessLayer->expects($this->once()) + ->method('update') + ->with($this->equalTo($feedId), $this->equalTo($userId)); + + $response = $this->feedAPI->update(); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testUpdateError() { + $this->feedBusinessLayer->expects($this->once()) + ->method('update') + ->will($this->throwException(new \Exception($this->msg))); + $this->api->expects($this->once()) + ->method('log') + ->with($this->equalTo('Could not update feed ' . $this->msg), + $this->equalTo('debug')); + + $response = $this->feedAPI->update(); + + $this->assertTrue($response instanceof JSONResponse); + + } + + +} diff --git a/tests/unit/api/FolderAPITest.php b/tests/unit/api/FolderAPITest.php new file mode 100644 index 000000000..8c33233b0 --- /dev/null +++ b/tests/unit/api/FolderAPITest.php @@ -0,0 +1,441 @@ +. +* +*/ + +namespace OCA\News\API; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Http\Http; + +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\BusinessLayer\BusinessLayerValidationException; + +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FolderAPITest extends ControllerTestUtility { + + private $folderBusinessLayer; + private $itemBusinessLayer; + private $folderAPI; + private $api; + private $user; + private $request; + private $msg; + + protected function setUp() { + $this->api = $this->getMockBuilder( + '\OCA\AppFramework\Core\API') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder( + '\OCA\AppFramework\Http\Request') + ->disableOriginalConstructor() + ->getMock(); + $this->folderBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\FolderBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->itemBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\ItemBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->folderAPI = new FolderAPI( + $this->api, + $this->request, + $this->folderBusinessLayer, + $this->itemBusinessLayer + ); + $this->user = 'tom'; + $this->msg = 'test'; + } + + + private function assertDefaultAnnotations($methodName){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', + 'Ajax', 'CSRFExemption', 'API'); + $this->assertAnnotations($this->folderAPI, $methodName, $annotations); + } + + + public function testGetAllAnnotations(){ + $this->assertDefaultAnnotations('getAll'); + } + + + public function testCreateAnnotations(){ + $this->assertDefaultAnnotations('create'); + } + + + public function testDeleteAnnotations(){ + $this->assertDefaultAnnotations('delete'); + } + + + public function testUpdateAnnotations(){ + $this->assertDefaultAnnotations('update'); + } + + + public function testReadAnnotations(){ + $this->assertDefaultAnnotations('read'); + } + + + public function testGetAll() { + $folders = array( + new Folder() + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($folders)); + + $response = $this->folderAPI->getAll(); + + $this->assertEquals(array( + 'folders' => array($folders[0]->toAPI()) + ), $response->getData()); + } + + + 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->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $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('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->folderBusinessLayer->expects($this->once()) + ->method('create') + ->will($this->throwException(new BusinessLayerConflictException($msg))); + + $response = $this->folderAPI->create(); + + $data = $response->getData(); + $this->assertEquals($msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); + } + + + public function testCreateInvalidFolderName() { + $msg = 'exists'; + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->folderBusinessLayer->expects($this->once()) + ->method('create') + ->will($this->throwException(new BusinessLayerValidationException($msg))); + + $response = $this->folderAPI->create(); + + $data = $response->getData(); + $this->assertEquals($msg, $data['message']); + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + + + public function testDelete() { + $folderId = 23; + + $this->folderAPI = new FolderAPI( + $this->api, + new Request(array('urlParams' => array( + 'folderId' => $folderId + ))), + $this->folderBusinessLayer, + $this->itemBusinessLayer + ); + + $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->assertEmpty($response->getData()); + } + + + public function testDeleteDoesNotExist() { + $folderId = 23; + + $this->folderAPI = new FolderAPI( + $this->api, + new Request(array('urlParams' => array( + 'folderId' => $folderId + ))), + $this->folderBusinessLayer, + $this->itemBusinessLayer + ); + + $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(); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testUpdate() { + $folderId = 23; + $folderName = 'test'; + + $this->folderAPI = new FolderAPI( + $this->api, + new Request( + array