ManagerTest.php 22 KB

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