ManagerTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. * @author Vincent Petry <pvince81@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud GmbH.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Group;
  23. use OC\Group\Database;
  24. use OC\User\Manager;
  25. use OCP\ILogger;
  26. use OCP\IUser;
  27. use OCP\GroupInterface;
  28. use Test\TestCase;
  29. class ManagerTest extends TestCase {
  30. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject $userManager */
  31. protected $userManager;
  32. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject $userManager */
  33. protected $logger;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->userManager = $this->createMock(Manager::class);
  37. $this->logger = $this->createMock(ILogger::class);
  38. }
  39. private function getTestUser($userId) {
  40. $mockUser = $this->createMock(IUser::class);
  41. $mockUser->expects($this->any())
  42. ->method('getUID')
  43. ->will($this->returnValue($userId));
  44. $mockUser->expects($this->any())
  45. ->method('getDisplayName')
  46. ->will($this->returnValue($userId));
  47. return $mockUser;
  48. }
  49. /**
  50. * @param null|int $implementedActions
  51. * @return \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private function getTestBackend($implementedActions = null) {
  54. if ($implementedActions === null) {
  55. $implementedActions =
  56. GroupInterface::ADD_TO_GROUP |
  57. GroupInterface::REMOVE_FROM_GOUP |
  58. GroupInterface::COUNT_USERS |
  59. GroupInterface::CREATE_GROUP |
  60. GroupInterface::DELETE_GROUP;
  61. }
  62. // need to declare it this way due to optional methods
  63. // thanks to the implementsActions logic
  64. $backend = $this->getMockBuilder(GroupInterface::class)
  65. ->disableOriginalConstructor()
  66. ->setMethods([
  67. 'getGroupDetails',
  68. 'implementsActions',
  69. 'getUserGroups',
  70. 'inGroup',
  71. 'getGroups',
  72. 'groupExists',
  73. 'usersInGroup',
  74. 'createGroup',
  75. 'addToGroup',
  76. 'removeFromGroup',
  77. ])
  78. ->getMock();
  79. $backend->expects($this->any())
  80. ->method('implementsActions')
  81. ->will($this->returnCallback(function($actions) use ($implementedActions) {
  82. return (bool)($actions & $implementedActions);
  83. }));
  84. return $backend;
  85. }
  86. public function testGet() {
  87. /**
  88. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  89. */
  90. $backend = $this->getTestBackend();
  91. $backend->expects($this->any())
  92. ->method('groupExists')
  93. ->with('group1')
  94. ->will($this->returnValue(true));
  95. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  96. $manager->addBackend($backend);
  97. $group = $manager->get('group1');
  98. $this->assertNotNull($group);
  99. $this->assertEquals('group1', $group->getGID());
  100. }
  101. public function testGetNoBackend() {
  102. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  103. $this->assertNull($manager->get('group1'));
  104. }
  105. public function testGetNotExists() {
  106. /**
  107. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  108. */
  109. $backend = $this->getTestBackend();
  110. $backend->expects($this->once())
  111. ->method('groupExists')
  112. ->with('group1')
  113. ->will($this->returnValue(false));
  114. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  115. $manager->addBackend($backend);
  116. $this->assertNull($manager->get('group1'));
  117. }
  118. public function testGetDeleted() {
  119. $backend = new \Test\Util\Group\Dummy();
  120. $backend->createGroup('group1');
  121. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  122. $manager->addBackend($backend);
  123. $group = $manager->get('group1');
  124. $group->delete();
  125. $this->assertNull($manager->get('group1'));
  126. }
  127. public function testGetMultipleBackends() {
  128. /**
  129. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
  130. */
  131. $backend1 = $this->getTestBackend();
  132. $backend1->expects($this->any())
  133. ->method('groupExists')
  134. ->with('group1')
  135. ->will($this->returnValue(false));
  136. /**
  137. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
  138. */
  139. $backend2 = $this->getTestBackend();
  140. $backend2->expects($this->any())
  141. ->method('groupExists')
  142. ->with('group1')
  143. ->will($this->returnValue(true));
  144. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  145. $manager->addBackend($backend1);
  146. $manager->addBackend($backend2);
  147. $group = $manager->get('group1');
  148. $this->assertNotNull($group);
  149. $this->assertEquals('group1', $group->getGID());
  150. }
  151. public function testCreate() {
  152. /**@var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Backend $backend */
  153. $backendGroupCreated = false;
  154. $backend = $this->getTestBackend();
  155. $backend->expects($this->any())
  156. ->method('groupExists')
  157. ->with('group1')
  158. ->will($this->returnCallback(function () use (&$backendGroupCreated) {
  159. return $backendGroupCreated;
  160. }));
  161. $backend->expects($this->once())
  162. ->method('createGroup')
  163. ->will($this->returnCallback(function () use (&$backendGroupCreated) {
  164. $backendGroupCreated = true;
  165. }));
  166. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  167. $manager->addBackend($backend);
  168. $group = $manager->createGroup('group1');
  169. $this->assertEquals('group1', $group->getGID());
  170. }
  171. public function testCreateExists() {
  172. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Backend $backend */
  173. $backend = $this->getTestBackend();
  174. $backend->expects($this->any())
  175. ->method('groupExists')
  176. ->with('group1')
  177. ->will($this->returnValue(true));
  178. $backend->expects($this->never())
  179. ->method('createGroup');
  180. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  181. $manager->addBackend($backend);
  182. $group = $manager->createGroup('group1');
  183. $this->assertEquals('group1', $group->getGID());
  184. }
  185. public function testSearch() {
  186. /**
  187. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  188. */
  189. $backend = $this->getTestBackend();
  190. $backend->expects($this->once())
  191. ->method('getGroups')
  192. ->with('1')
  193. ->will($this->returnValue(array('group1')));
  194. $backend->expects($this->once())
  195. ->method('groupExists')
  196. ->with('group1')
  197. ->will($this->returnValue(true));
  198. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  199. $manager->addBackend($backend);
  200. $groups = $manager->search('1');
  201. $this->assertCount(1, $groups);
  202. $group1 = reset($groups);
  203. $this->assertEquals('group1', $group1->getGID());
  204. }
  205. public function testSearchMultipleBackends() {
  206. /**
  207. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
  208. */
  209. $backend1 = $this->getTestBackend();
  210. $backend1->expects($this->once())
  211. ->method('getGroups')
  212. ->with('1')
  213. ->will($this->returnValue(array('group1')));
  214. $backend1->expects($this->any())
  215. ->method('groupExists')
  216. ->will($this->returnValue(true));
  217. /**
  218. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
  219. */
  220. $backend2 = $this->getTestBackend();
  221. $backend2->expects($this->once())
  222. ->method('getGroups')
  223. ->with('1')
  224. ->will($this->returnValue(array('group12', 'group1')));
  225. $backend2->expects($this->any())
  226. ->method('groupExists')
  227. ->will($this->returnValue(true));
  228. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  229. $manager->addBackend($backend1);
  230. $manager->addBackend($backend2);
  231. $groups = $manager->search('1');
  232. $this->assertCount(2, $groups);
  233. $group1 = reset($groups);
  234. $group12 = next($groups);
  235. $this->assertEquals('group1', $group1->getGID());
  236. $this->assertEquals('group12', $group12->getGID());
  237. }
  238. public function testSearchMultipleBackendsLimitAndOffset() {
  239. /**
  240. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
  241. */
  242. $backend1 = $this->getTestBackend();
  243. $backend1->expects($this->once())
  244. ->method('getGroups')
  245. ->with('1', 2, 1)
  246. ->will($this->returnValue(array('group1')));
  247. $backend1->expects($this->any())
  248. ->method('groupExists')
  249. ->will($this->returnValue(true));
  250. /**
  251. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
  252. */
  253. $backend2 = $this->getTestBackend();
  254. $backend2->expects($this->once())
  255. ->method('getGroups')
  256. ->with('1', 2, 1)
  257. ->will($this->returnValue(array('group12')));
  258. $backend2->expects($this->any())
  259. ->method('groupExists')
  260. ->will($this->returnValue(true));
  261. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  262. $manager->addBackend($backend1);
  263. $manager->addBackend($backend2);
  264. $groups = $manager->search('1', 2, 1);
  265. $this->assertCount(2, $groups);
  266. $group1 = reset($groups);
  267. $group12 = next($groups);
  268. $this->assertEquals('group1', $group1->getGID());
  269. $this->assertEquals('group12', $group12->getGID());
  270. }
  271. public function testSearchResultExistsButGroupDoesNot() {
  272. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Backend $backend */
  273. $backend = $this->createMock(Database::class);
  274. $backend->expects($this->once())
  275. ->method('getGroups')
  276. ->with('1')
  277. ->will($this->returnValue(['group1']));
  278. $backend->expects($this->once())
  279. ->method('groupExists')
  280. ->with('group1')
  281. ->will($this->returnValue(false));
  282. /** @var \OC\User\Manager $userManager */
  283. $userManager = $this->createMock(Manager::class);
  284. $manager = new \OC\Group\Manager($userManager, $this->logger);
  285. $manager->addBackend($backend);
  286. $groups = $manager->search('1');
  287. $this->assertEmpty($groups);
  288. }
  289. public function testGetUserGroups() {
  290. /**
  291. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  292. */
  293. $backend = $this->getTestBackend();
  294. $backend->expects($this->once())
  295. ->method('getUserGroups')
  296. ->with('user1')
  297. ->will($this->returnValue(array('group1')));
  298. $backend->expects($this->any())
  299. ->method('groupExists')
  300. ->with('group1')
  301. ->will($this->returnValue(true));
  302. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  303. $manager->addBackend($backend);
  304. $groups = $manager->getUserGroups($this->getTestUser('user1'));
  305. $this->assertCount(1, $groups);
  306. $group1 = reset($groups);
  307. $this->assertEquals('group1', $group1->getGID());
  308. }
  309. public function testGetUserGroupIds() {
  310. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Manager $manager */
  311. $manager = $this->getMockBuilder(\OC\Group\Manager::class)
  312. ->disableOriginalConstructor()
  313. ->setMethods(['getUserGroups'])
  314. ->getMock();
  315. $manager->expects($this->once())
  316. ->method('getUserGroups')
  317. ->willReturn([
  318. '123' => '123',
  319. 'abc' => 'abc',
  320. ]);
  321. /** @var \OC\User\User|\PHPUnit_Framework_MockObject_MockObject $user */
  322. $user = $this->createMock(IUser::class);
  323. $groups = $manager->getUserGroupIds($user);
  324. $this->assertCount(2, $groups);
  325. foreach ($groups as $group) {
  326. $this->assertInternalType('string', $group);
  327. }
  328. }
  329. public function testGetUserGroupsWithDeletedGroup() {
  330. /**
  331. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  332. */
  333. $backend = $this->createMock(Database::class);
  334. $backend->expects($this->once())
  335. ->method('getUserGroups')
  336. ->with('user1')
  337. ->will($this->returnValue(array('group1')));
  338. $backend->expects($this->any())
  339. ->method('groupExists')
  340. ->with('group1')
  341. ->will($this->returnValue(false));
  342. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  343. $manager->addBackend($backend);
  344. /** @var \OC\User\User|\PHPUnit_Framework_MockObject_MockObject $user */
  345. $user = $this->createMock(IUser::class);
  346. $user->expects($this->atLeastOnce())
  347. ->method('getUID')
  348. ->willReturn('user1');
  349. $groups = $manager->getUserGroups($user);
  350. $this->assertEmpty($groups);
  351. }
  352. public function testInGroup() {
  353. /**
  354. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  355. */
  356. $backend = $this->getTestBackend();
  357. $backend->expects($this->once())
  358. ->method('getUserGroups')
  359. ->with('user1')
  360. ->will($this->returnValue(array('group1', 'admin', 'group2')));
  361. $backend->expects($this->any())
  362. ->method('groupExists')
  363. ->will($this->returnValue(true));
  364. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  365. $manager->addBackend($backend);
  366. $this->assertTrue($manager->isInGroup('user1', 'group1'));
  367. }
  368. public function testIsAdmin() {
  369. /**
  370. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  371. */
  372. $backend = $this->getTestBackend();
  373. $backend->expects($this->once())
  374. ->method('getUserGroups')
  375. ->with('user1')
  376. ->will($this->returnValue(array('group1', 'admin', 'group2')));
  377. $backend->expects($this->any())
  378. ->method('groupExists')
  379. ->will($this->returnValue(true));
  380. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  381. $manager->addBackend($backend);
  382. $this->assertTrue($manager->isAdmin('user1'));
  383. }
  384. public function testNotAdmin() {
  385. /**
  386. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  387. */
  388. $backend = $this->getTestBackend();
  389. $backend->expects($this->once())
  390. ->method('getUserGroups')
  391. ->with('user1')
  392. ->will($this->returnValue(array('group1', 'group2')));
  393. $backend->expects($this->any())
  394. ->method('groupExists')
  395. ->will($this->returnValue(true));
  396. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  397. $manager->addBackend($backend);
  398. $this->assertFalse($manager->isAdmin('user1'));
  399. }
  400. public function testGetUserGroupsMultipleBackends() {
  401. /**
  402. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
  403. */
  404. $backend1 = $this->getTestBackend();
  405. $backend1->expects($this->once())
  406. ->method('getUserGroups')
  407. ->with('user1')
  408. ->will($this->returnValue(array('group1')));
  409. $backend1->expects($this->any())
  410. ->method('groupExists')
  411. ->will($this->returnValue(true));
  412. /**
  413. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
  414. */
  415. $backend2 = $this->getTestBackend();
  416. $backend2->expects($this->once())
  417. ->method('getUserGroups')
  418. ->with('user1')
  419. ->will($this->returnValue(array('group1', 'group2')));
  420. $backend1->expects($this->any())
  421. ->method('groupExists')
  422. ->will($this->returnValue(true));
  423. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  424. $manager->addBackend($backend1);
  425. $manager->addBackend($backend2);
  426. $groups = $manager->getUserGroups($this->getTestUser('user1'));
  427. $this->assertCount(2, $groups);
  428. $group1 = reset($groups);
  429. $group2 = next($groups);
  430. $this->assertEquals('group1', $group1->getGID());
  431. $this->assertEquals('group2', $group2->getGID());
  432. }
  433. public function testDisplayNamesInGroupWithOneUserBackend() {
  434. /**
  435. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  436. */
  437. $backend = $this->getTestBackend();
  438. $backend->expects($this->exactly(1))
  439. ->method('groupExists')
  440. ->with('testgroup')
  441. ->will($this->returnValue(true));
  442. $backend->expects($this->any())
  443. ->method('inGroup')
  444. ->will($this->returnCallback(function($uid, $gid) {
  445. switch($uid) {
  446. case 'user1' : return false;
  447. case 'user2' : return true;
  448. case 'user3' : return false;
  449. case 'user33': return true;
  450. default:
  451. return null;
  452. }
  453. }));
  454. $this->userManager->expects($this->any())
  455. ->method('searchDisplayName')
  456. ->with('user3')
  457. ->will($this->returnCallback(function($search, $limit, $offset) {
  458. switch($offset) {
  459. case 0 : return ['user3' => $this->getTestUser('user3'),
  460. 'user33' => $this->getTestUser('user33')];
  461. case 2 : return [];
  462. }
  463. return null;
  464. }));
  465. $this->userManager->expects($this->any())
  466. ->method('get')
  467. ->will($this->returnCallback(function($uid) {
  468. switch($uid) {
  469. case 'user1' : return $this->getTestUser('user1');
  470. case 'user2' : return $this->getTestUser('user2');
  471. case 'user3' : return $this->getTestUser('user3');
  472. case 'user33': return $this->getTestUser('user33');
  473. default:
  474. return null;
  475. }
  476. }));
  477. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  478. $manager->addBackend($backend);
  479. $users = $manager->displayNamesInGroup('testgroup', 'user3');
  480. $this->assertCount(1, $users);
  481. $this->assertFalse(isset($users['user1']));
  482. $this->assertFalse(isset($users['user2']));
  483. $this->assertFalse(isset($users['user3']));
  484. $this->assertTrue(isset($users['user33']));
  485. }
  486. public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
  487. /**
  488. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  489. */
  490. $backend = $this->getTestBackend();
  491. $backend->expects($this->exactly(1))
  492. ->method('groupExists')
  493. ->with('testgroup')
  494. ->will($this->returnValue(true));
  495. $backend->expects($this->any())
  496. ->method('inGroup')
  497. ->will($this->returnCallback(function($uid, $gid) {
  498. switch($uid) {
  499. case 'user1' : return false;
  500. case 'user2' : return true;
  501. case 'user3' : return false;
  502. case 'user33': return true;
  503. case 'user333': return true;
  504. default:
  505. return null;
  506. }
  507. }));
  508. $this->userManager->expects($this->any())
  509. ->method('searchDisplayName')
  510. ->with('user3')
  511. ->will($this->returnCallback(function($search, $limit, $offset) {
  512. switch($offset) {
  513. case 0 : return ['user3' => $this->getTestUser('user3'),
  514. 'user33' => $this->getTestUser('user33')];
  515. case 2 : return ['user333' => $this->getTestUser('user333')];
  516. }
  517. return null;
  518. }));
  519. $this->userManager->expects($this->any())
  520. ->method('get')
  521. ->will($this->returnCallback(function($uid) {
  522. switch($uid) {
  523. case 'user1' : return $this->getTestUser('user1');
  524. case 'user2' : return $this->getTestUser('user2');
  525. case 'user3' : return $this->getTestUser('user3');
  526. case 'user33': return $this->getTestUser('user33');
  527. case 'user333': return $this->getTestUser('user333');
  528. default:
  529. return null;
  530. }
  531. }));
  532. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  533. $manager->addBackend($backend);
  534. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
  535. $this->assertCount(1, $users);
  536. $this->assertFalse(isset($users['user1']));
  537. $this->assertFalse(isset($users['user2']));
  538. $this->assertFalse(isset($users['user3']));
  539. $this->assertTrue(isset($users['user33']));
  540. $this->assertFalse(isset($users['user333']));
  541. }
  542. public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified() {
  543. /**
  544. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  545. */
  546. $backend = $this->getTestBackend();
  547. $backend->expects($this->exactly(1))
  548. ->method('groupExists')
  549. ->with('testgroup')
  550. ->will($this->returnValue(true));
  551. $backend->expects($this->any())
  552. ->method('inGroup')
  553. ->will($this->returnCallback(function($uid) {
  554. switch($uid) {
  555. case 'user1' : return false;
  556. case 'user2' : return true;
  557. case 'user3' : return false;
  558. case 'user33': return true;
  559. case 'user333': return true;
  560. default:
  561. return null;
  562. }
  563. }));
  564. $this->userManager->expects($this->any())
  565. ->method('searchDisplayName')
  566. ->with('user3')
  567. ->will($this->returnCallback(function($search, $limit, $offset) {
  568. switch($offset) {
  569. case 0 :
  570. return [
  571. 'user3' => $this->getTestUser('user3'),
  572. 'user33' => $this->getTestUser('user33'),
  573. 'user333' => $this->getTestUser('user333')
  574. ];
  575. }
  576. return null;
  577. }));
  578. $this->userManager->expects($this->any())
  579. ->method('get')
  580. ->will($this->returnCallback(function($uid) {
  581. switch($uid) {
  582. case 'user1' : return $this->getTestUser('user1');
  583. case 'user2' : return $this->getTestUser('user2');
  584. case 'user3' : return $this->getTestUser('user3');
  585. case 'user33': return $this->getTestUser('user33');
  586. case 'user333': return $this->getTestUser('user333');
  587. default:
  588. return null;
  589. }
  590. }));
  591. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  592. $manager->addBackend($backend);
  593. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
  594. $this->assertCount(1, $users);
  595. $this->assertFalse(isset($users['user1']));
  596. $this->assertFalse(isset($users['user2']));
  597. $this->assertFalse(isset($users['user3']));
  598. $this->assertFalse(isset($users['user33']));
  599. $this->assertTrue(isset($users['user333']));
  600. }
  601. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() {
  602. /**
  603. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  604. */
  605. $backend = $this->getTestBackend();
  606. $backend->expects($this->exactly(1))
  607. ->method('groupExists')
  608. ->with('testgroup')
  609. ->will($this->returnValue(true));
  610. $backend->expects($this->once())
  611. ->method('usersInGroup')
  612. ->with('testgroup', '', -1, 0)
  613. ->will($this->returnValue(array('user2', 'user33')));
  614. $this->userManager->expects($this->any())
  615. ->method('get')
  616. ->will($this->returnCallback(function($uid) {
  617. switch($uid) {
  618. case 'user1' : return $this->getTestUser('user1');
  619. case 'user2' : return $this->getTestUser('user2');
  620. case 'user3' : return $this->getTestUser('user3');
  621. case 'user33': return $this->getTestUser('user33');
  622. default:
  623. return null;
  624. }
  625. }));
  626. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  627. $manager->addBackend($backend);
  628. $users = $manager->displayNamesInGroup('testgroup', '');
  629. $this->assertCount(2, $users);
  630. $this->assertFalse(isset($users['user1']));
  631. $this->assertTrue(isset($users['user2']));
  632. $this->assertFalse(isset($users['user3']));
  633. $this->assertTrue(isset($users['user33']));
  634. }
  635. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() {
  636. /**
  637. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  638. */
  639. $backend = $this->getTestBackend();
  640. $backend->expects($this->exactly(1))
  641. ->method('groupExists')
  642. ->with('testgroup')
  643. ->will($this->returnValue(true));
  644. $backend->expects($this->once())
  645. ->method('usersInGroup')
  646. ->with('testgroup', '', 1, 0)
  647. ->will($this->returnValue(array('user2')));
  648. $this->userManager->expects($this->any())
  649. ->method('get')
  650. ->will($this->returnCallback(function($uid) {
  651. switch($uid) {
  652. case 'user1' : return $this->getTestUser('user1');
  653. case 'user2' : return $this->getTestUser('user2');
  654. case 'user3' : return $this->getTestUser('user3');
  655. case 'user33': return $this->getTestUser('user33');
  656. default:
  657. return null;
  658. }
  659. }));
  660. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  661. $manager->addBackend($backend);
  662. $users = $manager->displayNamesInGroup('testgroup', '', 1);
  663. $this->assertCount(1, $users);
  664. $this->assertFalse(isset($users['user1']));
  665. $this->assertTrue(isset($users['user2']));
  666. $this->assertFalse(isset($users['user3']));
  667. $this->assertFalse(isset($users['user33']));
  668. }
  669. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
  670. /**
  671. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  672. */
  673. $backend = $this->getTestBackend();
  674. $backend->expects($this->exactly(1))
  675. ->method('groupExists')
  676. ->with('testgroup')
  677. ->will($this->returnValue(true));
  678. $backend->expects($this->once())
  679. ->method('usersInGroup')
  680. ->with('testgroup', '', 1, 1)
  681. ->will($this->returnValue(array('user33')));
  682. $this->userManager->expects($this->any())
  683. ->method('get')
  684. ->will($this->returnCallback(function($uid) {
  685. switch($uid) {
  686. case 'user1' : return $this->getTestUser('user1');
  687. case 'user2' : return $this->getTestUser('user2');
  688. case 'user3' : return $this->getTestUser('user3');
  689. case 'user33': return $this->getTestUser('user33');
  690. default:
  691. return null;
  692. }
  693. }));
  694. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  695. $manager->addBackend($backend);
  696. $users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
  697. $this->assertCount(1, $users);
  698. $this->assertFalse(isset($users['user1']));
  699. $this->assertFalse(isset($users['user2']));
  700. $this->assertFalse(isset($users['user3']));
  701. $this->assertTrue(isset($users['user33']));
  702. }
  703. public function testGetUserGroupsWithAddUser() {
  704. /**
  705. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  706. */
  707. $backend = $this->getTestBackend();
  708. $expectedGroups = [];
  709. $backend->expects($this->any())
  710. ->method('getUserGroups')
  711. ->with('user1')
  712. ->will($this->returnCallback(function () use (&$expectedGroups) {
  713. return $expectedGroups;
  714. }));
  715. $backend->expects($this->any())
  716. ->method('groupExists')
  717. ->with('group1')
  718. ->will($this->returnValue(true));
  719. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  720. $manager->addBackend($backend);
  721. // prime cache
  722. $user1 = $this->getTestUser('user1');
  723. $groups = $manager->getUserGroups($user1);
  724. $this->assertEquals(array(), $groups);
  725. // add user
  726. $group = $manager->get('group1');
  727. $group->addUser($user1);
  728. $expectedGroups[] = 'group1';
  729. // check result
  730. $groups = $manager->getUserGroups($user1);
  731. $this->assertCount(1, $groups);
  732. $group1 = reset($groups);
  733. $this->assertEquals('group1', $group1->getGID());
  734. }
  735. public function testGetUserGroupsWithRemoveUser() {
  736. /**
  737. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  738. */
  739. $backend = $this->getTestBackend();
  740. $expectedGroups = ['group1'];
  741. $backend->expects($this->any())
  742. ->method('getUserGroups')
  743. ->with('user1')
  744. ->will($this->returnCallback(function () use (&$expectedGroups) {
  745. return $expectedGroups;
  746. }));
  747. $backend->expects($this->any())
  748. ->method('groupExists')
  749. ->with('group1')
  750. ->will($this->returnValue(true));
  751. $backend->expects($this->once())
  752. ->method('inGroup')
  753. ->will($this->returnValue(true));
  754. $backend->expects($this->once())
  755. ->method('removeFromGroup')
  756. ->will($this->returnValue(true));
  757. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  758. $manager->addBackend($backend);
  759. // prime cache
  760. $user1 = $this->getTestUser('user1');
  761. $groups = $manager->getUserGroups($user1);
  762. $this->assertCount(1, $groups);
  763. $group1 = reset($groups);
  764. $this->assertEquals('group1', $group1->getGID());
  765. // remove user
  766. $group = $manager->get('group1');
  767. $group->removeUser($user1);
  768. $expectedGroups = array();
  769. // check result
  770. $groups = $manager->getUserGroups($user1);
  771. $this->assertEquals($expectedGroups, $groups);
  772. }
  773. public function testGetUserIdGroups() {
  774. /**
  775. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  776. */
  777. $backend = $this->getTestBackend();
  778. $backend->expects($this->any())
  779. ->method('getUserGroups')
  780. ->with('user1')
  781. ->will($this->returnValue(null));
  782. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  783. $manager->addBackend($backend);
  784. $groups = $manager->getUserIdGroups('user1');
  785. $this->assertEquals([], $groups);
  786. }
  787. public function testGroupDisplayName() {
  788. /**
  789. * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
  790. */
  791. $backend = $this->getTestBackend(
  792. GroupInterface::ADD_TO_GROUP |
  793. GroupInterface::REMOVE_FROM_GOUP |
  794. GroupInterface::COUNT_USERS |
  795. GroupInterface::CREATE_GROUP |
  796. GroupInterface::DELETE_GROUP |
  797. GroupInterface::GROUP_DETAILS
  798. );
  799. $backend->expects($this->any())
  800. ->method('getGroupDetails')
  801. ->will($this->returnValueMap([
  802. ['group1', ['gid' => 'group1', 'displayName' => 'Group One']],
  803. ['group2', ['gid' => 'group2']],
  804. ]));
  805. $manager = new \OC\Group\Manager($this->userManager, $this->logger);
  806. $manager->addBackend($backend);
  807. // group with display name
  808. $group = $manager->get('group1');
  809. $this->assertNotNull($group);
  810. $this->assertEquals('group1', $group->getGID());
  811. $this->assertEquals('Group One', $group->getDisplayName());
  812. // group without display name
  813. $group = $manager->get('group2');
  814. $this->assertNotNull($group);
  815. $this->assertEquals('group2', $group->getGID());
  816. $this->assertEquals('group2', $group->getDisplayName());
  817. }
  818. }