1
0

ManagerTest.php 23 KB

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