ManagerTest.php 30 KB

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