manager.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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 testInGroup() {
  269. /**
  270. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  271. */
  272. $backend = $this->getMock('\OC_Group_Database');
  273. $backend->expects($this->once())
  274. ->method('getUserGroups')
  275. ->with('user1')
  276. ->will($this->returnValue(array('group1', 'admin', 'group2')));
  277. $backend->expects($this->any())
  278. ->method('groupExists')
  279. ->will($this->returnValue(true));
  280. /**
  281. * @var \OC\User\Manager $userManager
  282. */
  283. $userManager = $this->getMock('\OC\User\Manager');
  284. $userBackend = $this->getMock('\OC_User_Backend');
  285. $manager = new \OC\Group\Manager($userManager);
  286. $manager->addBackend($backend);
  287. $this->assertTrue($manager->isInGroup('user1', 'group1'));
  288. }
  289. public function testIsAdmin() {
  290. /**
  291. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  292. */
  293. $backend = $this->getMock('\OC_Group_Database');
  294. $backend->expects($this->once())
  295. ->method('getUserGroups')
  296. ->with('user1')
  297. ->will($this->returnValue(array('group1', 'admin', 'group2')));
  298. $backend->expects($this->any())
  299. ->method('groupExists')
  300. ->will($this->returnValue(true));
  301. /**
  302. * @var \OC\User\Manager $userManager
  303. */
  304. $userManager = $this->getMock('\OC\User\Manager');
  305. $userBackend = $this->getMock('\OC_User_Backend');
  306. $manager = new \OC\Group\Manager($userManager);
  307. $manager->addBackend($backend);
  308. $this->assertTrue($manager->isAdmin('user1'));
  309. }
  310. public function testNotAdmin() {
  311. /**
  312. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  313. */
  314. $backend = $this->getMock('\OC_Group_Database');
  315. $backend->expects($this->once())
  316. ->method('getUserGroups')
  317. ->with('user1')
  318. ->will($this->returnValue(array('group1', 'group2')));
  319. $backend->expects($this->any())
  320. ->method('groupExists')
  321. ->will($this->returnValue(true));
  322. /**
  323. * @var \OC\User\Manager $userManager
  324. */
  325. $userManager = $this->getMock('\OC\User\Manager');
  326. $userBackend = $this->getMock('\OC_User_Backend');
  327. $manager = new \OC\Group\Manager($userManager);
  328. $manager->addBackend($backend);
  329. $this->assertFalse($manager->isAdmin('user1'));
  330. }
  331. public function testGetUserGroupsMultipleBackends() {
  332. /**
  333. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  334. */
  335. $backend1 = $this->getMock('\OC_Group_Database');
  336. $backend1->expects($this->once())
  337. ->method('getUserGroups')
  338. ->with('user1')
  339. ->will($this->returnValue(array('group1')));
  340. $backend1->expects($this->any())
  341. ->method('groupExists')
  342. ->will($this->returnValue(true));
  343. /**
  344. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2
  345. */
  346. $backend2 = $this->getMock('\OC_Group_Database');
  347. $backend2->expects($this->once())
  348. ->method('getUserGroups')
  349. ->with('user1')
  350. ->will($this->returnValue(array('group1', 'group2')));
  351. $backend1->expects($this->any())
  352. ->method('groupExists')
  353. ->will($this->returnValue(true));
  354. /**
  355. * @var \OC\User\Manager $userManager
  356. */
  357. $userManager = $this->getMock('\OC\User\Manager');
  358. $userBackend = $this->getMock('\OC_User_Backend');
  359. $manager = new \OC\Group\Manager($userManager);
  360. $manager->addBackend($backend1);
  361. $manager->addBackend($backend2);
  362. $groups = $manager->getUserGroups(new User('user1', $userBackend));
  363. $this->assertEquals(2, count($groups));
  364. $group1 = reset($groups);
  365. $group2 = next($groups);
  366. $this->assertEquals('group1', $group1->getGID());
  367. $this->assertEquals('group2', $group2->getGID());
  368. }
  369. public function testDisplayNamesInGroupWithOneUserBackend() {
  370. /**
  371. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  372. */
  373. $backend = $this->getMock('\OC_Group_Database');
  374. $backend->expects($this->exactly(1))
  375. ->method('groupExists')
  376. ->with('testgroup')
  377. ->will($this->returnValue(true));
  378. $backend->expects($this->any())
  379. ->method('InGroup')
  380. ->will($this->returnCallback(function($uid, $gid) {
  381. switch($uid) {
  382. case 'user1' : return false;
  383. case 'user2' : return true;
  384. case 'user3' : return false;
  385. case 'user33': return true;
  386. default:
  387. return null;
  388. }
  389. }));
  390. /**
  391. * @var \OC\User\Manager $userManager
  392. */
  393. $userManager = $this->getMock('\OC\User\Manager');
  394. $userBackend = $this->getMock('\OC_User_Backend');
  395. $userManager->expects($this->any())
  396. ->method('searchDisplayName')
  397. ->with('user3')
  398. ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
  399. switch($offset) {
  400. case 0 : return array('user3' => new User('user3', $userBackend),
  401. 'user33' => new User('user33', $userBackend));
  402. case 2 : return array();
  403. }
  404. }));
  405. $userManager->expects($this->any())
  406. ->method('get')
  407. ->will($this->returnCallback(function($uid) use ($userBackend) {
  408. switch($uid) {
  409. case 'user1' : return new User('user1', $userBackend);
  410. case 'user2' : return new User('user2', $userBackend);
  411. case 'user3' : return new User('user3', $userBackend);
  412. case 'user33': return new User('user33', $userBackend);
  413. default:
  414. return null;
  415. }
  416. }));
  417. $manager = new \OC\Group\Manager($userManager);
  418. $manager->addBackend($backend);
  419. $users = $manager->displayNamesInGroup('testgroup', 'user3');
  420. $this->assertEquals(1, count($users));
  421. $this->assertFalse(isset($users['user1']));
  422. $this->assertFalse(isset($users['user2']));
  423. $this->assertFalse(isset($users['user3']));
  424. $this->assertTrue(isset($users['user33']));
  425. }
  426. public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
  427. /**
  428. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  429. */
  430. $backend = $this->getMock('\OC_Group_Database');
  431. $backend->expects($this->exactly(1))
  432. ->method('groupExists')
  433. ->with('testgroup')
  434. ->will($this->returnValue(true));
  435. $backend->expects($this->any())
  436. ->method('InGroup')
  437. ->will($this->returnCallback(function($uid, $gid) {
  438. switch($uid) {
  439. case 'user1' : return false;
  440. case 'user2' : return true;
  441. case 'user3' : return false;
  442. case 'user33': return true;
  443. case 'user333': return true;
  444. default:
  445. return null;
  446. }
  447. }));
  448. /**
  449. * @var \OC\User\Manager $userManager
  450. */
  451. $userManager = $this->getMock('\OC\User\Manager');
  452. $userBackend = $this->getMock('\OC_User_Backend');
  453. $userManager->expects($this->any())
  454. ->method('searchDisplayName')
  455. ->with('user3')
  456. ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
  457. switch($offset) {
  458. case 0 : return array('user3' => new User('user3', $userBackend),
  459. 'user33' => new User('user33', $userBackend));
  460. case 2 : return array('user333' => new User('user333', $userBackend));
  461. }
  462. }));
  463. $userManager->expects($this->any())
  464. ->method('get')
  465. ->will($this->returnCallback(function($uid) use ($userBackend) {
  466. switch($uid) {
  467. case 'user1' : return new User('user1', $userBackend);
  468. case 'user2' : return new User('user2', $userBackend);
  469. case 'user3' : return new User('user3', $userBackend);
  470. case 'user33': return new User('user33', $userBackend);
  471. case 'user333': return new User('user333', $userBackend);
  472. default:
  473. return null;
  474. }
  475. }));
  476. $manager = new \OC\Group\Manager($userManager);
  477. $manager->addBackend($backend);
  478. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
  479. $this->assertEquals(1, count($users));
  480. $this->assertFalse(isset($users['user1']));
  481. $this->assertFalse(isset($users['user2']));
  482. $this->assertFalse(isset($users['user3']));
  483. $this->assertTrue(isset($users['user33']));
  484. $this->assertFalse(isset($users['user333']));
  485. }
  486. public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified() {
  487. /**
  488. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  489. */
  490. $backend = $this->getMock('\OC_Group_Database');
  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) {
  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. /**
  509. * @var \OC\User\Manager $userManager
  510. */
  511. $userManager = $this->getMock('\OC\User\Manager');
  512. $userBackend = $this->getMock('\OC_User_Backend');
  513. $userManager->expects($this->any())
  514. ->method('searchDisplayName')
  515. ->with('user3')
  516. ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
  517. switch($offset) {
  518. case 0 :
  519. return array(
  520. 'user3' => new User('user3', $userBackend),
  521. 'user33' => new User('user33', $userBackend),
  522. 'user333' => new User('user333', $userBackend)
  523. );
  524. }
  525. }));
  526. $userManager->expects($this->any())
  527. ->method('get')
  528. ->will($this->returnCallback(function($uid) use ($userBackend) {
  529. switch($uid) {
  530. case 'user1' : return new User('user1', $userBackend);
  531. case 'user2' : return new User('user2', $userBackend);
  532. case 'user3' : return new User('user3', $userBackend);
  533. case 'user33': return new User('user33', $userBackend);
  534. case 'user333': return new User('user333', $userBackend);
  535. default:
  536. return null;
  537. }
  538. }));
  539. $manager = new \OC\Group\Manager($userManager);
  540. $manager->addBackend($backend);
  541. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
  542. $this->assertEquals(1, count($users));
  543. $this->assertFalse(isset($users['user1']));
  544. $this->assertFalse(isset($users['user2']));
  545. $this->assertFalse(isset($users['user3']));
  546. $this->assertFalse(isset($users['user33']));
  547. $this->assertTrue(isset($users['user333']));
  548. }
  549. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() {
  550. /**
  551. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  552. */
  553. $backend = $this->getMock('\OC_Group_Database');
  554. $backend->expects($this->exactly(1))
  555. ->method('groupExists')
  556. ->with('testgroup')
  557. ->will($this->returnValue(true));
  558. $backend->expects($this->once())
  559. ->method('usersInGroup')
  560. ->with('testgroup', '', -1, 0)
  561. ->will($this->returnValue(array('user2', 'user33')));
  562. /**
  563. * @var \OC\User\Manager $userManager
  564. */
  565. $userManager = $this->getMock('\OC\User\Manager');
  566. $userBackend = $this->getMock('\OC_User_Backend');
  567. $userManager->expects($this->any())
  568. ->method('get')
  569. ->will($this->returnCallback(function($uid) use ($userBackend) {
  570. switch($uid) {
  571. case 'user1' : return new User('user1', $userBackend);
  572. case 'user2' : return new User('user2', $userBackend);
  573. case 'user3' : return new User('user3', $userBackend);
  574. case 'user33': return new User('user33', $userBackend);
  575. default:
  576. return null;
  577. }
  578. }));
  579. $manager = new \OC\Group\Manager($userManager);
  580. $manager->addBackend($backend);
  581. $users = $manager->displayNamesInGroup('testgroup', '');
  582. $this->assertEquals(2, count($users));
  583. $this->assertFalse(isset($users['user1']));
  584. $this->assertTrue(isset($users['user2']));
  585. $this->assertFalse(isset($users['user3']));
  586. $this->assertTrue(isset($users['user33']));
  587. }
  588. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() {
  589. /**
  590. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  591. */
  592. $backend = $this->getMock('\OC_Group_Database');
  593. $backend->expects($this->exactly(1))
  594. ->method('groupExists')
  595. ->with('testgroup')
  596. ->will($this->returnValue(true));
  597. $backend->expects($this->once())
  598. ->method('usersInGroup')
  599. ->with('testgroup', '', 1, 0)
  600. ->will($this->returnValue(array('user2')));
  601. /**
  602. * @var \OC\User\Manager $userManager
  603. */
  604. $userManager = $this->getMock('\OC\User\Manager');
  605. $userBackend = $this->getMock('\OC_User_Backend');
  606. $userManager->expects($this->any())
  607. ->method('get')
  608. ->will($this->returnCallback(function($uid) use ($userBackend) {
  609. switch($uid) {
  610. case 'user1' : return new User('user1', $userBackend);
  611. case 'user2' : return new User('user2', $userBackend);
  612. case 'user3' : return new User('user3', $userBackend);
  613. case 'user33': return new User('user33', $userBackend);
  614. default:
  615. return null;
  616. }
  617. }));
  618. $manager = new \OC\Group\Manager($userManager);
  619. $manager->addBackend($backend);
  620. $users = $manager->displayNamesInGroup('testgroup', '', 1);
  621. $this->assertEquals(1, count($users));
  622. $this->assertFalse(isset($users['user1']));
  623. $this->assertTrue(isset($users['user2']));
  624. $this->assertFalse(isset($users['user3']));
  625. $this->assertFalse(isset($users['user33']));
  626. }
  627. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
  628. /**
  629. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1
  630. */
  631. $backend = $this->getMock('\OC_Group_Database');
  632. $backend->expects($this->exactly(1))
  633. ->method('groupExists')
  634. ->with('testgroup')
  635. ->will($this->returnValue(true));
  636. $backend->expects($this->once())
  637. ->method('usersInGroup')
  638. ->with('testgroup', '', 1, 1)
  639. ->will($this->returnValue(array('user33')));
  640. /**
  641. * @var \OC\User\Manager $userManager
  642. */
  643. $userManager = $this->getMock('\OC\User\Manager');
  644. $userBackend = $this->getMock('\OC_User_Backend');
  645. $userManager->expects($this->any())
  646. ->method('get')
  647. ->will($this->returnCallback(function($uid) use ($userBackend) {
  648. switch($uid) {
  649. case 'user1' : return new User('user1', $userBackend);
  650. case 'user2' : return new User('user2', $userBackend);
  651. case 'user3' : return new User('user3', $userBackend);
  652. case 'user33': return new User('user33', $userBackend);
  653. default:
  654. return null;
  655. }
  656. }));
  657. $manager = new \OC\Group\Manager($userManager);
  658. $manager->addBackend($backend);
  659. $users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
  660. $this->assertEquals(1, count($users));
  661. $this->assertFalse(isset($users['user1']));
  662. $this->assertFalse(isset($users['user2']));
  663. $this->assertFalse(isset($users['user3']));
  664. $this->assertTrue(isset($users['user33']));
  665. }
  666. public function testGetUserGroupsWithAddUser() {
  667. /**
  668. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  669. */
  670. $backend = $this->getMock('\OC_Group_Database');
  671. $expectedGroups = array();
  672. $backend->expects($this->any())
  673. ->method('getUserGroups')
  674. ->with('user1')
  675. ->will($this->returnCallback(function () use (&$expectedGroups) {
  676. return $expectedGroups;
  677. }));
  678. $backend->expects($this->any())
  679. ->method('groupExists')
  680. ->with('group1')
  681. ->will($this->returnValue(true));
  682. $backend->expects($this->once())
  683. ->method('implementsActions')
  684. ->will($this->returnValue(true));
  685. /**
  686. * @var \OC\User\Manager $userManager
  687. */
  688. $userManager = $this->getMock('\OC\User\Manager');
  689. $manager = new \OC\Group\Manager($userManager);
  690. $manager->addBackend($backend);
  691. // prime cache
  692. $user1 = new User('user1', null);
  693. $groups = $manager->getUserGroups($user1);
  694. $this->assertEquals(array(), $groups);
  695. // add user
  696. $group = $manager->get('group1');
  697. $group->addUser($user1);
  698. $expectedGroups = array('group1');
  699. // check result
  700. $groups = $manager->getUserGroups($user1);
  701. $this->assertEquals(1, count($groups));
  702. $group1 = reset($groups);
  703. $this->assertEquals('group1', $group1->getGID());
  704. }
  705. public function testGetUserGroupsWithRemoveUser() {
  706. /**
  707. * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
  708. */
  709. $backend = $this->getMock('\OC_Group_Database');
  710. $expectedGroups = array('group1');
  711. $backend->expects($this->any())
  712. ->method('getUserGroups')
  713. ->with('user1')
  714. ->will($this->returnCallback(function () use (&$expectedGroups) {
  715. return $expectedGroups;
  716. }));
  717. $backend->expects($this->any())
  718. ->method('groupExists')
  719. ->with('group1')
  720. ->will($this->returnValue(true));
  721. $backend->expects($this->once())
  722. ->method('implementsActions')
  723. ->will($this->returnValue(true));
  724. $backend->expects($this->once())
  725. ->method('inGroup')
  726. ->will($this->returnValue(true));
  727. $backend->expects($this->once())
  728. ->method('removeFromGroup')
  729. ->will($this->returnValue(true));
  730. /**
  731. * @var \OC\User\Manager $userManager
  732. */
  733. $userManager = $this->getMock('\OC\User\Manager');
  734. $manager = new \OC\Group\Manager($userManager);
  735. $manager->addBackend($backend);
  736. // prime cache
  737. $user1 = new User('user1', null);
  738. $groups = $manager->getUserGroups($user1);
  739. $this->assertEquals(1, count($groups));
  740. $group1 = reset($groups);
  741. $this->assertEquals('group1', $group1->getGID());
  742. // remove user
  743. $group = $manager->get('group1');
  744. $group->removeUser($user1);
  745. $expectedGroups = array();
  746. // check result
  747. $groups = $manager->getUserGroups($user1);
  748. $this->assertEquals(array(), $groups);
  749. }
  750. }