1
0

ManagerTest.php 26 KB

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