ManagerTest.php 23 KB

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