manager.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Group;
  9. use OC\User\User;
  10. class Manager extends \PHPUnit_Framework_TestCase {
  11. public function testGet() {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  14. */
  15. $backend = $this->getMock('\OC_Group_Database');
  16. $backend->expects($this->any())
  17. ->method('groupExists')
  18. ->with('group1')
  19. ->will($this->returnValue(true));
  20. /**
  21. * @var \OC\User\Manager $userManager
  22. */
  23. $userManager = $this->getMock('\OC\User\Manager');
  24. $manager = new \OC\Group\Manager($userManager);
  25. $manager->addBackend($backend);
  26. $group = $manager->get('group1');
  27. $this->assertNotNull($group);
  28. $this->assertEquals('group1', $group->getGID());
  29. }
  30. public function testGetNoBackend() {
  31. /**
  32. * @var \OC\User\Manager $userManager
  33. */
  34. $userManager = $this->getMock('\OC\User\Manager');
  35. $manager = new \OC\Group\Manager($userManager);
  36. $this->assertNull($manager->get('group1'));
  37. }
  38. public function testGetNotExists() {
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  41. */
  42. $backend = $this->getMock('\OC_Group_Database');
  43. $backend->expects($this->once())
  44. ->method('groupExists')
  45. ->with('group1')
  46. ->will($this->returnValue(false));
  47. /**
  48. * @var \OC\User\Manager $userManager
  49. */
  50. $userManager = $this->getMock('\OC\User\Manager');
  51. $manager = new \OC\Group\Manager($userManager);
  52. $manager->addBackend($backend);
  53. $this->assertNull($manager->get('group1'));
  54. }
  55. public function testGetDeleted() {
  56. $backend = new \OC_Group_Dummy();
  57. $backend->createGroup('group1');
  58. /**
  59. * @var \OC\User\Manager $userManager
  60. */
  61. $userManager = $this->getMock('\OC\User\Manager');
  62. $manager = new \OC\Group\Manager($userManager);
  63. $manager->addBackend($backend);
  64. $group = $manager->get('group1');
  65. $group->delete();
  66. $this->assertNull($manager->get('group1'));
  67. }
  68. public function testGetMultipleBackends() {
  69. /**
  70. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  71. */
  72. $backend1 = $this->getMock('\OC_Group_Database');
  73. $backend1->expects($this->any())
  74. ->method('groupExists')
  75. ->with('group1')
  76. ->will($this->returnValue(false));
  77. /**
  78. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  79. */
  80. $backend2 = $this->getMock('\OC_Group_Database');
  81. $backend2->expects($this->any())
  82. ->method('groupExists')
  83. ->with('group1')
  84. ->will($this->returnValue(true));
  85. /**
  86. * @var \OC\User\Manager $userManager
  87. */
  88. $userManager = $this->getMock('\OC\User\Manager');
  89. $manager = new \OC\Group\Manager($userManager);
  90. $manager->addBackend($backend1);
  91. $manager->addBackend($backend2);
  92. $group = $manager->get('group1');
  93. $this->assertNotNull($group);
  94. $this->assertEquals('group1', $group->getGID());
  95. }
  96. public function testCreate() {
  97. /**
  98. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  99. */
  100. $backend = $this->getMock('\OC_Group_Database');
  101. $backend->expects($this->any())
  102. ->method('groupExists')
  103. ->with('group1')
  104. ->will($this->returnValue(false));
  105. $backend->expects($this->once())
  106. ->method('implementsActions')
  107. ->will($this->returnValue(true));
  108. $backend->expects($this->once())
  109. ->method('createGroup');
  110. /**
  111. * @var \OC\User\Manager $userManager
  112. */
  113. $userManager = $this->getMock('\OC\User\Manager');
  114. $manager = new \OC\Group\Manager($userManager);
  115. $manager->addBackend($backend);
  116. $group = $manager->createGroup('group1');
  117. $this->assertEquals('group1', $group->getGID());
  118. }
  119. public function testCreateExists() {
  120. /**
  121. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  122. */
  123. $backend = $this->getMock('\OC_Group_Database');
  124. $backend->expects($this->any())
  125. ->method('groupExists')
  126. ->with('group1')
  127. ->will($this->returnValue(true));
  128. $backend->expects($this->never())
  129. ->method('createGroup');
  130. /**
  131. * @var \OC\User\Manager $userManager
  132. */
  133. $userManager = $this->getMock('\OC\User\Manager');
  134. $manager = new \OC\Group\Manager($userManager);
  135. $manager->addBackend($backend);
  136. $group = $manager->createGroup('group1');
  137. $this->assertEquals('group1', $group->getGID());
  138. }
  139. public function testSearch() {
  140. /**
  141. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  142. */
  143. $backend = $this->getMock('\OC_Group_Database');
  144. $backend->expects($this->once())
  145. ->method('getGroups')
  146. ->with('1')
  147. ->will($this->returnValue(array('group1')));
  148. /**
  149. * @var \OC\User\Manager $userManager
  150. */
  151. $userManager = $this->getMock('\OC\User\Manager');
  152. $manager = new \OC\Group\Manager($userManager);
  153. $manager->addBackend($backend);
  154. $groups = $manager->search('1');
  155. $this->assertEquals(1, count($groups));
  156. $group1 = $groups[0];
  157. $this->assertEquals('group1', $group1->getGID());
  158. }
  159. public function testSearchMultipleBackends() {
  160. /**
  161. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  162. */
  163. $backend1 = $this->getMock('\OC_Group_Database');
  164. $backend1->expects($this->once())
  165. ->method('getGroups')
  166. ->with('1')
  167. ->will($this->returnValue(array('group1')));
  168. /**
  169. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  170. */
  171. $backend2 = $this->getMock('\OC_Group_Database');
  172. $backend2->expects($this->once())
  173. ->method('getGroups')
  174. ->with('1')
  175. ->will($this->returnValue(array('group12', 'group1')));
  176. /**
  177. * @var \OC\User\Manager $userManager
  178. */
  179. $userManager = $this->getMock('\OC\User\Manager');
  180. $manager = new \OC\Group\Manager($userManager);
  181. $manager->addBackend($backend1);
  182. $manager->addBackend($backend2);
  183. $groups = $manager->search('1');
  184. $this->assertEquals(2, count($groups));
  185. $group1 = $groups[0];
  186. $group12 = $groups[1];
  187. $this->assertEquals('group1', $group1->getGID());
  188. $this->assertEquals('group12', $group12->getGID());
  189. }
  190. public function testSearchMultipleBackendsLimitAndOffset() {
  191. /**
  192. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  193. */
  194. $backend1 = $this->getMock('\OC_Group_Database');
  195. $backend1->expects($this->once())
  196. ->method('getGroups')
  197. ->with('1', 2, 1)
  198. ->will($this->returnValue(array('group1')));
  199. /**
  200. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  201. */
  202. $backend2 = $this->getMock('\OC_Group_Database');
  203. $backend2->expects($this->once())
  204. ->method('getGroups')
  205. ->with('1', 1, 0)
  206. ->will($this->returnValue(array('group12')));
  207. /**
  208. * @var \OC\User\Manager $userManager
  209. */
  210. $userManager = $this->getMock('\OC\User\Manager');
  211. $manager = new \OC\Group\Manager($userManager);
  212. $manager->addBackend($backend1);
  213. $manager->addBackend($backend2);
  214. $groups = $manager->search('1', 2, 1);
  215. $this->assertEquals(2, count($groups));
  216. $group1 = $groups[0];
  217. $group12 = $groups[1];
  218. $this->assertEquals('group1', $group1->getGID());
  219. $this->assertEquals('group12', $group12->getGID());
  220. }
  221. public function testGetUserGroups() {
  222. /**
  223. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  224. */
  225. $backend = $this->getMock('\OC_Group_Database');
  226. $backend->expects($this->once())
  227. ->method('getUserGroups')
  228. ->with('user1')
  229. ->will($this->returnValue(array('group1')));
  230. /**
  231. * @var \OC\User\Manager $userManager
  232. */
  233. $userManager = $this->getMock('\OC\User\Manager');
  234. $manager = new \OC\Group\Manager($userManager);
  235. $manager->addBackend($backend);
  236. $groups = $manager->getUserGroups(new User('user1', null));
  237. $this->assertEquals(1, count($groups));
  238. $group1 = $groups[0];
  239. $this->assertEquals('group1', $group1->getGID());
  240. }
  241. public function testGetUserGroupsMultipleBackends() {
  242. /**
  243. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  244. */
  245. $backend1 = $this->getMock('\OC_Group_Database');
  246. $backend1->expects($this->once())
  247. ->method('getUserGroups')
  248. ->with('user1')
  249. ->will($this->returnValue(array('group1')));
  250. /**
  251. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  252. */
  253. $backend2 = $this->getMock('\OC_Group_Database');
  254. $backend2->expects($this->once())
  255. ->method('getUserGroups')
  256. ->with('user1')
  257. ->will($this->returnValue(array('group1', 'group2')));
  258. /**
  259. * @var \OC\User\Manager $userManager
  260. */
  261. $userManager = $this->getMock('\OC\User\Manager');
  262. $manager = new \OC\Group\Manager($userManager);
  263. $manager->addBackend($backend1);
  264. $manager->addBackend($backend2);
  265. $groups = $manager->getUserGroups(new User('user1', null));
  266. $this->assertEquals(2, count($groups));
  267. $group1 = $groups[0];
  268. $group2 = $groups[1];
  269. $this->assertEquals('group1', $group1->getGID());
  270. $this->assertEquals('group2', $group2->getGID());
  271. }
  272. }