From c8bdd9c3fb0ab872b868c151f052748235601653 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 21 Mar 2013 20:38:09 +0100 Subject: finished feedcontroller --- bl/feedbl.php | 28 ++--- bl/itembl.php | 29 +++-- controller/feedcontroller.php | 71 ++++++++++--- controller/itemcontroller.php | 8 ++ db/itemmapper.php | 10 ++ tests/bl/FeedBlTest.php | 53 ++++++++++ tests/bl/ItemBlTest.php | 140 +++++++++++++++++++++++++ tests/controller/FeedControllerTest.php | 180 +++++++++++++++++++++++++++++++- tests/controller/ItemControllerTest.php | 4 + 9 files changed, 484 insertions(+), 39 deletions(-) create mode 100644 tests/bl/ItemBlTest.php diff --git a/bl/feedbl.php b/bl/feedbl.php index d196586bb..be63862e4 100644 --- a/bl/feedbl.php +++ b/bl/feedbl.php @@ -35,34 +35,34 @@ class FeedBl extends Bl { parent::__construct($feedMapper); } - // README: only call this for the cronjob! - public function findAll(){ + // README: only call this for the cronjob because it does not + // check that the feeds belong to the right user + public function findAll(){ + return $this->mapper->findAll(); } - public function findAllFromUser(){ - + public function findAllFromUser($userId){ + return $this->mapper->findAllFromUser($userId); } - public function create(){ - + public function create($feedUrl, $parentId, $userId){ + // TODO: download new items of feed } - public function update(){ - + public function update($feedId, $userId){ + // TODO: update given feed } - public function move(){ - + public function move($feedId, $folderId, $userId){ + $feed = $this->find($feedId, $userId); + $feed->setFolderId($folderId); + $this->mapper->update($feed); } - public function read(){ - - } - } diff --git a/bl/itembl.php b/bl/itembl.php index 7dee2318f..f2b1ac1b3 100644 --- a/bl/itembl.php +++ b/bl/itembl.php @@ -37,22 +37,39 @@ class ItemBl extends Bl { public function findAll(){ - + // TODO all the crazy finding of items } - public function finStarred(){ + public function starredCount($userId){ + return $this->mapper->starredCount($userId); + } + + public function star($itemId, $isStarred, $userId){ + $item = $this->find($itemId, $userId); + if($isStarred){ + $item->setStarred(); + } else { + $item->setUnstarred(); + } + $this->mapper->update($item); } - public function star(){ - + public function read($itemId, $isRead, $userId){ + $item = $this->find($itemId, $userId); + if($isRead){ + $item->setRead(); + } else { + $item->setUnread(); + } + $this->mapper->update($item); } - public function read(){ - + public function readFeed($feedId, $userId){ + $this->mapper->readFeed($feedId, $userId); } } diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index 89a7d7f69..d91c52aa3 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -32,7 +32,7 @@ use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\AppFramework\Db\MultipleObjectsReturnedException; use \OCA\News\Bl\FeedBl; - +use \OCA\News\Bl\BLException; class FeedController extends Controller { @@ -50,7 +50,14 @@ class FeedController extends Controller { * @Ajax */ public function feeds(){ - + $userId = $this->api->getUserId(); + $result = $this->feedBl->findAllFromUser($userId); + + $params = array( + 'feeds' => $result + ); + + return $this->renderJSON($params); } @@ -60,7 +67,17 @@ class FeedController extends Controller { * @Ajax */ public function active(){ + $feedId = $this->api->getUserValue('lastViewedFeedId'); + $feedType = $this->api->getUserValue('lastViewedFeedType'); + $params = array( + 'activeFeed' => array( + 'id' => $feedId, + 'type' => $feedType + ) + ); + + return $this->renderJSON($params); } @@ -70,7 +87,21 @@ class FeedController extends Controller { * @Ajax */ public function create(){ - + $url = $this->params('url'); + $parentFolderId = $this->params('parentFolderId'); + $userId = $this->api->getUserId(); + + try { + $feed = $this->feedBl->create($url, $parentFolderId, $userId); + $params = array( + 'feeds' => array($feed) + ); + + return $this->renderJSON($params); + } catch(BLException $ex) { + + return $this->renderJSON(array(), $ex->getMessage()); + } } @@ -80,7 +111,12 @@ class FeedController extends Controller { * @Ajax */ public function delete(){ - + $feedId = $this->params('feedId'); + $userId = $this->api->getUserId(); + + $this->feedBl->delete($feedId, $userId); + + return $this->renderJSON(array()); } @@ -90,7 +126,16 @@ class FeedController extends Controller { * @Ajax */ public function update(){ - + $feedId = $this->params('feedId'); + $userId = $this->api->getUserId(); + + $feed = $this->feedBl->update($feedId, $userId); + + $params = array( + 'feeds' => array($feed) + ); + + return $this->renderJSON($params); } @@ -100,16 +145,14 @@ class FeedController extends Controller { * @Ajax */ public function move(){ - - } + $feedId = $this->params('feedId'); + $parentFolderId = $this->params('parentFolderId'); + $userId = $this->api->getUserId(); + $this->feedBl->move($feedId, $parentFolderId, $userId); - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax - */ - public function read(){ - + return $this->renderJSON(array()); } + + } \ No newline at end of file diff --git a/controller/itemcontroller.php b/controller/itemcontroller.php index f5f6d2a4b..d4cf44de9 100644 --- a/controller/itemcontroller.php +++ b/controller/itemcontroller.php @@ -102,4 +102,12 @@ class ItemController extends Controller { } + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @Ajax + */ + public function readFeed(){ + + } } \ No newline at end of file diff --git a/db/itemmapper.php b/db/itemmapper.php index 90c80d48c..04f073c36 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -138,6 +138,16 @@ class ItemMapper extends NewsMapper { return $item; } + + public function readFeed($feedId, $userId){ + // TODO + } + + + public function starredCount($userId){ + // TODO + } + } diff --git a/tests/bl/FeedBlTest.php b/tests/bl/FeedBlTest.php index 08f98beab..444018349 100644 --- a/tests/bl/FeedBlTest.php +++ b/tests/bl/FeedBlTest.php @@ -36,6 +36,8 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility { protected $api; protected $feedMapper; protected $feedBl; + protected $user; + protected $response; protected function setUp(){ $this->api = $this->getAPIMock(); @@ -43,11 +45,62 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility { ->disableOriginalConstructor() ->getMock(); $this->feedBl = new FeedBl($this->feedMapper); + $this->user = 'jack'; + $response = 'hi'; } public function testFindAll(){ + $this->feedMapper->expects($this->once()) + ->method('findAll') + ->will($this->returnValue($this->response)); + $result = $this->feedBl->findAll(); + $this->assertEquals($this->response, $result); } + + public function testFindAllFromUser(){ + $this->feedMapper->expects($this->once()) + ->method('findAllFromUser') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->feedBl->findAllFromUser($this->user); + $this->assertEquals($this->response, $result); + } + + + public function testCreate(){ + // TODO + } + + + public function testUpdate(){ + // TODO + } + + + public function testMove(){ + $feedId = 3; + $folderId = 4; + $feed = new Feed(); + $feed->setFolderId(16); + $feed->setId($feedId); + + $this->feedMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($feedId), $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + + $this->feedMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($feed)); + + $this->feedBl->move($feedId, $folderId, $this->user); + + $this->assertEquals($folderId, $feed->getFolderId()); + } + + } \ No newline at end of file diff --git a/tests/bl/ItemBlTest.php b/tests/bl/ItemBlTest.php new file mode 100644 index 000000000..5a4070e06 --- /dev/null +++ b/tests/bl/ItemBlTest.php @@ -0,0 +1,140 @@ +. +* +*/ + +namespace OCA\News\Bl; + +require_once(__DIR__ . "/../classloader.php"); + + +use \OCA\News\Db\Item; + + +class ItemBlTest extends \OCA\AppFramework\Utility\TestUtility { + + protected $api; + protected $mapper; + protected $bl; + protected $user; + protected $response; + + protected function setUp(){ + $this->api = $this->getAPIMock(); + $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') + ->disableOriginalConstructor() + ->getMock(); + $this->bl = new ItemBl($this->mapper); + $this->user = 'jack'; + $response = 'hi'; + } + + + + + /* + public function testFindAll(){ + $this->mapper->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAllFromUser($this->user); + $this->assertEquals($this->response, $result); + } + + */ + + public function testStarredCount(){ + $star = 18; + + $this->mapper->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($star)); + + $result = $this->bl->starredCount($this->user); + + $this->assertEquals($star, $result); + } + + + public function testStar(){ + $itemId = 3; + $item = new Item(); + $item->setStatus(128); + $item->setId($itemId); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($itemId), $this->equalTo($this->user)) + ->will($this->returnValue($item)); + + $this->mapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($item)); + + $this->bl->star($itemId, false, $this->user); + + $this->assertTrue($item->isUnstarred()); + } + + + public function testRead(){ + $itemId = 3; + $item = new Item(); + $item->setStatus(128); + $item->setId($itemId); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($itemId), $this->equalTo($this->user)) + ->will($this->returnValue($item)); + + $this->mapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($item)); + + $this->bl->read($itemId, false, $this->user); + + $this->assertTrue($item->isUnread()); + } + + + public function testReadFeed(){ + $feedId = 3; + + $this->mapper->expects($this->once()) + ->method('readFeed') + ->with($this->equalTo($feedId), $this->equalTo($this->user)); + + $this->bl->readFeed($feedId, $this->user); + } + +} + + + + + + diff --git a/tests/controller/FeedControllerTest.php b/tests/controller/FeedControllerTest.php index d702d8e41..cfb78083e 100644 --- a/tests/controller/FeedControllerTest.php +++ b/tests/controller/FeedControllerTest.php @@ -31,7 +31,8 @@ use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\AppFramework\Db\MultipleObjectsReturnedException; -use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Bl\BLException; require_once(__DIR__ . "/../classloader.php"); @@ -56,6 +57,7 @@ class FeedControllerTest extends ControllerTestUtility { $this->request = new Request(); $this->controller = new FeedController($this->api, $this->request, $this->bl); + $this->user = 'jack'; } private function assertFeedControllerAnnotations($methodName){ @@ -63,6 +65,18 @@ class FeedControllerTest extends ControllerTestUtility { $this->assertAnnotations($this->controller, $methodName, $annotations); } + + private function getPostController($postValue, $url=array()){ + $post = array( + 'post' => $postValue, + 'urlParams' => $url + ); + + $request = $this->getRequest($post); + return new FeedController($this->api, $request, $this->bl); + } + + public function testFeedsAnnotations(){ $this->assertFeedControllerAnnotations('feeds'); } @@ -88,13 +102,169 @@ class FeedControllerTest extends ControllerTestUtility { } - public function testReadAnnotations(){ - $this->assertFeedControllerAnnotations('read'); + public function testMoveAnnotations(){ + $this->assertFeedControllerAnnotations('move'); } - public function testMoveAnnotations(){ - $this->assertFeedControllerAnnotations('move'); + public function testFeeds(){ + $result = array( + 'feeds' => array( + array('a feed') + ) + ); + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('findAllFromUser') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'])); + + $response = $this->controller->feeds(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testActive(){ + $id = 3; + $type = 2; + $result = array( + 'activeFeed' => array( + 'id' => $id, + 'type' => $type + ) + ); + + $this->api->expects($this->at(0)) + ->method('getUserValue') + ->with($this->equalTo('lastViewedFeedId')) + ->will($this->returnValue($id)); + $this->api->expects($this->at(1)) + ->method('getUserValue') + ->with($this->equalTo('lastViewedFeedType')) + ->will($this->returnValue($type)); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCreate(){ + $result = array( + 'feeds' => array(new Feed()) + ); + + $post = array( + 'url' => 'hi', + 'parentFolderId' => 4 + ); + $this->controller = $this->getPostController($post); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + + $this->bl->expects($this->once()) + ->method('create') + ->with($this->equalTo($post['url']), + $this->equalTo($post['parentFolderId']), + $this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->controller->create(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCreateReturnsErrorForInvalidCreate(){ + $msg = 'except'; + $ex = new BLException($msg); + $this->bl->expects($this->once()) + ->method('create') + ->will($this->throwException($ex)); + + $response = $this->controller->create(); + $params = json_decode($response->render(), true); + + $this->assertEquals('error', $params['status']); + $this->assertEquals($msg, $params['msg']); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testDelete(){ + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('delete') + ->with($this->equalTo($url['feedId'])); + + $response = $this->controller->delete(); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testUpdate(){ + $result = array( + 'feeds' => array( + new Feed() + ) + ); + + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('update') + ->with($this->equalTo($url['feedId']), $this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->controller->update(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testMove(){ + $post = array( + 'parentFolderId' => 3 + ); + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController($post, $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('move') + ->with($this->equalTo($url['feedId']), + $this->equalTo($post['parentFolderId']), + $this->equalTo($this->user)); + + $response = $this->controller->move(); + + $this->assertTrue($response instanceof JSONResponse); } } \ No newline at end of file diff --git a/tests/controller/ItemControllerTest.php b/tests/controller/ItemControllerTest.php index 767b07b86..8e816e125 100644 --- a/tests/controller/ItemControllerTest.php +++ b/tests/controller/ItemControllerTest.php @@ -93,4 +93,8 @@ class ItemControllerTest extends ControllerTestUtility { } + public function testReadFeedAnnotations(){ + $this->assertItemControllerAnnotations('readFeed'); + } + } \ No newline at end of file -- cgit v1.2.3