From 7aaaa14b8328c9d6b29807d5591835413d40625c Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 2 Feb 2013 15:58:40 +0100 Subject: added the collapse method for the foldercontroller + tests --- controller/foldercontroller.php | 20 +++++++++ tests/controller/FolderControllerTest.php | 74 +++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index e0ae5f8fb..2e890cb5d 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -28,6 +28,7 @@ namespace OCA\News\Controller; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Db\DoesNotExistException; class FolderController extends Controller { @@ -52,4 +53,23 @@ class FolderController extends Controller { } + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @Ajax + * + * Collapses a folder + */ + public function collapse(){ + $folderId = (int) $this->params('folderId'); + + try { + $this->folderMapper->setCollapsed($folderId, true); + return $this->renderJSON(array()); + } catch (DoesNotExistException $e) { + return $this->renderJSON(array(), $e->getMessage()); + } + } + + } \ No newline at end of file diff --git a/tests/controller/FolderControllerTest.php b/tests/controller/FolderControllerTest.php index f1a853400..ac7ca30f0 100644 --- a/tests/controller/FolderControllerTest.php +++ b/tests/controller/FolderControllerTest.php @@ -28,6 +28,7 @@ namespace OCA\News\Controller; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\JSONResponse; use OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Db\DoesNotExistException; require_once(__DIR__ . "/../classloader.php"); @@ -47,14 +48,16 @@ class FolderControllerTest extends ControllerTestUtility { public function setUp(){ $this->api = $this->getAPIMock(); $this->folderMapper = $this->getMock('FolderMapper', - array('getAll')); + array('getAll', 'setCollapsed')); $this->request = new Request(); $this->controller = new FolderController($this->api, $this->request, $this->folderMapper); } - + /** + * getAll + */ public function testGetAllCalled(){ $this->folderMapper->expects($this->once()) ->method('getAll') @@ -86,7 +89,7 @@ class FolderControllerTest extends ControllerTestUtility { } - public function testReturnsJSON(){ + public function testGetAllReturnsJSON(){ $this->folderMapper->expects($this->once()) ->method('getAll') ->will($this->returnValue( array() )); @@ -96,4 +99,69 @@ class FolderControllerTest extends ControllerTestUtility { $this->assertTrue($response instanceof JSONResponse); } + + /** + * collapse + */ + public function testCollapseCalled(){ + $urlParams = array('folderId' => 1); + $this->folderMapper->expects($this->once()) + ->method('setCollapsed') + ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)); + $this->controller->setURLParams($urlParams); + + $this->controller->collapse(); + } + + + public function testCollapseReturnsNoParams(){ + $urlParams = array('folderId' => 1); + $this->folderMapper->expects($this->once()) + ->method('setCollapsed') + ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)); + $this->controller->setURLParams($urlParams); + + $response = $this->controller->collapse(); + $this->assertEquals(array(), $response->getParams()); + } + + + public function testCollapseAnnotations(){ + $methodName = 'collapse'; + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); + + $this->assertAnnotations($this->controller, $methodName, $annotations); + } + + + public function testCollapseReturnsJSON(){ + $urlParams = array('folderId' => 1); + $this->folderMapper->expects($this->once()) + ->method('setCollapsed') + ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)); + $this->controller->setURLParams($urlParams); + + $response = $this->controller->collapse(); + + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCollapseExceptionReturnsJSONError(){ + $errorMsg = 'exception'; + $ex = new DoesNotExistException($errorMsg); + $urlParams = array('folderId' => 1); + $this->folderMapper->expects($this->once()) + ->method('setCollapsed') + ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)) + ->will($this->throwException($ex)); + $this->controller->setURLParams($urlParams); + + $response = $this->controller->collapse(); + + $expected = '{"status":"error","data":[],"msg":"' . $errorMsg . '"}'; + $this->assertEquals($expected, $response->render()); + } + + } \ No newline at end of file -- cgit v1.2.3