ManagerTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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\User;
  9. use OC\AllConfig;
  10. use OC\User\Database;
  11. use OC\User\Manager;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\ICache;
  14. use OCP\ICacheFactory;
  15. use OCP\IConfig;
  16. use OCP\IUser;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Test\TestCase;
  19. /**
  20. * Class ManagerTest
  21. *
  22. * @group DB
  23. *
  24. * @package Test\User
  25. */
  26. class ManagerTest extends TestCase {
  27. /** @var IConfig */
  28. private $config;
  29. /** @var EventDispatcherInterface */
  30. private $oldDispatcher;
  31. /** @var IEventDispatcher */
  32. private $eventDispatcher;
  33. /** @var ICacheFactory */
  34. private $cacheFactory;
  35. /** @var ICache */
  36. private $cache;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->createMock(IConfig::class);
  40. $this->oldDispatcher = $this->createMock(EventDispatcherInterface::class);
  41. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  42. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  43. $this->cache = $this->createMock(ICache::class);
  44. $this->cacheFactory->method('createDistributed')
  45. ->with('user_backend_map')
  46. ->willReturn($this->cache);
  47. }
  48. public function testGetBackends() {
  49. $userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class);
  50. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  51. $manager->registerBackend($userDummyBackend);
  52. $this->assertEquals([$userDummyBackend], $manager->getBackends());
  53. $dummyDatabaseBackend = $this->createMock(Database::class);
  54. $manager->registerBackend($dummyDatabaseBackend);
  55. $this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends());
  56. }
  57. public function testUserExistsSingleBackendExists() {
  58. /**
  59. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  60. */
  61. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  62. $backend->expects($this->once())
  63. ->method('userExists')
  64. ->with($this->equalTo('foo'))
  65. ->willReturn(true);
  66. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  67. $manager->registerBackend($backend);
  68. $this->assertTrue($manager->userExists('foo'));
  69. }
  70. public function testUserExistsSingleBackendNotExists() {
  71. /**
  72. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  73. */
  74. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  75. $backend->expects($this->once())
  76. ->method('userExists')
  77. ->with($this->equalTo('foo'))
  78. ->willReturn(false);
  79. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  80. $manager->registerBackend($backend);
  81. $this->assertFalse($manager->userExists('foo'));
  82. }
  83. public function testUserExistsNoBackends() {
  84. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  85. $this->assertFalse($manager->userExists('foo'));
  86. }
  87. public function testUserExistsTwoBackendsSecondExists() {
  88. /**
  89. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
  90. */
  91. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  92. $backend1->expects($this->once())
  93. ->method('userExists')
  94. ->with($this->equalTo('foo'))
  95. ->willReturn(false);
  96. /**
  97. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend2
  98. */
  99. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  100. $backend2->expects($this->once())
  101. ->method('userExists')
  102. ->with($this->equalTo('foo'))
  103. ->willReturn(true);
  104. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  105. $manager->registerBackend($backend1);
  106. $manager->registerBackend($backend2);
  107. $this->assertTrue($manager->userExists('foo'));
  108. }
  109. public function testUserExistsTwoBackendsFirstExists() {
  110. /**
  111. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
  112. */
  113. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  114. $backend1->expects($this->once())
  115. ->method('userExists')
  116. ->with($this->equalTo('foo'))
  117. ->willReturn(true);
  118. /**
  119. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend2
  120. */
  121. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  122. $backend2->expects($this->never())
  123. ->method('userExists');
  124. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  125. $manager->registerBackend($backend1);
  126. $manager->registerBackend($backend2);
  127. $this->assertTrue($manager->userExists('foo'));
  128. }
  129. public function testCheckPassword() {
  130. /**
  131. * @var \OC\User\Backend | \PHPUnit\Framework\MockObject\MockObject $backend
  132. */
  133. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  134. $backend->expects($this->once())
  135. ->method('checkPassword')
  136. ->with($this->equalTo('foo'), $this->equalTo('bar'))
  137. ->willReturn(true);
  138. $backend->expects($this->any())
  139. ->method('implementsActions')
  140. ->willReturnCallback(function ($actions) {
  141. if ($actions === \OC\USER\BACKEND::CHECK_PASSWORD) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. });
  147. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  148. $manager->registerBackend($backend);
  149. $user = $manager->checkPassword('foo', 'bar');
  150. $this->assertTrue($user instanceof \OC\User\User);
  151. }
  152. public function testCheckPasswordNotSupported() {
  153. /**
  154. * @var \OC\User\Backend | \PHPUnit\Framework\MockObject\MockObject $backend
  155. */
  156. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  157. $backend->expects($this->never())
  158. ->method('checkPassword');
  159. $backend->expects($this->any())
  160. ->method('implementsActions')
  161. ->willReturn(false);
  162. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  163. $manager->registerBackend($backend);
  164. $this->assertFalse($manager->checkPassword('foo', 'bar'));
  165. }
  166. public function testGetOneBackendExists() {
  167. /**
  168. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  169. */
  170. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  171. $backend->expects($this->once())
  172. ->method('userExists')
  173. ->with($this->equalTo('foo'))
  174. ->willReturn(true);
  175. $backend->expects($this->never())
  176. ->method('loginName2UserName');
  177. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  178. $manager->registerBackend($backend);
  179. $this->assertEquals('foo', $manager->get('foo')->getUID());
  180. }
  181. public function testGetOneBackendNotExists() {
  182. /**
  183. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  184. */
  185. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  186. $backend->expects($this->once())
  187. ->method('userExists')
  188. ->with($this->equalTo('foo'))
  189. ->willReturn(false);
  190. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  191. $manager->registerBackend($backend);
  192. $this->assertEquals(null, $manager->get('foo'));
  193. }
  194. public function testGetOneBackendDoNotTranslateLoginNames() {
  195. /**
  196. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  197. */
  198. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  199. $backend->expects($this->once())
  200. ->method('userExists')
  201. ->with($this->equalTo('bLeNdEr'))
  202. ->willReturn(true);
  203. $backend->expects($this->never())
  204. ->method('loginName2UserName');
  205. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  206. $manager->registerBackend($backend);
  207. $this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID());
  208. }
  209. public function testSearchOneBackend() {
  210. /**
  211. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  212. */
  213. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  214. $backend->expects($this->once())
  215. ->method('getUsers')
  216. ->with($this->equalTo('fo'))
  217. ->willReturn(['foo', 'afoo', 'Afoo1', 'Bfoo']);
  218. $backend->expects($this->never())
  219. ->method('loginName2UserName');
  220. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  221. $manager->registerBackend($backend);
  222. $result = $manager->search('fo');
  223. $this->assertEquals(4, count($result));
  224. $this->assertEquals('afoo', array_shift($result)->getUID());
  225. $this->assertEquals('Afoo1', array_shift($result)->getUID());
  226. $this->assertEquals('Bfoo', array_shift($result)->getUID());
  227. $this->assertEquals('foo', array_shift($result)->getUID());
  228. }
  229. public function testSearchTwoBackendLimitOffset() {
  230. /**
  231. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
  232. */
  233. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  234. $backend1->expects($this->once())
  235. ->method('getUsers')
  236. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  237. ->willReturn(['foo1', 'foo2']);
  238. $backend1->expects($this->never())
  239. ->method('loginName2UserName');
  240. /**
  241. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend2
  242. */
  243. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  244. $backend2->expects($this->once())
  245. ->method('getUsers')
  246. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  247. ->willReturn(['foo3']);
  248. $backend2->expects($this->never())
  249. ->method('loginName2UserName');
  250. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  251. $manager->registerBackend($backend1);
  252. $manager->registerBackend($backend2);
  253. $result = $manager->search('fo', 3, 1);
  254. $this->assertEquals(3, count($result));
  255. $this->assertEquals('foo1', array_shift($result)->getUID());
  256. $this->assertEquals('foo2', array_shift($result)->getUID());
  257. $this->assertEquals('foo3', array_shift($result)->getUID());
  258. }
  259. public function dataCreateUserInvalid() {
  260. return [
  261. ['te?st', 'foo', 'Only the following characters are allowed in a username:'
  262. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  263. ["te\tst", '', 'Only the following characters are allowed in a username:'
  264. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  265. ["te\nst", '', 'Only the following characters are allowed in a username:'
  266. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  267. ["te\rst", '', 'Only the following characters are allowed in a username:'
  268. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  269. ["te\0st", '', 'Only the following characters are allowed in a username:'
  270. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  271. ["te\x0Bst", '', 'Only the following characters are allowed in a username:'
  272. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  273. ["te\xe2st", '', 'Only the following characters are allowed in a username:'
  274. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  275. ["te\x80st", '', 'Only the following characters are allowed in a username:'
  276. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  277. ["te\x8bst", '', 'Only the following characters are allowed in a username:'
  278. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  279. ['', 'foo', 'A valid username must be provided'],
  280. [' ', 'foo', 'A valid username must be provided'],
  281. [' test', 'foo', 'Username contains whitespace at the beginning or at the end'],
  282. ['test ', 'foo', 'Username contains whitespace at the beginning or at the end'],
  283. ['.', 'foo', 'Username must not consist of dots only'],
  284. ['..', 'foo', 'Username must not consist of dots only'],
  285. ['.test', '', 'A valid password must be provided'],
  286. ['test', '', 'A valid password must be provided'],
  287. ];
  288. }
  289. /**
  290. * @dataProvider dataCreateUserInvalid
  291. */
  292. public function testCreateUserInvalid($uid, $password, $exception) {
  293. /** @var \Test\Util\User\Dummy|\PHPUnit\Framework\MockObject\MockObject $backend */
  294. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  295. $backend->expects($this->once())
  296. ->method('implementsActions')
  297. ->with(\OC\User\Backend::CREATE_USER)
  298. ->willReturn(true);
  299. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  300. $manager->registerBackend($backend);
  301. $this->expectException(\InvalidArgumentException::class, $exception);
  302. $manager->createUser($uid, $password);
  303. }
  304. public function testCreateUserSingleBackendNotExists() {
  305. /**
  306. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  307. */
  308. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  309. $backend->expects($this->any())
  310. ->method('implementsActions')
  311. ->willReturn(true);
  312. $backend->expects($this->once())
  313. ->method('createUser')
  314. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  315. $backend->expects($this->once())
  316. ->method('userExists')
  317. ->with($this->equalTo('foo'))
  318. ->willReturn(false);
  319. $backend->expects($this->never())
  320. ->method('loginName2UserName');
  321. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  322. $manager->registerBackend($backend);
  323. $user = $manager->createUser('foo', 'bar');
  324. $this->assertEquals('foo', $user->getUID());
  325. }
  326. public function testCreateUserSingleBackendExists() {
  327. $this->expectException(\Exception::class);
  328. /**
  329. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  330. */
  331. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  332. $backend->expects($this->any())
  333. ->method('implementsActions')
  334. ->willReturn(true);
  335. $backend->expects($this->never())
  336. ->method('createUser');
  337. $backend->expects($this->once())
  338. ->method('userExists')
  339. ->with($this->equalTo('foo'))
  340. ->willReturn(true);
  341. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  342. $manager->registerBackend($backend);
  343. $manager->createUser('foo', 'bar');
  344. }
  345. public function testCreateUserSingleBackendNotSupported() {
  346. /**
  347. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  348. */
  349. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  350. $backend->expects($this->any())
  351. ->method('implementsActions')
  352. ->willReturn(false);
  353. $backend->expects($this->never())
  354. ->method('createUser');
  355. $backend->expects($this->never())
  356. ->method('userExists');
  357. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  358. $manager->registerBackend($backend);
  359. $this->assertFalse($manager->createUser('foo', 'bar'));
  360. }
  361. public function testCreateUserNoBackends() {
  362. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  363. $this->assertFalse($manager->createUser('foo', 'bar'));
  364. }
  365. public function testCreateUserFromBackendWithBackendError() {
  366. $this->expectException(\InvalidArgumentException::class);
  367. $this->expectExceptionMessage('Could not create user');
  368. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */
  369. $config = $this->createMock(IConfig::class);
  370. /** @var \Test\Util\User\Dummy|\PHPUnit\Framework\MockObject\MockObject $backend */
  371. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  372. $backend
  373. ->expects($this->once())
  374. ->method('createUser')
  375. ->with('MyUid', 'MyPassword')
  376. ->willReturn(false);
  377. $manager = new Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  378. $manager->createUserFromBackend('MyUid', 'MyPassword', $backend);
  379. }
  380. public function testCreateUserTwoBackendExists() {
  381. $this->expectException(\Exception::class);
  382. /**
  383. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
  384. */
  385. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  386. $backend1->expects($this->any())
  387. ->method('implementsActions')
  388. ->willReturn(true);
  389. $backend1->expects($this->never())
  390. ->method('createUser');
  391. $backend1->expects($this->once())
  392. ->method('userExists')
  393. ->with($this->equalTo('foo'))
  394. ->willReturn(false);
  395. /**
  396. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend2
  397. */
  398. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  399. $backend2->expects($this->any())
  400. ->method('implementsActions')
  401. ->willReturn(true);
  402. $backend2->expects($this->never())
  403. ->method('createUser');
  404. $backend2->expects($this->once())
  405. ->method('userExists')
  406. ->with($this->equalTo('foo'))
  407. ->willReturn(true);
  408. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  409. $manager->registerBackend($backend1);
  410. $manager->registerBackend($backend2);
  411. $manager->createUser('foo', 'bar');
  412. }
  413. public function testCountUsersNoBackend() {
  414. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  415. $result = $manager->countUsers();
  416. $this->assertTrue(is_array($result));
  417. $this->assertTrue(empty($result));
  418. }
  419. public function testCountUsersOneBackend() {
  420. /**
  421. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  422. */
  423. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  424. $backend->expects($this->once())
  425. ->method('countUsers')
  426. ->willReturn(7);
  427. $backend->expects($this->once())
  428. ->method('implementsActions')
  429. ->with(\OC\USER\BACKEND::COUNT_USERS)
  430. ->willReturn(true);
  431. $backend->expects($this->once())
  432. ->method('getBackendName')
  433. ->willReturn('Mock_Test_Util_User_Dummy');
  434. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  435. $manager->registerBackend($backend);
  436. $result = $manager->countUsers();
  437. $keys = array_keys($result);
  438. $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
  439. $users = array_shift($result);
  440. $this->assertEquals(7, $users);
  441. }
  442. public function testCountUsersTwoBackends() {
  443. /**
  444. * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
  445. */
  446. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  447. $backend1->expects($this->once())
  448. ->method('countUsers')
  449. ->willReturn(7);
  450. $backend1->expects($this->once())
  451. ->method('implementsActions')
  452. ->with(\OC\USER\BACKEND::COUNT_USERS)
  453. ->willReturn(true);
  454. $backend1->expects($this->once())
  455. ->method('getBackendName')
  456. ->willReturn('Mock_Test_Util_User_Dummy');
  457. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  458. $backend2->expects($this->once())
  459. ->method('countUsers')
  460. ->willReturn(16);
  461. $backend2->expects($this->once())
  462. ->method('implementsActions')
  463. ->with(\OC\USER\BACKEND::COUNT_USERS)
  464. ->willReturn(true);
  465. $backend2->expects($this->once())
  466. ->method('getBackendName')
  467. ->willReturn('Mock_Test_Util_User_Dummy');
  468. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  469. $manager->registerBackend($backend1);
  470. $manager->registerBackend($backend2);
  471. $result = $manager->countUsers();
  472. //because the backends have the same class name, only one value expected
  473. $this->assertEquals(1, count($result));
  474. $keys = array_keys($result);
  475. $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
  476. $users = array_shift($result);
  477. //users from backends shall be summed up
  478. $this->assertEquals(7 + 16, $users);
  479. }
  480. public function testCountUsersOnlyDisabled() {
  481. $manager = \OC::$server->getUserManager();
  482. // count other users in the db before adding our own
  483. $countBefore = $manager->countDisabledUsers();
  484. //Add test users
  485. $user1 = $manager->createUser('testdisabledcount1', 'testdisabledcount1');
  486. $user2 = $manager->createUser('testdisabledcount2', 'testdisabledcount2');
  487. $user2->setEnabled(false);
  488. $user3 = $manager->createUser('testdisabledcount3', 'testdisabledcount3');
  489. $user4 = $manager->createUser('testdisabledcount4', 'testdisabledcount4');
  490. $user4->setEnabled(false);
  491. $this->assertEquals($countBefore + 2, $manager->countDisabledUsers());
  492. //cleanup
  493. $user1->delete();
  494. $user2->delete();
  495. $user3->delete();
  496. $user4->delete();
  497. }
  498. public function testCountUsersOnlySeen() {
  499. $manager = \OC::$server->getUserManager();
  500. // count other users in the db before adding our own
  501. $countBefore = $manager->countUsers(true);
  502. //Add test users
  503. $user1 = $manager->createUser('testseencount1', 'testseencount1');
  504. $user1->updateLastLoginTimestamp();
  505. $user2 = $manager->createUser('testseencount2', 'testseencount2');
  506. $user2->updateLastLoginTimestamp();
  507. $user3 = $manager->createUser('testseencount3', 'testseencount3');
  508. $user4 = $manager->createUser('testseencount4', 'testseencount4');
  509. $user4->updateLastLoginTimestamp();
  510. $this->assertEquals($countBefore + 3, $manager->countUsers(true));
  511. //cleanup
  512. $user1->delete();
  513. $user2->delete();
  514. $user3->delete();
  515. $user4->delete();
  516. }
  517. public function testCallForSeenUsers() {
  518. $manager = \OC::$server->getUserManager();
  519. // count other users in the db before adding our own
  520. $count = 0;
  521. $function = function (IUser $user) use (&$count) {
  522. $count++;
  523. };
  524. $manager->callForAllUsers($function, '', true);
  525. $countBefore = $count;
  526. //Add test users
  527. $user1 = $manager->createUser('testseen1', 'testseen1');
  528. $user1->updateLastLoginTimestamp();
  529. $user2 = $manager->createUser('testseen2', 'testseen2');
  530. $user2->updateLastLoginTimestamp();
  531. $user3 = $manager->createUser('testseen3', 'testseen3');
  532. $user4 = $manager->createUser('testseen4', 'testseen4');
  533. $user4->updateLastLoginTimestamp();
  534. $count = 0;
  535. $manager->callForAllUsers($function, '', true);
  536. $this->assertEquals($countBefore + 3, $count);
  537. //cleanup
  538. $user1->delete();
  539. $user2->delete();
  540. $user3->delete();
  541. $user4->delete();
  542. }
  543. public function testDeleteUser() {
  544. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  545. $backend = new \Test\Util\User\Dummy();
  546. $manager->registerBackend($backend);
  547. $backend->createUser('foo', 'bar');
  548. $this->assertTrue($manager->userExists('foo'));
  549. $manager->get('foo')->delete();
  550. $this->assertFalse($manager->userExists('foo'));
  551. }
  552. public function testGetByEmail() {
  553. $config = $this->getMockBuilder(AllConfig::class)
  554. ->disableOriginalConstructor()
  555. ->getMock();
  556. $config
  557. ->expects($this->at(0))
  558. ->method('getUsersForUserValueCaseInsensitive')
  559. ->with('settings', 'email', 'test@example.com')
  560. ->willReturn(['uid1', 'uid99', 'uid2']);
  561. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  562. $backend->expects($this->at(0))
  563. ->method('userExists')
  564. ->with($this->equalTo('uid1'))
  565. ->willReturn(true);
  566. $backend->expects($this->at(1))
  567. ->method('userExists')
  568. ->with($this->equalTo('uid99'))
  569. ->willReturn(false);
  570. $backend->expects($this->at(2))
  571. ->method('userExists')
  572. ->with($this->equalTo('uid2'))
  573. ->willReturn(true);
  574. $manager = new \OC\User\Manager($config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  575. $manager->registerBackend($backend);
  576. $users = $manager->getByEmail('test@example.com');
  577. $this->assertCount(2, $users);
  578. $this->assertEquals('uid1', $users[0]->getUID());
  579. $this->assertEquals('uid2', $users[1]->getUID());
  580. }
  581. }