GroupsControllerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * @author Lukas Reschke
  4. * @copyright 2014 Lukas Reschke lukas@owncloud.com
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. namespace Tests\Settings\Controller;
  11. use OC\Group\MetaData;
  12. use OC\Settings\Controller\GroupsController;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\IGroupManager;
  16. use OCP\IL10N;
  17. use OCP\IRequest;
  18. use OCP\IUserSession;
  19. /**
  20. * @package Tests\Settings\Controller
  21. */
  22. class GroupsControllerTest extends \Test\TestCase {
  23. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  24. private $groupManager;
  25. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  26. private $userSession;
  27. /** @var GroupsController */
  28. private $groupsController;
  29. protected function setUp() {
  30. parent::setUp();
  31. $this->groupManager = $this->createMock(IGroupManager::class);
  32. $this->userSession = $this->createMock(IUserSession::class);
  33. $l = $this->createMock(IL10N::class);
  34. $l->method('t')
  35. ->will($this->returnCallback(function($text, $parameters = []) {
  36. return vsprintf($text, $parameters);
  37. }));
  38. $this->groupsController = new GroupsController(
  39. 'settings',
  40. $this->createMock(IRequest::class),
  41. $this->groupManager,
  42. $this->userSession,
  43. true,
  44. $l
  45. );
  46. }
  47. /**
  48. * TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
  49. * to test for subadmins. Thus the test always assumes you have admin permissions...
  50. */
  51. public function testIndexSortByName() {
  52. $firstGroup = $this->getMockBuilder('\OC\Group\Group')
  53. ->disableOriginalConstructor()->getMock();
  54. $firstGroup
  55. ->method('getGID')
  56. ->will($this->returnValue('firstGroup'));
  57. $firstGroup
  58. ->method('count')
  59. ->will($this->returnValue(12));
  60. $secondGroup = $this->getMockBuilder('\OC\Group\Group')
  61. ->disableOriginalConstructor()->getMock();
  62. $secondGroup
  63. ->method('getGID')
  64. ->will($this->returnValue('secondGroup'));
  65. $secondGroup
  66. ->method('count')
  67. ->will($this->returnValue(25));
  68. $thirdGroup = $this->getMockBuilder('\OC\Group\Group')
  69. ->disableOriginalConstructor()->getMock();
  70. $thirdGroup
  71. ->method('getGID')
  72. ->will($this->returnValue('thirdGroup'));
  73. $thirdGroup
  74. ->method('count')
  75. ->will($this->returnValue(14));
  76. $fourthGroup = $this->getMockBuilder('\OC\Group\Group')
  77. ->disableOriginalConstructor()->getMock();
  78. $fourthGroup
  79. ->method('getGID')
  80. ->will($this->returnValue('admin'));
  81. $fourthGroup
  82. ->method('count')
  83. ->will($this->returnValue(18));
  84. /** @var \OC\Group\Group[] $groups */
  85. $groups = array();
  86. $groups[] = $firstGroup;
  87. $groups[] = $secondGroup;
  88. $groups[] = $thirdGroup;
  89. $groups[] = $fourthGroup;
  90. $user = $this->getMockBuilder('\OC\User\User')
  91. ->disableOriginalConstructor()->getMock();
  92. $this->userSession
  93. ->expects($this->once())
  94. ->method('getUser')
  95. ->will($this->returnValue($user));
  96. $user
  97. ->expects($this->once())
  98. ->method('getUID')
  99. ->will($this->returnValue('MyAdminUser'));
  100. $this->groupManager->method('search')
  101. ->will($this->returnValue($groups));
  102. $expectedResponse = new DataResponse(
  103. array(
  104. 'data' => array(
  105. 'adminGroups' => array(
  106. 0 => array(
  107. 'id' => 'admin',
  108. 'name' => 'admin',
  109. 'usercount' => 0,//User count disabled 18,
  110. )
  111. ),
  112. 'groups' =>
  113. array(
  114. 0 => array(
  115. 'id' => 'firstGroup',
  116. 'name' => 'firstGroup',
  117. 'usercount' => 0,//User count disabled 12,
  118. ),
  119. 1 => array(
  120. 'id' => 'secondGroup',
  121. 'name' => 'secondGroup',
  122. 'usercount' => 0,//User count disabled 25,
  123. ),
  124. 2 => array(
  125. 'id' => 'thirdGroup',
  126. 'name' => 'thirdGroup',
  127. 'usercount' => 0,//User count disabled 14,
  128. ),
  129. )
  130. )
  131. )
  132. );
  133. $response = $this->groupsController->index('', false, MetaData::SORT_GROUPNAME);
  134. $this->assertEquals($expectedResponse, $response);
  135. }
  136. /**
  137. * TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
  138. * to test for subadmins. Thus the test always assumes you have admin permissions...
  139. */
  140. public function testIndexSortbyCount() {
  141. $firstGroup = $this->getMockBuilder('\OC\Group\Group')
  142. ->disableOriginalConstructor()->getMock();
  143. $firstGroup
  144. ->method('getGID')
  145. ->will($this->returnValue('firstGroup'));
  146. $firstGroup
  147. ->method('count')
  148. ->will($this->returnValue(12));
  149. $secondGroup = $this->getMockBuilder('\OC\Group\Group')
  150. ->disableOriginalConstructor()->getMock();
  151. $secondGroup
  152. ->method('getGID')
  153. ->will($this->returnValue('secondGroup'));
  154. $secondGroup
  155. ->method('count')
  156. ->will($this->returnValue(25));
  157. $thirdGroup = $this->getMockBuilder('\OC\Group\Group')
  158. ->disableOriginalConstructor()->getMock();
  159. $thirdGroup
  160. ->method('getGID')
  161. ->will($this->returnValue('thirdGroup'));
  162. $thirdGroup
  163. ->method('count')
  164. ->will($this->returnValue(14));
  165. $fourthGroup = $this->getMockBuilder('\OC\Group\Group')
  166. ->disableOriginalConstructor()->getMock();
  167. $fourthGroup
  168. ->method('getGID')
  169. ->will($this->returnValue('admin'));
  170. $fourthGroup
  171. ->method('count')
  172. ->will($this->returnValue(18));
  173. /** @var \OC\Group\Group[] $groups */
  174. $groups = array();
  175. $groups[] = $firstGroup;
  176. $groups[] = $secondGroup;
  177. $groups[] = $thirdGroup;
  178. $groups[] = $fourthGroup;
  179. $user = $this->getMockBuilder('\OC\User\User')
  180. ->disableOriginalConstructor()->getMock();
  181. $this->userSession
  182. ->expects($this->once())
  183. ->method('getUser')
  184. ->will($this->returnValue($user));
  185. $user
  186. ->expects($this->once())
  187. ->method('getUID')
  188. ->will($this->returnValue('MyAdminUser'));
  189. $this->groupManager
  190. ->method('search')
  191. ->will($this->returnValue($groups));
  192. $expectedResponse = new DataResponse(
  193. array(
  194. 'data' => array(
  195. 'adminGroups' => array(
  196. 0 => array(
  197. 'id' => 'admin',
  198. 'name' => 'admin',
  199. 'usercount' => 18,
  200. )
  201. ),
  202. 'groups' =>
  203. array(
  204. 0 => array(
  205. 'id' => 'secondGroup',
  206. 'name' => 'secondGroup',
  207. 'usercount' => 25,
  208. ),
  209. 1 => array(
  210. 'id' => 'thirdGroup',
  211. 'name' => 'thirdGroup',
  212. 'usercount' => 14,
  213. ),
  214. 2 => array(
  215. 'id' => 'firstGroup',
  216. 'name' => 'firstGroup',
  217. 'usercount' => 12,
  218. ),
  219. )
  220. )
  221. )
  222. );
  223. $response = $this->groupsController->index();
  224. $this->assertEquals($expectedResponse, $response);
  225. }
  226. public function testCreateWithExistingGroup() {
  227. $this->groupManager
  228. ->expects($this->once())
  229. ->method('groupExists')
  230. ->with('ExistingGroup')
  231. ->will($this->returnValue(true));
  232. $expectedResponse = new DataResponse(
  233. array(
  234. 'message' => 'Group already exists.'
  235. ),
  236. Http::STATUS_CONFLICT
  237. );
  238. $response = $this->groupsController->create('ExistingGroup');
  239. $this->assertEquals($expectedResponse, $response);
  240. }
  241. public function testCreateSuccessful() {
  242. $this->groupManager
  243. ->expects($this->once())
  244. ->method('groupExists')
  245. ->with('NewGroup')
  246. ->will($this->returnValue(false));
  247. $this->groupManager
  248. ->expects($this->once())
  249. ->method('createGroup')
  250. ->with('NewGroup')
  251. ->will($this->returnValue(true));
  252. $expectedResponse = new DataResponse(
  253. array(
  254. 'groupname' => 'NewGroup'
  255. ),
  256. Http::STATUS_CREATED
  257. );
  258. $response = $this->groupsController->create('NewGroup');
  259. $this->assertEquals($expectedResponse, $response);
  260. }
  261. public function testCreateUnsuccessful() {
  262. $this->groupManager
  263. ->expects($this->once())
  264. ->method('groupExists')
  265. ->with('NewGroup')
  266. ->will($this->returnValue(false));
  267. $this->groupManager
  268. ->expects($this->once())
  269. ->method('createGroup')
  270. ->with('NewGroup')
  271. ->will($this->returnValue(false));
  272. $expectedResponse = new DataResponse(
  273. array(
  274. 'status' => 'error',
  275. 'data' => array('message' => 'Unable to add group.')
  276. ),
  277. Http::STATUS_FORBIDDEN
  278. );
  279. $response = $this->groupsController->create('NewGroup');
  280. $this->assertEquals($expectedResponse, $response);
  281. }
  282. public function testDestroySuccessful() {
  283. $group = $this->getMockBuilder('\OC\Group\Group')
  284. ->disableOriginalConstructor()->getMock();
  285. $this->groupManager
  286. ->expects($this->once())
  287. ->method('get')
  288. ->with('ExistingGroup')
  289. ->will($this->returnValue($group));
  290. $group
  291. ->expects($this->once())
  292. ->method('delete')
  293. ->will($this->returnValue(true));
  294. $expectedResponse = new DataResponse(
  295. array(
  296. 'status' => 'success',
  297. 'data' => array('groupname' => 'ExistingGroup')
  298. ),
  299. Http::STATUS_NO_CONTENT
  300. );
  301. $response = $this->groupsController->destroy('ExistingGroup');
  302. $this->assertEquals($expectedResponse, $response);
  303. }
  304. public function testDestroyUnsuccessful() {
  305. $this->groupManager
  306. ->expects($this->once())
  307. ->method('get')
  308. ->with('ExistingGroup')
  309. ->will($this->returnValue(null));
  310. $expectedResponse = new DataResponse(
  311. array(
  312. 'status' => 'error',
  313. 'data' => array('message' => 'Unable to delete group.')
  314. ),
  315. Http::STATUS_FORBIDDEN
  316. );
  317. $response = $this->groupsController->destroy('ExistingGroup');
  318. $this->assertEquals($expectedResponse, $response);
  319. }
  320. }