ManagerTest.php 23 KB

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