diff options
Diffstat (limited to 'tests/unit/controller')
-rw-r--r-- | tests/unit/controller/ApiControllerTest.php | 142 | ||||
-rw-r--r-- | tests/unit/controller/FeedApiControllerTest.php | 546 | ||||
-rw-r--r-- | tests/unit/controller/FeedControllerTest.php | 20 | ||||
-rw-r--r-- | tests/unit/controller/FolderApiControllerTest.php | 440 | ||||
-rw-r--r-- | tests/unit/controller/FolderControllerTest.php | 6 | ||||
-rw-r--r-- | tests/unit/controller/ItemApiControllerTest.php | 689 | ||||
-rw-r--r-- | tests/unit/controller/ItemControllerTest.php | 10 |
7 files changed, 1835 insertions, 18 deletions
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 @@ +<?php + +/** +* ownCloud - News +* +* @author Alessandro Cosentino +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +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 @@ +<?php + +/** +* ownCloud - News +* +* @author Alessandro Cosentino +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +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('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $response = $this->feedAPI->index(); + + $this->assertEquals(array( + 'feeds' => array($feeds[0]->toAPI()), + 'starredCount' => $starredCount, + 'newestItemId' => $newestItemId + ), $response->getData()); + } + + + public function testIndexNoNewestItemId() { + $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->index(); + + $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 FeedApiController( + $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 FeedApiController( + $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 FeedApiController( + $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 FeedApiController( + $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 FeedApiController( + $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 FeedApiController( + $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 FeedApiController( + $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 testfromAllUsers(){ + $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->fromAllUsers(); + $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 FeedApiController( + $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/controller/FeedControllerTest.php b/tests/unit/controller/FeedControllerTest.php index ac7b998cb..90c1b8e14 100644 --- a/tests/unit/controller/FeedControllerTest.php +++ b/tests/unit/controller/FeedControllerTest.php @@ -91,7 +91,7 @@ class FeedControllerTest extends ControllerTestUtility { public function testFeedsAnnotations(){ - $this->assertFeedControllerAnnotations('feeds'); + $this->assertFeedControllerAnnotations('index'); } @@ -126,14 +126,14 @@ class FeedControllerTest extends ControllerTestUtility { public function testImportArticlesAnnotations(){ - $this->assertFeedControllerAnnotations('importArticles'); + $this->assertFeedControllerAnnotations('import'); } public function testReadAnnotations(){ $this->assertFeedControllerAnnotations('read'); } - public function testFeeds(){ + public function testIndex(){ $result = array( 'feeds' => array( array('a feed'), @@ -156,14 +156,14 @@ class FeedControllerTest extends ControllerTestUtility { ->with($this->equalTo($this->user)) ->will($this->returnValue($result['starred'])); - $response = $this->controller->feeds(); + $response = $this->controller->index(); $this->assertEquals($result, $response->getData()); $this->assertTrue($response instanceof JSONResponse); } - public function testFeedsHighestItemIdExists(){ + public function testIndexHighestItemIdExists(){ $result = array( 'feeds' => array( array('a feed'), @@ -187,7 +187,7 @@ class FeedControllerTest extends ControllerTestUtility { ->with($this->equalTo($this->user)) ->will($this->returnValue($result['starred'])); - $response = $this->controller->feeds(); + $response = $this->controller->index(); $this->assertEquals($result, $response->getData()); $this->assertTrue($response instanceof JSONResponse); @@ -620,7 +620,7 @@ class FeedControllerTest extends ControllerTestUtility { } - public function testImportArticles() { + public function testImport() { $feed = new Feed(); $post = array( @@ -640,14 +640,14 @@ class FeedControllerTest extends ControllerTestUtility { $this->equalTo($this->user)) ->will($this->returnValue($feed)); - $response = $this->controller->importArticles(); + $response = $this->controller->import(); $this->assertEquals($expected, $response->getData()); $this->assertTrue($response instanceof JSONResponse); } - public function testImportArticlesCreatesNoAdditionalFeed() { + public function testImportCreatesNoAdditionalFeed() { $feed = new Feed(); $post = array( @@ -665,7 +665,7 @@ class FeedControllerTest extends ControllerTestUtility { $this->equalTo($this->user)) ->will($this->returnValue(null)); - $response = $this->controller->importArticles(); + $response = $this->controller->import(); $this->assertEquals($expected, $response->getData()); $this->assertTrue($response instanceof JSONResponse); diff --git a/tests/unit/controller/FolderApiControllerTest.php b/tests/unit/controller/FolderApiControllerTest.php new file mode 100644 index 000000000..cc799dc18 --- /dev/null +++ b/tests/unit/controller/FolderApiControllerTest.php @@ -0,0 +1,440 @@ +<?php + +/** +* ownCloud - News +* +* @author Alessandro Cosentino +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +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\BusinessLayer\BusinessLayerValidationException; + +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FolderApiControllerTest 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 FolderApiController( + $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 testIndexAnnotations(){ + $this->assertDefaultAnnotations('index'); + } + + + 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 testIndex() { + $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->index(); + + $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 FolderApiController( + $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('cr |