summaryrefslogtreecommitdiffstats
path: root/tests/unit/controller/FolderControllerTest.php
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-04-05 10:27:28 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-04-05 10:27:28 +0200
commit7171a3dba0f932bffa2b7bba6e84edf02712cea9 (patch)
tree6006aaa04fec995e858b3ab17b594274cbb0fb44 /tests/unit/controller/FolderControllerTest.php
parentd001da33947a0fa1b65f0e1b5749e9b97bd4b779 (diff)
moved tests into unit directory
Diffstat (limited to 'tests/unit/controller/FolderControllerTest.php')
-rw-r--r--tests/unit/controller/FolderControllerTest.php260
1 files changed, 260 insertions, 0 deletions
diff --git a/tests/unit/controller/FolderControllerTest.php b/tests/unit/controller/FolderControllerTest.php
new file mode 100644
index 000000000..231cc9187
--- /dev/null
+++ b/tests/unit/controller/FolderControllerTest.php
@@ -0,0 +1,260 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Copyright
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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 \OCA\AppFramework\Http\Request;
+use \OCA\AppFramework\Http\JSONResponse;
+use \OCA\AppFramework\Utility\ControllerTestUtility;
+use \OCA\AppFramework\Db\DoesNotExistException;
+use \OCA\AppFramework\Db\MultipleObjectsReturnedException;
+
+use \OCA\News\Db\Folder;
+use \OCA\News\Bl\BLException;
+
+require_once(__DIR__ . "/../../classloader.php");
+
+
+class FolderControllerTest extends ControllerTestUtility {
+
+ private $api;
+ private $bl;
+ private $request;
+ private $controller;
+
+
+ /**
+ * Gets run before each test
+ */
+ public function setUp(){
+ $this->api = $this->getAPIMock();
+ $this->bl = $this->getMockBuilder('\OCA\News\Bl\FolderBl')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->request = new Request();
+ $this->controller = new FolderController($this->api, $this->request,
+ $this->bl);
+ $this->user = 'jack';
+ }
+
+
+ private function assertFolderControllerAnnotations($methodName){
+ $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax');
+ $this->assertAnnotations($this->controller, $methodName, $annotations);
+ }
+
+
+ private function getPostController($postValue, $url=array()){
+ $post = array(
+ 'post' => $postValue,
+ 'urlParams' => $url
+ );
+
+ $request = $this->getRequest($post);
+ return new FolderController($this->api, $request, $this->bl);
+ }
+
+ public function testFoldersAnnotations(){
+ $this->assertFolderControllerAnnotations('folders');
+ }
+
+
+ public function testOpenAnnotations(){
+ $this->assertFolderControllerAnnotations('open');
+ }
+
+
+ public function testCollapseAnnotations(){
+ $this->assertFolderControllerAnnotations('collapse');
+ }
+
+
+ public function testCreateAnnotations(){
+ $this->assertFolderControllerAnnotations('create');
+ }
+
+
+ public function testDeleteAnnotations(){
+ $this->assertFolderControllerAnnotations('delete');
+ }
+
+
+ public function testRenameAnnotations(){
+ $this->assertFolderControllerAnnotations('rename');
+ }
+
+
+
+ public function testFolders(){
+ $return = array(
+ new Folder(),
+ new Folder(),
+ );
+ $this->bl->expects($this->once())
+ ->method('findAll')
+ ->will($this->returnValue($return));
+
+ $response = $this->controller->folders();
+ $expected = array(
+ 'folders' => $return
+ );
+ $this->assertEquals($expected, $response->getParams());
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+
+
+ public function testOpen(){
+ $url = array('folderId' => 5);
+ $this->controller = $this->getPostController(array(), $url);
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->bl->expects($this->once())
+ ->method('open')
+ ->with($this->equalTo($url['folderId']),
+ $this->equalTo(true), $this->equalTo($this->user));
+
+ $response = $this->controller->open();
+
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+
+
+ public function testCollapse(){
+ $url = array('folderId' => 5);
+ $this->controller = $this->getPostController(array(), $url);
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->bl->expects($this->once())
+ ->method('open')
+ ->with($this->equalTo($url['folderId']),
+ $this->equalTo(false), $this->equalTo($this->user));
+
+ $response = $this->controller->collapse();
+
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+
+
+ public function testCreate(){
+ $post = array('folderName' => 'tech');
+ $this->controller = $this->getPostController($post);
+ $result = array(
+ 'folders' => array(new Folder())
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->bl->expects($this->once())
+ ->method('create')
+ ->with($this->equalTo($post['folderName']),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($result['folders'][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('folderId' => 5);
+ $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['folderId']),
+ $this->equalTo($this->user));
+
+ $response = $this->controller->delete();
+
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+
+
+ public function testRename(){
+ $post = array('folderName' => 'tech');
+ $url = array('folderId' => 4);
+ $this->controller = $this->getPostController($post, $url);
+ $result = array(
+ 'folders' => array(new Folder())
+ );
+
+ $this->api->expects($this->once())
+ ->method('getUserId')
+ ->will($this->returnValue($this->user));
+ $this->bl->expects($this->once())
+ ->method('rename')
+ ->with($this->equalTo($url['folderId']),
+ $this->equalTo($post['folderName']),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($result['folders'][0]));
+
+ $response = $this->controller->rename();
+
+ $this->assertEquals($result, $response->getParams());
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+
+
+ public function testRenameReturnsErrorForInvalidCreate(){
+ $msg = 'except';
+ $ex = new BLException($msg);
+ $this->bl->expects($this->once())
+ ->method('rename')
+ ->will($this->throwException($ex));
+
+ $response = $this->controller->rename();
+ $params = json_decode($response->render(), true);
+
+ $this->assertEquals('error', $params['status']);
+ $this->assertEquals($msg, $params['msg']);
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+} \ No newline at end of file