From 643fa4624dd7ba2db1349f16131bf330aeee3387 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 9 Apr 2014 16:10:48 +0200 Subject: port to internal controller, some routes are still broken --- tests/unit/api/FeedAPITest.php | 546 ---------------- tests/unit/api/FolderAPITest.php | 440 ------------- tests/unit/api/ItemAPITest.php | 689 --------------------- tests/unit/api/NewsAPITest.php | 142 ----- .../articleenhancer/XPathArticleEnhancerTest.php | 11 +- tests/unit/businesslayer/FeedBusinessLayerTest.php | 15 +- .../unit/businesslayer/FolderBusinessLayerTest.php | 14 +- tests/unit/businesslayer/ItemBusinessLayerTest.php | 14 +- tests/unit/controller/ApiControllerTest.php | 142 +++++ tests/unit/controller/FeedApiControllerTest.php | 546 ++++++++++++++++ tests/unit/controller/FeedControllerTest.php | 20 +- tests/unit/controller/FolderApiControllerTest.php | 440 +++++++++++++ tests/unit/controller/FolderControllerTest.php | 6 +- tests/unit/controller/ItemApiControllerTest.php | 689 +++++++++++++++++++++ tests/unit/controller/ItemControllerTest.php | 10 +- tests/unit/fetcher/FeedFetcherTest.php | 18 +- 16 files changed, 1884 insertions(+), 1858 deletions(-) delete mode 100644 tests/unit/api/FeedAPITest.php delete mode 100644 tests/unit/api/FolderAPITest.php delete mode 100644 tests/unit/api/ItemAPITest.php delete mode 100644 tests/unit/api/NewsAPITest.php create mode 100644 tests/unit/controller/ApiControllerTest.php create mode 100644 tests/unit/controller/FeedApiControllerTest.php create mode 100644 tests/unit/controller/FolderApiControllerTest.php create mode 100644 tests/unit/controller/ItemApiControllerTest.php (limited to 'tests') diff --git a/tests/unit/api/FeedAPITest.php b/tests/unit/api/FeedAPITest.php deleted file mode 100644 index 935b5214b..000000000 --- a/tests/unit/api/FeedAPITest.php +++ /dev/null @@ -1,546 +0,0 @@ -. -* -*/ - -namespace OCA\News\API; - -use \OCP\IRequest; -use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; - -use \OCA\News\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\News\Core\API') - ->disableOriginalConstructor() - ->getMock(); - $this->request = $this->getMockBuilder( - '\OCP\IRequest') - ->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('NoAdminRequired', 'NoCSRFRequired', '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('NoCSRFRequired', 'API'); - $this->assertAnnotations($this->feedAPI, 'getAllFromAllUsers', $annotations); - } - - - public function testUpdateAnnotations(){ - $annotations = array('NoCSRFRequired', '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 = $this->getRequest(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 = $this->getRequest(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 = $this->getRequest(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 = $this->getRequest(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 = $this->getRequest(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 = $this->getRequest(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 testRename() { - $feedId = 3; - $feedTitle = 'test'; - - $request = $this->getRequest(array( - 'urlParams' => array( - 'feedId' => $feedId - ), - 'params' => array( - 'feedTitle' => $feedTitle - ) - )); - $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('rename') - ->with( - $this->equalTo($feedId), - $this->equalTo($feedTitle), - $this->equalTo($this->user)); - - $response = $this->feedAPI->rename(); - - $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 = $this->getRequest(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 deleted file mode 100644 index c835e4722..000000000 --- a/tests/unit/api/FolderAPITest.php +++ /dev/null @@ -1,440 +0,0 @@ -. -* -*/ - -namespace OCA\News\API; - -use \OCP\IRequest; -use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; - -use \OCA\News\Utility\ControllerTestUtility; -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\News\Core\API') - ->disableOriginalConstructor() - ->getMock(); - $this->request = $this->getMockBuilder( - '\OCP\IRequest') - ->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('NoAdminRequired', 'NoCSRFRequired', '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, - $this->getRequest(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, - $this->getRequest(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, - $this->getRequest(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, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - '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('rename') - ->with($this->equalTo($folderId), - $this->equalTo($folderName), - $this->equalTo($this->user)); - - $response = $this->folderAPI->update(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - public function testUpdateDoesNotExist() { - $folderId = 23; - $folderName = 'test'; - - $this->folderAPI = new FolderAPI( - $this->api, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - '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('rename') - ->will($this->throwException(new BusinessLayerException($this->msg))); - - $response = $this->folderAPI->update(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - - public function testUpdateExists() { - $folderId = 23; - $folderName = 'test'; - - $this->folderAPI = new FolderAPI( - $this->api, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - '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('rename') - ->will($this->throwException(new BusinessLayerConflictException($this->msg))); - - $response = $this->folderAPI->update(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); - } - - - public function testUpdateInvalidFolderName() { - $folderId = 23; - $folderName = ''; - - $this->folderAPI = new FolderAPI( - $this->api, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - '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('rename') - ->will($this->throwException(new BusinessLayerValidationException($this->msg))); - - $response = $this->folderAPI->update(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - } - - - public function testRead() { - $request = $this->getRequest(array( - 'urlParams' => array( - 'folderId' => 3 - ), - 'params' => array( - 'newestItemId' => 30, - ) - )); - $this->folderAPI = new FolderAPI( - $this->api, - $request, - $this->folderBusinessLayer, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('readFolder') - ->with( - $this->equalTo(3), - $this->equalTo(30), - $this->equalTo($this->user)); - - $response = $this->folderAPI->read(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - -} diff --git a/tests/unit/api/ItemAPITest.php b/tests/unit/api/ItemAPITest.php deleted file mode 100644 index 4c44e37f2..000000000 --- a/tests/unit/api/ItemAPITest.php +++ /dev/null @@ -1,689 +0,0 @@ -. -* -*/ - -namespace OCA\News\API; - -use \OCP\IRequest; -use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; - -use \OCA\News\Utility\ControllerTestUtility; -use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\Db\Item; - -require_once(__DIR__ . "/../../classloader.php"); - - -class ItemAPITest extends ControllerTestUtility { - - private $itemBusinessLayer; - private $itemAPI; - private $api; - private $user; - private $request; - private $msg; - - protected function setUp() { - $this->api = $this->getMockBuilder( - '\OCA\News\Core\API') - ->disableOriginalConstructor() - ->getMock(); - $this->request = $this->getMockBuilder( - '\OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - $this->itemBusinessLayer = $this->getMockBuilder( - '\OCA\News\BusinessLayer\ItemBusinessLayer') - ->disableOriginalConstructor() - ->getMock(); - $this->user = 'tom'; - $this->itemAPI = new ItemAPI( - $this->api, - $this->request, - $this->itemBusinessLayer - ); - $this->msg = 'hi'; - } - - - private function assertDefaultAnnotations($methodName){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); - $this->assertAnnotations($this->itemAPI, $methodName, $annotations); - } - - - public function testGetAllAnnotations(){ - $this->assertDefaultAnnotations('getAll'); - } - - - public function testGetUpdatedAnnotations(){ - $this->assertDefaultAnnotations('getUpdated'); - } - - - public function testReadAllAnnotations(){ - $this->assertDefaultAnnotations('readAll'); - } - - - public function testReadAnnotations(){ - $this->assertDefaultAnnotations('read'); - } - - - public function testStarAnnotations(){ - $this->assertDefaultAnnotations('star'); - } - - - public function testUnreadAnnotations(){ - $this->assertDefaultAnnotations('unread'); - } - - - public function testUnstarAnnotations(){ - $this->assertDefaultAnnotations('unstar'); - } - - - public function testReadMultipleAnnotations(){ - $this->assertDefaultAnnotations('readMultiple'); - } - - - public function testStarMultipleAnnotations(){ - $this->assertDefaultAnnotations('starMultiple'); - } - - - public function testUnreadMultipleAnnotations(){ - $this->assertDefaultAnnotations('unreadMultiple'); - } - - - public function testUnstarMultipleAnnotations(){ - $this->assertDefaultAnnotations('unstarMultiple'); - } - - - public function testGetAll() { - $items = array( - new Item() - ); - $request = $this->getRequest(array('params' => array( - 'batchSize' => 30, - 'offset' => 20, - 'type' => 1, - 'id' => 2, - 'getRead' => 'false' - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('findAll') - ->with( - $this->equalTo(2), - $this->equalTo(1), - $this->equalTo(30), - $this->equalTo(20), - $this->equalTo(false), - $this->equalTo($this->user) - ) - ->will($this->returnValue($items)); - - $response = $this->itemAPI->getAll(); - - $this->assertEquals(array( - 'items' => array($items[0]->toAPI()) - ), $response->getData()); - } - - - public function testGetAllDefaultBatchSize() { - $items = array( - new Item() - ); - $request = $this->getRequest(array('params' => array( - 'offset' => 20, - 'type' => 1, - 'id' => 2, - 'getRead' => 'false' - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('findAll') - ->with( - $this->equalTo(2), - $this->equalTo(1), - $this->equalTo(20), - $this->equalTo(20), - $this->equalTo(false), - $this->equalTo($this->user) - ) - ->will($this->returnValue($items)); - - $response = $this->itemAPI->getAll(); - - $this->assertEquals(array( - 'items' => array($items[0]->toAPI()) - ), $response->getData()); - } - - - public function testGetUpdated() { - $items = array( - new Item() - ); - $request = $this->getRequest(array('params' => array( - 'lastModified' => 30, - 'type' => 1, - 'id' => 2, - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('findAllNew') - ->with( - $this->equalTo(2), - $this->equalTo(1), - $this->equalTo(30), - $this->equalTo(true), - $this->equalTo($this->user) - ) - ->will($this->returnValue($items)); - - $response = $this->itemAPI->getUpdated(); - - $this->assertEquals(array( - 'items' => array($items[0]->toAPI()) - ), $response->getData()); - } - - - public function testRead() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('read') - ->with( - $this->equalTo(2), - $this->equalTo(true), - $this->equalTo($this->user) - ); - - $response = $this->itemAPI->read(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testReadDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('read') - ->will($this->throwException(new BusinessLayerException($this->msg))); - - $response = $this->itemAPI->read(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - - public function testUnread() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('read') - ->with( - $this->equalTo(2), - $this->equalTo(false), - $this->equalTo($this->user) - ); - - $response = $this->itemAPI->unread(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testUnreadDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('read') - ->will($this->throwException(new BusinessLayerException($this->msg))); - - $response = $this->itemAPI->unread(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - - public function testStar() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('star') - ->with( - $this->equalTo(2), - $this->equalTo('hash'), - $this->equalTo(true), - $this->equalTo($this->user) - ); - - $response = $this->itemAPI->star(); - - $data = $response->getData(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testStarDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('star') - ->will($this->throwException(new BusinessLayerException($this->msg))); - - $response = $this->itemAPI->star(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - - public function testUnstar() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('star') - ->with( - $this->equalTo(2), - $this->equalTo('hash'), - $this->equalTo(false), - $this->equalTo($this->user) - ); - - $response = $this->itemAPI->unstar(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testUnstarDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('star') - ->will($this->throwException(new BusinessLayerException($this->msg))); - - $response = $this->itemAPI->unstar(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - - public function testReadAll() { - $request = $this->getRequest(array( - 'params' => array( - 'newestItemId' => 30, - ) - )); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->once()) - ->method('readAll') - ->with( - $this->equalTo(30), - $this->equalTo($this->user)); - - $response = $this->itemAPI->readAll(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - - public function testReadMultiple() { - $request = $this->getRequest(array('params' => array( - 'items' => array(2, 4) - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->at(0)) - ->method('read') - ->with($this->equalTo(2), - $this->equalTo(true), - $this->equalTo($this->user)); - $this->itemBusinessLayer->expects($this->at(1)) - ->method('read') - ->with($this->equalTo(4), - $this->equalTo(true), - $this->equalTo($this->user)); - $response = $this->itemAPI->readMultiple(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testReadMultipleDoesntCareAboutException() { - $request = $this->getRequest(array('params' => array( - 'items' => array(2, 4) - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->at(0)) - ->method('read') - ->will($this->throwException(new BusinessLayerException(''))); - $this->itemBusinessLayer->expects($this->at(1)) - ->method('read') - ->with($this->equalTo(4), - $this->equalTo(true), - $this->equalTo($this->user)); - $this->itemAPI->readMultiple(); - } - - - public function testUnreadMultiple() { - $request = $this->getRequest(array('params' => array( - 'items' => array(2, 4) - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->at(0)) - ->method('read') - ->with($this->equalTo(2), - $this->equalTo(false), - $this->equalTo($this->user)); - $this->itemBusinessLayer->expects($this->at(1)) - ->method('read') - ->with($this->equalTo(4), - $this->equalTo(false), - $this->equalTo($this->user)); - $response = $this->itemAPI->unreadMultiple(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testStarMultiple() { - $request = $this->getRequest(array('params' => array( - 'items' => array( - array( - 'feedId' => 2, - 'guidHash' => 'a' - ), - array( - 'feedId' => 4, - 'guidHash' => 'b' - ) - ) - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->at(0)) - ->method('star') - ->with($this->equalTo(2), - $this->equalTo('a'), - $this->equalTo(true), - $this->equalTo($this->user)); - $this->itemBusinessLayer->expects($this->at(1)) - ->method('star') - ->with($this->equalTo(4), - $this->equalTo('b'), - $this->equalTo(true), - $this->equalTo($this->user)); - $response = $this->itemAPI->starMultiple(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testStarMultipleDoesntCareAboutException() { - $request = $this->getRequest(array('params' => array( - 'items' => array( - array( - 'feedId' => 2, - 'guidHash' => 'a' - ), - array( - 'feedId' => 4, - 'guidHash' => 'b' - ) - ) - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->at(0)) - ->method('star') - ->will($this->throwException(new BusinessLayerException(''))); - $this->itemBusinessLayer->expects($this->at(1)) - ->method('star') - ->with($this->equalTo(4), - $this->equalTo('b'), - $this->equalTo(true), - $this->equalTo($this->user)); - $this->itemAPI->starMultiple(); - } - - - public function testUnstarMultiple() { - $request = $this->getRequest(array('params' => array( - 'items' => array( - array( - 'feedId' => 2, - 'guidHash' => 'a' - ), - array( - 'feedId' => 4, - 'guidHash' => 'b' - ) - ) - ))); - $this->itemAPI = new ItemAPI( - $this->api, - $request, - $this->itemBusinessLayer - ); - - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); - $this->itemBusinessLayer->expects($this->at(0)) - ->method('star') - ->with($this->equalTo(2), - $this->equalTo('a'), - $this->equalTo(false), - $this->equalTo($this->user)); - $this->itemBusinessLayer->expects($this->at(1)) - ->method('star') - ->with($this->equalTo(4), - $this->equalTo('b'), - $this->equalTo(false), - $this->equalTo($this->user)); - $response = $this->itemAPI->unstarMultiple(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - -} diff --git a/tests/unit/api/NewsAPITest.php b/tests/unit/api/NewsAPITest.php deleted file mode 100644 index f0aaf2411..000000000 --- a/tests/unit/api/NewsAPITest.php +++ /dev/null @@ -1,142 +0,0 @@ -. -* -*/ - -namespace OCA\News\API; - -use \OCP\IRequest; -use \OCP\AppFramework\Http\JSONResponse; - - -use \OCA\News\Utility\ControllerTestUtility; - -require_once(__DIR__ . "/../../classloader.php"); - - -class NewsAPITest extends ControllerTestUtility { - - private $api; - private $request; - private $newsAPI; - private $updater; - - protected function setUp() { - $this->api = $this->getMockBuilder( - '\OCA\News\Core\API') - ->disableOriginalConstructor() - ->getMock(); - $this->request = $this->getMockBuilder( - '\OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - $this->updater = $this->getMockBuilder( - '\OCA\News\Utility\Updater') - ->disableOriginalConstructor() - ->getMock(); - $this->newsAPI = new NewsAPI($this->api, $this->request, $this->updater); - } - - - private function assertDefaultAnnotations($methodName){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); - $this->assertAnnotations($this->newsAPI, $methodName, $annotations); - } - - public function testVersionAnnotations(){ - $this->assertDefaultAnnotations('version'); - } - - public function testBeforeUpdateAnnotations(){ - $annotations = array('NoCSRFRequired', 'API'); - $this->assertAnnotations($this->newsAPI, 'beforeUpdate', $annotations); - } - - public function testAfterUpdateAnnotations(){ - $annotations = array('NoCSRFRequired', 'API'); - $this->assertAnnotations($this->newsAPI, 'afterUpdate', $annotations); - } - - public function testGetVersion(){ - $this->api->expects($this->once()) - ->method('getAppValue') - ->with($this->equalTo('installed_version')) - ->will($this->returnValue('1.0')); - - $response = $this->newsAPI->version(); - $data = $response->getData(); - $version = $data['version']; - - $this->assertEquals('1.0', $version); - } - - - public function testBeforeUpdate(){ - $this->updater->expects($this->once()) - ->method('beforeUpdate'); - $response = $this->newsAPI->beforeUpdate(); - $this->assertTrue($response instanceof JSONResponse); - } - - - public function testAfterUpdate(){ - $this->updater->expects($this->once()) - ->method('afterUpdate'); - $response = $this->newsAPI->afterUpdate(); - $this->assertTrue($response instanceof JSONResponse); - } - - - public function testCorsAnnotations(){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'PublicPage'); - $this->assertAnnotations($this->newsAPI, 'cors', $annotations); - } - - - public function testCors() { - $this->request = $this->getRequest(array('server' => array())); - $this->newsAPI = new NewsAPI($this->api, $this->request, $this->updater); - $response = $this->newsAPI->cors(); - - $headers = $response->getHeaders(); - - $this->assertEquals('*', $headers['Access-Control-Allow-Origin']); - $this->assertEquals('PUT, POST, GET, DELETE', $headers['Access-Control-Allow-Methods']); - $this->assertEquals('true', $headers['Access-Control-Allow-Credentials']); - $this->assertEquals('Authorization, Content-Type', $headers['Access-Control-Allow-Headers']); - $this->assertEquals('1728000', $headers['Access-Control-Max-Age']); - } - - - public function testCorsUsesOriginIfGiven() { - $this->request = $this->getRequest(array('server' => array('HTTP_ORIGIN' => 'test'))); - $this->newsAPI = new NewsAPI($this->api, $this->request, $this->updater); - $response = $this->newsAPI->cors(); - - $headers = $response->getHeaders(); - - $this->assertEquals('test', $headers['Access-Control-Allow-Origin']); - } - - -} diff --git a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php index e594e105f..a600d6739 100644 --- a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php +++ b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php @@ -40,10 +40,17 @@ class XPathArticleEnhancerTest extends \OCA\News\Utility\TestUtility { private $userAgent; protected function setUp() { - $timeout = 30; + $this->timeout = 30; $this->fileFactory = $this->getMockBuilder('\OCA\News\Utility\SimplePieAPIFactory') ->disableOriginalConstructor() ->getMock(); + $config = $this->getMockBuilder( + '\OCA\News\Utility\Config') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->any()) + ->method('getFeedFetcherTimeout') + ->will($this->returnValue($this->timeout)); $this->testEnhancer = new XPathArticleEnhancer( $this->fileFactory, @@ -53,7 +60,7 @@ class XPathArticleEnhancerTest extends \OCA\News\Utility\TestUtility { '/explosm.net\/all/' => '//body/*', '/themerepublic.net/' => '//*[@class=\'post hentry\']' ), - $this->timeout + $config ); $this->redirects = 5; $this->headers = null; diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php index 4e35326d6..19ff7da7f 100644 --- a/tests/unit/businesslayer/FeedBusinessLayerTest.php +++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php @@ -53,10 +53,7 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { $this->api = $this->getAPIMock(); $this->time = 222; $this->autoPurgeMinimumInterval = 10; - $timeFactory = $this->getMockBuilder( - '\OCA\News\Utility\TimeFactory') - ->disableOriginalConstructor() - ->getMock(); + $timeFactory = $this->getMock('TimeFactory', array('getTime')); $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); @@ -73,9 +70,17 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { ->disableOriginalConstructor() ->getMock(); $this->purifier = $this->getMock('purifier', array('purify')); + $config = $this->getMockBuilder( + '\OCA\News\Utility\Config') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->any()) + ->method('getAutoPurgeMinimumInterval') + ->will($this->returnValue($this->autoPurgeMinimumInterval)); + $this->feedBusinessLayer = new FeedBusinessLayer($this->feedMapper, $this->fetcher, $this->itemMapper, $this->api, - $timeFactory, $this->autoPurgeMinimumInterval, + $timeFactory, $config, $this->enhancer, $this->purifier); $this->user = 'jack'; $response = 'hi'; diff --git a/tests/unit/businesslayer/FolderBusinessLayerTest.php b/tests/unit/businesslayer/FolderBusinessLayerTest.php index c68d0e768..17eb1b8ae 100644 --- a/tests/unit/businesslayer/FolderBusinessLayerTest.php +++ b/tests/unit/businesslayer/FolderBusinessLayerTest.php @@ -42,10 +42,7 @@ class FolderBusinessLayerTest extends \OCA\News\Utility\TestUtility { protected function setUp(){ $this->api = $this->getAPIMock(); $this->time = 222; - $timeFactory = $this->getMockBuilder( - '\OCA\News\Utility\TimeFactory') - ->disableOriginalConstructor() - ->getMock(); + $timeFactory = $this->getMock('TimeFactory', array('getTime')); $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); @@ -54,9 +51,16 @@ class FolderBusinessLayerTest extends \OCA\News\Utility\TestUtility { ->disableOriginalConstructor() ->getMock(); $this->autoPurgeMinimumInterval = 10; + $config = $this->getMockBuilder( + '\OCA\News\Utility\Config') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->any()) + ->method('getAutoPurgeMinimumInterval') + ->will($this->returnValue($this->autoPurgeMinimumInterval)); $this->folderBusinessLayer = new FolderBusinessLayer( $this->folderMapper, $this->api, $timeFactory, - $this->autoPurgeMinimumInterval); + $config); $this->user = 'hi'; } diff --git a/tests/unit/businesslayer/ItemBusinessLayerTest.php b/tests/unit/businesslayer/ItemBusinessLayerTest.php index 349ad865c..a3e7f7a14 100644 --- a/tests/unit/businesslayer/ItemBusinessLayerTest.php +++ b/tests/unit/businesslayer/ItemBusinessLayerTest.php @@ -47,10 +47,7 @@ class ItemBusinessLayerTest extends \OCA\News\Utility\TestUtility { protected function setUp(){ $this->time = 222; - $timeFactory = $this->getMockBuilder( - '\OCA\News\Utility\TimeFactory') - ->disableOriginalConstructor() - ->getMock(); + $timeFactory = $this->getMock('TimeFactory', array('getTime')); $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); @@ -66,8 +63,15 @@ class ItemBusinessLayerTest extends \OCA\News\Utility\TestUtility { ->method('typeToStatus') ->will($this->returnValue($this->status)); $this->threshold = 2; + $config = $this->getMockBuilder( + '\OCA\News\Utility\Config') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->any()) + ->method('getAutoPurgeCount') + ->will($this->returnValue($this->threshold)); $this->itemBusinessLayer = new ItemBusinessLayer($this->mapper, - $statusFlag, $timeFactory, $this->threshold); + $statusFlag, $timeFactory, $config); $this->user = 'jack'; $response = 'hi'; $this->id = 3; diff --git a/tests/unit/controller/ApiControllerTest.php b/tests/unit/controller/ApiControllerTest.php new file mode 100644 index 000000000..722830a23 --- /dev/null +++ b/tests/unit/controller/ApiControllerTest.php @@ -0,0 +1,142 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Http\JSONResponse; + + +use \OCA\News\Utility\ControllerTestUtility; + +require_once(__DIR__ . "/../../classloader.php"); + + +class ApiControllerTest extends ControllerTestUtility { + + private $api; + private $request; + private $newsAPI; + private $updater; + + protected function setUp() { + $this->api = $this->getMockBuilder( + '\OCA\News\Core\API') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->updater = $this->getMockBuilder( + '\OCA\News\Utility\Updater') + ->disableOriginalConstructor() + ->getMock(); + $this->newsAPI = new ApiController($this->api, $this->request, $this->updater); + } + + + private function assertDefaultAnnotations($methodName){ + $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); + $this->assertAnnotations($this->newsAPI, $methodName, $annotations); + } + + public function testVersionAnnotations(){ + $this->assertDefaultAnnotations('version'); + } + + public function testBeforeUpdateAnnotations(){ + $annotations = array('NoCSRFRequired', 'API'); + $this->assertAnnotations($this->newsAPI, 'beforeUpdate', $annotations); + } + + public function testAfterUpdateAnnotations(){ + $annotations = array('NoCSRFRequired', 'API'); + $this->assertAnnotations($this->newsAPI, 'afterUpdate', $annotations); + } + + public function testGetVersion(){ + $this->api->expects($this->once()) + ->method('getAppValue') + ->with($this->equalTo('installed_version')) + ->will($this->returnValue('1.0')); + + $response = $this->newsAPI->version(); + $data = $response->getData(); + $version = $data['version']; + + $this->assertEquals('1.0', $version); + } + + + public function testBeforeUpdate(){ + $this->updater->expects($this->once()) + ->method('beforeUpdate'); + $response = $this->newsAPI->beforeUpdate(); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testAfterUpdate(){ + $this->updater->expects($this->once()) + ->method('afterUpdate'); + $response = $this->newsAPI->afterUpdate(); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCorsAnnotations(){ + $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'PublicPage'); + $this->assertAnnotations($this->newsAPI, 'cors', $annotations); + } + + + public function testCors() { + $this->request = $this->getRequest(array('server' => array())); + $this->newsAPI = new ApiController($this->api, $this->request, $this->updater); + $response = $this->newsAPI->cors(); + + $headers = $response->getHeaders(); + + $this->assertEquals('*', $headers['Access-Control-Allow-Origin']); + $this->assertEquals('PUT, POST, GET, DELETE', $headers['Access-Control-Allow-Methods']); + $this->assertEquals('true', $headers['Access-Control-Allow-Credentials']); + $this->assertEquals('Authorization, Content-Type', $headers['Access-Control-Allow-Headers']); + $this->assertEquals('1728000', $headers['Access-Control-Max-Age']); + } + + + public function testCorsUsesOriginIfGiven() { + $this->request = $this->getRequest(array('server' => array('HTTP_ORIGIN' => 'test'))); + $this->newsAPI = new ApiController($this->api, $this->request, $this->updater); + $response = $this->newsAPI->cors(); + + $headers = $response->getHeaders(); + + $this->assertEquals('test', $headers['Access-Control-Allow-Origin']); + } + + +} diff --git a/tests/unit/controller/FeedApiControllerTest.php b/tests/unit/controller/FeedApiControllerTest.php new file mode 100644 index 000000000..8bb8447bd --- /dev/null +++ b/tests/unit/controller/FeedApiControllerTest.php @@ -0,0 +1,546 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\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 FeedApiControllerTest 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\News\Core\API') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->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 FeedApiController( + $this->api, + $this->request, + $this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer + ); + $this->user = 'tom'; + $this->msg = 'hohoho'; + } + + + private function assertDefaultAnnotations($methodName){ + $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); + $this->assertAnnotations($this->feedAPI, $methodName, $annotations); + } + + + public function testGetAllAnnotations(){ + $this->assertDefaultAnnotations('index'); + } + + + 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 testFromUsersAnnotations(){ + $annotations = array('NoCSRFRequired', 'API'); + $this->assertAnnotations($this->feedAPI, 'fromAllUsers', $annotations); + } + + + public function testUpdateAnnotations(){ + $annotations = array('NoCSRFRequired', 'API'); + $this->assertAnnotations($this->feedAPI, 'update', $annotations); + } + + + public function testIndex() { + $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('f