groupscontrollertest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 OC\Settings\Controller;
  11. use OC\Group\Group;
  12. use \OC\Settings\Application;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\DataResponse;
  15. /**
  16. * @package OC\Settings\Controller
  17. */
  18. class GroupsControllerTest extends \Test\TestCase {
  19. /** @var \OCP\AppFramework\IAppContainer */
  20. private $container;
  21. /** @var GroupsController */
  22. private $groupsController;
  23. protected function setUp() {
  24. $app = new Application();
  25. $this->container = $app->getContainer();
  26. $this->container['AppName'] = 'settings';
  27. $this->container['GroupManager'] = $this->getMockBuilder('\OCP\IGroupManager')
  28. ->disableOriginalConstructor()->getMock();
  29. $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session')
  30. ->disableOriginalConstructor()->getMock();
  31. $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N')
  32. ->disableOriginalConstructor()->getMock();
  33. $this->container['IsAdmin'] = true;
  34. $this->container['L10N']
  35. ->expects($this->any())
  36. ->method('t')
  37. ->will($this->returnCallback(function($text, $parameters = array()) {
  38. return vsprintf($text, $parameters);
  39. }));
  40. $this->groupsController = $this->container['GroupsController'];
  41. }
  42. /**
  43. * TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
  44. * to test for subadmins. Thus the test always assumes you have admin permissions...
  45. */
  46. public function testIndex() {
  47. $firstGroup = $this->getMockBuilder('\OC\Group\Group')
  48. ->disableOriginalConstructor()->getMock();
  49. $firstGroup
  50. ->method('getGID')
  51. ->will($this->returnValue('firstGroup'));
  52. $firstGroup
  53. ->method('count')
  54. ->will($this->returnValue(12));
  55. $secondGroup = $this->getMockBuilder('\OC\Group\Group')
  56. ->disableOriginalConstructor()->getMock();
  57. $secondGroup
  58. ->method('getGID')
  59. ->will($this->returnValue('secondGroup'));
  60. $secondGroup
  61. ->method('count')
  62. ->will($this->returnValue(25));
  63. $thirdGroup = $this->getMockBuilder('\OC\Group\Group')
  64. ->disableOriginalConstructor()->getMock();
  65. $thirdGroup
  66. ->method('getGID')
  67. ->will($this->returnValue('thirdGroup'));
  68. $thirdGroup
  69. ->method('count')
  70. ->will($this->returnValue(14));
  71. $fourthGroup = $this->getMockBuilder('\OC\Group\Group')
  72. ->disableOriginalConstructor()->getMock();
  73. $fourthGroup
  74. ->method('getGID')
  75. ->will($this->returnValue('admin'));
  76. $fourthGroup
  77. ->method('count')
  78. ->will($this->returnValue(18));
  79. /** @var \OC\Group\Group[] $groups */
  80. $groups = array();
  81. $groups[] = $firstGroup;
  82. $groups[] = $secondGroup;
  83. $groups[] = $thirdGroup;
  84. $groups[] = $fourthGroup;
  85. $user = $this->getMockBuilder('\OC\User\User')
  86. ->disableOriginalConstructor()->getMock();
  87. $this->container['UserSession']
  88. ->expects($this->once())
  89. ->method('getUser')
  90. ->will($this->returnValue($user));
  91. $user
  92. ->expects($this->once())
  93. ->method('getUID')
  94. ->will($this->returnValue('MyAdminUser'));
  95. $this->container['GroupManager']
  96. ->method('search')
  97. ->will($this->returnValue($groups));
  98. $expectedResponse = new DataResponse(
  99. array(
  100. 'data' => array(
  101. 'adminGroups' => array(
  102. 0 => array(
  103. 'id' => 'admin',
  104. 'name' => 'admin',
  105. 'usercount' => 18
  106. )
  107. ),
  108. 'groups' =>
  109. array(
  110. 0 => array(
  111. 'id' => 'secondGroup',
  112. 'name' => 'secondGroup',
  113. 'usercount' => 25
  114. ),
  115. 1 => array(
  116. 'id' => 'thirdGroup',
  117. 'name' => 'thirdGroup',
  118. 'usercount' => 14
  119. ),
  120. 2 => array(
  121. 'id' => 'firstGroup',
  122. 'name' => 'firstGroup',
  123. 'usercount' => 12
  124. )
  125. )
  126. )
  127. )
  128. );
  129. $response = $this->groupsController->index();
  130. $this->assertEquals($expectedResponse, $response);
  131. }
  132. public function testCreateWithExistingGroup() {
  133. $this->container['GroupManager']
  134. ->expects($this->once())
  135. ->method('groupExists')
  136. ->with('ExistingGroup')
  137. ->will($this->returnValue(true));
  138. $expectedResponse = new DataResponse(
  139. array(
  140. 'message' => 'Group already exists.'
  141. ),
  142. Http::STATUS_CONFLICT
  143. );
  144. $response = $this->groupsController->create('ExistingGroup');
  145. $this->assertEquals($expectedResponse, $response);
  146. }
  147. public function testCreateSuccessful() {
  148. $this->container['GroupManager']
  149. ->expects($this->once())
  150. ->method('groupExists')
  151. ->with('NewGroup')
  152. ->will($this->returnValue(false));
  153. $this->container['GroupManager']
  154. ->expects($this->once())
  155. ->method('createGroup')
  156. ->with('NewGroup')
  157. ->will($this->returnValue(true));
  158. $expectedResponse = new DataResponse(
  159. array(
  160. 'groupname' => 'NewGroup'
  161. ),
  162. Http::STATUS_CREATED
  163. );
  164. $response = $this->groupsController->create('NewGroup');
  165. $this->assertEquals($expectedResponse, $response);
  166. }
  167. public function testCreateUnsuccessful() {
  168. $this->container['GroupManager']
  169. ->expects($this->once())
  170. ->method('groupExists')
  171. ->with('NewGroup')
  172. ->will($this->returnValue(false));
  173. $this->container['GroupManager']
  174. ->expects($this->once())
  175. ->method('createGroup')
  176. ->with('NewGroup')
  177. ->will($this->returnValue(false));
  178. $expectedResponse = new DataResponse(
  179. array(
  180. 'status' => 'error',
  181. 'data' => array('message' => 'Unable to add group.')
  182. ),
  183. Http::STATUS_FORBIDDEN
  184. );
  185. $response = $this->groupsController->create('NewGroup');
  186. $this->assertEquals($expectedResponse, $response);
  187. }
  188. public function testDestroySuccessful() {
  189. $group = $this->getMockBuilder('\OC\Group\Group')
  190. ->disableOriginalConstructor()->getMock();
  191. $this->container['GroupManager']
  192. ->expects($this->once())
  193. ->method('get')
  194. ->with('ExistingGroup')
  195. ->will($this->returnValue($group));
  196. $group
  197. ->expects($this->once())
  198. ->method('delete')
  199. ->will($this->returnValue(true));
  200. $expectedResponse = new DataResponse(
  201. array(
  202. 'status' => 'success',
  203. 'data' => array('groupname' => 'ExistingGroup')
  204. ),
  205. Http::STATUS_NO_CONTENT
  206. );
  207. $response = $this->groupsController->destroy('ExistingGroup');
  208. $this->assertEquals($expectedResponse, $response);
  209. }
  210. public function testDestroyUnsuccessful() {
  211. $this->container['GroupManager']
  212. ->expects($this->once())
  213. ->method('get')
  214. ->with('ExistingGroup')
  215. ->will($this->returnValue(null));
  216. $expectedResponse = new DataResponse(
  217. array(
  218. 'status' => 'error',
  219. 'data' => array('message' => 'Unable to delete group.')
  220. ),
  221. Http::STATUS_FORBIDDEN
  222. );
  223. $response = $this->groupsController->destroy('ExistingGroup');
  224. $this->assertEquals($expectedResponse, $response);
  225. }
  226. }