ManagerTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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 EventDispatcherInterface */
  30. private $oldDispatcher;
  31. /** @var IEventDispatcher */
  32. private $eventDispatcher;
  33. /** @var ICacheFactory */
  34. private $cacheFactory;
  35. /** @var ICache */
  36. private $cache;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->createMock(IConfig::class);
  40. $this->oldDispatcher = $this->createMock(EventDispatcherInterface::class);
  41. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  42. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  43. $this->cache = $this->createMock(ICache::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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  79. $manager->registerBackend($backend);
  80. $this->assertFalse($manager->userExists('foo'));
  81. }
  82. public function testUserExistsNoBackends() {
  83. $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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", and "_.@-\'"'],
  262. ["te\tst", '', 'Only the following characters are allowed in a username:'
  263. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  264. ["te\nst", '', 'Only the following characters are allowed in a username:'
  265. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  266. ["te\rst", '', 'Only the following characters are allowed in a username:'
  267. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  268. ["te\0st", '', 'Only the following characters are allowed in a username:'
  269. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  270. ["te\x0Bst", '', 'Only the following characters are allowed in a username:'
  271. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  272. ["te\xe2st", '', 'Only the following characters are allowed in a username:'
  273. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  274. ["te\x80st", '', 'Only the following characters are allowed in a username:'
  275. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  276. ["te\x8bst", '', 'Only the following characters are allowed in a username:'
  277. . ' "a-z", "A-Z", "0-9", 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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  362. $this->assertFalse($manager->createUser('foo', 'bar'));
  363. }
  364. public function testCreateUserFromBackendWithBackendError() {
  365. $this->expectException(\InvalidArgumentException::class);
  366. $this->expectExceptionMessage('Could not create user');
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  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 = \OC::$server->getUserManager();
  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 = \OC::$server->getUserManager();
  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 = \OC::$server->getUserManager();
  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. public function testDeleteUser() {
  543. $config = $this->getMockBuilder(AllConfig::class)
  544. ->disableOriginalConstructor()
  545. ->getMock();
  546. $config
  547. ->expects($this->any())
  548. ->method('getUserValue')
  549. ->willReturnArgument(3);
  550. $config
  551. ->expects($this->any())
  552. ->method('getAppValue')
  553. ->willReturnArgument(2);
  554. $manager = new \OC\User\Manager($config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  555. $backend = new \Test\Util\User\Dummy();
  556. $manager->registerBackend($backend);
  557. $backend->createUser('foo', 'bar');
  558. $this->assertTrue($manager->userExists('foo'));
  559. $manager->get('foo')->delete();
  560. $this->assertFalse($manager->userExists('foo'));
  561. }
  562. public function testGetByEmail() {
  563. $config = $this->getMockBuilder(AllConfig::class)
  564. ->disableOriginalConstructor()
  565. ->getMock();
  566. $config
  567. ->expects($this->once())
  568. ->method('getUsersForUserValueCaseInsensitive')
  569. ->with('settings', 'email', 'test@example.com')
  570. ->willReturn(['uid1', 'uid99', 'uid2']);
  571. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  572. $backend->expects($this->exactly(3))
  573. ->method('userExists')
  574. ->withConsecutive(
  575. [$this->equalTo('uid1')],
  576. [$this->equalTo('uid99')],
  577. [$this->equalTo('uid2')]
  578. )
  579. ->willReturnOnConsecutiveCalls(
  580. true,
  581. false,
  582. true
  583. );
  584. $manager = new \OC\User\Manager($config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
  585. $manager->registerBackend($backend);
  586. $users = $manager->getByEmail('test@example.com');
  587. $this->assertCount(2, $users);
  588. $this->assertEquals('uid1', $users[0]->getUID());
  589. $this->assertEquals('uid2', $users[1]->getUID());
  590. }
  591. }