2014-12-04 13:15:55 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke
|
|
|
|
* @copyright 2014 Lukas Reschke lukas@owncloud.com
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
2016-05-19 09:17:01 +00:00
|
|
|
|
|
|
|
namespace Tests\Settings\Controller;
|
2014-12-04 13:15:55 +00:00
|
|
|
|
2015-06-11 15:57:00 +00:00
|
|
|
use OC\Group\MetaData;
|
2016-05-19 09:17:01 +00:00
|
|
|
use OC\Settings\Controller\GroupsController;
|
2014-12-08 14:32:59 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2014-12-04 13:15:55 +00:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2016-10-06 12:05:52 +00:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2014-12-04 13:15:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-19 09:17:01 +00:00
|
|
|
* @package Tests\Settings\Controller
|
2014-12-04 13:15:55 +00:00
|
|
|
*/
|
|
|
|
class GroupsControllerTest extends \Test\TestCase {
|
|
|
|
|
2016-10-06 12:05:52 +00:00
|
|
|
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $userSession;
|
2014-12-04 13:15:55 +00:00
|
|
|
|
|
|
|
/** @var GroupsController */
|
|
|
|
private $groupsController;
|
|
|
|
|
|
|
|
protected function setUp() {
|
2016-10-06 12:05:52 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$l = $this->createMock(IL10N::class);
|
|
|
|
$l->method('t')
|
|
|
|
->will($this->returnCallback(function($text, $parameters = []) {
|
|
|
|
return vsprintf($text, $parameters);
|
|
|
|
}));
|
|
|
|
$this->groupsController = new GroupsController(
|
|
|
|
'settings',
|
|
|
|
$this->createMock(IRequest::class),
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession,
|
|
|
|
true,
|
|
|
|
$l
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
|
|
|
|
* to test for subadmins. Thus the test always assumes you have admin permissions...
|
|
|
|
*/
|
2015-06-11 15:57:00 +00:00
|
|
|
public function testIndexSortByName() {
|
2014-12-04 13:15:55 +00:00
|
|
|
$firstGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$firstGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('firstGroup'));
|
|
|
|
$firstGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(12));
|
|
|
|
$secondGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$secondGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('secondGroup'));
|
|
|
|
$secondGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(25));
|
|
|
|
$thirdGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$thirdGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('thirdGroup'));
|
|
|
|
$thirdGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(14));
|
|
|
|
$fourthGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$fourthGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('admin'));
|
|
|
|
$fourthGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(18));
|
|
|
|
/** @var \OC\Group\Group[] $groups */
|
|
|
|
$groups = array();
|
|
|
|
$groups[] = $firstGroup;
|
|
|
|
$groups[] = $secondGroup;
|
|
|
|
$groups[] = $thirdGroup;
|
|
|
|
$groups[] = $fourthGroup;
|
|
|
|
|
|
|
|
$user = $this->getMockBuilder('\OC\User\User')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->userSession
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('MyAdminUser'));
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager->method('search')
|
2014-12-04 13:15:55 +00:00
|
|
|
->will($this->returnValue($groups));
|
|
|
|
|
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
|
|
|
'data' => array(
|
|
|
|
'adminGroups' => array(
|
|
|
|
0 => array(
|
|
|
|
'id' => 'admin',
|
|
|
|
'name' => 'admin',
|
2015-05-18 10:23:12 +00:00
|
|
|
'usercount' => 0,//User count disabled 18,
|
2014-12-04 13:15:55 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'groups' =>
|
|
|
|
array(
|
|
|
|
0 => array(
|
2015-05-18 10:23:12 +00:00
|
|
|
'id' => 'firstGroup',
|
|
|
|
'name' => 'firstGroup',
|
|
|
|
'usercount' => 0,//User count disabled 12,
|
|
|
|
),
|
|
|
|
1 => array(
|
2014-12-04 13:15:55 +00:00
|
|
|
'id' => 'secondGroup',
|
|
|
|
'name' => 'secondGroup',
|
2015-05-18 10:23:12 +00:00
|
|
|
'usercount' => 0,//User count disabled 25,
|
2014-12-04 13:15:55 +00:00
|
|
|
),
|
2015-05-18 10:23:12 +00:00
|
|
|
2 => array(
|
2014-12-04 13:15:55 +00:00
|
|
|
'id' => 'thirdGroup',
|
|
|
|
'name' => 'thirdGroup',
|
2015-05-18 10:23:12 +00:00
|
|
|
'usercount' => 0,//User count disabled 14,
|
2014-12-04 13:15:55 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2015-06-11 15:57:00 +00:00
|
|
|
$response = $this->groupsController->index('', false, MetaData::SORT_GROUPNAME);
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
|
|
|
|
* to test for subadmins. Thus the test always assumes you have admin permissions...
|
|
|
|
*/
|
|
|
|
public function testIndexSortbyCount() {
|
|
|
|
$firstGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$firstGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('firstGroup'));
|
|
|
|
$firstGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(12));
|
|
|
|
$secondGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$secondGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('secondGroup'));
|
|
|
|
$secondGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(25));
|
|
|
|
$thirdGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$thirdGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('thirdGroup'));
|
|
|
|
$thirdGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(14));
|
|
|
|
$fourthGroup = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$fourthGroup
|
|
|
|
->method('getGID')
|
|
|
|
->will($this->returnValue('admin'));
|
|
|
|
$fourthGroup
|
|
|
|
->method('count')
|
|
|
|
->will($this->returnValue(18));
|
|
|
|
/** @var \OC\Group\Group[] $groups */
|
|
|
|
$groups = array();
|
|
|
|
$groups[] = $firstGroup;
|
|
|
|
$groups[] = $secondGroup;
|
|
|
|
$groups[] = $thirdGroup;
|
|
|
|
$groups[] = $fourthGroup;
|
|
|
|
|
|
|
|
$user = $this->getMockBuilder('\OC\User\User')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->userSession
|
2015-06-11 15:57:00 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('MyAdminUser'));
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2015-06-11 15:57:00 +00:00
|
|
|
->method('search')
|
|
|
|
->will($this->returnValue($groups));
|
|
|
|
|
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
|
|
|
'data' => array(
|
|
|
|
'adminGroups' => array(
|
|
|
|
0 => array(
|
|
|
|
'id' => 'admin',
|
|
|
|
'name' => 'admin',
|
|
|
|
'usercount' => 18,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'groups' =>
|
|
|
|
array(
|
|
|
|
0 => array(
|
|
|
|
'id' => 'secondGroup',
|
|
|
|
'name' => 'secondGroup',
|
|
|
|
'usercount' => 25,
|
|
|
|
),
|
|
|
|
1 => array(
|
|
|
|
'id' => 'thirdGroup',
|
|
|
|
'name' => 'thirdGroup',
|
|
|
|
'usercount' => 14,
|
|
|
|
),
|
|
|
|
2 => array(
|
|
|
|
'id' => 'firstGroup',
|
|
|
|
'name' => 'firstGroup',
|
|
|
|
'usercount' => 12,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
$response = $this->groupsController->index();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithExistingGroup() {
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('ExistingGroup')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2014-12-08 14:32:59 +00:00
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
2014-12-08 15:35:13 +00:00
|
|
|
'message' => 'Group already exists.'
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_CONFLICT
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
$response = $this->groupsController->create('ExistingGroup');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateSuccessful() {
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('NewGroup')
|
|
|
|
->will($this->returnValue(false));
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('createGroup')
|
|
|
|
->with('NewGroup')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2014-12-08 14:32:59 +00:00
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
2014-12-08 15:35:13 +00:00
|
|
|
'groupname' => 'NewGroup'
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_CREATED
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
$response = $this->groupsController->create('NewGroup');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateUnsuccessful() {
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('NewGroup')
|
|
|
|
->will($this->returnValue(false));
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('createGroup')
|
|
|
|
->with('NewGroup')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2014-12-08 14:32:59 +00:00
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'error',
|
|
|
|
'data' => array('message' => 'Unable to add group.')
|
|
|
|
),
|
|
|
|
Http::STATUS_FORBIDDEN
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
$response = $this->groupsController->create('NewGroup');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDestroySuccessful() {
|
|
|
|
$group = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('ExistingGroup')
|
|
|
|
->will($this->returnValue($group));
|
|
|
|
$group
|
|
|
|
->expects($this->once())
|
|
|
|
->method('delete')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2014-12-08 14:32:59 +00:00
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'success',
|
|
|
|
'data' => array('groupname' => 'ExistingGroup')
|
|
|
|
),
|
|
|
|
Http::STATUS_NO_CONTENT
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
$response = $this->groupsController->destroy('ExistingGroup');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDestroyUnsuccessful() {
|
2016-10-06 12:05:52 +00:00
|
|
|
$this->groupManager
|
2014-12-04 13:15:55 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('ExistingGroup')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
|
2014-12-08 14:32:59 +00:00
|
|
|
$expectedResponse = new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'error',
|
|
|
|
'data' => array('message' => 'Unable to delete group.')
|
|
|
|
),
|
|
|
|
Http::STATUS_FORBIDDEN
|
|
|
|
);
|
2014-12-04 13:15:55 +00:00
|
|
|
$response = $this->groupsController->destroy('ExistingGroup');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|