manager.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 \Test\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. $backendGroupCreated = false;
  101. $backend = $this->getMock('\OC_Group_Database');
  102. $backend->expects($this->any())
  103. ->method('groupExists')
  104. ->with('group1')
  105. ->will($this->returnCallback(function () use (&$backendGroupCreated) {
  106. return $backendGroupCreated;
  107. }));
  108. $backend->expects($this->once())
  109. ->method('implementsActions')
  110. ->will($this->returnValue(true));
  111. $backend->expects($this->once())
  112. ->method('createGroup')
  113. ->will($this->returnCallback(function () use (&$backendGroupCreated) {
  114. $backendGroupCreated = true;
  115. }));;
  116. /**
  117. * @var \OC\User\Manager $userManager
  118. */
  119. $userManager = $this->getMock('\OC\User\Manager');
  120. $manager = new \OC\Group\Manager($userManager);
  121. $manager->addBackend($backend);
  122. $group = $manager->createGroup('group1');
  123. $this->assertEquals('group1', $group->getGID());
  124. }
  125. public function testCreateExists() {
  126. /**
  127. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  128. */
  129. $backend = $this->getMock('\OC_Group_Database');
  130. $backend->expects($this->any())
  131. ->method('groupExists')
  132. ->with('group1')
  133. ->will($this->returnValue(true));
  134. $backend->expects($this->never())
  135. ->method('createGroup');
  136. /**
  137. * @var \OC\User\Manager $userManager
  138. */
  139. $userManager = $this->getMock('\OC\User\Manager');
  140. $manager = new \OC\Group\Manager($userManager);
  141. $manager->addBackend($backend);
  142. $group = $manager->createGroup('group1');
  143. $this->assertEquals('group1', $group->getGID());
  144. }
  145. public function testSearch() {
  146. /**
  147. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  148. */
  149. $backend = $this->getMock('\OC_Group_Database');
  150. $backend->expects($this->once())
  151. ->method('getGroups')
  152. ->with('1')
  153. ->will($this->returnValue(array('group1')));
  154. $backend->expects($this->once())
  155. ->method('groupExists')
  156. ->with('group1')
  157. ->will($this->returnValue(true));
  158. /**
  159. * @var \OC\User\Manager $userManager
  160. */
  161. $userManager = $this->getMock('\OC\User\Manager');
  162. $manager = new \OC\Group\Manager($userManager);
  163. $manager->addBackend($backend);
  164. $groups = $manager->search('1');
  165. $this->assertEquals(1, count($groups));
  166. $group1 = reset($groups);
  167. $this->assertEquals('group1', $group1->getGID());
  168. }
  169. public function testSearchMultipleBackends() {
  170. /**
  171. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  172. */
  173. $backend1 = $this->getMock('\OC_Group_Database');
  174. $backend1->expects($this->once())
  175. ->method('getGroups')
  176. ->with('1')
  177. ->will($this->returnValue(array('group1')));
  178. $backend1->expects($this->any())
  179. ->method('groupExists')
  180. ->will($this->returnValue(true));
  181. /**
  182. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  183. */
  184. $backend2 = $this->getMock('\OC_Group_Database');
  185. $backend2->expects($this->once())
  186. ->method('getGroups')
  187. ->with('1')
  188. ->will($this->returnValue(array('group12', 'group1')));
  189. $backend2->expects($this->any())
  190. ->method('groupExists')
  191. ->will($this->returnValue(true));
  192. /**
  193. * @var \OC\User\Manager $userManager
  194. */
  195. $userManager = $this->getMock('\OC\User\Manager');
  196. $manager = new \OC\Group\Manager($userManager);
  197. $manager->addBackend($backend1);
  198. $manager->addBackend($backend2);
  199. $groups = $manager->search('1');
  200. $this->assertEquals(2, count($groups));
  201. $group1 = reset($groups);
  202. $group12 = next($groups);
  203. $this->assertEquals('group1', $group1->getGID());
  204. $this->assertEquals('group12', $group12->getGID());
  205. }
  206. public function testSearchMultipleBackendsLimitAndOffset() {
  207. /**
  208. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  209. */
  210. $backend1 = $this->getMock('\OC_Group_Database');
  211. $backend1->expects($this->once())
  212. ->method('getGroups')
  213. ->with('1', 2, 1)
  214. ->will($this->returnValue(array('group1')));
  215. $backend1->expects($this->any())
  216. ->method('groupExists')
  217. ->will($this->returnValue(true));
  218. /**
  219. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  220. */
  221. $backend2 = $this->getMock('\OC_Group_Database');
  222. $backend2->expects($this->once())
  223. ->method('getGroups')
  224. ->with('1', 2, 1)
  225. ->will($this->returnValue(array('group12')));
  226. $backend2->expects($this->any())
  227. ->method('groupExists')
  228. ->will($this->returnValue(true));
  229. /**
  230. * @var \OC\User\Manager $userManager
  231. */
  232. $userManager = $this->getMock('\OC\User\Manager');
  233. $manager = new \OC\Group\Manager($userManager);
  234. $manager->addBackend($backend1);
  235. $manager->addBackend($backend2);
  236. $groups = $manager->search('1', 2, 1);
  237. $this->assertEquals(2, count($groups));
  238. $group1 = reset($groups);
  239. $group12 = next($groups);
  240. $this->assertEquals('group1', $group1->getGID());
  241. $this->assertEquals('group12', $group12->getGID());
  242. }
  243. public function testGetUserGroups() {
  244. /**
  245. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  246. */
  247. $backend = $this->getMock('\OC_Group_Database');
  248. $backend->expects($this->once())
  249. ->method('getUserGroups')
  250. ->with('user1')
  251. ->will($this->returnValue(array('group1')));
  252. $backend->expects($this->any())
  253. ->method('groupExists')
  254. ->with('group1')
  255. ->will($this->returnValue(true));
  256. /**
  257. * @var \OC\User\Manager $userManager
  258. */
  259. $userManager = $this->getMock('\OC\User\Manager');
  260. $userBackend = $this->getMock('\OC_User_Backend');
  261. $manager = new \OC\Group\Manager($userManager);
  262. $manager->addBackend($backend);
  263. $groups = $manager->getUserGroups(new User('user1', $userBackend));
  264. $this->assertEquals(1, count($groups));
  265. $group1 = reset($groups);
  266. $this->assertEquals('group1', $group1->getGID());
  267. }
  268. public function testGetUserGroupIds() {
  269. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Manager $manager */
  270. $manager = $this->getMockBuilder('OC\Group\Manager')
  271. ->disableOriginalConstructor()
  272. ->setMethods(['getUserGroups'])
  273. ->getMock();
  274. $manager->expects($this->once())
  275. ->method('getUserGroups')
  276. ->willReturn([
  277. '123' => '123',
  278. 'abc' => 'abc',
  279. ]);
  280. /** @var \OC\User\User $user */
  281. $user = $this->getMockBuilder('OC\User\User')
  282. ->disableOriginalConstructor()
  283. ->getMock();
  284. $groups = $manager->getUserGroupIds($user);
  285. $this->assertEquals(2, count($groups));
  286. foreach ($groups as $group) {
  287. $this->assertInternalType('string', $group);
  288. }
  289. }
  290. public function testInGroup() {
  291. /**
  292. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  293. */
  294. $backend = $this->getMock('\OC_Group_Database');
  295. $backend->expects($this->once())
  296. ->method('getUserGroups')
  297. ->with('user1')
  298. ->will($this->returnValue(array('group1', 'admin', 'group2')));
  299. $backend->expects($this->any())
  300. ->method('groupExists')
  301. ->will($this->returnValue(true));
  302. /**
  303. * @var \OC\User\Manager $userManager
  304. */
  305. $userManager = $this->getMock('\OC\User\Manager');
  306. $userBackend = $this->getMock('\OC_User_Backend');
  307. $manager = new \OC\Group\Manager($userManager);
  308. $manager->addBackend($backend);
  309. $this->assertTrue($manager->isInGroup('user1', 'group1'));
  310. }
  311. public function testIsAdmin() {
  312. /**
  313. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  314. */
  315. $backend = $this->getMock('\OC_Group_Database');
  316. $backend->expects($this->once())
  317. ->method('getUserGroups')
  318. ->with('user1')
  319. ->will($this->returnValue(array('group1', 'admin', 'group2')));
  320. $backend->expects($this->any())
  321. ->method('groupExists')
  322. ->will($this->returnValue(true));
  323. /**
  324. * @var \OC\User\Manager $userManager
  325. */
  326. $userManager = $this->getMock('\OC\User\Manager');
  327. $userBackend = $this->getMock('\OC_User_Backend');
  328. $manager = new \OC\Group\Manager($userManager);
  329. $manager->addBackend($backend);
  330. $this->assertTrue($manager->isAdmin('user1'));
  331. }
  332. public function testNotAdmin() {
  333. /**
  334. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  335. */
  336. $backend = $this->getMock('\OC_Group_Database');
  337. $backend->expects($this->once())
  338. ->method('getUserGroups')
  339. ->with('user1')
  340. ->will($this->returnValue(array('group1', 'group2')));
  341. $backend->expects($this->any())
  342. ->method('groupExists')
  343. ->will($this->returnValue(true));
  344. /**
  345. * @var \OC\User\Manager $userManager
  346. */
  347. $userManager = $this->getMock('\OC\User\Manager');
  348. $userBackend = $this->getMock('\OC_User_Backend');
  349. $manager = new \OC\Group\Manager($userManager);
  350. $manager->addBackend($backend);
  351. $this->assertFalse($manager->isAdmin('user1'));
  352. }
  353. public function testGetUserGroupsMultipleBackends() {
  354. /**
  355. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  356. */
  357. $backend1 = $this->getMock('\OC_Group_Database');
  358. $backend1->expects($this->once())
  359. ->method('getUserGroups')
  360. ->with('user1')
  361. ->will($this->returnValue(array('group1')));
  362. $backend1->expects($this->any())
  363. ->method('groupExists')
  364. ->will($this->returnValue(true));
  365. /**
  366. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  367. */
  368. $backend2 = $this->getMock('\OC_Group_Database');
  369. $backend2->expects($this->once())
  370. ->method('getUserGroups')
  371. ->with('user1')
  372. ->will($this->returnValue(array('group1', 'group2')));
  373. $backend1->expects($this->any())
  374. ->method('groupExists')
  375. ->will($this->returnValue(true));
  376. /**
  377. * @var \OC\User\Manager $userManager
  378. */
  379. $userManager = $this->getMock('\OC\User\Manager');
  380. $userBackend = $this->getMock('\OC_User_Backend');
  381. $manager = new \OC\Group\Manager($userManager);
  382. $manager->addBackend($backend1);
  383. $manager->addBackend($backend2);
  384. $groups = $manager->getUserGroups(new User('user1', $userBackend));
  385. $this->assertEquals(2, count($groups));
  386. $group1 = reset($groups);
  387. $group2 = next($groups);
  388. $this->assertEquals('group1', $group1->getGID());
  389. $this->assertEquals('group2', $group2->getGID());
  390. }
  391. public function testDisplayNamesInGroupWithOneUserBackend() {
  392. /**
  393. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  394. */
  395. $backend = $this->getMock('\OC_Group_Database');
  396. $backend->expects($this->exactly(1))
  397. ->method('groupExists')
  398. ->with('testgroup')
  399. ->will($this->returnValue(true));
  400. $backend->expects($this->any())
  401. ->method('InGroup')
  402. ->will($this->returnCallback(function($uid, $gid) {
  403. switch($uid) {
  404. case 'user1' : return false;
  405. case 'user2' : return true;
  406. case 'user3' : return false;
  407. case 'user33': return true;
  408. default:
  409. return null;
  410. }
  411. }));
  412. /**
  413. * @var \OC\User\Manager $userManager
  414. */
  415. $userManager = $this->getMock('\OC\User\Manager');
  416. $userBackend = $this->getMock('\OC_User_Backend');
  417. $userManager->expects($this->any())
  418. ->method('searchDisplayName')
  419. ->with('user3')
  420. ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
  421. switch($offset) {
  422. case 0 : return array('user3' => new User('user3', $userBackend),
  423. 'user33' => new User('user33', $userBackend));
  424. case 2 : return array();
  425. }
  426. }));
  427. $userManager->expects($this->any())
  428. ->method('get')
  429. ->will($this->returnCallback(function($uid) use ($userBackend) {
  430. switch($uid) {
  431. case 'user1' : return new User('user1', $userBackend);
  432. case 'user2' : return new User('user2', $userBackend);
  433. case 'user3' : return new User('user3', $userBackend);
  434. case 'user33': return new User('user33', $userBackend);
  435. default:
  436. return null;
  437. }
  438. }));
  439. $manager = new \OC\Group\Manager($userManager);
  440. $manager->addBackend($backend);
  441. $users = $manager->displayNamesInGroup('testgroup', 'user3');
  442. $this->assertEquals(1, count($users));
  443. $this->assertFalse(isset($users['user1']));
  444. $this->assertFalse(isset($users['user2']));
  445. $this->assertFalse(isset($users['user3']));
  446. $this->assertTrue(isset($users['user33']));
  447. }
  448. public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
  449. /**
  450. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  451. */
  452. $backend = $this->getMock('\OC_Group_Database');
  453. $backend->expects($this->exactly(1))
  454. ->method('groupExists')
  455. ->with('testgroup')
  456. ->will($this->returnValue(true));
  457. $backend->expects($this->any())
  458. ->method('InGroup')
  459. ->will($this->returnCallback(function($uid, $gid) {
  460. switch($uid) {
  461. case 'user1' : return false;
  462. case 'user2' : return true;
  463. case 'user3' : return false;
  464. case 'user33': return true;
  465. case 'user333': return true;
  466. default:
  467. return null;
  468. }
  469. }));
  470. /**
  471. * @var \OC\User\Manager $userManager
  472. */
  473. $userManager = $this->getMock('\OC\User\Manager');
  474. $userBackend = $this->getMock('\OC_User_Backend');
  475. $userManager->expects($this->any())
  476. ->method('searchDisplayName')
  477. ->with('user3')
  478. ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
  479. switch($offset) {
  480. case 0 : return array('user3' => new User('user3', $userBackend),
  481. 'user33' => new User('user33', $userBackend));
  482. case 2 : return array('user333' => new User('user333', $userBackend));
  483. }
  484. }));
  485. $userManager->expects($this->any())
  486. ->method('get')
  487. ->will($this->returnCallback(function($uid) use ($userBackend) {
  488. switch($uid) {
  489. case 'user1' : return new User('user1', $userBackend);
  490. case 'user2' : return new User('user2', $userBackend);
  491. case 'user3' : return new User('user3', $userBackend);
  492. case 'user33': return new User('user33', $userBackend);
  493. case 'user333': return new User('user333', $userBackend);
  494. default:
  495. return null;
  496. }
  497. }));
  498. $manager = new \OC\Group\Manager($userManager);
  499. $manager->addBackend($backend);
  500. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
  501. $this->assertEquals(1, count($users));
  502. $this->assertFalse(isset($users['user1']));
  503. $this->assertFalse(isset($users['user2']));
  504. $this->assertFalse(isset($users['user3']));
  505. $this->assertTrue(isset($users['user33']));
  506. $this->assertFalse(isset($users['user333']));
  507. }
  508. public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified() {
  509. /**
  510. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  511. */
  512. $backend = $this->getMock('\OC_Group_Database');
  513. $backend->expects($this->exactly(1))
  514. ->method('groupExists')
  515. ->with('testgroup')
  516. ->will($this->returnValue(true));
  517. $backend->expects($this->any())
  518. ->method('inGroup')
  519. ->will($this->returnCallback(function($uid) {
  520. switch($uid) {
  521. case 'user1' : return false;
  522. case 'user2' : return true;
  523. case 'user3' : return false;
  524. case 'user33': return true;
  525. case 'user333': return true;
  526. default:
  527. return null;
  528. }
  529. }));
  530. /**
  531. * @var \OC\User\Manager $userManager
  532. */
  533. $userManager = $this->getMock('\OC\User\Manager');
  534. $userBackend = $this->getMock('\OC_User_Backend');
  535. $userManager->expects($this->any())
  536. ->method('searchDisplayName')
  537. ->with('user3')
  538. ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
  539. switch($offset) {
  540. case 0 :
  541. return array(
  542. 'user3' => new User('user3', $userBackend),
  543. 'user33' => new User('user33', $userBackend),
  544. 'user333' => new User('user333', $userBackend)
  545. );
  546. }
  547. }));
  548. $userManager->expects($this->any())
  549. ->method('get')
  550. ->will($this->returnCallback(function($uid) use ($userBackend) {
  551. switch($uid) {
  552. case 'user1' : return new User('user1', $userBackend);
  553. case 'user2' : return new User('user2', $userBackend);
  554. case 'user3' : return new User('user3', $userBackend);
  555. case 'user33': return new User('user33', $userBackend);
  556. case 'user333': return new User('user333', $userBackend);
  557. default:
  558. return null;
  559. }
  560. }));
  561. $manager = new \OC\Group\Manager($userManager);
  562. $manager->addBackend($backend);
  563. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
  564. $this->assertEquals(1, count($users));
  565. $this->assertFalse(isset($users['user1']));
  566. $this->assertFalse(isset($users['user2']));
  567. $this->assertFalse(isset($users['user3']));
  568. $this->assertFalse(isset($users['user33']));
  569. $this->assertTrue(isset($users['user333']));
  570. }
  571. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() {
  572. /**
  573. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  574. */
  575. $backend = $this->getMock('\OC_Group_Database');
  576. $backend->expects($this->exactly(1))
  577. ->method('groupExists')
  578. ->with('testgroup')
  579. ->will($this->returnValue(true));
  580. $backend->expects($this->once())
  581. ->method('usersInGroup')
  582. ->with('testgroup', '', -1, 0)
  583. ->will($this->returnValue(array('user2', 'user33')));
  584. /**
  585. * @var \OC\User\Manager $userManager
  586. */
  587. $userManager = $this->getMock('\OC\User\Manager');
  588. $userBackend = $this->getMock('\OC_User_Backend');
  589. $userManager->expects($this->any())
  590. ->method('get')
  591. ->will($this->returnCallback(function($uid) use ($userBackend) {
  592. switch($uid) {
  593. case 'user1' : return new User('user1', $userBackend);
  594. case 'user2' : return new User('user2', $userBackend);
  595. case 'user3' : return new User('user3', $userBackend);
  596. case 'user33': return new User('user33', $userBackend);
  597. default:
  598. return null;
  599. }
  600. }));
  601. $manager = new \OC\Group\Manager($userManager);
  602. $manager->addBackend($backend);
  603. $users = $manager->displayNamesInGroup('testgroup', '');
  604. $this->assertEquals(2, count($users));
  605. $this->assertFalse(isset($users['user1']));
  606. $this->assertTrue(isset($users['user2']));
  607. $this->assertFalse(isset($users['user3']));
  608. $this->assertTrue(isset($users['user33']));
  609. }
  610. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() {
  611. /**
  612. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  613. */
  614. $backend = $this->getMock('\OC_Group_Database');
  615. $backend->expects($this->exactly(1))
  616. ->method('groupExists')
  617. ->with('testgroup')
  618. ->will($this->returnValue(true));
  619. $backend->expects($this->once())
  620. ->method('usersInGroup')
  621. ->with('testgroup', '', 1, 0)
  622. ->will($this->returnValue(array('user2')));
  623. /**
  624. * @var \OC\User\Manager $userManager
  625. */
  626. $userManager = $this->getMock('\OC\User\Manager');
  627. $userBackend = $this->getMock('\OC_User_Backend');
  628. $userManager->expects($this->any())
  629. ->method('get')
  630. ->will($this->returnCallback(function($uid) use ($userBackend) {
  631. switch($uid) {
  632. case 'user1' : return new User('user1', $userBackend);
  633. case 'user2' : return new User('user2', $userBackend);
  634. case 'user3' : return new User('user3', $userBackend);
  635. case 'user33': return new User('user33', $userBackend);
  636. default:
  637. return null;
  638. }
  639. }));
  640. $manager = new \OC\Group\Manager($userManager);
  641. $manager->addBackend($backend);
  642. $users = $manager->displayNamesInGroup('testgroup', '', 1);
  643. $this->assertEquals(1, count($users));
  644. $this->assertFalse(isset($users['user1']));
  645. $this->assertTrue(isset($users['user2']));
  646. $this->assertFalse(isset($users['user3']));
  647. $this->assertFalse(isset($users['user33']));
  648. }
  649. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
  650. /**
  651. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  652. */
  653. $backend = $this->getMock('\OC_Group_Database');
  654. $backend->expects($this->exactly(1))
  655. ->method('groupExists')
  656. ->with('testgroup')
  657. ->will($this->returnValue(true));
  658. $backend->expects($this->once())
  659. ->method('usersInGroup')
  660. ->with('testgroup', '', 1, 1)
  661. ->will($this->returnValue(array('user33')));
  662. /**
  663. * @var \OC\User\Manager $userManager
  664. */
  665. $userManager = $this->getMock('\OC\User\Manager');
  666. $userBackend = $this->getMock('\OC_User_Backend');
  667. $userManager->expects($this->any())
  668. ->method('get')
  669. ->will($this->returnCallback(function($uid) use ($userBackend) {
  670. switch($uid) {
  671. case 'user1' : return new User('user1', $userBackend);
  672. case 'user2' : return new User('user2', $userBackend);
  673. case 'user3' : return new User('user3', $userBackend);
  674. case 'user33': return new User('user33', $userBackend);
  675. default:
  676. return null;
  677. }
  678. }));
  679. $manager = new \OC\Group\Manager($userManager);
  680. $manager->addBackend($backend);
  681. $users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
  682. $this->assertEquals(1, count($users));
  683. $this->assertFalse(isset($users['user1']));
  684. $this->assertFalse(isset($users['user2']));
  685. $this->assertFalse(isset($users['user3']));
  686. $this->assertTrue(isset($users['user33']));
  687. }
  688. public function testGetUserGroupsWithAddUser() {
  689. /**
  690. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  691. */
  692. $backend = $this->getMock('\OC_Group_Database');
  693. $expectedGroups = array();
  694. $backend->expects($this->any())
  695. ->method('getUserGroups')
  696. ->with('user1')
  697. ->will($this->returnCallback(function () use (&$expectedGroups) {
  698. return $expectedGroups;
  699. }));
  700. $backend->expects($this->any())
  701. ->method('groupExists')
  702. ->with('group1')
  703. ->will($this->returnValue(true));
  704. $backend->expects($this->once())
  705. ->method('implementsActions')
  706. ->will($this->returnValue(true));
  707. /**
  708. * @var \OC\User\Manager $userManager
  709. */
  710. $userManager = $this->getMock('\OC\User\Manager');
  711. $manager = new \OC\Group\Manager($userManager);
  712. $manager->addBackend($backend);
  713. // prime cache
  714. $user1 = new User('user1', null);
  715. $groups = $manager->getUserGroups($user1);
  716. $this->assertEquals(array(), $groups);
  717. // add user
  718. $group = $manager->get('group1');
  719. $group->addUser($user1);
  720. $expectedGroups = array('group1');
  721. // check result
  722. $groups = $manager->getUserGroups($user1);
  723. $this->assertEquals(1, count($groups));
  724. $group1 = reset($groups);
  725. $this->assertEquals('group1', $group1->getGID());
  726. }
  727. public function testGetUserGroupsWithRemoveUser() {
  728. /**
  729. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  730. */
  731. $backend = $this->getMock('\OC_Group_Database');
  732. $expectedGroups = array('group1');
  733. $backend->expects($this->any())
  734. ->method('getUserGroups')
  735. ->with('user1')
  736. ->will($this->returnCallback(function () use (&$expectedGroups) {
  737. return $expectedGroups;
  738. }));
  739. $backend->expects($this->any())
  740. ->method('groupExists')
  741. ->with('group1')
  742. ->will($this->returnValue(true));
  743. $backend->expects($this->once())
  744. ->method('implementsActions')
  745. ->will($this->returnValue(true));
  746. $backend->expects($this->once())
  747. ->method('inGroup')
  748. ->will($this->returnValue(true));
  749. $backend->expects($this->once())
  750. ->method('removeFromGroup')
  751. ->will($this->returnValue(true));
  752. /**
  753. * @var \OC\User\Manager $userManager
  754. */
  755. $userManager = $this->getMock('\OC\User\Manager');
  756. $manager = new \OC\Group\Manager($userManager);
  757. $manager->addBackend($backend);
  758. // prime cache
  759. $user1 = new User('user1', null);
  760. $groups = $manager->getUserGroups($user1);
  761. $this->assertEquals(1, count($groups));
  762. $group1 = reset($groups);
  763. $this->assertEquals('group1', $group1->getGID());
  764. // remove user
  765. $group = $manager->get('group1');
  766. $group->removeUser($user1);
  767. $expectedGroups = array();
  768. // check result
  769. $groups = $manager->getUserGroups($user1);
  770. $this->assertEquals(array(), $groups);
  771. }
  772. public function testGetUserIdGroups() {
  773. /**
  774. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  775. */
  776. $backend = $this->getMock('\OC_Group_Database');
  777. $backend->expects($this->any())
  778. ->method('getUserGroups')
  779. ->with('user1')
  780. ->will($this->returnValue(null));
  781. /**
  782. * @var \OC\User\Manager $userManager
  783. */
  784. $userManager = $this->getMock('\OC\User\Manager');
  785. $manager = new \OC\Group\Manager($userManager);
  786. $manager->addBackend($backend);
  787. $groups = $manager->getUserIdGroups('user1');
  788. $this->assertEquals([], $groups);
  789. }
  790. }