1
0

ManagerTest.php 22 KB

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